before merging master
[inav.git] / src / main / drivers / serial_uart_hal_at32f43x.c
bloba06869ba10c5cdd52ab3413e7b2aa03247448d40
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 void uartSetOptions(serialPort_t *instance, portOptions_t options)
183 uartPort_t *uartPort = (uartPort_t *)instance;
184 uartPort->port.options = options;
185 uartReconfigure(uartPort);
188 uint32_t uartTotalRxBytesWaiting(const serialPort_t *instance)
190 const uartPort_t *s = (const uartPort_t*)instance;
192 if (s->port.rxBufferHead >= s->port.rxBufferTail) {
193 return s->port.rxBufferHead - s->port.rxBufferTail;
194 } else {
195 return s->port.rxBufferSize + s->port.rxBufferHead - s->port.rxBufferTail;
199 uint32_t uartTotalTxBytesFree(const serialPort_t *instance)
201 const uartPort_t *s = (const uartPort_t*)instance;
203 uint32_t bytesUsed;
205 if (s->port.txBufferHead >= s->port.txBufferTail) {
206 bytesUsed = s->port.txBufferHead - s->port.txBufferTail;
207 } else {
208 bytesUsed = s->port.txBufferSize + s->port.txBufferHead - s->port.txBufferTail;
211 return (s->port.txBufferSize - 1) - bytesUsed;
214 bool isUartTransmitBufferEmpty(const serialPort_t *instance)
216 const uartPort_t *s = (const uartPort_t *)instance;
217 return s->port.txBufferTail == s->port.txBufferHead;
220 uint8_t uartRead(serialPort_t *instance)
222 uint8_t ch;
223 uartPort_t *s = (uartPort_t *)instance;
225 ch = s->port.rxBuffer[s->port.rxBufferTail];
226 if (s->port.rxBufferTail + 1 >= s->port.rxBufferSize) {
227 s->port.rxBufferTail = 0;
228 } else {
229 s->port.rxBufferTail++;
232 return ch;
235 void uartWrite(serialPort_t *instance, uint8_t ch)
237 uartPort_t *s = (uartPort_t *)instance;
238 s->port.txBuffer[s->port.txBufferHead] = ch;
239 if (s->port.txBufferHead + 1 >= s->port.txBufferSize) {
240 s->port.txBufferHead = 0;
241 } else {
242 s->port.txBufferHead++;
245 usart_interrupt_enable (s->USARTx, USART_TDBE_INT, TRUE);
249 bool isUartIdle(serialPort_t *instance)
251 uartPort_t *s = (uartPort_t *)instance;
252 if(usart_flag_get(s->USARTx, USART_IDLEF_FLAG)) {
254 uartClearIdleFlag(s);
255 return true;
256 } else {
257 return false;
261 const struct serialPortVTable uartVTable[] = {
263 .serialWrite = uartWrite,
264 .serialTotalRxWaiting = uartTotalRxBytesWaiting,
265 .serialTotalTxFree = uartTotalTxBytesFree,
266 .serialRead = uartRead,
267 .serialSetBaudRate = uartSetBaudRate,
268 .isSerialTransmitBufferEmpty = isUartTransmitBufferEmpty,
269 .setMode = uartSetMode,
270 .setOptions = uartSetOptions,
271 .isConnected = NULL,
272 .writeBuf = NULL,
273 .beginWrite = NULL,
274 .endWrite = NULL,
275 .isIdle = isUartIdle,