Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / serial_uart_impl.h
blobda12bb47145385983484072fa26a74e9a0e1e8a9
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 // Configuration constants
25 #if defined(STM32F1)
26 #define UARTDEV_COUNT_MAX 3
27 #define UARTHARDWARE_MAX_PINS 3
28 #ifndef UART_RX_BUFFER_SIZE
29 #define UART_RX_BUFFER_SIZE 128
30 #endif
31 #ifndef UART_TX_BUFFER_SIZE
32 #define UART_TX_BUFFER_SIZE 256
33 #endif
34 #elif defined(STM32F3)
35 #define UARTDEV_COUNT_MAX 5
36 #define UARTHARDWARE_MAX_PINS 4
37 #ifndef UART_RX_BUFFER_SIZE
38 #define UART_RX_BUFFER_SIZE 128
39 #endif
40 #ifndef UART_TX_BUFFER_SIZE
41 #define UART_TX_BUFFER_SIZE 256
42 #endif
43 #elif defined(STM32F4)
44 #define UARTDEV_COUNT_MAX 6
45 #define UARTHARDWARE_MAX_PINS 4
46 #ifndef UART_RX_BUFFER_SIZE
47 #define UART_RX_BUFFER_SIZE 128
48 #endif
49 #ifndef UART_TX_BUFFER_SIZE
50 #ifdef USE_MSP_DISPLAYPORT
51 #define UART_TX_BUFFER_SIZE 1280
52 #else
53 #define UART_TX_BUFFER_SIZE 256
54 #endif
55 #endif
56 #elif defined(STM32F7)
57 #define UARTDEV_COUNT_MAX 8
58 #define UARTHARDWARE_MAX_PINS 4
59 #ifndef UART_RX_BUFFER_SIZE
60 #define UART_RX_BUFFER_SIZE 128
61 #endif
62 #ifndef UART_TX_BUFFER_SIZE
63 #ifdef USE_MSP_DISPLAYPORT
64 #define UART_TX_BUFFER_SIZE 1280
65 #else
66 #define UART_TX_BUFFER_SIZE 256
67 #endif
68 #endif
69 #elif defined(STM32H7)
70 #define UARTDEV_COUNT_MAX 8
71 #define UARTHARDWARE_MAX_PINS 5
72 #ifndef UART_RX_BUFFER_SIZE
73 #define UART_RX_BUFFER_SIZE 128
74 #endif
75 #ifndef UART_TX_BUFFER_SIZE
76 #ifdef USE_MSP_DISPLAYPORT
77 #define UART_TX_BUFFER_SIZE 1280
78 #else
79 #define UART_TX_BUFFER_SIZE 256
80 #endif
81 #endif
82 #elif defined(STM32G4)
83 #define UARTDEV_COUNT_MAX 9 // UART1~5 + UART9 (Implemented with LPUART1)
84 #define UARTHARDWARE_MAX_PINS 3
85 #ifndef UART_RX_BUFFER_SIZE
86 #define UART_RX_BUFFER_SIZE 128
87 #endif
88 #ifndef UART_TX_BUFFER_SIZE
89 #ifdef USE_MSP_DISPLAYPORT
90 #define UART_TX_BUFFER_SIZE 1280
91 #else
92 #define UART_TX_BUFFER_SIZE 256
93 #endif
94 #endif
95 #else
96 #error unknown MCU family
97 #endif
99 // Count number of configured UARTs
101 #ifdef USE_UART1
102 #define UARTDEV_COUNT_1 1
103 #else
104 #define UARTDEV_COUNT_1 0
105 #endif
107 #ifdef USE_UART2
108 #define UARTDEV_COUNT_2 1
109 #else
110 #define UARTDEV_COUNT_2 0
111 #endif
113 #ifdef USE_UART3
114 #define UARTDEV_COUNT_3 1
115 #else
116 #define UARTDEV_COUNT_3 0
117 #endif
119 #ifdef USE_UART4
120 #define UARTDEV_COUNT_4 1
121 #else
122 #define UARTDEV_COUNT_4 0
123 #endif
125 #ifdef USE_UART5
126 #define UARTDEV_COUNT_5 1
127 #else
128 #define UARTDEV_COUNT_5 0
129 #endif
131 #ifdef USE_UART6
132 #define UARTDEV_COUNT_6 1
133 #else
134 #define UARTDEV_COUNT_6 0
135 #endif
137 #ifdef USE_UART7
138 #define UARTDEV_COUNT_7 1
139 #else
140 #define UARTDEV_COUNT_7 0
141 #endif
143 #ifdef USE_UART8
144 #define UARTDEV_COUNT_8 1
145 #else
146 #define UARTDEV_COUNT_8 0
147 #endif
149 #ifdef USE_UART9
150 #define UARTDEV_COUNT_9 1
151 #else
152 #define UARTDEV_COUNT_9 0
153 #endif
155 #ifdef USE_UART10
156 #define UARTDEV_COUNT_10 1
157 #else
158 #define UARTDEV_COUNT_10 0
159 #endif
161 #ifdef USE_LPUART1
162 #define LPUARTDEV_COUNT_1 1
163 #else
164 #define LPUARTDEV_COUNT_1 0
165 #endif
167 #define UARTDEV_COUNT (UARTDEV_COUNT_1 + UARTDEV_COUNT_2 + UARTDEV_COUNT_3 + UARTDEV_COUNT_4 + UARTDEV_COUNT_5 + UARTDEV_COUNT_6 + UARTDEV_COUNT_7 + UARTDEV_COUNT_8 + UARTDEV_COUNT_9 + UARTDEV_COUNT_10 + LPUARTDEV_COUNT_1)
169 typedef struct uartPinDef_s {
170 ioTag_t pin;
171 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
172 uint8_t af;
173 #endif
174 } uartPinDef_t;
176 typedef struct uartHardware_s {
177 UARTDevice_e device; // XXX Not required for full allocation
178 USART_TypeDef* reg;
180 #ifdef USE_DMA
181 dmaResource_t *txDMAResource;
182 dmaResource_t *rxDMAResource;
183 // For H7 and G4, {tx|rx}DMAChannel are DMAMUX input index for peripherals (DMA_REQUEST_xxx); H7:RM0433 Table 110, G4:RM0440 Table 80.
184 // For F4 and F7, these are 32-bit channel identifiers (DMA_CHANNEL_x).
185 uint32_t txDMAChannel;
186 uint32_t rxDMAChannel;
187 #endif // USE_DMA
189 uartPinDef_t rxPins[UARTHARDWARE_MAX_PINS];
190 uartPinDef_t txPins[UARTHARDWARE_MAX_PINS];
192 rccPeriphTag_t rcc;
194 #if !defined(STM32F7)
195 uint8_t af;
196 #endif
198 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
199 uint8_t txIrq;
200 uint8_t rxIrq;
201 #else
202 uint8_t irqn;
203 #endif
204 uint8_t txPriority;
205 uint8_t rxPriority;
207 volatile uint8_t *txBuffer;
208 volatile uint8_t *rxBuffer;
209 uint16_t txBufferSize;
210 uint16_t rxBufferSize;
211 } uartHardware_t;
213 extern const uartHardware_t uartHardware[];
215 // uartDevice_t is an actual device instance.
216 // XXX Instances are allocated for uarts defined by USE_UARTx atm.
218 typedef struct uartDevice_s {
219 uartPort_t port;
220 const uartHardware_t *hardware;
221 uartPinDef_t rx;
222 uartPinDef_t tx;
223 volatile uint8_t *rxBuffer;
224 volatile uint8_t *txBuffer;
225 #if !(defined(STM32F1) || defined(STM32F4)) // Older CPUs don't support pin swap.
226 bool pinSwap;
227 #endif
228 } uartDevice_t;
230 extern uartDevice_t *uartDevmap[];
232 extern const struct serialPortVTable uartVTable[];
234 void uartTryStartTxDMA(uartPort_t *s);
236 uartPort_t *serialUART(UARTDevice_e device, uint32_t baudRate, portMode_e mode, portOptions_e options);
238 void uartIrqHandler(uartPort_t *s);
240 void uartReconfigure(uartPort_t *uartPort);
242 void uartConfigureDma(uartDevice_t *uartdev);
244 void uartDmaIrqHandler(dmaChannelDescriptor_t* descriptor);
246 #if defined(STM32F3) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
247 #define UART_REG_RXD(base) ((base)->RDR)
248 #define UART_REG_TXD(base) ((base)->TDR)
249 #elif defined(STM32F1) || defined(STM32F4)
250 #define UART_REG_RXD(base) ((base)->DR)
251 #define UART_REG_TXD(base) ((base)->DR)
252 #endif
254 #define UART_BUFFER(type, n, rxtx) type volatile uint8_t uart ## n ## rxtx ## xBuffer[UART_ ## rxtx ## X_BUFFER_SIZE]
256 #define UART_BUFFERS_EXTERN(n) \
257 UART_BUFFER(extern, n, R); \
258 UART_BUFFER(extern, n, T); struct dummy_s
260 #define LPUART_BUFFER(type, n, rxtx) type volatile uint8_t lpuart ## n ## rxtx ## xBuffer[UART_ ## rxtx ## X_BUFFER_SIZE]
262 #define LPUART_BUFFERS_EXTERN(n) \
263 LPUART_BUFFER(extern, n, R); \
264 LPUART_BUFFER(extern, n, T); struct dummy_s
266 #ifdef USE_UART1
267 UART_BUFFERS_EXTERN(1);
268 #endif
270 #ifdef USE_UART2
271 UART_BUFFERS_EXTERN(2);
272 #endif
274 #ifdef USE_UART3
275 UART_BUFFERS_EXTERN(3);
276 #endif
278 #ifdef USE_UART4
279 UART_BUFFERS_EXTERN(4);
280 #endif
282 #ifdef USE_UART5
283 UART_BUFFERS_EXTERN(5);
284 #endif
286 #ifdef USE_UART6
287 UART_BUFFERS_EXTERN(6);
288 #endif
290 #ifdef USE_UART7
291 UART_BUFFERS_EXTERN(7);
292 #endif
294 #ifdef USE_UART8
295 UART_BUFFERS_EXTERN(8);
296 #endif
298 #ifdef USE_UART9
299 UART_BUFFERS_EXTERN(9);
300 #endif
302 #ifdef USE_UART10
303 UART_BUFFERS_EXTERN(10);
304 #endif
306 #ifdef USE_LPUART1
307 LPUART_BUFFERS_EXTERN(1);
308 #endif
310 #undef UART_BUFFERS_EXTERN