nRF52832 internal flash memory with littlefs

Hi,

we are having issue with nRF52832 and littlefs. We are using Nordic Connect SDK 1.9.1 and Visual Studio Code.

We created dts overlay and removed partitions not needed and expanded storage_partition so it takes all available space, in our case it was 256KB and first 256KB is for image.
We are using bluetooth for stored data transfer and some other data processing. 

Idea was to write some struct (in our case is 40B) to file by advancing file index (fs_seek) by the amount of struct that we need to store, and when flash is full index is set to 0. Some sort of ring storage file. Data is written approximately each 400ms on bursts and can be idle for many hours.

Up to this point everything was working fine, reading and writing.

The problem occurs when function fs_write returns error -ENOSPC. Then file writing index is set to 0 to continue writing to the beginning of file. Every following call to fs_write return success, but function fs_close returns error -ENOSPC. This is happening to all following writes.

Next we tried several approaches to fix this problem.

We tried to use only smaller portion of flash for storage (64KB) and this happend again. 
Then we tried to truncate file (
fs_truncate) for last unsuccessful write to free some space and set index to 0 and this also returned error on next write.

Interesting thing happened on last attempt. We used all available storage for file system (256KB) and limit file index size to half of available storage, this means file size was max 128KB and this leaves us another 128KB free space in file system. When we hit this limit, index is set to 0 to continue writing to beginning of file. And then writing was fine up to 4K and then fs_write returns error -ENOSPC. 

Also some combination of truncating file, using smaller file system or file size was attempted with no success.

On every attempt reading was working fine. Data we are writing was not consistent any more but can be readed.


What we are doing wrong?

In examples there is basically same thing we are trying to achieve when increasing boot count value except seek to beginning is used when we hit some condition.
https://github.com/nrfconnect/sdk-zephyr/blob/main/samples/subsys/fs/littlefs/src/main.c

Thank you!

Parents
  • Hi,

    -ENOSPC indicate that there is no more space, and this corresponds to LFS_ERR_NOSPC. I do not notice anything sticking out as incorrect in what you describe. Can you enable logging and also add CONFIG_FS_LOG_LEVEL_DBG=y and see if the log revels anything?

    If you don't make progress it would be interesting to test on my end if you have a minimal failing example that runs on a DK so that I can test on my end.

  • CONFIG_FS_LOG_LEVEL_DBG=y did not reveal anything new.

    This is my log:

    This is first unsuccessful write at data file index 12240. It fails on fs_write():

    [00:03:39.664,245] <dbg> main.dataDetected: newDataDetected
    [00:03:39.669,860] <dbg> dataStorage.writeData: seek data to: 12240, 0
    [00:03:39.940,765] <err> littlefs: WEST_TOPDIR/modules/fs/littlefs/lfs.c:567: No more free space 10
    [00:03:39.940,795] <err> fs: file write error (-28)
    [00:03:39.940,795] <dbg> dataStorage.writeData: write new data to: 12240, -28
    [00:03:39.940,826] <err> dataStorage: data write error, no more space left: -28


    Set data file index to 0, retry to write, fs_write succeded but fails on fs_close
    From this point on fs_write suceeds to write, data file index is incremented, fs_close fails with error -28 (-ENOSPC)

    [00:03:39.946,380] <dbg> dataStorage.writeData: seek data to: 0, 0
    [00:03:40.092,834] <dbg> dataStorage.writeData: write new data to: 0, 40
    [00:03:40.292,480] <err> littlefs: WEST_TOPDIR/modules/fs/littlefs/lfs.c:567: No more free space 10
    [00:03:40.292,510] <err> fs: file close error (-28)
    [00:03:40.292,541] <err> dataStorage: File close error: -28
    [00:03:40.292,541] <dbg> dataStorage.dataStorage_addData: data write #2 sucess: 40
    [00:03:40.298,065] <dbg> dataStorage.writeDataFileIndex: write new data_file_index: 40, ret: 4
    
    [00:03:40.809,844] <dbg> main.dataDetected: newDataDetected
    [00:03:40.815,765] <dbg> dataStorage.writeData: seek data to: 40, 0
    [00:03:40.994,689] <dbg> dataStorage.writeData: write new data to: 40, 40
    [00:03:41.192,596] <err> littlefs: WEST_TOPDIR/modules/fs/littlefs/lfs.c:567: No more free space 10
    [00:03:41.192,626] <err> fs: file close error (-28)
    [00:03:41.192,626] <err> dataStorage: File close error: -28
    [00:03:41.198,211] <dbg> dataStorage.writeDataFileIndex: write new data_file_index: 80, ret: 4
    
    [00:03:41.699,188] <dbg> main.dataDetected: newDataDetected
    [00:03:41.704,833] <dbg> dataStorage.writeData: seek data to: 80, 0
    [00:03:41.834,899] <dbg> dataStorage.writeData: write new data to: 80, 40
    [00:03:42.032,684] <err> littlefs: WEST_TOPDIR/modules/fs/littlefs/lfs.c:567: No more free space 10
    [00:03:42.032,714] <err> fs: file close error (-28)
    [00:03:42.032,714] <err> dataStorage: File close error: -28
    [00:03:42.038,299] <dbg> dataStorage.writeDataFileIndex: write new data_file_index: 120, ret: 4

    In this case file system is 24kB in size, but this write fails on 12kB.

    I will prepare an example.

  • Here is sample:
    https://github.com/davorinm/littlefsSample

    Board used nrf52dk_nrf52832.
    Dtk is not modified, storage partition is 24kB.

    After about a minute write will begin to fail.

    Thank you!

Reply Children
Related