fatfs: added an API to format FAT on sdcard

This commit is contained in:
Armando 2023-01-04 17:33:00 +08:00
parent 4150bfb403
commit 90a6018e67
8 changed files with 476 additions and 155 deletions

View File

@ -51,8 +51,9 @@
// No runner
#include "driver/sdmmc_host.h"
static void test_setup_sdmmc(void)
static void test_setup_sdmmc(sdmmc_card_t **out_card)
{
sdmmc_card_t *card = NULL;
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
@ -60,12 +61,13 @@ static void test_setup_sdmmc(void)
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card));
*out_card = card;
}
static void test_teardown_sdmmc(void)
static void test_teardown_sdmmc(sdmmc_card_t *card)
{
TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount("/sdcard", card));
}
static const char* test_filename = "/sdcard/hello.txt";
@ -90,118 +92,144 @@ TEST_CASE("Mount fails cleanly without card inserted", "[fatfs][ignore]")
HEAP_SIZE_CHECK(heap_size, 0);
}
TEST_CASE("(SD) can format partition", "[fatfs][sdmmc]")
{
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
TEST_ESP_OK(esp_vfs_fat_sdcard_format("/sdcard", card));
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
test_fatfs_read_file(test_filename);
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) can create and write file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) can read file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
test_fatfs_read_file(test_filename);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) can read file with pread()", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
test_fatfs_pread_file(test_filename);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) pwrite() works well", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_pwrite_file(test_filename);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) overwrite and append file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_overwrite_append(test_filename);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) can lseek", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_lseek("/sdcard/seek.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) can truncate", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_truncate_file("/sdcard/truncate.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) can ftruncate", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_ftruncate_file("/sdcard/ftrunc.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) stat returns correct values", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_stat("/sdcard/stat.txt", "/sdcard");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) utime sets modification time", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_utime("/sdcard/utime.txt", "/sdcard");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) unlink removes a file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_unlink("/sdcard/unlink.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) link copies a file, rename moves a file", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_link_rename("/sdcard/link");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) can create and remove directories", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_mkdir_rmdir("/sdcard/dir");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) can opendir root directory of FS", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_can_opendir("/sdcard");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_opendir_readdir_rewinddir("/sdcard/dir");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_concurrent("/sdcard/f");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool write);
@ -231,6 +259,7 @@ TEST_CASE("(SD) write/read speed test", "[fatfs][sdmmc]")
static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool write)
{
sdmmc_card_t *card = NULL;
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
@ -239,11 +268,11 @@ static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool
.max_files = 5,
.allocation_unit_size = 64 * 1024
};
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card));
test_fatfs_rw_speed("/sdcard/4mb.bin", buf, buf_size, file_size, write);
TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount("/sdcard", card));
}
TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fatfs][sdmmc]")
@ -265,17 +294,18 @@ TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fat
/* Mount FATFS in SD can WL at the same time. Create a file on each FS */
wl_handle_t wl_handle = WL_INVALID_HANDLE;
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &wl_handle));
unlink(filename_sd);
unlink(filename_wl);
test_fatfs_create_file_with_text(filename_sd, str_sd);
test_fatfs_create_file_with_text(filename_wl, str_wl);
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", wl_handle));
test_teardown_sdmmc();
test_teardown_sdmmc(card);
/* Check that the file "sd.txt" was created on FS in SD, and has the right data */
test_setup_sdmmc();
test_setup_sdmmc(&card);
TEST_ASSERT_NULL(fopen(filename_wl, "r"));
FILE* f = fopen(filename_sd, "r");
TEST_ASSERT_NOT_NULL(f);
@ -283,7 +313,7 @@ TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fat
TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
TEST_ASSERT_EQUAL(0, strcmp(buf, str_sd));
fclose(f);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
/* Check that the file "wl.txt" was created on FS in WL, and has the right data */
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &wl_handle));
@ -307,25 +337,28 @@ static const char* test_filename_utf_8 = "/sdcard/测试文件.txt";
TEST_CASE("(SD) can read file using UTF-8 encoded strings", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_create_file_with_text(test_filename_utf_8, fatfs_test_hello_str_utf);
test_fatfs_read_file_utf_8(test_filename_utf_8);
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected using UTF-8 encoded strings", "[fatfs][ignore]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_opendir_readdir_rewinddir_utf_8("/sdcard/目录");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
#endif // CONFIG_FATFS_API_ENCODING_UTF_8 && CONFIG_FATFS_CODEPAGE == 936
TEST_CASE("(SD) can get partition info", "[fatfs][sdmmc]")
{
test_setup_sdmmc();
sdmmc_card_t *card = NULL;
test_setup_sdmmc(&card);
test_fatfs_info("/sdcard", "/sdcard/test.txt");
test_teardown_sdmmc();
test_teardown_sdmmc(card);
}
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)

View File

@ -53,6 +53,7 @@ typedef struct sdspi_mem {
uint32_t* buf;
} sdspi_mem_t;
static const char* s_test_filename = "/sdcard/hello.txt";
static void sdspi_speed_test(void *buf, size_t buf_size, size_t file_size, bool write);
static void test_setup_sdspi(sdspi_mem_t* mem)
@ -159,3 +160,34 @@ TEST_CASE("(SDSPI) can get partition info", "[fatfs][sdspi]")
test_teardown_sdspi(&mem);
}
TEST_CASE("(SDSPI) can format card", "[fatfs][sdspi]")
{
sdspi_mem_t mem;
test_setup_sdspi(&mem);
const char path[] = "/sdcard";
sdmmc_card_t *card;
card = NULL;
sdspi_device_config_t device_cfg = {
.gpio_cs = SDSPI_CS_PIN,
.host_id = SDSPI_HOST_ID,
.gpio_cd = SDSPI_SLOT_NO_CD,
.gpio_wp = SDSPI_SLOT_NO_WP,
.gpio_int = SDSPI_SLOT_NO_INT,
};
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.slot = SDSPI_HOST_ID;
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5,
.allocation_unit_size = 64 * 1024
};
TEST_ESP_OK(esp_vfs_fat_sdspi_mount(path, &host, &device_cfg, &mount_config, &card));
TEST_ESP_OK(esp_vfs_fat_sdcard_format("/sdcard", card));
test_fatfs_create_file_with_text(s_test_filename, fatfs_test_hello_str);
test_fatfs_read_file(s_test_filename);
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount(path, card));
test_teardown_sdspi(&mem);
}

View File

@ -207,6 +207,23 @@ esp_err_t esp_vfs_fat_sdmmc_unmount(void);
*/
esp_err_t esp_vfs_fat_sdcard_unmount(const char* base_path, sdmmc_card_t *card);
/**
* @brief Format FAT filesystem
*
* @note
* This API should be only called when the FAT is already mounted.
*
* @param base_path Path where partition should be registered (e.g. "/sdcard")
* @param card Pointer to the card handle, which should be initialised by calling `esp_vfs_fat_sdspi_mount` first
*
* @return
* - ESP_OK
* - ESP_ERR_INVALID_STATE: FAT partition isn't mounted, call esp_vfs_fat_sdmmc_mount or esp_vfs_fat_sdspi_mount first
* - ESP_ERR_NO_MEM: if memory can not be allocated
* - ESP_FAIL: fail to format it, or fail to mount back
*/
esp_err_t esp_vfs_fat_sdcard_format(const char *base_path, sdmmc_card_t *card);
/**
* @brief Convenience function to initialize FAT filesystem in SPI flash and register it in VFS
*

View File

@ -22,9 +22,6 @@
#endif
static const char* TAG = "vfs_fat_sdmmc";
static sdmmc_card_t* s_card = NULL;
static uint8_t s_pdrv = FF_DRV_NOT_USED;
static char * s_base_path = NULL;
#define CHECK_EXECUTE_RESULT(err, str) do { \
if ((err) !=ESP_OK) { \
@ -33,10 +30,50 @@ static char * s_base_path = NULL;
} \
} while(0)
typedef struct vfs_fat_sd_ctx_t {
BYTE pdrv; //Drive number that is mounted
esp_vfs_fat_mount_config_t mount_config; //Mount configuration
FATFS *fs; //FAT structure pointer that is registered
sdmmc_card_t *card; //Card info
char *base_path; //Path where partition is registered
} vfs_fat_sd_ctx_t;
static vfs_fat_sd_ctx_t *s_ctx[FF_VOLUMES] = {};
/**
* This `s_saved_ctx_id` is only used by `esp_vfs_fat_sdmmc_unmount`, which is deprecated.
* This variable together with `esp_vfs_fat_sdmmc_unmount` should be removed in next major version
*/
static uint32_t s_saved_ctx_id = FF_VOLUMES;
static void call_host_deinit(const sdmmc_host_t *host_config);
static esp_err_t partition_card(const esp_vfs_fat_mount_config_t *mount_config,
const char *drv, sdmmc_card_t *card, BYTE pdrv);
static bool s_get_context_id_by_card(const sdmmc_card_t *card, uint32_t *out_id)
{
vfs_fat_sd_ctx_t *p_ctx = NULL;
for (int i = 0; i < FF_VOLUMES; i++) {
p_ctx = s_ctx[i];
if (p_ctx) {
if (p_ctx->card == card) {
*out_id = i;
return true;
}
}
}
return false;
}
static uint32_t s_get_unused_context_id(void)
{
for (uint32_t i = 0; i < FF_VOLUMES; i++) {
if (!s_ctx[i]) {
return i;
}
}
return FF_VOLUMES;
}
static esp_err_t mount_prepare_mem(const char *base_path,
BYTE *out_pdrv,
char **out_dup_path,
@ -79,10 +116,40 @@ cleanup:
return err;
}
static esp_err_t mount_to_vfs_fat(const esp_vfs_fat_mount_config_t *mount_config, sdmmc_card_t *card, uint8_t pdrv,
const char *base_path)
static esp_err_t s_f_mount(sdmmc_card_t *card, FATFS *fs, const char *drv, uint8_t pdrv, const esp_vfs_fat_mount_config_t *mount_config)
{
FATFS* fs = NULL;
esp_err_t err = ESP_OK;
FRESULT res = f_mount(fs, drv, 1);
if (res != FR_OK) {
err = ESP_FAIL;
ESP_LOGW(TAG, "failed to mount card (%d)", res);
bool need_mount_again = (res == FR_NO_FILESYSTEM || res == FR_INT_ERR) && mount_config->format_if_mount_failed;
if (!need_mount_again) {
return ESP_FAIL;
}
err = partition_card(mount_config, drv, card, pdrv);
if (err != ESP_OK) {
return err;
}
ESP_LOGW(TAG, "mounting again");
res = f_mount(fs, drv, 0);
if (res != FR_OK) {
err = ESP_FAIL;
ESP_LOGD(TAG, "f_mount failed after formatting (%d)", res);
return err;
}
}
return ESP_OK;
}
static esp_err_t mount_to_vfs_fat(const esp_vfs_fat_mount_config_t *mount_config, sdmmc_card_t *card, uint8_t pdrv,
const char *base_path, FATFS **out_fs)
{
FATFS *fs = NULL;
esp_err_t err;
ff_diskio_register_sdmmc(pdrv, card);
ff_sdmmc_set_disk_status_check(pdrv, mount_config->disk_status_check_enable);
@ -91,6 +158,7 @@ static esp_err_t mount_to_vfs_fat(const esp_vfs_fat_mount_config_t *mount_config
// connect FATFS to VFS
err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
*out_fs = fs;
if (err == ESP_ERR_INVALID_STATE) {
// it's okay, already registered with VFS
} else if (err != ESP_OK) {
@ -99,27 +167,9 @@ static esp_err_t mount_to_vfs_fat(const esp_vfs_fat_mount_config_t *mount_config
}
// Try to mount partition
FRESULT res = f_mount(fs, drv, 1);
if (res != FR_OK) {
err = ESP_FAIL;
ESP_LOGW(TAG, "failed to mount card (%d)", res);
if (!((res == FR_NO_FILESYSTEM || res == FR_INT_ERR)
&& mount_config->format_if_mount_failed)) {
goto fail;
}
err = partition_card(mount_config, drv, card, pdrv);
if (err != ESP_OK) {
goto fail;
}
ESP_LOGW(TAG, "mounting again");
res = f_mount(fs, drv, 0);
if (res != FR_OK) {
err = ESP_FAIL;
ESP_LOGD(TAG, "f_mount failed after formatting (%d)", res);
goto fail;
}
err = s_f_mount(card, fs, drv, pdrv, mount_config);
if (err != ESP_OK) {
goto fail;
}
return ESP_OK;
@ -187,6 +237,9 @@ esp_err_t esp_vfs_fat_sdmmc_mount(const char* base_path,
sdmmc_card_t** out_card)
{
esp_err_t err;
vfs_fat_sd_ctx_t *ctx = NULL;
uint32_t ctx_id = FF_VOLUMES;
FATFS *fs = NULL;
int card_handle = -1; //uninitialized
sdmmc_card_t* card = NULL;
BYTE pdrv = FF_DRV_NOT_USED;
@ -212,20 +265,30 @@ esp_err_t esp_vfs_fat_sdmmc_mount(const char* base_path,
err = sdmmc_card_init(host_config, card);
CHECK_EXECUTE_RESULT(err, "sdmmc_card_init failed");
err = mount_to_vfs_fat(mount_config, card, pdrv, dup_path);
err = mount_to_vfs_fat(mount_config, card, pdrv, dup_path, &fs);
CHECK_EXECUTE_RESULT(err, "mount_to_vfs failed");
if (out_card != NULL) {
*out_card = card;
}
if (s_card == NULL) {
//store the ctx locally to be back-compatible
s_card = card;
s_pdrv = pdrv;
s_base_path = dup_path;
} else {
free(dup_path);
//For deprecation backward compatibility
if (s_saved_ctx_id == FF_VOLUMES) {
s_saved_ctx_id = 0;
}
ctx = calloc(sizeof(vfs_fat_sd_ctx_t), 1);
if (!ctx) {
CHECK_EXECUTE_RESULT(ESP_ERR_NO_MEM, "no mem");
}
ctx->pdrv = pdrv;
memcpy(&ctx->mount_config, mount_config, sizeof(esp_vfs_fat_mount_config_t));
ctx->card = card;
ctx->base_path = dup_path;
ctx->fs = fs;
ctx_id = s_get_unused_context_id();
assert(ctx_id != FF_VOLUMES);
s_ctx[ctx_id] = ctx;
return ESP_OK;
cleanup:
if (host_inited) {
@ -233,6 +296,10 @@ cleanup:
}
free(card);
free(dup_path);
if (ctx) {
free(ctx);
s_ctx[ctx_id] = NULL;
}
return err;
}
#endif
@ -257,6 +324,9 @@ esp_err_t esp_vfs_fat_sdspi_mount(const char* base_path,
{
const sdmmc_host_t* host_config = host_config_input;
esp_err_t err;
vfs_fat_sd_ctx_t *ctx = NULL;
uint32_t ctx_id = FF_VOLUMES;
FATFS *fs = NULL;
int card_handle = -1; //uninitialized
bool host_inited = false;
BYTE pdrv = FF_DRV_NOT_USED;
@ -294,20 +364,30 @@ esp_err_t esp_vfs_fat_sdspi_mount(const char* base_path,
err = sdmmc_card_init(host_config, card);
CHECK_EXECUTE_RESULT(err, "sdmmc_card_init failed");
err = mount_to_vfs_fat(mount_config, card, pdrv, dup_path);
err = mount_to_vfs_fat(mount_config, card, pdrv, dup_path, &fs);
CHECK_EXECUTE_RESULT(err, "mount_to_vfs failed");
if (out_card != NULL) {
*out_card = card;
}
if (s_card == NULL) {
//store the ctx locally to be back-compatible
s_card = card;
s_pdrv = pdrv;
s_base_path = dup_path;
} else {
free(dup_path);
//For deprecation backward compatibility
if (s_saved_ctx_id == FF_VOLUMES) {
s_saved_ctx_id = 0;
}
ctx = calloc(sizeof(vfs_fat_sd_ctx_t), 1);
if (!ctx) {
CHECK_EXECUTE_RESULT(ESP_ERR_NO_MEM, "no mem");
}
ctx->pdrv = pdrv;
memcpy(&ctx->mount_config, mount_config, sizeof(esp_vfs_fat_mount_config_t));
ctx->card = card;
ctx->base_path = dup_path;
ctx->fs = fs;
ctx_id = s_get_unused_context_id();
assert(ctx_id != FF_VOLUMES);
s_ctx[ctx_id] = ctx;
return ESP_OK;
cleanup:
@ -316,18 +396,14 @@ cleanup:
}
free(card);
free(dup_path);
if (ctx) {
free(ctx);
s_ctx[ctx_id] = NULL;
}
return err;
}
static void local_card_remove(void)
{
s_card = NULL;
free(s_base_path);
s_base_path = NULL;
s_pdrv = FF_DRV_NOT_USED;
}
static void call_host_deinit(const sdmmc_host_t *host_config)
{
if (host_config->flags & SDMMC_HOST_FLAG_DEINIT_ARG) {
@ -359,17 +435,74 @@ static esp_err_t unmount_card_core(const char *base_path, sdmmc_card_t *card)
esp_err_t esp_vfs_fat_sdmmc_unmount(void)
{
sdmmc_card_t* card = s_card;
esp_err_t err = unmount_card_core(s_base_path, card);
local_card_remove();
esp_err_t err = unmount_card_core(s_ctx[s_saved_ctx_id]->base_path, s_ctx[s_saved_ctx_id]->card);
free(s_ctx[s_saved_ctx_id]);
s_ctx[s_saved_ctx_id] = NULL;
s_saved_ctx_id = FF_VOLUMES;
return err;
}
esp_err_t esp_vfs_fat_sdcard_unmount(const char *base_path, sdmmc_card_t *card)
{
esp_err_t err = unmount_card_core(base_path, card);
if (s_card == card) {
local_card_remove();
uint32_t id = FF_VOLUMES;
bool found = s_get_context_id_by_card(card, &id);
if (!found) {
return ESP_ERR_INVALID_ARG;
}
free(s_ctx[id]);
s_ctx[id] = NULL;
esp_err_t err = unmount_card_core(base_path, card);
return err;
}
esp_err_t esp_vfs_fat_sdcard_format(const char *base_path, sdmmc_card_t *card)
{
esp_err_t ret = ESP_OK;
if (!card) {
ESP_LOGE(TAG, "card not initialized");
return ESP_ERR_INVALID_STATE;
}
BYTE pdrv = ff_diskio_get_pdrv_card(card);
if (pdrv == 0xff) {
ESP_LOGE(TAG, "card driver not registered");
return ESP_ERR_INVALID_STATE;
}
const size_t workbuf_size = 4096;
void *workbuf = ff_memalloc(workbuf_size);
if (workbuf == NULL) {
return ESP_ERR_NO_MEM;
}
//unmount
char drv[3] = {(char)('0' + pdrv), ':', 0};
f_mount(0, drv, 0);
//format
uint32_t id = FF_VOLUMES;
bool found = s_get_context_id_by_card(card, &id);
assert(found);
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(
card->csd.sector_size,
s_ctx[id]->mount_config.allocation_unit_size);
ESP_LOGI(TAG, "Formatting card, allocation unit size=%d", alloc_unit_size);
const MKFS_PARM opt = {(BYTE)FM_ANY, 0, 0, 0, alloc_unit_size};
FRESULT res = f_mkfs(drv, &opt, workbuf, workbuf_size);
free(workbuf);
if (res != FR_OK) {
ret = ESP_FAIL;
ESP_LOGD(TAG, "f_mkfs failed (%d)", res);
}
//mount back
esp_err_t err = s_f_mount(card, s_ctx[id]->fs, drv, pdrv, &s_ctx[id]->mount_config);
if (err != ESP_OK) {
unmount_card_core(base_path, card);
ESP_LOGE(TAG, "failed to format, resources recycled, please mount again");
}
return ret;
}

View File

@ -15,11 +15,50 @@
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"
#define EXAMPLE_MAX_CHAR_SIZE 64
static const char *TAG = "example";
#define MOUNT_POINT "/sdcard"
static esp_err_t s_example_write_file(const char *path, char *data)
{
ESP_LOGI(TAG, "Opening file %s", path);
FILE *f = fopen(path, "w");
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(const char *path)
{
ESP_LOGI(TAG, "Reading file %s", path);
FILE *f = fopen(path, "r");
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_err_t ret;
@ -103,19 +142,14 @@ void app_main(void)
// First create a file.
const char *file_hello = MOUNT_POINT"/hello.txt";
ESP_LOGI(TAG, "Opening file %s", file_hello);
FILE *f = fopen(file_hello, "w");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for writing");
char data[EXAMPLE_MAX_CHAR_SIZE];
snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s!\n", "Hello", card->cid.name);
ret = s_example_write_file(file_hello, data);
if (ret != ESP_OK) {
return;
}
fprintf(f, "Hello %s!\n", card->cid.name);
fclose(f);
ESP_LOGI(TAG, "File written");
const char *file_foo = MOUNT_POINT"/foo.txt";
// Check if destination file exists before renaming
struct stat st;
if (stat(file_foo, &st) == 0) {
@ -130,25 +164,38 @@ void app_main(void)
return;
}
// Open renamed file for reading
ESP_LOGI(TAG, "Reading file %s", file_foo);
f = fopen(file_foo, "r");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for reading");
ret = s_example_read_file(file_foo);
if (ret != ESP_OK) {
return;
}
// Read a line from file
char line[64];
fgets(line, sizeof(line), f);
fclose(f);
// Strip newline
char *pos = strchr(line, '\n');
if (pos) {
*pos = '\0';
// Format FATFS
ret = esp_vfs_fat_sdcard_format(mount_point, card);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(ret));
return;
}
if (stat(file_foo, &st) == 0) {
ESP_LOGI(TAG, "file still exists");
return;
} else {
ESP_LOGI(TAG, "file doesnt exist, format done");
}
const char *file_nihao = MOUNT_POINT"/nihao.txt";
memset(data, 0, EXAMPLE_MAX_CHAR_SIZE);
snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s!\n", "Nihao", card->cid.name);
ret = s_example_write_file(file_nihao, data);
if (ret != ESP_OK) {
return;
}
//Open file for reading
ret = s_example_read_file(file_nihao);
if (ret != ESP_OK) {
return;
}
ESP_LOGI(TAG, "Read from file: '%s'", line);
// All done, unmount partition and disable SDMMC peripheral
esp_vfs_fat_sdcard_unmount(mount_point, card);

View File

@ -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
@ -31,7 +31,13 @@ def test_examples_sd_card_sdmmc(dut: Dut) -> None:
'Renaming file /sdcard/hello.txt to /sdcard/foo.txt',
'Reading file /sdcard/foo.txt',
"Read from file: 'Hello {}!'".format(name),
re.compile(str.encode('Formatting card, allocation unit size=\\S+')),
'file doesnt exist, format done',
'Opening file /sdcard/nihao.txt',
'File written',
'Reading file /sdcard/nihao.txt',
"Read from file: 'Nihao {}!'".format(name),
'Card unmounted')
for msg in message_list:
dut.expect(msg, timeout=10)
dut.expect(msg, timeout=20)

View File

@ -14,6 +14,8 @@
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define EXAMPLE_MAX_CHAR_SIZE 64
static const char *TAG = "example";
#define MOUNT_POINT "/sdcard"
@ -25,6 +27,42 @@ static const char *TAG = "example";
#define PIN_NUM_CLK CONFIG_EXAMPLE_PIN_CLK
#define PIN_NUM_CS CONFIG_EXAMPLE_PIN_CS
static esp_err_t s_example_write_file(const char *path, char *data)
{
ESP_LOGI(TAG, "Opening file %s", path);
FILE *f = fopen(path, "w");
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(const char *path)
{
ESP_LOGI(TAG, "Reading file %s", path);
FILE *f = fopen(path, "r");
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)
{
@ -99,16 +137,12 @@ void app_main(void)
// First create a file.
const char *file_hello = MOUNT_POINT"/hello.txt";
ESP_LOGI(TAG, "Opening file %s", file_hello);
FILE *f = fopen(file_hello, "w");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for writing");
char data[EXAMPLE_MAX_CHAR_SIZE];
snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s!\n", "Hello", card->cid.name);
ret = s_example_write_file(file_hello, data);
if (ret != ESP_OK) {
return;
}
fprintf(f, "Hello %s!\n", card->cid.name);
fclose(f);
ESP_LOGI(TAG, "File written");
const char *file_foo = MOUNT_POINT"/foo.txt";
@ -126,25 +160,38 @@ void app_main(void)
return;
}
// Open renamed file for reading
ESP_LOGI(TAG, "Reading file %s", file_foo);
f = fopen(file_foo, "r");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for reading");
ret = s_example_read_file(file_foo);
if (ret != ESP_OK) {
return;
}
// Read a line from file
char line[64];
fgets(line, sizeof(line), f);
fclose(f);
// Strip newline
char *pos = strchr(line, '\n');
if (pos) {
*pos = '\0';
// Format FATFS
ret = esp_vfs_fat_sdcard_format(mount_point, card);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(ret));
return;
}
if (stat(file_foo, &st) == 0) {
ESP_LOGI(TAG, "file still exists");
return;
} else {
ESP_LOGI(TAG, "file doesnt exist, format done");
}
const char *file_nihao = MOUNT_POINT"/nihao.txt";
memset(data, 0, EXAMPLE_MAX_CHAR_SIZE);
snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s!\n", "Nihao", card->cid.name);
ret = s_example_write_file(file_nihao, data);
if (ret != ESP_OK) {
return;
}
//Open file for reading
ret = s_example_read_file(file_nihao);
if (ret != ESP_OK) {
return;
}
ESP_LOGI(TAG, "Read from file: '%s'", line);
// All done, unmount partition and disable SPI peripheral
esp_vfs_fat_sdcard_unmount(mount_point, card);

View File

@ -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
@ -33,6 +33,12 @@ def test_examples_sd_card_sdspi(dut: Dut) -> None:
'Renaming file /sdcard/hello.txt to /sdcard/foo.txt',
'Reading file /sdcard/foo.txt',
"Read from file: 'Hello {}!'".format(name),
re.compile(str.encode('Formatting card, allocation unit size=\\S+')),
'file doesnt exist, format done',
'Opening file /sdcard/nihao.txt',
'File written',
'Reading file /sdcard/nihao.txt',
"Read from file: 'Nihao {}!'".format(name),
'Card unmounted')
for msg in message_list: