RKTK API Docs RKTK Home Repo

rktk_drivers_nrf/softdevice/
flash.rs

1use nrf_softdevice::{Flash, Softdevice};
2use rktk::utils::Mutex;
3use rktk_drivers_common::storage::flash_sequential_map::{
4    sequential_storage::cache::NoCache, FlashSequentialMapStorage,
5};
6use static_cell::StaticCell;
7
8pub type SharedFlash = Mutex<Flash>;
9
10static FLASH: StaticCell<SharedFlash> = StaticCell::new();
11
12/// Get steal from softdevice instance.
13///
14/// This function must be called only once. Otherwise, it will panic.
15pub fn get_flash(sd: &Softdevice) -> (&'static SharedFlash, Mutex<NoCache>) {
16    (
17        FLASH.init(Mutex::new(nrf_softdevice::Flash::take(sd))),
18        Mutex::new(NoCache::new()),
19    )
20}
21
22// 4kb * 160 = 640kb
23const FLASH_START: u32 = 4096 * 160;
24// 4kb * 3 = 12kb
25const FLASH_END: u32 = FLASH_START + 4096 * 3;
26
27pub fn create_storage_driver<'a>(
28    flash: &'a SharedFlash,
29    cache: &'a Mutex<NoCache>,
30) -> FlashSequentialMapStorage<'a, Flash> {
31    FlashSequentialMapStorage {
32        flash,
33        flash_range: FLASH_START..FLASH_END,
34        cache,
35    }
36}