nrf-rs / nrf-hal

A Rust HAL for the nRF family of devices
Apache License 2.0
503 stars 139 forks source link

Provides common Nvmc functionality #337

Closed huntc closed 3 years ago

huntc commented 3 years ago

The embedded-storage traits are implemented to provide a higher-level abstraction for reading and writing from/to flash storage. In the first instance, support is provided for the nRF52 boards and the nRF9160, but other boards should be relatively easy to support if required in the future.

Sample usage of writing to flash given some 32 bit aligned slice of bytes:

// Erase a page
let _ = nvmc.try_erase(0, 4096);

// Write the 32 bit aligned slice
let _ = nvmc.try_write(0, some_u8_slice_on_32_bit_bounds);

An example of establishing the Nvmc on the nRF9160:

let mut nvmc = Nvmc::new(board.NVMC_NS, unsafe { &mut CONFIG });

...where CONFIG points to flash:

extern "C" {
    #[link_name = "_config"]
    static mut CONFIG: [u32; 1024];
}

...which can be expressed in a memory layout:

MEMORY
{
  /* NOTE 1 K = 1 KiBi = 1024 bytes */
  FLASH : ORIGIN = 0x00040000, LENGTH = 764K
  CONFIG : ORIGIN = ORIGIN(FLASH) + LENGTH(FLASH), LENGTH = 4K /* 4K is the flash page size */
  RAM : ORIGIN = 0x20020000, LENGTH = 128K
}

_config = ORIGIN(CONFIG);

Testing on the nRF52840 device can be achieved via cargo test --test nvmc from within the nrf52840-hal-tests folder.

Fixes #336

TODO:

huntc commented 3 years ago

https://github.com/titanclass/thingy91-lorawan-nbiot/pull/2/commits/4a9f6730e329758f4bc7a78be3f221466cbcfb81 shows the positive impact of this abstraction.

huntc commented 3 years ago

@jonas-schievink @Dirbaio We now have tests for the NVMC via nRF52840-hal-tests, and things appear to be working! I want to go through things finely before converting from a draft PR, and write an example. I also want to do some boundary checks via my tests. Your feedback is appreciated!

huntc commented 3 years ago

This PR is complete and ready for a thorough review.

jonas-schievink commented 3 years ago

bors r+

bors[bot] commented 3 years ago

Build succeeded: