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;
15
16// Allow dead code, because it isn't required for a given build to have any devices.
17/// Device uniqueness.
18///
19/// As the zephyr devices are statically defined structures, this `Unique` value ensures that the
20/// user is only able to get a single instance of any given device.
21///
22/// Note that some devices in zephyr will require more than one instance of the actual device.  For
23/// example, a [`GpioPin`] will reference a single pin, but the underlying device for the gpio
24/// driver will be shared among then.  Generally, the constructor for the individual device will
25/// call `get_instance_raw()` on the underlying device.
26pub(crate) struct Unique(pub(crate) AtomicBool);
27
28impl Unique {
29    // Note that there are circumstances where these are in zero-initialized memory, so false must
30    // be used here, and the result of `once` inverted.
31    /// Construct a new unique counter.
32    pub(crate) const fn new() -> Unique {
33        Unique(AtomicBool::new(false))
34    }
35
36    /// Indicates if this particular entity can be used.  This function, on a given `Unique` value
37    /// will return true exactly once.
38    pub(crate) fn once(&self) -> bool {
39        // `fetch_add` is likely to be faster than compare_exchage.  This does have the limitation
40        // that `once` is not called more than `usize::MAX` times.
41        !self.0.fetch_or(true, Ordering::AcqRel)
42    }
43}
44
45/// For devices that don't need any associated static data, This NoStatic type will take no space
46/// and generate no code, and has the const constructor needed for the type.
47pub(crate) struct NoStatic;
48
49impl NoStatic {
50    pub(crate) const fn new() -> Self {
51        Self
52    }
53}