Skip to content

Allow using embedded_storage implementors with littlefs2 by replacing Storage with NorFlash #117

@kleinesfilmroellchen

Description

@kleinesfilmroellchen

embedded_storage seems to be a converging HAL for block/byte-level storage I/O. Just looking at the reverse dependencies gives a good idea of how many HALs and peripheral drives provide an implementation of embedded_storage.

Looking deeper into it, it seems like littlefs2’s storage API is practically identical to embedded_storage::NorFlash (block-sized reads and writes, erase support), which makes a lot of sense given that LittleFS is designed for flash. The only difference are:

  • CACHE_SIZE and LOOKAHEAD_SIZE values, which are related to LittleFS itself, rather than the driver. It seems possible to split this out so that the user has to configure this on the LittleFS instance itself (filesystem, not driver). It may create const parameter hell though, I don’t know :)
  • The error types are distinct. embedded_storage allows for arbitrary driver errors, while littlefs2 requires a predefined set. Most of the errors are for higher-level FS issues and unrelated to driver operations, so this doesn’t seem like a significant thing to change, given that the FS is already generic over the driver (and therefore theoretically over its error type too).
  • On success, functions return the amount of data written/read/erased. embedded_storage forces drivers to always write as much data as requested, which also seems like a reasonable approach for littlefs2 and not like a hassle for driver implementations given that embedded_storage is doing just fine. (This might even reduce the size of that Result type depending on how large the error enum is.)
  • capacity is a dynamic value, rather than a static constant (BLOCK_SIZE in combination with BLOCK_COUNT). Looking at the code that passes this to the C library config, it doesn’t seem like this actually needs to be a constant and making the capacity dynamic appears to be trivial. However, the concept of block sizes is still foreign to NorFlash (so probably many real devices), unfortunately. It seems like a reasonable approach to calculate the block size as LCM(READ_SIZE, WRITE_SIZE, ERASE_SIZE), after all, drivers can always choose to fake their sizes to be larger than required by hardware (and e.g. perform multiple data transfers).

Once I have time next week I’ll hack up an adapter struct for plugging any NorFlash implementation into littlefs2::Storage and report back if there’s any other issues. Otherwise, I’d like for Storage to be replaced, as it is simply unnecessarily cumbersome for users on common platforms that already have robust embedded_storage::NorFlash implementations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions