Skip to content

Commit b09a3f0

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 26f4475 commit b09a3f0

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 {
@@ -154,6 +174,15 @@ pub mod flash {
154174
pub(crate) device: *const raw::device,
155175
}
156176

177+
impl FlashController {
178+
/// Constructor, intended to be called by devicetree generated code.
179+
///
180+
/// TODO: Instance safety
181+
pub unsafe fn new(device: *const raw::device) -> FlashController {
182+
FlashController { device }
183+
}
184+
}
185+
157186
/// A wrapper for flash partitions. There is no Zephyr struct that corresponds with this
158187
/// information, which is typically used in a more direct underlying manner.
159188
#[allow(dead_code)]
@@ -166,4 +195,14 @@ pub mod flash {
166195
#[allow(dead_code)]
167196
pub(crate) size: u32,
168197
}
198+
199+
impl FlashPartition {
200+
/// Constructor, intended to be called by devicetree generated code.
201+
pub unsafe fn new(device: *const raw::device, offset: u32, size: u32) -> FlashPartition {
202+
// The `get_instance` on the flash controller would try to guarantee a unique instance,
203+
// but in this case, we need one for each device, so just construct it here.
204+
let controller = FlashController::new(device);
205+
FlashPartition { controller, offset, size }
206+
}
207+
}
169208
}

0 commit comments

Comments
 (0)