Skip to content

Commit

Permalink
feat(spi_nand_flash): Add linux target support
Browse files Browse the repository at this point in the history
  • Loading branch information
RathiSonika committed Feb 20, 2025
1 parent 52be198 commit e7d6c04
Show file tree
Hide file tree
Showing 22 changed files with 987 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .build-test-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ spi_nand_flash/test_app:
- if: IDF_VERSION_MAJOR < 5
reason: The spi_nand_flash component is compatible with IDF version v5.0 and above, due to a change in the f_mkfs API in versions above v5.0, which is not supported in older IDF versions.

spi_nand_flash/host_test:
disable:
- if: IDF_VERSION_MAJOR < 5
reason: The spi_nand_flash component is compatible with IDF version v5.0 and above, due to a change in the f_mkfs API in versions above v5.0, which is not supported in older IDF versions.

thorvg/examples/thorvg-example:
enable:
- if: (IDF_VERSION_MAJOR == 5 and IDF_VERSION_MINOR >= 3) and (IDF_TARGET in ["esp32p4"])
Expand Down
42 changes: 29 additions & 13 deletions spi_nand_flash/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
idf_build_get_property(target IDF_TARGET)

set(reqs fatfs)
set(inc diskio include)
set(priv_inc priv_include)
set(srcs "src/nand.c"
"src/nand_winbond.c"
"src/nand_gigadevice.c"
"src/nand_alliance.c"
"src/nand_micron.c"
"src/nand_impl.c"
"src/nand_impl_wrap.c"
"src/nand_diag_api.c"
"src/spi_nand_oper.c"
"src/dhara_glue.c"
"vfs/vfs_fat_spinandflash.c"
"src/nand_impl_wrap.c"
"diskio/diskio_nand.c")

set(reqs fatfs)
if(${target} STREQUAL "linux")

list(APPEND srcs "src/nand_impl_linux.c"
"src/nand_linux_mmap_emul.c")

else()

list(APPEND srcs "src/nand_winbond.c"
"src/nand_gigadevice.c"
"src/nand_alliance.c"
"src/nand_micron.c"
"src/nand_impl.c"
"src/nand_impl_wrap.c"
"src/nand_diag_api.c"
"src/spi_nand_oper.c"
"vfs/vfs_fat_spinandflash.c")

set(priv_reqs vfs)
list(APPEND inc vfs)

if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER "5.3")
list(APPEND reqs esp_driver_spi)
else()
list(APPEND reqs driver)
endif()

set(priv_reqs vfs)
endif()


idf_component_register(SRCS ${srcs}
INCLUDE_DIRS include vfs diskio
PRIV_INCLUDE_DIRS "priv_include"
INCLUDE_DIRS ${inc}
PRIV_INCLUDE_DIRS ${priv_inc}
REQUIRES ${reqs}
PRIV_REQUIRES ${priv_reqs})
7 changes: 7 additions & 0 deletions spi_nand_flash/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ menu "SPI NAND Flash configuration"
If this option is enabled, any time SPI NAND flash is written then the data will be read
back and verified. This can catch hardware problems with SPI NAND flash, or flash which
was not erased before verification.

config NAND_ENABLE_STATS
bool "Host test statistics enabled"
depends on IDF_TARGET_LINUX
default n
help
This option enables gathering host test statistics and SPI NAND flash wear levelling simulation.
endmenu
22 changes: 22 additions & 0 deletions spi_nand_flash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ SPI NAND Flash combines the benefits of NAND Flash technology with the simplicit

* Fast Read/Write Operations: The SPI interface enables reasonably fast read and write operations, making it suitable for applications where data access speed is crucial.

### Implementation Architecture

```mermaid
graph TD
A[Application] --> B[FATFS]
B --> C[Dhara Library]
C --> Hardware_Path[Hardware Path]
C --> Linux_Path[Linux Path]
subgraph Hardware_Path [Hardware Path]
HP1[NAND Flash Layer]
HP1 --> HP2[SPI NAND Flash Driver]
HP2 --> HP3["SPI Driver (ESP-IDF)"]
HP3 --> HP4[Hardware via SPI]
end
subgraph Linux_Path [Linux Path]
LP1[NAND Flash Layer]
LP1 --> LP2[NAND Emulation Layer]
LP2 --> LP3[Memory Mapped File]
end
```
## Supported SPI NAND Flash chips

At present, `spi_nand_flash` component is compatible with the chips produced by the following manufacturers and and their respective model numbers:
Expand Down
3 changes: 2 additions & 1 deletion spi_nand_flash/diskio/diskio_nand.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ DRESULT ff_nand_ioctl(BYTE pdrv, BYTE cmd, void *buff)
break;
}
#if FF_USE_TRIM
case CTRL_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
6 changes: 6 additions & 0 deletions spi_nand_flash/host_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main)

project(nand_flash_host_test)
54 changes: 54 additions & 0 deletions spi_nand_flash/host_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
| Supported Targets | Linux |
| ----------------- | ----- |

# Host Test for SPI NAND Flash Emulation

## NAND Flash Emulation Configuration

The NAND flash emulation can be configured using the `nand_mmap_emul_config_t` structure:

```c
// Configuration structure for NAND emulation
nand_mmap_emul_config_t cfg = {
.flash_file_name = "", // Empty string for temporary file, or specify path
.flash_file_size = EMULATED_NAND_SIZE, // Default is 500MB
.remove_dump = true // true to remove file after tests
};
```

### Configuration Options:

1. **flash_file_name**:
- Empty string ("") - Creates temporary file with pattern "/tmp/idf-nand-XXXXXX"
- Custom path - Creates file at specified location
- Maximum length: 256 characters

2. **flash_file_size**:
- Default: EMULATED_NAND_SIZE (500MB)
- Can be customized based on test requirements
- Must be aligned to block size

3. **remove_dump**:
- true: Removes the memory-mapped file after testing
- false: Keeps the file for debugging or data persistence

### Usage Example:

```c
// Initialize with custom settings
nand_mmap_emul_config_t cfg = {
.flash_file_name = "/tmp/my_nand.bin",
.flash_file_size = 1024 * 1024, // 1MB
.remove_dump = false
};
spi_nand_flash_config_t nand_flash_config = {.emul_conf = &cfg};

// Initialize nand_flash with NAND emulation parameter
spi_nand_flash_device_t *handle;
spi_nand_flash_init_device(&nand_flash_config, &handle)

// Use NAND operations...

// Cleanup
ESP_ERROR_CHECK(spi_nand_flash_deinit_device(handle));
```
6 changes: 6 additions & 0 deletions spi_nand_flash/host_test/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
idf_component_register(SRCS "test_nand_flash.cpp" "test_app_main.cpp"
REQUIRES fatfs
WHOLE_ARCHIVE
)

target_link_libraries(${COMPONENT_LIB} PRIVATE Catch2WithMain)
5 changes: 5 additions & 0 deletions spi_nand_flash/host_test/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies:
espressif/catch2: "^3.4.0"
espressif/spi_nand_flash:
version: '*'
override_path: '../../'
28 changes: 28 additions & 0 deletions spi_nand_flash/host_test/main/test_app_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#include <catch2/catch_session.hpp>
#include <catch2/catch_test_macros.hpp>


extern "C" void app_main(void)
{
int argc = 1;
const char *argv[2] = {
"target_test_main",
NULL
};

auto result = Catch::Session().run(argc, argv);
if (result != 0) {
printf("Test failed with result %d\n", result);
} else {
printf("Test passed.\n");
}
fflush(stdout);
exit(result);
}
Loading

0 comments on commit e7d6c04

Please sign in to comment.