littlefs-project / littlefs

A little fail-safe filesystem designed for microcontrollers
BSD 3-Clause "New" or "Revised" License
4.95k stars 775 forks source link

LittleFS setup on NOR flash #122

Open ste-camp opened 5 years ago

ste-camp commented 5 years ago

Hello, I need some help setting up LittleFS. I'm programming a custom OS for embedded devices and I'm trying to integrate little fs in it for flash filesystem management, but somehow I can't get to make it work properly and documentation is not very detailed on the meaning of the config parameters: I have an SPI NOR Flash (this one Winbond Electronics W25Q64FVSSIG ) and this is my config code:

const struct lfs_config cfg = {
        // block device operations
        .read  = lfs_read_wrapper,
        .prog  = lfs_write_wrapper,
        .erase = lfs_erase_wrapper,
        .sync  = lfs_sync_wrapper,

        // block device configuration
        .read_size = (256),
        .prog_size = (256),
        .block_size = (4096),
        .block_count = 2048,
        .lookahead = 256,
};

The error I'm getting is that even after I completely erase the flash chip and call lfs_format() I get LFS_ERR_NOSPC error, but makes no sense because the chip has just been deleted.

Am I doing something wrong? Or maybe lfs doesn't support this kind of flash?

Thank you

FabianInostroza commented 5 years ago

I had a similar issue, I solved it by adding some delay after/before the CS toggling.

michaelkollmann commented 5 years ago

I had a similar issue, I solved it by adding some delay after/before the CS toggling.

After / before the CS toggling. What's a "CS toggling"?

FabianInostroza commented 5 years ago

@michaelkollmann The assertion/deassertion of the flash CS (or chip enable) pin between SPI transfers.

For the record, I faced a similar issue on a STM32L0 using libopencm3, the cause was bug in the silicon (it corrupted the last bit in the data register), see section 2.6.3 of the STM32L058 errata.

michaelkollmann commented 5 years ago

Thanks! I'm pretty new to the whole embedded development world, that's why I was confused by the CS thingy.

The solution to the problem I was having was, that the SPI driver on my cpu only allowed a maximum data length of 255B. In the littlefs config I set the page sitze to 256B. This resulted in an invalid cast which caused the read and write to fail.

All I had to do was to separate each read and write call into two separate calls to my memory, offset by 128B.

geky commented 5 years ago

@michaelkollmann, glad you were able to figure it out! The SPI flash protocol also has ~3 bytes for the address, so that is probably why you were exceeding 256B.

I faced a similar issue on a STM32L0 using libopencm3, the cause was bug in the silicon

oof!


@ste-camp are you still running into problems? Your configuration looks correct. Do you have assertions enabled (should be on by default). With that config littlefs should not error with ENOSPC in lfs_format. I'm guessing it's an issue with the connection to the block device as @FabianInostroza pointed out. Does your 'lfs_read_wrapper' functions return their own errors?

FabianInostroza commented 5 years ago

Today I found that the delay I added to the spi functions was just a bad workaround for other issue. I needed to check the flash busy bit before reading, the delay I added gave time to the flash to finish the write/erase operations. By checking the busy bit before reading the flash the program works fine without the delays.