stm32f1xx_hal/lib.rs
1//! # HAL for the STM32F1 family of microcontrollers
2//!
3//! This is an implementation of the [`embedded-hal`] traits for the STM32F1 family of
4//! microcontrollers.
5//!
6//! [`embedded-hal`]: https://crates.io/crates/embedded-hal
7//!
8//! # Usage
9//!
10//! ## Building an application (binary crate)
11//!
12//! A detailed usage guide can be found in the [README]
13//!
14//! supported microcontrollers are:
15//!
16//! - stm32f103
17//! - stm32f101
18//! - stm32f100
19//! - stm32f105
20//! - stm32f107
21//!
22//! ## Usage
23//!
24//! This crate supports multiple microcontrollers in the
25//! stm32f1 family. Which specific microcontroller you want to build for has to be
26//! specified with a feature, for example `stm32f103`.
27//!
28//! If no microcontroller is specified, the crate will not compile.
29//!
30//! The currently supported variants are
31//!
32//! - `stm32f100`
33//! - `stm32f101`
34//! - `stm32f103`
35//! - `stm32f105`
36//! - `stm32f107`
37//!
38//! You may also need to specify the density of the device with `medium`, `high` or `xl`
39//! to enable certain peripherals. Generally the density can be determined by the 2nd character
40//! after the number in the device name (i.e. For STM32F103C6U, the 6 indicates a low-density
41//! device) but check the datasheet or CubeMX to be sure.
42//! * 4, 6 => low density, no feature required
43//! * 8, B => `medium` feature
44//! * C, D, E => `high` feature
45//! * F, G => `xl` feature
46//!
47//! ## Commonly used setup
48//! Almost all peripherals require references to some registers in `RCC` and `AFIO`. The following
49//! code shows how to set up those registers
50//!
51//! ```rust
52//! // Get access to the device specific peripherals from the peripheral access crate
53//! let dp = pac::Peripherals::take().unwrap();
54//!
55//! // Take ownership over the raw flash and rcc devices and convert them into the corresponding
56//! // HAL structs
57//! let mut flash = dp.FLASH.constrain();
58//! let mut rcc = dp.RCC.constrain();
59//!
60//! // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
61//! // `clocks`
62//! let clocks = rcc.cfgr.freeze(&mut flash.acr);
63//!
64//! // Prepare the alternate function I/O registers
65//! let mut afio = dp.AFIO.constrain(&mut rcc);
66//! ```
67//!
68//! ## Usage examples
69//!
70//! See the [examples] folder.
71//!
72//! Most of the examples require the following additional dependencies
73//! ```toml
74//! [dependencies]
75//! embedded-hal = "0.2.3"
76//! nb = "0.1.2"
77//! cortex-m = "0.6.2"
78//! cortex-m-rt = "0.6.11"
79//! # Panic behaviour, see https://crates.io/keywords/panic-impl for alternatives
80//! panic-halt = "0.2.0"
81//! ```
82//!
83//! [examples]: https://github.com/stm32-rs/stm32f1xx-hal/tree/v0.7.0/examples
84//! [README]: https://github.com/stm32-rs/stm32f1xx-hal/tree/v0.7.0
85
86#![no_std]
87#![deny(rustdoc::broken_intra_doc_links)]
88
89// If no target specified, print error message.
90#[cfg(not(any(
91 feature = "stm32f100",
92 feature = "stm32f101",
93 feature = "stm32f103",
94 feature = "stm32f105",
95 feature = "stm32f107",
96)))]
97compile_error!("Target not found. A `--features <target-name>` is required.");
98
99// If any two or more targets are specified, print error message.
100#[cfg(any(
101 all(feature = "stm32f100", feature = "stm32f101"),
102 all(feature = "stm32f100", feature = "stm32f103"),
103 all(feature = "stm32f100", feature = "stm32f105"),
104 all(feature = "stm32f100", feature = "stm32f107"),
105 all(feature = "stm32f101", feature = "stm32f103"),
106 all(feature = "stm32f101", feature = "stm32f105"),
107 all(feature = "stm32f101", feature = "stm32f107"),
108 all(feature = "stm32f103", feature = "stm32f105"),
109 all(feature = "stm32f103", feature = "stm32f107"),
110 all(feature = "stm32f105", feature = "stm32f107"),
111))]
112compile_error!(
113 "Multiple targets specified. Only a single `--features <target-name>` can be specified."
114);
115
116pub mod pacext;
117
118pub use embedded_hal as hal;
119pub use embedded_hal_02 as hal_02;
120
121#[cfg(feature = "stm32f100")]
122pub use stm32f1::stm32f100 as pac;
123
124#[cfg(feature = "stm32f101")]
125pub use stm32f1::stm32f101 as pac;
126
127#[cfg(feature = "stm32f103")]
128pub use stm32f1::stm32f103 as pac;
129
130#[cfg(any(feature = "stm32f105", feature = "stm32f107"))]
131pub use stm32f1::stm32f107 as pac;
132
133pub mod adc;
134pub mod afio;
135pub mod backup_domain;
136pub mod bb;
137#[cfg(feature = "has-can")]
138pub mod can;
139pub mod crc;
140#[cfg(feature = "has-dac")]
141pub mod dac;
142pub mod dma;
143pub mod flash;
144pub mod gpio;
145pub mod i2c;
146pub mod prelude;
147pub mod rcc;
148pub mod rtc;
149pub mod serial;
150pub mod spi;
151pub mod time;
152pub mod timer;
153#[cfg(feature = "stm32-usbd")]
154#[cfg(feature = "stm32f103")]
155pub mod usb;
156pub mod watchdog;
157
158mod sealed {
159 pub trait Sealed {}
160}
161use sealed::Sealed;
162use stm32f1::Periph;
163
164impl<RB, const A: usize> Sealed for Periph<RB, A> {}
165
166pub trait Ptr: Sealed {
167 /// RegisterBlock structure
168 type RB;
169 /// Return the pointer to the register block
170 fn ptr() -> *const Self::RB;
171}
172
173impl<RB, const A: usize> Ptr for Periph<RB, A> {
174 type RB = RB;
175 fn ptr() -> *const Self::RB {
176 Self::ptr()
177 }
178}
179
180pub trait Steal: Sealed {
181 /// Steal an instance of this peripheral
182 ///
183 /// # Safety
184 ///
185 /// Ensure that the new instance of the peripheral cannot be used in a way
186 /// that may race with any existing instances, for example by only
187 /// accessing read-only or write-only registers, or by consuming the
188 /// original peripheral and using critical sections to coordinate
189 /// access between multiple new instances.
190 ///
191 /// Additionally the HAL may rely on only one
192 /// peripheral instance existing to ensure memory safety; ensure
193 /// no stolen instances are passed to such software.
194 unsafe fn steal() -> Self;
195}
196
197impl<RB, const A: usize> Steal for Periph<RB, A> {
198 unsafe fn steal() -> Self {
199 Self::steal()
200 }
201}