Merge remote-tracking branch 'upstream/master' into abo_RTH_sanity_fix
[inav.git] / src / main / drivers / uart_inverter.c
blob0cb015671b24f4ce2965be6874c3ea8a73eb3476
1 /*
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/>.
18 #include <stdbool.h>
19 #include <stdint.h>
21 #include "platform.h"
23 #include "drivers/io.h"
24 #include "drivers/io_impl.h"
25 #include "drivers/uart_inverter.h"
27 #if defined(USE_UART_INVERTER)
29 static void inverterPinSet(IO_t pin, bool on)
31 IOWrite(pin, on);
34 static void initInverter(ioTag_t ioTag)
36 IO_t pin = IOGetByTag(ioTag);
37 IOInit(pin, OWNER_INVERTER, RESOURCE_OUTPUT, 0);
38 IOConfigGPIO(pin, IOCFG_OUT_PP);
40 inverterPinSet(pin, false);
43 void uartInverterInit(void)
46 // UART1
47 #ifdef INVERTER_PIN_UART1_RX
48 initInverter(IO_TAG(INVERTER_PIN_UART1_RX));
49 #endif
51 #ifdef INVERTER_PIN_UART1_TX
52 initInverter(IO_TAG(INVERTER_PIN_UART1_TX));
53 #endif
55 // UART2
56 #ifdef INVERTER_PIN_UART2_RX
57 initInverter(IO_TAG(INVERTER_PIN_UART2_RX));
58 #endif
60 #ifdef INVERTER_PIN_UART2_TX
61 initInverter(IO_TAG(INVERTER_PIN_UART2_TX));
62 #endif
64 // UART3
65 #ifdef INVERTER_PIN_UART3_RX
66 initInverter(IO_TAG(INVERTER_PIN_UART3_RX));
67 #endif
69 #ifdef INVERTER_PIN_UART3_TX
70 initInverter(IO_TAG(INVERTER_PIN_UART3_TX));
71 #endif
73 // UART4
74 #ifdef INVERTER_PIN_UART4_RX
75 initInverter(IO_TAG(INVERTER_PIN_UART4_RX));
76 #endif
78 #ifdef INVERTER_PIN_UART4_TX
79 initInverter(IO_TAG(INVERTER_PIN_UART4_TX));
80 #endif
82 // UART5
83 #ifdef INVERTER_PIN_UART5_RX
84 initInverter(IO_TAG(INVERTER_PIN_UART5_RX));
85 #endif
87 #ifdef INVERTER_PIN_UART5_TX
88 initInverter(IO_TAG(INVERTER_PIN_UART5_TX));
89 #endif
91 // UART6
92 #ifdef INVERTER_PIN_UART6_RX
93 initInverter(IO_TAG(INVERTER_PIN_UART6_RX));
94 #endif
96 #ifdef INVERTER_PIN_UART6_TX
97 initInverter(IO_TAG(INVERTER_PIN_UART6_TX));
98 #endif
102 void uartInverterSet(USART_TypeDef *USARTx, uartInverterLine_e line, bool enable)
104 IO_t rx_pin = IO_NONE;
105 IO_t tx_pin = IO_NONE;
107 // UART1
108 #if defined(INVERTER_PIN_UART1_RX) || defined(INVERTER_PIN_UART1_TX)
109 if (USARTx == USART1) {
110 #if defined(INVERTER_PIN_UART1_RX)
111 rx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART1_RX));
112 #endif
113 #if defined(INVERTER_PIN_UART1_TX)
114 tx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART1_TX));
115 #endif
117 #endif
119 // UART2
120 #if defined(INVERTER_PIN_UART2_RX) || defined(INVERTER_PIN_UART2_TX)
121 if (USARTx == USART2) {
122 #if defined(INVERTER_PIN_UART2_RX)
123 rx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART2_RX));
124 #endif
125 #if defined(INVERTER_PIN_UART2_TX)
126 tx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART2_TX));
127 #endif
129 #endif
131 // UART3
132 #if defined(INVERTER_PIN_UART3_RX) || defined(INVERTER_PIN_UART3_TX)
133 if (USARTx == USART3) {
134 #if defined(INVERTER_PIN_UART3_RX)
135 rx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART3_RX));
136 #endif
137 #if defined(INVERTER_PIN_UART3_TX)
138 tx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART3_TX));
139 #endif
141 #endif
143 // UART4
144 #if defined(INVERTER_PIN_UART4_RX) || defined(INVERTER_PIN_UART4_TX)
145 if (USARTx == USART4) {
146 #if defined(INVERTER_PIN_UART4_RX)
147 rx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART4_RX));
148 #endif
149 #if defined(INVERTER_PIN_UART4_TX)
150 tx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART4_TX));
151 #endif
153 #endif
155 // UART5
156 #if defined(INVERTER_PIN_UART5_RX) || defined(INVERTER_PIN_UART5_TX)
157 if (USARTx == USART5) {
158 #if defined(INVERTER_PIN_UART5_RX)
159 rx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART5_RX));
160 #endif
161 #if defined(INVERTER_PIN_UART5_TX)
162 tx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART5_TX));
163 #endif
165 #endif
167 // UART6
168 #if defined(INVERTER_PIN_UART6_RX) || defined(INVERTER_PIN_UART6_TX)
169 if (USARTx == USART6) {
170 #if defined(INVERTER_PIN_UART6_RX)
171 rx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART6_RX));
172 #endif
173 #if defined(INVERTER_PIN_UART6_TX)
174 tx_pin = IOGetByTag(IO_TAG(INVERTER_PIN_UART6_TX));
175 #endif
177 #endif
179 // Now do the actual work
180 if (rx_pin != IO_NONE && (line & UART_INVERTER_LINE_RX)) {
181 inverterPinSet(rx_pin, enable);
183 if (tx_pin != IO_NONE && (line & UART_INVERTER_LINE_TX)) {
184 inverterPinSet(tx_pin, enable);
188 #endif