Skip to main content
RKTK API Docs RKTK Home Repo

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