zephyr/device.rs
1//! Device wrappers
2//!
3//! This module contains implementations of wrappers for various types of devices in zephyr. In
4//! general, these wrap a `*const device` from Zephyr, and provide an API that is appropriate.
5//!
6//! Most of these instances come from the device tree.
7
8// Allow for a Zephyr build that has no devices at all.
9#![allow(dead_code)]
10
11use crate::sync::atomic::{AtomicBool, Ordering};
12
13pub mod flash;
14pub mod gpio;
15pub mod i2c;
16pub mod led;
17pub mod led_strip;
18
19// Allow dead code, because it isn't required for a given build to have any devices.
20/// Device uniqueness.
21///
22/// As the zephyr devices are statically defined structures, this `Unique` value ensures that the
23/// user is only able to get a single instance of any given device.
24///
25/// Note that some devices in zephyr will require more than one instance of the actual device. For
26/// example, a [`GpioPin`] will reference a single pin, but the underlying device for the gpio
27/// driver will be shared among then. Generally, the constructor for the individual device will
28/// call `get_instance_raw()` on the underlying device.
29pub(crate) struct Unique(pub(crate) AtomicBool);
30
31impl Unique {
32 // Note that there are circumstances where these are in zero-initialized memory, so false must
33 // be used here, and the result of `once` inverted.
34 /// Construct a new unique counter.
35 pub(crate) const fn new() -> Unique {
36 Unique(AtomicBool::new(false))
37 }
38
39 /// Indicates if this particular entity can be used. This function, on a given `Unique` value
40 /// will return true exactly once.
41 pub(crate) fn once(&self) -> bool {
42 // `fetch_add` is likely to be faster than compare_exchage. This does have the limitation
43 // that `once` is not called more than `usize::MAX` times.
44 !self.0.fetch_or(true, Ordering::AcqRel)
45 }
46}
47
48/// For devices that don't need any associated static data, This NoStatic type will take no space
49/// and generate no code, and has the const constructor needed for the type.
50pub struct NoStatic;
51
52impl NoStatic {
53 pub(crate) const fn new() -> Self {
54 Self
55 }
56}