Skip to content

Commit 116bceb

Browse files
authored
Merge pull request #447 from RathiSonika/feat/spi_nand_expose_lower_level_api
feat(spi_nand_flash): expose lower-level API and make it usable without Dhara
2 parents 772c89d + eb4e40c commit 116bceb

20 files changed

+1138
-549
lines changed

spi_nand_flash/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
set(srcs "src/nand.c"
2+
"src/nand_winbond.c"
3+
"src/nand_gigadevice.c"
4+
"src/nand_alliance.c"
5+
"src/nand_micron.c"
6+
"src/nand_impl.c"
7+
"src/nand_impl_wrap.c"
28
"src/spi_nand_oper.c"
39
"src/dhara_glue.c"
410
"vfs/vfs_fat_spinandflash.c"
@@ -16,5 +22,6 @@ set(priv_reqs vfs)
1622

1723
idf_component_register(SRCS ${srcs}
1824
INCLUDE_DIRS include vfs diskio
25+
PRIV_INCLUDE_DIRS "priv_include"
1926
REQUIRES ${reqs}
2027
PRIV_REQUIRES ${priv_reqs})

spi_nand_flash/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.6.0"
1+
version: "0.7.0"
22
description: Driver for accessing SPI NAND Flash
33
url: https://github.com/espressif/idf-extra-components/tree/master/spi_nand_flash
44
issues: https://github.com/espressif/idf-extra-components/issues
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdint.h>
10+
#include "esp_err.h"
11+
#include "spi_nand_flash.h"
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
// These APIs provide direct access to lower-level NAND functions, bypassing the Dhara library.
18+
// These functions differ from the similarly named `nand_*` functions in that they also take the mutex for the duration of the call.
19+
20+
esp_err_t nand_wrap_is_bad(spi_nand_flash_device_t *handle, uint32_t b, bool *is_bad_status);
21+
esp_err_t nand_wrap_mark_bad(spi_nand_flash_device_t *handle, uint32_t b);
22+
esp_err_t nand_wrap_erase_chip(spi_nand_flash_device_t *handle);
23+
esp_err_t nand_wrap_erase_block(spi_nand_flash_device_t *handle, uint32_t b);
24+
esp_err_t nand_wrap_prog(spi_nand_flash_device_t *handle, uint32_t p, const uint8_t *data);
25+
esp_err_t nand_wrap_is_free(spi_nand_flash_device_t *handle, uint32_t p, bool *is_free_status);
26+
esp_err_t nand_wrap_read(spi_nand_flash_device_t *handle, uint32_t p, size_t offset, size_t length, uint8_t *data);
27+
esp_err_t nand_wrap_copy(spi_nand_flash_device_t *handle, uint32_t src, uint32_t dst);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif

spi_nand_flash/include/spi_nand_flash.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
6-
* SPDX-FileContributor: 2015-2023 Espressif Systems (Shanghai) CO LTD
6+
* SPDX-FileContributor: 2015-2024 Espressif Systems (Shanghai) CO LTD
77
*/
88

99
#pragma once
@@ -12,7 +12,6 @@
1212
#include "esp_err.h"
1313
#include "driver/spi_common.h"
1414
#include "driver/spi_master.h"
15-
#include "dhara/map.h"
1615

1716
#ifdef __cplusplus
1817
extern "C" {
@@ -47,7 +46,7 @@ esp_err_t spi_nand_flash_init_device(spi_nand_flash_config_t *config, spi_nand_f
4746
* @param sector_id The id of the sector to read.
4847
* @return ESP_OK on success, or a flash error code if the read failed.
4948
*/
50-
esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *buffer, dhara_sector_t sector_id);
49+
esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *buffer, uint32_t sector_id);
5150

5251
/** @brief Copy a sector to another sector from the nand flash.
5352
*
@@ -56,7 +55,7 @@ esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *b
5655
* @param dst_sec The destination sector id to which data should be copied.
5756
* @return ESP_OK on success, or a flash error code if the copy failed.
5857
*/
59-
esp_err_t spi_nand_flash_copy_sector(spi_nand_flash_device_t *handle, dhara_sector_t src_sec, dhara_sector_t dst_sec);
58+
esp_err_t spi_nand_flash_copy_sector(spi_nand_flash_device_t *handle, uint32_t src_sec, uint32_t dst_sec);
6059

6160
/** @brief Write a sector to the nand flash.
6261
*
@@ -65,7 +64,7 @@ esp_err_t spi_nand_flash_copy_sector(spi_nand_flash_device_t *handle, dhara_sect
6564
* @param sector_id The id of the sector to write.
6665
* @return ESP_OK on success, or a flash error code if the write failed.
6766
*/
68-
esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, dhara_sector_t sector_id);
67+
esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, uint32_t sector_id);
6968

7069
/** @brief Trim sector from the nand flash.
7170
*
@@ -77,7 +76,7 @@ esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uin
7776
* @param sector_id The id of the sector to be trimmed.
7877
* @return ESP_OK on success, or a flash error code if the trim failed.
7978
*/
80-
esp_err_t spi_nand_flash_trim(spi_nand_flash_device_t *handle, dhara_sector_t sector_id);
79+
esp_err_t spi_nand_flash_trim(spi_nand_flash_device_t *handle, uint32_t sector_id);
8180

8281
/** @brief Synchronizes any cache to the device.
8382
*
@@ -94,23 +93,39 @@ esp_err_t spi_nand_flash_sync(spi_nand_flash_device_t *handle);
9493
* @param[out] number_of_sectors A pointer of where to put the return value
9594
* @return ESP_OK on success, or a flash error code if the operation failed.
9695
*/
97-
esp_err_t spi_nand_flash_get_capacity(spi_nand_flash_device_t *handle, dhara_sector_t *number_of_sectors);
96+
esp_err_t spi_nand_flash_get_capacity(spi_nand_flash_device_t *handle, uint32_t *number_of_sectors);
9897

9998
/** @brief Retrieve the size of each sector.
10099
*
101100
* @param handle The handle to the SPI nand flash chip.
102-
* @param[out] number_of_sectors A pointer of where to put the return value
101+
* @param[out] sectors_size A pointer of where to put the return value
103102
* @return ESP_OK on success, or a flash error code if the operation failed.
104103
*/
105104
esp_err_t spi_nand_flash_get_sector_size(spi_nand_flash_device_t *handle, uint32_t *sector_size);
106105

106+
/** @brief Retrieve the size of each block.
107+
*
108+
* @param handle The handle to the SPI nand flash chip.
109+
* @param[out] block_size A pointer of where to put the return value
110+
* @return ESP_OK on success, or a flash error code if the operation failed.
111+
*/
112+
esp_err_t spi_nand_flash_get_block_size(spi_nand_flash_device_t *handle, uint32_t *block_size);
113+
107114
/** @brief Erases the entire chip, invalidating any data on the chip.
108115
*
109116
* @param handle The handle to the SPI nand flash chip.
110117
* @return ESP_OK on success, or a flash error code if the erase failed.
111118
*/
112119
esp_err_t spi_nand_erase_chip(spi_nand_flash_device_t *handle);
113120

121+
/** @brief Retrieve the number of blocks available.
122+
*
123+
* @param handle The handle to the SPI nand flash chip.
124+
* @param[out] number_of_blocks A pointer of where to put the return value
125+
* @return ESP_OK on success, or a flash error code if the operation failed.
126+
*/
127+
esp_err_t spi_nand_flash_get_block_num(spi_nand_flash_device_t *handle, uint32_t *number_of_blocks);
128+
114129
/** @brief De-initialize the handle, releasing any resources reserved.
115130
*
116131
* @param handle The handle to the SPI nand flash chip.

spi_nand_flash/priv_include/nand.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 mikkeldamsgaard project
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* SPDX-FileContributor: 2015-2024 Espressif Systems (Shanghai) CO LTD
7+
*/
8+
9+
#pragma once
10+
11+
#include <stdint.h>
12+
#include "spi_nand_flash.h"
13+
#include "freertos/semphr.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
#define INVALID_PAGE 0xFFFF
20+
21+
typedef enum {
22+
STAT_ECC_OK = 0,
23+
STAT_ECC_1_TO_3_BITS_CORRECTED = 1,
24+
STAT_ECC_BITS_CORRECTED = STAT_ECC_1_TO_3_BITS_CORRECTED,
25+
STAT_ECC_NOT_CORRECTED = 2,
26+
STAT_ECC_4_TO_6_BITS_CORRECTED = 3,
27+
STAT_ECC_MAX_BITS_CORRECTED = STAT_ECC_4_TO_6_BITS_CORRECTED,
28+
STAT_ECC_7_8_BITS_CORRECTED = 5,
29+
STAT_ECC_MAX
30+
} ecc_status_t;
31+
32+
typedef struct {
33+
uint8_t ecc_status_reg_len_in_bits;
34+
uint8_t ecc_data_refresh_threshold;
35+
ecc_status_t ecc_corrected_bits_status;
36+
} ecc_data_t;
37+
38+
typedef struct {
39+
uint8_t log2_page_size; //is power of 2, log2_page_size shift (1<<log2_page_size) is stored to page_size
40+
uint8_t log2_ppb; //is power of 2, log2_ppb shift ((1<<log2_ppb) * page_size) will be stored in block size
41+
uint32_t block_size;
42+
uint32_t page_size;
43+
uint32_t num_blocks;
44+
uint32_t read_page_delay_us;
45+
uint32_t erase_block_delay_us;
46+
uint32_t program_page_delay_us;
47+
ecc_data_t ecc_data;
48+
} spi_nand_chip_t;
49+
50+
typedef struct {
51+
esp_err_t (*init)(spi_nand_flash_device_t *handle);
52+
esp_err_t (*deinit)(spi_nand_flash_device_t *handle);
53+
esp_err_t (*read)(spi_nand_flash_device_t *handle, uint8_t *buffer, uint32_t sector_id);
54+
esp_err_t (*write)(spi_nand_flash_device_t *handle, const uint8_t *buffer, uint32_t sector_id);
55+
esp_err_t (*erase_chip)(spi_nand_flash_device_t *handle);
56+
esp_err_t (*erase_block)(spi_nand_flash_device_t *handle, uint32_t block);
57+
esp_err_t (*trim)(spi_nand_flash_device_t *handle, uint32_t sector_id);
58+
esp_err_t (*sync)(spi_nand_flash_device_t *handle);
59+
esp_err_t (*copy_sector)(spi_nand_flash_device_t *handle, uint32_t src_sec, uint32_t dst_sec);
60+
esp_err_t (*get_capacity)(spi_nand_flash_device_t *handle, uint32_t *number_of_sectors);
61+
} spi_nand_ops;
62+
63+
struct spi_nand_flash_device_t {
64+
spi_nand_flash_config_t config;
65+
spi_nand_chip_t chip;
66+
const spi_nand_ops *ops;
67+
void *ops_priv_data;
68+
uint8_t *work_buffer;
69+
uint8_t *read_buffer;
70+
SemaphoreHandle_t mutex;
71+
};
72+
73+
esp_err_t nand_register_dev(spi_nand_flash_device_t *handle);
74+
esp_err_t nand_unregister_dev(spi_nand_flash_device_t *handle);
75+
76+
#ifdef __cplusplus
77+
}
78+
#endif
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdint.h>
10+
#include "esp_err.h"
11+
#include "spi_nand_flash.h"
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
esp_err_t spi_nand_winbond_init(spi_nand_flash_device_t *dev);
18+
esp_err_t spi_nand_alliance_init(spi_nand_flash_device_t *dev);
19+
esp_err_t spi_nand_gigadevice_init(spi_nand_flash_device_t *dev);
20+
esp_err_t spi_nand_micron_init(spi_nand_flash_device_t *dev);
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif
File renamed without changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 mikkeldamsgaard project
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* SPDX-FileContributor: 2015-2023 Espressif Systems (Shanghai) CO LTD
7+
*/
8+
9+
#pragma once
10+
11+
#include <stdint.h>
12+
#include "esp_err.h"
13+
#include "nand.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
esp_err_t nand_is_bad(spi_nand_flash_device_t *handle, uint32_t b, bool *is_bad_status);
20+
esp_err_t nand_mark_bad(spi_nand_flash_device_t *handle, uint32_t b);
21+
esp_err_t nand_erase_chip(spi_nand_flash_device_t *handle);
22+
esp_err_t nand_erase_block(spi_nand_flash_device_t *handle, uint32_t b);
23+
esp_err_t nand_prog(spi_nand_flash_device_t *handle, uint32_t p, const uint8_t *data);
24+
esp_err_t nand_is_free(spi_nand_flash_device_t *handle, uint32_t p, bool *is_free_status);
25+
esp_err_t nand_read(spi_nand_flash_device_t *handle, uint32_t p, size_t offset, size_t length, uint8_t *data);
26+
esp_err_t nand_copy(spi_nand_flash_device_t *handle, uint32_t src, uint32_t dst);
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif
File renamed without changes.

0 commit comments

Comments
 (0)