OTA失败,且不断重启问题.

xuanzihao
Posts: 8
Joined: Thu Dec 06, 2018 3:59 am

OTA失败,且不断重启问题.

Postby xuanzihao » Wed Jan 09, 2019 10:18 am

Hello
最近碰到一个问题~
进行OTA开发时,参考OTA的DEMO
升级OTA1分区时,
在esp_ota_begin中,等待2S后,没有退出此函数,并马上重启,而且OTA0也无法读取了,导致不断重启
备注:因为不使用https,使用http,所以不用文件的cert,直接设置了空的数组

const uint8_t server_cert_pem_start[]="";

分区表如下:
nvs, data, nvs, , 0x73000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, , 0x1b4000,
ota_1, app, ota_1, , 0x1b4000,
LOG如下:

[0;32mI (12655) HXJ_OTA: Starting hxj_ota_task...[0m
[0;32mI (12655) HXJ_OTA: Running partition type 0 subtype 16 (offset 0x00080000)[0m
[0;32mI (12895) HXJ_OTA: Starting OTA...[0m
[0;32mI (12895) HXJ_OTA: Writing to partition subtype 17 at offset 0x240000[0m
Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x40092c89 PS : 0x00060e33 A0 : 0x800d3428 A1 : 0x3ffbc2f0
A2 : 0xffffffff A3 : 0x7fffffff A4 : 0x80091f68 A5 : 0x3fff1810
A6 : 0x00000003 A7 : 0x00060023 A8 : 0x0000cdcd A9 : 0xb33fffff
A10 : 0x0000abab A11 : 0x00060e20 A12 : 0x00000000 A13 : 0x00000001
A14 : 0x0000abab A15 : 0x00000004 SAR : 0x00000000 EXCCAUSE: 0x0000001d
EXCVADDR: 0xffffffff LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000

Backtrace: 0x40092c89:0x3ffbc2f0 0x400d3425:0x3ffbc310 0x400d3473:0x3ffbc340 0x400d372d:0x3ffbc360 0x400932cd:0x3ffbc380

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6192
ho 0 tail 12 room 4
load:0x40078000,len:10168
ho 0 tail 12 room 4
load:0x40080400,len:6552
entry 0x40080764
[0;32mI (32) boot: ESP-IDF v3.2-beta1-34-gfb7ba1b 2nd stage bootloader[0m
[0;32mI (32) boot: compile time 11:54:57[0m
[0;32mI (33) boot: Enabling RNG early entropy source...[0m
[0;32mI (39) boot: SPI Speed : 40MHz[0m
[0;32mI (43) boot: SPI Mode : DIO[0m
[0;32mI (47) boot: SPI Flash Size : 4MB[0m
[0;32mI (51) boot: Partition Table:[0m
[0;32mI (54) boot: ## Label Usage Type ST Offset Length[0m
[0;32mI (62) boot: 0 nvs WiFi data 01 02 00009000 00073000[0m
[0;32mI (69) boot: 1 otadata OTA data 01 00 0007c000 00002000[0m
[0;32mI (77) boot: 2 phy_init RF data 01 01 0007e000 00001000[0m
[0;32mI (84) boot: 3 ota_0 OTA app 00 10 00080000 001b4000[0m
[0;32mI (92) boot: 4 ota_1 OTA app 00 11 00240000 001b4000[0m
[0;32mI (99) boot: End of partition table[0m
[0;32mI (103) boot: No factory image, trying OTA 0[0m
[0;31mE (108) esp_image: image at 0x80000 has invalid magic byte[0m
[0;33mW (114) esp_image: image at 0x80000 has invalid SPI mode 255[0m
[0;33mW (121) esp_image: image at 0x80000 has invalid SPI size 15[0m
[0;31mE (127) boot: OTA app partition slot 0 is not bootable[0m
[0;31mE (133) esp_image: image at 0x240000 has invalid magic byte[0m
[0;33mW (139) esp_image: image at 0x240000 has invalid SPI mode 255[0m
[0;33mW (145) esp_image: image at 0x240000 has invalid SPI size 15[0m
[0;31mE (152) boot: OTA app partition slot 1 is not bootable[0m
[0;31mE (158) boot: No bootable app partitions in the partition table[0m
ets Jun 8 2016 00:22:57


代码如下:

Code: Select all

void hxj_ota_task(void * pvParameter)
{
    ESP_LOGI(TAG, "Starting hxj_ota_task...");

    
    esp_http_client_config_t config = {
        .url = "http://file2.hxjiot.com/firmware/C849A7A10470000151FB14E91918A870.bin",
        .cert_pem = (char *)server_cert_pem_start,
        .event_handler = _http_event_handler,
    };
    esp_err_t ret = esp_https_ota(&config);
    if (ret == ESP_OK) {
        esp_restart();
    } else {
        ESP_LOGE(TAG, "Firmware Upgrades Failed");
    }
    while (1) {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
esp_err_t esp_https_ota(const esp_http_client_config_t *config)
{
	/*
    if (!config) {
        ESP_LOGE(TAG, "esp_http_client config not found");
        return ESP_ERR_INVALID_ARG;
    }

    if (!config->cert_pem) {
        ESP_LOGE(TAG, "Server certificate not found in esp_http_client config");
        return ESP_FAIL;
    }
    */
	const esp_partition_t *configured = esp_ota_get_boot_partition();
	const esp_partition_t *running = esp_ota_get_running_partition();

	if (configured != running) {
		ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
				 configured->address, running->address);
		ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
	}
	ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
			 running->type, running->subtype, running->address);

    esp_http_client_handle_t client = esp_http_client_init(config);
    if (client == NULL) {
        ESP_LOGE(TAG, "Failed to initialise HTTP connection");
        return ESP_FAIL;
    }

	/*
    if (esp_http_client_get_transport_type(client) != HTTP_TRANSPORT_OVER_SSL) {
        ESP_LOGE(TAG, "Transport is not over HTTPS");
        return ESP_FAIL;
    }
    */

    esp_err_t err = esp_http_client_open(client, 0);
    if (err != ESP_OK) {
        esp_http_client_cleanup(client);
        ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
        return err;
    }

    esp_http_client_fetch_headers(client);
    esp_ota_handle_t update_handle = 0;
    const esp_partition_t *update_partition = NULL;
    ESP_LOGI(TAG, "Starting OTA...");
    update_partition = esp_ota_get_next_update_partition(NULL);
    if (update_partition == NULL) {
        ESP_LOGE(TAG, "Passive OTA partition not found");
        http_cleanup(client);
        return ESP_FAIL;
    }
    ESP_LOGI(TAG, "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, "esp_ota_begin failed, error=%d", err);
        http_cleanup(client);
        return err;
    }
    ESP_LOGI(TAG, "esp_ota_begin succeeded");
    ESP_LOGI(TAG, "Please Wait. This may take time");

    esp_err_t ota_write_err = ESP_OK;
    char *upgrade_data_buf = (char *)malloc(OTA_BUF_SIZE);
    if (!upgrade_data_buf) {
        ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
        return ESP_ERR_NO_MEM;
    }
    int binary_file_len = 0;
    while (1) {
        int data_read = esp_http_client_read(client, upgrade_data_buf, OTA_BUF_SIZE);
        if (data_read == 0) {
            ESP_LOGI(TAG, "Connection closed,all data received");
            break;
        }
        if (data_read < 0) {
            ESP_LOGE(TAG, "Error: SSL data read error");
            break;
        }
        if (data_read > 0) {
            ota_write_err = esp_ota_write( update_handle, (const void *)upgrade_data_buf, data_read);
            if (ota_write_err != ESP_OK) {
                break;
            }
            binary_file_len += data_read;
            ESP_LOGD(TAG, "Written image length %d", binary_file_len);
        }
    }
    free(upgrade_data_buf);
    http_cleanup(client); 
    ESP_LOGD(TAG, "Total binary data length writen: %d", binary_file_len);
    
    esp_err_t ota_end_err = esp_ota_end(update_handle);
    if (ota_write_err != ESP_OK) {
        ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%d", err);
        return ota_write_err;
    } else if (ota_end_err != ESP_OK) {
        ESP_LOGE(TAG, "Error: esp_ota_end failed! err=0x%d. Image is invalid", ota_end_err);
        return ota_end_err;
    }

    err = esp_ota_set_boot_partition(update_partition);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%d", err);
        return err;
    }
    ESP_LOGI(TAG, "esp_ota_set_boot_partition succeeded"); 

    return ESP_OK;
}

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: OTA失败,且不断重启问题.

Postby ESP_igrr » Wed Jan 09, 2019 12:28 pm

Could you please run the app using IDF Monitor ("make monitor" or "idf.py monitor") tool? This will produce a readable backtrace.
See https://docs.espressif.com/projects/esp ... nitor.html.

The resulting backtrace will give more information to figure out what the issue is.

xuanzihao
Posts: 8
Joined: Thu Dec 06, 2018 3:59 am

Re: OTA失败,且不断重启问题.

Postby xuanzihao » Thu Jan 10, 2019 6:27 am

Hi,I actually find the reason.I just use the 2Mbit flash ,so i replace it to the 4Mbit flash.
and then all is ok.....
Thank you ....

Who is online

Users browsing this forum: No registered users and 157 guests