Stop ADC continuous in ISR function

shadowkane
Posts: 6
Joined: Tue May 07, 2019 1:57 pm

Stop ADC continuous in ISR function

Postby shadowkane » Wed Feb 21, 2024 5:40 pm

Hi,
i'm looking for a way to stop the ADC continuous mode as soon as the conversion ends, in my situation is, when the ADC_complete() ISR function is called.
this is my code:

Code: Select all

// functions
void ARDUINO_ISR_ATTR vAdc_continous_complete_isr_fn(){
  // stop ADC continous until the next cycle
  analogContinuousStop();
  bAdcResultReady_flag = true;
}

void setup() {
  // Serial port config
  Serial.begin(115200);
  
  // ADC configuration
  analogContinuousSetWidth(12);
  analogContinuousSetAtten(ADC_11db);
  uint8_t pu8AdcPins[] = {34};
  size_t xNbrAdcPins = sizeof(pu8AdcPins)/sizeof(uint8_t);
  analogContinuous(pu8AdcPins, xNbrAdcPins, 10, 20000, &vAdc_continous_complete_isr_fn);
  //  Start ADC Continuous conversions
   delay(2000);
  analogContinuousStart();
}
void loop() {
  if(bAdcResultReady_flag){
    bAdcResultReady_flag = false;
    Serial.println("ADC completed");
    // start it again for new cycle
    analogContinuousStart();
    delay(10000);
  }
}
wheni upload this to my device which is based on ESP32, it keep retarting after 2 seconds and each time i get this error message:

assert failed: xQueueGiveFromISR queue.c:1311 (!( ( pxQueue->pcHead == ((void *)0) ) && ( pxQueue->u.xSemaphore.xMutexHolder != ((void *)0) ) ))
Backtrace: 0x4008207d:0x3ffbf06c |<-CORRUPTED

wheni i remove "analogContinuousStop();" from ISR function, it works fine.
Can someone tell me, why the function can't be called from ISR and what is the alternative solution to stop the ADC continuous from ISR function?

Thank you.

lbernstone
Posts: 673
Joined: Mon Jul 22, 2019 3:20 pm

Re: Stop ADC continuous in ISR function

Postby lbernstone » Thu Feb 22, 2024 6:53 pm

While an ISR is running, many hardware functions are interrupted, so simply cannot be accessed. In addition, an ISR must process quickly so that normal operation can continue.
You should set a semaphore, and check (take) it in your normal flow to turn the ADC off. You could also have the ADC started from a separate task, and use suspend/resume to turn it on and off from an ISR.

shadowkane
Posts: 6
Joined: Tue May 07, 2019 1:57 pm

Re: Stop ADC continuous in ISR function

Postby shadowkane » Fri Feb 23, 2024 1:39 pm

Hi @lbernstone and thank you for your answer.

your suggestion is good but the thing is, i'm not using any rtos, also for this project i just want a sequential programming, no threads or semaphores (ofc, i can replace semaphore by using a flag) but that won't prevent the ADC continuous from interrupting the program until i actual call the stop function "analogContinuousStop();", and i actualy do it in the main function when i get information that data is ready, so i can process the information and start the ADC again. but i just wanted to stop ADC as soon as the data is ready at the first time, which mean when the interruption happens, that's why i was trying to call "analogContinuousStop();" from the ISR function.
Also i wanted to use deep sleep mode, but i think this approach won't work because in deep sleep DMA (so, ADC in continuous mode) don't work, maybe if i use ULP co-processor, i'm still learning about this.
for the now i'm dropping off the approach of using ADC in continuous mode, because i need to use deep sleep, unless if you have an advice for me that could help to use both features.

again, thank you for your help.

lbernstone
Posts: 673
Joined: Mon Jul 22, 2019 3:20 pm

Re: Stop ADC continuous in ISR function

Postby lbernstone » Fri Feb 23, 2024 5:03 pm


shadowkane
Posts: 6
Joined: Tue May 07, 2019 1:57 pm

Re: Stop ADC continuous in ISR function

Postby shadowkane » Fri Feb 23, 2024 5:54 pm

Thank you :)

Who is online

Users browsing this forum: No registered users and 130 guests