Removed excess trailing spaces before new lines on licenses.
[betaflight.git] / src / main / drivers / serial_uart_impl.h
blobd572dc9c2a651212f15e37eea89738346fa38df7
1 /*
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)
8 * any later version.
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/>.
21 #pragma once
23 // Configuration constants
25 #if defined(STM32F1)
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
30 #endif
31 #ifndef UART_TX_BUFFER_SIZE
32 #define UART_TX_BUFFER_SIZE 128
33 #endif
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
39 #endif
40 #ifndef UART_TX_BUFFER_SIZE
41 #define UART_TX_BUFFER_SIZE 128
42 #endif
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
48 #endif
49 #ifndef UART_TX_BUFFER_SIZE
50 #define UART_TX_BUFFER_SIZE 128
51 #endif
52 #elif defined(STM32F7)
53 #define UARTDEV_COUNT_MAX 8
54 #define UARTHARDWARE_MAX_PINS 3
55 #ifndef UART_RX_BUFFER_SIZE
56 #define UART_RX_BUFFER_SIZE 128
57 #endif
58 #ifndef UART_TX_BUFFER_SIZE
59 #define UART_TX_BUFFER_SIZE 128
60 #endif
61 #else
62 #error unknown MCU family
63 #endif
65 // Count number of configured UARTs
67 #ifdef USE_UART1
68 #define UARTDEV_COUNT_1 1
69 #else
70 #define UARTDEV_COUNT_1 0
71 #endif
73 #ifdef USE_UART2
74 #define UARTDEV_COUNT_2 1
75 #else
76 #define UARTDEV_COUNT_2 0
77 #endif
79 #ifdef USE_UART3
80 #define UARTDEV_COUNT_3 1
81 #else
82 #define UARTDEV_COUNT_3 0
83 #endif
85 #ifdef USE_UART4
86 #define UARTDEV_COUNT_4 1
87 #else
88 #define UARTDEV_COUNT_4 0
89 #endif
91 #ifdef USE_UART5
92 #define UARTDEV_COUNT_5 1
93 #else
94 #define UARTDEV_COUNT_5 0
95 #endif
97 #ifdef USE_UART6
98 #define UARTDEV_COUNT_6 1
99 #else
100 #define UARTDEV_COUNT_6 0
101 #endif
103 #ifdef USE_UART7
104 #define UARTDEV_COUNT_7 1
105 #else
106 #define UARTDEV_COUNT_7 0
107 #endif
109 #ifdef USE_UART8
110 #define UARTDEV_COUNT_8 1
111 #else
112 #define UARTDEV_COUNT_8 0
113 #endif
115 #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)
117 typedef struct uartHardware_s {
118 UARTDevice_e device; // XXX Not required for full allocation
119 USART_TypeDef* reg;
120 #if defined(STM32F1) || defined(STM32F3)
121 DMA_Channel_TypeDef *txDMAChannel;
122 DMA_Channel_TypeDef *rxDMAChannel;
123 #elif defined(STM32F4) || defined(STM32F7)
124 uint32_t DMAChannel;
125 DMA_Stream_TypeDef *txDMAStream;
126 DMA_Stream_TypeDef *rxDMAStream;
127 #endif
128 ioTag_t rxPins[UARTHARDWARE_MAX_PINS];
129 ioTag_t txPins[UARTHARDWARE_MAX_PINS];
130 #if defined(STM32F7)
131 uint32_t rcc_ahb1;
132 rccPeriphTag_t rcc_apb2;
133 rccPeriphTag_t rcc_apb1;
134 #else
135 rccPeriphTag_t rcc;
136 #endif
137 uint8_t af;
138 #if defined(STM32F7)
139 uint8_t txIrq;
140 uint8_t rxIrq;
141 #else
142 uint8_t irqn;
143 #endif
144 uint8_t txPriority;
145 uint8_t rxPriority;
146 } uartHardware_t;
148 extern const uartHardware_t uartHardware[];
150 // uartDevice_t is an actual device instance.
151 // XXX Instances are allocated for uarts defined by USE_UARTx atm.
153 typedef struct uartDevice_s {
154 uartPort_t port;
155 const uartHardware_t *hardware;
156 ioTag_t rx;
157 ioTag_t tx;
158 volatile uint8_t rxBuffer[UART_RX_BUFFER_SIZE];
159 volatile uint8_t txBuffer[UART_TX_BUFFER_SIZE];
160 } uartDevice_t;
162 extern uartDevice_t *uartDevmap[];
164 extern const struct serialPortVTable uartVTable[];
166 #ifdef USE_HAL_DRIVER
167 void uartStartTxDMA(uartPort_t *s);
168 #else
169 void uartTryStartTxDMA(uartPort_t *s);
170 #endif
172 uartPort_t *serialUART(UARTDevice_e device, uint32_t baudRate, portMode_e mode, portOptions_e options);
174 void uartIrqHandler(uartPort_t *s);
176 void uartReconfigure(uartPort_t *uartPort);