Page 1 of 1

Read ADC voltage in wake stub

Posted: Thu May 28, 2020 6:02 pm
by timmbo
Hi all,

I would like to read the battery voltage in the wake stub (esp_wake_deep_sleep), but as the ADC functions are not in the RTC memory I have absolutely no clue where to start. Couldn't find anything here: https://github.com/espressif/esp-idf/tr ... /esp32/rom

Did anybody manage to read ADC values in the wake stub?

Thanks in advance & greetings,
Timm

Re: Read ADC voltage in wake stub

Posted: Tue Jun 02, 2020 6:15 am
by timmbo
Hi all,

I figured it out and would like to share the code in case anyone else has a similar problem. The following function can run in the wake stub, turns on ADC and measures the voltage of a certain channel of AD1 (result will be stored in adcValue) and then turns off ADC again:

Code: Select all

RTC_DATA_ATTR uint16_t adcValue;

void RTC_IRAM_ATTR adc1_get_raw_ram(adc1_channel_t channel) {
    SENS.sar_read_ctrl.sar1_dig_force = 0; // switch SARADC into RTC channel 
    SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU; // adc_power_on
    RTCIO.hall_sens.xpd_hall = false; //disable other peripherals
    SENS.sar_meas_wait2.force_xpd_amp = SENS_FORCE_XPD_AMP_PD; // channel is set in the convert function
    
	// disable FSM, it's only used by the LNA.
    SENS.sar_meas_ctrl.amp_rst_fb_fsm = 0; 
    SENS.sar_meas_ctrl.amp_short_ref_fsm = 0;
    SENS.sar_meas_ctrl.amp_short_ref_gnd_fsm = 0;
    SENS.sar_meas_wait1.sar_amp_wait1 = 1;
    SENS.sar_meas_wait1.sar_amp_wait2 = 1;
    SENS.sar_meas_wait2.sar_amp_wait3 = 1; 

    //set controller
    SENS.sar_read_ctrl.sar1_dig_force = false;      //RTC controller controls the ADC, not digital controller
    SENS.sar_meas_start1.meas1_start_force = true;  //RTC controller controls the ADC,not ulp coprocessor
    SENS.sar_meas_start1.sar1_en_pad_force = true;  //RTC controller controls the data port, not ulp coprocessor
    SENS.sar_touch_ctrl1.xpd_hall_force = true;     // RTC controller controls the hall sensor power,not ulp coprocessor
    SENS.sar_touch_ctrl1.hall_phase_force = true;   // RTC controller controls the hall sensor phase,not ulp coprocessor
    
    //start conversion
    SENS.sar_meas_start1.sar1_en_pad = (1 << channel); //only one channel is selected.
    while (SENS.sar_slave_addr1.meas_status != 0);
    SENS.sar_meas_start1.meas1_start_sar = 0;
    SENS.sar_meas_start1.meas1_start_sar = 1;
    while (SENS.sar_meas_start1.meas1_done_sar == 0);
    adcValue = SENS.sar_meas_start1.meas1_data_sar; // set adc value!

    SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PD; // adc power off
}

Re: Read ADC voltage in wake stub

Posted: Tue Jul 11, 2023 8:03 am
by i.gonzalez
Hi, I'm trying to read ADC voltage in wake stub and I have tried the code that has been posted here but I just get errors. Can someone help me?

Re: Read ADC voltage in wake stub

Posted: Tue Jul 11, 2023 10:16 pm
by tommeyers
My help: what kind of errors, be specific, cut and paste.

Re: Read ADC voltage in wake stub

Posted: Wed Jul 12, 2023 8:38 am
by timmbo
Hey,

the posted code is specific for the ESP32 Pico D4. Maybe different hardware?

Best

Re: Read ADC voltage in wake stub

Posted: Wed Jul 12, 2023 8:47 am
by i.gonzalez
The thing is, I copy the previously provided code into the Arduino IDE and include the relevant libraries, but I only get errors like this: Compilation error: 'sens_dev_t' {aka 'volatile struct sens_dev_s'} has no member named 'sar_read_ctrl'; did you mean 'sar_reader1_ctrl'?

From what I've been seeing, these functions used to be in "rtc_module.c" but now this file doesn't exist (I haven't found it in my ESP folder) and I've read that they are in different libraries. Well, even including these libraries I am unable to prevent the code from making errors.

Re: Read ADC voltage in wake stub

Posted: Wed Jul 12, 2023 8:49 am
by i.gonzalez
I'm using an ESP32 S2 SOLO

Re: Read ADC voltage in wake stub

Posted: Mon Dec 04, 2023 6:12 pm
by LaCocoRoco
timmbo wrote:
Tue Jun 02, 2020 6:15 am
Hi all,

I figured it out and would like to share the code in case anyone else has a similar problem. The following function can run in the wake stub, turns on ADC and measures the voltage of a certain channel of AD1 (result will be stored in adcValue) and then turns off ADC again:

Code: Select all

RTC_DATA_ATTR uint16_t adcValue;

void RTC_IRAM_ATTR adc1_get_raw_ram(adc1_channel_t channel) {
    SENS.sar_read_ctrl.sar1_dig_force = 0; // switch SARADC into RTC channel 
    SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU; // adc_power_on
    RTCIO.hall_sens.xpd_hall = false; //disable other peripherals
    SENS.sar_meas_wait2.force_xpd_amp = SENS_FORCE_XPD_AMP_PD; // channel is set in the convert function
    
	// disable FSM, it's only used by the LNA.
    SENS.sar_meas_ctrl.amp_rst_fb_fsm = 0; 
    SENS.sar_meas_ctrl.amp_short_ref_fsm = 0;
    SENS.sar_meas_ctrl.amp_short_ref_gnd_fsm = 0;
    SENS.sar_meas_wait1.sar_amp_wait1 = 1;
    SENS.sar_meas_wait1.sar_amp_wait2 = 1;
    SENS.sar_meas_wait2.sar_amp_wait3 = 1; 

    //set controller
    SENS.sar_read_ctrl.sar1_dig_force = false;      //RTC controller controls the ADC, not digital controller
    SENS.sar_meas_start1.meas1_start_force = true;  //RTC controller controls the ADC,not ulp coprocessor
    SENS.sar_meas_start1.sar1_en_pad_force = true;  //RTC controller controls the data port, not ulp coprocessor
    SENS.sar_touch_ctrl1.xpd_hall_force = true;     // RTC controller controls the hall sensor power,not ulp coprocessor
    SENS.sar_touch_ctrl1.hall_phase_force = true;   // RTC controller controls the hall sensor phase,not ulp coprocessor
    
    //start conversion
    SENS.sar_meas_start1.sar1_en_pad = (1 << channel); //only one channel is selected.
    while (SENS.sar_slave_addr1.meas_status != 0);
    SENS.sar_meas_start1.meas1_start_sar = 0;
    SENS.sar_meas_start1.meas1_start_sar = 1;
    while (SENS.sar_meas_start1.meas1_done_sar == 0);
    adcValue = SENS.sar_meas_start1.meas1_data_sar; // set adc value!

    SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PD; // adc power off
}

Thank you very much for sharing the code! Works like a charm!
Do you probably have a repository?