Somrtimes ota works well,sometimes ota will go wrong.

WayneJia
Posts: 17
Joined: Thu Aug 03, 2017 7:28 am

Somrtimes ota works well,sometimes ota will go wrong.

Postby WayneJia » Tue Oct 24, 2017 2:08 am

I have some problem with OTA.Sometimes,When halfway through the ota,it will go wrong.Sometimes it can ota successfully!
My device is control by nrf52 through uart1 command.
When I use old device which have 4M flash and no nrf52,It work well whith OTA.(with no source code changed.)
Now,my device only have 2M flash.But ,I already had adjust the partitions.

My factory app is 571K, ota app is 644K.Sometimes ota works well,sometimes ota will go wrong.

Code: Select all

nvs,      data, nvs,     0x9000,  0x4000
otadata,  data, ota,     0xd000,  0x2000
phy_init, data, phy,     0xf000,  0x1000
factory,  0,    0,       0x10000,  576K
ota_0,    0,    ota_0,   ,         704K
ota_1,    0,    ota_1,   ,         704K
This is the log.

Code: Select all

OTA_NOW:buff_len = 436,resp_body_start = 1[0;32mI (85167) YH_OTA: -----Have written image length 26681-----[0m
OTA_NOW:buff_len = 1024,resp_body_start = 1[0;32mI (85173) YH_OTA: -----Have written image length 27705-----[0m
ets Jun  8 2016 00:22:57

rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
Falling back to built-in command interpreter.
OK
>ets Jun  8 2016 00:22:57

rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
Falling back to built-in command interpreter.
OK
>ets Jun  8 2016 00:22:57
Thanks for your help. :)

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Somrtimes ota works well,sometimes ota will go wrong.

Postby ESP_Angus » Tue Oct 24, 2017 6:05 am

rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
It looks like your OTA code is corrupting/erasing the bootloader. Does it ever recover from this or do you have to reflash?

Are you using the IDF OTA API? Can you post some of your code?

WayneJia
Posts: 17
Joined: Thu Aug 03, 2017 7:28 am

Re: Somrtimes ota works well,sometimes ota will go wrong.

Postby WayneJia » Tue Oct 24, 2017 8:53 am

ESP_Angus wrote:
rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
It looks like your OTA code is corrupting/erasing the bootloader. Does it ever recover from this or do you have to reflash?

Are you using the IDF OTA API? Can you post some of your code?
This is my code.

Code: Select all

void ota_clear(void)
{
	ESP_LOGI(TAG_OTA, "-----OTA clear-----");

	close(ota_socket_id);
	
	binary_file_length = 0;
	
	xEventGroupClearBits(wifi_event_group, OTA_BIT);
}
static void ota_timer_callback(TimerHandle_t timer)
{
	ESP_LOGI(TAG_OTA, "-----ota failed because bad network! Prepare to restart system!-----");
	ota_reboot_system();
}

static void ota_start_timer_timeout(void)
{
	int count = 0;
	if(ota_timer != NULL)
	{
		xTimerReset(ota_timer,0);
	}
	else
	{
		ota_timer = xTimerCreate("ota_timer", 60000 / portTICK_PERIOD_MS, pdFALSE,(void *)&count, ota_timer_callback);
		xTimerStart(ota_timer, 0);
	}
	
}
static void ota_task(void *pvParameter)
{
    esp_err_t err;
    /* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
    esp_ota_handle_t update_handle = 0 ;
    const esp_partition_t *update_partition = NULL;
	char ota_http_request[64] = {0};
	/*an ota data write buffer ready to write to the flash*/
	//char ota_write_data[OTA_BUFFSIZE + 1] = { 0 };
	/*an packet receive buffer*/
	char ota_text[OTA_TEXT_BUFFSIZE + 1] = { 0 };
	uint8_t ota_recv_end = 0; 

    ESP_LOGI(TAG_OTA, "-----Starting OTA...-----");

    const esp_partition_t *configured = esp_ota_get_boot_partition();
    const esp_partition_t *running = esp_ota_get_running_partition();

    assert(configured == running); /* fresh from reset, should be running from configured boot partition */
    ESP_LOGI(TAG_OTA, "-----Running partition type %d subtype %d (offset 0x%08x)-----",
             configured->type, configured->subtype, configured->address);

    /* Wait for the callback to set the CONNECTED_BIT in the
       event group.
    */
OTA_RESTART:
    xEventGroupWaitBits(wifi_event_group, OTA_BIT,
                        false, true, portMAX_DELAY);
    ESP_LOGI(TAG_OTA, "-----Connect to Wifi ! Start to Connect to Server....-----");

    /*connect to http server*/
    if (connect_to_ota_http_server()) {
        ESP_LOGI(TAG_OTA, "-----OTA Connected to http server-----");
    } else {
        ESP_LOGE(TAG_OTA, "-----OTA Connect to http server failed!-----");
        //task_fatal_error();
        ota_clear();//---OK!!!it was verified by jiawei. 20170622
		goto OTA_RESTART;
    }

    int res = -1;
    /*send GET request to http server*/
	sprintf(ota_http_request, "GET %s HTTP/1.1\r\nHost: %s:%d \r\n\r\n", OTA_FILE_PATH, OTA_HTTP_IP, OTA_HTTP_PORT);
    res = send(ota_socket_id, ota_http_request, strlen(ota_http_request), 0);
    if (res == -1) {
        ESP_LOGE(TAG_OTA, "-----OTA Send GET request to server failed-----");
        //task_fatal_error();
        ota_clear();
		goto OTA_RESTART;
    } else {
        ESP_LOGI(TAG_OTA, "-----OTA Send GET request to server succeeded-----");
    }

    update_partition = esp_ota_get_next_update_partition(NULL);
    ESP_LOGI(TAG_OTA, "-----Writing to partition subtype %d at offset 0x%x-----",
             update_partition->subtype, update_partition->address);
    assert(update_partition != NULL);

	
    err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
    if (err != ESP_OK) {
        ESP_LOGE(TAG_OTA, "-----esp_ota_begin failed, error=%d-----", err);
        //task_fatal_error();
        ota_reboot_system();
		//goto OTA_RESTART;
    }
    ESP_LOGI(TAG_OTA, "-----esp_ota_begin succeeded-----");

    bool resp_body_start = false, flag = true;
    /*deal with all receive packet*/
	
    while (flag) {
		ota_start_timer_timeout();
		
        memset(ota_text, 0, OTA_TEXT_BUFFSIZE);
        memset(ota_write_data, 0, OTA_BUFFSIZE);
        int buff_len = recv(ota_socket_id, ota_text, OTA_TEXT_BUFFSIZE, 0);
		printf("OTA_NOW:buff_len = %d,resp_body_start = %d",buff_len,resp_body_start);
		
        if (buff_len < 0) { /*receive error*/
            ESP_LOGE(TAG_OTA, "-----Error: receive data error! errno=%d-----", errno);
            //task_fatal_error();
			ota_recv_end = 0;
			ota_reboot_system();
			//goto OTA_RESTART;
        } else if (buff_len > 0 && !resp_body_start) { /*deal with response header*/
            memcpy(ota_write_data, ota_text, buff_len);
            resp_body_start = read_past_http_header(ota_text, buff_len, update_handle);
        } else if (buff_len > 0 && resp_body_start) { /*deal with response body*/
            memcpy(ota_write_data, ota_text, buff_len);
            err = esp_ota_write( update_handle, (const void *)ota_write_data, buff_len);
            if (err != ESP_OK) {
                ESP_LOGE(TAG_OTA, "-----Error: esp_ota_write failed! err=0x%x-----", err);                
                ota_recv_end = 0;
               ota_reboot_system();
 ota again when ota written half,ota can succeed!!!
				//goto OTA_RESTART;
            }
            binary_file_length += buff_len;
            ESP_LOGI(TAG_OTA, "-----Have written image length %d-----", binary_file_length);
        } else if (buff_len == 0) {  /*packet over*/
            flag = false;
            ESP_LOGI(TAG_OTA, "-----Connection closed, all packets received-----");
			ota_recv_end = 1;
            close(ota_socket_id);
        } else {
            ESP_LOGE(TAG_OTA, "-----Unexpected recv result-----");
        }
    }

    ESP_LOGI(TAG_OTA, "-----Total Write binary data length : %d-----", binary_file_length);

    if (esp_ota_end(update_handle) != ESP_OK) {
		ota_recv_end = 0;
        ESP_LOGE(TAG_OTA, "-----esp_ota_end failed!-----");
        //task_fatal_error();
    }
    err = esp_ota_set_boot_partition(update_partition);
    if (err != ESP_OK) {
		ota_recv_end = 0;
        ESP_LOGE(TAG_OTA, "-----esp_ota_set_boot_partition failed! err=0x%x-----", err);
        //task_fatal_error();
    }
#ifndef __FACTORY_APP_BIN__
	if(ota_recv_end)
	{
		vTaskDelay(1000 / portTICK_PERIOD_MS);
		post_uart_status_to_queue(UART_ESP32_OTA_OVER);
		vTaskDelay(2000 / portTICK_PERIOD_MS);
	}
#endif
    ota_reboot_system();
    return ;

WayneJia
Posts: 17
Joined: Thu Aug 03, 2017 7:28 am

Re: Somrtimes ota works well,sometimes ota will go wrong.

Postby WayneJia » Tue Oct 24, 2017 11:10 am

ESP_Angus wrote:
rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
It looks like your OTA code is corrupting/erasing the bootloader. Does it ever recover from this or do you have to reflash?

Are you using the IDF OTA API? Can you post some of your code?
Other device,it will happen everytime. And the log is different.

Code: Select all

>ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0008,len:8
load:0x3fff0010,len:4384
load:0x40078000,len:11072
load:0x40080000,len:252
csum err:0x51!=0xd1
Falling back to built-in command interpreter.
OK

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Re: Somrtimes ota works well,sometimes ota will go wrong.

Postby gregstewart90 » Tue Oct 24, 2017 1:35 pm

Is bluetooth enabled? I had to disable bluetooth to make OTA work successfully. WIFI and bluetooth can exist at the same time, but it can cause issues.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Somrtimes ota works well,sometimes ota will go wrong.

Postby ESP_Angus » Thu Oct 26, 2017 2:17 am

I have a few more questions and some suggestions:

What IDF version are you using? And what hardware?

In your log output, is the partition address correctly logged when you start writing OTA output?

Does the reset happen immediately after the OTA update starts, or does it freeze first?

In make menuconfig, you can try disabling "Interrupt watchdog" and "Task watchdog" in Component Config -> ESP32-Specific. Maybe try them one at a time. One of these may be triggerring the WDT reset which you are seeing as the cause when the device resets.

Also, do you have the "Hardware brownout detect & reset" enabled under this menu? You can try adjusting this setting and see if anything changes.

WayneJia
Posts: 17
Joined: Thu Aug 03, 2017 7:28 am

Re: Somrtimes ota works well,sometimes ota will go wrong.

Postby WayneJia » Thu Oct 26, 2017 7:35 am

ESP_Angus wrote:I have a few more questions and some suggestions:

What IDF version are you using? And what hardware?

In your log output, is the partition address correctly logged when you start writing OTA output?

Does the reset happen immediately after the OTA update starts, or does it freeze first?

In make menuconfig, you can try disabling "Interrupt watchdog" and "Task watchdog" in Component Config -> ESP32-Specific. Maybe try them one at a time. One of these may be triggerring the WDT reset which you are seeing as the cause when the device resets.

Also, do you have the "Hardware brownout detect & reset" enabled under this menu? You can try adjusting this setting and see if anything changes.
1.IDF version is v2.1
2.Hardware is nrf52 control Pin9 CHIP_PU to turn on or turn off esp32. Chip is ESP32D0WDQ6 (revision 0).
3.The partition address Iis correct.
4.It freeze few second(about 2 to 5 second) first,then Reset happen.
5.I have not the "Hardware brownout detect & reset" menu.

Important:
I find that I must use DC POWER SUPPLY and voltage must be higher than 4.5V,the OTA will success.
When I use battery ,it will fail most time.



6.When I try disabling "Interrupt watchdog" and "Task watchdog",the log is the same.

Code: Select all

[0;32mI (72204) YH_OTA: -----OTA Send GET request to server succeeded-----[0m
[0;32mI (72211) YH_OTA: -----Writing to partition subtype 16 at offset 0xa0000-----[0m
ets Jun  8 2016 00:22:57

rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
Falling back to built-in command interpreter.
OK
>ets Jun  8 2016 00:22:57

rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
Falling back to built-in command interpreter.
OK
>ets Jun  8 2016 00:22:57

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Somrtimes ota works well,sometimes ota will go wrong.

Postby ESP_Angus » Thu Oct 26, 2017 8:14 am

WayneJia wrote: 5.I have not the "Hardware brownout detect & reset" menu.

Important:
I find that I must use DC POWER SUPPLY and voltage must be higher than 4.5V,the OTA will success.
When I use battery ,it will fail most time.
OK. It sounds like the ESP32 and/or the flash chip is browning out due to insufficient power. ESP32 browning out can cause resets. The flash chip is browning out then this can cause writes to the wrong addresses, which explains the corruption you are seeing.

Is the DC power supply or battery feeding a 3.3V voltage regulator of some kind? You may need to check the voltage regulator current capacity (and dropout voltage in the case of the battery), and make sure you have enough capacitance on all the power rails.

IDF master branch (pre-V3.0) has brownout detector support in software so brownouts on the ESP32 should be easier to catch and understand.

Who is online

Users browsing this forum: No registered users and 78 guests