diff --git a/examples/device/dfu_freertos/src/main.c b/examples/device/dfu_freertos/src/main.c index 3bfb85b27..65262ba10 100644 --- a/examples/device/dfu_freertos/src/main.c +++ b/examples/device/dfu_freertos/src/main.c @@ -135,6 +135,41 @@ static void dl_task(void* arg) } } +// task to complete flashing, used when entering manifestation +static void complete_task(void* arg) +{ + esp_err_t rc; + // finish flashing + if (ota_handle) { + rc = esp_ota_end(ota_handle); + ota_handle = 0; + if (ESP_OK != rc) { + ESP_LOGE(TAG, "close OTA failed"); + tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG); + return; + } + } + + // switch to OTA app + const esp_partition_t *ota = esp_ota_get_next_update_partition(NULL); + esp_app_desc_t ota_desc; + esp_ota_get_partition_description(ota, &ota_desc); + if (ESP_APP_DESC_MAGIC_WORD == ota_desc.magic_word) { + ESP_LOGI(TAG, "set boot to valid app"); + esp_ota_set_boot_partition(ota); + } else { + ESP_LOGI(TAG, "no valid app"); + tud_dfu_finish_flashing(DFU_STATUS_ERR_VERIFY); + return; + } + + // flashing op for manifest is complete without error + xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_IDLE), 0); + tud_dfu_finish_flashing(DFU_STATUS_OK); + + vTaskDelete(NULL); // close task +} + //--------------------------------------------------------------------+ // Main //--------------------------------------------------------------------+ @@ -289,38 +324,9 @@ void tud_dfu_download_cb(uint8_t alt, uint16_t block_num, uint8_t const* data, u // Once finished flashing, application must call tud_dfu_finish_flashing() void tud_dfu_manifest_cb(uint8_t alt) { - (void) alt; - esp_err_t rc; + (void)alt; // not used ESP_LOGI(TAG, "download completed, enter manifestation"); - blinky_tm = xTimerCreateStatic(NULL, pdMS_TO_TICKS(BLINK_IDLE), true, NULL, led_blinky_cb, &blinky_tmdef); - - // finish flashing - if (ota_handle) { - rc = esp_ota_end(ota_handle); - ota_handle = 0; - if (ESP_OK != rc) { - ESP_LOGE(TAG, "close OTA failed"); - tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG); - return; - } - } - - // switch to OTA app - const esp_partition_t *ota = esp_ota_get_next_update_partition(NULL); - esp_app_desc_t ota_desc; - esp_ota_get_partition_description(ota, &ota_desc); - if (ESP_APP_DESC_MAGIC_WORD == ota_desc.magic_word) { - ESP_LOGI(TAG, "boot set to valid app"); - esp_ota_set_boot_partition(ota); - } else { - ESP_LOGI(TAG, "no valid app"); - tud_dfu_finish_flashing(DFU_STATUS_ERR_VERIFY); - return; - } - - // flashing op for manifest is complete without error - // Application can perform checksum, should it fail, use appropriate status such as errVERIFY. - tud_dfu_finish_flashing(DFU_STATUS_OK); + xTaskCreate(complete_task, "complete_task", 3 * 1024, NULL, 8, NULL); } // Invoked when received DFU_UPLOAD request