Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / drivers / pinio.c
blob0f5d09609726ba101a599662388a39f399c7ca48
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 <stdint.h>
23 #include "platform.h"
25 #ifdef USE_PINIO
27 #include "build/debug.h"
29 #include "pg/pinio.h"
31 #include "drivers/io.h"
33 typedef struct pinioRuntime_s {
34 IO_t io;
35 bool inverted;
36 bool state;
37 } pinioRuntime_t;
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]);
46 if (!io) {
47 continue;
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) {
57 IOHi(io);
59 IOConfigGPIO(io, IOCFG_OUT_PP);
60 break;
63 if (pinioConfig->config[i] & PINIO_CONFIG_OUT_INVERTED)
65 pinioRuntime[i].inverted = true;
66 IOHi(io);
67 pinioRuntime[i].state = true;
68 } else {
69 pinioRuntime[i].inverted = false;
70 IOLo(io);
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;
85 #endif