Skip to content

Commit 129332a

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 206bfe7 commit 129332a

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
@@ -96,6 +96,13 @@ pub mod gpio {
9696
}
9797

9898
impl Gpio {
99+
/// Constructor, used by the devicetree generated code.
100+
///
101+
/// TODO: Guarantee single instancing.
102+
pub unsafe fn new(device: *const raw::device) -> Gpio {
103+
Gpio { device }
104+
}
105+
99106
/// Verify that the device is ready for use. At a minimum, this means the device has been
100107
/// successfully initialized.
101108
pub fn is_ready(&self) -> bool {
@@ -113,6 +120,19 @@ pub mod gpio {
113120
}
114121

115122
impl GpioPin {
123+
/// Constructor, used by the devicetree generated code.
124+
///
125+
/// TODO: Guarantee single instancing.
126+
pub unsafe fn new(device: *const raw::device, pin: u32, dt_flags: u32) -> GpioPin {
127+
GpioPin {
128+
pin: raw::gpio_dt_spec {
129+
port: device,
130+
pin: pin as raw::gpio_pin_t,
131+
dt_flags: dt_flags as raw::gpio_dt_flags_t,
132+
}
133+
}
134+
}
135+
116136
/// Verify that the device is ready for use. At a minimum, this means the device has been
117137
/// successfully initialized.
118138
pub fn is_ready(&self) -> bool {
@@ -156,6 +176,15 @@ pub mod flash {
156176
pub(crate) device: *const raw::device,
157177
}
158178

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

0 commit comments

Comments
 (0)