Skip to content

Commit

Permalink
feat(spi_nand_flash): expose lower-level API and make it usable witho…
Browse files Browse the repository at this point in the history
…ut Dhara
  • Loading branch information
RathiSonika committed Nov 11, 2024
1 parent d402e9c commit b66eda8
Show file tree
Hide file tree
Showing 17 changed files with 961 additions and 549 deletions.
6 changes: 6 additions & 0 deletions spi_nand_flash/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
set(srcs "src/nand.c"
"src/nand_winbond.c"
"src/nand_gigadevice.c"
"src/nand_alliance.c"
"src/nand_micron.c"
"src/spi_nand_oper.c"
"src/spi_nand_api.c"
"src/dhara_glue.c"
"vfs/vfs_fat_spinandflash.c"
"diskio/diskio_nand.c")
Expand All @@ -16,5 +21,6 @@ set(priv_reqs vfs)

idf_component_register(SRCS ${srcs}
INCLUDE_DIRS include vfs diskio
PRIV_INCLUDE_DIRS "priv_include"
REQUIRES ${reqs}
PRIV_REQUIRES ${priv_reqs})
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.6.0"
version: "0.7.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
36 changes: 29 additions & 7 deletions spi_nand_flash/include/spi_nand_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2015-2024 Espressif Systems (Shanghai) CO LTD
*/

#pragma once
Expand All @@ -12,12 +12,17 @@
#include "esp_err.h"
#include "driver/spi_common.h"
#include "driver/spi_master.h"
#include "dhara/map.h"

#ifdef __cplusplus
extern "C" {
#endif

#define NAND_MAX_DEVICES 1

typedef uint32_t page_t;
typedef uint32_t block_t;
typedef uint32_t sector_t;

/** @brief Structure to describe how to configure the nand access layer.
@note The spi_device_handle_t must be initialized with the flag SPI_DEVICE_HALFDUPLEX
*/
Expand Down Expand Up @@ -47,7 +52,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, dhara_sector_t sector_id);
esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *buffer, sector_t sector_id);

/** @brief Copy a sector to another sector from the nand flash.
*
Expand All @@ -56,7 +61,7 @@ esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *b
* @param dst_sec The destination sector id to which data should be copied.
* @return ESP_OK on success, or a flash error code if the copy failed.
*/
esp_err_t spi_nand_flash_copy_sector(spi_nand_flash_device_t *handle, dhara_sector_t src_sec, dhara_sector_t dst_sec);
esp_err_t spi_nand_flash_copy_sector(spi_nand_flash_device_t *handle, sector_t src_sec, sector_t dst_sec);

/** @brief Write a sector to the nand flash.
*
Expand All @@ -65,7 +70,7 @@ esp_err_t spi_nand_flash_copy_sector(spi_nand_flash_device_t *handle, dhara_sect
* @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, dhara_sector_t sector_id);
esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, sector_t sector_id);

/** @brief Trim sector from the nand flash.
*
Expand All @@ -77,7 +82,7 @@ esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uin
* @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);
esp_err_t spi_nand_flash_trim(spi_nand_flash_device_t *handle, sector_t sector_id);

/** @brief Synchronizes any cache to the device.
*
Expand All @@ -94,7 +99,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, dhara_sector_t *number_of_sectors);
esp_err_t spi_nand_flash_get_capacity(spi_nand_flash_device_t *handle, sector_t *number_of_sectors);

/** @brief Retrieve the size of each sector.
*
Expand All @@ -111,6 +116,23 @@ esp_err_t spi_nand_flash_get_sector_size(spi_nand_flash_device_t *handle, uint32
*/
esp_err_t spi_nand_erase_chip(spi_nand_flash_device_t *handle);

/** @brief Retrieve the number of blocks available.
*
* @param handle The handle to the SPI nand flash chip.
* @param[out] number_of_blocks 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_block_num(spi_nand_flash_device_t *handle, block_t *number_of_blocks);

/** @brief Check if the given block is bad block.
*
* @param handle The handle to the SPI nand flash chip.
* @param block The block number to verify if it is bad block
* @param[out] is_bad 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_is_bad_block(spi_nand_flash_device_t *handle, block_t block, bool *is_bad);

/** @brief De-initialize the handle, releasing any resources reserved.
*
* @param handle The handle to the SPI nand flash chip.
Expand Down
76 changes: 76 additions & 0 deletions spi_nand_flash/priv_include/nand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* SPDX-FileCopyrightText: 2022 mikkeldamsgaard project
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2015-2024 Espressif Systems (Shanghai) CO LTD
*/

#pragma once

#include <stdint.h>
#include "spi_nand_flash.h"
#include "freertos/semphr.h"

#ifdef __cplusplus
extern "C" {
#endif

#define INVALID_PAGE 0xFFFF

typedef enum {
STAT_ECC_OK = 0,
STAT_ECC_1_TO_3_BITS_CORRECTED = 1,
STAT_ECC_BITS_CORRECTED = STAT_ECC_1_TO_3_BITS_CORRECTED,
STAT_ECC_NOT_CORRECTED = 2,
STAT_ECC_4_TO_6_BITS_CORRECTED = 3,
STAT_ECC_MAX_BITS_CORRECTED = STAT_ECC_4_TO_6_BITS_CORRECTED,
STAT_ECC_7_8_BITS_CORRECTED = 5,
STAT_ECC_MAX
} ecc_status_t;

typedef struct {
uint8_t ecc_status_reg_len_in_bits;
uint8_t ecc_data_refresh_threshold;
ecc_status_t ecc_corrected_bits_status;
} ecc_data_t;

typedef struct {
uint8_t log2_page_size; //is power of 2, log2_page_size shift (1<<log2_page_size) is stored to page_size
uint8_t log2_ppb; //is power of 2, log2_ppb shift ((1<<log2_ppb) * page_size) will be stored in block size
uint32_t block_size;
uint32_t page_size;
uint32_t num_blocks;
uint32_t read_page_delay_us;
uint32_t erase_block_delay_us;
uint32_t program_page_delay_us;
ecc_data_t ecc_data;
} spi_nand_chip_t;

typedef struct {
esp_err_t (*init)(spi_nand_flash_device_t *handle);
esp_err_t (*deinit)(spi_nand_flash_device_t *handle);
esp_err_t (*read)(spi_nand_flash_device_t *handle, uint8_t *buffer, sector_t sector_id);
esp_err_t (*write)(spi_nand_flash_device_t *handle, const uint8_t *buffer, sector_t sector_id);
esp_err_t (*trim)(spi_nand_flash_device_t *handle, sector_t sector_id);
esp_err_t (*sync)(spi_nand_flash_device_t *handle);
esp_err_t (*copy_sector)(spi_nand_flash_device_t *handle, sector_t src_sec, sector_t dst_sec);
esp_err_t (*get_capacity)(spi_nand_flash_device_t *handle, sector_t *number_of_sectors);
} spi_nand_ops;

struct spi_nand_flash_device_t {
uint8_t dev_index;
spi_nand_flash_config_t config;
spi_nand_chip_t chip;
const spi_nand_ops *ops;
uint8_t *work_buffer;
uint8_t *read_buffer;
SemaphoreHandle_t mutex;
};

esp_err_t register_nand_dev(spi_nand_flash_device_t *handle);
esp_err_t unregister_nand_dev(spi_nand_flash_device_t *handle);

#ifdef __cplusplus
}
#endif
30 changes: 30 additions & 0 deletions spi_nand_flash/priv_include/spi_nand_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2022 mikkeldamsgaard project
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2015-2023 Espressif Systems (Shanghai) CO LTD
*/

#pragma once

#include <stdint.h>
#include "esp_err.h"
#include "nand.h"

#ifdef __cplusplus
extern "C" {
#endif

esp_err_t nand_is_bad(spi_nand_flash_device_t *handle, block_t b, bool *is_bad_status);
esp_err_t nand_mark_bad(spi_nand_flash_device_t *handle, block_t b);
esp_err_t nand_erase_chip(spi_nand_flash_device_t *handle);
esp_err_t nand_erase_block(spi_nand_flash_device_t *handle, block_t b);
esp_err_t nand_prog(spi_nand_flash_device_t *handle, page_t p, const uint8_t *data);
esp_err_t nand_is_free(spi_nand_flash_device_t *handle, page_t p, bool *is_free_status);
esp_err_t nand_read(spi_nand_flash_device_t *handle, page_t p, size_t offset, size_t length, uint8_t *data);
esp_err_t nand_copy(spi_nand_flash_device_t *handle, page_t src, page_t dst);

#ifdef __cplusplus
}
#endif
File renamed without changes.
Loading

0 comments on commit b66eda8

Please sign in to comment.