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/>.
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
)
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)
47 #ifdef INVERTER_PIN_UART1_RX
48 initInverter(IO_TAG(INVERTER_PIN_UART1_RX
));
51 #ifdef INVERTER_PIN_UART1_TX
52 initInverter(IO_TAG(INVERTER_PIN_UART1_TX
));
56 #ifdef INVERTER_PIN_UART2_RX
57 initInverter(IO_TAG(INVERTER_PIN_UART2_RX
));
60 #ifdef INVERTER_PIN_UART2_TX
61 initInverter(IO_TAG(INVERTER_PIN_UART2_TX
));
65 #ifdef INVERTER_PIN_UART3_RX
66 initInverter(IO_TAG(INVERTER_PIN_UART3_RX
));
69 #ifdef INVERTER_PIN_UART3_TX
70 initInverter(IO_TAG(INVERTER_PIN_UART3_TX
));
74 #ifdef INVERTER_PIN_UART4_RX
75 initInverter(IO_TAG(INVERTER_PIN_UART4_RX
));
78 #ifdef INVERTER_PIN_UART4_TX
79 initInverter(IO_TAG(INVERTER_PIN_UART4_TX
));
83 #ifdef INVERTER_PIN_UART5_RX
84 initInverter(IO_TAG(INVERTER_PIN_UART5_RX
));
87 #ifdef INVERTER_PIN_UART5_TX
88 initInverter(IO_TAG(INVERTER_PIN_UART5_TX
));
92 #ifdef INVERTER_PIN_UART6_RX
93 initInverter(IO_TAG(INVERTER_PIN_UART6_RX
));
96 #ifdef INVERTER_PIN_UART6_TX
97 initInverter(IO_TAG(INVERTER_PIN_UART6_TX
));
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
;
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
));
113 #if defined(INVERTER_PIN_UART1_TX)
114 tx_pin
= IOGetByTag(IO_TAG(INVERTER_PIN_UART1_TX
));
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
));
125 #if defined(INVERTER_PIN_UART2_TX)
126 tx_pin
= IOGetByTag(IO_TAG(INVERTER_PIN_UART2_TX
));
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
));
137 #if defined(INVERTER_PIN_UART3_TX)
138 tx_pin
= IOGetByTag(IO_TAG(INVERTER_PIN_UART3_TX
));
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
));
149 #if defined(INVERTER_PIN_UART4_TX)
150 tx_pin
= IOGetByTag(IO_TAG(INVERTER_PIN_UART4_TX
));
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
));
161 #if defined(INVERTER_PIN_UART5_TX)
162 tx_pin
= IOGetByTag(IO_TAG(INVERTER_PIN_UART5_TX
));
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
));
173 #if defined(INVERTER_PIN_UART6_TX)
174 tx_pin
= IOGetByTag(IO_TAG(INVERTER_PIN_UART6_TX
));
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
);