Skip to content

Commit

Permalink
Merge pull request #443 from RathiSonika/feat/nand_flash_impl_CTRL_TRIM
Browse files Browse the repository at this point in the history
feat(spi_nand_flash): implement CTRL_TRIM in fatfs diskio layer
  • Loading branch information
RathiSonika authored Nov 5, 2024
2 parents bb6abc0 + e7fa7e3 commit b118925
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
33 changes: 33 additions & 0 deletions spi_nand_flash/diskio/diskio_nand.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ DRESULT ff_nand_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
return RES_ERROR;
}

#if FF_USE_TRIM
DRESULT ff_nand_trim(BYTE pdrv, DWORD start_sector, DWORD sector_count)
{
esp_err_t ret;
uint32_t sector_size;
spi_nand_flash_device_t *dev = ff_nand_handles[pdrv];
assert(dev);

ESP_GOTO_ON_ERROR(spi_nand_flash_get_sector_size(dev, &sector_size), fail, TAG, "");

if ((start_sector > sector_size) || ((start_sector + sector_count) > sector_size)) {
return RES_PARERR;
}

for (int i = 0; i < sector_count; i++) {
ESP_GOTO_ON_ERROR(spi_nand_flash_trim(dev, start_sector + i),
fail, TAG, "spi_nand_flash_trim failed");
}
return RES_OK;

fail:
ESP_LOGE(TAG, "ff_nand_trim failed with error 0x%X", ret);
return RES_ERROR;
}
#endif //FF_USE_TRIM

DRESULT ff_nand_ioctl(BYTE pdrv, BYTE cmd, void *buff)
{
spi_nand_flash_device_t *dev = ff_nand_handles[pdrv];
Expand Down Expand Up @@ -106,6 +132,13 @@ DRESULT ff_nand_ioctl(BYTE pdrv, BYTE cmd, void *buff)
ESP_LOGV(TAG, "sector size=%d", *((WORD *)buff));
break;
}
#if FF_USE_TRIM
case CTRL_TRIM:
DWORD start_sector = *((DWORD *)buff);
DWORD end_sector = *((DWORD *)buff + 1) + 1;
DWORD sector_count = end_sector - start_sector;
return ff_nand_trim(pdrv, start_sector, sector_count);
#endif //FF_USE_TRIM
default:
return RES_ERROR;
}
Expand Down
2 changes: 1 addition & 1 deletion spi_nand_flash/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "0.5.0"
version: "0.6.0"
description: Driver for accessing SPI NAND Flash
url: https://github.com/espressif/idf-extra-components/tree/master/spi_nand_flash
issues: https://github.com/espressif/idf-extra-components/issues
Expand Down
12 changes: 12 additions & 0 deletions spi_nand_flash/include/spi_nand_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ esp_err_t spi_nand_flash_copy_sector(spi_nand_flash_device_t *handle, dhara_sect
*/
esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, dhara_sector_t sector_id);

/** @brief Trim sector from the nand flash.
*
* This function marks specified sector as free to optimize memory usage
* and support wear-leveling. Typically invoked when files are deleted or
* resized to allow the underlying storage to manage these sectors.
*
* @param handle The handle to the SPI nand flash chip.
* @param sector_id The id of the sector to be trimmed.
* @return ESP_OK on success, or a flash error code if the trim failed.
*/
esp_err_t spi_nand_flash_trim(spi_nand_flash_device_t *handle, dhara_sector_t sector_id);

/** @brief Synchronizes any cache to the device.
*
* After this method is called, the nand flash chip should be synchronized with the results of any previous read/writes.
Expand Down
15 changes: 15 additions & 0 deletions spi_nand_flash/src/nand.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,21 @@ esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uin
return ret;
}

esp_err_t spi_nand_flash_trim(spi_nand_flash_device_t *handle, dhara_sector_t sector_id)
{
dhara_error_t err;
esp_err_t ret = ESP_OK;

xSemaphoreTake(handle->mutex, portMAX_DELAY);

if (dhara_map_trim(&handle->dhara_map, sector_id, &err)) {
ret = ESP_ERR_FLASH_BASE + err;
}

xSemaphoreGive(handle->mutex);
return ret;
}

esp_err_t spi_nand_flash_sync(spi_nand_flash_device_t *handle)
{
dhara_error_t err;
Expand Down

0 comments on commit b118925

Please sign in to comment.