Page 1 of 1

How to feed task watchdog from asynchronous code such as a BLE task?

Posted: Tue Sep 19, 2017 8:05 am
by halfro
So I have a BLE task and the task contains a gatt server and advertisement data. Is there a way to feed the task watchdog. I have read the notes on the watchdogs but I can't find a way for my BleTask to be watched especially since BLE operations are very asynchronous.

Here is my trial at it.

Code: Select all

void BleTask(void *pvParameters)
{
    esp_err_t nvs_state, bt_init_fail;

    /* Initialise NVS*/
    nvs_state = nvs_flash_init();

    if (nvs_state == ESP_ERR_NVS_NO_FREE_PAGES)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        nvs_state = nvs_flash_init();
    }
    ESP_ERROR_CHECK(nvs_state);

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();

    bt_init_fail = esp_bt_controller_init(&bt_cfg);
    if (bt_init_fail)
    {
        ESP_LOGE(BLE_TASK_TAG, "function:\"%s\" BLUETOOTH CONTROLLER INIT FAILED\n", __func__);
        vTaskDelete(xBleTaskHandle);
    }

    bt_init_fail = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
    if (bt_init_fail)
    {
        ESP_LOGE(BLE_TASK_TAG, "function:\"%s\" BLUETOOTH CONTROLLER ENABLE FAILED\n", __func__);
        vTaskDelete(xBleTaskHandle);
    }

    ESP_LOGI(BLE_TASK_TAG, "function:\"%s\" INITIALISED BLUETOOTH CONTROLLER\n", __func__);

    bt_init_fail = esp_bluedroid_init();
    if (bt_init_fail)
    {
        ESP_LOGE(BLE_TASK_TAG, "function:\"%s\" BLUEDROID STACK INIT FAILED\n", __func__);
        vTaskDelete(xBleTaskHandle);
    }

    bt_init_fail = esp_bluedroid_enable();
    if (bt_init_fail) 
    {
        ESP_LOGE(BLE_TASK_TAG, "function:\"%s\" BLUEDROID STACK ENABLE FAILED\n", __func__);
        vTaskDelete(xBleTaskHandle);
    }

    Ble_task_parameters* ble_param = NULL;
    ble_param = (Ble_task_parameters*)pvParameters;

    esp_ble_gatts_register_callback(gatts_event_handler);
    esp_ble_gap_register_callback(gap_event_handler);
    esp_ble_gatts_app_register(SYSTEM_APP_ID);

    for(;;)
    {
     esp_task_wdt_feed();
    }
}

Re: How to feed task watchdog from asynchronous code such as a BLE task?

Posted: Tue Sep 19, 2017 8:55 am
by ESP_Sprite
If the task runs at unpredictable times, why would you put in a watchdog at all?

Re: How to feed task watchdog from asynchronous code such as a BLE task?

Posted: Tue Sep 19, 2017 8:36 pm
by halfro
ESP_Sprite wrote:If the task runs at unpredictable times, why would you put in a watchdog at all?
Thanks for the insight. I haven't used watchdogs before so I was trying to understand if it can be used for non deterministic code to get rid of the "unable to feed task watchdog" message.

Re: How to feed task watchdog from asynchronous code such as a BLE task?

Posted: Wed Sep 20, 2017 1:21 am
by ESP_Sprite
'Unable to feed task watchdog'? Is this a literal quote?

If you mean that the task watchdog times out on your idle task, it is normally because you have one thread that hogs the CPU, not allowing other tasks (specifically, the idle task, which is watched by the task watchdog) to run.