stm32f4xx_hal/
lib.rs

1//! Multi device hardware abstraction on top of the peripheral access API for the STMicro STM32F4 series microcontrollers.
2//!
3//! ## Feature flags
4#![doc = document_features::document_features!()]
5#![no_std]
6#![allow(non_camel_case_types)]
7
8use enumflags2::{BitFlag, BitFlags};
9
10pub use embedded_hal as hal;
11pub use embedded_hal_02 as hal_02;
12
13pub use nb;
14pub use nb::block;
15
16#[cfg(feature = "stm32f401")]
17/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f401 peripherals.
18pub use stm32f4::stm32f401 as pac;
19
20#[cfg(feature = "stm32f405")]
21/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f405 peripherals.
22pub use stm32f4::stm32f405 as pac;
23
24#[cfg(feature = "stm32f407")]
25/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f407 peripherals.
26pub use stm32f4::stm32f407 as pac;
27
28#[cfg(feature = "stm32f410")]
29/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f410 peripherals.
30pub use stm32f4::stm32f410 as pac;
31
32#[cfg(feature = "stm32f411")]
33/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f411 peripherals.
34pub use stm32f4::stm32f411 as pac;
35
36#[cfg(feature = "stm32f412")]
37/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f412 peripherals.
38pub use stm32f4::stm32f412 as pac;
39
40#[cfg(feature = "stm32f413")]
41/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f413 peripherals.
42pub use stm32f4::stm32f413 as pac;
43
44#[cfg(feature = "stm32f415")]
45/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f405 peripherals.
46pub use stm32f4::stm32f405 as pac;
47
48#[cfg(feature = "stm32f417")]
49/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f407 peripherals.
50pub use stm32f4::stm32f407 as pac;
51
52#[cfg(feature = "stm32f423")]
53/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f413 peripherals.
54pub use stm32f4::stm32f413 as pac;
55
56#[cfg(feature = "stm32f427")]
57/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f427 peripherals.
58pub use stm32f4::stm32f427 as pac;
59
60#[cfg(feature = "stm32f429")]
61/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f429 peripherals.
62pub use stm32f4::stm32f429 as pac;
63
64#[cfg(feature = "stm32f437")]
65/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f427 peripherals.
66pub use stm32f4::stm32f427 as pac;
67
68#[cfg(feature = "stm32f439")]
69/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f429 peripherals.
70pub use stm32f4::stm32f429 as pac;
71
72#[cfg(feature = "stm32f446")]
73/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f446 peripherals.
74pub use stm32f4::stm32f446 as pac;
75
76#[cfg(feature = "stm32f469")]
77/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f469 peripherals.
78pub use stm32f4::stm32f469 as pac;
79
80#[cfg(feature = "stm32f479")]
81/// Re-export of the [svd2rust](https://crates.io/crates/svd2rust) auto-generated API for the stm32f469 peripherals.
82pub use stm32f4::stm32f469 as pac;
83
84// Enable use of interrupt macro
85pub use crate::pac::interrupt;
86
87pub mod adc;
88pub mod bb;
89#[cfg(all(feature = "can", any(feature = "can1", feature = "can2")))]
90pub mod can;
91pub mod crc32;
92#[cfg(feature = "dac")]
93pub mod dac;
94#[cfg(feature = "fmpi2c1")]
95pub mod fmpi2c;
96pub mod gpio;
97pub mod i2c;
98pub mod i2s;
99#[cfg(all(feature = "usb_fs", feature = "otg-fs"))]
100pub mod otg_fs;
101#[cfg(all(any(feature = "usb_hs", docsrs), feature = "otg-hs"))]
102pub mod otg_hs;
103
104#[cfg(feature = "rng")]
105pub mod rng;
106
107pub mod dma;
108#[cfg(feature = "dsihost")]
109pub mod dsi;
110pub mod dwt;
111pub mod flash;
112#[cfg(any(feature = "fmc", feature = "fsmc"))]
113#[cfg(feature = "stm32-fmc")]
114pub mod fmc;
115#[cfg(all(feature = "fsmc_lcd", any(feature = "fmc", feature = "fsmc")))]
116pub mod fsmc_lcd;
117#[cfg(all(feature = "dma2d", feature = "ltdc"))]
118pub mod ltdc;
119pub mod prelude;
120pub mod qei;
121#[cfg(feature = "quadspi")]
122pub mod qspi;
123pub mod rcc;
124pub mod rtc;
125#[cfg(feature = "sai")]
126pub mod sai;
127#[cfg(all(feature = "sdio-host", feature = "sdio"))]
128pub mod sdio;
129pub mod serial;
130pub mod signature;
131pub mod spi;
132pub mod syscfg;
133pub mod time;
134pub mod timer;
135#[cfg(feature = "uart4")]
136pub mod uart;
137pub mod watchdog;
138
139mod sealed {
140    pub trait Sealed {}
141}
142pub(crate) use sealed::Sealed;
143
144fn stripped_type_name<T>() -> &'static str {
145    let s = core::any::type_name::<T>();
146    let p = s.split("::");
147    p.last().unwrap()
148}
149
150pub trait ReadFlags {
151    /// Enum of bit flags
152    type Flag: BitFlag;
153
154    /// Get all interrupts flags a once.
155    fn flags(&self) -> BitFlags<Self::Flag>;
156}
157
158pub trait ClearFlags {
159    /// Enum of manually clearable flags
160    type Flag: BitFlag;
161
162    /// Clear interrupts flags with `Self::Flags`s
163    ///
164    /// If event flag is not cleared, it will immediately retrigger interrupt
165    /// after interrupt handler has finished.
166    fn clear_flags(&mut self, flags: impl Into<BitFlags<Self::Flag>>);
167
168    /// Clears all interrupts flags
169    #[inline(always)]
170    fn clear_all_flags(&mut self) {
171        self.clear_flags(BitFlags::ALL)
172    }
173}
174
175pub trait Listen {
176    /// Enum of bit flags associated with events
177    type Event: BitFlag;
178
179    /// Start listening for `Event`s
180    ///
181    /// Note, you will also have to enable the appropriate interrupt in the NVIC to start
182    /// receiving events.
183    fn listen(&mut self, event: impl Into<BitFlags<Self::Event>>);
184
185    /// Start listening for `Event`s, stop all other
186    ///
187    /// Note, you will also have to enable the appropriate interrupt in the NVIC to start
188    /// receiving events.
189    fn listen_only(&mut self, event: impl Into<BitFlags<Self::Event>>);
190
191    /// Stop listening for `Event`s
192    fn unlisten(&mut self, event: impl Into<BitFlags<Self::Event>>);
193
194    /// Start listening all `Event`s
195    #[inline(always)]
196    fn listen_all(&mut self) {
197        self.listen(BitFlags::ALL)
198    }
199
200    /// Stop listening all `Event`s
201    #[inline(always)]
202    fn unlisten_all(&mut self) {
203        self.unlisten(BitFlags::ALL)
204    }
205}
206
207pub trait Ptr {
208    /// RegisterBlock structure
209    type RB;
210    /// Return the pointer to the register block
211    fn ptr() -> *const Self::RB;
212}
213
214pub trait Steal {
215    /// Steal an instance of this peripheral
216    ///
217    /// # Safety
218    ///
219    /// Ensure that the new instance of the peripheral cannot be used in a way
220    /// that may race with any existing instances, for example by only
221    /// accessing read-only or write-only registers, or by consuming the
222    /// original peripheral and using critical sections to coordinate
223    /// access between multiple new instances.
224    ///
225    /// Additionally the HAL may rely on only one
226    /// peripheral instance existing to ensure memory safety; ensure
227    /// no stolen instances are passed to such software.
228    unsafe fn steal() -> Self;
229}
230
231#[allow(unused)]
232const fn max_u32(first: u32, second: u32) -> u32 {
233    if second > first {
234        second
235    } else {
236        first
237    }
238}
239
240const fn min_u32(first: u32, second: u32) -> u32 {
241    if second < first {
242        second
243    } else {
244        first
245    }
246}