Skip to content

Commit a73e9e2

Browse files
committed
zephyr: Add unsafe constructor to device types
Add constructors to the individual device types. These are unsafe, and are all referenced from the generated devicetree code.
1 parent c406a3f commit a73e9e2

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

zephyr/src/sys.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ pub mod gpio {
9494
}
9595

9696
impl Gpio {
97+
/// Constructor, used by the devicetree generated code.
98+
///
99+
/// TODO: Guarantee single instancing.
100+
pub unsafe fn new(device: *const raw::device) -> Gpio {
101+
Gpio { device }
102+
}
103+
97104
/// Verify that the device is ready for use. At a minimum, this means the device has been
98105
/// successfully initialized.
99106
pub fn is_ready(&self) -> bool {
@@ -111,6 +118,19 @@ pub mod gpio {
111118
}
112119

113120
impl GpioPin {
121+
/// Constructor, used by the devicetree generated code.
122+
///
123+
/// TODO: Guarantee single instancing.
124+
pub unsafe fn new(device: *const raw::device, pin: u32, dt_flags: u32) -> GpioPin {
125+
GpioPin {
126+
pin: raw::gpio_dt_spec {
127+
port: device,
128+
pin: pin as raw::gpio_pin_t,
129+
dt_flags: dt_flags as raw::gpio_dt_flags_t,
130+
}
131+
}
132+
}
133+
114134
/// Verify that the device is ready for use. At a minimum, this means the device has been
115135
/// successfully initialized.
116136
pub fn is_ready(&self) -> bool {
@@ -160,6 +180,15 @@ pub mod flash {
160180
pub(crate) device: *const raw::device,
161181
}
162182

183+
impl FlashController {
184+
/// Constructor, intended to be called by devicetree generated code.
185+
///
186+
/// TODO: Instance safety
187+
pub unsafe fn new(device: *const raw::device) -> FlashController {
188+
FlashController { device }
189+
}
190+
}
191+
163192
/// A wrapper for flash partitions. There is no Zephyr struct that corresponds with this
164193
/// information, which is typically used in a more direct underlying manner.
165194
#[allow(dead_code)]
@@ -172,4 +201,14 @@ pub mod flash {
172201
#[allow(dead_code)]
173202
pub(crate) size: u32,
174203
}
204+
205+
impl FlashPartition {
206+
/// Constructor, intended to be called by devicetree generated code.
207+
pub unsafe fn new(device: *const raw::device, offset: u32, size: u32) -> FlashPartition {
208+
// The `get_instance` on the flash controller would try to guarantee a unique instance,
209+
// but in this case, we need one for each device, so just construct it here.
210+
let controller = FlashController::new(device);
211+
FlashPartition { controller, offset, size }
212+
}
213+
}
175214
}

0 commit comments

Comments
 (0)