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