Source code analysis problem:controlLoop_IRQHandler (void)

Hello guys,
I am reading the source code, trying to figure out how odrive works, and planning to do some modifies for my own project.
today I stucked here:

ControlLoop_IRQHandler(void) {…} // in board.cpp

I realize this is the core loop of the FOC algrithm. However, I don’t figure out how this IRQ is called.
Though, I found this “ControlLoop_IRQHandler” is a definition of “OTG_HS_IRQHandler”

#define ControlLoop_IRQHandler OTG_HS_IRQHandler
#define ControlLoop_IRQn OTG_HS_IRQn
Then I googled OTG_HS_IRQ, turns out “OTG_HS” is a USB interrupt .
I am very confused!
What is the relationship of Controlloop and an USB interrupt !? and when is this function called?

By my understanding: kinda core loop should be synchronized with PWM, and always called by a Timer IRQ.

Can someone give me clue and straight me out?

1 Like

It’s called in board.cpp - the NVIC “Software Triggered Interrupt Register” NVIC->STIR is set from a timer, this seems to be a hack to switch interrupt priority from high (timer IRQ) to low (USB IRQ).

The OTG_HS_IRQ interrupt is not being used for USB - it’s just being used as a spare low-priority interrupt slot to run the control functions.

1 Like

Thank you for the explaination. This explains most things, just some detail not very clear:
Why ControlLoop has the lowest priority among those interrupts?
I understand that the data updates happens after hardware sampling, but seems
“fetch_and_reset_adcs” & “measure current” are functions of “ControlLoop” it self. I feels little tangled.

P.S. control_loop_cb, what here this surfix “cb” refers? what does those functions end with “_cb” in commom?

1 Like

_cb stands for ‘callback’ - i.e. a function called in a foreign context

The timer interrupts themselves must have the highest priority since they run the PWM outputs - we don’t want the PWM to be stuck, because if that happens it could cause smoke. Similarly the ADCs are running via DMA and they are highly timing sensitive - they run in the background at a higher priority than all but the PWM timers.
The control loop reads from the ADC and writes to the PWM, and has a higher priority than all of the RTOS threads, but lower than the timers and ADCs. At least that is my understanding.

:grinning: that’s very clear! That verifies most of my concerns. cool, thank you.

1 Like