From 964592e1896b75aa32f85c27a152d5da0b03ac8e Mon Sep 17 00:00:00 2001 From: Vamshi Gajjela Date: Sat, 5 Mar 2022 16:35:37 +0530 Subject: [PATCH] fatfs: Add trim ioctl call, set FF_USE_TRIM as default option. FF_USE_TRIM is set by default with this commit. Fatfs invokes disk_ioctl with CTRL_TRIM to erase the sectors calling sdmmc_erase_sectors to choose the right argument for the erase operation based on media type. --- components/fatfs/diskio/diskio_sdmmc.c | 29 ++++++++++++++++++++++++++ components/fatfs/src/ffconf.h | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/fatfs/diskio/diskio_sdmmc.c b/components/fatfs/diskio/diskio_sdmmc.c index 079129215e..19f6d5698c 100644 --- a/components/fatfs/diskio/diskio_sdmmc.c +++ b/components/fatfs/diskio/diskio_sdmmc.c @@ -67,6 +67,23 @@ DRESULT ff_sdmmc_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) return RES_OK; } +#if FF_USE_TRIM +DRESULT ff_sdmmc_trim (BYTE pdrv, DWORD start_sector, DWORD sector_count) +{ + sdmmc_card_t* card = s_cards[pdrv]; + assert(card); + sdmmc_erase_arg_t arg; + + arg = sdmmc_can_discard(card) == ESP_OK ? SDMMC_DISCARD_ARG : SDMMC_ERASE_ARG; + esp_err_t err = sdmmc_erase_sectors(card, start_sector, sector_count, arg); + if (unlikely(err != ESP_OK)) { + ESP_LOGE(TAG, "sdmmc_erase_sectors failed (%d)", err); + return RES_ERROR; + } + return RES_OK; +} +#endif //FF_USE_TRIM + DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff) { sdmmc_card_t* card = s_cards[pdrv]; @@ -82,6 +99,18 @@ DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff) return RES_OK; case GET_BLOCK_SIZE: return RES_ERROR; +#if FF_USE_TRIM + case CTRL_TRIM: + /* + * limitation with sector erase when used in SDSPI mode + * hence return if host is SPI. + */ + if ((card->host.flags & SDMMC_HOST_FLAG_SPI) != 0) { + return RES_ERROR; + } + return ff_sdmmc_trim (pdrv, *((DWORD*)buff), //start_sector + (*((DWORD*)buff + 1) - *((DWORD*)buff) + 1)); //sector_count +#endif //FF_USE_TRIM } return RES_ERROR; } diff --git a/components/fatfs/src/ffconf.h b/components/fatfs/src/ffconf.h index dcabe50ce6..a265dee159 100644 --- a/components/fatfs/src/ffconf.h +++ b/components/fatfs/src/ffconf.h @@ -235,7 +235,7 @@ / f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */ -#define FF_USE_TRIM 0 +#define FF_USE_TRIM 1 /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) / To enable Trim function, also CTRL_TRIM command should be implemented to the / disk_ioctl() function. */