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/>.
20 * Dominic Clifton - Serial port abstraction, Separation of common STM32 code for cleanflight, various cleanups.
21 * Hamasaki/Timecop - Initial baseflight code
28 #include "build/build_config.h"
30 #include "common/utils.h"
32 #include "drivers/uart_inverter.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)
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
);
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
;
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
);
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
)
103 } else if (USARTx
== USART1
) {
104 s
= serialUART1(baudRate
, mode
, options
);
108 } else if (USARTx
== USART2
) {
109 s
= serialUART2(baudRate
, mode
, options
);
112 } else if (USARTx
== USART3
) {
113 s
= serialUART3(baudRate
, mode
, options
);
116 } else if (USARTx
== UART4
) {
117 s
= serialUART4(baudRate
, mode
, options
);
120 } else if (USARTx
== UART5
) {
121 s
= serialUART5(baudRate
, mode
, options
);
124 } else if (USARTx
== USART6
) {
125 s
= serialUART6(baudRate
, mode
, options
);
128 } else if (USARTx
== UART7
) {
129 s
= serialUART7(baudRate
, mode
, options
);
132 } else if (USARTx
== UART8
) {
133 s
= serialUART8(baudRate
, mode
, options
);
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
;
147 s
->port
.baudRate
= baudRate
;
148 s
->port
.options
= options
;
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
;
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
;
198 if (s
->port
.txBufferHead
>= s
->port
.txBufferTail
) {
199 bytesUsed
= s
->port
.txBufferHead
- s
->port
.txBufferTail
;
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
)
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;
222 s
->port
.rxBufferTail
++;
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;
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
);
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
,
267 .isIdle
= isUartIdle
,