FATFS format

User avatar
brp80000
Posts: 138
Joined: Thu Oct 04, 2018 7:13 pm

FATFS format

Postby brp80000 » Tue Dec 04, 2018 4:31 pm

I want to add an option for the user that will allow fatfs format to remove all files. But I can't find the command to format in IDF

Reiner1210
Posts: 39
Joined: Tue Mar 20, 2018 6:28 pm

Re: FATFS format

Postby Reiner1210 » Tue Dec 04, 2018 11:18 pm

Details:
http://elm-chan.org/fsw/ff/00index_e.html

I have done it a long time ago .... the core is this
  1. typedef struct
  2. {
  3.     uint8_t pdrv;
  4.     sdmmc_card_t card;
  5.     char* base;
  6. } sd_data_t;
  7.  
  8. typedef struct
  9. {
  10.     size_t unit_size;
  11. } sd_format_data_t;
  12.  
  13. static bool sd_format(const sd_data_t* sd_data, const sd_format_data_t* format_data)
  14. {
  15.     ESP_LOGI(log_tag, "Format SD card %u", sd_data->pdrv);
  16.     bool flag = false;
  17.     DWORD plist[] = {100, 0, 0, 0};
  18.     FRESULT res;
  19.     size_t buffer_size = FF_MAX_SS;
  20.     char* buffer = malloc(buffer_size);
  21.     if ((res = f_fdisk(sd_data->pdrv, plist, (void*)buffer)) == FR_OK)
  22.     {
  23.         char drv[3] = {(char)('0' + sd_data->pdrv), ':', '\0'};
  24.         // printf("Format SD card 2\n");
  25.         if ((res = f_mkfs(drv, FM_FAT32, esp_vfs_fat_get_allocation_unit_size(sd_data->card.csd.sector_size, format_data->unit_size), (void*)buffer, buffer_size)) == FR_OK)
  26.             flag = true;
  27.         else
  28.             {ESP_LOGE(log_tag, "f_mkfs failed with %d", res);}
  29.     }
  30.     else
  31.         {ESP_LOGE(log_tag, "f_fdisk failed with %d", res);}
  32.     free(buffer);
  33.     ESP_LOGI(log_tag, "Format of SD card %u finished", sd_data->pdrv);
  34.     return flag;
  35. }
  36. static bool do_sd_init(const char* base, const sdmmc_host_t* host, const sdspi_slot_config_t* slot, sd_data_t* sd_data, const sd_format_data_t* format_data)
  37. {
  38.     bool flag = false;
  39.     sd_data->pdrv = 0xFF;
  40.     esp_err_t err;
  41.     bool host_init = false;
  42.     bool vfs_register = false;
  43.     bool diskio_register = false;
  44.     FATFS* fs = NULL;
  45.     char drv[3];
  46.     sd_data->base = strdup(base);
  47.    
  48.     // connect SDMMC driver to FATFS
  49.     if ((err = ff_diskio_get_drive(&sd_data->pdrv)) == ESP_OK && sd_data->pdrv != 0xFF)
  50.     {
  51.         drv[0] = (char)('0' + sd_data->pdrv);
  52.         drv[1] = ':';
  53.         drv[2] = '\0';
  54.         if ((err = (*host->init)()) == ESP_OK)
  55.         {
  56.             host_init = true;
  57.             // configure SD slot
  58.             if ((err = sdspi_host_init_slot(host->slot, slot)) == ESP_OK)
  59.             {
  60.                 // probe and initialize card
  61.                 if ((err = sdmmc_card_init(host, &sd_data->card) == ESP_OK))
  62.                 {
  63.                     diskio_register = true;
  64.                     ff_diskio_register_sdmmc(sd_data->pdrv, &sd_data->card);
  65.  
  66.                     // connect FATFS to VFS
  67.                     if ((err = esp_vfs_fat_register(base, drv, 16, &fs)) == ESP_OK)
  68.                     {
  69.                         vfs_register = true;
  70.                         if (!format_data || sd_format(sd_data, format_data))
  71.                         {
  72.                             FRESULT res;
  73.                             if ((res = f_mount(fs, drv, 1)) == FR_OK)
  74.                             {
  75.                                 flag = true;
  76.                                 ESP_LOGI(log_tag, "SD card %u mounted", sd_data->pdrv);
  77.                             }
  78.                             else
  79.                                 {ESP_LOGE(log_tag, "f_mount failed with %d", res);}
  80.                         }
  81.                         else
  82.                             {ESP_LOGE(log_tag, "format_sd failed");}
  83.                     }
  84.                     else
  85.                         {ESP_LOGE(log_tag, "esp_vfs_fat_register failed with '%s (%d)'", esp_err_to_name(err), err);}
  86.                 }
  87.                 else
  88.                     ESP_LOGE(log_tag, "sdmmc_card_init failed with '%s (%d)'", esp_err_to_name(err), err);
  89.             }
  90.             else
  91.                 {ESP_LOGE(log_tag, "sdspi_host_init_slot failed with '%s (%d)'", esp_err_to_name(err), err);}
  92.         }
  93.         else
  94.             {ESP_LOGE(log_tag, "host->init() failed with '%s (%d)'", esp_err_to_name(err), err);}
  95.     }
  96.     else
  97.         {ESP_LOGE(log_tag, "ff_diskio_get_drive failed with '%s (%d)'", esp_err_to_name(err), err);}
  98.    
  99.     if (!flag)
  100.     {
  101.         if (host_init)
  102.             host->deinit();
  103.         if (fs)
  104.             f_mount(NULL, drv, 0);
  105.         if (vfs_register)
  106.             esp_vfs_fat_unregister_path(base);
  107.         if (diskio_register)
  108.             ff_diskio_unregister(sd_data->pdrv);
  109.     }
  110.     is_init = flag;
  111.     return flag;
  112. }
  113.  
  114. bool sd_init(const char* base, const sdmmc_host_t* host, const sdspi_slot_config_t* slot, sd_data_t* sd_data)
  115. {
  116.     return do_sd_init(base, host, slot, sd_data, NULL);
  117. }
  118.  
  119. bool sd_init_and_format(const char* base, const sdmmc_host_t* host, const sdspi_slot_config_t* slot, sd_data_t* sd_data, const sd_format_data_t* format_data)
  120. {
  121.     return do_sd_init(base, host, slot, sd_data, format_data);
  122. }
  123.  

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 128 guests