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/>.
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
34 ioRec_t
* IO_Rec(IO_t io
)
39 GPIO_TypeDef
* IO_GPIO(IO_t io
)
41 const ioRec_t
*ioRec
= IO_Rec(io
);
45 uint16_t IO_Pin(IO_t io
)
47 const ioRec_t
*ioRec
= IO_Rec(io
);
51 #if defined(STM32F4) || defined(APM32F4)
52 int IO_EXTI_PortSourceGPIO(IO_t io
)
54 return IO_GPIOPortIdx(io
);
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
)
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
);
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
)
90 ioRec_t
*ioRec
= IO_Rec(io
);
95 void IORelease(IO_t io
)
100 ioRec_t
*ioRec
= IO_Rec(io
);
101 ioRec
->owner
= OWNER_FREE
;
104 resourceOwner_e
IOGetOwner(IO_t io
)
109 const ioRec_t
*ioRec
= IO_Rec(io
);
113 bool IOIsFreeOrPreinit(IO_t io
)
115 resourceOwner_e owner
= IOGetOwner(io
);
117 if (owner
== OWNER_FREE
|| owner
== OWNER_PREINIT
) {
124 #if DEFIO_IO_USED_COUNT
125 ioRec_t ioRecs
[DEFIO_IO_USED_COUNT
];
127 // Avoid -Wpedantic warning
131 void IOTraversePins(IOTraverseFuncPtr_t fnPtr
)
133 for (int i
= 0; i
< DEFIO_IO_USED_COUNT
; i
++) {