Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 32 bit sector id (IEC-140) #356

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions spi_nand_flash/diskio/diskio_nand.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -85,17 +85,23 @@ 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;
ESP_LOGV(TAG, "capacity=%"PRIu32"", *((DWORD *) 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems fine, it should prevent FATFS from initializing further.

}

*((WORD *)buff) = capacity;
ESP_LOGV(TAG, "sector size=%d", *((WORD *)buff));
break;
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.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
Expand Down
8 changes: 4 additions & 4 deletions spi_nand_flash/include/spi_nand_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -73,15 +73,15 @@ 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.
*
* @param handle The handle to the SPI nand flash chip.
* @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.
*
Expand Down
8 changes: 4 additions & 4 deletions spi_nand_flash/src/nand.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions spi_nand_flash/test_app/main/test_spi_nand_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <inttypes.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
Expand Down Expand Up @@ -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, &sector_num));
TEST_ESP_OK(spi_nand_flash_get_sector_size(flash, &sector_size));
Expand All @@ -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, &sector_num));
TEST_ESP_OK(spi_nand_flash_get_sector_size(nand_flash_device_handle, &sector_size));
igrr marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down
2 changes: 1 addition & 1 deletion spi_nand_flash/vfs/vfs_fat_spinandflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading