Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/hal/STM32/hal_STM32_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,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;
Expand Down
5 changes: 5 additions & 0 deletions src/imu/imu_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this works, but does not make sense to me. See

https://stackoverflow.com/questions/50243996/what-are-valid-values-of-hal-nvic-setpriority-when-using-stm32-and-freertos

as far as I understand it configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY is a interrupt priority, but in the HAL_NVIC_SetPriority call it is used as a subpriority.

Why not use HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, 0); i.e. use the highest priority?

Or use HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, 0);, but I'm not sure if this will fix the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I mixed up preempt priority & subpriority. It worked because the preempt priority was 0 (the highest).

HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, 0); doesn't work, which seems logical since configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY's value is already the default preempt priority.

Why not use HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, 0); i.e. use the highest priority?

Because using a priority higher than configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY will not allow the ISR to call FreeRTOS functions (FreeRTOSConfig_Default.h).
It looks like no FreeRTOS calls are made in the imu ISR, so it's ok to use a priority > configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, but that is something to remember for future development.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently on STM32 the whole imu interrupt (including imu_loop() ) runs in interrupt context. So probably will have issues as soon as SDCARD (uses separate task) is implemented for STM32.

On ESP32/RP2 the imu stuff runs as high priority task, which is notified by the imu interrupt. I could not get this to work on STM32, but I did not make a big effort on this...

Anyway - to move ahead and get at least the interrupt working again:

Let's go with HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, 0), as this will trigger the imu interrupt for sure.

Maybe enclose everything (including IRQn_Type hal_get_irqn_from_pin(int pin)) with #ifdef MF_STM32_INTERRUPT_HACK instead of #ifdef ARDUINO_ARCH_STM32 and set #define MF_STM32_INTERRUPT_HACK 1 at the beginning of in hal_stm32_cpp.h with some comments. Makes it easier to find this hack back later.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe enclose everything (including IRQn_Type hal_get_irqn_from_pin(int pin)) with #ifdef MF_STM32_INTERRUPT_HACK instead of #ifdef ARDUINO_ARCH_STM32 and set #define MF_STM32_INTERRUPT_HACK 1 at the beginning of in hal_stm32_cpp.h with some comments. Makes it easier to find this hack back later.

This won't worlk - leave as is...

#endif
}

inline void _imu_ll_interrupt_handler2() {
Expand Down