Static task works, dynamic not - Not enough memory?

mateo_
Posts: 3
Joined: Mon Apr 11, 2022 7:53 am

Static task works, dynamic not - Not enough memory?

Postby mateo_ » Mon Apr 11, 2022 8:46 am

Hi,

I have running code but for some time now adding new line of code (e.g. MDF_LOGI or watchdog) causes problem with initialization of program when I load wifi configuration, ... etc. from NVS.

I needed to add watchdogs to both tasks.
I guess its a problem with memory because I changed one task from dynamic to static and it works fine again.

So I have 3 cases:

1. Adding watchdog to one task - WORKS

Code: Select all

xTaskCreate(communication_task, "communication_task", 4 * 1024, NULL, 6, NULL);
xTaskCreate(VL53L0X_task, "VL53L0X_task", 4 * 1024, NULL, 4, &xHandle_VL53L0X);

MDF_ERROR_ASSERT( esp_task_wdt_init(100,1) );
MDF_ERROR_ASSERT( esp_task_wdt_add(xHandle_VL53L0X) );
2. Adding watchdog to second task - DOESN'T WORK (compiles but fails during initialization from NVS)

Code: Select all

xTaskCreate(communication_task, "communication_task", 2 * 1024, NULL, 6, &xHandle_communication);
xTaskCreate(VL53L0X_task, "VL53L0X_task", 4 * 1024, NULL, 4, &xHandle_VL53L0X);

MDF_ERROR_ASSERT( esp_task_wdt_init(100,1) );
MDF_ERROR_ASSERT( esp_task_wdt_add(xHandle_communication) );
MDF_ERROR_ASSERT( esp_task_wdt_add(xHandle_VL53L0X) );
3. Adding watchdog to second task and changing it to static- WORKS AGAIN (compiles but fails during initialization from NVS)

Code: Select all

xHandle_communication = xTaskCreateStatic(
                communication_task, 
                "communication_task", 
                4 * 1024, 
                ( void * ) 1, 
                6,
                xStack,
                &xTaskBuffer );

xTaskCreate(VL53L0X_task, "VL53L0X_task", 4 * 1024, NULL, 4, &xHandle_VL53L0X);

MDF_ERROR_ASSERT( esp_task_wdt_init(100,1) );
MDF_ERROR_ASSERT( esp_task_wdt_add(xHandle_communication) );
MDF_ERROR_ASSERT( esp_task_wdt_add(xHandle_VL53L0X) );

Is it a problem with memory? If so which memory is responislble for that and how can I check it?

Thanks in advance and best regards,
Mateusz

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

Re: Static task works, dynamic not - Not enough memory?

Postby ESP_Sprite » Tue Apr 12, 2022 7:56 am

1. How does it fail?
2. 4K is a very low amount of stack you're allocating. Try to increase that, see if it fixes things.

mateo_
Posts: 3
Joined: Mon Apr 11, 2022 7:53 am

Re: Static task works, dynamic not - Not enough memory?

Postby mateo_ » Tue Apr 12, 2022 10:16 am

ESP_Sprite wrote:
Tue Apr 12, 2022 7:56 am
1. How does it fail?
2. 4K is a very low amount of stack you're allocating. Try to increase that, see if it fixes things.
1. Those are logs from monitor, the error is in wifi initialization function. But during debuging, the error appeard on earlier stage of program, so I guess it isn't the cause
error msg.png
error msg.png (704.74 KiB) Viewed 1912 times
Main function wifi initialization is befor starting my tasks.

Code: Select all

void app_main()
{

    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    MDF_LOGI("This is %s chip with %d CPU core(s), WiFi%s%s, ",
            CONFIG_IDF_TARGET,
            chip_info.cores,
            (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
            (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");

    MDF_LOGI("silicon revision %d, ", chip_info.revision);

    initialisation();

    mwifi_init_config_t cfg   = MWIFI_INIT_CONFIG_DEFAULT();

    MDF_LOGI("Lantern enabled");
    mwifi_config_t config;
    MDF_LOGI("INITIALIZED CHANNEL NUMBER: %d", config.channel);
    uint8_t channel = 0;
    config.channel = channel;
    size_t required_size;
    esp_err_t err = nvs_flash_init();
    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }
    ESP_ERROR_CHECK( err );

    nvs_handle_t handle;

    
    err = nvs_open("storage", NVS_READWRITE, &handle);
    if (err != ESP_OK) 
    {
        MDF_LOGE("Error (%s) opening NVS handle! ESP will restart in 5 seconds", esp_err_to_name(err));
        vTaskDelay(pdMS_TO_TICKS(5000));
        esp_restart();
    }
    else 
    {
        MDF_LOGI("NVS initialised");

        err = nvs_get_u8(handle, "edge_configured", &edge_configured);
        switch (err) 
        {
            case ESP_OK:
                MDF_LOGI("edge_configured = %d", edge_configured);
                break;
            case ESP_ERR_NVS_NOT_FOUND:
                MDF_LOGW("edge_configured is not initialized yet!");
                break;
            default :
                MDF_LOGE("Error (%s) reading edge_configured!", esp_err_to_name(err));
        }

        if ( edge_configured )
        {
            nvs_get_str(handle, "edge_ip", NULL, &required_size);
            edge_ip = (char*) malloc(required_size);
            err = nvs_get_str(handle, "edge_ip", edge_ip, &required_size);
            switch (err) 
            {
                case ESP_OK:
                    MDF_LOGI("edge_ip = %s", edge_ip);
                    break;
                case ESP_ERR_NVS_NOT_FOUND:
                    MDF_LOGW("edge_ip is not initialized yet!");
                    break;
                default :
                    MDF_LOGE("Error (%s) reading edge_ip!", esp_err_to_name(err));
            }

            err = nvs_get_u16(handle, "edge_port", &edge_port);
            switch (err) 
            {
                case ESP_OK:
                    MDF_LOGI("edge_port = %d", edge_port);
                    break;
                case ESP_ERR_NVS_NOT_FOUND:
                    MDF_LOGW("edge_port is not initialized yet!");
                    break;
                default :
                    MDF_LOGE("Error (%s) reading edge_port!", esp_err_to_name(err));
            }

            nvs_get_str(handle, "conf_wifi", NULL, &required_size);
            configured_wifi = (char*) malloc(required_size);
            err = nvs_get_str(handle, "conf_wifi", configured_wifi, &required_size);
            switch (err) 
            {
                case ESP_OK:
                    MDF_LOGI("conf_wifi = %s", configured_wifi);
                    break;
                case ESP_ERR_NVS_NOT_FOUND:
                    MDF_LOGW("conf_wifi is not initialized yet!");
                    break;
                default :
                    MDF_LOGE("Error (%s) reading conf_wifi!", esp_err_to_name(err));
            }

            nvs_get_str(handle, "conf_wifi_pass", NULL, &required_size);
            configured_wifi_password = (char*) malloc(required_size);
            err = nvs_get_str(handle, "conf_wifi_pass", configured_wifi_password, &required_size);
            switch (err) 
            {
                case ESP_OK:
                    MDF_LOGI("conf_wifi_pass = %s", configured_wifi_password);
                    break;
                case ESP_ERR_NVS_NOT_FOUND:
                    MDF_LOGW("conf_wifi_pass is not initialized yet!");
                    break;
                default :
                    MDF_LOGE("Error (%s) reading conf_wifi_pass!", esp_err_to_name(err));
            }

            nvs_get_str(handle, "conf_mesh_id", NULL, &required_size);
            configured_mesh_id = (char*) malloc(required_size);
            err = nvs_get_str(handle, "conf_mesh_id", configured_mesh_id, &required_size);
            switch (err) 
            {
                case ESP_OK:
                    MDF_LOGI("conf_mesh_id = %s", configured_mesh_id);
                    break;
                case ESP_ERR_NVS_NOT_FOUND:
                    MDF_LOGW("conf_mesh_id is not initialized yet!");
                    break;
                default :
                    MDF_LOGE("Error (%s) reading conf_mesh_id!", esp_err_to_name(err));
            }
            
            strcpy(config.router_ssid, configured_wifi);
            strcpy(config.router_password, configured_wifi_password);
            memcpy(config.mesh_id, configured_mesh_id, sizeof(config.mesh_id));
            strcpy(config.mesh_password, CONFIG_MESH_PASSWORD);
            MDF_LOGI("CHANNEL NUMBER: %d", config.channel);
            nvs_close(handle);

        }
        else
        {
            //set default values
            MDF_LOGI("Setting default values");  
            strcpy(config.router_ssid, CONFIG_ROUTER_SSID);
            strcpy(config.router_password, CONFIG_ROUTER_PASSWORD);
            memcpy(config.mesh_id, CONFIG_MESH_ID, sizeof(config.mesh_id));
            strcpy(config.mesh_password, CONFIG_MESH_PASSWORD);
        }
    }


    esp_log_level_set("*", ESP_LOG_INFO);
    esp_log_level_set(TAG, ESP_LOG_DEBUG);


    MDF_ERROR_ASSERT(mdf_event_loop_init(event_loop_cb));
    MDF_ERROR_ASSERT(wifi_init());		///////////PROGRAM FAILES SOMEWHERE HERE
    MDF_ERROR_ASSERT(mwifi_init(&cfg));
    MDF_ERROR_ASSERT(mwifi_set_config(&config));
    MDF_ERROR_ASSERT(mwifi_start());

    initialize_sntp();

    uint8_t sta_mac[MWIFI_ADDR_LEN] = {0};
    esp_wifi_get_mac(WIFI_IF_STA, sta_mac);
    asprintf(&mac_address, MACSTR, MAC2STR(sta_mac));
    

    if( get_time_ms() > 10000000000)
    {
        sales_measurment = 1;
    }
    MDF_LOGW("Starting Tasks");
    TaskHandle_t xHandle_VL53L0X = NULL;
    TaskHandle_t xHandle_communication = NULL;

    //xTaskCreate(VL53L0X_task, "VL53L0X_task", 4 * 1024, NULL, 4, NULL);
    xTaskCreate(communication_task, "communication_task", 4 * 1024, NULL, 6, NULL);

    xTaskCreate(VL53L0X_task, "VL53L0X_task", 4 * 1024, NULL, 4, &xHandle_VL53L0X);
    //xTaskCreate(communication_task, "communication_task", 2 * 1024, NULL, 6, &xHandle_communication);
    /*
    xHandle_communication = xTaskCreateStatic(
                communication_task, 
                "communication_task", 
                4 * 1024, 
                ( void * ) 1, 
                6,
                xStack,
                &xTaskBuffer );*/

    xTaskCreate(input_voltage_adc_task, "input_voltage_adc_task", 4 * 1024, NULL, 2, NULL);
    //xTaskCreate(RTC_task, "RTC_task", 4 * 1024, NULL, 1, NULL);

    MDF_ERROR_ASSERT( esp_task_wdt_init(100,1) );
    MDF_ERROR_ASSERT( esp_task_wdt_add(xHandle_VL53L0X) );
    //MDF_ERROR_ASSERT( esp_task_wdt_add(xHandle_communication) );
    
    TimerHandle_t system_info_timer = xTimerCreate("print_system_info", 5000 / portTICK_RATE_MS,
                                        true, NULL, print_system_info_timercb);
    xTimerStart(system_info_timer, 0);


    if ( edge_configured )
    {
        TimerHandle_t additional_info_timer = xTimerCreate("print_additional_info", 15000 / portTICK_RATE_MS,
                                        true, NULL, print_additional_info_timercb);
        xTimerStart(additional_info_timer, 0);
    }

}
2. I guess I tried to increase the stack before. Tomorrow I will have access to the device and will try again.
Attachments
main.cpp
(8.28 KiB) Downloaded 287 times

Who is online

Users browsing this forum: No registered users and 164 guests