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

feat(spi_nand_flash): Add linux target support #472

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RathiSonika
Copy link
Collaborator

@RathiSonika RathiSonika commented Jan 30, 2025

Change description

Below is the implementation architecture of the spi_nand_flash component.

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
Loading

This PR adds support for the Linux target, following the Linux path shown in the diagram above. The files nand_impl_linux.c and nand_linux_mmap_emul.c have been added to implement the NAND emulation layer and memory-mapped file handling which is used to emulate NAND Flash.

Additionally, host_tests have been included to verify that all tests function correctly on the Linux platform.

@RathiSonika RathiSonika force-pushed the feat/nand_flash_linux_support branch 4 times, most recently from 05e804f to 4790c94 Compare February 3, 2025 12:45
@RathiSonika RathiSonika self-assigned this Feb 3, 2025
@RathiSonika RathiSonika force-pushed the feat/nand_flash_linux_support branch from 4790c94 to 6082e74 Compare February 3, 2025 13:22
@RathiSonika RathiSonika requested a review from igrr February 3, 2025 13:23
@RathiSonika RathiSonika marked this pull request as ready for review February 3, 2025 13:24
Copy link
Member

@igrr igrr left a comment

Choose a reason for hiding this comment

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

If I understand correctly, NAND flash is emulated by the "storage" partition in the emulated SPI flash.

If yes, this raises a couple of questions.

First, emulated SPI flash size is currently limited to 4 MB (espressif/esp-idf#14894). So it seems we can only emulate very tiny NAND flash, if we rely on esp_partition calls. This looks like a limitation.

Second, hard-coded usage of the "storage" partition doesn't seem to be documented. How should the user of this library find out that the spi_nand_flash component is using the "storage" partition, so that they avoid using "storage" themselves?

I also wonder if you have considered any alternative designs while working on this feature. For example, storing NAND flash contents in a separate file — which wouldn't be impacted by the above limitations. What are the reasons why you have chosen this particular design over the alternatives?

By the way, I would also suggest including such information in the PR description. A few sentences with an overview of the design or implementation, and some reasons why it was chosen, can help reviewers a lot.

@RathiSonika RathiSonika marked this pull request as draft February 20, 2025 08:10
@RathiSonika RathiSonika force-pushed the feat/nand_flash_linux_support branch 2 times, most recently from 17ef5d3 to e7d6c04 Compare February 20, 2025 08:43
@RathiSonika
Copy link
Collaborator Author

Thank you for your comments @igrr.
The previous design prioritised direct integration with ESP-IDF's partition system to ensure better compatibility with existing storage mechanisms, which is why the "storage" partition was originally used. However, hard-coded usage of the "storage" partition is not good way and also having spi_nand_flash depend on esp_partition is not the correct approach—it should be the other way around. To address this, the design has been updated to use a separate memory-mapped file for storing NAND flash contents.

Regarding PR documentation, Agreed! Adding a design rationale in the PR description will help reviewers better understand the approach. I’ll make sure to include it moving forward.

Additionally, I have updated the README.md file to include documentation for these changes.
PTAL. Thank you!

@RathiSonika RathiSonika force-pushed the feat/nand_flash_linux_support branch from d8c8cfe to a7b4b6d Compare February 25, 2025 11:38
Copy link

@pacucha42 pacucha42 left a comment

Choose a reason for hiding this comment

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

First round of the final review done. Thanks for revamping the previous design, now all generally LGTM, except for the dependencies: please, try to resolve the FatFS separation, as there should be no relation of any file system to this component. Consider placing all the FatFS specific code (diskio, examples) into IDF, and add the dependency on spi_nand_flash there (similar to esp_littlfs model). Thanks

@@ -1,28 +1,44 @@
idf_build_get_property(target IDF_TARGET)

set(reqs fatfs)

Choose a reason for hiding this comment

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

Why do we need dependency on FatFS?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This dependency is primarily required for the diskio layer implementation. However, I have now made it private to this component, preventing other components from inheriting it directly.


```mermaid
graph TD
A[Application] --> B[FATFS]

Choose a reason for hiding this comment

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

FatFS is unrelated to this component, imo. See previous comment

@@ -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: {

Choose a reason for hiding this comment

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

Dtto. This update should be internal to the FatFS code (please, check CMake for possible forking)

"src/nand_impl_wrap.c"
"src/nand_diag_api.c"
"src/spi_nand_oper.c"
"vfs/vfs_fat_spinandflash.c")

Choose a reason for hiding this comment

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

FatFS deps, see previous comments

@RathiSonika RathiSonika force-pushed the feat/nand_flash_linux_support branch 2 times, most recently from 231ffc1 to cbc7032 Compare February 27, 2025 09:27
@RathiSonika RathiSonika force-pushed the feat/nand_flash_linux_support branch from cbc7032 to 40e002c Compare February 27, 2025 11:20
@RathiSonika RathiSonika force-pushed the feat/nand_flash_linux_support branch from 40e002c to 555fe21 Compare February 27, 2025 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants