Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / serial_uart_hal_at32f43x.c
blob0e0f11b1c13a6ec33f5be7b361cc9b21b3f15a2b
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/>.
19 * Authors:
20 * Dominic Clifton - Serial port abstraction, Separation of common STM32 code for cleanflight, various cleanups.
21 * Hamasaki/Timecop - Initial baseflight code
23 #include <stdbool.h>
24 #include <stdint.h>
26 #include "platform.h"
28 #include "build/build_config.h"
30 #include "common/utils.h"
32 #include "drivers/uart_inverter.h"
34 #include "serial.h"
35 #include "serial_uart.h"
36 #include "serial_uart_impl.h"
38 static void usartConfigurePinInversion(uartPort_t *uartPort) {
39 #if !defined(USE_UART_INVERTER)
40 UNUSED(uartPort);
41 #else
42 bool inverted = uartPort->port.options & SERIAL_INVERTED;
44 #ifdef USE_UART_INVERTER
45 uartInverterLine_e invertedLines = UART_INVERTER_LINE_NONE;
46 if (uartPort->port.mode & MODE_RX) {
47 invertedLines |= UART_INVERTER_LINE_RX;
49 if (uartPort->port.mode & MODE_TX) {
50 invertedLines |= UART_INVERTER_LINE_TX;
52 uartInverterSet(uartPort->USARTx, invertedLines, inverted);
53 #endif
55 #endif
58 static void uartReconfigure(uartPort_t *uartPort)
60 usart_enable(uartPort->USARTx, FALSE);
61 uint32_t baud_rate = 115200;
62 usart_data_bit_num_type data_bit = USART_DATA_8BITS;
63 usart_stop_bit_num_type stop_bit = USART_STOP_1_BIT;
64 usart_parity_selection_type parity_type = USART_PARITY_EVEN;
66 baud_rate = uartPort->port.baudRate;
67 stop_bit = (uartPort->port.options & SERIAL_STOPBITS_2) ? USART_STOP_2_BIT : USART_STOP_1_BIT;
69 // according to the stm32 documentation wordlen has to be 9 for parity bits
70 // this does not seem to matter for rx but will give bad data on tx!
71 if (uartPort->port.options & SERIAL_PARITY_EVEN) {
72 data_bit = USART_DATA_9BITS;
73 } else {
74 data_bit = USART_DATA_8BITS;
76 usart_init(uartPort->USARTx, baud_rate, data_bit, stop_bit);
78 parity_type = (uartPort->port.options & SERIAL_PARITY_EVEN) ? USART_PARITY_EVEN : USART_PARITY_NONE;
79 usart_parity_selection_config(uartPort->USARTx, parity_type);
80 usart_hardware_flow_control_set (uartPort->USARTx, USART_HARDWARE_FLOW_NONE);
82 if (uartPort->port.mode & MODE_RX)
83 usart_receiver_enable(uartPort->USARTx, TRUE);
84 if (uartPort->port.mode & MODE_TX)
85 usart_transmitter_enable(uartPort->USARTx, TRUE);
87 usartConfigurePinInversion(uartPort);
89 if (uartPort->port.options & SERIAL_BIDIR)
90 usart_single_line_halfduplex_select(uartPort->USARTx, TRUE);
91 else
92 usart_single_line_halfduplex_select(uartPort->USARTx, FALSE);
94 usart_enable(uartPort->USARTx, TRUE);
97 serialPort_t *uartOpen(usart_type *USARTx, serialReceiveCallbackPtr rxCallback, void *rxCallbackData, uint32_t baudRate, portMode_t mode, portOptions_t options)
99 uartPort_t *s = NULL;
101 if (false) {
102 #ifdef USE_UART1
103 } else if (USARTx == USART1) {
104 s = serialUART1(baudRate, mode, options);
106 #endif
107 #ifdef USE_UART2
108 } else if (USARTx == USART2) {
109 s = serialUART2(baudRate, mode, options);
110 #endif
111 #ifdef USE_UART3
112 } else if (USARTx == USART3) {
113 s = serialUART3(baudRate, mode, options);
114 #endif
115 #ifdef USE_UART4
116 } else if (USARTx == UART4) {
117 s = serialUART4(baudRate, mode, options);
118 #endif
119 #ifdef USE_UART5
120 } else if (USARTx == UART5) {
121 s = serialUART5(baudRate, mode, options);
122 #endif
123 #ifdef USE_UART6
124 } else if (USARTx == USART6) {
125 s = serialUART6(baudRate, mode, options);
126 #endif
127 #ifdef USE_UART7
128 } else if (USARTx == UART7) {
129 s = serialUART7(baudRate, mode, options);
130 #endif
131 #ifdef USE_UART8
132 } else if (USARTx == UART8) {
133 s = serialUART8(baudRate, mode, options);
134 #endif
136 } else {
137 return (serialPort_t *)s;
140 // common serial initialisation code should move to serialPort::init()
141 s->port.rxBufferHead = s->port.rxBufferTail = 0;
142 s->port.txBufferHead = s->port.txBufferTail = 0;
143 // callback works for IRQ-based RX ONLY
144 s->port.rxCallback = rxCallback;
145 s->port.rxCallbackData = rxCallbackData;
146 s->port.mode = mode;
147 s->port.baudRate = baudRate;
148 s->port.options = options;
150 uartReconfigure(s);
152 if (mode & MODE_RX) {
153 usart_flag_clear(s->USARTx, USART_RDBF_FLAG);
154 usart_interrupt_enable (s->USARTx, USART_RDBF_INT, TRUE);
158 if (mode & MODE_TX) {
159 usart_interrupt_enable (s->USARTx, USART_TDBE_INT, TRUE);
162 usart_enable(s->USARTx, TRUE);
164 return (serialPort_t *)s;
167 void uartSetBaudRate(serialPort_t *instance, uint32_t baudRate)
169 uartPort_t *uartPort = (uartPort_t *)instance;
170 uartPort->port.baudRate = baudRate;
171 uartReconfigure(uartPort);
174 void uartSetMode(serialPort_t *instance, portMode_t mode)
176 uartPort_t *uartPort = (uartPort_t *)instance;
177 uartPort->port.mode = mode;
178 uartReconfigure(uartPort);
181 uint32_t uartTotalRxBytesWaiting(const serialPort_t *instance)
183 const uartPort_t *s = (const uartPort_t*)instance;
185 if (s->port.rxBufferHead >= s->port.rxBufferTail) {
186 return s->port.rxBufferHead - s->port.rxBufferTail;
187 } else {
188 return s->port.rxBufferSize + s->port.rxBufferHead - s->port.rxBufferTail;
192 uint32_t uartTotalTxBytesFree(const serialPort_t *instance)
194 const uartPort_t *s = (const uartPort_t*)instance;
196 uint32_t bytesUsed;
198 if (s->port.txBufferHead >= s->port.txBufferTail) {
199 bytesUsed = s->port.txBufferHead - s->port.txBufferTail;
200 } else {
201 bytesUsed = s->port.txBufferSize + s->port.txBufferHead - s->port.txBufferTail;
204 return (s->port.txBufferSize - 1) - bytesUsed;
207 bool isUartTransmitBufferEmpty(const serialPort_t *instance)
209 const uartPort_t *s = (const uartPort_t *)instance;
210 return s->port.txBufferTail == s->port.txBufferHead;
213 uint8_t uartRead(serialPort_t *instance)
215 uint8_t ch;
216 uartPort_t *s = (uartPort_t *)instance;
218 ch = s->port.rxBuffer[s->port.rxBufferTail];
219 if (s->port.rxBufferTail + 1 >= s->port.rxBufferSize) {
220 s->port.rxBufferTail = 0;
221 } else {
222 s->port.rxBufferTail++;
225 return ch;
228 void uartWrite(serialPort_t *instance, uint8_t ch)
230 uartPort_t *s = (uartPort_t *)instance;
231 s->port.txBuffer[s->port.txBufferHead] = ch;
232 if (s->port.txBufferHead + 1 >= s->port.txBufferSize) {
233 s->port.txBufferHead = 0;
234 } else {
235 s->port.txBufferHead++;
238 usart_interrupt_enable (s->USARTx, USART_TDBE_INT, TRUE);
242 bool isUartIdle(serialPort_t *instance)
244 uartPort_t *s = (uartPort_t *)instance;
245 if(usart_flag_get(s->USARTx, USART_IDLEF_FLAG)) {
247 uartClearIdleFlag(s);
248 return true;
249 } else {
250 return false;
254 const struct serialPortVTable uartVTable[] = {
256 .serialWrite = uartWrite,
257 .serialTotalRxWaiting = uartTotalRxBytesWaiting,
258 .serialTotalTxFree = uartTotalTxBytesFree,
259 .serialRead = uartRead,
260 .serialSetBaudRate = uartSetBaudRate,
261 .isSerialTransmitBufferEmpty = isUartTransmitBufferEmpty,
262 .setMode = uartSetMode,
263 .isConnected = NULL,
264 .writeBuf = NULL,
265 .beginWrite = NULL,
266 .endWrite = NULL,
267 .isIdle = isUartIdle,