diff --git a/spi_nand_flash/diskio/diskio_nand.c b/spi_nand_flash/diskio/diskio_nand.c index 8edcdfe2aa..6b01020591 100644 --- a/spi_nand_flash/diskio/diskio_nand.c +++ b/spi_nand_flash/diskio/diskio_nand.c @@ -33,7 +33,7 @@ DRESULT ff_nand_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count) ESP_LOGV(TAG, "ff_nand_read - pdrv=%i, sector=%i, count=%i", (unsigned int) pdrv, (unsigned int) sector, (unsigned int) count); esp_err_t ret; - uint16_t sector_size; + uint32_t sector_size; spi_nand_flash_device_t *dev = ff_nand_handles[pdrv]; assert(dev); @@ -56,7 +56,7 @@ DRESULT ff_nand_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count) ESP_LOGV(TAG, "ff_nand_write - pdrv=%i, sector=%i, count=%i", (unsigned int) pdrv, (unsigned int) sector, (unsigned int) count); esp_err_t ret; - uint16_t sector_size; + uint32_t sector_size; spi_nand_flash_device_t *dev = ff_nand_handles[pdrv]; assert(dev); @@ -85,7 +85,7 @@ DRESULT ff_nand_ioctl(BYTE pdrv, BYTE cmd, void *buff) ESP_GOTO_ON_ERROR(spi_nand_flash_sync(dev), fail, TAG, "sync failed"); break; case GET_SECTOR_COUNT: { - uint16_t num_sectors; + uint32_t num_sectors; ESP_GOTO_ON_ERROR(spi_nand_flash_get_capacity(dev, &num_sectors), fail, TAG, "get_capacity failed"); *((DWORD *)buff) = num_sectors; @@ -93,9 +93,15 @@ DRESULT ff_nand_ioctl(BYTE pdrv, BYTE cmd, void *buff) break; } case GET_SECTOR_SIZE: { - uint16_t capacity; + uint32_t capacity; ESP_GOTO_ON_ERROR(spi_nand_flash_get_sector_size(dev, &capacity), fail, TAG, "get_sector_size failed"); + + if (capacity > 0xFFFF) { + ESP_LOGE(TAG, "sector size too large"); + return RES_PARERR; + } + *((WORD *)buff) = capacity; ESP_LOGV(TAG, "sector size=%d", *((WORD *)buff)); break; diff --git a/spi_nand_flash/idf_component.yml b/spi_nand_flash/idf_component.yml index 4113a67771..0827276fdf 100644 --- a/spi_nand_flash/idf_component.yml +++ b/spi_nand_flash/idf_component.yml @@ -1,4 +1,4 @@ -version: "0.2.0" +version: "0.3.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 diff --git a/spi_nand_flash/include/spi_nand_flash.h b/spi_nand_flash/include/spi_nand_flash.h index 6119b97b48..cb62434049 100644 --- a/spi_nand_flash/include/spi_nand_flash.h +++ b/spi_nand_flash/include/spi_nand_flash.h @@ -47,7 +47,7 @@ esp_err_t spi_nand_flash_init_device(spi_nand_flash_config_t *config, spi_nand_f * @param sector_id The id of the sector to read. * @return ESP_OK on success, or a flash error code if the read failed. */ -esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *buffer, uint16_t sector_id); +esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *buffer, dhara_sector_t sector_id); /** @brief Write a sector to the nand flash. * @@ -56,7 +56,7 @@ esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *b * @param sector_id The id of the sector to write. * @return ESP_OK on success, or a flash error code if the write failed. */ -esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, uint16_t sector_id); +esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, dhara_sector_t sector_id); /** @brief Synchronizes any cache to the device. * @@ -73,7 +73,7 @@ esp_err_t spi_nand_flash_sync(spi_nand_flash_device_t *handle); * @param[out] number_of_sectors A pointer of where to put the return value * @return ESP_OK on success, or a flash error code if the operation failed. */ -esp_err_t spi_nand_flash_get_capacity(spi_nand_flash_device_t *handle, uint16_t *number_of_sectors); +esp_err_t spi_nand_flash_get_capacity(spi_nand_flash_device_t *handle, dhara_sector_t *number_of_sectors); /** @brief Retrieve the size of each sector. * @@ -81,7 +81,7 @@ esp_err_t spi_nand_flash_get_capacity(spi_nand_flash_device_t *handle, uint16_t * @param[out] number_of_sectors A pointer of where to put the return value * @return ESP_OK on success, or a flash error code if the operation failed. */ -esp_err_t spi_nand_flash_get_sector_size(spi_nand_flash_device_t *handle, uint16_t *sector_size); +esp_err_t spi_nand_flash_get_sector_size(spi_nand_flash_device_t *handle, uint32_t *sector_size); /** @brief Erases the entire chip, invalidating any data on the chip. * diff --git a/spi_nand_flash/src/nand.c b/spi_nand_flash/src/nand.c index 8892ee741e..515cc4b584 100644 --- a/spi_nand_flash/src/nand.c +++ b/spi_nand_flash/src/nand.c @@ -274,7 +274,7 @@ esp_err_t spi_nand_erase_chip(spi_nand_flash_device_t *handle) return ret; } -esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *buffer, uint16_t sector_id) +esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *buffer, dhara_sector_t sector_id) { dhara_error_t err; esp_err_t ret = ESP_OK; @@ -294,7 +294,7 @@ esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *b return ret; } -esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, uint16_t sector_id) +esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, dhara_sector_t sector_id) { dhara_error_t err; esp_err_t ret = ESP_OK; @@ -324,13 +324,13 @@ esp_err_t spi_nand_flash_sync(spi_nand_flash_device_t *handle) return ret; } -esp_err_t spi_nand_flash_get_capacity(spi_nand_flash_device_t *handle, uint16_t *number_of_sectors) +esp_err_t spi_nand_flash_get_capacity(spi_nand_flash_device_t *handle, dhara_sector_t *number_of_sectors) { *number_of_sectors = dhara_map_capacity(&handle->dhara_map); return ESP_OK; } -esp_err_t spi_nand_flash_get_sector_size(spi_nand_flash_device_t *handle, uint16_t *sector_size) +esp_err_t spi_nand_flash_get_sector_size(spi_nand_flash_device_t *handle, uint32_t *sector_size) { *sector_size = handle->page_size; return ESP_OK; diff --git a/spi_nand_flash/test_app/main/test_spi_nand_flash.c b/spi_nand_flash/test_app/main/test_spi_nand_flash.c index 1e835c1735..fec8fcf68d 100644 --- a/spi_nand_flash/test_app/main/test_spi_nand_flash.c +++ b/spi_nand_flash/test_app/main/test_spi_nand_flash.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -129,7 +130,7 @@ static void do_single_write_test(spi_nand_flash_device_t *flash, uint32_t start_ { uint8_t *temp_buf = NULL; uint8_t *pattern_buf = NULL; - uint16_t sector_size, sector_num; + uint32_t sector_size, sector_num; TEST_ESP_OK(spi_nand_flash_get_capacity(flash, §or_num)); TEST_ESP_OK(spi_nand_flash_get_sector_size(flash, §or_size)); @@ -153,14 +154,14 @@ static void do_single_write_test(spi_nand_flash_device_t *flash, uint32_t start_ TEST_CASE("write nand flash sectors", "[spi_nand_flash]") { - uint16_t sector_num, sector_size; + uint32_t sector_num, sector_size; spi_nand_flash_device_t *nand_flash_device_handle; spi_device_handle_t spi; setup_nand_flash(&nand_flash_device_handle, &spi); TEST_ESP_OK(spi_nand_flash_get_capacity(nand_flash_device_handle, §or_num)); TEST_ESP_OK(spi_nand_flash_get_sector_size(nand_flash_device_handle, §or_size)); - printf("Number of sectors: %d, Sector size: %d\n", sector_num, sector_size); + printf("Number of sectors: %" PRIu32 ", Sector size: %" PRIu32 "\n", sector_num, sector_size); do_single_write_test(nand_flash_device_handle, 1, 16); do_single_write_test(nand_flash_device_handle, 16, 32); diff --git a/spi_nand_flash/vfs/vfs_fat_spinandflash.c b/spi_nand_flash/vfs/vfs_fat_spinandflash.c index 089ee33d1a..5482ee4a62 100644 --- a/spi_nand_flash/vfs/vfs_fat_spinandflash.c +++ b/spi_nand_flash/vfs/vfs_fat_spinandflash.c @@ -24,7 +24,7 @@ esp_err_t esp_vfs_fat_nand_mount(const char *base_path, spi_nand_flash_device_t esp_err_t ret = ESP_OK; void *workbuf = NULL; FATFS *fs = NULL; - uint16_t sector_size; + uint32_t sector_size; const size_t workbuf_size = 4096; // connect driver to FATFS