Help with coding in esp_wake_deep_sleep()

Supertramp4
Posts: 6
Joined: Sun May 29, 2022 6:40 pm

Help with coding in esp_wake_deep_sleep()

Postby Supertramp4 » Mon May 30, 2022 3:47 pm

HI,
This is my 1st post to the forum, and hope someone can give me some guidance.
I have a ESP32-c3-12 on a dev board from RF Solutions, and is simple C coding without any rtos , and need to make use of the deep_sleep feature for waking up to a GPIO Level, as this is a battery powered water meter pulse detector

There are 3 places where once the BUTTON1( GPIO_NUM_2) is pressed the esp_wake_deep_sleep function loads, it encounters illegal Instruction for the following 3 lines :

Code: Select all

ds_wakeup_reason = esp_sleep_get_wakeup_cause()
esp_deep_sleep_enable_gpio_wakeup(1ULL << BUTTON1,ESP_GPIO_WAKEUP_GPIO_LOW);
esp_deep_sleep_enable_gpio_wakeup(1ULL << BUTTON1,ESP_GPIO_WAKEUP_GPIO_HIGH);
Going to deep sleep:ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x5 (DSLEEP),boot:0xd (SPI_FAST_FLASH_BOOT)
STUB : Wake count 0
Guru Meditation Error: Core 0 panic'ed (Illegal instruction)


Here is the code i currently have for the esp_wake_deep_sleep()

Code: Select all

#define BUTTON1 GPIO_NUM_2
RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR int Armed = 0;
RTC_DATA_ATTR int Water = 0;
RTC_DATA_ATTR int wake_deep_stub_count;

void RTC_IRAM_ATTR esp_wake_deep_sleep(void) {
    esp_default_wake_deep_sleep();
    static RTC_RODATA_ATTR const char fmt_str[] = "STUB : Wake count %d\n";
    esp_rom_printf(fmt_str, wake_deep_stub_count++);

    esp_sleep_wakeup_cause_t ds_wakeup_reason;
    ds_wakeup_reason = esp_sleep_get_wakeup_cause();   // << causes Core 0 panic'ed (Illegal instruction)

    if(ds_wakeup_reason == 7) // GPIO Caused Wakeup
    {
      if(Armed == 1)  // button has been released
      {
          Armed = 0;
         esp_deep_sleep_enable_gpio_wakeup(1ULL << BUTTON1,ESP_GPIO_WAKEUP_GPIO_LOW); // << causes Core 0 panic'ed (Illegal instruction)
           ++Water;
      }  
      if(Armed == 0) // button has been pushed
      {
          Armed = 1;
          esp_deep_sleep_enable_gpio_wakeup(1ULL << BUTTON1,ESP_GPIO_WAKEUP_GPIO_HIGH); // << causes Core 0 panic'ed (Illegal instruction)
      }  
     
    }
    //Now go back to deep sleep
    CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG,RTC_CNTL_SLEEP_EN);
    SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG,RTC_CNTL_SLEEP_EN);    
    while(true) {
    ;
    }
    // for any other reason, then continue to wake up normally 
}


Could anyone suggest how I implement these 3 lines in ROM style code.
Many Thanks

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: Help with coding in esp_wake_deep_sleep()

Postby ESP_Sprite » Tue May 31, 2022 1:52 am

I'm not sure if there's variants of those functions that are callable from a wake stub... you may need to flip the interrupt level bit yourself instead. Suggest you look at the TRM for the correct register and setting; perhaps this helps as well.

Supertramp4
Posts: 6
Joined: Sun May 29, 2022 6:40 pm

Re: Help with coding in esp_wake_deep_sleep()

Postby Supertramp4 » Tue May 31, 2022 4:13 pm

Hi ESP_Sprite,
Thanks for the link to the Doc, I'll take a look.
I'm struggling to find the register that i can use for the esp_sleep_get_wakeup_cause().
I thought REG_READ(RTC_CNTL_WAKEUP_STATE_REG), but this always returns 393216 decimal irrespective of what causes the wakes

Any Thoughts ?

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

Re: Help with coding in esp_wake_deep_sleep()

Postby lbernstone » Wed Jun 01, 2022 2:52 am

esp_sleep_get_wakeup_cause() is defined at https://github.com/espressif/esp-idf/bl ... es.c#L1160

Supertramp4
Posts: 6
Joined: Sun May 29, 2022 6:40 pm

Re: Help with coding in esp_wake_deep_sleep()

Postby Supertramp4 » Wed Jun 01, 2022 11:55 am

Hi,
The REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG,RTC_CNTL_WAKEUP_CAUSE) doesn't work.
Here's a simple example where the REG_GET_FIELD always returns Zero, but the esp_sleep_get_wakeup_cause() returns the correct result.

Code: Select all

#include <esp_sleep.h>
#include "soc/rtc_cntl_reg.h"

#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  5        /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int bootCount = 0;

void setup(){
  Serial.begin(115200);
  delay(1000); //Take some time to open up the Serial Monitor

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Print the wakeup reason for ESP32
  Serial.printf("DS Wakeup 1 - Register: %u\n", REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG,RTC_CNTL_WAKEUP_CAUSE));
  esp_sleep_wakeup_cause_t wakeup_reason;
  wakeup_reason = esp_sleep_get_wakeup_cause();
  Serial.printf("DS Wakeup 2 - Function: %d\n", wakeup_reason);
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

  Serial.println("Going to sleep now");
  Serial.flush(); 
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

void loop(){
  //This is not going to be called
}

Outputs :

Code: Select all

Attempting to boot anyway...
entry 0x403ce000
Boot number: 43
DS Wakeup 1 - Register: 0
DS Wakeup 2 - Function: 4
Going to sleep now
Remember, that i need this to work in the void RTC_IRAM_ATTR esp_wake_deep_sleep(void) section, this is just to prove its not working.

benoitm974
Posts: 1
Joined: Thu Aug 10, 2023 5:19 pm

Re: Help with coding in esp_wake_deep_sleep()

Postby benoitm974 » Thu Aug 10, 2023 5:20 pm

I was able ot get the wakeup cause in stub on esp32C3 doing:
int wake_reason = REG_GET_FIELD(RTC_CNTL_SLP_WAKEUP_CAUSE_REG, RTC_CNTL_WAKEUP_CAUSE);

regards,

Who is online

Users browsing this forum: No registered users and 120 guests