Stack overflow/reset/LoadProhibited when trying to monitor wifi bits with task.

greyarea
Posts: 4
Joined: Mon Jan 31, 2022 2:10 pm

Stack overflow/reset/LoadProhibited when trying to monitor wifi bits with task.

Postby greyarea » Mon Mar 21, 2022 6:02 pm

I'm trying to take the station example from 4.4 of the ESP-IDF and make it non-blocking.

I'd like to have the wifi try to connect on startup and if it does, great - but if not to continue running other tasks and keep trying to connect.

The way I approcated this was to remove the xEventGroupWaitBits section in the example. https://github.com/espressif/esp-idf/bl ... #L107-L130, and also to modify the event handler to continue attempting to connect on a WIFI_EVENT_STA_DISCONNECTED event.

If I compile and run this, then it works as expected, the event handler runs and when the AP becomes available, it connects. If I turn off the AP I see a disconnected message. I'm sure that there's additional events I will need to handle properly, but so far, so good.

My problems start when I try to add a task[2] to monitor the bits set in `s_wifi_event_group`, namely `WIFI_CONNECTED_BIT (BIT0)` and `WIFI_FAIL_BIT (BIT1)`. I start this task after the `wifi_init_sta()` call in `app_main`[3].

Even though I've tried delaying the task startup by several seconds to allow time for the event handler to be created, I seem to get an immediate panic along the lines of:

Code: Select all

I (751) wifi station: wifi_init_sta finished.
I (751) wifi stati
***ERROR*** A stack overflow in task task_poll_wifi_ has been detected.
Or sometimes a `LoadProhibited` message.

What is the reason for this? Is there a way to check that the event group has been created, checking for a NULL value seems to give me an exception also.

Any advice appreciated, thanks.



[1] Modified event handler.

Code: Select all

static void event_handler(void* arg, esp_event_base_t event_base,
                                int32_t event_id, void* event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
    {
        esp_wifi_connect();
    }
    // If we are disconnected, try to connect
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
    {
        xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
        ESP_LOGI(TAG, "Try to connect to the AP");
        esp_wifi_connect();
        ESP_LOGI(TAG,"connect to the AP fail");
    }
    else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
    {
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
        ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
        s_retry_num = 0;
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
    }
}
[2] Task to monitor `s_wifi_event_group` bits.

Code: Select all

void task_poll_wifi_status(void *parameters)
{
    ESP_LOGI(TAG, "Delaying for 10 seconds to allow wifi to start.");
    // Delaying to allow wifi event group to get created.
    vTaskDelay(10000 / portTICK_PERIOD_MS);
    ESP_LOGI(TAG, "Starting polling task.");

    EventBits_t bits;

    // Tasks should not return.
    while(1)
    {
        printf("\nPolling wifi status.\n");

        bits = xEventGroupGetBits(s_wifi_event_group);

        printf("\nDo we ever get here?\n");

        if (bits & WIFI_CONNECTED_BIT)
        {
            ESP_LOGI(TAG, "connected to ap");
        }
        else if (bits & WIFI_FAIL_BIT)
        {
            ESP_LOGI(TAG, "Failed to connect to ap");
        }
        else 
        {
            ESP_LOGE(TAG, "UNEXPECTED EVENT");
        }

        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}
[3] Starting task `task_poll_wifi_status` after `wifi_init_sta()` in `app_main()`

Code: Select all

    xTaskCreate(
        task_poll_wifi_status,      // Task function.
        "task_poll_wifi_status",    // Task name
        1024,                       // Stack size in bytes
        NULL,                       // Parameters for task
        5,                          // Task priority
        NULL                        // Task handle.
    );

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

Re: Stack overflow/reset/LoadProhibited when trying to monitor wifi bits with task.

Postby ESP_Sprite » Tue Mar 22, 2022 2:13 am

1024 bytes stack is tiny and likely not enough. Try to increase that number.

greyarea
Posts: 4
Joined: Mon Jan 31, 2022 2:10 pm

Re: Stack overflow/reset/LoadProhibited when trying to monitor wifi bits with task.

Postby greyarea » Tue Mar 22, 2022 10:07 am

Ah, thank you. The fact that I was sometimes getting LoadProhibited was confusing me, I didn't think I'd need that much stack for that task and clearly need to research how to size a FreeRTOS stack allocation..

Thanks again.

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

Re: Stack overflow/reset/LoadProhibited when trying to monitor wifi bits with task.

Postby ESP_Sprite » Wed Mar 23, 2022 1:55 am

You generally want to allocate a fair few K to a task. From memory, if you want to do anything printf-related (like ESP_LOG*), you already need 4K of stack for it to work.

Who is online

Users browsing this forum: Google [Bot] and 204 guests