Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / src / main / drivers / serial_uart_at32f43x.c
blobf58e0a956aa903b08b3bf2d5ab0f6ffee0e64d6e
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdint.h>
21 #include "platform.h"
23 #include "drivers/time.h"
24 #include "drivers/io.h"
25 #include "rcc.h"
26 #include "drivers/nvic.h"
28 #include "serial.h"
29 #include "serial_uart.h"
30 #include "serial_uart_impl.h"
32 #define UART_RX_BUFFER_SIZE UART1_RX_BUFFER_SIZE
33 #define UART_TX_BUFFER_SIZE UART1_RX_BUFFER_SIZE
35 typedef struct uartDevice_s {
36 usart_type* dev;
37 uartPort_t port;
38 ioTag_t rx;
39 uint8_t rx_af;
40 ioTag_t tx;
41 uint8_t tx_af;
42 volatile uint8_t rxBuffer[UART_RX_BUFFER_SIZE];
43 volatile uint8_t txBuffer[UART_TX_BUFFER_SIZE];
44 uint32_t rcc_ahb1;
45 rccPeriphTag_t rcc_apb2;
46 rccPeriphTag_t rcc_apb1;
47 uint8_t irq;
48 uint32_t irqPriority;
49 bool pinSwap;
50 } uartDevice_t;
52 #ifdef USE_UART1
53 static uartDevice_t uart1 = {
54 .dev = USART1,
55 .rx = IO_TAG(UART1_RX_PIN),
56 .tx = IO_TAG(UART1_TX_PIN),
57 #if defined(UART1_RX_AF)
58 .rx_af = CONCAT(GPIO_MUX_, UART1_RX_AF),
59 #elif defined(UART1_AF)
60 .rx_af = CONCAT(GPIO_MUX_, UART1_AF),
61 #else
62 .rx_af = GPIO_MUX_7,
63 #endif
64 #if defined(UART1_TX_AF)
65 .tx_af = CONCAT(GPIO_MUX_, UART1_TX_AF),
66 #elif defined(UART1_AF)
67 .tx_af = CONCAT(GPIO_MUX_, UART1_AF),
68 #else
69 .tx_af = GPIO_MUX_7,
70 #endif
71 #ifdef UART1_AHB1_PERIPHERALS
72 .rcc_ahb1 = UART1_AHB1_PERIPHERALS,
73 #endif
74 .rcc_apb2 = RCC_APB2(USART1),
75 .irq = USART1_IRQn,
76 .irqPriority = NVIC_PRIO_SERIALUART,
77 #ifdef USE_UART1_PIN_SWAP
78 .pinSwap = true,
79 #else
80 .pinSwap = false,
81 #endif
83 #endif
85 #ifdef USE_UART2
86 static uartDevice_t uart2 =
88 .dev = USART2,
89 .rx = IO_TAG(UART2_RX_PIN),
90 .tx = IO_TAG(UART2_TX_PIN),
91 #if defined(UART2_RX_AF)
92 .rx_af = CONCAT(GPIO_MUX_, UART2_RX_AF),
93 #elif defined(UART2_AF)
94 .rx_af = CONCAT(GPIO_MUX_, UART2_AF),
95 #else
96 .rx_af = GPIO_MUX_7,
97 #endif
98 #if defined(UART2_TX_AF)
99 .tx_af = CONCAT(GPIO_MUX_, UART2_TX_AF),
100 #elif defined(UART2_AF)
101 .tx_af = CONCAT(GPIO_MUX_, UART2_AF),
102 #else
103 .tx_af = GPIO_MUX_7,
104 #endif
105 #ifdef UART2_AHB1_PERIPHERALS
106 .rcc_ahb1 = UART2_AHB1_PERIPHERALS,
107 #endif
108 .rcc_apb1 = RCC_APB1(USART2),
109 .irq = USART2_IRQn,
110 .irqPriority = NVIC_PRIO_SERIALUART,
111 #ifdef USE_UART2_PIN_SWAP
112 .pinSwap = true,
113 #else
114 .pinSwap = false,
115 #endif
117 #endif
119 #ifdef USE_UART3
120 static uartDevice_t uart3 =
122 .dev = USART3,
123 .rx = IO_TAG(UART3_RX_PIN),
124 .tx = IO_TAG(UART3_TX_PIN),
125 #if defined(UART3_RX_AF)
126 .rx_af = CONCAT(GPIO_MUX_, UART3_RX_AF),
127 #elif defined(UART3_AF)
128 .rx_af = CONCAT(GPIO_MUX_, UART3_AF),
129 #else
130 .rx_af = GPIO_MUX_7,
131 #endif
132 #if defined(UART3_TX_AF)
133 .tx_af = CONCAT(GPIO_MUX_, UART3_TX_AF),
134 #elif defined(UART3_AF)
135 .tx_af = CONCAT(GPIO_MUX_, UART3_AF),
136 #else
137 .tx_af = GPIO_MUX_7,
138 #endif
139 #ifdef UART3_AHB1_PERIPHERALS
140 .rcc_ahb1 = UART3_AHB1_PERIPHERALS,
141 #endif
142 .rcc_apb1 = RCC_APB1(USART3),
143 .irq = USART3_IRQn,
144 .irqPriority = NVIC_PRIO_SERIALUART,
145 #ifdef USE_UART3_PIN_SWAP
146 .pinSwap = true,
147 #else
148 .pinSwap = false,
149 #endif
151 #endif
153 #ifdef USE_UART4
154 static uartDevice_t uart4 =
156 .dev = UART4,
157 .rx = IO_TAG(UART4_RX_PIN),
158 .tx = IO_TAG(UART4_TX_PIN),
159 #if defined(UART4_RX_AF)
160 .rx_af = CONCAT(GPIO_MUX_, UART4_RX_AF),
161 #elif defined(UART4_AF)
162 .rx_af = CONCAT(GPIO_MUX_, UART4_AF),
163 #else
164 .rx_af = GPIO_MUX_8,
165 #endif
166 #if defined(UART4_TX_AF)
167 .tx_af = CONCAT(GPIO_MUX_, UART4_TX_AF),
168 #elif defined(UART4_AF)
169 .tx_af = CONCAT(GPIO_MUX_, UART4_AF),
170 #else
171 .tx_af = GPIO_MUX_8,
172 #endif
173 #ifdef UART4_AHB1_PERIPHERALS
174 .rcc_ahb1 = UART4_AHB1_PERIPHERALS,
175 #endif
176 .rcc_apb1 = RCC_APB1(UART4),
177 .irq = UART4_IRQn,
178 .irqPriority = NVIC_PRIO_SERIALUART,
179 #ifdef USE_UART4_PIN_SWAP
180 .pinSwap = true,
181 #else
182 .pinSwap = false,
183 #endif
185 #endif
187 #ifdef USE_UART5
188 static uartDevice_t uart5 =
190 .dev = UART5,
191 .rx = IO_TAG(UART5_RX_PIN),
192 .tx = IO_TAG(UART5_TX_PIN),
193 #if defined(UART5_RX_AF)
194 .rx_af = CONCAT(GPIO_MUX_, UART5_RX_AF),
195 #elif defined(UART5_AF)
196 .rx_af = CONCAT(GPIO_MUX_, UART5_AF),
197 #else
198 .rx_af = GPIO_MUX_8,
199 #endif
200 #if defined(UART5_TX_AF)
201 .tx_af = CONCAT(GPIO_MUX_, UART5_TX_AF),
202 #elif defined(UART5_AF)
203 .tx_af = CONCAT(GPIO_MUX_, UART5_AF),
204 #else
205 .tx_af = GPIO_MUX_8,
206 #endif
207 #ifdef UART5_AHB1_PERIPHERALS
208 .rcc_ahb1 = UART5_AHB1_PERIPHERALS,
209 #endif
210 .rcc_apb1 = RCC_APB1(UART5),
211 .irq = UART5_IRQn,
212 .irqPriority = NVIC_PRIO_SERIALUART,
213 #ifdef USE_UART5_PIN_SWAP
214 .pinSwap = true,
215 #else
216 .pinSwap = false,
217 #endif
219 #endif
221 #ifdef USE_UART6
222 static uartDevice_t uart6 =
224 .dev = USART6,
225 .rx = IO_TAG(UART6_RX_PIN),
226 .tx = IO_TAG(UART6_TX_PIN),
227 #if defined(UART6_RX_AF)
228 .rx_af = CONCAT(GPIO_MUX_, UART6_RX_AF),
229 #elif defined(UART6_AF)
230 .rx_af = CONCAT(GPIO_MUX_, UART6_AF),
231 #else
232 .rx_af = GPIO_MUX_8,
233 #endif
234 #if defined(UART6_TX_AF)
235 .tx_af = CONCAT(GPIO_MUX_, UART6_TX_AF),
236 #elif defined(UART6_AF)
237 .tx_af = CONCAT(GPIO_MUX_, UART6_AF),
238 #else
239 .tx_af = GPIO_MUX_8,
240 #endif
241 #ifdef UART6_AHB1_PERIPHERALS
242 .rcc_ahb1 = UART6_AHB1_PERIPHERALS,
243 #endif
244 .rcc_apb2 = RCC_APB2(USART6),
245 .irq = USART6_IRQn,
246 .irqPriority = NVIC_PRIO_SERIALUART,
247 #ifdef USE_UART6_PIN_SWAP
248 .pinSwap = true,
249 #else
250 .pinSwap = false,
251 #endif
253 #endif
255 #ifdef USE_UART7
256 static uartDevice_t uart7 =
258 .dev = UART7,
259 .rx = IO_TAG(UART7_RX_PIN),
260 .tx = IO_TAG(UART7_TX_PIN),
261 #if defined(UART7_RX_AF)
262 .rx_af = CONCAT(GPIO_MUX_, UART7_RX_AF),
263 #elif defined(UART7_AF)
264 .rx_af = CONCAT(GPIO_MUX_, UART7_AF),
265 #else
266 .rx_af = GPIO_MUX_8,
267 #endif
268 #if defined(UART7_TX_AF)
269 .tx_af = CONCAT(GPIO_MUX_, UART7_TX_AF),
270 #elif defined(UART7_AF)
271 .tx_af = CONCAT(GPIO_MUX_, UART7_AF),
272 #else
273 .tx_af = GPIO_MUX_8,
274 #endif
275 .rcc_apb1 = RCC_APB1(UART7),
276 .irq = UART7_IRQn,
277 .irqPriority = NVIC_PRIO_SERIALUART,
278 #ifdef USE_UART7_PIN_SWAP
279 .pinSwap = true,
280 #else
281 .pinSwap = false,
282 #endif
284 #endif
286 #ifdef USE_UART8
287 static uartDevice_t uart8 =
289 .dev = UART8,
290 .rx = IO_TAG(UART8_RX_PIN),
291 .tx = IO_TAG(UART8_TX_PIN),
292 #if defined(UART8_RX_AF)
293 .rx_af = CONCAT(GPIO_MUX_, UART8_RX_AF),
294 #elif defined(UART8_AF)
295 .rx_af = CONCAT(GPIO_MUX_, UART8_AF),
296 #else
297 .rx_af = GPIO_MUX_8,
298 #endif
299 #if defined(UART8_TX_AF)
300 .tx_af = CONCAT(GPIO_MUX_, UART8_TX_AF),
301 #elif defined(UART8_AF)
302 .tx_af = CONCAT(GPIO_MUX_, UART8_AF),
303 #else
304 .tx_af = GPIO_MUX_8,
305 #endif
306 .rcc_apb1 = RCC_APB1(UART8),
307 .irq = UART8_IRQn,
308 .irqPriority = NVIC_PRIO_SERIALUART,
309 #ifdef USE_UART8_PIN_SWAP
310 .pinSwap = true,
311 #else
312 .pinSwap = false,
313 #endif
315 #endif
317 static uartDevice_t* uartHardwareMap[] = {
318 #ifdef USE_UART1
319 &uart1,
320 #else
321 NULL,
322 #endif
323 #ifdef USE_UART2
324 &uart2,
325 #else
326 NULL,
327 #endif
328 #ifdef USE_UART3
329 &uart3,
330 #else
331 NULL,
332 #endif
333 #ifdef USE_UART4
334 &uart4,
335 #else
336 NULL,
337 #endif
338 #ifdef USE_UART5
339 &uart5,
340 #else
341 NULL,
342 #endif
343 #ifdef USE_UART6
344 &uart6,
345 #else
346 NULL,
347 #endif
348 #ifdef USE_UART7
349 &uart7,
350 #else
351 NULL,
352 #endif
353 #ifdef USE_UART8
354 &uart8,
355 #else
356 NULL,
357 #endif
360 void uartIrqHandler(uartPort_t *s)
362 if (usart_flag_get(s->USARTx, USART_RDBF_FLAG) == SET) {
363 if (s->port.rxCallback) {
364 s->port.rxCallback(s->USARTx->dt, s->port.rxCallbackData);
365 } else {
366 s->port.rxBuffer[s->port.rxBufferHead] = s->USARTx->dt;
367 s->port.rxBufferHead = (s->port.rxBufferHead + 1) % s->port.rxBufferSize;
371 if (usart_flag_get(s->USARTx, USART_TDBE_FLAG) == SET) {
372 if (s->port.txBufferTail != s->port.txBufferHead) {
373 usart_data_transmit(s->USARTx, s->port.txBuffer[s->port.txBufferTail]);
374 s->port.txBufferTail = (s->port.txBufferTail + 1) % s->port.txBufferSize;
375 } else {
376 usart_interrupt_enable (s->USARTx, USART_TDBE_INT, FALSE);
380 if (usart_flag_get(s->USARTx, USART_ROERR_FLAG) == SET)
382 usart_flag_clear(s->USARTx, USART_ROERR_FLAG);
386 void uartGetPortPins(UARTDevice_e device, serialPortPins_t * pins)
388 uartDevice_t *uart = uartHardwareMap[device];
390 if (uart) {
391 pins->txPin = uart->tx;
392 pins->rxPin = uart->rx;
394 else {
395 pins->txPin = IO_TAG(NONE);
396 pins->rxPin = IO_TAG(NONE);
400 void uartClearIdleFlag(uartPort_t *s)
402 (void) s->USARTx->sts;
403 (void) s->USARTx->dt;
406 static uartDevice_t *uartFindDevice(uartPort_t *uartPort)
408 for (uint32_t i = 0; i < UARTDEV_MAX; i++) {
409 uartDevice_t *pDevice = uartHardwareMap[i];
411 if (pDevice->dev == uartPort->USARTx) {
412 return pDevice;
415 return NULL;
418 void uartConfigurePinSwap(uartPort_t *uartPort)
420 uartDevice_t *uartDevice = uartFindDevice(uartPort);
421 if (!uartDevice) {
422 return;
425 if (uartDevice->pinSwap) {
426 usart_transmit_receive_pin_swap(uartPort->USARTx, TRUE);
430 uartPort_t *serialUART(UARTDevice_e device, uint32_t baudRate, portMode_t mode, portOptions_t options)
432 uartPort_t *s;
434 uartDevice_t *uart = uartHardwareMap[device];
435 if (!uart) return NULL;
437 s = &(uart->port);
438 s->port.vTable = uartVTable;
440 s->port.baudRate = baudRate;
442 s->port.rxBuffer = uart->rxBuffer;
443 s->port.txBuffer = uart->txBuffer;
444 s->port.rxBufferSize = sizeof(uart->rxBuffer);
445 s->port.txBufferSize = sizeof(uart->txBuffer);
447 s->USARTx = uart->dev;
449 IO_t tx = IOGetByTag(uart->tx);
450 IO_t rx = IOGetByTag(uart->rx);
452 if (uart->rcc_apb2)
453 RCC_ClockCmd(uart->rcc_apb2, ENABLE);
455 if (uart->rcc_apb1)
456 RCC_ClockCmd(uart->rcc_apb1, ENABLE);
458 if (uart->rcc_ahb1)
459 RCC_ClockCmd(uart->rcc_apb1, ENABLE);
461 if (options & SERIAL_BIDIR) {
462 IOInit(tx, OWNER_SERIAL, RESOURCE_UART_TXRX, RESOURCE_INDEX(device));
463 if (options & SERIAL_BIDIR_PP) {
464 IOConfigGPIOAF(tx, IOCFG_AF_PP, uart->tx_af);
465 } else {
466 IOConfigGPIOAF(tx,
467 (options & SERIAL_BIDIR_NOPULL) ? IOCFG_AF_OD : IOCFG_AF_OD_UP,
468 uart->tx_af);
471 else {
472 if (mode & MODE_TX) {
473 IOInit(tx, OWNER_SERIAL, RESOURCE_UART_TX, RESOURCE_INDEX(device));
474 IOConfigGPIOAF(tx, IOCFG_AF_PP, uart->rx_af);
477 if (mode & MODE_RX) {
478 IOInit(rx, OWNER_SERIAL, RESOURCE_UART_RX, RESOURCE_INDEX(device));
479 IOConfigGPIOAF(rx, IOCFG_AF_PP, uart->rx_af);
483 nvic_irq_enable(uart->irq, uart->irqPriority, 0);
486 return s;
489 #ifdef USE_UART1
490 uartPort_t *serialUART1(uint32_t baudRate, portMode_t mode, portOptions_t options)
492 return serialUART(UARTDEV_1, baudRate, mode, options);
495 // USART1 Rx/Tx IRQ Handler
496 void USART1_IRQHandler(void)
498 uartPort_t *s = &(uartHardwareMap[UARTDEV_1]->port);
499 uartIrqHandler(s);
502 #endif
504 #ifdef USE_UART2
505 // USART2 (RX + TX by IRQ)
506 uartPort_t *serialUART2(uint32_t baudRate, portMode_t mode, portOptions_t options)
508 return serialUART(UARTDEV_2, baudRate, mode, options);
511 void USART2_IRQHandler(void)
513 uartPort_t *s = &(uartHardwareMap[UARTDEV_2]->port);
514 uartIrqHandler(s);
516 #endif
518 #ifdef USE_UART3
519 // USART3
520 uartPort_t *serialUART3(uint32_t baudRate, portMode_t mode, portOptions_t options)
522 return serialUART(UARTDEV_3, baudRate, mode, options);
525 void USART3_IRQHandler(void)
527 uartPort_t *s = &(uartHardwareMap[UARTDEV_3]->port);
528 uartIrqHandler(s);
530 #endif
532 #ifdef USE_UART4
533 // USART4
534 uartPort_t *serialUART4(uint32_t baudRate, portMode_t mode, portOptions_t options)
536 return serialUART(UARTDEV_4, baudRate, mode, options);
539 void UART4_IRQHandler(void)
541 uartPort_t *s = &(uartHardwareMap[UARTDEV_4]->port);
542 uartIrqHandler(s);
544 #endif
546 #ifdef USE_UART5
547 // USART5
548 uartPort_t *serialUART5(uint32_t baudRate, portMode_t mode, portOptions_t options)
550 return serialUART(UARTDEV_5, baudRate, mode, options);
553 void UART5_IRQHandler(void)
555 uartPort_t *s = &(uartHardwareMap[UARTDEV_5]->port);
556 uartIrqHandler(s);
558 #endif
560 #ifdef USE_UART6
561 // USART6
562 uartPort_t *serialUART6(uint32_t baudRate, portMode_t mode, portOptions_t options)
564 return serialUART(UARTDEV_6, baudRate, mode, options);
567 void USART6_IRQHandler(void)
569 uartPort_t *s = &(uartHardwareMap[UARTDEV_6]->port);
570 uartIrqHandler(s);
572 #endif
574 #ifdef USE_UART7
575 uartPort_t *serialUART7(uint32_t baudRate, portMode_t mode, portOptions_t options)
577 return serialUART(UARTDEV_7, baudRate, mode, options);
580 // UART7 Rx/Tx IRQ Handler
581 void UART7_IRQHandler(void)
583 uartPort_t *s = &(uartHardwareMap[UARTDEV_7]->port);
584 uartIrqHandler(s);
586 #endif
588 #ifdef USE_UART8
589 uartPort_t *serialUART8(uint32_t baudRate, portMode_t mode, portOptions_t options)
591 return serialUART(UARTDEV_8, baudRate, mode, options);
594 // UART8 Rx/Tx IRQ Handler
595 void UART8_IRQHandler(void)
597 uartPort_t *s = &(uartHardwareMap[UARTDEV_8]->port);
598 uartIrqHandler(s);
600 #endif