SuperHouse / esp-open-rtos

Open source FreeRTOS-based ESP8266 software framework
BSD 3-Clause "New" or "Revised" License
1.52k stars 491 forks source link

sdk_spi_flash_erase_sector and OTA issue #665

Open apiel opened 5 years ago

apiel commented 5 years ago

Hi,

I spent more than a week to manage to make the OTA work on one of my device. While it was working without problem on my Wemos mini board, I had issue with my Sonoff Basic.

  1. esptool was detecting 16mb but after making some test on the size of the memory I found out that it was 8mb, therefor I need to use make flash FLASH_SIZE=8
  2. It seem that sdk_spi_flash_erase_sector is not very stable after that the wifi is connected, so the first thing I do when the rom start is to cleanup some space for the next rom:
extern "C" void user_init(void)
{
    uart_set_baud(0, 115200);
    printf("SDK version: %s\n", sdk_system_get_sdk_version());

    rboot_config rboot_config = rboot_get_config();
    uint8_t nextRom = (rboot_config.current_rom+1)%rboot_config.count;
    printf("ROMROM: current %d count %d offset %d\n", rboot_config.current_rom, rboot_config.count, rboot_config.roms[rboot_config.current_rom]);
    printf("ROMROM: next rom %d next offset %d\n", nextRom, rboot_config.roms[nextRom]);

    int end = rboot_config.roms[nextRom] + 350000; // we assume that our firmware is less than 350 000 bytes
    for(int offset = rboot_config.roms[nextRom]; offset < end; offset += SPI_FLASH_SEC_SIZE) {
        if(sdk_spi_flash_erase_sector(offset/SPI_FLASH_SEC_SIZE) != SPI_FLASH_RESULT_OK) {
            printf("\nsdk_spi_flash_erase_sector stop earlier than plan at %d!\n", offset);
            break;
        }
    }
.....
  1. Then we have to connect to the wifi and do nothing else, wait for the connect:
void wifi_wait_connection(void)
{
    for(int retry = 20; retry > 0; retry--) {
        if (sdk_wifi_station_get_connect_status() == STATION_GOT_IP) {
            break;
        }
        printf(".\n"); // we could show the status
        taskYIELD();
        vTaskDelay(100);
    }
    if (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
        sdk_system_restart();
    }
    printf("\nConnected to wifi\n");
}

static void  main_task(void *pvParameters)
{
    wifi_wait_connection();
...
  1. Finally when the connection succeed, I run http client OTA (and nothing else) but first I had to comment out the sdk_spi_flash_erase_sector in extras/http_client_ota/http_client_ota.c around line 75
    // // Ready for flash device, the erase NANDFLASH Block
    // if (flash_offset % SECTOR_SIZE == 0) {
    //     unsigned int sector;

    //     sector = flash_offset / SECTOR_SIZE;
    //     sdk_spi_flash_erase_sector(sector);
    // }

So now I wonder if you can see any reason why this happen and if we could fix it?

HomeACcessoryKid commented 5 years ago

you could consider looking at https://github.com/HomeACcessoryKid/life-cycle-manager for a system that works.