ESP32 ULP program to read ADC and wakeup by ULP

Hello everyone. I am currently working on a project where I need to use ULP to read ADC and wake up the main CPU after certain threshold are met. Below is my code, which can wake up the CPU, but it can't after I uncomment "I2 ADC (R0, 0, 6)". Does anyone know the reason? Thank you in advance.

#include "esp32/ulp.h"
#include "soc/rtc_cntl_reg.h"
#include "driver/rtc_io.h"
#include "esp32/ulp.h"
#include "driver/adc.h"                    
 
void init_ulp_program();
 
RTC_DATA_ATTR int bootCount = 0;
 
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;
 
  wakeup_reason = esp_sleep_get_wakeup_cause();
 
  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}
 
void setup() {
    Serial.begin(115200);
    //while( !Serial ){}
 
    Serial.println("Init");
 
    //Increment boot number and print it every reboot
    ++bootCount;
    Serial.println("Boot number: " + String(bootCount));
 
    //Print the wakeup reason for ESP32
    print_wakeup_reason();
 
    esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
    if (cause != ESP_SLEEP_WAKEUP_ULP) {
        Serial.println("Initializing ULP");
        init_ulp_program();
        /* Set ULP wake up period to 5s */
        
        ulp_set_wakeup_period(0, 5 * 1000 * 1000);
    }
 
    Serial.println("Entering deep sleep\n");
    ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup() );
 
    esp_deep_sleep_start();
}
 
void loop(){
}
 
void init_ulp_program() {
    

   
    const ulp_insn_t program[] = {
//      M_LABEL (1),                // do {
//
                I_MOVI  (R0, 40),          // R0 = n * 1000 / 5, where n is the number of seconds to delay, 200 = 1 s
                M_LABEL (2),                // do {
                    I_DELAY (40000),            // delay (5);                           // since ULP runs at 8 MHz, 40000 cycles correspond to 5 ms (max possible delay is 65535 cycles or 8.19 ms)
                    I_SUBI  (R0, R0, 1),        // R0 --;
                M_BGE (2, 1),               // } while (R0 >= 1); ... jump to label 2 if R0 > 0

                //I_ADC   (R0, 0, 6),         
                
                //I_ST  (R3, 0, 100),

                //I_MOVR  (R0, R3),

            //M_BGE (1, 3740),

            
            // initiate wakeup of the SoC
            I_WAKE(),
            // stop the ULP program
            I_HALT()
    };
 
    size_t load_addr = 0;
    size_t size = sizeof(program)/sizeof(ulp_insn_t);
    ulp_process_macros_and_load(load_addr, program, &size);
    ulp_run(0);
    
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.