From ba09f886ba94ede5f509e666cdd2e97b46a6218f Mon Sep 17 00:00:00 2001 From: EnzoPB Date: Fri, 19 Sep 2025 11:19:31 +0200 Subject: [PATCH 1/2] Fix #79 imu interrupt not firing on STM32 --- src/hal/STM32/hal_STM32_cpp.h | 24 ++++++++++++++++++++++++ src/imu/imu_cpp.h | 5 +++++ 2 files changed, 29 insertions(+) diff --git a/src/hal/STM32/hal_STM32_cpp.h b/src/hal/STM32/hal_STM32_cpp.h index f1540e75..5c5fdb76 100644 --- a/src/hal/STM32/hal_STM32_cpp.h +++ b/src/hal/STM32/hal_STM32_cpp.h @@ -204,6 +204,30 @@ void hal_print_pin_name(int pinnum) { } } +IRQn_Type hal_get_irqn_from_pin(int pin) { + // converts an arduino pin number to a STM32 HAL interrupt id + // taken from https://github.com/stm32duino/Arduino_Core_STM32/blob/main/libraries/SrcWrapper/src/stm32/interrupt.cpp#L158 + PinName pinName = digitalPinToPinName(pin); + uint16_t gpioPin = STM_GPIO_PIN(pinName); + uint8_t id = 0; + + while (gpioPin != 0x0001) { + gpioPin = gpioPin >> 1; + id++; + } + + switch (id) { + case 0: return EXTI0_IRQn; + case 1: return EXTI1_IRQn; + case 2: return EXTI2_IRQn; + case 3: return EXTI3_IRQn; + case 4: return EXTI4_IRQn; + case 5: case 6: case 7: case 8: case 9: + return EXTI9_5_IRQn; + default: + return EXTI15_10_IRQn; + } +} MF_Serial* hal_get_ser_bus(int bus_id, int baud, MF_SerialMode mode, bool invert) { if(bus_id < 0 || bus_id >= HAL_SER_NUM) return nullptr; diff --git a/src/imu/imu_cpp.h b/src/imu/imu_cpp.h index 22db3546..680ef442 100644 --- a/src/imu/imu_cpp.h +++ b/src/imu/imu_cpp.h @@ -388,6 +388,11 @@ void _imu_ll_interrupt_handler(); void _imu_ll_interrupt_setup(int interrupt_pin) { Serial.println(MF_MOD ": IMU_EXEC_IRQ"); attachInterrupt(digitalPinToInterrupt(interrupt_pin), _imu_ll_interrupt_handler, RISING); + #ifdef ARDUINO_ARCH_STM32 + // (#79) on stm32, increase the interrupt priority, otherwise it does not get called + // configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY is the maximum priority allowed for interrupts that needs to call FreeRTOS api + HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); + #endif } inline void _imu_ll_interrupt_handler2() { From d2dce95c254bef288445f759c5262baa05a250e1 Mon Sep 17 00:00:00 2001 From: Enzo PB Date: Tue, 23 Sep 2025 21:09:36 +0200 Subject: [PATCH 2/2] set imu interrupt to the highest priority --- src/imu/imu_cpp.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/imu/imu_cpp.h b/src/imu/imu_cpp.h index afcd98ea..d776ac46 100644 --- a/src/imu/imu_cpp.h +++ b/src/imu/imu_cpp.h @@ -390,9 +390,8 @@ void _imu_ll_interrupt_handler(); Serial.println(MF_MOD ": IMU_EXEC_IRQ"); attachInterrupt(digitalPinToInterrupt(interrupt_pin), _imu_ll_interrupt_handler, RISING); #ifdef ARDUINO_ARCH_STM32 - // (#79) on stm32, increase the interrupt priority, otherwise it does not get called - // configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY is the maximum priority allowed for interrupts that needs to call FreeRTOS api - HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); + // (#79) on stm32, set the interrupt to the highest priority, otherwise it does not get called + HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, 0); #endif }