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/>.
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
38 { .ioTag
= IO_TAG(PINIO1_PIN
), .ioMode
= IOCFG_OUT_PP
, .flags
= PINIO1_FLAGS
},
41 #if defined(PINIO2_PIN)
42 #if !defined(PINIO2_FLAGS)
43 #define PINIO2_FLAGS 0
45 { .ioTag
= IO_TAG(PINIO2_PIN
), .ioMode
= IOCFG_OUT_PP
, .flags
= PINIO2_FLAGS
},
48 #if defined(PINIO3_PIN)
49 #if !defined(PINIO3_FLAGS)
50 #define PINIO3_FLAGS 0
52 { .ioTag
= IO_TAG(PINIO3_PIN
), .ioMode
= IOCFG_OUT_PP
, .flags
= PINIO3_FLAGS
},
55 #if defined(PINIO4_PIN)
56 #if !defined(PINIO4_FLAGS)
57 #define PINIO4_FLAGS 0
59 { .ioTag
= IO_TAG(PINIO4_PIN
), .ioMode
= IOCFG_OUT_PP
, .flags
= PINIO4_FLAGS
},
63 const int pinioHardwareCount
= sizeof(pinioHardware
) / sizeof(pinioHardware
[0]);
65 /*** Runtime configuration ***/
66 typedef struct pinioRuntime_s
{
72 static pinioRuntime_t pinioRuntime
[PINIO_COUNT
];
76 if (pinioHardwareCount
== 0) {
80 for (int i
= 0; i
< pinioHardwareCount
; i
++) {
81 IO_t io
= IOGetByTag(pinioHardware
[i
].ioTag
);
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;
94 pinioRuntime
[i
].inverted
= false;
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
) {
111 if (newState
!= pinioRuntime
[index
].state
) {
112 IOWrite(pinioRuntime
[index
].io
, newState
);
113 pinioRuntime
[index
].state
= newState
;