mirror of
https://github.com/espressif/esp-idf.git
synced 2026-04-27 19:13:21 +00:00
fatfs: added an API to format FAT on spiflash
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
#define EXAMPLE_MAX_CHAR_SIZE 128
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
// Handle of the wear levelling library instance
|
||||
@@ -25,6 +27,42 @@ static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
|
||||
// Mount path for the partition
|
||||
const char *base_path = "/spiflash";
|
||||
|
||||
static esp_err_t s_example_write_file(char *path, char *data)
|
||||
{
|
||||
ESP_LOGI(TAG, "Opening file");
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (f == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to open file for writing");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
fprintf(f, data);
|
||||
fclose(f);
|
||||
ESP_LOGI(TAG, "File written");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t s_example_read_file(char *path)
|
||||
{
|
||||
ESP_LOGI(TAG, "Reading file");
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (f == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to open file for reading");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
char line[EXAMPLE_MAX_CHAR_SIZE];
|
||||
fgets(line, sizeof(line), f);
|
||||
fclose(f);
|
||||
// strip newline
|
||||
char *pos = strchr(line, '\n');
|
||||
if (pos) {
|
||||
*pos = '\0';
|
||||
}
|
||||
ESP_LOGI(TAG, "Read from file: '%s'", line);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Mounting FAT filesystem");
|
||||
@@ -40,32 +78,49 @@ void app_main(void)
|
||||
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "Opening file");
|
||||
FILE *f = fopen("/spiflash/hello.txt", "wb");
|
||||
if (f == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
fprintf(f, "written using ESP-IDF %s\n", esp_get_idf_version());
|
||||
fclose(f);
|
||||
ESP_LOGI(TAG, "File written");
|
||||
|
||||
// Open file for reading
|
||||
ESP_LOGI(TAG, "Reading file");
|
||||
f = fopen("/spiflash/hello.txt", "rb");
|
||||
if (f == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to open file for reading");
|
||||
//Create file and write
|
||||
char data[EXAMPLE_MAX_CHAR_SIZE];
|
||||
snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s\n", "hello world, from ESP-IDF", esp_get_idf_version());
|
||||
err = s_example_write_file("/spiflash/hello.txt", data);
|
||||
if (err != ESP_OK) {
|
||||
return;
|
||||
}
|
||||
char line[128];
|
||||
fgets(line, sizeof(line), f);
|
||||
fclose(f);
|
||||
// strip newline
|
||||
char *pos = strchr(line, '\n');
|
||||
if (pos) {
|
||||
*pos = '\0';
|
||||
|
||||
//Open file for reading
|
||||
err = s_example_read_file("/spiflash/hello.txt");
|
||||
if (err != ESP_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Format FATFS
|
||||
err = esp_vfs_fat_spiflash_format_rw_wl(base_path, "storage");
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
struct stat st;
|
||||
if (stat("/spiflash/hello.txt", &st) == 0) {
|
||||
ESP_LOGI(TAG, "file still exists");
|
||||
return;
|
||||
} else {
|
||||
ESP_LOGI(TAG, "file doesnt exist, format done");
|
||||
}
|
||||
|
||||
//Create file and write
|
||||
memset(data, 0, EXAMPLE_MAX_CHAR_SIZE);
|
||||
snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s\n", "nihao shijie, from ESP-IDF", esp_get_idf_version());
|
||||
err = s_example_write_file("/spiflash/nihao.txt", data);
|
||||
if (err != ESP_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
//Open file for reading
|
||||
err = s_example_read_file("/spiflash/nihao.txt");
|
||||
if (err != ESP_OK) {
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "Read from file: '%s'", line);
|
||||
|
||||
// Unmount FATFS
|
||||
ESP_LOGI(TAG, "Unmounting FAT filesystem");
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
|
||||
|
||||
@@ -16,7 +16,13 @@ def test_wear_levelling_example(dut: Dut) -> None:
|
||||
'example: Opening file',
|
||||
'example: File written',
|
||||
'example: Reading file',
|
||||
re.compile(str.encode('example: Read from file: \'written using ESP-IDF \\S+\'')),
|
||||
re.compile(str.encode('example: Read from file: \'hello world, from ESP-IDF \\S+\'')),
|
||||
re.compile(str.encode('vfs_fat_spiflash: Formatting FATFS partition, allocation unit size=\\S+')),
|
||||
'example: file doesnt exist, format done',
|
||||
'example: Opening file',
|
||||
'example: File written',
|
||||
'example: Reading file',
|
||||
re.compile(str.encode('example: Read from file: \'nihao shijie, from ESP-IDF \\S+\'')),
|
||||
'example: Unmounting FAT filesystem',
|
||||
'example: Done')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user