rktk_drivers_nrf/softdevice/flash.rs
1use embassy_embedded_hal::flash::partition::Partition;
2use nrf_softdevice::{Flash, Softdevice};
3use rktk::utils::{Mutex, RawMutex};
4use sequential_storage::{cache::NoCache, map::MapStorage};
5use static_cell::StaticCell;
6
7pub type SoftdeviceFlashStorage = MapStorage<u8, Partition<'static, RawMutex, Flash>, NoCache>;
8pub type SoftdeviceFlashPartition = Partition<'static, RawMutex, Flash>;
9
10static FLASH: StaticCell<Mutex<Flash>> = StaticCell::new();
11
12/// Take flash from softdevice instance.
13/// When you don't need custom flash partitions, use [`get_typical_flash_partitions`] instead.
14///
15/// This function or [`get_typical_flash_partitions`] must be called only once. Otherwise, it will panic.
16pub fn get_flash(sd: &Softdevice) -> &'static Mutex<Flash> {
17 FLASH.init(Mutex::new(nrf_softdevice::Flash::take(sd)))
18}
19
20const FLASH_START: u32 = 4096 * 160;
21const MAIN_FLASH_SIZE: u32 = 4096 * 3; // 4kb * 3
22const BOND_FLASH_SIZE: u32 = 4096 * 2; // 4kb * 2
23
24/// Take flash and create typical flash partitions for application and bond storage.
25/// Returns (application_partition, bond_partition).
26///
27/// This function or [`get_flash`] must be called only once. Otherwise, it will panic.
28///
29/// Address Size Content / Region Name
30/// +------------+----------+-----------------------------+
31/// | 0x00000000 | | |
32/// | | | 152 KB | SoftDevice |
33/// | 0x00025FFF | | |
34/// +------------+----------+-----------------------------+
35/// | 0x00026000 | | |
36/// | | | 488 KB | Application |
37/// | 0x0009FFFF | | |
38/// +------------+----------+-----------------------------+
39/// | 0x000A0000 | | |
40/// | | | 12 KB | Store app information |
41/// | 0x000A2FFF | | |
42/// +------------+----------+-----------------------------+
43/// | 0x000A3000 | | |
44/// | | | 8 KB | Store bond information |
45/// | 0x000A4FFF | | |
46/// +------------+----------+-----------------------------+
47///
48pub fn get_typical_flash_partitions(
49 sd: &Softdevice,
50) -> (SoftdeviceFlashPartition, SoftdeviceFlashPartition) {
51 let flash = get_flash(sd);
52
53 let partition_main = Partition::new(flash, FLASH_START, MAIN_FLASH_SIZE);
54 let partition_bond = Partition::new(flash, FLASH_START + MAIN_FLASH_SIZE, BOND_FLASH_SIZE);
55
56 (partition_main, partition_bond)
57}
RKTK API Docs