Module or chip used: ESP32-S2-WROOM-I
IDF version: v4.2.1-141-g1e3638390
Build System: idf.py
Operating System: Windows10
Using an IDE: Eclipse
Power Supply: USB
Problem Description
I'm not able to read & write a file using non-encrypted FATFS with wear levelling, secure boot[V2] and flash encryption[Release-Mode].
Wear levelling's Sdkconfig screenshot attached.
Partition Table screenshot attached.
I'm using wear levelling partition to store some states/errors/values into separate files. I'm using a maximum of 4 files and use file operation one at a time.
This problem is shown in some of the devices randomly likes 1 out of 10 devices.
I'm using the wrapper function to write and read files into the wear levelling partition.
Init/Mount Wear-levelling partition
- // Handle of the wear levelling library instance
- static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
- /**
- *
- */
- esp_err_t init_wearlevel() {
- ESP_LOGI(TAG, "Mounting FAT filesystem");
- // To mount device we need name of device partition, define base_path
- // and allow format partition in case if it is new one and was not formated before
- const esp_vfs_fat_mount_config_t mount_config = { .max_files = 4, .format_if_mount_failed = true,
- .allocation_unit_size = CONFIG_WL_SECTOR_SIZE };
- esp_err_t err = esp_vfs_fat_spiflash_mount(WEARLEVEL_FATFS_BASE_PATH, "storage", &mount_config, &s_wl_handle);
- if (err != ESP_OK) {
- ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
- return err;
- }
- vTaskDelay(pdMS_TO_TICKS(100));
- return ESP_OK;
- }
Write Function
- /**
- *
- */
- esp_err_t write_into_file(const char *file_path, void *data, size_t data_len) {
- vTaskDelay(pdMS_TO_TICKS(100));
- ESP_LOGI(TAG, "Writing File: %s", file_path);
- FILE *fptr = fopen((const char *)file_path, "wb");
- if (fptr == NULL) {
- ESP_LOGE(TAG, "Failed to open file for writing: %s", file_path);
- return ESP_FAIL;
- }
- vTaskDelay(pdMS_TO_TICKS(100));
- size_t w_byte = fwrite(data, data_len, 1, fptr);
- vTaskDelay(pdMS_TO_TICKS(100));
- fclose(fptr);
- if (w_byte != 1) { // write 1 number of member into to file
- ESP_LOGE(TAG, "Failed to write data into the file: %s", file_path);
- return ESP_FAIL;
- }
- ESP_LOGI(TAG, "");
- return ESP_OK;
- }
Read Function
- /**
- *
- */
- esp_err_t read_from_file(const char *file_path, void *data, size_t data_len) {
- vTaskDelay(pdMS_TO_TICKS(100));
- ESP_LOGI(TAG, "Reading File: %s", file_path);
- FILE *fptr = fopen((const char *)file_path, "rb");
- if (fptr == NULL) {
- ESP_LOGE(TAG, "Failed to open file for reading: %s", file_path);
- return ESP_FAIL;
- }
- vTaskDelay(pdMS_TO_TICKS(100));
- size_t r_byte = fread(data, data_len, 1, fptr);
- vTaskDelay(pdMS_TO_TICKS(100));
- fclose(fptr);
- if (r_byte != 1) {
- ESP_LOGE(TAG, "Failed to read data from the file: %s", file_path);
- return ESP_FAIL;
- }
- ESP_LOGI(TAG, "");
- vTaskDelay(pdMS_TO_TICKS(100));
- return ESP_OK;
- }
Functions Usages
- // Wearlevelling Storage Path
- #define CUSTOM_MAC_ADDR_FNAME "/spiflash/cust_mac.txt"
- uint8_t cust_mac[6] = { 0x7c, 0x7c, 0x7c, 0x00, 0x00, 0x01 };
- /**
- *
- */
- if(write_into_file(CUSTOM_MAC_ADDR_FNAME , cust_mac, sizeof(cust_mac) != ESP_OK){
- return ESP_FAIL;
- }
- /**
- *
- */
- uint8_t read_cust_mac[6] = { 0 };
- esp_err_t get_mac_id_from_file()
- {
- ESP_LOGI(TAG, "Going to read custom mac data from %s",CUSTOM_MAC_ADDR_FNAME);
- esp_err_t err = read_from_file(CUSTOM_MAC_ADDR_FNAME, read_cust_mac, sizeof(read_cust_mac));
- if(err == ESP_OK) {
- ESP_LOGI(TAG, "MAC read from file = %02x:%02x:%02x:%02x:%02x:%02x", read_cust_mac[0],read_cust_mac[1],read_cust_mac[2],
- read_cust_mac[3],read_cust_mac[4],read_cust_mac[5],);
- return ESP_OK;
- } else {
- return ESP_FAIL;
- }
- vTaskDelay(30 / portTICK_PERIOD_MS);
- }