Flash memory data erase

#1

If you are asking a question, please follow this template:

  1. My goal is: don’t wants tp erase previous data while uploading new code
  2. My actions are: I store the data in esp32 flash memory but when I manually upload new code via USB Cable then the previous data is erase but if we are giving OTA then flash memory does not erase the data.is there any method to which doesn’t erase the flash memory while uploading the code via USB.
  3. The result I see is: when I upload new code via USB then data of flash memory will store as it is.
  4. My expectation & question is: is there any partition systems which store the previous data in flash.
#2

When flashing a new firmware the expected behavior is to write the application and the filesystem in the first slot.

When doing OTA update, the new firmware (application and filesystem) is written in the available slot and upon boot the files from the previous filesystem are copied over in the new filesystem.

If you need to preserve some files after flashing a new version, you can create an extra filesystem and save the data there.

#3

thank you so much…can you provide me any example so I can understand it better.

#4

A simple example to send the log files to the extra partition.
Clone the empty app and modify mos.yml

config_schema:
  - ["debug.level", 2]
  
  # fstab options
  - ["fstab.fs0.opts", '{"bs": 4096, "es": 4096}']
  - ["fstab.fs0.path", "/ext"]
  - ["fstab.fs0.create", true] # use this line only when first flashing the firmware
  #- ["fstab.fs0.created", true] # use this line after the first flash of the firmware
  
  - ["file_logger.enable", true]
  - ["file_logger.dir", "/ext"]

libs:
  - origin: https://github.com/mongoose-os-libs/file-logger
  - origin: https://github.com/mongoose-os-libs/fstab
  - origin: https://github.com/mongoose-os-libs/ota-http-server
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/rpc-ws
  - origin: https://github.com/mongoose-os-libs/sntp
  - origin: https://github.com/mongoose-os-libs/vfs-fs-spiffs
  - origin: https://github.com/mongoose-os-libs/wifi

conds:
  - when: mos.platform == "esp32"
    apply:
      config_schema:
      # esp32 partition
        - ["fstab.fs0.dev", "fs_ext"]
        - ["fstab.fs0.type", "SPIFFS"]
      build_vars:
        ESP_IDF_EXTRA_PARTITION: fs_ext,data,spiffs,,256K
  - when: mos.platform == "esp8266"
    apply:
      libs:
        - origin: https://github.com/mongoose-os-libs/vfs-dev-part
      config_schema:
        - ["devtab.dev0.name", "data"]
        - ["devtab.dev0.type", "part"]
        - ["devtab.dev0.opts", '{"dev": "sfl0", "offset": 0x300000, "size": 0x40000}']
        - ["fstab.fs0.dev", "data"]
        - ["fstab.fs0.type", "SPIFFS"]
        - ["fstab.fs0.opts", '{"bs": 4096, "es": 4096}']
        - ["fstab.fs0.path", "/ext"]
mos ls -l ext
log_000-19700101-000115.log 11335
log_001-19700101-000001.log 3933
log_002-20210224-131209.log 3978
log_003-20210224-131624.log 3978
log_004-20210224-132039.log 2107
#5

thank you so much @nliviu

#6

#include “mgos.h”
static void timer_cb()
{
char filename1[40] = “/ext/append.csv”;
FILE f;
LOG(LL_INFO,(“Writing”));
f = fopen(filename1, “a”);
fprintf(f, “1,2,3,4,5,6,7\n”);
fclose(f);
LOG(LL_INFO, ("%s",“Reading file”));
f = fopen(filename1, “r”);
if (f == NULL)
{
LOG(LL_INFO, ("%s",“Failed to open file for reading”));
return;
}
char line[64];
fgets(line, sizeof(line), f);
fclose(f);
char
pos = strchr(line, ‘\n’);
if (pos)
{
*pos = ‘\0’;
}
LOG(LL_INFO, (“Read from file: %s”, line));
}
enum mgos_app_init_result mgos_app_init(void)
{
mgos_set_timer(100 , true, timer_cb, NULL);
return MGOS_APP_INIT_SUCCESS;
}

this is my code still my flash memory doesn’t store the previous data when the new code flash

#7

Use ["fstab.fs0.created", true] when you build the firmware after the first flash and comment out ["fstab.fs0.create", true].

#8

thank you…now its work for me

#9

Of course it works. You didn’t read these notes

  - ["fstab.fs0.create", true] # use this line only when first flashing the firmware
  #- ["fstab.fs0.created", true] # use this line after the first flash of the firmware