Calling ADC conversion from PWM interrupt

Discussions and support about ChibiOS/RT, the free embedded RTOS.
gp7
Posts: 17
Joined: Fri Oct 14, 2022 1:52 pm
Has thanked: 10 times

Calling ADC conversion from PWM interrupt

Postby gp7 » Mon Mar 06, 2023 4:20 pm

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?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Calling ADC conversion from PWM interrupt

Postby Giovanni » Mon Mar 06, 2023 4:54 pm

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


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 3 guests