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)
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/>.
23 // Configuration constants
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
31 #ifndef UART_TX_BUFFER_SIZE
32 #define UART_TX_BUFFER_SIZE 256
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
40 #ifndef UART_TX_BUFFER_SIZE
41 #define UART_TX_BUFFER_SIZE 256
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
49 #ifndef UART_TX_BUFFER_SIZE
50 #ifdef USE_MSP_DISPLAYPORT
51 #define UART_TX_BUFFER_SIZE 1280
53 #define UART_TX_BUFFER_SIZE 256
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
62 #ifndef UART_TX_BUFFER_SIZE
63 #ifdef USE_MSP_DISPLAYPORT
64 #define UART_TX_BUFFER_SIZE 1280
66 #define UART_TX_BUFFER_SIZE 256
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
75 #ifndef UART_TX_BUFFER_SIZE
76 #ifdef USE_MSP_DISPLAYPORT
77 #define UART_TX_BUFFER_SIZE 1280
79 #define UART_TX_BUFFER_SIZE 256
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
88 #ifndef UART_TX_BUFFER_SIZE
89 #ifdef USE_MSP_DISPLAYPORT
90 #define UART_TX_BUFFER_SIZE 1280
92 #define UART_TX_BUFFER_SIZE 256
96 #error unknown MCU family
99 // Count number of configured UARTs
102 #define UARTDEV_COUNT_1 1
104 #define UARTDEV_COUNT_1 0
108 #define UARTDEV_COUNT_2 1
110 #define UARTDEV_COUNT_2 0
114 #define UARTDEV_COUNT_3 1
116 #define UARTDEV_COUNT_3 0
120 #define UARTDEV_COUNT_4 1
122 #define UARTDEV_COUNT_4 0
126 #define UARTDEV_COUNT_5 1
128 #define UARTDEV_COUNT_5 0
132 #define UARTDEV_COUNT_6 1
134 #define UARTDEV_COUNT_6 0
138 #define UARTDEV_COUNT_7 1
140 #define UARTDEV_COUNT_7 0
144 #define UARTDEV_COUNT_8 1
146 #define UARTDEV_COUNT_8 0
150 #define UARTDEV_COUNT_9 1
152 #define UARTDEV_COUNT_9 0
156 #define UARTDEV_COUNT_10 1
158 #define UARTDEV_COUNT_10 0
162 #define LPUARTDEV_COUNT_1 1
164 #define LPUARTDEV_COUNT_1 0
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
{
171 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
176 typedef struct uartHardware_s
{
177 UARTDevice_e device
; // XXX Not required for full allocation
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
;
189 uartPinDef_t rxPins
[UARTHARDWARE_MAX_PINS
];
190 uartPinDef_t txPins
[UARTHARDWARE_MAX_PINS
];
194 #if !defined(STM32F7)
198 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
207 volatile uint8_t *txBuffer
;
208 volatile uint8_t *rxBuffer
;
209 uint16_t txBufferSize
;
210 uint16_t rxBufferSize
;
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
{
220 const uartHardware_t
*hardware
;
223 volatile uint8_t *rxBuffer
;
224 volatile uint8_t *txBuffer
;
225 #if !(defined(STM32F1) || defined(STM32F4)) // Older CPUs don't support pin swap.
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)
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
267 UART_BUFFERS_EXTERN(1);
271 UART_BUFFERS_EXTERN(2);
275 UART_BUFFERS_EXTERN(3);
279 UART_BUFFERS_EXTERN(4);
283 UART_BUFFERS_EXTERN(5);
287 UART_BUFFERS_EXTERN(6);
291 UART_BUFFERS_EXTERN(7);
295 UART_BUFFERS_EXTERN(8);
299 UART_BUFFERS_EXTERN(9);
303 UART_BUFFERS_EXTERN(10);
307 LPUART_BUFFERS_EXTERN(1);
310 #undef UART_BUFFERS_EXTERN