diff --git a/src/main/drivers/io.c b/src/main/drivers/io.c index 7c8dc12203..7c766d9430 100644 --- a/src/main/drivers/io.c +++ b/src/main/drivers/io.c @@ -26,10 +26,7 @@ #include "common/utils.h" -// io ports defs are stored in array by index now -struct ioPortDef_s { - rccPeriphTag_t rcc; -}; +// io ports defs are stored by index in array ioRecs of ioRec_t ioRec_t* IO_Rec(IO_t io) { @@ -48,39 +45,6 @@ uint16_t IO_Pin(IO_t io) return ioRec->pin; } -#if defined(STM32F4) || defined(APM32F4) -int IO_EXTI_PortSourceGPIO(IO_t io) -{ - return IO_GPIOPortIdx(io); -} -#endif - -int IO_GPIO_PortSource(IO_t io) -{ - return IO_GPIOPortIdx(io); -} - -// zero based pin index -int IO_GPIOPinIdx(IO_t io) -{ - if (!io) { - return -1; - } - return 31 - __builtin_clz(IO_Pin(io)); -} - -#if defined(STM32F4) || defined(APM32F4) -int IO_EXTI_PinSource(IO_t io) -{ - return IO_GPIOPinIdx(io); -} -#endif - -int IO_GPIO_PinSource(IO_t io) -{ - return IO_GPIOPinIdx(io); -} - // claim IO pin, set owner and resources void IOInit(IO_t io, resourceOwner_e owner, uint8_t index) { diff --git a/src/main/drivers/io_def.h b/src/main/drivers/io_def.h index 6566bd34fb..8906c71b2a 100644 --- a/src/main/drivers/io_def.h +++ b/src/main/drivers/io_def.h @@ -41,11 +41,24 @@ // get ioRec by index #define DEFIO_REC_INDEXED(idx) (ioRecs + (idx)) +// split ioTag bits between pin and port +// port is encoded as +1 to avoid collision with 0x0 (false as bool) +#ifndef DEFIO_PORT_PINS +// pins per port +#define DEFIO_PORT_PINS 16 +#endif + +STATIC_ASSERT((DEFIO_PORT_PINS & (DEFIO_PORT_PINS - 1)) == 0, "DEFIO_PORT_PINS must be power of 2"); + +#define DEFIO_PORT_BITSHIFT LOG2(DEFIO_PORT_PINS) +#define DEFIO_PIN_BITMASK ((1 << DEFIO_PORT_BITSHIFT ) - 1) + // ioTag_t accessor macros -#define DEFIO_TAG_MAKE(gpioid, pin) ((ioTag_t)((((gpioid) + 1) << 4) | (pin))) +#define DEFIO_TAG_MAKE(gpioid, pin) ((ioTag_t)((((gpioid) + 1) << DEFIO_PORT_BITSHIFT) | (pin))) #define DEFIO_TAG_ISEMPTY(tag) (!(tag)) -#define DEFIO_TAG_GPIOID(tag) (((tag) >> 4) - 1) -#define DEFIO_TAG_PIN(tag) ((tag) & 0x0f) +#define DEFIO_TAG_GPIOID(tag) (((tag) >> DEFIO_PORT_BITSHIFT) - 1) +#define DEFIO_TAG_PIN(tag) ((tag) & DEFIO_PIN_BITMASK) + // TARGET must define used pins #include "target.h" diff --git a/src/platform/common/stm32/io_impl.c b/src/platform/common/stm32/io_impl.c index c2a98ad40f..a7fabbd3f8 100644 --- a/src/platform/common/stm32/io_impl.c +++ b/src/platform/common/stm32/io_impl.c @@ -76,3 +76,36 @@ int IO_GPIOPortIdx(IO_t io) } return (((size_t)IO_GPIO(io) - GPIOA_BASE) >> 10); } + +#if defined(STM32F4) || defined(APM32F4) +int IO_EXTI_PortSourceGPIO(IO_t io) +{ + return IO_GPIOPortIdx(io); +} +#endif + +int IO_GPIO_PortSource(IO_t io) +{ + return IO_GPIOPortIdx(io); +} + +// zero based pin index +int IO_GPIOPinIdx(IO_t io) +{ + if (!io) { + return -1; + } + return 31 - __builtin_clz(IO_Pin(io)); +} + +#if defined(STM32F4) || defined(APM32F4) +int IO_EXTI_PinSource(IO_t io) +{ + return IO_GPIOPinIdx(io); +} +#endif + +int IO_GPIO_PinSource(IO_t io) +{ + return IO_GPIOPinIdx(io); +}