Adding support for UART0 (#14094)
[betaflight.git] / src / main / pg / serial_uart.c
blob3bec1a3804db13c4f0a89ab2b147c06514359195
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 <stdint.h>
22 #include <stdbool.h>
24 #include "platform.h"
26 #ifdef USE_UART
28 #include "common/utils.h"
30 #include "pg/pg.h"
31 #include "pg/pg_ids.h"
32 #include "pg/serial_uart.h"
34 #include "drivers/io_types.h"
35 #include "drivers/serial.h"
36 #include "drivers/serial_uart.h"
37 #include "drivers/dma_reqmap.h"
39 // TODO(hertz@): UARTDEV_CONFIG_MAX is measured to be exactly 8, which cannot accomodate even all the UARTs below
40 PG_REGISTER_ARRAY_WITH_RESET_FN(serialUartConfig_t, UARTDEV_CONFIG_MAX, serialUartConfig, PG_SERIAL_UART_CONFIG, 0);
42 typedef struct uartDmaopt_s {
43 serialPortIdentifier_e identifier;
44 int8_t txDmaopt;
45 int8_t rxDmaopt;
46 } uartDmaopt_t;
48 static const uartDmaopt_t uartDmaopt[] = {
49 #ifdef USE_UART0
50 { SERIAL_PORT_UART0, UART0_TX_DMA_OPT, UART0_RX_DMA_OPT },
51 #endif
52 #ifdef USE_UART1
53 { SERIAL_PORT_USART1, UART1_TX_DMA_OPT, UART1_RX_DMA_OPT },
54 #endif
55 #ifdef USE_UART2
56 { SERIAL_PORT_USART2, UART2_TX_DMA_OPT, UART2_RX_DMA_OPT },
57 #endif
58 #ifdef USE_UART3
59 { SERIAL_PORT_USART3, UART3_TX_DMA_OPT, UART3_RX_DMA_OPT },
60 #endif
61 #ifdef USE_UART4
62 { SERIAL_PORT_UART4, UART4_TX_DMA_OPT, UART4_RX_DMA_OPT },
63 #endif
64 #ifdef USE_UART5
65 { SERIAL_PORT_UART5, UART5_TX_DMA_OPT, UART5_RX_DMA_OPT },
66 #endif
67 #ifdef USE_USART6
68 { SERIAL_PORT_UART6, UART6_TX_DMA_OPT, UART6_RX_DMA_OPT },
69 #endif
70 #ifdef USE_USART7
71 { SERIAL_PORT_UART7, UART7_TX_DMA_OPT, UART7_RX_DMA_OPT },
72 #endif
73 #ifdef USE_USART8
74 { SERIAL_PORT_UART8, UART8_TX_DMA_OPT, UART8_RX_DMA_OPT },
75 #endif
76 #ifdef USE_UART9
77 { SERIAL_PORT_UART9, UART9_TX_DMA_OPT, UART9_RX_DMA_OPT },
78 #endif
79 #ifdef USE_UART10
80 { SERIAL_PORT_UART10, UART10_TX_DMA_OPT, UART10_RX_DMA_OPT },
81 #endif
82 #ifdef USE_LPUART1
83 { SERIAL_PORT_LPUART1, DMA_OPT_UNUSED, DMA_OPT_UNUSED },
84 #endif
87 void pgResetFn_serialUartConfig(serialUartConfig_t *config)
89 for (unsigned i = 0; i < UARTDEV_CONFIG_MAX; i++) {
90 config[i].txDmaopt = DMA_OPT_UNUSED;
91 config[i].rxDmaopt = DMA_OPT_UNUSED;
94 for (unsigned i = 0; i < ARRAYLEN(uartDmaopt); i++) {
95 const int resourceIndex = serialResourceIndex(uartDmaopt[i].identifier);
96 if (resourceIndex >= 0 && resourceIndex < UARTDEV_CONFIG_MAX) { // hadle corrupted config gracefuly
97 config[resourceIndex].txDmaopt = uartDmaopt[i].txDmaopt;
98 config[resourceIndex].rxDmaopt = uartDmaopt[i].rxDmaopt;
102 #endif