wavenumber-eng / mcxn-iot

mcxn_iot
MIT License
0 stars 0 forks source link

littlefs over SD card in Zephyr #4

Closed ehughes closed 1 month ago

ehughes commented 2 months ago

There is an NXP customer that needs some help with zephyr, SD card and the little FS file system. They were following this sample:

https://docs.zephyrproject.org/latest/samples/subsys/fs/littlefs/README.html

They were following the instructions here: image

for preparing a block device.

LittleFS is a special file system that can't be read normally in windows/linux. From those instructions, it sounds like you have to prepare the card with a special utility.

Questions.

1.) Can you see if you can follow the instructions to make the sample work? You can use the MCX N board w/ SD card

There is also this for windows:

https://github.com/bluscape/LittleFS-Explorer-for-Windows

2.) It seems to me that there shoudl be a way to directly format the card in Zephyr without having to prepare the card on a PC. Can you look into this?

ehughes commented 1 month ago

@jerpa77

If you continue to have trouble with this, here is a suggestion:

see if you can get littlefs working without using the zephyr file system abstraction layer. Instead trying to get it working as if the zephyr module sisn't exist and you were just pulling in the littleFS code on your own.

Here is the idea

1.) Create a simple zephyr project without the file system features turned on in kconfig (you can make a simple sample in this repo)

2.) Copy the little FS source directly into the project from the main littleFS repo:

https://github.com/littlefs-project/littlefs

There is a simple sample in the read me. You just have to specify the parameters in the lfs_config struct.

For the read/prog/erase functions, I would just link to raw SD card access functions in zephyr

The sd card test projects should how to read/write raw blocks of 512 bytes:

https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/subsys/sd/sdmmc/src/main.c

You can experiment with these settings:

// block device configuration
.read_size = 16,
.prog_size = 16,
.block_size = 4096,
.block_count = 128,
.cache_size = 16,
.lookahead_size = 16,
.block_cycles = 500,

SD cards can only be accessed 512 bytes at a time.

3.) Another option ozone to single step through the code and see how the zephyr file system abstraction is configuring little FS. there is a layer of code to translate file system calls, perform formats, etc. I think there might be a settings issue.

I think that doing #2 will get you to a solution the fastest as you are using littleFS in a direct manner. You could even "fake" the SD by creating a simulated driver that uses a block of RAM. The idea is to simulate how an SD card would function with only being able to do 512byte read/writes.

jerpa77 commented 1 month ago

I was able to make it run. These are the steps that I followed:

  1. Create an configurations overlay file of the board
  2. Set the following configurations:

CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=8192 CONFIG_FS_LITTLEFS_FC_HEAP_SIZE=8192 CONFIG_DISK_ACCESS=y CONFIG_DISK_DRIVERS=y CONFIG_DISK_DRIVER_SDMMC=y CONFIG_FS_LITTLEFS_BLK_DEV=y CONFIG_FS_LITTLEFS_FMP_DEV=n CONFIG_APP_LITTLEFS_STORAGE_BLK_SDMMC=y

I borrowed them from the prj_blk.conf file, and for the nucleo_h743zi_blk.conf file

jerpa77 commented 1 month ago

It is not necessary to format the SD card using a PC. If the SD is not in the required format, the example code automatically formats it. image

jerpa77 commented 1 month ago

I'm not able to see the content of the SD card using thr LittleFS explorer image