[SOLVED] Change level wake after first wake up

guillermop
Posts: 12
Joined: Thu Jul 06, 2017 2:52 am

[SOLVED] Change level wake after first wake up

Postby guillermop » Thu Jul 06, 2017 10:37 pm

I'm doing this to sleep my device:

Code: Select all

esp_deep_sleep_enable_ext0_wakeup (GPIO_NUM_33, 0);
As my external pulse will remain LOW for a long time, I'd like to change the trigger level to the oposite.

I understand this can be done with a stub, but I'm not sure if it's really possible.

Is it possible, can anyone give an example?
Last edited by guillermop on Mon Jul 10, 2017 2:43 am, edited 3 times in total.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Change level wake after first wake up

Postby WiFive » Fri Jul 07, 2017 2:29 am

https://gist.github.com/igrr/54f7fbe051 ... d7fbecfeab

You have to change some stuff and add

Code: Select all

// Set level which will trigger wakeup
    SET_PERI_REG_BITS(RTC_CNTL_EXT_WAKEUP_CONF_REG, 0x1, level, RTC_CNTL_EXT_WAKEUP0_LV_S);

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Change level wake after first wake up

Postby ESP_igrr » Fri Jul 07, 2017 2:40 am

There is a single bit you need to set or clear in order to change wakeup trigger level:
https://github.com/espressif/esp-idf/bl ... eep.c#L236

guillermop
Posts: 12
Joined: Thu Jul 06, 2017 2:52 am

Re: Change level wake after first wake up

Postby guillermop » Fri Jul 07, 2017 5:12 am

Thank you both for your answers

I've tried with:

Code: Select all

static void RTC_IRAM_ATTR wake_stub()
{
  ets_printf(alive_prnt);
  SET_PERI_REG_BITS(RTC_CNTL_EXT_WAKEUP_CONF_REG, 0x1, 1, RTC_CNTL_EXT_WAKEUP0_LV_S);
}
the ets_printf is to know the function is executed. But it keeps having the same behavior, the esp goes to sleep and wakes up every time:

This is the serial output
rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
wake_stub alive
ets Jun 8 2016 00:22:57

rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0008,len:8
load:0x3fff0010,len:160
load:0x40078000,len:10632
load:0x40080000,len:252
entry 0x40080034
Boot number: 1
Wakeup was not caused by deep sleep
Going to sleep now
ets Jun 8 2016 00:22:57
This is repeated while I've the external source in LOW

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Change level wake after first wake up

Postby WiFive » Fri Jul 07, 2017 5:43 am

That's not what he meant. You also have to do the stuff at the bottom of the example gist.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Change level wake after first wake up

Postby ESP_igrr » Fri Jul 07, 2017 6:03 am

WiFive: sorry, i haven't noticed that you also mentioned the register to set wakeup level.

guillermop: The gist linked above *waits* while the input signal changes level again. You need to replace that part (https://gist.github.com/igrr/54f7fbe051 ... -L100-L111) with the line which will toggle the wakeup level setting. Something like

Code: Select all

int current_level = REG_GET_FIELD(RTC_CNTL_EXT_WAKEUP_CONF_REG, RTC_CNTL_EXT_WAKEUP0_LV);
REG_SET_FIELD(RTC_CNTL_EXT_WAKEUP_CONF_REG, RTC_CNTL_EXT_WAKEUP0_LV, !current_level);

guillermop
Posts: 12
Joined: Thu Jul 06, 2017 2:52 am

Re: Change level wake after first wake up

Postby guillermop » Sat Jul 08, 2017 9:54 am

As I'm new with ESP, there some things I'm not familiar with, so as I understand now:

Code: Select all

#define PULSE_CNT_IS_HIGH() \
    ((REG_GET_FIELD(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT) \
            & BIT(GPIO_NUM_33)) == 1)
PULSE_CNT_IS_HIGH (I've renamed) will check if the pin is still in high, which make the device wakes up.

Code: Select all

int current_level = REG_GET_FIELD(RTC_CNTL_EXT_WAKEUP_CONF_REG, RTC_CNTL_EXT_WAKEUP0_LV);
Will get the current level set in wakeup0

Code: Select all

REG_SET_FIELD(RTC_CNTL_EXT_WAKEUP_CONF_REG, RTC_CNTL_EXT_WAKEUP0_LV, !current_level);
Will set the opposite

That is what I've to wait the change

Code: Select all

do {
        while (PULSE_CNT_IS_HIGH()) {
          int current_level = REG_GET_FIELD(RTC_CNTL_EXT_WAKEUP_CONF_REG, RTC_CNTL_EXT_WAKEUP0_LV);
          REG_SET_FIELD(RTC_CNTL_EXT_WAKEUP_CONF_REG, RTC_CNTL_EXT_WAKEUP0_LV, !current_level);
        }
        // debounce, 10ms
        ets_delay_us(10000);
    } while (PULSE_CNT_IS_HIGH());
I'm almost sure I'm misunderstood something, I've also tried with other combinations but none of them give me the result I'm expecting

I appreciate your patience, sorry if what I'm doing is nonsense

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Change level wake after first wake up

Postby WiFive » Sun Jul 09, 2017 7:44 am

guillermop wrote:sorry if what I'm doing is nonsense
You're forgiven :P

guillermop
Posts: 12
Joined: Thu Jul 06, 2017 2:52 am

Re: Change level wake after first wake up

Postby guillermop » Sun Jul 09, 2017 9:12 pm

WiFive wrote:
guillermop wrote:sorry if what I'm doing is nonsense
You're forgiven :P
Hahaha Thanks

after re-think about my problem I concluded that my approach was wrong, I was making a mountain out of a molehill.

So I solved first, storing the wake up level in the RTC memory. I've assigned the value of -1 to know if the device was booting for the first time or not.

Code: Select all

RTC_DATA_ATTR int wakeup_level = -1;
After that, I've checked the wake up level variable in memory, if it's the first time booting (-1) I assigned the stated I wanted, in this case LOW, otherwise the opposite current value

Code: Select all

if(wakeup_level == -1){
    wakeup_level = 0;
  }else{
    wakeup_level = !REG_GET_FIELD(RTC_CNTL_EXT_WAKEUP_CONF_REG, RTC_CNTL_EXT_WAKEUP0_LV);
  }
I finally set the external trigger with the value of wakeup_level

Code: Select all

esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_33, wakeup_level);
I don't know if it's the better/elegant solution for this problem, but at least it is working. Any suggestion will be appreciated.

Thanks you both for your help guys.

Who is online

Users browsing this forum: No registered users and 297 guests