zephyr/device/flash.rs
1//! Device wrappers for flash controllers, and flash partitions.
2
3// Note that currently, the flash partition shares the controller, so the underlying operations
4// are not actually safe. Need to rethink how to manage this.
5
6use super::{NoStatic, Unique};
7use crate::raw;
8
9/// A flash controller
10///
11/// This is a wrapper around the `struct device` in Zephyr that represents a flash controller.
12/// Using the flash controller allows flash operations on the entire device. See
13/// [`FlashPartition`] for a wrapper that limits the operation to a partition as defined in the
14/// DT.
15#[allow(dead_code)]
16pub struct FlashController {
17 pub(crate) device: *const raw::device,
18}
19
20impl FlashController {
21 /// Constructor, intended to be called by devicetree generated code.
22 #[allow(dead_code)]
23 pub(crate) unsafe fn new(
24 unique: &Unique,
25 _static: &NoStatic,
26 device: *const raw::device,
27 ) -> Option<FlashController> {
28 if !unique.once() {
29 return None;
30 }
31
32 Some(FlashController { device })
33 }
34}
35
36/// A wrapper for flash partitions. There is no Zephyr struct that corresponds with this
37/// information, which is typically used in a more direct underlying manner.
38#[allow(dead_code)]
39pub struct FlashPartition {
40 /// The underlying controller.
41 #[allow(dead_code)]
42 pub(crate) controller: FlashController,
43 #[allow(dead_code)]
44 pub(crate) offset: u32,
45 #[allow(dead_code)]
46 pub(crate) size: u32,
47}
48
49impl FlashPartition {
50 /// Constructor, intended to be called by devicetree generated code.
51 #[allow(dead_code)]
52 pub(crate) unsafe fn new(
53 unique: &Unique,
54 _static: &NoStatic,
55 device: *const raw::device,
56 offset: u32,
57 size: u32,
58 ) -> Option<FlashPartition> {
59 if !unique.once() {
60 return None;
61 }
62
63 // The `get_instance` on the flash controller would try to guarantee a unique instance,
64 // but in this case, we need one for each device, so just construct it here.
65 // TODO: This is not actually safe.
66 let controller = FlashController { device };
67 Some(FlashPartition {
68 controller,
69 offset,
70 size,
71 })
72 }
73}