Refactor missing prototypes 2 (#14170)
[betaflight.git] / src / main / drivers / io.c
blob7c8dc122035ecb3dc5216d0775dafa4c77d7ecc0
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 "platform.h"
23 #include "drivers/io.h"
24 #include "drivers/io_impl.h"
25 #include "drivers/rcc.h"
27 #include "common/utils.h"
29 // io ports defs are stored in array by index now
30 struct ioPortDef_s {
31 rccPeriphTag_t rcc;
34 ioRec_t* IO_Rec(IO_t io)
36 return io;
39 GPIO_TypeDef* IO_GPIO(IO_t io)
41 const ioRec_t *ioRec = IO_Rec(io);
42 return ioRec->gpio;
45 uint16_t IO_Pin(IO_t io)
47 const ioRec_t *ioRec = IO_Rec(io);
48 return ioRec->pin;
51 #if defined(STM32F4) || defined(APM32F4)
52 int IO_EXTI_PortSourceGPIO(IO_t io)
54 return IO_GPIOPortIdx(io);
56 #endif
58 int IO_GPIO_PortSource(IO_t io)
60 return IO_GPIOPortIdx(io);
63 // zero based pin index
64 int IO_GPIOPinIdx(IO_t io)
66 if (!io) {
67 return -1;
69 return 31 - __builtin_clz(IO_Pin(io));
72 #if defined(STM32F4) || defined(APM32F4)
73 int IO_EXTI_PinSource(IO_t io)
75 return IO_GPIOPinIdx(io);
77 #endif
79 int IO_GPIO_PinSource(IO_t io)
81 return IO_GPIOPinIdx(io);
84 // claim IO pin, set owner and resources
85 void IOInit(IO_t io, resourceOwner_e owner, uint8_t index)
87 if (!io) {
88 return;
90 ioRec_t *ioRec = IO_Rec(io);
91 ioRec->owner = owner;
92 ioRec->index = index;
95 void IORelease(IO_t io)
97 if (!io) {
98 return;
100 ioRec_t *ioRec = IO_Rec(io);
101 ioRec->owner = OWNER_FREE;
104 resourceOwner_e IOGetOwner(IO_t io)
106 if (!io) {
107 return OWNER_FREE;
109 const ioRec_t *ioRec = IO_Rec(io);
110 return ioRec->owner;
113 bool IOIsFreeOrPreinit(IO_t io)
115 resourceOwner_e owner = IOGetOwner(io);
117 if (owner == OWNER_FREE || owner == OWNER_PREINIT) {
118 return true;
121 return false;
124 #if DEFIO_IO_USED_COUNT
125 ioRec_t ioRecs[DEFIO_IO_USED_COUNT];
126 #else
127 // Avoid -Wpedantic warning
128 ioRec_t ioRecs[1];
129 #endif
131 void IOTraversePins(IOTraverseFuncPtr_t fnPtr)
133 for (int i = 0; i < DEFIO_IO_USED_COUNT; i++) {
134 fnPtr(&ioRecs[i]);