How to format sd card?

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

How to format sd card?

Postby zliudr » Wed Feb 10, 2021 5:58 pm

I'm considering formatting the sd card (surface-mount IC) with a command or function, as a maintenance option. I know that you can format the card if it fails to mount but I'm talking about a healthy card that may have developed some file system corruptions and a quick format is necessary for stability. I couldn't find examples of this. Can someone point me in the correct direction? Thanks.

chegewara
Posts: 2240
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to format sd card?

Postby chegewara » Wed Feb 10, 2021 6:12 pm

Code: Select all

esp_err_t formatSD()
{
    FRESULT res = FR_OK;
    esp_err_t err = ESP_OK;
    const size_t workbuf_size = 4096;
    void *workbuf = NULL;
    ESP_LOGW(TAG, "partitioning card");

    workbuf = ff_memalloc(workbuf_size);
    if (workbuf == NULL)
    {
        return ESP_ERR_NO_MEM;
    }

    DWORD plist[] = {100, 0, 0, 0};
    res = f_fdisk(pdrv, plist, workbuf);
    if (res != FR_OK)
    {
        err = ESP_FAIL;
        ESP_LOGE(TAG, "f_fdisk failed (%d)", res);
    }
    else
    {
        size_t alloc_unit_size = 512;
        ESP_LOGW(TAG, "formatting card, allocation unit size=%d", alloc_unit_size);
        res = f_mkfs("", FM_FAT, alloc_unit_size, workbuf, workbuf_size);
        if (res != FR_OK)
        {
            err = ESP_FAIL;
            ESP_LOGE(TAG, "f_mkfs failed (%d)", res);
        }
    }

    ESP_LOGW(TAG, "partitioning card finished");
    free(workbuf);
    return err;
}

zliudr
Posts: 357
Joined: Thu Oct 03, 2019 5:15 am

Re: How to format sd card?

Postby zliudr » Wed Feb 10, 2021 6:46 pm

Thanks. Which header file should I include for ff_memalloc()? I can't find it in ESP-IDF doc so I assume it may be part of POSIX or RTOS call?

chegewara
Posts: 2240
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to format sd card?

Postby chegewara » Wed Feb 10, 2021 6:57 pm

I dont know exactly, but i borrowed this code from espressif. Its one of header files in that file:
https://github.com/espressif/esp-idf/bl ... .c#L15-L21

ThomasVortex
Posts: 2
Joined: Wed Jun 21, 2023 6:42 am

Re: How to format sd card?

Postby ThomasVortex » Wed Jun 21, 2023 6:46 am

chegewara wrote:
Wed Feb 10, 2021 6:12 pm

Code: Select all

esp_err_t formatSD()
{
    FRESULT res = FR_OK;
    esp_err_t err = ESP_OK;
    const size_t workbuf_size = 4096;
    void *workbuf = NULL;
    ESP_LOGW(TAG, "partitioning card");

    workbuf = ff_memalloc(workbuf_size);
    if (workbuf == NULL)
    {
        return ESP_ERR_NO_MEM;
    }

    DWORD plist[] = {100, 0, 0, 0};
    res = f_fdisk(pdrv, plist, workbuf);
    if (res != FR_OK)
    {
        err = ESP_FAIL;
        ESP_LOGE(TAG, "f_fdisk failed (%d)", res);
    }
    else
    {
        size_t alloc_unit_size = 512;
        ESP_LOGW(TAG, "formatting card, allocation unit size=%d", alloc_unit_size);
        res = f_mkfs("", FM_FAT, alloc_unit_size, workbuf, workbuf_size);
        if (res != FR_OK)
        {
            err = ESP_FAIL;
            ESP_LOGE(TAG, "f_mkfs failed (%d)", res);
        }
    }

    ESP_LOGW(TAG, "partitioning card finished");
    free(workbuf);
    return err;
}
This might be a dumb question but how can you find the pdrv of a sd card plugged into the board?

chegewara
Posts: 2240
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to format sd card?

Postby chegewara » Wed Jun 21, 2023 9:12 pm

Hi,
i dont remember, you would have to study esp-idf code, but what i remember is that first pdrv is always 0, so you can assume its 0 if you have 1 sd card.

ThomasVortex
Posts: 2
Joined: Wed Jun 21, 2023 6:42 am

Re: How to format sd card?

Postby ThomasVortex » Mon Jul 03, 2023 6:34 am

I tried to combine this snippet with sdspi example in esp_idf and I got a panic abort during execution. Do you have any idea why it is?
chegewara wrote:
Wed Feb 10, 2021 6:12 pm

Code: Select all

/* Sys Library */
#include <sys/unistd.h>
#include <sys/stat.h>

/* Standard Library */
#include <stdio.h>
#include <inttypes.h>
#include <string.h>

/* Defined Library in ESP-IDF */
#include "ff.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include "vfs_fat_internal.h"
#include "sdkconfig.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_log.h"
#include "diskio_impl.h"

static const char *TAG = "sdFormat";

#define MOUNT_POINT "/sdcard"

// Pin assignments can be set in menuconfig, see "SD SPI Example Configuration" menu.
// You can also change the pin assignments here by changing the following 4 lines.
#define PIN_NUM_MISO  0
#define PIN_NUM_MOSI  4
#define PIN_NUM_CLK   3
#define PIN_NUM_CS    8

esp_err_t formatSD() {
    FRESULT res = FR_OK;
    esp_err_t err = ESP_OK;
    const size_t workbuf_size = 4096;
    void* workbuf = NULL;
    ESP_LOGI(TAG, "partitioning card");

    workbuf = ff_memalloc(workbuf_size);
    ESP_LOGI(TAG, "memalloc completed");
    if (workbuf == NULL)
    {
        ESP_LOGI(TAG, "work buffer null");
        return ESP_ERR_NO_MEM;
    }

//    PARTITION VolToPart[FF_VOLUMES] = {
//            {0, 1},
//            {0, 2}
//    };

    DWORD plist[] = {100    , 0, 0};
    ESP_LOGI(TAG, "Ready to fdisk");
    res = f_fdisk(0, plist, workbuf);
    ESP_LOGI(TAG, "fdisk completed");
    if (res != FR_OK)
    {
        err = ESP_FAIL;
        ESP_LOGE(TAG, "f_fdisk failed (%d)", res);
    }
    else
    {
        ESP_LOGI(TAG, "Ready to mkfs");
        res = f_mkfs("0:", 0, workbuf, workbuf_size);
        if (res != FR_OK)
        {
            err = ESP_FAIL;
            ESP_LOGE(TAG, "f_mkfs failed (%d)", res);
        }

        //res = f_mkfs("1:", 0, workbuf, workbuf_size);
        if (res != FR_OK)
        {
            err = ESP_FAIL;
            ESP_LOGE(TAG, "f_mkfs failed (%d)", res);
        }

    }

    ESP_LOGI(TAG, "partitioning card finished");
    free(workbuf);

    FATFS fs;
    f_mount(&fs, "0:", 0);
    f_mount(&fs, "1:", 0);
    ESP_LOGI(TAG, "mounting drive finished");
    return err;
}

void app_main(void)
{
    esp_err_t ret;

    // Options for mounting the filesystem.
    // If format_if_mount_failed is set to true, SD card will be partitioned and
    // formatted in case when mounting fails.
    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
#ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
        .format_if_mount_failed = true,
#else
        .format_if_mount_failed = false,
#endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
        .max_files = 5,
        .allocation_unit_size = 16 * 1024
    };
    sdmmc_card_t *card;
    const char mount_point[] = MOUNT_POINT;
    ESP_LOGI(TAG, "Initializing SD card");

    // Use settings defined above to initialize SD card and mount FAT filesystem.
    // Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
    // Please check its source code and implement error recovery when developing
    // production applications.
    ESP_LOGI(TAG, "Using SPI peripheral");

    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
    host.max_freq_khz = 10000;

    spi_bus_config_t bus_cfg = {
        .mosi_io_num = PIN_NUM_MOSI,
        .miso_io_num = PIN_NUM_MISO,
        .sclk_io_num = PIN_NUM_CLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 4000,
    };
    ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to initialize bus.");
        return;
    }

    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
    sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
    slot_config.gpio_cs = PIN_NUM_CS;
    slot_config.host_id = host.slot;

    ESP_LOGI(TAG, "Mounting filesystem");
    ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
//    ret = initialize_card(mount_point, &host, &slot_config, &card);
    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount filesystem. "
                     "If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
        } else {
            ESP_LOGE(TAG, "Failed to initialize the card (%s). "
                     "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
        }
        return;
    }
    ESP_LOGI(TAG, "Filesystem mounted");

    formatSD();

    // All done, unmount partition and disable SPI peripheral
//    esp_vfs_fat_sdcard_unmount(mount_point, card);
//    ESP_LOGI(TAG, "Card unmounted");

    //deinitialize the bus after all devices are removed
    spi_bus_free(host.slot);
}


Who is online

Users browsing this forum: tomy983 and 254 guests