Read ADC voltage in wake stub

timmbo
Posts: 30
Joined: Fri Jan 03, 2020 11:43 am

Read ADC voltage in wake stub

Postby timmbo » Thu May 28, 2020 6:02 pm

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

timmbo
Posts: 30
Joined: Fri Jan 03, 2020 11:43 am

Re: Read ADC voltage in wake stub

Postby timmbo » 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
}

i.gonzalez
Posts: 4
Joined: Thu Jan 19, 2023 11:58 am

Re: Read ADC voltage in wake stub

Postby i.gonzalez » Tue Jul 11, 2023 8:03 am

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?

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: Read ADC voltage in wake stub

Postby tommeyers » Tue Jul 11, 2023 10:16 pm

My help: what kind of errors, be specific, cut and paste.
IT Professional, Maker
Santiago, Dominican Republic

timmbo
Posts: 30
Joined: Fri Jan 03, 2020 11:43 am

Re: Read ADC voltage in wake stub

Postby timmbo » Wed Jul 12, 2023 8:38 am

Hey,

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

Best

i.gonzalez
Posts: 4
Joined: Thu Jan 19, 2023 11:58 am

Re: Read ADC voltage in wake stub

Postby i.gonzalez » Wed Jul 12, 2023 8:47 am

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.

i.gonzalez
Posts: 4
Joined: Thu Jan 19, 2023 11:58 am

Re: Read ADC voltage in wake stub

Postby i.gonzalez » Wed Jul 12, 2023 8:49 am

I'm using an ESP32 S2 SOLO

LaCocoRoco
Posts: 1
Joined: Mon Dec 04, 2023 5:03 pm

Re: Read ADC voltage in wake stub

Postby LaCocoRoco » Mon Dec 04, 2023 6:12 pm

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?

Who is online

Users browsing this forum: Baidu [Spider], jsmith56x and 311 guests