-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSTM_UART.h
169 lines (147 loc) · 3.16 KB
/
STM_UART.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - Modem Firmware
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (c) 2020 Jonathan Naylor, G4KLX
* Copyright (c) 2020 Geoffrey Merck, F4FXL - KC3FRA
*
*/
/**
* @file STM_UART.h
* @ingroup modem_fw
* @file STM_UART.cpp
* @ingroup modem_fw
*/
#if defined(STM32F4XX) || defined(STM32F7XX)
#if !defined(__STM_UART_H__)
#define __STM_UART_H__
#include "Defines.h"
#if defined(STM32F4XX)
#include "stm32f4xx.h"
#endif
#if defined(STM32F7XX)
#include "stm32f7xx.h"
#endif
const uint16_t BUFFER_SIZE = 2048U; //needs to be a power of 2 !
const uint16_t BUFFER_MASK = BUFFER_SIZE - 1;
// ---------------------------------------------------------------------------
// Class Declaration
// ---------------------------------------------------------------------------
/**
* @brief This class represents a FIFO buffer on a STM32 UART.
* @ingroup modem_fw
*/
class DSP_FW_API STM_UARTFIFO {
public:
/**
* @brief Initializes a new instance of the STM_UARTFIFO class.
*/
STM_UARTFIFO() :
m_head(0U),
m_tail(0U)
{
/* stub */
}
/**
* @brief
* @returns uint8_t
*/
uint8_t get()
{
return m_buffer[BUFFER_MASK & (m_tail++)];
}
/**
* @brief
* @param data
*/
void put(uint8_t data)
{
m_buffer[BUFFER_MASK & (m_head++)] = data;
}
/**
* @brief Helper to reset data values to defaults.
*/
void reset()
{
m_tail = 0U;
m_head = 0U;
}
/**
* @brief
* @returns bool
*/
bool isEmpty()
{
return m_tail == m_head;
}
/**
* @brief
* @returns bool
*/
bool isFull()
{
return ((m_head + 1U) & BUFFER_MASK) == (m_tail & BUFFER_MASK);
}
private:
volatile uint8_t m_buffer[BUFFER_SIZE];
volatile uint16_t m_head;
volatile uint16_t m_tail;
};
// ---------------------------------------------------------------------------
// Class Declaration
// ---------------------------------------------------------------------------
/**
* @brief This class represents an STM32 UART.
* @ingroup modem_fw
*/
class STM_UART {
public:
/**
* @brief Initializes a new instance of the STM_UART class.
*/
STM_UART();
/**
* @brief Initializes the UART.
* @param usart
*/
void init(USART_TypeDef* usart);
/**
* @brief
* @returns uint8_t
*/
uint8_t read();
/**
* @brief
* @param[in] data
* @param length
*/
void write(const uint8_t* data, uint16_t length);
/**
* @brief
*/
void handleIRQ();
/**
* @brief Flushes the transmit shift register.
*
* This call is blocking!
*/
void flush();
/**
* @brief
* @returns uint16_t
*/
uint16_t available();
/**
* @brief
* @returns uint16_t
*/
uint16_t availableForWrite();
private:
USART_TypeDef* m_usart;
STM_UARTFIFO m_rxFifo;
STM_UARTFIFO m_txFifo;
};
#endif // __SERIAL_PORT_H__
#endif