|
1 |
| -#![allow(clippy::needless_doctest_main)] |
2 |
| -//! Core peripherals |
| 1 | +//! Core peripherals. |
3 | 2 | //!
|
4 | 3 | //! # API
|
5 | 4 | //!
|
|
9 | 8 | //! the [`Peripherals::take`](struct.Peripherals.html#method.take) method.
|
10 | 9 | //!
|
11 | 10 | //! ``` no_run
|
12 |
| -//! use cortex_m::peripheral::Peripherals; |
13 |
| -//! |
14 |
| -//! fn main() { |
15 |
| -//! let mut peripherals = Peripherals::take().unwrap(); |
16 |
| -//! peripherals.DWT.enable_cycle_counter(); |
17 |
| -//! } |
| 11 | +//! # use cortex_m::peripheral::Peripherals; |
| 12 | +//! let mut peripherals = Peripherals::take().unwrap(); |
| 13 | +//! peripherals.DWT.enable_cycle_counter(); |
18 | 14 | //! ```
|
19 | 15 | //!
|
20 | 16 | //! This method can only be successfully called *once* -- this is why the method returns an
|
21 | 17 | //! `Option`. Subsequent calls to the method will result in a `None` value being returned.
|
22 | 18 | //!
|
23 |
| -//! ``` no_run |
24 |
| -//! use cortex_m::peripheral::Peripherals; |
25 |
| -//! |
26 |
| -//! fn main() { |
27 |
| -//! let ok = Peripherals::take().unwrap(); |
28 |
| -//! let panics = Peripherals::take().unwrap(); |
29 |
| -//! } |
| 19 | +//! ``` no_run, should_panic |
| 20 | +//! # use cortex_m::peripheral::Peripherals; |
| 21 | +//! let ok = Peripherals::take().unwrap(); |
| 22 | +//! let panics = Peripherals::take().unwrap(); |
30 | 23 | //! ```
|
31 | 24 | //! A part of the peripheral API doesn't require access to a peripheral instance. This part of the
|
32 | 25 | //! API is provided as static methods on the peripheral types. One example is the
|
33 | 26 | //! [`DWT::get_cycle_count`](struct.DWT.html#method.get_cycle_count) method.
|
34 | 27 | //!
|
35 | 28 | //! ``` no_run
|
36 |
| -//! use cortex_m::peripheral::{DWT, Peripherals}; |
37 |
| -//! |
38 |
| -//! fn main() { |
39 |
| -//! { |
40 |
| -//! let mut peripherals = Peripherals::take().unwrap(); |
41 |
| -//! peripherals.DWT.enable_cycle_counter(); |
42 |
| -//! } // all the peripheral singletons are destroyed here |
| 29 | +//! # use cortex_m::peripheral::{DWT, Peripherals}; |
| 30 | +//! { |
| 31 | +//! let mut peripherals = Peripherals::take().unwrap(); |
| 32 | +//! peripherals.DWT.enable_cycle_counter(); |
| 33 | +//! } // all the peripheral singletons are destroyed here |
43 | 34 | //!
|
44 |
| -//! // but this method can be called without a DWT instance |
45 |
| -//! let cyccnt = DWT::get_cycle_count(); |
46 |
| -//! } |
| 35 | +//! // but this method can be called without a DWT instance |
| 36 | +//! let cyccnt = DWT::get_cycle_count(); |
47 | 37 | //! ```
|
48 | 38 | //!
|
49 | 39 | //! The singleton property can be *unsafely* bypassed using the `ptr` static method which is
|
50 | 40 | //! available on all the peripheral types. This method is a useful building block for implementing
|
51 | 41 | //! safe higher level abstractions.
|
52 | 42 | //!
|
53 | 43 | //! ``` no_run
|
54 |
| -//! use cortex_m::peripheral::{DWT, Peripherals}; |
55 |
| -//! |
56 |
| -//! fn main() { |
57 |
| -//! { |
58 |
| -//! let mut peripherals = Peripherals::take().unwrap(); |
59 |
| -//! peripherals.DWT.enable_cycle_counter(); |
60 |
| -//! } // all the peripheral singletons are destroyed here |
| 44 | +//! # use cortex_m::peripheral::{DWT, Peripherals}; |
| 45 | +//! { |
| 46 | +//! let mut peripherals = Peripherals::take().unwrap(); |
| 47 | +//! peripherals.DWT.enable_cycle_counter(); |
| 48 | +//! } // all the peripheral singletons are destroyed here |
61 | 49 | //!
|
62 |
| -//! // actually safe because this is an atomic read with no side effects |
63 |
| -//! let cyccnt = unsafe { (*DWT::ptr()).cyccnt.read() }; |
64 |
| -//! } |
| 50 | +//! // actually safe because this is an atomic read with no side effects |
| 51 | +//! let cyccnt = unsafe { (*DWT::ptr()).cyccnt.read() }; |
65 | 52 | //! ```
|
66 | 53 | //!
|
67 | 54 | //! # References
|
@@ -138,6 +125,10 @@ pub struct Peripherals {
|
138 | 125 |
|
139 | 126 | /// Trace Port Interface Unit (not present on Cortex-M0 variants)
|
140 | 127 | pub TPIU: TPIU,
|
| 128 | + |
| 129 | + // Private field making `Peripherals` non-exhaustive. We don't use `#[non_exhaustive]` so we |
| 130 | + // can support older Rust versions. |
| 131 | + _priv: (), |
141 | 132 | }
|
142 | 133 |
|
143 | 134 | // NOTE `no_mangle` is used here to prevent linking different minor versions of this crate as that
|
@@ -200,6 +191,7 @@ impl Peripherals {
|
200 | 191 | TPIU: TPIU {
|
201 | 192 | _marker: PhantomData,
|
202 | 193 | },
|
| 194 | + _priv: (), |
203 | 195 | }
|
204 | 196 | }
|
205 | 197 | }
|
|
0 commit comments