stm32-rs / stm32f0xx-hal

A Rust `embedded-hal` implementation for all MCUs in the STM32 F0 family
BSD Zero Clause License
134 stars 59 forks source link

Implement interface to internal flash storage #172

Closed JanekGraff closed 11 months ago

JanekGraff commented 11 months ago

This implements an interface to reading and writing to the internal flash memory. This implements the NorFlash traits from the embedded-storage crate.

The interface is inspired by the interfaces used in the stm32f4xx-hal and the stm32g0xx-hal.

The interface could definitely use some clean up and I'm open to suggestions on that matter. Especially the handling of writing to addresses not aligned to the native write size is something I'm not sure about. The stm32g0xx-hal has a different approach for this, but their approach has some problems in my opinion:

  1. Trying to write data that is shorter than the unaligned size (e.g. writing 3 Bytes to 0x08008001) will lead to a panic due to the slice index being out of range.
  2. (I'm not 100% sure about this) It seems like they will write unaligned data to an address that is in front of the address given by the user, which is not intuitive in my opinion.
  3. Passing an unaligned address to the write function will lead to some of the data in front of the address being overwritten.

The reasons above are why i choose the approach of returning an Error instead of trying to pad the data into native writes.

I also provided a basic example of how to use the flash interface.

The CI checks of this PR will fail until #170 (or an alternative solution) is merged.