Zigbee examples: help understanding how they work

Rob190
Posts: 5
Joined: Mon May 27, 2024 11:40 am

Zigbee examples: help understanding how they work

Postby Rob190 » Wed May 29, 2024 7:03 pm

I'm looking at the example here: examples/esp_zigbee_HA_sample/HA_on_off_light

It works fine but I'm having trouble understanding the code. In particular, I don't see how the main loop runs.

Code: Select all

static void esp_zb_task(void *pvParameters)
{
    /* initialize Zigbee stack */
    esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG();
    esp_zb_init(&zb_nwk_cfg);
    esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG();
    esp_zb_ep_list_t *esp_zb_on_off_light_ep = esp_zb_on_off_light_ep_create(HA_ESP_LIGHT_ENDPOINT, &light_cfg);
    esp_zb_device_register(esp_zb_on_off_light_ep);
    esp_zb_core_action_handler_register(zb_action_handler);
    esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
    ESP_ERROR_CHECK(esp_zb_start(false));
    esp_zb_main_loop_iteration();
}
This is called as a task from app_main:

Code: Select all

    xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
It looks to me that esp_zb_main_loop_iteration() is only run once. Shouldn't it be inside a while(true) loop?

The documentation says "Must be called after esp_zb_int() and esp_zb_start() inside the application’s main cycle. ". This implies it is called repeatedly but the documentation for xTaskCreate implies that you need a loop, for(;;) in the example:

https://www.freertos.org/a00125.html

stef70
Posts: 1
Joined: Mon Jun 03, 2024 12:53 pm

Re: Zigbee examples: help understanding how they work

Postby stef70 » Mon Jun 03, 2024 1:14 pm

According to https://github.com/espressif/esp-zigbee ... 1986330620 , the disassembly of esp_zb_main_loop_iteration is

Code: Select all

4202075e <esp_zb_main_loop_iteration>:
4202075e:       1141                    add     sp,sp,-16
42020760:       c606                    sw      ra,12(sp)
42020762:       a029                    j       4202076c <esp_zb_main_loop_iteration+0xe>
42020764:       7d02e0ef                jal     4204ef34 <zboss_main_loop_iteration>
42020768:       fe9ff0ef                jal     42020750 <esp_zb_lock_release>
4202076c:       557d                    li      a0,-1
4202076e:       fd5ff0ef                jal     42020742 <esp_zb_lock_acquire>
42020772:       f96d                    bnez    a0,42020764 <esp_zb_main_loop_iteration+0x6>
42020774:       bfd5                    j       42020768 <esp_zb_main_loop_iteration+0xa>
If I am not mistaken, that should correspond to the following code:

Code: Select all

void esp_zb_main_loop_iteration() {
  while(true) {
    if ( esp_zb_lock_acquire(portMAX_DELAY) ) {
      zboss_main_loop_iteration() 
    }
    esp_zb_lock_release() 
  }
}
So there is a infinite loop. The name was poorly chosen.

Rob190
Posts: 5
Joined: Mon May 27, 2024 11:40 am

Re: Zigbee examples: help understanding how they work

Postby Rob190 » Wed Jun 05, 2024 9:00 am

stef70 wrote:
Mon Jun 03, 2024 1:14 pm

So there is a infinite loop. The name was poorly chosen.
Thanks!

Agree, not a good name. Maybe I'll suggest they change it. Can't imagine I'm the only person to be confused by it.

Who is online

Users browsing this forum: No registered users and 5 guests