Merge remote-tracking branch 'upstream/master' into abo_RTH_sanity_fix
[inav.git] / src / main / drivers / pinio.c
blob9eb88c37820ef60a63cc1ca9c3c5abe3be062629
1 /*
2 * This file is part of Cleanflight, Betaflight and INAV
4 * Cleanflight, Betaflight and INAV are free software. You can
5 * redistribute this software and/or modify this software under
6 * the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License,
8 * or (at your option) any later version.
10 * Cleanflight, Betaflight and INAV are distributed in the hope that
11 * they will be useful, but WITHOUT ANY WARRANTY; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. 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>
23 #include "platform.h"
25 #ifdef USE_PINIO
27 #include "build/debug.h"
28 #include "common/memory.h"
29 #include "drivers/io.h"
30 #include "drivers/pinio.h"
32 /*** Hardware definitions ***/
33 const pinioHardware_t pinioHardware[] = {
34 #if defined(PINIO1_PIN)
35 #if !defined(PINIO1_FLAGS)
36 #define PINIO1_FLAGS 0
37 #endif
38 { .ioTag = IO_TAG(PINIO1_PIN), .ioMode = IOCFG_OUT_PP, .flags = PINIO1_FLAGS },
39 #endif
41 #if defined(PINIO2_PIN)
42 #if !defined(PINIO2_FLAGS)
43 #define PINIO2_FLAGS 0
44 #endif
45 { .ioTag = IO_TAG(PINIO2_PIN), .ioMode = IOCFG_OUT_PP, .flags = PINIO2_FLAGS },
46 #endif
48 #if defined(PINIO3_PIN)
49 #if !defined(PINIO3_FLAGS)
50 #define PINIO3_FLAGS 0
51 #endif
52 { .ioTag = IO_TAG(PINIO3_PIN), .ioMode = IOCFG_OUT_PP, .flags = PINIO3_FLAGS },
53 #endif
55 #if defined(PINIO4_PIN)
56 #if !defined(PINIO4_FLAGS)
57 #define PINIO4_FLAGS 0
58 #endif
59 { .ioTag = IO_TAG(PINIO4_PIN), .ioMode = IOCFG_OUT_PP, .flags = PINIO4_FLAGS },
60 #endif
63 const int pinioHardwareCount = sizeof(pinioHardware) / sizeof(pinioHardware[0]);
65 /*** Runtime configuration ***/
66 typedef struct pinioRuntime_s {
67 IO_t io;
68 bool inverted;
69 bool state;
70 } pinioRuntime_t;
72 static pinioRuntime_t pinioRuntime[PINIO_COUNT];
74 void pinioInit(void)
76 if (pinioHardwareCount == 0) {
77 return;
80 for (int i = 0; i < pinioHardwareCount; i++) {
81 IO_t io = IOGetByTag(pinioHardware[i].ioTag);
83 if (!io) {
84 continue;
87 IOInit(io, OWNER_PINIO, RESOURCE_OUTPUT, RESOURCE_INDEX(i));
88 IOConfigGPIO(io, pinioHardware[i].ioMode);
90 if (pinioHardware[i].flags & PINIO_FLAGS_INVERTED) {
91 pinioRuntime[i].inverted = true;
92 IOHi(io);
93 } else {
94 pinioRuntime[i].inverted = false;
95 IOLo(io);
98 pinioRuntime[i].io = io;
99 pinioRuntime[i].state = false;
103 void pinioSet(int index, bool on)
105 const bool newState = on ^ pinioRuntime[index].inverted;
107 if (!pinioRuntime[index].io) {
108 return;
111 if (newState != pinioRuntime[index].state) {
112 IOWrite(pinioRuntime[index].io, newState);
113 pinioRuntime[index].state = newState;
116 #endif