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)
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/>.
27 #include "build/debug.h"
31 #include "drivers/io.h"
33 typedef struct pinioRuntime_s
{
39 static pinioRuntime_t pinioRuntime
[PINIO_COUNT
];
41 void pinioInit(const pinioConfig_t
*pinioConfig
)
43 for (int i
= 0; i
< PINIO_COUNT
; i
++) {
44 IO_t io
= IOGetByTag(pinioConfig
->ioTag
[i
]);
50 IOInit(io
, OWNER_PINIO
, RESOURCE_INDEX(i
));
52 switch (pinioConfig
->config
[i
] & PINIO_CONFIG_MODE_MASK
) {
53 case PINIO_CONFIG_MODE_OUT_PP
:
54 // Initial state after reset is input, pull-up.
55 // Avoid momentary off by presetting the output to hi.
56 if (pinioConfig
->config
[i
] & PINIO_CONFIG_OUT_INVERTED
) {
59 IOConfigGPIO(io
, IOCFG_OUT_PP
);
63 if (pinioConfig
->config
[i
] & PINIO_CONFIG_OUT_INVERTED
)
65 pinioRuntime
[i
].inverted
= true;
67 pinioRuntime
[i
].state
= true;
69 pinioRuntime
[i
].inverted
= false;
71 pinioRuntime
[i
].state
= false;
73 pinioRuntime
[i
].io
= io
;
77 void pinioSet(int index
, bool on
)
79 bool newState
= on
^ pinioRuntime
[index
].inverted
;
80 if (newState
!= pinioRuntime
[index
].state
) {
81 IOWrite(pinioRuntime
[index
].io
, newState
);
82 pinioRuntime
[index
].state
= newState
;