diff --git a/ports/mimxrt10xx/Makefile b/ports/mimxrt10xx/Makefile index d8d12b74da..542014e91a 100644 --- a/ports/mimxrt10xx/Makefile +++ b/ports/mimxrt10xx/Makefile @@ -136,6 +136,7 @@ SRC_C += \ peripherals/mimxrt10xx/$(CHIP_FAMILY)/clocks.c \ peripherals/mimxrt10xx/$(CHIP_FAMILY)/periph.c \ peripherals/mimxrt10xx/$(CHIP_FAMILY)/pins.c \ + peripherals/mimxrt10xx/pins.c \ reset.c \ supervisor/flexspi_nor_flash_ops.c diff --git a/ports/mimxrt10xx/common-hal/microcontroller/Pin.c b/ports/mimxrt10xx/common-hal/microcontroller/Pin.c index fc455d7a27..a5c491ee7d 100644 --- a/ports/mimxrt10xx/common-hal/microcontroller/Pin.c +++ b/ports/mimxrt10xx/common-hal/microcontroller/Pin.c @@ -120,104 +120,7 @@ void common_hal_mcu_pin_reset_number(uint8_t pin_no) { common_hal_reset_pin((mcu_pin_obj_t *)(mcu_pin_globals.map.table[pin_no].value)); } -/* Array of GPIO peripheral base address. */ -static GPIO_Type *const s_gpioBases[] = GPIO_BASE_PTRS; -static uint32_t GPIO_GetInstance(GPIO_Type *base) { - uint32_t instance; - - /* Find the instance index from base address mappings. */ - for (instance = 0U; instance < ARRAY_SIZE(s_gpioBases); instance++) - { - if (s_gpioBases[instance] == base) { - break; - } - } - - assert(instance < ARRAY_SIZE(s_gpioBases)); - - return instance; -} - -/* to find IRQ based on GPIO */ -static const IRQn_Type low_irqs[] = GPIO_COMBINED_LOW_IRQS; -static const IRQn_Type high_irqs[] = GPIO_COMBINED_HIGH_IRQS; - -typedef struct { - gpio_change_interrupt_t *func; - void *data; -} pin_change_interrupt_data; - -volatile static pin_change_interrupt_data pcid[MP_ARRAY_SIZE(s_gpioBases)][32]; - // The 'data' pointers may be to gc objects, they must be kept alive. void pin_gc_collect() { gc_collect_root((void **)&pcid, sizeof(pcid) / sizeof(void *)); } - -void enable_pin_change_interrupt(const mcu_pin_obj_t *pin, gpio_change_interrupt_t func, void *data) { - int instance = GPIO_GetInstance(pin->gpio); - volatile pin_change_interrupt_data *pci = &pcid[instance][pin->number]; - common_hal_mcu_disable_interrupts(); - pci->data = data; - pci->func = func; - IRQn_Type irq = pin->number < 16 ? low_irqs[instance] : high_irqs[instance]; - if (irq != NotAvail_IRQn) { - EnableIRQ(irq); - } - pin->gpio->IMR |= (1 << pin->number); - common_hal_mcu_enable_interrupts(); -} - -void disable_pin_change_interrupt(const mcu_pin_obj_t *pin) { - volatile pin_change_interrupt_data *pci = &pcid[GPIO_GetInstance(pin->gpio)][pin->number]; - common_hal_mcu_disable_interrupts(); - pin->gpio->IMR &= ~(1 << pin->number); - pci->data = NULL; - pci->func = NULL; - pin->gpio->ISR = (1 << pin->number); // acknowledge any pending interrupt - common_hal_mcu_enable_interrupts(); -} - -static void pin_change_interrupt_common(uint32_t isr, volatile pin_change_interrupt_data *pcr) { - for (uint32_t i = 0; i < 32; i++) { - if (isr & (1 << i)) { - pin_change_interrupt_data cb = pcr[i]; - if (cb.func) { - cb.func(cb.data); - } - } - } -} - -#define GPIO_INTERRUPT_HANDLER(name, ptr, instance, offset) \ - void name(void); \ - __attribute__((used)) void name(void) { \ - uint32_t isr = ptr->ISR; \ - ptr->ISR = isr; \ - pin_change_interrupt_common(isr, pcid[instance]); \ - } - -#if defined(GPIO1) -GPIO_INTERRUPT_HANDLER(GPIO1_Combined_0_15_IRQHandler, GPIO1, 1, 0); -GPIO_INTERRUPT_HANDLER(GPIO1_Combined_16_31_IRQHandler, GPIO1, 1, 16); -#endif -#if defined(GPIO2) -GPIO_INTERRUPT_HANDLER(GPIO2_Combined_0_15_IRQHandler, GPIO2, 2, 0); -GPIO_INTERRUPT_HANDLER(GPIO2_Combined_16_31_IRQHandler, GPIO2, 2, 16); -#endif -#if defined(GPIO3) -GPIO_INTERRUPT_HANDLER(GPIO3_Combined_0_15_IRQHandler, GPIO3, 3, 0); -GPIO_INTERRUPT_HANDLER(GPIO3_Combined_16_31_IRQHandler, GPIO3, 3, 16); -#endif -#if defined(GPIO4) -GPIO_INTERRUPT_HANDLER(GPIO4_Combined_0_15_IRQHandler, GPIO4, 4, 0); -GPIO_INTERRUPT_HANDLER(GPIO4_Combined_16_31_IRQHandler, GPIO4, 4, 16); -#endif -#if defined(GPIO5) -GPIO_INTERRUPT_HANDLER(GPIO5_Combined_0_15_IRQHandler, GPIO5, 5, 0); -GPIO_INTERRUPT_HANDLER(GPIO5_Combined_16_31_IRQHandler, GPIO5, 5, 16); -#endif -#if defined(GPIO6) -GPIO_INTERRUPT_HANDLER(GPIO6_Combined_0_15_IRQHandler, GPIO6, 6, 0); -GPIO_INTERRUPT_HANDLER(GPIO6_Combined_16_31_IRQHandler, GPIO6, 6, 16); -#endif diff --git a/ports/mimxrt10xx/common-hal/microcontroller/Pin.h b/ports/mimxrt10xx/common-hal/microcontroller/Pin.h index f75f4fbbee..57df61c691 100644 --- a/ports/mimxrt10xx/common-hal/microcontroller/Pin.h +++ b/ports/mimxrt10xx/common-hal/microcontroller/Pin.h @@ -45,9 +45,6 @@ extern const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[]; // the port-default reset behavior. extern bool mimxrt10xx_board_reset_pin_number(const mcu_pin_obj_t *pin); -typedef void (gpio_change_interrupt_t)(void *data); -void disable_pin_change_interrupt(const mcu_pin_obj_t *pin); -void enable_pin_change_interrupt(const mcu_pin_obj_t *pin, gpio_change_interrupt_t func, void *data); void pin_gc_collect(void); #endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_MICROCONTROLLER_PIN_H diff --git a/ports/mimxrt10xx/common-hal/rotaryio/IncrementalEncoder.c b/ports/mimxrt10xx/common-hal/rotaryio/IncrementalEncoder.c index dab74c1ce9..043b791b4d 100644 --- a/ports/mimxrt10xx/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/mimxrt10xx/common-hal/rotaryio/IncrementalEncoder.c @@ -33,7 +33,7 @@ #include "py/runtime.h" -#include "fsl_gpio.h" +#include "sdk/drivers/igpio/fsl_gpio.h" static void encoder_change(void *self_in) { rotaryio_incrementalencoder_obj_t *self = self_in; diff --git a/ports/mimxrt10xx/peripherals/mimxrt10xx/pins.c b/ports/mimxrt10xx/peripherals/mimxrt10xx/pins.c new file mode 100644 index 0000000000..1f2d5321e9 --- /dev/null +++ b/ports/mimxrt10xx/peripherals/mimxrt10xx/pins.c @@ -0,0 +1,94 @@ +#include "peripherals/mimxrt10xx/pins.h" + +/* Array of GPIO peripheral base address. */ +static GPIO_Type *const s_gpioBases[] = GPIO_BASE_PTRS; +static uint32_t GPIO_GetInstance(GPIO_Type *base) { + uint32_t instance; + + /* Find the instance index from base address mappings. */ + for (instance = 0U; instance < ARRAY_SIZE(s_gpioBases); instance++) + { + if (s_gpioBases[instance] == base) { + break; + } + } + + assert(instance < ARRAY_SIZE(s_gpioBases)); + + return instance; +} + + +/* to find IRQ based on GPIO */ +static const IRQn_Type low_irqs[] = GPIO_COMBINED_LOW_IRQS; +static const IRQn_Type high_irqs[] = GPIO_COMBINED_HIGH_IRQS; + +volatile pin_change_interrupt_data pcid[MP_ARRAY_SIZE(s_gpioBases)][32]; + +void enable_pin_change_interrupt(const mcu_pin_obj_t *pin, gpio_change_interrupt_t func, void *data) { + int instance = GPIO_GetInstance(pin->gpio); + volatile pin_change_interrupt_data *pci = &pcid[instance][pin->number]; + common_hal_mcu_disable_interrupts(); + pci->data = data; + pci->func = func; + IRQn_Type irq = pin->number < 16 ? low_irqs[instance] : high_irqs[instance]; + if (irq != NotAvail_IRQn) { + EnableIRQ(irq); + } + pin->gpio->IMR |= (1 << pin->number); + common_hal_mcu_enable_interrupts(); +} + +void disable_pin_change_interrupt(const mcu_pin_obj_t *pin) { + volatile pin_change_interrupt_data *pci = &pcid[GPIO_GetInstance(pin->gpio)][pin->number]; + common_hal_mcu_disable_interrupts(); + pin->gpio->IMR &= ~(1 << pin->number); + pci->data = NULL; + pci->func = NULL; + pin->gpio->ISR = (1 << pin->number); // acknowledge any pending interrupt + common_hal_mcu_enable_interrupts(); +} + +static void pin_change_interrupt_common(uint32_t isr, volatile pin_change_interrupt_data *pcr) { + for (uint32_t i = 0; i < 32; i++) { + if (isr & (1 << i)) { + pin_change_interrupt_data cb = pcr[i]; + if (cb.func) { + cb.func(cb.data); + } + } + } +} + +#define GPIO_INTERRUPT_HANDLER(name, ptr, instance, offset) \ + void name(void); \ + __attribute__((used)) void name(void) { \ + uint32_t isr = ptr->ISR; \ + ptr->ISR = isr; \ + pin_change_interrupt_common(isr, pcid[instance]); \ + } + +#if defined(GPIO1) +GPIO_INTERRUPT_HANDLER(GPIO1_Combined_0_15_IRQHandler, GPIO1, 1, 0); +GPIO_INTERRUPT_HANDLER(GPIO1_Combined_16_31_IRQHandler, GPIO1, 1, 16); +#endif +#if defined(GPIO2) +GPIO_INTERRUPT_HANDLER(GPIO2_Combined_0_15_IRQHandler, GPIO2, 2, 0); +GPIO_INTERRUPT_HANDLER(GPIO2_Combined_16_31_IRQHandler, GPIO2, 2, 16); +#endif +#if defined(GPIO3) +GPIO_INTERRUPT_HANDLER(GPIO3_Combined_0_15_IRQHandler, GPIO3, 3, 0); +GPIO_INTERRUPT_HANDLER(GPIO3_Combined_16_31_IRQHandler, GPIO3, 3, 16); +#endif +#if defined(GPIO4) +GPIO_INTERRUPT_HANDLER(GPIO4_Combined_0_15_IRQHandler, GPIO4, 4, 0); +GPIO_INTERRUPT_HANDLER(GPIO4_Combined_16_31_IRQHandler, GPIO4, 4, 16); +#endif +#if defined(GPIO5) +GPIO_INTERRUPT_HANDLER(GPIO5_Combined_0_15_IRQHandler, GPIO5, 5, 0); +GPIO_INTERRUPT_HANDLER(GPIO5_Combined_16_31_IRQHandler, GPIO5, 5, 16); +#endif +#if defined(GPIO6) +GPIO_INTERRUPT_HANDLER(GPIO6_Combined_0_15_IRQHandler, GPIO6, 6, 0); +GPIO_INTERRUPT_HANDLER(GPIO6_Combined_16_31_IRQHandler, GPIO6, 6, 16); +#endif diff --git a/ports/mimxrt10xx/peripherals/mimxrt10xx/pins.h b/ports/mimxrt10xx/peripherals/mimxrt10xx/pins.h index 82cb0dc69e..76a8d03e08 100644 --- a/ports/mimxrt10xx/peripherals/mimxrt10xx/pins.h +++ b/ports/mimxrt10xx/peripherals/mimxrt10xx/pins.h @@ -72,6 +72,16 @@ typedef struct { .pad_reset = p_pad_reset, \ } +typedef void (gpio_change_interrupt_t)(void *data); +typedef struct { + gpio_change_interrupt_t *func; + void *data; +} pin_change_interrupt_data; +extern volatile pin_change_interrupt_data pcid[MP_ARRAY_SIZE((GPIO_Type *const[])GPIO_BASE_PTRS)][32]; + +void disable_pin_change_interrupt(const mcu_pin_obj_t *pin); +void enable_pin_change_interrupt(const mcu_pin_obj_t *pin, gpio_change_interrupt_t func, void *data); + #ifdef MIMXRT1011_SERIES #include "MIMXRT1011/pins.h" #elif defined(MIMXRT1021_SERIES)