espressif / esp-zigbee-sdk

Espressif Zigbee SDK
Apache License 2.0
154 stars 26 forks source link

esp_zb_sleep_now() does not wake up from gpio (TZ-268) #99

Closed blastrock closed 5 months ago

blastrock commented 1 year ago

Hello,

I'm trying to make my device sleep, and wake it up when a button is pressed. I am not sure what esp_zb_sleep_now() does. My signal handler looks like this:

  case ESP_ZB_COMMON_SIGNAL_CAN_SLEEP:
    ESP_ERROR_CHECK(
        gpio_wakeup_enable(GPIO_INPUT_IO_TOGGLE_SWITCH, GPIO_INTR_LOW_LEVEL));
    ESP_ERROR_CHECK(esp_sleep_enable_gpio_wakeup());
    esp_zb_sleep_now();
    ESP_LOGI(TAG, "wake up cause %d", esp_sleep_get_wakeup_cause());
    break;

But when I push my button, the controller will not wake up from sleep. And when it wakes up, the cause is always 4 (the timer).

Is this supposed to work, and my issue is somewhere else? What does the esp_zb_sleep_now() function do? Is there a way to just call esp_light_sleep_start() instead?

kelin6 commented 1 year ago

@blastrock ESP_ZB_COMMON_SIGNAL_CAN_SLEEP is a signal in the Zigbee stack that notifies the user stack when it's safe to put the device to sleep. When esp_zb_sleep_now is called, the stack provides the sleep duration, during which the device enters a low-power sleep mode, effectively shutting down RF and peripheral power.

To enable wake-up from sleep using GPIO, you should call ESP_ERROR_CHECK(esp_sleep_enable_gpio_wakeup()); before invoking esp_zb_init(). This setup should not be called repeatedly within this context.

agustinebrecht commented 1 year ago

Hello @kelin6 how are you?, is it possible to modify the time spent by the zigbee stack? In the Nordic zigbee sdk, it is possible to modify this time that is passed to the zb_osif_sleep function, in order to force more or less sleep time.

kelin6 commented 1 year ago

@agustinebrecht In the Nordic zigbee stack, there's a function call zb_osif_sleep(sleep_tmo) where sleep_tmo represents the amount of time the ZBOSS stack determines as suitable for sleeping, based on the time until the next scheduled task. This sleep duration cannot be directly modified.

If you wish to extend or shorten the sleep duration, you can adjust the value of ED_KEEP_ALIVE

agustinebrecht commented 1 year ago

Thank you very much for your reply @kelin6 . I take the opportunity to ask her about the gpio wake up that she mentioned to @blastrock . How do I indicate which pins have to wake me up due to interruption and which ones don't? since I have seen that the esp32H2 does not have configurations enabled in the esp-idf to establish that gpio raises the micro

blastrock commented 12 months ago

Thanks for the answer, I experimented a bit more. Unfortunately, moving the esp_sleep_enable_gpio_wakeup call does not seem to change anything.

I tried changing the value of esp_pm_config::light_sleep_enable, and calling esp_light_sleep_start instead of esp_zb_sleep_now. Of course, this breaks the zigbee stack, but this is just to check if the gpio wake up works. Here are the results:

I am a bit surprised by this zigbee sleep API. Can't we ask the zigbee stack for how long we need to sleep (or let it set up the rtc wake up) and do the esp_light_sleep_start call ourselves?

In case it helps, I pushed my code here

kelin6 commented 11 months ago

@blastrock

#define BOOT_BUTTON_NUM         GPIO_NUM_9     
#define GPIO_WAKEUP_NUM         BOOT_BUTTON_NUM  

esp_sleep_enable_ext1_wakeup(1ULL << GPIO_WAKEUP_NUM, ESP_EXT1_WAKEUP_ANY_HIGH);  

esp32h2_ext1_wakeup

blastrock commented 11 months ago

In the Zigbee sleep example, the TOP is powered down

How is it disabled? Is it the CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP config? Is it possible to just enable it back?

RTC_IO can be used as a wake-up source, as follows

I tried using RTC as you suggested. I completely disabled switch_driver.c's code so that the GPIO usage doesn't interfere with RTC, and I added this code to my main function:

  ESP_ERROR_CHECK(
      esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
  ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(
      1ULL << GPIO_INPUT_IO_TOGGLE_SWITCH, ESP_EXT1_WAKEUP_ANY_HIGH));
  rtc_gpio_pullup_dis(GPIO_INPUT_IO_TOGGLE_SWITCH);
  rtc_gpio_pulldown_en(GPIO_INPUT_IO_TOGGLE_SWITCH);

I am using GPIO0 here. This code works when I test it with a while(true) esp_light_sleep_start();, but I still can't make it work with the zigbee sleep. I do have some wake ups with cause 3, but they seem to occur a bit randomly, definitely not right when I connect the pin. It usually takes a multiple seconds, almost as if it woke up because of the clock, despite the cause showing as 3

kelin6 commented 11 months ago

@blastrock

#define BOOT_BUTTON_NUM         GPIO_NUM_9     
#define GPIO_WAKEUP_NUM         BOOT_BUTTON_NUM  
esp_sleep_enable_ext1_wakeup(1ULL << GPIO_WAKEUP_NUM, ESP_EXT1_WAKEUP_ANY_HIGH);  

Using the code above is indeed feasible, and I have tested it. The occasional 3 printing occurs because the esp_zb_sleep_now(); is followed by the print statement. This causes the reason to be printed only when esp_timer wakes up. I recommend adding a GPIO interrupt service function to print the wake-up reason within it. This way, when GPIO wakes up and triggers the GPIO interrupt, the wake-up reason can be obtained immediately. You can refer to the following example GPIO wakeup.

blastrock commented 10 months ago

Thanks a lot for your help!

I realized that esp_zb_sleep_now does not actually make the device sleep, it makes the task sleep, and then the OS puts the device to sleep when there are no pending tasks. This, and your other advises helped me debug my code.

I have a couple bugs left. The first one is that when I press the button just before the zigbee task goes to sleep, the esp_zb_zcl_on_off_cmd_req function returns, but the zigbee message is not sent until the device wakes up again.

Then, I noticed that a new zigbee sdk was released, so I upgraded from 0.6.0 to 1.0.1 to see if the previous bug has been already fixed. Now, when I call esp_zb_sleep_enable, my device won't join anymore and crashes. When trying to join the network, it gets a LEAVE RESET signal. After a few seconds, it crashes with the following log:

I (634) ieee802154: ieee802154 mac sleep retention initialization
I (652) zigbee: ZDO signal: ZDO Config Ready (0x17), status: ESP_FAIL
I (653) zigbee: ZDO signal: ZDO Skip Start Up (0x1), status: ESP_OK
I (655) zigbee: Zigbee stack initialized
I (661) zigbee: ZDO signal: BDB Device Start (0x5), status: ESP_OK
I (667) zigbee: Start network steering
I (651) main_task: Returned from app_main()
I (9271) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9645) zigbee: zigbee wakeup
I (9647) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9788) zigbee: zigbee wakeup
I (9789) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9884) zigbee: zigbee wakeup
I (19292) zigbee: ZDO signal: ZDO Leave (0x3), status: ESP_OK
I (19293) zigbee: Reset device

abort() was called at PC 0x42017bb3 on core 0
0x42017bb3: zb_assert at ??:?

Core  0 register dump:
MEPC    : 0x4080063a  RA      : 0x40809966  SP      : 0x4087e150  GP      : 0x4080fbf4  
0x4080063a: panic_abort at /home/blastrock/esp/esp-idf/components/esp_system/panic.c:452

0x40809966: __ubsan_include at /home/blastrock/esp/esp-idf/components/esp_system/ubsan.c:313

TP      : 0x4086da5c  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130  
S0/FP   : 0x00000004  S1      : 0x4087e1b4  A0      : 0x4087e17c  A1      : 0x4087e1b2  
A2      : 0x00000000  A3      : 0x4087e1a9  A4      : 0x00000001  A5      : 0x40817000  
A6      : 0x00000000  A7      : 0x76757473  S2      : 0x00000001  S3      : 0x0000077b  
S4      : 0x00000038  S5      : 0x00000000  S6      : 0x00000000  S7      : 0x00000000  
S8      : 0x00000000  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000  
T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938  
MSTATUS : 0x00001881  MTVEC   : 0x40800001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000  
0x40800001: _vector_table at ??:?

MHARTID : 0x00000000  

Stack memory:
4087e150: 0x4081b1e0 0x40880000 0x4087e1b0 0x4080e2be 0x4206d0e8 0x00000225 0x00000001 0x408106a0
0x4080e2be: abort at /home/blastrock/esp/esp-idf/components/newlib/abort.c:35 (discriminator 3)

4087e170: 0x4087e1b4 0x408106bc 0x4087e1b0 0x726f6261 0x20292874 0x20736177 0x6c6c6163 0x61206465
4087e190: 0x43502074 0x34783020 0x37313032 0x20336262 0x63206e6f 0x2065726f 0x00000030 0x42050000
0x42050000: esp_task_wdt_reset at /home/blastrock/esp/esp-idf/components/esp_system/task_wdt/task_wdt.c:783 (discriminator 3)

4087e1b0: 0x00000030 0x31303234 0x33626237 0x00000700 0x00000000 0x00000000 0x00000038 0x42017bb6
0x42017bb6: zb_globals_init at ??:?

4087e1d0: 0x4087ee0c 0x4087ee34 0x40812070 0x42026274 0x00000000 0x00000000 0x4087ce74 0x4201c7f4
0x42026274: zb_multimac_mlme_reset_request_proxy_send_req at nwk_multimac_proxy.c.obj:?

0x4201c7f4: zb_sched_loop_iteration at ??:?

4087e1f0: 0x00000000 0x42026260 0x0000077b 0x423b391e 0x0000003c 0x00000004 0x4087ed98 0x4087ede4
0x42026260: zb_multimac_debug_transaction_stuck_handler at nwk_multimac_proxy.c.obj:?

4087e210: 0x4087ee0c 0x4087ee34 0x4087ce74 0x42037cbe 0x00000000 0x00000000 0x4087f544 0x420103ae
0x42037cbe: zboss_main_loop_iteration at ??:?

0x420103ae: esp_zb_main_loop_iteration at ??:?

4087e230: 0x4087ee0c 0x4087ee34 0x4087ce74 0x4200b2ac 0x00000000 0x00000008 0x00000000 0x00000000
0x4200b2ac: esp_zb_task at /home/blastrock/work/esp32/HA_on_off_light/main/esp_zb_light.c:372 (discriminator 2)

4087e250: 0x00000000 0x00000002 0x00060000 0x00009c40 0x00000000 0x00000000 0x00000000 0x00000000
4087e270: 0x00000000 0x00000000 0x00000000 0x4080be02 0x00000000 0x00000000 0x00000000 0x00000000
0x4080be02: vPortTaskWrapper at /home/blastrock/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:205

4087e290: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
4087e2b0: 0xa5a5a5a5 0xa5a5a5a5 0x00000154 0x4087e080 0x000070ca 0x40811110 0x40811110 0x4087e2bc
4087e2d0: 0x40811108 0x00000014 0x4081b2a0 0x4081b2a0 0x4087e2bc 0x00000000 0x00000005 0x4087d2b8
4087e2f0: 0x6267695a 0x6d5f6565 0x006e6961 0x00000000 0x00000000 0x4087e2b0 0x00000005 0x00000000
4087e310: 0x00000000 0x00000000 0x00000009 0x40818288 0x408182f0 0x40818358 0x00000000 0x00000000
4087e330: 0x00000001 0x00000000 0x00000000 0x00000000 0x4205d224 0x00000000 0x00000000 0x00000000
0x4205d224: _cleanup_r at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdio/findfp.c:229

4087e350: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e370: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e390: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e3b0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e3d0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e3f0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x40000000
0x40000000: _start in ROM

4087e410: 0x00000020 0x00000000 0x00000000 0x00000003 0x42071920 0x4081b0b0 0x00000001 0xb33fffff
4087e430: 0x00000000 0x00000054 0x00000000 0x4087e438 0x00000000 0x00000000 0x00000000 0x4087e450
4087e450: 0xffffffff 0x4087e450 0x4087e450 0x00000000 0x4087e464 0xffffffff 0x4087e464 0x4087e464
4087e470: 0x00000001 0x00000001 0x00000000 0x0000ffff 0x00000000 0xb33fffff 0x00000000 0x00000054
4087e490: 0x00000000 0x4087e490 0x00000000 0x00000000 0x00000000 0x4087e4a8 0xffffffff 0x4087e4a8
4087e4b0: 0x4087e4a8 0x00000000 0x4087e4bc 0xffffffff 0x4087e4bc 0x4087e4bc 0x00000001 0x00000001
4087e4d0: 0x00000000 0x4000ffff 0x00000000 0xb33fffff 0x00000000 0x00000054 0x00000000 0x4087e4e8
0x4000ffff: ppRxFragmentProc in ROM

4087e4f0: 0x00000000 0x00000000 0x00000000 0x4087e500 0xffffffff 0x4087e500 0x4087e500 0x00000000
4087e510: 0x4087e514 0xffffffff 0x4087e514 0x4087e514 0x00000001 0x00000001 0x00000000 0x1d00ffff
4087e530: 0x00000000 0xb33fffff 0x00000000 0x00000028 0xffff0000 0x00002000 0x70030000 0x4081a100

ELF file SHA256: 496866645743efa3

Rebooting...

Sometimes it happens in another function like map_vram_to_pram, but I can't reproduce it anymore. I pushed my code here: https://github.com/blastrock/HA_on_off_light/blob/wip/main/esp_zb_light.c . I have disabled all the GPIO code, there is only the zigbee task left.

Should I open another issue for this?

EDIT: I just tried compiling the sleepy_end_device example from the main branch, it crashes too.

Bosemani commented 10 months ago

Hai @blastrock Did you solved this issue? I'm also facing same!

blastrock commented 10 months ago

I haven't, I have no idea how I can investigate this bug.

blastrock commented 10 months ago

I have reported this as a proper issue in https://github.com/espressif/esp-zigbee-sdk/issues/148 to give it more visibility. It includes more detailed steps to reproduce the issue.

kelin6 commented 10 months ago

@blastrock

According to your current code, When the button is pressed before the Zigbee stack can sleep, the Zigbee stack sends a sleep signal (ESP_ZB_COMMON_SIGNAL_CAN_SLEEP) to the user after it becomes possible for Zigbee to sleep. After that, esp_zb_sleep_now is called, causing the device to sleep. The device wakes up when esp_zb_sleep_now exits, and check_gpio(button_func_pair, PAIR_SIZE(button_func_pair)); is then executed, leading to the delayed sending of the Zigbee on/off command until the device wakes up again.

blastrock commented 10 months ago

It should be called within the GPIO interrupt IRAM_ATTR gpio_isr_handler.

Yes, I changed that after your previous message, but didn't push my code yet, I was still experimenting.

Regarding the second issue, updating to IDF version v5.1.2 should resolve it

According to this comment https://github.com/espressif/esp-zigbee-sdk/issues/148#issuecomment-1813875241 ESP32-C6 is not supported at all...?

I have just tested upgrading to 5.1.2, I can confirm that I'm using the right version:

I (17) boot: ESP-IDF v5.1.2 2nd stage bootloader

However, I still have the exact same crash with the sleepy_end_device example.

kelin6 commented 10 months ago

@blastrock Yes, as of ESP-IDF V5.1 (commit: 482a8fb2d7), Zigbee sleep for ESP32-C6 is currently not supported. However, Zigbee sleep is supported for ESP32-H2, and I have confirmed and tested this.

The relevant support code for ESP32-C6 Zigbee sleep has been internally reviewed and merged. It may take a few weeks for these changes to be visible on GitHub. I will notify you promptly once the support is available on GitHub.

blastrock commented 10 months ago

I only have a C6, so I can't test on an H2. I just noticed I didn't specify I was using a C6 on this issue, sorry for that.

I'll just wait for the next release then. Thanks for your help!

chshu commented 9 months ago

@blastrock The C6 light sleep issue has been fixed in IDF master, could you update both esp-zigbee-sdk and esp-idf to the latest master? Let us know if you see any other issues.

blastrock commented 9 months ago

Thanks a lot for helping me out through this. Unfortunately I can't seem to make my code work. I have tested again my code with esp-idf's commit https://github.com/espressif/esp-idf/commit/f0607f6d3b4b3bfcbebdd9c580c6a034a183aad6 from master and esp-zigbee-sdk and zboss 1.0.6 which just got released.

My ESP32-C6 now connects to the zigbee network and goes to sleep. I have set it up to wake up with EXT1 RTC and I use the GPIO interrupt to process the event. The problem is now that when the ESP goes back to sleep, it crashes. The stack trace does not contain my code, nor esp-zigbee-sdk's code, so I'm not sure where the bug is. Here is the log of the crash:

I (17) boot: ESP-IDF v5.3-dev-867-gf0607f6d3b 2nd stage bootloader
I (18) boot: compile time Dec  8 2023 19:58:07
I (19) boot: chip revision: v0.0
I (22) boot.esp32c6: SPI Speed      : 80MHz
I (27) boot.esp32c6: SPI Mode       : DIO
I (31) boot.esp32c6: SPI Flash Size : 2MB
I (36) boot: Enabling RNG early entropy source...
I (42) boot: Partition Table:
I (45) boot: ## Label            Usage          Type ST Offset   Length
I (52) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (60) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (67) boot:  2 factory          factory app      00 00 00010000 000a2800
I (75) boot:  3 zb_storage       Unknown data     01 81 000b3000 00004000
I (82) boot:  4 zb_fct           Unknown data     01 81 000b7000 00000400
I (90) boot: End of partition table
I (94) esp_image: segment 0: paddr=00010020 vaddr=42068020 size=12d98h ( 77208) map
I (134) esp_image: segment 1: paddr=00022dc0 vaddr=40800000 size=05258h ( 21080) load
I (145) esp_image: segment 2: paddr=00028020 vaddr=42000020 size=652e4h (414436) map
I (312) esp_image: segment 3: paddr=0008d30c vaddr=40805258 size=0b730h ( 46896) load
I (335) esp_image: segment 4: paddr=00098a44 vaddr=40810990 size=01e6ch (  7788) load
I (340) esp_image: segment 5: paddr=0009a8b8 vaddr=50000000 size=00058h (    88) load
I (348) boot: Loaded app from partition at offset 0x10000
I (349) boot: Disabling RNG early entropy source...
I (364) cpu_start: Unicore app
W (374) clk: esp_perip_clk_init() has not been implemented yet
I (381) cpu_start: Pro cpu start user code
I (381) cpu_start: cpu freq: 160000000 Hz
I (381) cpu_start: Application information:
I (384) cpu_start: Project name:     light_bulb
I (389) cpu_start: App version:      b2fb8a5-dirty
I (395) cpu_start: Compile time:     Dec  8 2023 19:57:58
I (401) cpu_start: ELF file SHA256:  048aba923...
I (406) cpu_start: ESP-IDF:          v5.3-dev-867-gf0607f6d3b
I (412) cpu_start: Min chip rev:     v0.0
I (417) cpu_start: Max chip rev:     v0.99 
I (422) cpu_start: Chip rev:         v0.0
I (427) heap_init: Initializing. RAM available for dynamic allocation:
I (434) heap_init: At 40819C50 len 000629C0 (394 KiB): RAM
I (440) heap_init: At 4087C610 len 00002F54 (11 KiB): RAM
I (446) heap_init: At 50000058 len 00003F90 (15 KiB): RTCRAM
I (453) spi_flash: detected chip: generic
I (457) spi_flash: flash io: dio
W (461) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (474) sleep: Enable automatic switching of GPIO sleep configuration
I (482) sleep_clock: System Power, Clock and Reset sleep retention initialization
I (490) sleep_clock: Modem Power, Clock and Reset sleep retention initialization
I (498) sleep_sys_periph: Interrupt Matrix sleep retention initialization
I (505) sleep_sys_periph: HP System sleep retention initialization
I (512) sleep_sys_periph: TEE/APM sleep retention initialization
I (519) sleep_sys_periph: UART sleep retention initialization
I (525) sleep_sys_periph: Timer Group sleep retention initialization
I (532) sleep_sys_periph: IO Matrix sleep retention initialization
I (539) sleep_sys_periph: SPI Mem sleep retention initialization
I (546) sleep_sys_periph: SysTimer sleep retention initialization
I (564) coexist: coex firmware version: e41c5cb
I (564) coexist: coexist rom version 5b8dcfa
I (564) main_task: Started on CPU0
I (566) main_task: Calling app_main()
I (570) zigbee: wake up early 0
I (580) pm: Frequency switching config: CPU_MAX: 160, APB_MAX: 160, APB_MIN: 160, Light sleep: ENABLED
I (584) gpio: GPIO[0]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 1| Intr:5 
I (594) esp_zb_sleep: Enable ieee802154 zigbee light sleep, the wake up source is ESP timer
I (603) phy_init: phy_version 230,c773401,Oct 30 2023,15:07:16
I (652) phy: libbtbb version: 7243671, Oct 30 2023, 15:07:30
I (653) btbb_init: btbb sleep retention initialization
I (654) ieee802154: ieee802154 mac sleep retention initialization
I (672) main_task: Returned from app_main()
I (673) zigbee: ZDO signal: ZDO Config Ready (0x17), status: ESP_FAIL
I (674) zigbee: ZDO signal: ZDO Skip Start Up (0x1), status: ESP_OK
I (680) zigbee: Zigbee stack initialized
I (688) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (691) zigbee: zigbee wakeup
I (695) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (701) zigbee: zigbee wakeup
I (705) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (711) zigbee: zigbee wakeup
I (715) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (721) zigbee: zigbee wakeup
I (725) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (731) zigbee: zigbee wakeup
I (735) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (741) zigbee: zigbee wakeup
I (8871) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9384) zigbee: zigbee wakeup
I (9410) zigbee: ZDO signal: BDB Device Reboot (0x6), status: ESP_OK
I (9434) zigbee: Start network steering
I (9442) zigbee: ZDO signal: BDB Steering (0xa), status: ESP_OK
I (9442) zigbee: Joined network successfully (Extended PAN ID: 00:12:4b:00:25:8c:5c:86, PAN ID: 0x1a62, Channel:11)
I (9449) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9528) zigbee: zigbee wakeup
I (9535) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9686) zigbee: zigbee wakeup
I (9686) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9921) zigbee: zigbee wakeup
I (9921) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10033) zigbee: zigbee wakeup
I (10033) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10130) zigbee: zigbee wakeup
I (10141) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10241) zigbee: zigbee wakeup
I (10247) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10353) zigbee: zigbee wakeup
I (10359) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10511) zigbee: zigbee wakeup
I (10517) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10746) zigbee: zigbee wakeup
I (10752) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (11088) zigbee: zigbee wakeup
I (11094) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (11599) zigbee: zigbee wakeup
I (11605) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (12356) zigbee: zigbee wakeup
I (12361) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (12398) switch: GPIO interrupts: off
I (12398) switch: GPIO ISR HANDLER cause 4
I (12399) switch: queue event
I (12406) switch: loop resume
I (12406) switch: state is 0, value is 1
I (12408) switch: state is 2, value is 1
I (12413) switch: state is 2, value is 1
I (12417) switch: state is 2, value is 1
I (12422) switch: state is 2, value is 1
I (12426) switch: state is 2, value is 1
I (12431) switch: state is 2, value is 1
I (12436) switch: state is 2, value is 1
I (12440) switch: state is 2, value is 1
I (12445) switch: state is 2, value is 1
I (12449) switch: state is 2, value is 1
I (12454) switch: state is 2, value is 1
I (12459) switch: state is 2, value is 1
I (12463) switch: state is 2, value is 1
I (12468) switch: state is 2, value is 1
I (12472) switch: state is 2, value is 1
I (12477) switch: state is 2, value is 1
I (12482) switch: state is 2, value is 1
I (12486) switch: state is 2, value is 1
I (12491) switch: state is 2, value is 1
I (12495) switch: state is 2, value is 1
I (12500) switch: state is 2, value is 1
I (12505) switch: state is 2, value is 1
I (12509) switch: state is 2, value is 1
I (12514) switch: state is 2, value is 1
I (12518) switch: state is 2, value is 1
I (12523) switch: state is 2, value is 1
I (12528) switch: state is 2, value is 1
I (12532) switch: state is 2, value is 1
I (12537) switch: state is 2, value is 1
I (12541) switch: state is 2, value is 1
I (12546) switch: state is 2, value is 1
I (12551) switch: state is 2, value is 1
I (12555) switch: state is 2, value is 1
I (12560) switch: state is 2, value is 1
I (12564) switch: state is 2, value is 0
I (12569) switch: state is 4, value is 0
I (12574) zigbee: Send 'on_off toggle' command
I (12578) zigbee: done!
I (12582) switch: back to sleep
I (12586) switch: GPIO interrupts: on
Guru Meditation Error: Core  0 panic'ed (Load access fault). Exception was unhandled.

Core  0 register dump:
MEPC    : 0x40801f58  RA      : 0x408024e2  SP      : 0x4081ece0  GP      : 0x40811194  
0x40801f58: cpu_domain_dev_regs_save at /home/blastrock/esp/esp-idf/components/esp_hw_support/sleep_cpu.c:635

0x408024e2: esp_sleep_cpu_retention at /home/blastrock/esp/esp-idf/components/esp_hw_support/sleep_cpu.c:713

TP      : 0x4080cbfc  T0      : 0x40027bea  T1      : 0x4081d770  T2      : 0x00000000  
0x4080cbfc: xTaskPriorityDisinherit at /home/blastrock/esp/esp-idf/components/freertos/FreeRTOS-Kernel/tasks.c:5107

0x40027bea: Cache_Suspend_ICache in ROM

S0/FP   : 0x408112b0  S1      : 0x4080788c  A0      : 0x4081f90c  A1      : 0x4081d780  
0x4080788c: pmu_sleep_start at /home/blastrock/esp/esp-idf/components/esp_hw_support/port/esp32c6/pmu_sleep.c:256

A2      : 0x00000012  A3      : 0x00000000  A4      : 0x00000002  A5      : 0x00000000  
A6      : 0x00000002  A7      : 0x00000002  S2      : 0x000d8d2d  S3      : 0x00000012  
S4      : 0x00000012  S5      : 0x00000000  S6      : 0x00000000  S7      : 0x00000089  
S8      : 0x00000012  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000  
T3      : 0x40000000  T4      : 0x000002b0  T5      : 0x00000000  T6      : 0x00000000  
0x40000000: _start in ROM

MSTATUS : 0x00001801  MTVEC   : 0x40800001  MCAUSE  : 0x00000005  MTVAL   : 0x00000000  
0x40800001: _vector_table at ??:?

MHARTID : 0x00000000  

Stack memory:
4081ece0: 0x0c000000 0x00000000 0x00000000 0x408024e2 0x000d8d2d 0x00000002 0x00000001 0x00000000
0x408024e2: esp_sleep_cpu_retention at /home/blastrock/esp/esp-idf/components/esp_hw_support/sleep_cpu.c:713

4081ed00: 0x00000000 0x00000000 0x00000001 0x00000000 0x000d8d2d 0x00000010 0x00000000 0x40801ca4
0x40801ca4: esp_sleep_start at /home/blastrock/esp/esp-idf/components/esp_hw_support/sleep_modes.c:803

4081ed20: 0xa5a5a5a5 0xa5a5a5a5 0xa8200000 0x0c000000 0x00000000 0x00000000 0x40000000 0x00000000
0x40000000: _start in ROM

4081ed40: 0xc0000000 0x00000000 0x00000000 0x00000000 0xc0000000 0x08040000 0x00000000 0x00000000
4081ed60: 0x00000000 0x00000000 0xc0000000 0x60400000 0x00000000 0x000045a6 0x0000091e 0x001e001e
4081ed80: 0x0000000f 0x00000036 0x1236001e 0x0ece1e00 0x00000001 0x000001e0 0x00000003 0x000000a0
4081eda0: 0x00000000 0x00007c17 0x00000000 0x00000000 0x00000000 0x00007c17 0x00000000 0x00000000
4081edc0: 0x00000000 0x000d8d2d 0x00007c17 0x40801e28 0x00bfadf5 0x000d8d2d 0x40810a80 0x42004572
0x40801e28: esp_light_sleep_inner at /home/blastrock/esp/esp-idf/components/esp_hw_support/sleep_modes.c:990

0x42004572: esp_light_sleep_start at /home/blastrock/esp/esp-idf/components/esp_hw_support/sleep_modes.c:1221

4081ede0: 0x00000000 0x00000000 0x00000000 0x600b1c00 0x00000000 0x00000000 0x00000000 0x00000000
4081ee00: 0x0000037a 0x00000000 0x00bfade2 0x40804290 0x00000000 0x00000000 0x00000000 0x4080c648
0x40804290: vApplicationSleep at /home/blastrock/esp/esp-idf/components/esp_pm/pm_impl.c:811

0x4080c648: prvIdleTask at /home/blastrock/esp/esp-idf/components/freertos/FreeRTOS-Kernel/tasks.c:4309

4081ee20: 0x00000000 0x00000000 0x00000000 0x4080b378 0x00000000 0x00000000 0x00000000 0x00000000
0x4080b378: vPortTaskWrapper at /home/blastrock/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:258

4081ee40: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
4081ee60: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0x00000150 0x4081ed80 0x00000000 0x40812a0c
4081ee80: 0x40812a0c 0x4081ee74 0x40812a04 0x00000019 0x00000000 0x00000000 0x4081ee74 0x00000000
4081eea0: 0x00000000 0x4081e870 0x454c4449 0x00000000 0x00000000 0x00000000 0x4081ee60 0x00000000
4081eec0: 0x00000000 0x00000000 0x00000000 0x00000000 0x4081a730 0x4081a798 0x4081a800 0x00000000
4081eee0: 0x00000000 0x00000001 0x00000000 0x00000000 0x00000000 0x42007c08 0x00000000 0x00000000
0x42007c08: esp_cleanup_r at /home/blastrock/esp/esp-idf/components/newlib/newlib_init.c:60

4081ef00: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4081ef20: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4081ef40: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4081ef60: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4081ef80: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4081efa0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4081efc0: 0x00000200 0x0000000c 0x0e56000a 0x00000000 0x4081f09c 0x00000054 0x00000000 0x4081efd8
4081efe0: 0x00000000 0x00000000 0x00000000 0x4081eff0 0xffffffff 0x4081eff0 0x4081eff0 0x00000000
4081f000: 0x4081f004 0xffffffff 0x4081f004 0x4081f004 0x00000001 0x00000001 0x00000000 0xc100ffff
4081f020: 0x00000000 0xad54d7b6 0x4081efd0 0x00000054 0x00000000 0x4081f030 0x00000000 0x00000000
4081f040: 0x00000000 0x4081f048 0xffffffff 0x4081f048 0x4081f048 0x00000000 0x4081f05c 0xffffffff
4081f060: 0x4081f05c 0x4081f05c 0x00000001 0x00000001 0x00000000 0x6900ffff 0x00000000 0x28622496
4081f080: 0x4081f028 0x00000010 0x40819774 0x00000000 0x00000000 0x00000100 0x0000000c 0x0d580008
4081f0a0: 0x00000000 0x408205c0 0x00000054 0x00000000 0x4081f0ac 0x00000000 0x00000000 0x00000000
4081f0c0: 0x4081f0c4 0xffffffff 0x4081f0c4 0x4081f0c4 0x00000000 0x4081f0d8 0xffffffff 0x4081f0d8

ELF file SHA256: 048aba923

Rebooting...

I pushed my latest code https://github.com/blastrock/HA_on_off_light/tree/wip . If I disable the zigbee task, my program does not crash. Also, I've made it so that the button only prints logs on the screen and commented the line that sends a zigbee command.

kelin6 commented 9 months ago

@blastrock Based on your code, I have reproduced the same phenomenon. The issue lies in the fact that the task stack allocation for button_detected with a size of 2048 is too small, leading to a stack overflow and system crash. After increasing the button_detected task stack size to 4096, everything runs normally. The modifications based on your code are as follows:

image

Below is the log during runtime and the current situation of RTC IO wakeup:

I (23) boot: ESP-IDF v5.3-dev-867-gf0607f6d3b-dirty 2nd stage bootloader
I (24) boot: compile time Dec 19 2023 21:39:49
I (25) boot: chip revision: v0.0
I (28) boot.esp32c6: SPI Speed      : 80MHz
I (33) boot.esp32c6: SPI Mode       : DIO
I (38) boot.esp32c6: SPI Flash Size : 2MB
I (43) boot: Enabling RNG early entropy source...
I (48) boot: Partition Table:
I (52) boot: ## Label            Usage          Type ST Offset   Length
I (59) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (66) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (74) boot:  2 factory          factory app      00 00 00010000 000a2800
I (81) boot:  3 zb_storage       Unknown data     01 81 000b3000 00004000
I (89) boot:  4 zb_fct           Unknown data     01 81 000b7000 00000400
I (96) boot: End of partition table
I (100) esp_image: segment 0: paddr=00010020 vaddr=42068020 size=12d98h ( 77208) map
I (125) esp_image: segment 1: paddr=00022dc0 vaddr=40800000 size=05258h ( 21080) load
I (130) esp_image: segment 2: paddr=00028020 vaddr=42000020 size=652e0h (414432) map
I (215) esp_image: segment 3: paddr=0008d308 vaddr=40805258 size=0b730h ( 46896) load
I (227) esp_image: segment 4: paddr=00098a40 vaddr=40810990 size=01e6ch (  7788) load
I (229) esp_image: segment 5: paddr=0009a8b4 vaddr=50000000 size=00058h (    88) load
I (236) boot: Loaded app from partition at offset 0x10000
I (239) boot: Disabling RNG early entropy source...
I (255) cpu_start: Unicore app
W (264) clk: esp_perip_clk_init() has not been implemented yet
I (271) cpu_start: Pro cpu start user code
I (271) cpu_start: cpu freq: 160000000 Hz
I (271) cpu_start: Application information:
I (274) cpu_start: Project name:     light_bulb
I (279) cpu_start: App version:      6cb8617
I (284) cpu_start: Compile time:     Dec 20 2023 12:24:37
I (290) cpu_start: ELF file SHA256:  e63539e69...
I (296) cpu_start: ESP-IDF:          v5.3-dev-867-gf0607f6d3b
I (302) cpu_start: Min chip rev:     v0.0
I (307) cpu_start: Max chip rev:     v0.99 
I (312) cpu_start: Chip rev:         v0.0
I (316) heap_init: Initializing. RAM available for dynamic allocation:
I (323) heap_init: At 40819C50 len 000629C0 (394 KiB): RAM
I (330) heap_init: At 4087C610 len 00002F54 (11 KiB): RAM
I (336) heap_init: At 50000058 len 00003F90 (15 KiB): RTCRAM
I (343) spi_flash: detected chip: generic
I (347) spi_flash: flash io: dio
W (351) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (364) sleep: Enable automatic switching of GPIO sleep configuration
I (371) sleep_clock: System Power, Clock and Reset sleep retention initialization
I (379) sleep_clock: Modem Power, Clock and Reset sleep retention initialization
I (387) sleep_sys_periph: Interrupt Matrix sleep retention initialization
I (395) sleep_sys_periph: HP System sleep retention initialization
I (402) sleep_sys_periph: TEE/APM sleep retention initialization
I (408) sleep_sys_periph: UART sleep retention initialization
I (415) sleep_sys_periph: Timer Group sleep retention initialization
I (422) sleep_sys_periph: IO Matrix sleep retention initialization
I (429) sleep_sys_periph: SPI Mem sleep retention initialization
I (436) sleep_sys_periph: SysTimer sleep retention initialization
I (453) coexist: coex firmware version: e41c5cb
I (453) coexist: coexist rom version 5b8dcfa
I (454) main_task: Started on CPU0
I (456) main_task: Calling app_main()
I (460) zigbee: wake up early 0
I (470) pm: Frequency switching config: CPU_MAX: 160, APB_MAX: 160, APB_MIN: 160, Light sleep: ENABLED
I (474) gpio: GPIO[0]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 1| Intr:5 
I (484) esp_zb_sleep: Enable ieee802154 zigbee light sleep, the wake up source is ESP timer
I (493) phy_init: phy_version 230,c773401,Oct 30 2023,15:07:16
W (499) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration
I (550) phy: libbtbb version: 7243671, Oct 30 2023, 15:07:30
I (551) btbb_init: btbb sleep retention initialization
I (552) ieee802154: ieee802154 mac sleep retention initialization
I (582) main_task: Returned from app_main()
I (650) zigbee: ZDO signal: ZDO Config Ready (0x17), status: ESP_FAIL
I (651) zigbee: ZDO signal: ZDO Skip Start Up (0x1), status: ESP_OK
I (654) zigbee: Zigbee stack initialized
I (660) zigbee: ZDO signal: BDB Device Start (0x5), status: ESP_OK
I (665) zigbee: Start network steering
I (9273) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9570) zigbee: zigbee wakeup
I (9571) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9789) zigbee: zigbee wakeup
I (9790) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (9886) zigbee: zigbee wakeup
I (9936) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10020) zigbee: zigbee wakeup
I (10076) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10190) zigbee: zigbee wakeup
I (10221) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10302) zigbee: zigbee wakeup
I (10315) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10414) zigbee: zigbee wakeup
I (10444) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10541) zigbee: zigbee wakeup
I (10553) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10668) zigbee: zigbee wakeup
I (10681) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (10780) zigbee: zigbee wakeup
I (10816) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (11015) zigbee: zigbee wakeup
I (11016) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (11311) zigbee: zigbee wakeup
I (11339) zigbee: ZDO signal: BDB Steering (0xa), status: ESP_OK
I (11361) zigbee: Joined network successfully (Extended PAN ID: 60:55:f9:ff:fe:f7:3e:7f, PAN ID: 0x2bfa, Channel:13)
I (11361) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (15003) zigbee: zigbee wakeup
I (15004) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (16405) zigbee: zigbee wakeup
I (16411) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (20080) zigbee: zigbee wakeup
I (20081) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (21482) zigbee: zigbee wakeup
I (21487) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (26571) zigbee: zigbee wakeup
I (26576) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (31644) zigbee: zigbee wakeup
I (31650) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (32501) switch: GPIO interrupts: off
I (32501) switch: GPIO ISR HANDLER cause 3
I (32508) switch: queue event
I (32508) switch: loop resume
I (32508) switch: state is 0, value is 1
I (32513) switch: state is 2, value is 1
I (32517) switch: state is 2, value is 1
I (32522) switch: state is 2, value is 0
I (32527) switch: state is 4, value is 0
I (32531) zigbee: Send 'on_off toggle' command
I (32536) zigbee: done!
I (32539) switch: back to sleep
I (32543) switch: GPIO interrupts: on
I (32573) switch: GPIO interrupts: off
I (32573) switch: GPIO ISR HANDLER cause 3
I (32591) switch: queue event
I (32591) switch: loop resume
I (32591) switch: state is 0, value is 1
I (32596) switch: state is 2, value is 1
I (32600) switch: state is 2, value is 1
I (32605) switch: state is 2, value is 1
I (32610) switch: state is 2, value is 1
I (32614) switch: state is 2, value is 1
I (32619) switch: state is 2, value is 1
I (32623) switch: state is 2, value is 0
I (32628) switch: state is 4, value is 0
I (32633) zigbee: Send 'on_off toggle' command
I (32638) zigbee: done!
I (32641) switch: back to sleep
I (32645) switch: GPIO interrupts: on
I (32649) zigbee: zigbee wakeup
I (326I (32654) switch: GPIO interrupts: off
I (32654) switch: GPIO ISR HANDLER cause 3
54) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (32663) switch: queue event
I (32672) switch: loop resume
I (32676) switch: state is 0, value is 1
I (32681) switch: state is 2, value is 1
I (32685) switch: state is 2, value is 1
I (32690) switch: state is 2, value is 1
I (32694) switch: state is 2, value is 1
I (32699) switch: state is 2, value is 1
I (32704) switch: state is 2, value is 1
I (32708) switch: state is 2, value is 1
I (32713) switch: state is 2, value is 1
I (32717) switch: state is 2, value is 1
I (32722) switch: state is 2, value is 1
I (32727) switch: state is 2, value is 1
I (32731) switch: state is 2, value is 1
I (32736) switch: state is 2, value is 1
I (32740) switch: state is 2, value is 1
I (32745) switch: state is 2, value is 1
I (32750) switch: state is 2, value is 1
I (32754) switch: state is 2, value is 1
I (32759) switch: state is 2, value is 1
I (32763) switch: state is 2, value is 1
I (32768) switch: state is 2, value is 1
I (32773) switch: state is 2, value is 1
I (32777) switch: state is 2, value is 1
I (32782) switch: state is 2, value is 1
I (32786) switch: state is 2, value is 1
I (32791) switch: state is 2, value is 1
I (32796) switch: state is 2, value is 1
I (32800) switch: state is 2, value is 1
I (32805) switch: state is 2, value is 1
I (32809) switch: state is 2, value is 1
I (32814) switch: state is 2, value is 1
I (32819) switch: state is 2, value is 1
I (32823) switch: state is 2, value is 1
I (32828) switch: state is 2, value is 1
I (32832) switch: state is 2, value is 1
I (32837) switch: state is 2, value is 1
I (32842) switch: state is 2, value is 1
I (32846) switch: state is 2, value is 1
I (32851) switch: state is 2, value is 1
I (32855) switch: state is 2, value is 1
I (32860) switch: state is 2, value is 1
I (32865) switch: state is 2, value is 0
I (32869) switch: state is 4, value is 0
I (32874) zigbee: Send 'on_off toggle' command
I (32879) zigbee: done!
I (32882) switch: back to sleep
I (32886) switch: GPIO interrupts: on
I (36742) zigbee: zigbee wakeup
I (36747) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (39824) switch: GPIO interrupts: off
I (39824) switch: GPIO ISR HANDLER cause 3
I (39831) switch: queue event
I (39831) switch: loop resume
I (39831) switch: state is 0, value is 1
I (39836) switch: state is 2, value is 1
I (39840) switch: state is 2, value is 1
I (39845) switch: state is 2, value is 1
I (39850) switch: state is 2, value is 1
I (39854) switch: state is 2, value is 1
I (39859) switch: state is 2, value is 1
I (39863) switch: state is 2, value is 1
I (39868) switch: state is 2, value is 1
I (39873) switch: state is 2, value is 1
I (39877) switch: state is 2, value is 1
I (39882) switch: state is 2, value is 1
I (39886) switch: state is 2, value is 1
I (39891) switch: state is 2, value is 1
I (39896) switch: state is 2, value is 1
I (39900) switch: state is 2, value is 1
I (39905) switch: state is 2, value is 1
I (39909) switch: state is 2, value is 1
I (39914) switch: state is 2, value is 1
I (39919) switch: state is 2, value is 1
I (39923) switch: state is 2, value is 1
I (39928) switch: state is 2, value is 1
I (39932) switch: state is 2, value is 1
I (39937) switch: state is 2, value is 1
I (39942) switch: state is 2, value is 1
I (39946) switch: state is 2, value is 1
I (39951) switch: state is 2, value is 1
I (39955) switch: state is 2, value is 1
I (39960) switch: state is 2, value is 1
I (39965) switch: state is 2, value is 1
I (39969) switch: state is 2, value is 1
I (39974) switch: state is 2, value is 1
I (39978) switch: state is 2, value is 1
I (39983) switch: state is 2, value is 1
I (39988) switch: state is 2, value is 1
I (39992) switch: state is 2, value is 1
I (39997) switch: state is 2, value is 1
I (40001) switch: state is 2, value is 1
I (40006) switch: state is 2, value is 1
I (40011) switch: state is 2, value is 1
I (40015) switch: state is 2, value is 1
I (40020) switch: state is 2, value is 1
I (40024) switch: state is 2, value is 1
I (40029) switch: state is 2, value is 1
I (40034) switch: state is 2, value is 1
I (40038) switch: state is 2, value is 1
I (40043) switch: state is 2, value is 1
I (40047) switch: state is 2, value is 1
I (40052) switch: state is 2, value is 1
I (40057) switch: state is 2, value is 1
I (40061) switch: state is 2, value is 1
I (40066) switch: state is 2, value is 1
I (40070) switch: state is 2, value is 1
I (40075) switch: state is 2, value is 1
I (40080) switch: state is 2, value is 1
I (40084) switch: state is 2, value is 1
I (40089) switch: state is 2, value is 1
I (40093) switch: state is 2, value is 1
I (40098) switch: state is 2, value is 1
I (40103) switch: state is 2, value is 1
I (40107) switch: state is 2, value is 1
I (40112) switch: state is 2, value is 1
I (40116) switch: state is 2, value is 1
I (40121) switch: state is 2, value is 1
I (40126) switch: state is 2, value is 1
I (40130) switch: state is 2, value is 1
I (40135) switch: state is 2, value is 1
I (40139) switch: state is 2, value is 1
I (40144) switch: state is 2, value is 1
I (40149) switch: state is 2, value is 1
I (40153) switch: state is 2, value is 1
I (40158) switch: state is 2, value is 0
I (40162) switch: state is 4, value is 0
I (40167) zigbee: Send 'on_off toggle' command
I (40172) zigbee: done!
I (40175) switch: back to sleep
I (40179) switch: GPIO interrupts: on
I (40183) switch: GPIO interrupts: off
I (40183) switch: GPIO ISR HANDLER cause 3
I (40193) switch: queue event
I (40196) switch: loop resume
I (40200) switch: state is 0, value is 1
I (40205) switch: state is 2, value is 1
I (40209) switch: state is 2, value is 1
I (40214) switch: state is 2, value is 1
I (40218) switch: state is 2, value is 1
I (40223) switch: state is 2, value is 1
I (40228) switch: state is 2, value is 1
I (40232) switch: state is 2, value is 0
I (40237) switch: state is 4, value is 0
I (40241) zigbee: Send 'on_off toggle' command
I (40246) zigbee: done!
I (40249) switch: back to sleep
I (40253) switch: GPIO interrupts: on
I (41825) zigbee: zigbee wakeup
I (41831) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK
I (46898) zigbee: zigbee wakeup
I (46904) zigbee: ZDO signal: Can Sleep (0x16), status: ESP_OK

image

blastrock commented 9 months ago

Thanks a lot for your help! So the short stack was indeed the cause of the crash. Are there tools to help debug that? Or was that just your intuition?

Anyway, I have one last bug. When I press the button, I call esp_zb_zcl_on_off_cmd_req to toggle a light (see https://github.com/blastrock/HA_on_off_light/blob/1bb427431ab81140c54f131d57b7eff554094398/main/esp_zb_light.c#L66 ). However, if I press the button during sleep, the device wakes up, registers the button press and goes back to sleep, but the zigbee message isn't sent yet. Later when the device wakes up again because of the zigbee task, it finally sends the message.

It looks like esp_zb_zcl_on_off_cmd_req should wake up the zigbee task to send the message, but it does not. Is there something else I did wrong in my code?

kelin6 commented 8 months ago

Thanks a lot for your help! So the short stack was indeed the cause of the crash. Are there tools to help debug that? Or was that just your intuition?

Anyway, I have one last bug. When I press the button, I call esp_zb_zcl_on_off_cmd_req to toggle a light (see https://github.com/blastrock/HA_on_off_light/blob/1bb427431ab81140c54f131d57b7eff554094398/main/esp_zb_light.c#L66 ). However, if I press the button during sleep, the device wakes up, registers the button press and goes back to sleep, but the zigbee message isn't sent yet. Later when the device wakes up again because of the zigbee task, it finally sends the message.

It looks like esp_zb_zcl_on_off_cmd_req should wake up the zigbee task to send the message, but it does not. Is there something else I did wrong in my code?

@blastrock There are two ways to debug: 1) Normally, esp-idf monitors stack overflow using esp_cpu_set_watchpoint. However, during sleep, the CPU is powered down, and the watchpoint-related functionality is not fully supported in sleep mode. Therefore, it won't prompt for a stack overflow after a crash. For example, based on your code, you can configure CONFIG_PM_POWER_DOWN_CPU_IN_LIGHT_SLEEP=n to keep the CPU powered, then compile your code. You should then be able to see the following information: image

2) You can inspect the stack usage in the corresponding task by using the following code:

size_t stackHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
esp_rom_printf("Task Stack High Water Mark: %u bytes\n", stackHighWaterMark);

Print the stack consumption in the "button_detected" task to determine that ESP_LOGI requires the most stack, almost 1 kbytes, while the "button_detected" task is allocated only 2 kbytes in total. When executing subsequent code, this leads to insufficient stack resources.

Regarding the issue of "esp_zb_zcl_on_off_cmd_req waking up the Zigbee task to send the message", this is a known problem, and we are currently discussing potential solutions.

blastrock commented 8 months ago

There are two ways to debug:

Thanks for the tips, I'll check that next time I have a crash.

Regarding the issue of "esp_zb_zcl_on_off_cmd_req waking up the Zigbee task to send the message", this is a known problem, and we are currently discussing potential solutions.

Ok, thanks for looking into it, I'll wait for your update then.

susch19 commented 6 months ago

Regarding the issue of "esp_zb_zcl_on_off_cmd_req waking up the Zigbee task to send the message", this is a known problem, and we are currently discussing potential solutions.

Is there any update on this? When I try to send a button via light sleep, the behavior is still, that it sends it as soon as the task from Zigbee is woken up after the timer. I also tried to use vTaskResume with the handler of the Zigbee Main Task, but that didn't work either. As far as I understand the only solution is to use deep sleep? Because then the Zigbee task never sleeps and waking up the esp via ext1 from deep sleep always works.

xieqinan commented 5 months ago

@blastrock @susch19 @agustinebrecht ,

The esp-zigbee-sdk v1.2.2 now supports immediate wake up from GPIO. Could you please try using the new version to test this issue again?

susch19 commented 5 months ago

@blastrock @susch19 @agustinebrecht ,

The esp-zigbee-sdk v1.2.2 now supports immediate wake up from GPIO. Could you please try using the new version to test this issue again?

Just had the time to test it and it works wonderfully. I have an end device with 30s long poll interval, for "long" light sleeps and when I press a button the message is immediately send and received. The delay is nearly not existent / as good as with other zigbee end devices.