Page 1 of 1

Calling ADC conversion from PWM interrupt

Posted: Mon Mar 06, 2023 4:20 pm
by gp7
Hi,

I've been debugging code for a motor control thread which is calling an ADC conversion from within a PWM interrupt service routine.
Although the code appeared to run on previous Chibios/RT versions it fails on 21.11.x
When I run the application in debug mode I notice the device gets stuck and on pausing I find that it has stuck within the ADC that's triggered by the PWM ISR with chSysHalt("SV#10"). "I-Class function called out of a critical zone."

If I change the ADC calling function from adcStartConversionI to adcStartConversion I then find that code becomes stuck with chSysHalt("SV#4").
"The function chSysLock() has been called from ISR context or from within a critical zone. This function is meant to start a critical zone from thread context. This can also happen when a normal API is called from within a critical zone."

Is there a safe way of triggering an STM32G4 ADC from within a timer ISR? As I understand it the code is implemented this way because the analog values being read by the ADC are needed for control of the PWM duty cycle for the associated motor bridge driver. I am driving a PZT with the motor driver so the timing of the ADC conversion needs to be pretty accurate.

I'm not certain but perhaps there's another recognized way of reading motor current in this scenario in a timely fashion? Does anyone have any advice or experience on how to do this sort of thing in Chibios?

Re: Calling ADC conversion from PWM interrupt

Posted: Mon Mar 06, 2023 4:54 pm
by Giovanni
Hi,

The correct function is adcStartConversionI(), make sure it is called within chSysLockFromISR() and chSysUnlockFromISR() or you get that SV#10.

This could be of help: https://www.chibios.org/dokuwiki/doku.p ... interrupts

Giovanni