[Resolved]wifi_fpm_do_sleep crashes

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

[Resolved]wifi_fpm_do_sleep crashes

Postby tve » Mon Sep 21, 2015 3:06 pm

I'm trying to use the new wifi_fpm_* APIs and I see the system crash. The code is as follows:

Code: Select all

static void ICACHE_FLASH_ATTR prSleepTimerCb(void *arg) {
   os_printf("Sleep!\n");
   os_delay_us(600); // time for "Sleep!" to be printed at 115200 baud
   int8_t err = wifi_fpm_do_sleep(1000*1000);
   if (err != 0) os_printf("Sleep error: %d\n", err);
   os_printf("Back!\n");
 }

void user_init(void) {
  ...
  os_timer_disarm(&prHeapTimer);
  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
  wifi_fpm_open();
  os_timer_disarm(&prHeapTimer);
  os_timer_setfn(&prHeapTimer, prSleepTimerCb, NULL);
  os_timer_arm(&prHeapTimer, 20000, 0);
  ...
}


Some of the os_printf output:

Code: Select all

   141> force slp enable,type: 1
   141> fpm open,type:1 0
...
  8831> connected with tve-home, channel 1
  8831> dhcp client start...
 11605> ip:192.168.0.114,mask:255.255.255.0,gw:192.168.0.1
 15125> Wifi check: mode=STA status=5
 17948> fpm close 3
 17948> pm open phy_2,type:2 0 0
 20141> Sleep!
 20142> pm2 684

 ets Jan  8 2013,rst cause:1, boot mode:(3,7)

Ooops, that's a crash! (The number at the left shows milliseconds since boot.)
The crash info is: Reset cause: 3=soft wdt , exccause=4 epc1=0x40221ac6 epc2=0x0 epc3=0x0 excvaddr=0x0 depc=0x0
The crash location is near the beginning of fpm_do_sleep:

Code: Select all

40221aa8 <fpm_do_sleep>:
fpm_do_sleep():
40221aa8:       f0c112          addi    a1, a1, -16
40221aab:       006122          s32i    a2, a1, 0
40221aae:       016102          s32i    a0, a1, 4
40221ab1:       ff3485          call0   40220dfc <pm_is_open>
40221ab4:       011216          beqz    a2, 40221ac9 <fpm_do_sleep+0x21>
40221ab7:       fe7721          l32r    a2, 40221494 <fpm_attach+0x1e4>
40221aba:       fe7731          l32r    a3, 40221498 <fpm_attach+0x1e8>
40221abd:       aca242          movi    a4, 0x2ac
40221ac0:       7d5901          l32r    a0, 40201024 <_irom0_text_start+0x14>
40221ac3:       0000c0          callx0  a0
40221ac6:       ffff06          j       40221ac6 <fpm_do_sleep+0x1e>
40221ac9:       fd7941          l32r    a4, 402210b0 <pm_get_idle_wait_time+0x44>
40221acc:       4408            l32i.n  a0, a4, 16
40221ace:       e27c            movi.n  a2, -2
40221ad0:       02e0d7          bbsi    a0, 13, 40221ad6 <fpm_do_sleep+0x2e>
40221ad3:       0001c6          j       40221ade <fpm_do_sleep+0x36>
...

Help?

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: wifi_fpm_do_sleep crashes

Postby tve » Mon Sep 21, 2015 3:19 pm

I noticed that the example in the SDK docs disables wifi before calling sleep, so I added:

Code: Select all

wifi_station_disconnect();
wifi_set_opmode_current(NULL_MODE);

before the sleep, but that doesn't make a difference, it still crashes. I will have to simplify the code so you can make sense of it...
Last edited by tve on Mon Sep 21, 2015 3:30 pm, edited 1 time in total.

ESP_Faye
Posts: 1641
Joined: Mon Oct 27, 2014 11:08 am

Re: wifi_fpm_do_sleep crashes

Postby ESP_Faye » Mon Sep 21, 2015 3:28 pm

Hi,

Yes, wifi_station_disconnect(); and wifi_set_opmode_current(NULL_MODE); are needed before calling force sleep function. We will add more description on this in the documentation.

Please provide your test code, we will have a try.

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: wifi_fpm_do_sleep crashes

Postby tve » Tue Sep 22, 2015 2:30 pm

I combined the relevant pieces into a minimalistic firmware that shows the crash. The source code is at https://github.com/jeelabs/esp-pico and can be compiled using esp-open-sdk and esp_iot_sdk_v1.4.0. The paths to these components are defined in the Makefile. There are only 3 .c files involved (in the core directory) so you should be able to compile it easily using your tools of choice also. Note that the firmware assumes that the wifi station SSID and password are already configured in the flash. You can also find a compiled binary at https://github.com/jeelabs/esp-pico/rel ... /user1.bin (this should be loaded at 0x1000 and uses the 4MB OTA firmware partition model with 2x512KB partitions).

Here is a sample output:

Code: Select all

 ets Jan  8 2013,rst cause:1, boot mode:(3,7)

load 0x40100000, len 1396, room 16
tail 4
chksum 0x89
load 0x3ffe8000, len 776, room 4
tail 4
chksum 0xe8
load 0x3ffe8308, len 540, room 4
tail 8
chksum 0xc0
csum 0xc0

2nd boot version : 1.4(b1)
  SPI Speed      : 80MHz
  SPI Mode       : QIO
  SPI Flash Size & Map: 32Mbit(512KB+512KB)
jump to run user1 @ 1000

r▒9l▒▒^!!!▒

** esp-pico master - 2015-09-21 23:01:49 - development
sleep enable,type: 1
force slp enable,type: 1
fpm open,type:1 0
Reset cause: 3=soft wdt
exccause=4 epc1=0x4021aace epc2=0x0 epc3=0x0 excvaddr=0x0 depc=0x0
Flash map 4MB:512/512, manuf 0xEF chip 0x4016
** esp-pico ready
mode : sta(18:fe:34:a6:e8:8f)
add if0
f r-1, scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt
  2094> wifi status=1 tick=0
  4094> wifi status=1 tick=0
  6094> wifi status=1 tick=0
state: 5 -> 2 (fa0)
rm 0
Wifi disconnected from ssid tve-home, reason group_key_update_timeout (15)
reconnect
state: 2 -> 0 (0)
f r-1, scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt
  8094> wifi status=2 tick=0

connected with tve-home, channel 1
dhcp client start...
Wifi connected to ssid tve-home, ch 1
 10094> wifi status=2 tick=0
 12094> wifi status=2 tick=0
ip:192.168.0.114,mask:255.255.255.0,gw:192.168.0.1
Wifi got ip:192.168.0.114,mask:255.255.255.0,gw:192.168.0.1
 14094> wifi status=5 tick=0
 16094> wifi status=5 tick=1
fpm close 3
pm open phy_2,type:1 0 0
 18094> wifi status=5 tick=2
 20094> wifi status=5 tick=3
 22094> wifi status=5 tick=4
Sleep!
pm2 684

 ets Jan  8 2013,rst cause:1, boot mode:(3,7)

load 0x40100000, len 1396, room 16
tail 4
chksum 0x89
load 0x3ffe8000, len 776, room 4
tail 4
chksum 0xe8
load 0x3ffe8308, len 540, room 4
tail 8
chksum 0xc0
csum 0xc0

2nd boot version : 1.4(b1)
  SPI Speed      : 80MHz
  SPI Mode       : QIO
  SPI Flash Size & Map: 32Mbit(512KB+512KB)
jump to run user1 @ 1000

r▒9l▒▒^!!!▒

** esp-pico master - 2015-09-21 23:01:49 - development
sleep enable,type: 1
force slp enable,type: 1
fpm open,type:1 0
Reset cause: 3=soft wdt
exccause=4 epc1=0x4021aace epc2=0x0 epc3=0x0 excvaddr=0x0 depc=0x0
Flash map 4MB:512/512, manuf 0xEF chip 0x4016

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: wifi_fpm_do_sleep crashes

Postby tve » Wed Sep 23, 2015 9:41 am

@Espressif_Faye do you have enough info to try this out? Thanks much!

ESP_Faye
Posts: 1641
Joined: Mon Oct 27, 2014 11:08 am

Re: wifi_fpm_do_sleep crashes

Postby ESP_Faye » Wed Sep 23, 2015 11:41 am

Hi,

In your log

Code: Select all

Wifi got ip:192.168.0.114,mask:255.255.255.0,gw:192.168.0.1
 14094> wifi status=5 tick=0
fpm close 3
pm open phy_2,type:1 0 0
 18094> wifi status=5 tick=2
Sleep!
pm2 684

It seems that you did not call wifi_station_disconnect(); and wifi_set_opmode_current(NULL_MODE); before enter force sleep mode.
Otherwise,the log should be

Code: Select all

ip:192.168.1.113,mask:255.255.255.0,gw:192.168.1.1
state: 5 -> 0 (0)


In your case, the ESP8266 is still in the staion mode and connected to a router. You need to call wifi_station_disconnect(); and wifi_set_opmode_current(NULL_MODE); before enter force sleep mode.

Code: Select all

wifi_station_disconnect();
wifi_set_opmode_current(NULL_MODE);
wifi_fpm_set_sleep_type(XXX);
wifi_fpm_open();

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: wifi_fpm_do_sleep crashes

Postby tve » Wed Sep 23, 2015 11:58 am

What you describe is almost exactly what I have: https://github.com/jeelabs/esp-pico/blo ... .c#L47-L54

Code: Select all

    // before entering light sleep we have to turn off wifi
    wifi_station_disconnect();
    wifi_set_opmode_current(NULL_MODE);
    os_delay_us(1000);
    // print a message and sleep enough for the uart to send the characters out
    os_printf("Sleep!\n");
    os_delay_us(600);
    // enter sleep mode
    int8_t err = wifi_fpm_do_sleep(10*1000);
    if (err != 0) os_printf("Sleep error: %d\n", err);
    // and here we're back?
    os_printf("Back!\n");
    wifi_set_opmode(STATION_MODE);
    wifi_station_connect();

Do I have to repeat the call to wifi_fpm_set_sleep_type(XXX) ? I only have that once at initialization time together with wifi_fpm_open: https://github.com/jeelabs/esp-pico/blo ... .c#L96-L97

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: wifi_fpm_do_sleep crashes

Postby tve » Wed Sep 23, 2015 12:26 pm

I added wifi_fpm_set_sleep_type(LIGHT_SLEEP_T) just before wifi_fpm_do_sleep but it's still not working.

Something I'm seeing is that after a reset I initialize fpm and it prints:

Code: Select all

force slp enable,type: 1
fpm open,type:1 0
and then the wifi eventually connects and I see:

Code: Select all

Wifi connected to ssid tve-home, ch 1
ip:192.168.0.114,mask:255.255.255.0,gw:192.168.0.1
fpm close 3
pm open phy_2,type:1 0 0

Why does it say "fpm close 3"? I didn't call wifi_fpm_close()

After this there is a failed sleep (notice sleep error -2):

Code: Select all

state: 5 -> 0 (0)
rm 0
pm close 7 0 1381340/2756243
del if0
usl
mode : null
Sleep!
force slp enable,type: 1
Sleep error: -2
Back!
mode : sta(18:fe:34:a6:e8:8f)
add if0

At the next attempt sleep crashes:
Wifi connected to ssid tve-home, ch 1

Code: Select all

ip:192.168.0.114,mask:255.255.255.0,gw:192.168.0.1
Wifi got ip:192.168.0.114,mask:255.255.255.0,gw:192.168.0.1
pm open phy_2,type:1 0 0
Sleep!
force slp enable,type: 1
pm2 684
▒@▒rjrA(▒

The crashing one has many fewer state transitions, it seems. Something I find suspicious in both cases is that "Sleep!" is printed before "force slp enable,type: 1" even though the code is

Code: Select all

os_printf("Sleep!\n");
os_delay_us(600);
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
int8_t err = wifi_fpm_do_sleep(100*1000);

This smells like a race condition somewhere?

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: wifi_fpm_do_sleep crashes

Postby tve » Wed Sep 23, 2015 12:33 pm

Sorry for the many posts, but I'm trying out different combinations. I changed the code further and now have:

Code: Select all

wifi_station_disconnect();
wifi_set_opmode_current(NULL_MODE);
os_delay_us(1000);
os_printf("Sleep!\n");
os_delay_us(600);
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
wifi_fpm_open();
int8_t err = wifi_fpm_do_sleep(100*1000);
if (err != 0) os_printf("Sleep error: %d\n", err);
os_printf("Back!\n");
wifi_set_opmode(STATION_MODE);
wifi_station_connect();

This does the sleep properly, but afterwards the wifi does not reconnect:

Code: Select all

ip:192.168.0.114,mask:255.255.255.0,gw:192.168.0.1
fpm close 3
pm open phy_2,type:1 0 0
Sleep!
force slp enable,type: 1
pm close 7 0 2131886/3837583
fpm open,type:1 0
Sleep error: 0
Back!
fpm wk err: pm is open
fpm wk err: pm is open
fpm wk err: pm is open
f r0, fpm wk err: pm is open
fpm wk err: pm is open
fpm wk err: pm is open
fpm wk err: pm is open
scandone
state: 5 -> 0 (0)
fpm wk err: pm is open
fpm wk err: pm is open
fpm wk err: pm is open
rm 0
fpm wk err: pm is open
fpm wk err: pm is open
del if0
usl
mode : null
Wifi disconnected from ssid tv▒ 뫫      ▒WV▒▒▒
R֮▒k▒ū+▒W$%NJC▒▒LC▒▒pm,w1
wifi status=255

After this wifi_station_get_connect_status() always returns 255

ESP_Faye
Posts: 1641
Joined: Mon Oct 27, 2014 11:08 am

Re: wifi_fpm_do_sleep crashes

Postby ESP_Faye » Wed Sep 23, 2015 3:45 pm

Hi,

Here is the documentation about sleep modehttp://bbs.espressif.com/viewtopic.php?f=51&t=645.

If ESP8266 entered the light sleep mode, it needs to be waked up via external GPIO before calling wifi_set_opmode(STATION_MODE); and wifi_station_connect();

Please have a try with the lib as attachment, we add an API to set the callback which will be entered after wake up from force sleep.

Code: Select all

void wifi_fpm_set_wakeup_cb(void (*fpm_wakeup_cb_func)(void));


Example:

Code: Select all

//sleep over.
void fpm_wakup_cb_func1(void)
{
   wifi_fpm_close();   //disable sleep function
   wifi_set_opmode(STATION_MODE);      //set wifi mode to station mode
   wifi_station_connect();         //connect ap
}

void user_func(...)

wifi_station_disconnect();
wifi_set_opmode(NULL_MODE);      //set wifi mode to null mode.
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);      //set force sleep type, clsoe rf&cpu
wifi_fpm_open();            //enable force sleep fucntion
wifi_fpm_set_wakeup_cb(fpm_wakup_cb_func1);   //Set fpm wakeup callback function
wifi_fpm_do_sleep(10*1000);         // do sleep
...
}
Attachments
libpp_fpm_sdk_v1.4.0.zip
(79.27 KiB) Downloaded 438 times

Who is online

Users browsing this forum: No registered users and 8 guests