Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / inverter.c
blob847b9b01b42c807ab1bc29c36d51c1eed450144a
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>
24 #include "platform.h"
26 #ifdef USE_INVERTER
28 #include "io/serial.h" // For SERIAL_PORT_IDENTIFIER_TO_INDEX
29 #include "drivers/io.h"
30 #include "drivers/serial.h"
32 #include "inverter.h"
34 static const serialPinConfig_t *pSerialPinConfig;
36 static void inverterSet(int identifier, bool on)
38 IO_t pin = IOGetByTag(pSerialPinConfig->ioTagInverter[SERIAL_PORT_IDENTIFIER_TO_INDEX(identifier)]);
40 if (pin) {
41 IOWrite(pin, on);
45 static void initInverter(int identifier)
47 int uartIndex = SERIAL_PORT_IDENTIFIER_TO_INDEX(identifier);
48 IO_t pin = IOGetByTag(pSerialPinConfig->ioTagInverter[uartIndex]);
50 if (pin) {
51 IOInit(pin, OWNER_INVERTER, RESOURCE_INDEX(uartIndex));
52 IOConfigGPIO(pin, IOCFG_OUT_PP);
54 inverterSet(identifier, false);
58 void initInverters(const serialPinConfig_t *serialPinConfigToUse)
60 pSerialPinConfig = serialPinConfigToUse;
62 #ifdef USE_UART1
63 initInverter(SERIAL_PORT_USART1);
64 #endif
66 #ifdef USE_UART2
67 initInverter(SERIAL_PORT_USART2);
68 #endif
70 #ifdef USE_UART3
71 initInverter(SERIAL_PORT_USART3);
72 #endif
74 #ifdef USE_UART4
75 initInverter(SERIAL_PORT_UART4);
76 #endif
78 #ifdef USE_UART5
79 initInverter(SERIAL_PORT_UART5);
80 #endif
82 #ifdef USE_UART6
83 initInverter(SERIAL_PORT_USART6);
84 #endif
87 void enableInverter(USART_TypeDef *USARTx, bool on)
89 int identifier = SERIAL_PORT_NONE;
91 #ifdef USE_UART1
92 if (USARTx == USART1) {
93 identifier = SERIAL_PORT_USART1;
95 #endif
97 #ifdef USE_UART2
98 if (USARTx == USART2) {
99 identifier = SERIAL_PORT_USART2;
101 #endif
103 #ifdef USE_UART3
104 if (USARTx == USART3) {
105 identifier = SERIAL_PORT_USART3;
107 #endif
109 #ifdef USE_UART4
110 if (USARTx == UART4) {
111 identifier = SERIAL_PORT_UART4;
113 #endif
115 #ifdef USE_UART5
116 if (USARTx == UART5) {
117 identifier = SERIAL_PORT_UART5;
119 #endif
121 #ifdef USE_UART6
122 if (USARTx == USART6) {
123 identifier = SERIAL_PORT_USART6;
125 #endif
127 if (identifier != SERIAL_PORT_NONE) {
128 inverterSet(identifier, on);
131 #endif // USE_INVERTER