rtic/lib.rs
1//! Real-Time Interrupt-driven Concurrency (RTIC) framework for ARM Cortex-M microcontrollers.
2//!
3//! **IMPORTANT**: This crate is published as [`cortex-m-rtic`] on crates.io but the name of the
4//! library is `rtic`.
5//!
6//! [`cortex-m-rtic`]: https://crates.io/crates/cortex-m-rtic
7//!
8//! The user level documentation can be found [here].
9//!
10//! [here]: https://rtic.rs
11//!
12//! Don't forget to check the documentation of the `#[app]` attribute (listed under the reexports
13//! section), which is the main component of the framework.
14//!
15//! # Minimum Supported Rust Version (MSRV)
16//!
17//! This crate is compiled and tested with the latest toolchain (rolling) as of the release date.
18//! If you run into compilation errors, try the latest stable release of the rust toolchain.
19//!
20//! # Semantic Versioning
21//!
22//! Like the Rust project, this crate adheres to [SemVer]: breaking changes in the API and semantics
23//! require a *semver bump* (since 1.0.0 a new major version release), with the exception of breaking changes
24//! that fix soundness issues -- those are considered bug fixes and can be landed in a new patch
25//! release.
26//!
27//! [SemVer]: https://semver.org/spec/v2.0.0.html
28
29#![deny(missing_docs)]
30#![deny(rust_2021_compatibility)]
31#![deny(rust_2018_compatibility)]
32#![deny(rust_2018_idioms)]
33#![no_std]
34#![doc(
35 html_logo_url = "https://raw.githubusercontent.com/rtic-rs/cortex-m-rtic/master/book/en/src/RTIC.svg",
36 html_favicon_url = "https://raw.githubusercontent.com/rtic-rs/cortex-m-rtic/master/book/en/src/RTIC.svg"
37)]
38//deny_warnings_placeholder_for_ci
39#![allow(clippy::inline_always)]
40
41use cortex_m::{interrupt::InterruptNumber, peripheral::NVIC};
42pub use cortex_m_rtic_macros::app;
43pub use rtic_core::{prelude as mutex_prelude, Exclusive, Mutex};
44pub use rtic_monotonic::{self, Monotonic};
45
46/// module `mutex::prelude` provides `Mutex` and multi-lock variants. Recommended over `mutex_prelude`
47pub mod mutex {
48 pub use rtic_core::prelude;
49 pub use rtic_core::Mutex;
50}
51
52#[doc(hidden)]
53pub mod export;
54#[doc(hidden)]
55mod tq;
56
57/// Sets the given `interrupt` as pending
58///
59/// This is a convenience function around
60/// [`NVIC::pend`](../cortex_m/peripheral/struct.NVIC.html#method.pend)
61pub fn pend<I>(interrupt: I)
62where
63 I: InterruptNumber,
64{
65 NVIC::pend(interrupt);
66}
67
68use core::cell::UnsafeCell;
69
70/// Internal replacement for `static mut T`
71///
72/// Used to represent RTIC Resources
73///
74/// Soundness:
75/// 1) Unsafe API for internal use only
76/// 2) ``get_mut(&self) -> *mut T``
77/// returns a raw mutable pointer to the inner T
78/// casting to &mut T is under control of RTIC
79/// RTIC ensures &mut T to be unique under Rust aliasing rules.
80///
81/// Implementation uses the underlying ``UnsafeCell<T>``
82/// self.0.get() -> *mut T
83///
84/// 3) get(&self) -> *const T
85/// returns a raw immutable (const) pointer to the inner T
86/// casting to &T is under control of RTIC
87/// RTIC ensures &T to be shared under Rust aliasing rules.
88///
89/// Implementation uses the underlying ``UnsafeCell<T>``
90/// self.0.get() -> *mut T, demoted to *const T
91///
92#[repr(transparent)]
93pub struct RacyCell<T>(UnsafeCell<T>);
94
95impl<T> RacyCell<T> {
96 /// Create a ``RacyCell``
97 #[inline(always)]
98 pub const fn new(value: T) -> Self {
99 RacyCell(UnsafeCell::new(value))
100 }
101
102 /// Get `*mut T`
103 ///
104 /// # Safety
105 ///
106 /// See documentation notes for [`RacyCell`]
107 #[inline(always)]
108 pub unsafe fn get_mut(&self) -> *mut T {
109 self.0.get()
110 }
111
112 /// Get `*const T`
113 ///
114 /// # Safety
115 ///
116 /// See documentation notes for [`RacyCell`]
117 #[inline(always)]
118 pub unsafe fn get(&self) -> *const T {
119 self.0.get()
120 }
121}
122
123unsafe impl<T> Sync for RacyCell<T> {}