Skip to main content

ariel_os_esp/
lib.rs

1//! Items specific to the Espressif ESP MCUs.
2
3#![no_std]
4#![cfg_attr(nightly, feature(doc_cfg))]
5#![deny(missing_docs)]
6
7#[cfg(feature = "_radio-esp")]
8extern crate alloc;
9
10mod app_desc {
11    esp_bootloader_esp_idf::esp_app_desc!();
12}
13
14#[cfg(feature = "_radio-esp")]
15mod radio;
16#[cfg(feature = "_radio-esp")]
17mod scheduler;
18#[cfg(feature = "_radio-esp")]
19mod semaphore;
20#[cfg(feature = "_radio-esp")]
21mod wait_queue;
22
23pub mod gpio;
24
25#[cfg(feature = "ble-esp")]
26#[doc(hidden)]
27pub mod ble;
28
29#[cfg(feature = "hwrng")]
30#[doc(hidden)]
31pub mod hwrng {
32    pub fn construct_rng(_peripherals: &mut crate::OptionalPeripherals) {
33        // handled in `init()`
34    }
35}
36
37#[cfg(feature = "i2c")]
38pub mod i2c;
39
40#[doc(hidden)]
41pub mod identity;
42
43#[cfg(feature = "spi")]
44pub mod spi;
45
46#[cfg(feature = "uart")]
47pub mod uart;
48
49#[cfg(feature = "usb")]
50#[doc(hidden)]
51pub mod usb;
52
53#[cfg(feature = "wifi")]
54#[doc(hidden)]
55pub mod wifi;
56
57#[doc(hidden)]
58pub mod peripheral {}
59
60pub mod peripherals {
61    //! Types for the peripheral singletons.
62
63    pub use esp_hal::peripherals::*;
64}
65
66#[cfg(feature = "time")]
67mod time_driver;
68
69#[doc(hidden)]
70pub use esp_hal::peripherals::OptionalPeripherals;
71
72#[doc(hidden)]
73pub trait IntoPeripheral<'a, T> {
74    fn into_hal_peripheral(self) -> T;
75}
76
77#[doc(hidden)]
78impl<T> IntoPeripheral<'_, T> for T {
79    fn into_hal_peripheral(self) -> T {
80        self
81    }
82}
83
84#[doc(hidden)]
85#[must_use]
86pub fn init() -> OptionalPeripherals {
87    let config = esp_hal::Config::default().with_cpu_clock(esp_hal::clock::CpuClock::max());
88
89    #[allow(unused_mut, reason = "mut only needed for some features")]
90    let mut peripherals = OptionalPeripherals::from(esp_hal::init(config));
91
92    #[cfg(feature = "hwrng")]
93    {
94        ariel_os_log::debug!("initializing hwrng");
95        let mut rng = esp_hal::rng::Rng::new();
96        ariel_os_random::construct_rng(&mut ariel_os_random::RngAdapter(&mut rng));
97    }
98
99    #[cfg(feature = "time")]
100    {
101        let embassy_timer = {
102            cfg_select! {
103                context = "esp32" => {
104                    use esp_hal::timer::timg::TimerGroup;
105                    TimerGroup::new(peripherals.TIMG1.take().unwrap()).timer0
106                }
107                _ => {
108                    use esp_hal::timer::systimer::{SystemTimer};
109                    SystemTimer::new(peripherals.SYSTIMER.take().unwrap()).alarm0
110                }
111            }
112        };
113
114        crate::time_driver::init(embassy_timer);
115    }
116
117    peripherals
118}
119
120#[cfg(feature = "time")]
121embassy_time_driver::time_driver_impl!(static TIMER_QUEUE: crate::time_driver::TimerQueue = crate::time_driver::TimerQueue::new());