[SOLVED]Load spiffs_image.img after OTA

elie_daan
Posts: 5
Joined: Thu Jul 04, 2019 8:27 am

[SOLVED]Load spiffs_image.img after OTA

Postby elie_daan » Fri Jul 05, 2019 9:21 am

Hello,

After a lot of research I can't yet fix my issue. I'm trying to update my program including the images inside with OTA.
My program is based on the native_ota_example provided by Espressif and the loboris TFT library for spiffs image.

I have two tasks. One is for download the .bin file contained my program and another task executed first to download the spiffs_image.img file into spiffs partition at 0x002b0000.

My partition table is :

boot: Partition Table:

Label Usage Type ST Offset Length
0 nvs WiFi data 01 02 00009000 00004000
1 otadata OTA data 01 00 0000d000 00002000
2 phy_init RF data 01 01 0000f000 00001000
3 factory factory app 00 00 00010000 000e0000
4 ota_0 OTA app 00 10 000f0000 000e0000
5 ota_1 OTA app 00 11 001d0000 000e0000
6 storage Unknown data 01 82 002b0000 00100000

To download the spiffs_image and write it into spiffs partition , I write this code :
  1. /**
  2.  
  3. @brief Task to update spiffs partition
  4. @param pvParameter
  5. */
  6. static void ota_spiffs_task(void pvParameter)
  7. {
  8. esp_err_t err;
  9. / update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
  10. esp_ota_handle_t update_handle = 0 ;
  11. const esp_partition_t *update_partition = NULL;
  12. esp_partition_t *spiffs_partition=NULL;
  13. ESP_LOGI(TAG, "Starting OTA SPIFFS...");
  14.  
  15. const esp_partition_t *configured = esp_ota_get_boot_partition();
  16. const esp_partition_t *running = esp_ota_get_running_partition();
  17.  
  18. /*Update SPIFFS : 1/ First we need to find SPIFFS partition  */
  19.  
  20. esp_partition_iterator_t spiffs_partition_iterator=esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS,NULL);
  21. while(spiffs_partition_iterator !=NULL){
  22.     spiffs_partition = (esp_partition_t *)esp_partition_get(spiffs_partition_iterator);
  23.     printf("main: partition type = %d.\n", spiffs_partition->type);
  24.     printf("main: partition subtype = %d.\n", spiffs_partition->subtype);
  25.     printf("main: partition starting address = %x.\n", spiffs_partition->address);
  26.     printf("main: partition size = %x.\n", spiffs_partition->size);
  27.     printf("main: partition label = %s.\n", spiffs_partition->label);
  28.     printf("main: partition subtype = %d.\n", spiffs_partition->encrypted);
  29.     printf("\n");
  30.     printf("\n");
  31.     spiffs_partition_iterator=esp_partition_next(spiffs_partition_iterator);
  32. }
  33. vTaskDelay(1000/portTICK_RATE_MS);
  34. esp_partition_iterator_release(spiffs_partition_iterator);
  35.  
  36. /* Wait for the callback to set the CONNECTED_BIT in the
  37.    event group.
  38. */
  39. xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  40.                     false, true, portMAX_DELAY);
  41. ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server....");
  42.  
  43. esp_http_client_config_t config = {
  44.     .url = SPIFFS_SERVER_URL,
  45.     .cert_pem = (char *)server_cert_pem_start,
  46. };
  47. esp_http_client_handle_t client = esp_http_client_init(&config);
  48. if (client == NULL) {
  49.     ESP_LOGE(TAG, "Failed to initialise HTTP connection");
  50.     task_fatal_error();
  51. }
  52. err = esp_http_client_open(client, 0);
  53. if (err != ESP_OK) {
  54.     ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  55.     esp_http_client_cleanup(client);
  56.     task_fatal_error();
  57. }
  58. esp_http_client_fetch_headers(client);
  59.  
  60. /* 2: Delete SPIFFS Partition  */
  61. err=esp_partition_erase_range(spiffs_partition,spiffs_partition->address,spiffs_partition->size);
  62.  
  63.  
  64. int binary_file_length = 0;
  65. /*deal with all receive packet*/
  66. while (1) {
  67.     int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
  68.     if (data_read < 0) {
  69.         ESP_LOGE(TAG, "Error: SSL data read error");
  70.         http_cleanup(client);
  71.         task_fatal_error();
  72.     } else if (data_read > 0) {
  73.         /* 3 : WRITE SPIFFS PARTITION */
  74.         err = esp_partition_write(spiffs_partition,0x0,(const void *)ota_write_data, data_read);
  75.  
  76.         if (err != ESP_OK) {
  77.             http_cleanup(client);
  78.             task_fatal_error();
  79.         }
  80.        
  81.         binary_file_length += data_read;
  82.         ESP_LOGD(TAG, "Written image length %d", binary_file_length);
  83.     } else if (data_read == 0) {
  84.         ESP_LOGI(TAG, "Connection closed,all data received");
  85.         break;
  86.     }
  87. }
  88. ESP_LOGI(TAG, "Total Write binary data length : %d", binary_file_length);
  89.  
  90. ESP_LOGI(TAG, "Prepare to launch ota APP task or restart!");
  91. xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
  92. vTaskDelete(NULL);
  93. }
After reboot, the program is correctly executed but without the images.
I get this log error :
E (176) SPIFFS: mount failed, -10025
E (179) [SPIFFS Test]: Failed to mount or format filesystem

I need to run make flashfs to display images correctly.

Thanks a lot for your help
Last edited by elie_daan on Wed Jul 10, 2019 12:21 pm, edited 1 time in total.

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Load spiffs_image.img after OTA

Postby Ritesh » Sun Jul 07, 2019 2:52 am

elie_daan wrote:
Fri Jul 05, 2019 9:21 am
Hello,

After a lot of research I can't yet fix my issue. I'm trying to update my program including the images inside with OTA.
My program is based on the native_ota_example provided by Espressif and the loboris TFT library for spiffs image.

I have two tasks. One is for download the .bin file contained my program and another task executed first to download the spiffs_image.img file into spiffs partition at 0x002b0000.

My partition table is :

boot: Partition Table:

Label Usage Type ST Offset Length
0 nvs WiFi data 01 02 00009000 00004000
1 otadata OTA data 01 00 0000d000 00002000
2 phy_init RF data 01 01 0000f000 00001000
3 factory factory app 00 00 00010000 000e0000
4 ota_0 OTA app 00 10 000f0000 000e0000
5 ota_1 OTA app 00 11 001d0000 000e0000
6 storage Unknown data 01 82 002b0000 00100000

To download the spiffs_image and write it into spiffs partition , I write this code :
  1. /**
  2.  
  3. @brief Task to update spiffs partition
  4. @param pvParameter
  5. */
  6. static void ota_spiffs_task(void pvParameter)
  7. {
  8. esp_err_t err;
  9. / update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
  10. esp_ota_handle_t update_handle = 0 ;
  11. const esp_partition_t *update_partition = NULL;
  12. esp_partition_t *spiffs_partition=NULL;
  13. ESP_LOGI(TAG, "Starting OTA SPIFFS...");
  14.  
  15. const esp_partition_t *configured = esp_ota_get_boot_partition();
  16. const esp_partition_t *running = esp_ota_get_running_partition();
  17.  
  18. /*Update SPIFFS : 1/ First we need to find SPIFFS partition  */
  19.  
  20. esp_partition_iterator_t spiffs_partition_iterator=esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS,NULL);
  21. while(spiffs_partition_iterator !=NULL){
  22.     spiffs_partition = (esp_partition_t *)esp_partition_get(spiffs_partition_iterator);
  23.     printf("main: partition type = %d.\n", spiffs_partition->type);
  24.     printf("main: partition subtype = %d.\n", spiffs_partition->subtype);
  25.     printf("main: partition starting address = %x.\n", spiffs_partition->address);
  26.     printf("main: partition size = %x.\n", spiffs_partition->size);
  27.     printf("main: partition label = %s.\n", spiffs_partition->label);
  28.     printf("main: partition subtype = %d.\n", spiffs_partition->encrypted);
  29.     printf("\n");
  30.     printf("\n");
  31.     spiffs_partition_iterator=esp_partition_next(spiffs_partition_iterator);
  32. }
  33. vTaskDelay(1000/portTICK_RATE_MS);
  34. esp_partition_iterator_release(spiffs_partition_iterator);
  35.  
  36. /* Wait for the callback to set the CONNECTED_BIT in the
  37.    event group.
  38. */
  39. xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  40.                     false, true, portMAX_DELAY);
  41. ESP_LOGI(TAG, "Connect to Wifi ! Start to Connect to Server....");
  42.  
  43. esp_http_client_config_t config = {
  44.     .url = SPIFFS_SERVER_URL,
  45.     .cert_pem = (char *)server_cert_pem_start,
  46. };
  47. esp_http_client_handle_t client = esp_http_client_init(&config);
  48. if (client == NULL) {
  49.     ESP_LOGE(TAG, "Failed to initialise HTTP connection");
  50.     task_fatal_error();
  51. }
  52. err = esp_http_client_open(client, 0);
  53. if (err != ESP_OK) {
  54.     ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  55.     esp_http_client_cleanup(client);
  56.     task_fatal_error();
  57. }
  58. esp_http_client_fetch_headers(client);
  59.  
  60. /* 2: Delete SPIFFS Partition  */
  61. err=esp_partition_erase_range(spiffs_partition,spiffs_partition->address,spiffs_partition->size);
  62.  
  63.  
  64. int binary_file_length = 0;
  65. /*deal with all receive packet*/
  66. while (1) {
  67.     int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
  68.     if (data_read < 0) {
  69.         ESP_LOGE(TAG, "Error: SSL data read error");
  70.         http_cleanup(client);
  71.         task_fatal_error();
  72.     } else if (data_read > 0) {
  73.         /* 3 : WRITE SPIFFS PARTITION */
  74.         err = esp_partition_write(spiffs_partition,0x0,(const void *)ota_write_data, data_read);
  75.  
  76.         if (err != ESP_OK) {
  77.             http_cleanup(client);
  78.             task_fatal_error();
  79.         }
  80.        
  81.         binary_file_length += data_read;
  82.         ESP_LOGD(TAG, "Written image length %d", binary_file_length);
  83.     } else if (data_read == 0) {
  84.         ESP_LOGI(TAG, "Connection closed,all data received");
  85.         break;
  86.     }
  87. }
  88. ESP_LOGI(TAG, "Total Write binary data length : %d", binary_file_length);
  89.  
  90. ESP_LOGI(TAG, "Prepare to launch ota APP task or restart!");
  91. xTaskCreate(&ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
  92. vTaskDelete(NULL);
  93. }
After reboot, the program is correctly executed but without the images.
I get this log error :
E (176) SPIFFS: mount failed, -10025
E (179) [SPIFFS Test]: Failed to mount or format filesystem

I need to run make flashfs to display images correctly.

Thanks a lot for your help
Hello,

Which ESP32 IDF you are using? Also are you using latest Loboris code for your development purpose?

Which partition you hav used for SPIFFS?

Would you please check manually first to flash that image into SPIFFS partition and make sure that it is working fine or not without any issue?
Regards,
Ritesh Prajapati

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Load spiffs_image.img after OTA

Postby WiFive » Sun Jul 07, 2019 3:15 am

Every write should not start at zero

Code: Select all

esp_partition_write(spiffs_partition,0x0,(const void *)ota_write_data, data_read);

elie_daan
Posts: 5
Joined: Thu Jul 04, 2019 8:27 am

Re: Load spiffs_image.img after OTA

Postby elie_daan » Mon Jul 08, 2019 7:51 am

Hello,

This morning I've updated ESP-IDF so now I'm using the v3.2. The partition for spiffs is "storage" at adress 0x2b0000 and the size is 1MB.

Yes, with make flashfs the Spiffs is correctly mounted..

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Load spiffs_image.img after OTA

Postby Ritesh » Tue Jul 09, 2019 8:53 am

elie_daan wrote:
Mon Jul 08, 2019 7:51 am
Hello,

This morning I've updated ESP-IDF so now I'm using the v3.2. The partition for spiffs is "storage" at adress 0x2b0000 and the size is 1MB.

Yes, with make flashfs the Spiffs is correctly mounted..
Ok.

So, After updating ESP32 IDF, are you still facing same issue? or issue has been resolved?

Also if manually SPIFFS is working fine without any issue then there might be issue into sequence or calls which you have used.
Regards,
Ritesh Prajapati

elie_daan
Posts: 5
Joined: Thu Jul 04, 2019 8:27 am

Re: Load spiffs_image.img after OTA

Postby elie_daan » Tue Jul 09, 2019 10:01 am

Hello Ritesh,

No, I'm facing the same issue. Also I've tried to upload spiffs_images.img into the loboris example with my task ota_spiffs_task but it's also formatting the spiffs partition after reboot.

Yes but I don't understand why it's working with flashfs and not with esp_partition_write. I really need to update the jpeg by OTA :cry:

flashfs is :

Code: Select all

flashfs: $(SDKCONFIG_MAKEFILE) mkspiffs
	@echo "Making spiffs image ..."
	@echo "$(ESPTOOLPY_WRITE_FLASH)"
	$(MKSPIFFS_COMPONENT_PATH)/../mkspiffs/src/mkspiffs -c $(SPIFFS_IMAGE_COMPONENT_PATH)/image -b $(CONFIG_SPIFFS_LOG_BLOCK_SIZE) -p $(CONFIG_SPIFFS_LOG_PAGE_SIZE) -s $(CONFIG_SPIFFS_SIZE) $(BUILD_DIR_BASE)/spiffs_image.img
	$(ESPTOOLPY_WRITE_FLASH) $(CONFIG_SPIFFS_BASE_ADDR) $(BUILD_DIR_BASE)/spiffs_image.img 
Best regards,
Elie.

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Load spiffs_image.img after OTA

Postby Ritesh » Tue Jul 09, 2019 2:01 pm

elie_daan wrote:
Tue Jul 09, 2019 10:01 am
Hello Ritesh,

No, I'm facing the same issue. Also I've tried to upload spiffs_images.img into the loboris example with my task ota_spiffs_task but it's also formatting the spiffs partition after reboot.

Yes but I don't understand why it's working with flashfs and not with esp_partition_write. I really need to update the jpeg by OTA :cry:

flashfs is :

Code: Select all

flashfs: $(SDKCONFIG_MAKEFILE) mkspiffs
	@echo "Making spiffs image ..."
	@echo "$(ESPTOOLPY_WRITE_FLASH)"
	$(MKSPIFFS_COMPONENT_PATH)/../mkspiffs/src/mkspiffs -c $(SPIFFS_IMAGE_COMPONENT_PATH)/image -b $(CONFIG_SPIFFS_LOG_BLOCK_SIZE) -p $(CONFIG_SPIFFS_LOG_PAGE_SIZE) -s $(CONFIG_SPIFFS_SIZE) $(BUILD_DIR_BASE)/spiffs_image.img
	$(ESPTOOLPY_WRITE_FLASH) $(CONFIG_SPIFFS_BASE_ADDR) $(BUILD_DIR_BASE)/spiffs_image.img
Best regards,
Elie.
Hello,

Would you please read flashed data which you have written using OTA mechanism and check with original image and see like is there any difference into that or not?

So you will get an idea regarding issue which you are facing.
Regards,
Ritesh Prajapati

elie_daan
Posts: 5
Joined: Thu Jul 04, 2019 8:27 am

Re: Load spiffs_image.img after OTA

Postby elie_daan » Wed Jul 10, 2019 9:25 am

Hello,

Thanks you Ritesh for your help. Finally I resolved the problem. It was always writing at the beginning of the partition. I had to implement a offset because my buffer was 1Kb.

Code: Select all

int offset=0;
    /*deal with all receive packet*/
    while (1) {
        int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
        if (data_read < 0) {
            ESP_LOGE(TAG, "Error: SSL data read error");
            http_cleanup(client);
            task_fatal_error();
        } else if (data_read > 0) {
            /* 3 : WRITE SPIFFS PARTITION */
            err= esp_partition_write(spiffs_partition,offset,(const void *)ota_write_data, data_read);
            
            if (err != ESP_OK) {
                http_cleanup(client);
                task_fatal_error();
            }
            offset=offset+1024;

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Load spiffs_image.img after OTA

Postby WiFive » Wed Jul 10, 2019 2:34 pm

:idea:
WiFive wrote:
Sun Jul 07, 2019 3:15 am
Every write should not start at zero

Code: Select all

esp_partition_write(spiffs_partition,0x0,(const void *)ota_write_data, data_read);
Anyway, you should use data_read because it is not guaranteed to be the full buffer size

elie_daan
Posts: 5
Joined: Thu Jul 04, 2019 8:27 am

Re: Load spiffs_image.img after OTA

Postby elie_daan » Wed Jul 10, 2019 3:36 pm

WiFive wrote:
Wed Jul 10, 2019 2:34 pm
:idea:
WiFive wrote:
Sun Jul 07, 2019 3:15 am
Every write should not start at zero

Code: Select all

esp_partition_write(spiffs_partition,0x0,(const void *)ota_write_data, data_read);
Anyway, you should use data_read because it is not guaranteed to be the full buffer size
Hello,

Please can you develop your answer because I don't really understand what do you mean..

Who is online

Users browsing this forum: Baidu [Spider] and 329 guests