Skip to content

Commit a22f95c

Browse files
committed
Update MMC_SD support format sd card API
1 parent 4819ba8 commit a22f95c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

libraries/SD_MMC/src/SD_MMC.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern "C" {
2424
#include "sdmmc_cmd.h"
2525
}
2626
#include "ff.h"
27+
#include "vfs_fat_internal.h"
2728
#include "SD_MMC.h"
2829

2930
using namespace fs;
@@ -91,6 +92,37 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
9192
return true;
9293
}
9394

95+
esp_err_t SDMMCFS::format() {
96+
char drv[3] = {'0', ':', 0};
97+
const size_t workBuff_size = 4096;
98+
void* workBuff = NULL;
99+
esp_err_t err = ESP_OK;
100+
ESP_LOGW("sdcard", "Formatting the SD card");
101+
102+
size_t allocation_unit_size = 16 * 1024;
103+
104+
workBuff = ff_memalloc(workBuff_size);
105+
if (workBuff == NULL) {
106+
return ESP_ERR_NO_MEM;
107+
}
108+
109+
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(
110+
_card->csd.sector_size,
111+
allocation_unit_size);
112+
113+
FRESULT res = f_mkfs(drv, FM_ANY, alloc_unit_size, workBuff, workBuff_size);
114+
if (res != FR_OK) {
115+
err = ESP_FAIL;
116+
ESP_LOGE("sdcard", "f_mkfs failed (%d)", res);
117+
}
118+
119+
free(workBuff);
120+
121+
ESP_LOGI("sdcard", "Successfully formatted the SD card");
122+
123+
return err;
124+
}
125+
94126
void SDMMCFS::end()
95127
{
96128
if(_card) {

libraries/SD_MMC/src/SD_MMC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class SDMMCFS : public FS
3434
uint64_t cardSize();
3535
uint64_t totalBytes();
3636
uint64_t usedBytes();
37+
esp_err_t format();
3738
};
3839

3940
}

0 commit comments

Comments
 (0)