Merge pull request #11494 from haslinghuis/dshot_gpio
[betaflight.git] / src / main / drivers / serial_pinconfig.c
blob1efd3e513f23703c60e359fd00bdb1fde778d108
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 #include <stdbool.h>
22 #include <stdint.h>
23 #include <string.h>
25 #include "platform.h"
27 #ifdef USE_UART
29 #include "build/build_config.h"
31 #include "io/serial.h"
32 #include "drivers/serial.h"
33 #include "drivers/serial_uart.h"
35 #include "pg/pg_ids.h"
37 // Backward compatibility for exisiting targets
39 // F1 targets don't explicitly define pins.
41 #ifdef STM32F1
42 #ifdef USE_UART1
43 #ifndef UART1_RX_PIN
44 #define UART1_RX_PIN PA10
45 #endif
46 #ifndef UART1_TX_PIN
47 #define UART1_TX_PIN PA9
48 #endif
49 #endif // USE_UART1
51 #ifdef USE_UART2
52 #ifndef UART2_RX_PIN
53 #define UART2_RX_PIN PA3
54 #endif
55 #ifndef UART2_TX_PIN
56 #define UART2_TX_PIN PA2
57 #endif
58 #endif // USE_UART2
59 #endif // STM32F1
61 // XXX Is there an F3 target that does not define UART pins?
63 #ifdef STM32F3
64 #ifdef USE_UART1
65 #ifndef UART1_TX_PIN
66 #define UART1_TX_PIN PA9
67 #endif
68 #ifndef UART1_RX_PIN
69 #define UART1_RX_PIN PA10
70 #endif
71 #endif
73 #ifdef USE_UART2
74 #ifndef UART2_TX_PIN
75 #define UART2_TX_PIN PD5
76 #endif
77 #ifndef UART2_RX_PIN
78 #define UART2_RX_PIN PD6
79 #endif
80 #endif
82 #ifdef USE_UART3
83 #ifndef UART3_TX_PIN
84 #define UART3_TX_PIN PB10
85 #endif
86 #ifndef UART3_RX_PIN
87 #define UART3_RX_PIN PB11
88 #endif
89 #endif
91 #ifdef USE_UART4
92 #ifndef UART4_TX_PIN
93 #define UART4_TX_PIN PC10
94 #endif
95 #ifndef UART4_RX_PIN
96 #define UART4_RX_PIN PC11
97 #endif
98 #endif
100 #ifdef USE_UART5
101 #ifndef UART5_TX_PIN
102 #define UART5_TX_PIN PC12
103 #endif
104 #ifndef UART5_RX_PIN
105 #define UART5_RX_PIN PD2
106 #endif
107 #endif
108 #endif // STM32F3
110 // Default pin (NONE).
112 #ifdef USE_UART1
113 # if !defined(UART1_RX_PIN)
114 # define UART1_RX_PIN NONE
115 # endif
116 # if !defined(UART1_TX_PIN)
117 # define UART1_TX_PIN NONE
118 # endif
119 # if !defined(INVERTER_PIN_UART1)
120 # define INVERTER_PIN_UART1 NONE
121 # endif
122 #endif
124 #ifdef USE_UART2
125 # if !defined(UART2_RX_PIN)
126 # define UART2_RX_PIN NONE
127 # endif
128 # if !defined(UART2_TX_PIN)
129 # define UART2_TX_PIN NONE
130 # endif
131 # if !defined(INVERTER_PIN_UART2)
132 # define INVERTER_PIN_UART2 NONE
133 # endif
134 #endif
136 #ifdef USE_UART3
137 # if !defined(UART3_RX_PIN)
138 # define UART3_RX_PIN NONE
139 # endif
140 # if !defined(UART3_TX_PIN)
141 # define UART3_TX_PIN NONE
142 # endif
143 # if !defined(INVERTER_PIN_UART3)
144 # define INVERTER_PIN_UART3 NONE
145 # endif
146 #endif
148 #ifdef USE_UART4
149 # if !defined(UART4_RX_PIN)
150 # define UART4_RX_PIN NONE
151 # endif
152 # if !defined(UART4_TX_PIN)
153 # define UART4_TX_PIN NONE
154 # endif
155 # if !defined(INVERTER_PIN_UART4)
156 # define INVERTER_PIN_UART4 NONE
157 # endif
158 #endif
160 #ifdef USE_UART5
161 # if !defined(UART5_RX_PIN)
162 # define UART5_RX_PIN NONE
163 # endif
164 # if !defined(UART5_TX_PIN)
165 # define UART5_TX_PIN NONE
166 # endif
167 # if !defined(INVERTER_PIN_UART5)
168 # define INVERTER_PIN_UART5 NONE
169 # endif
170 #endif
172 #ifdef USE_UART6
173 # if !defined(UART6_RX_PIN)
174 # define UART6_RX_PIN NONE
175 # endif
176 # if !defined(UART6_TX_PIN)
177 # define UART6_TX_PIN NONE
178 # endif
179 # if !defined(INVERTER_PIN_UART6)
180 # define INVERTER_PIN_UART6 NONE
181 # endif
182 #endif
184 #ifdef USE_UART7
185 # if !defined(UART7_RX_PIN)
186 # define UART7_RX_PIN NONE
187 # endif
188 # if !defined(UART7_TX_PIN)
189 # define UART7_TX_PIN NONE
190 # endif
191 # if !defined(INVERTER_PIN_UART7)
192 # define INVERTER_PIN_UART7 NONE
193 # endif
194 #endif
196 #ifdef USE_UART8
197 # if !defined(UART8_RX_PIN)
198 # define UART8_RX_PIN NONE
199 # endif
200 # if !defined(UART8_TX_PIN)
201 # define UART8_TX_PIN NONE
202 # endif
203 # if !defined(INVERTER_PIN_UART8)
204 # define INVERTER_PIN_UART8 NONE
205 # endif
206 #endif
208 #ifdef USE_UART9
209 # if !defined(UART9_RX_PIN)
210 # define UART9_RX_PIN NONE
211 # endif
212 # if !defined(UART9_TX_PIN)
213 # define UART9_TX_PIN NONE
214 # endif
215 # if !defined(INVERTER_PIN_UART9)
216 # define INVERTER_PIN_UART9 NONE
217 # endif
218 #endif
220 #ifdef USE_UART10
221 # if !defined(UART10_RX_PIN)
222 # define UART10_RX_PIN NONE
223 # endif
224 # if !defined(UART10_TX_PIN)
225 # define UART10_TX_PIN NONE
226 # endif
227 # if !defined(INVERTER_PIN_UART10)
228 # define INVERTER_PIN_UART10 NONE
229 # endif
230 #endif
232 #ifdef USE_LPUART1
233 # if !defined(LPUART1_RX_PIN)
234 # define LPUART1_RX_PIN NONE
235 # endif
236 # if !defined(LPUART1_TX_PIN)
237 # define LPUART1_TX_PIN NONE
238 # endif
239 # if !defined(INVERTER_PIN_LPUART1)
240 # define INVERTER_PIN_LPUART1 NONE
241 # endif
242 #endif
244 #ifdef USE_SOFTSERIAL1
245 # if !defined(SOFTSERIAL1_RX_PIN)
246 # define SOFTSERIAL1_RX_PIN NONE
247 # endif
248 # if !defined(SOFTSERIAL1_TX_PIN)
249 # define SOFTSERIAL1_TX_PIN NONE
250 # endif
251 #endif
253 #ifdef USE_SOFTSERIAL2
254 # if !defined(SOFTSERIAL2_RX_PIN)
255 # define SOFTSERIAL2_RX_PIN NONE
256 # endif
257 # if !defined(SOFTSERIAL2_TX_PIN)
258 # define SOFTSERIAL2_TX_PIN NONE
259 # endif
260 #endif
262 #if defined(USE_UART) || defined(USE_SOFTSERIAL1) || defined(USE_SOFTSERIAL1)
263 typedef struct serialDefaultPin_s {
264 serialPortIdentifier_e ident;
265 ioTag_t rxIO, txIO, inverterIO;
266 } serialDefaultPin_t;
268 static const serialDefaultPin_t serialDefaultPin[] = {
269 #ifdef USE_UART1
270 { SERIAL_PORT_USART1, IO_TAG(UART1_RX_PIN), IO_TAG(UART1_TX_PIN), IO_TAG(INVERTER_PIN_UART1) },
271 #endif
272 #ifdef USE_UART2
273 { SERIAL_PORT_USART2, IO_TAG(UART2_RX_PIN), IO_TAG(UART2_TX_PIN), IO_TAG(INVERTER_PIN_UART2) },
274 #endif
275 #ifdef USE_UART3
276 { SERIAL_PORT_USART3, IO_TAG(UART3_RX_PIN), IO_TAG(UART3_TX_PIN), IO_TAG(INVERTER_PIN_UART3) },
277 #endif
278 #ifdef USE_UART4
279 { SERIAL_PORT_UART4, IO_TAG(UART4_RX_PIN), IO_TAG(UART4_TX_PIN), IO_TAG(INVERTER_PIN_UART4) },
280 #endif
281 #ifdef USE_UART5
282 { SERIAL_PORT_UART5, IO_TAG(UART5_RX_PIN), IO_TAG(UART5_TX_PIN), IO_TAG(INVERTER_PIN_UART5) },
283 #endif
284 #ifdef USE_UART6
285 { SERIAL_PORT_USART6, IO_TAG(UART6_RX_PIN), IO_TAG(UART6_TX_PIN), IO_TAG(INVERTER_PIN_UART6) },
286 #endif
287 #ifdef USE_UART7
288 { SERIAL_PORT_USART7, IO_TAG(UART7_RX_PIN), IO_TAG(UART7_TX_PIN), IO_TAG(INVERTER_PIN_UART7) },
289 #endif
290 #ifdef USE_UART8
291 { SERIAL_PORT_USART8, IO_TAG(UART8_RX_PIN), IO_TAG(UART8_TX_PIN), IO_TAG(INVERTER_PIN_UART8) },
292 #endif
293 #ifdef USE_UART9
294 { SERIAL_PORT_UART9, IO_TAG(UART9_RX_PIN), IO_TAG(UART9_TX_PIN), IO_TAG(INVERTER_PIN_UART9) },
295 #endif
296 #ifdef USE_UART10
297 { SERIAL_PORT_USART10, IO_TAG(UART10_RX_PIN), IO_TAG(UART10_TX_PIN), IO_TAG(INVERTER_PIN_UART10) },
298 #endif
299 #ifdef USE_LPUART1
300 { SERIAL_PORT_LPUART1, IO_TAG(LPUART1_RX_PIN), IO_TAG(LPUART1_TX_PIN), IO_TAG(INVERTER_PIN_LPUART1) },
301 #endif
302 #ifdef USE_SOFTSERIAL1
303 { SERIAL_PORT_SOFTSERIAL1, IO_TAG(SOFTSERIAL1_RX_PIN), IO_TAG(SOFTSERIAL1_TX_PIN), IO_TAG(NONE) },
304 #endif
305 #ifdef USE_SOFTSERIAL2
306 { SERIAL_PORT_SOFTSERIAL2, IO_TAG(SOFTSERIAL2_RX_PIN), IO_TAG(SOFTSERIAL2_TX_PIN), IO_TAG(NONE) },
307 #endif
310 PG_REGISTER_WITH_RESET_FN(serialPinConfig_t, serialPinConfig, PG_SERIAL_PIN_CONFIG, 0);
312 void pgResetFn_serialPinConfig(serialPinConfig_t *serialPinConfig)
314 for (size_t index = 0 ; index < ARRAYLEN(serialDefaultPin) ; index++) {
315 const serialDefaultPin_t *defpin = &serialDefaultPin[index];
316 serialPinConfig->ioTagRx[SERIAL_PORT_IDENTIFIER_TO_INDEX(defpin->ident)] = defpin->rxIO;
317 serialPinConfig->ioTagTx[SERIAL_PORT_IDENTIFIER_TO_INDEX(defpin->ident)] = defpin->txIO;
318 serialPinConfig->ioTagInverter[SERIAL_PORT_IDENTIFIER_TO_INDEX(defpin->ident)] = defpin->inverterIO;
321 #endif
322 #endif