Skip to content

Commit 0133bd8

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 f5131db commit 0133bd8

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
@@ -90,6 +90,13 @@ pub mod gpio {
9090
}
9191

9292
impl Gpio {
93+
/// Constructor, used by the devicetree generated code.
94+
///
95+
/// TODO: Guarantee single instancing.
96+
pub unsafe fn new(device: *const raw::device) -> Gpio {
97+
Gpio { device }
98+
}
99+
93100
/// Verify that the device is ready for use. At a minimum, this means the device has been
94101
/// successfully initialized.
95102
pub fn is_ready(&self) -> bool {
@@ -107,6 +114,19 @@ pub mod gpio {
107114
}
108115

109116
impl GpioPin {
117+
/// Constructor, used by the devicetree generated code.
118+
///
119+
/// TODO: Guarantee single instancing.
120+
pub unsafe fn new(device: *const raw::device, pin: u32, dt_flags: u32) -> GpioPin {
121+
GpioPin {
122+
pin: raw::gpio_dt_spec {
123+
port: device,
124+
pin: pin as raw::gpio_pin_t,
125+
dt_flags: dt_flags as raw::gpio_dt_flags_t,
126+
}
127+
}
128+
}
129+
110130
/// Verify that the device is ready for use. At a minimum, this means the device has been
111131
/// successfully initialized.
112132
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)