Skip to main content

ariel_os_stm32/
lib.rs

1//! Items specific to the STMicroelectronics STM32 MCUs.
2
3#![no_std]
4#![cfg_attr(nightly, feature(doc_cfg))]
5#![cfg_attr(feature = "rcc-config-override", expect(unsafe_code))]
6#![deny(missing_docs)]
7
8mod rcc;
9
10pub mod gpio;
11
12#[doc(hidden)]
13pub mod peripheral {
14    pub use embassy_stm32::Peri;
15}
16
17#[cfg(feature = "external-interrupts")]
18#[doc(hidden)]
19pub mod extint_registry;
20
21#[cfg(feature = "i2c")]
22pub mod i2c;
23
24#[doc(hidden)]
25pub mod identity;
26
27#[cfg(feature = "spi")]
28pub mod spi;
29
30#[cfg(feature = "uart")]
31pub mod uart;
32
33#[cfg(feature = "storage")]
34#[doc(hidden)]
35pub mod storage;
36
37#[cfg(feature = "usb")]
38#[doc(hidden)]
39pub mod usb;
40
41#[cfg(feature = "ethernet")]
42#[doc(hidden)]
43pub mod ethernet;
44
45use embassy_stm32::Config;
46
47#[doc(hidden)]
48pub use embassy_stm32::{OptionalPeripherals, Peri, PeripheralType, Peripherals, interrupt};
49
50pub use embassy_stm32::peripherals;
51
52#[cfg(feature = "executor-interrupt")]
53pub(crate) use embassy_executor::InterruptExecutor as Executor;
54
55#[cfg(feature = "hwrng")]
56#[doc(hidden)]
57pub mod hwrng;
58
59#[cfg(feature = "executor-interrupt")]
60include!(concat!(env!("OUT_DIR"), "/swi.rs"));
61
62#[cfg(capability = "hw/stm32-dual-core")]
63use {core::mem::MaybeUninit, embassy_stm32::SharedData};
64
65// Ariel OS doesn't support the second core yet, but upstream needs this.
66#[cfg(capability = "hw/stm32-dual-core")]
67static SHARED_DATA: MaybeUninit<SharedData> = MaybeUninit::uninit();
68
69#[cfg(feature = "executor-interrupt")]
70#[doc(hidden)]
71pub static EXECUTOR: Executor = Executor::new();
72
73#[doc(hidden)]
74pub trait IntoPeripheral<'a, T: PeripheralType>: private::Sealed {
75    fn into_hal_peripheral(self) -> Peri<'a, T>;
76}
77
78impl<T: PeripheralType> private::Sealed for Peri<'_, T> {}
79
80#[doc(hidden)]
81impl<'a, T: PeripheralType> IntoPeripheral<'a, T> for Peri<'a, T> {
82    fn into_hal_peripheral(self) -> Peri<'a, T> {
83        self
84    }
85}
86
87mod private {
88    pub trait Sealed {}
89}
90
91#[doc(hidden)]
92#[must_use]
93pub fn init() -> OptionalPeripherals {
94    let mut config = Config::default();
95    board_config(&mut config);
96
97    #[cfg(not(capability = "hw/stm32-dual-core"))]
98    let peripherals = embassy_stm32::init(config);
99
100    #[cfg(capability = "hw/stm32-dual-core")]
101    let peripherals = embassy_stm32::init_primary(config, &SHARED_DATA);
102
103    enable_flash_cache();
104
105    OptionalPeripherals::from(peripherals)
106}
107
108fn board_config(config: &mut Config) {
109    config.rcc = rcc::config();
110}
111
112fn enable_flash_cache() {
113    // F2 and F4 support these
114    #[cfg(any(context = "stm32f401re", context = "stm32f411re",))]
115    {
116        // reset the instruction cache
117        embassy_stm32::pac::FLASH
118            .acr()
119            .modify(|w| w.set_icrst(true));
120        // enable the instruction cache and prefetch
121        embassy_stm32::pac::FLASH.acr().modify(|w| w.set_icen(true));
122        embassy_stm32::pac::FLASH
123            .acr()
124            .modify(|w| w.set_prften(true));
125        // reset the data cache
126        embassy_stm32::pac::FLASH
127            .acr()
128            .modify(|w| w.set_dcrst(true));
129        embassy_stm32::pac::FLASH
130            .acr()
131            .modify(|w| w.set_dcrst(false));
132        // enable the data cache
133        embassy_stm32::pac::FLASH.acr().modify(|w| w.set_dcen(true));
134    }
135}