Skip to content

Commit 46495c9

Browse files
committed
Numerous minor fixes from github reviews
Apply various minor fixes. None of these should affect the code execution itself. Signed-off-by: David Brown <[email protected]>
1 parent 5d1af26 commit 46495c9

File tree

11 files changed

+46
-105
lines changed

11 files changed

+46
-105
lines changed

samples/blinky/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-License-Identifier: Apache-2.0
1+
# SPDX-License-Identifier: Apache-2.0 OR MIT
22

33
cmake_minimum_required(VERSION 3.20.0)
44
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

samples/blinky/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "rustapp"
77
version = "0.1.0"
88
edition = "2021"
99
description = "Blink an LED forever using the GPIO API"
10-
license = "Apache-2.0 or MIT"
10+
license = "Apache-2.0 OR MIT"
1111

1212
[lib]
1313
crate-type = ["staticlib"]

samples/blinky/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ extern "C" fn rust_main() {
2121
unsafe { zephyr::set_logger().unwrap(); }
2222

2323
warn!("Starting blinky");
24-
// println!("Blinky!");
24+
2525
// Invoke "blink" as a user thread.
26-
// blink();
2726
if false {
2827
unsafe {
2928
zephyr::raw::k_thread_user_mode_enter

samples/blinky/src/main.c

-48
This file was deleted.

zephyr-build/src/devicetree.rs

+28-39
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct DeviceTree {
3535
/// The root of the tree.
3636
root: Rc<Node>,
3737
/// All of the labels.
38+
/// Note that this is a BTree so that the output will be deterministic.
3839
labels: BTreeMap<String, Rc<Node>>,
3940
}
4041

@@ -70,7 +71,7 @@ pub struct Property {
7071
pub enum Value {
7172
Words(Vec<Word>),
7273
Bytes(Vec<u8>),
73-
Phandle(Phandle), // TODO
74+
Phandle(Phandle),
7475
String(String),
7576
}
7677

@@ -123,34 +124,24 @@ impl Node {
123124
}
124125

125126
fn parent_walk(self: &Rc<Self>) {
126-
// *(self.parent.borrow_mut()) = Some(parent.clone());
127-
128127
for child in &self.children {
129128
*(child.parent.borrow_mut()) = Some(self.clone());
130129
child.parent_walk()
131130
}
132131
}
133132

134133
fn is_compatible(&self, name: &str) -> bool {
135-
if let Some(prop) = self.properties.iter().find(|p| p.name == "compatible") {
136-
prop.value.iter().any(|v| {
137-
match v {
138-
Value::String(vn) if name == vn => true,
139-
_ => false,
140-
}
141-
})
142-
} else {
143-
// If there is no compatible field, we are clearly not compatible.
144-
false
145-
}
134+
self.properties
135+
.iter()
136+
.filter(|p| p.name == "compatible")
137+
.flat_map(|prop| prop.value.iter())
138+
.any(|v| matches!(v, Value::String(vn) if name == vn))
146139
}
147140

148141
/// A richer compatible test. Walks a series of names, in reverse. Any that are "Some(x)" must
149142
/// be compatible with "x" at that level.
150143
fn compatible_path(&self, path: &[Option<&str>]) -> bool {
151-
let res = self.path_walk(path, 0);
152-
// println!("compatible? {}: {} {:?}", res, self.path, path);
153-
res
144+
self.path_walk(path, 0)
154145
}
155146

156147
/// Recursive path walk, to make borrowing simpler.
@@ -161,10 +152,8 @@ impl Node {
161152
}
162153

163154
// Check the failure condition, where this node isn't compatible with this section of the path.
164-
if let Some(name) = path[pos] {
165-
if !self.is_compatible(name) {
166-
return false;
167-
}
155+
if matches!(path[pos], Some(name) if !self.is_compatible(name)) {
156+
return false;
168157
}
169158

170159
// Walk down the tree. We have to check for None here, as we can't recurse on the none
@@ -177,19 +166,17 @@ impl Node {
177166
}
178167
}
179168

180-
/// Is the named property present?
169+
/// Returns `true` if there is a property with this name.
181170
fn has_prop(&self, name: &str) -> bool {
182171
self.properties.iter().any(|p| p.name == name)
183172
}
184173

185-
/// Get this property in its entirety.
174+
/// Returns the slice of values of a property with this name as `Some` or `None` if the property
175+
/// does not exist.
186176
fn get_property(&self, name: &str) -> Option<&[Value]> {
187-
for p in &self.properties {
188-
if p.name == name {
189-
return Some(&p.value);
190-
}
191-
}
192-
return None;
177+
self.properties
178+
.iter()
179+
.find_map(|p| if p.name == name { Some(p.value.as_slice()) } else { None })
193180
}
194181

195182
/// Attempt to retrieve the named property, as a single entry of Words.
@@ -244,12 +231,11 @@ impl Node {
244231
impl Value {
245232
fn phandle_walk(&self, labels: &BTreeMap<String, Rc<Node>>) {
246233
match self {
247-
Value::Phandle(ph) => ph.phandle_resolve(labels),
248-
Value::Words(words) => {
234+
Self::Phandle(ph) => ph.phandle_resolve(labels),
235+
Self::Words(words) => {
249236
for w in words {
250-
match w {
251-
Word::Phandle(ph) => ph.phandle_resolve(labels),
252-
_ => (),
237+
if let Word::Phandle(ph) = w {
238+
ph.phandle_resolve(labels);
253239
}
254240
}
255241
}
@@ -260,8 +246,8 @@ impl Value {
260246

261247
impl Phandle {
262248
/// Construct a phandle that is unresolved.
263-
pub fn new(name: String) -> Phandle {
264-
Phandle {
249+
pub fn new(name: String) -> Self {
250+
Self {
265251
name,
266252
node: RefCell::new(None),
267253
}
@@ -286,9 +272,9 @@ impl Phandle {
286272
}
287273

288274
impl Word {
289-
pub fn get_number(&self) -> Option<u32> {
275+
pub fn as_number(&self) -> Option<u32> {
290276
match self {
291-
Word::Number(n) => Some(*n),
277+
Self::Number(n) => Some(*n),
292278
_ => None,
293279
}
294280
}
@@ -297,6 +283,9 @@ impl Word {
297283
// To avoid recursion, the debug printer for Phandle just prints the name.
298284
impl std::fmt::Debug for Phandle {
299285
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
300-
write!(fmt, "Phandle({:?})", self.name)
286+
fmt
287+
.debug_struct("Phandle")
288+
.field("name", &self.name)
289+
.finish_non_exhaustive()
301290
}
302291
}

zephyr-build/src/devicetree/augment.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl RawInfo {
191191
fn generate(&self, node: &Node, device: &str) -> TokenStream {
192192
let device_id = str_to_path(device);
193193
match self {
194-
RawInfo::Myself => {
194+
Self::Myself => {
195195
let ord = node.ord;
196196
let rawdev = format_ident!("__device_dts_ord_{}", ord);
197197
quote! {
@@ -209,7 +209,7 @@ impl RawInfo {
209209
}
210210
}
211211
}
212-
RawInfo::Phandle(pname) => {
212+
Self::Phandle(pname) => {
213213
let words = node.get_words(pname).unwrap();
214214
// We assume that elt 0 is the phandle, and that the rest are numbers.
215215
let target = if let Word::Phandle(handle) = &words[0] {
@@ -221,7 +221,7 @@ impl RawInfo {
221221
// TODO: We would try to correlate with parent node's notion of number of cells, and
222222
// try to handle cases where there is more than one reference. It is unclear when
223223
// this will be needed.
224-
let args: Vec<u32> = words[1..].iter().map(|n| n.get_number().unwrap()).collect();
224+
let args: Vec<u32> = words[1..].iter().map(|n| n.as_number().unwrap()).collect();
225225

226226
let target_route = target.route_to_rust();
227227

@@ -235,7 +235,7 @@ impl RawInfo {
235235
}
236236
}
237237
}
238-
RawInfo::Parent { level, args } => {
238+
Self::Parent { level, args } => {
239239
let get_args = args.iter().map(|arg| arg.args(node));
240240

241241
assert!(*level > 0);

zephyr-build/src/devicetree/output.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,8 @@ impl DeviceTree {
102102
pub const #tag: u32 = #n;
103103
};
104104
}
105-
_ => return general_property(prop),
105+
_ => (),
106106
}
107-
} else {
108-
return general_property(prop);
109107
}
110108
}
111109
Value::Phandle(ref ph) => {
@@ -118,7 +116,7 @@ impl DeviceTree {
118116
}
119117
}
120118
}
121-
_ => return general_property(prop),
119+
_ => (),
122120
}
123121
}
124122
general_property(prop)

zephyr-sys/wrapper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
/*
15-
* This is getting build with KERNEL defined, which causes syscalls to not be implemented. Work
15+
* This is getting built with KERNEL defined, which causes syscalls to not be implemented. Work
1616
* around this by just undefining this symbol.
1717
*/
1818
#undef KERNEL

zephyr/src/device.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
//!
66
//! Most of these instances come from the device tree.
77
8+
// Allow for a Zephyr build that has no devices at all.
9+
#![allow(dead_code)]
10+
811
use crate::sync::atomic::{AtomicBool, Ordering};
912

1013
pub mod gpio;
@@ -20,7 +23,6 @@ pub mod flash;
2023
/// example, a [`GpioPin`] will reference a single pin, but the underlying device for the gpio
2124
/// driver will be shared among then. Generally, the constructor for the individual device will
2225
/// call `get_instance_raw()` on the underlying device.
23-
#[allow(dead_code)]
2426
pub(crate) struct Unique(pub(crate) AtomicBool);
2527

2628
impl Unique {
@@ -33,7 +35,6 @@ impl Unique {
3335

3436
/// Indicates if this particular entity can be used. This function, on a given `Unique` value
3537
/// will return true exactly once.
36-
#[allow(dead_code)]
3738
pub(crate) fn once(&self) -> bool {
3839
// `fetch_add` is likely to be faster than compare_exchage. This does have the limitation
3940
// that `once` is not called more than `usize::MAX` times.

zephyr/src/device/flash.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Device wrappers for flash controllers, and flash partitions.
22
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+
36
use crate::raw;
47
use super::Unique;
58

@@ -54,6 +57,3 @@ impl FlashPartition {
5457
Some(FlashPartition { controller, offset, size })
5558
}
5659
}
57-
58-
// Note that currently, the flash partition shares the controller, so the underlying operations
59-
// are not actually safe. Need to rethink how to manage this.

zephyr/src/device/gpio.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use super::Unique;
1212

1313
/// Global instance to help make gpio in Rust slightly safer.
1414
///
15+
/// # Safety
16+
///
1517
/// To help with safety, the rust types use a global instance of a gpio-token. Methods will
1618
/// take a mutable reference to this, which will require either a single thread in the
1719
/// application code, or something like a mutex or critical section to manage. The operation

0 commit comments

Comments
 (0)