esp_hal/
lib.rs

1#![cfg_attr(
2    all(docsrs, not(not_really_docsrs)),
3    doc = "<div style='padding:30px;background:#810;color:#fff;text-align:center;'><p>You might want to <a href='https://docs.espressif.com/projects/rust/'>browse the <code>esp-hal</code> documentation on the esp-rs website</a> instead.</p><p>The documentation here on <a href='https://docs.rs'>docs.rs</a> is built for a single chip only (ESP32-C6, in particular), while on the esp-rs website you can select your exact chip from the list of supported devices. Available peripherals and their APIs change depending on the chip.</p></div>\n\n<br/>\n\n"
4)]
5//! # Bare-metal (`no_std`) HAL for all Espressif ESP32 devices.
6//!
7//! This documentation is built for the
8#![cfg_attr(esp32, doc = "**ESP32**")]
9#![cfg_attr(esp32s2, doc = "**ESP32-S2**")]
10#![cfg_attr(esp32s3, doc = "**ESP32-S3**")]
11#![cfg_attr(esp32c2, doc = "**ESP32-C2**")]
12#![cfg_attr(esp32c3, doc = "**ESP32-C3**")]
13#![cfg_attr(esp32c6, doc = "**ESP32-C6**")]
14#![cfg_attr(esp32h2, doc = "**ESP32-H2**")]
15//! . Please ensure you are reading the correct [documentation] for your target
16//! device.
17//!
18//! ## Choosing a Device
19//!
20//! Depending on your target device, you need to enable the chip feature
21//! for that device. You may also need to do this on ancillary esp-hal crates.
22//!
23//! ## Overview
24//!
25//! ### Peripheral drivers
26//!
27//! The HAL implements both blocking _and_ async APIs for many peripherals.
28//! Where applicable, driver implement the [embedded-hal] and
29//! [embedded-hal-async] traits.
30//!
31//! ### Peripheral singletons
32//!
33//! Each peripheral driver needs a peripheral singleton that tells the driver
34//! which hardware block to use. The peripheral singletons are created by the
35//! HAL initialization, and are returned from [`init`] as fields of the
36//! [`Peripherals`] struct.
37//!
38//! These singletons, by default, represent peripherals for the entire lifetime
39//! of the program. To allow for reusing peripherals, the HAL provides a
40//! `reborrow` method on each peripheral singleton. This method creates a new
41//! handle to the peripheral with a shorter lifetime. This allows you to pass
42//! the handle to a driver, while still keeping the original handle alive. Once
43//! you drop the driver, you will be able to reborrow the peripheral again.
44//!
45//! For example, if you want to use the [`I2c`](i2c::master::I2c) driver and you
46//! don't intend to drop the driver, you can pass the peripheral singleton to
47//! the driver by value:
48//!
49//! ```rust, ignore
50//! // Peripheral singletons are returned from the `init` function.
51//! let peripherals = esp_hal::init(esp_hal::Config::default());
52//!
53//! let mut i2c = I2C::new(peripherals.I2C0, /* ... */);
54//! ```
55//!
56//! If you want to use the peripheral in multiple places (for example, you want
57//! to drop the driver for some period of time to minimize power consumption),
58//! you can reborrow the peripheral singleton and pass it to the driver by
59//! reference:
60//!
61//! ```rust, ignore
62//! // Note that in this case, `peripherals` needs to be mutable.
63//! let mut peripherals = esp_hal::init(esp_hal::Config::default());
64//!
65//! let i2c = I2C::new(peripherals.I2C0.reborrow(), /* ... */);
66//!
67//! // Do something with the I2C driver...
68//!
69//! core::mem::drop(i2c); // Drop the driver to minimize power consumption.
70//!
71//! // Do something else...
72//!
73//! // You can then take or reborrow the peripheral singleton again.
74//! let i2c = I2C::new(peripherals.I2C0.reborrow(), /* ... */);
75//! ```
76//!
77//! ## Examples
78//!
79//! We have a plethora of [examples] in the esp-hal repository. We use
80//! an [xtask] to automate the building, running, and testing of code and
81//! examples within esp-hal.
82//!
83//! Invoke the following command in the root of the esp-hal repository to get
84//! started:
85//!
86//! ```bash
87//! cargo xtask help
88//! ```
89//!
90//! ## Creating a Project
91//!
92//! We have a [book] that explains the full esp-rs ecosystem
93//! and how to get started, it's advisable to give that a read
94//! before proceeding. We also have a [training] that covers some common
95//! scenarios with examples.
96//!
97//! We have developed a project generation tool, [esp-generate], which we
98//! recommend when starting new projects. It can be installed and run, e.g.
99//! for the ESP32-C6, as follows:
100//!
101//! ```bash
102//! cargo install esp-generate
103//! esp-generate --chip=esp32c6 your-project
104//! ```
105//!
106//! ## Blinky
107//!
108//! Some minimal code to blink an LED looks like this:
109//!
110//! ```rust, no_run
111//! #![no_std]
112//! #![no_main]
113//!
114//! use esp_hal::{
115//!     clock::CpuClock,
116//!     gpio::{Io, Level, Output, OutputConfig},
117//!     main,
118//!     time::{Duration, Instant},
119//! };
120//!
121//! // You need a panic handler. Usually, you you would use esp_backtrace, panic-probe, or
122//! // something similar, but you can also bring your own like this:
123//! #[panic_handler]
124//! fn panic(_: &core::panic::PanicInfo) -> ! {
125//!     esp_hal::system::software_reset()
126//! }
127//!
128//! #[main]
129//! fn main() -> ! {
130//!     let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
131//!     let peripherals = esp_hal::init(config);
132//!
133//!     // Set GPIO0 as an output, and set its state high initially.
134//!     let mut led = Output::new(peripherals.GPIO0, Level::High, OutputConfig::default());
135//!
136//!     loop {
137//!         led.toggle();
138//!         // Wait for half a second
139//!         let delay_start = Instant::now();
140//!         while delay_start.elapsed() < Duration::from_millis(500) {}
141//!     }
142//! }
143//! ```
144//!
145//! ## Additional configuration
146//!
147//! We've exposed some configuration options that don't fit into cargo
148//! features. These can be set via environment variables, or via cargo's `[env]`
149//! section inside `.cargo/config.toml`. Note that unstable options can only be
150//! enabled when the `unstable` feature is enabled for the crate. Below is a
151//! table of tunable parameters for this crate:
152#![doc = ""]
153#![doc = include_str!(concat!(env!("OUT_DIR"), "/esp_hal_config_table.md"))]
154#![doc = ""]
155//! ## Don't use `core::mem::forget`
156//!
157//! You should never use `core::mem::forget` on any type defined in the HAL.
158//! Some types heavily rely on their `Drop` implementation to not leave the
159//! hardware in undefined state and causing UB.
160//!
161//! You might want to consider using [`#[deny(clippy::mem_forget)`](https://rust-lang.github.io/rust-clippy/v0.0.212/index.html#mem_forget) in your project.
162//!
163//! ## Library usage
164//!
165//! If you intend to write a library that uses esp-hal, you should import it as follows:
166//!
167//! ```toml
168//! [dependencies]
169//! esp-hal = { version = "1", default-features = false } }
170//! ```
171//!
172//! This ensures that the `rt` feature is not enabled, nor any chip features. The application that
173//! uses your library will then be able to choose the chip feature it needs and enable `rt` such
174//! that only the final user application calls [`init`].
175//!
176//! If your library depends on `unstable` features, you *must* use the `requires-unstable` feature,
177//! and *not* the unstable feature itself. Doing so, improves the quality of the error messages if a
178//! user hasn't enabled the unstable feature of esp-hal.
179//!
180//! [documentation]: https://docs.espressif.com/projects/rust/esp-hal/latest/
181//! [examples]: https://github.com/esp-rs/esp-hal/tree/main/examples
182//! [embedded-hal]: https://docs.rs/embedded-hal/latest/embedded_hal/
183//! [embedded-hal-async]: https://docs.rs/embedded-hal-async/latest/embedded_hal_async/
184//! [xtask]: https://github.com/matklad/cargo-xtask
185//! [esp-generate]: https://github.com/esp-rs/esp-generate
186//! [book]: https://docs.espressif.com/projects/rust/book/
187//! [training]: https://docs.espressif.com/projects/rust/no_std-training/
188//!
189//! ## Feature Flags
190#![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)]
191#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/46717278")]
192#![allow(asm_sub_register, async_fn_in_trait, stable_features)]
193#![cfg_attr(xtensa, feature(asm_experimental_arch))]
194#![deny(missing_docs, rust_2018_idioms, rustdoc::all)]
195#![allow(rustdoc::private_doc_tests)] // compile tests are done via rustdoc
196#![cfg_attr(docsrs, feature(doc_cfg, custom_inner_attributes, proc_macro_hygiene))]
197#![no_std]
198
199// MUST be the first module
200mod fmt;
201
202#[macro_use]
203extern crate esp_metadata_generated;
204
205use core::marker::PhantomData;
206
207pub use esp_metadata_generated::chip;
208use esp_rom_sys as _;
209
210metadata!("build_info", CHIP_NAME, chip!());
211
212#[cfg(all(riscv, feature = "rt"))]
213#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", feature = "rt"))))]
214#[cfg_attr(not(feature = "unstable"), doc(hidden))]
215pub use esp_riscv_rt::{self, riscv};
216pub(crate) use peripherals::pac;
217#[cfg(xtensa)]
218#[cfg(all(xtensa, feature = "rt"))]
219#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", feature = "rt"))))]
220#[cfg_attr(not(feature = "unstable"), doc(hidden))]
221pub use xtensa_lx_rt::{self, xtensa_lx};
222
223#[cfg(soc_has_efuse)]
224#[instability::unstable]
225#[cfg_attr(not(feature = "unstable"), allow(unused))]
226pub use self::soc::efuse;
227#[cfg(lp_core)]
228#[instability::unstable]
229#[cfg_attr(not(feature = "unstable"), allow(unused))]
230pub use self::soc::lp_core;
231#[instability::unstable]
232#[cfg(feature = "psram")]
233pub use self::soc::psram;
234#[cfg(ulp_riscv_core)]
235#[instability::unstable]
236#[cfg_attr(not(feature = "unstable"), allow(unused))]
237pub use self::soc::ulp_core;
238
239#[cfg(any(soc_has_dport, soc_has_hp_sys, soc_has_pcr, soc_has_system))]
240pub mod clock;
241#[cfg(soc_has_gpio)]
242pub mod gpio;
243#[cfg(any(soc_has_i2c0, soc_has_i2c1))]
244pub mod i2c;
245pub mod peripherals;
246#[cfg(all(feature = "unstable", any(soc_has_hmac, soc_has_sha)))]
247mod reg_access;
248#[cfg(any(soc_has_spi0, soc_has_spi1, soc_has_spi2, soc_has_spi3))]
249pub mod spi;
250pub mod system;
251pub mod time;
252#[cfg(any(soc_has_uart0, soc_has_uart1, soc_has_uart2))]
253pub mod uart;
254
255mod macros;
256
257#[cfg(feature = "rt")]
258pub use procmacros::blocking_main as main;
259#[cfg(any(lp_core, ulp_riscv_core))]
260#[cfg(feature = "unstable")]
261#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
262pub use procmacros::load_lp_code;
263#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
264#[instability::unstable]
265#[cfg_attr(not(feature = "unstable"), allow(unused))]
266pub use procmacros::{handler, ram};
267
268// can't use instability on inline module definitions, see https://github.com/rust-lang/rust/issues/54727
269#[doc(hidden)]
270macro_rules! unstable_module {
271    ($(
272        $(#[$meta:meta])*
273        pub mod $module:ident;
274    )*) => {
275        $(
276            $(#[$meta])*
277            #[cfg(feature = "unstable")]
278            #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
279            pub mod $module;
280
281            $(#[$meta])*
282            #[cfg(not(feature = "unstable"))]
283            #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
284            #[allow(unused)]
285            pub(crate) mod $module;
286        )*
287    };
288}
289
290// can't use instability on inline module definitions, see https://github.com/rust-lang/rust/issues/54727
291// we don't want unstable drivers to be compiled even, unless enabled
292#[doc(hidden)]
293macro_rules! unstable_driver {
294    ($(
295        $(#[$meta:meta])*
296        pub mod $module:ident;
297    )*) => {
298        $(
299            $(#[$meta])*
300            #[cfg(feature = "unstable")]
301            #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
302            pub mod $module;
303        )*
304    };
305}
306
307pub(crate) use unstable_driver;
308pub(crate) use unstable_module;
309
310unstable_module! {
311    pub mod asynch;
312    pub mod config;
313    pub mod debugger;
314    #[cfg(any(soc_has_dport, soc_has_interrupt_core0, soc_has_interrupt_core1))]
315    pub mod interrupt;
316    pub mod rom;
317    #[doc(hidden)]
318    pub mod sync;
319    // Drivers needed for initialization or they are tightly coupled to something else.
320    #[cfg(any(adc, dac))]
321    pub mod analog;
322    #[cfg(any(systimer, timergroup))]
323    pub mod timer;
324    #[cfg(soc_has_lpwr)]
325    pub mod rtc_cntl;
326    #[cfg(any(gdma, pdma))]
327    pub mod dma;
328    #[cfg(soc_has_etm)]
329    pub mod etm;
330    #[cfg(soc_has_usb0)]
331    pub mod otg_fs;
332}
333
334unstable_driver! {
335    #[cfg(soc_has_aes)]
336    pub mod aes;
337    #[cfg(soc_has_assist_debug)]
338    pub mod assist_debug;
339    pub mod delay;
340    #[cfg(soc_has_ecc)]
341    pub mod ecc;
342    #[cfg(soc_has_hmac)]
343    pub mod hmac;
344    #[cfg(any(soc_has_i2s0, soc_has_i2s1))]
345    pub mod i2s;
346    #[cfg(soc_has_lcd_cam)]
347    pub mod lcd_cam;
348    #[cfg(soc_has_ledc)]
349    pub mod ledc;
350    #[cfg(any(soc_has_mcpwm0, soc_has_mcpwm1))]
351    pub mod mcpwm;
352    #[cfg(soc_has_parl_io)]
353    pub mod parl_io;
354    #[cfg(soc_has_pcnt)]
355    pub mod pcnt;
356    #[cfg(soc_has_rmt)]
357    pub mod rmt;
358    #[cfg(soc_has_rng)]
359    pub mod rng;
360    #[cfg(soc_has_rsa)]
361    pub mod rsa;
362    #[cfg(soc_has_sha)]
363    pub mod sha;
364    #[cfg(touch)]
365    pub mod touch;
366    #[cfg(soc_has_trace0)]
367    pub mod trace;
368    #[cfg(soc_has_tsens)]
369    pub mod tsens;
370    #[cfg(any(soc_has_twai0, soc_has_twai1))]
371    pub mod twai;
372    #[cfg(soc_has_usb_device)]
373    pub mod usb_serial_jtag;
374}
375
376/// State of the CPU saved when entering exception or interrupt
377#[instability::unstable]
378#[cfg(feature = "rt")]
379#[allow(unused_imports)]
380pub mod trapframe {
381    #[cfg(riscv)]
382    pub use esp_riscv_rt::TrapFrame;
383    #[cfg(xtensa)]
384    pub use xtensa_lx_rt::exception::Context as TrapFrame;
385}
386
387// The `soc` module contains chip-specific implementation details and should not
388// be directly exposed.
389mod soc;
390
391#[cfg(is_debug_build)]
392procmacros::warning! {"
393WARNING: use --release
394  We *strongly* recommend using release profile when building esp-hal.
395  The dev profile can potentially be one or more orders of magnitude
396  slower than release, and may cause issues with timing-senstive
397  peripherals and/or devices.
398"}
399
400/// A marker trait for driver modes.
401///
402/// Different driver modes offer different features and different API. Using
403/// this trait as a generic parameter ensures that the driver is initialized in
404/// the correct mode.
405pub trait DriverMode: crate::private::Sealed {}
406
407#[procmacros::doc_replace]
408/// Marker type signalling that a driver is initialized in blocking mode.
409///
410/// Drivers are constructed in blocking mode by default. To learn about the
411/// differences between blocking and async drivers, see the [`Async`] mode
412/// documentation.
413///
414/// [`Async`] drivers can be converted to a [`Blocking`] driver using the
415/// `into_blocking` method, for example:
416///
417/// ```rust, no_run
418/// # {before_snippet}
419/// # use esp_hal::uart::{Config, Uart};
420/// let uart = Uart::new(peripherals.UART0, Config::default())?
421///     .with_rx(peripherals.GPIO1)
422///     .with_tx(peripherals.GPIO2)
423///     .into_async();
424///
425/// let blocking_uart = uart.into_blocking();
426///
427/// # {after_snippet}
428/// ```
429#[derive(Debug)]
430#[non_exhaustive]
431pub struct Blocking;
432
433#[procmacros::doc_replace]
434/// Marker type signalling that a driver is initialized in async mode.
435///
436/// Drivers are constructed in blocking mode by default. To set up an async
437/// driver, a [`Blocking`] driver must be converted to an `Async` driver using
438/// the `into_async` method, for example:
439///
440/// ```rust, no_run
441/// # {before_snippet}
442/// # use esp_hal::uart::{Config, Uart};
443/// let uart = Uart::new(peripherals.UART0, Config::default())?
444///     .with_rx(peripherals.GPIO1)
445///     .with_tx(peripherals.GPIO2)
446///     .into_async();
447///
448/// # {after_snippet}
449/// ```
450///
451/// Drivers can be converted back to blocking mode using the `into_blocking`
452/// method, see [`Blocking`] documentation for more details.
453///
454/// Async mode drivers offer most of the same features as blocking drivers, but
455/// with the addition of async APIs. Interrupt-related functions are not
456/// available in async mode, as they are handled by the driver's interrupt
457/// handlers.
458///
459/// Note that async functions usually take up more space than their blocking
460/// counterparts, and they are generally slower. This is because async functions
461/// are implemented using a state machine that is driven by interrupts and is
462/// polled by a runtime. For short operations, the overhead of the state machine
463/// can be significant. Consider using the blocking functions on the async
464/// driver for small transfers.
465///
466/// When initializing an async driver, the driver disables user-specified
467/// interrupt handlers, and sets up internal interrupt handlers that drive the
468/// driver's async API. The driver's interrupt handlers run on the same core as
469/// the driver was initialized on. This means that the driver can not be sent
470/// across threads, to prevent incorrect concurrent access to the peripheral.
471///
472/// Switching back to blocking mode will disable the interrupt handlers and
473/// return the driver to a state where it can be sent across threads.
474#[derive(Debug)]
475#[non_exhaustive]
476pub struct Async(PhantomData<*const ()>);
477
478unsafe impl Sync for Async {}
479
480impl crate::DriverMode for Blocking {}
481impl crate::DriverMode for Async {}
482impl crate::private::Sealed for Blocking {}
483impl crate::private::Sealed for Async {}
484
485pub(crate) mod private {
486    use core::mem::ManuallyDrop;
487
488    pub trait Sealed {}
489
490    #[non_exhaustive]
491    #[doc(hidden)]
492    /// Magical incantation to gain access to internal APIs.
493    pub struct Internal;
494
495    impl Internal {
496        /// Obtain magical powers to access internal APIs.
497        ///
498        /// # Safety
499        ///
500        /// By calling this function, you accept that you are using an internal
501        /// API that is not guaranteed to be documented, stable, working
502        /// and may change at any time.
503        ///
504        /// You declare that you have tried to look for other solutions, that
505        /// you have opened a feature request or an issue to discuss the
506        /// need for this function.
507        pub unsafe fn conjure() -> Self {
508            Self
509        }
510    }
511
512    pub(crate) struct OnDrop<F: FnOnce()>(ManuallyDrop<F>);
513    impl<F: FnOnce()> OnDrop<F> {
514        pub fn new(cb: F) -> Self {
515            Self(ManuallyDrop::new(cb))
516        }
517
518        pub fn defuse(self) {
519            core::mem::forget(self);
520        }
521    }
522
523    impl<F: FnOnce()> Drop for OnDrop<F> {
524        fn drop(&mut self) {
525            unsafe { (ManuallyDrop::take(&mut self.0))() }
526        }
527    }
528}
529
530#[cfg(feature = "unstable")]
531#[doc(hidden)]
532pub use private::Internal;
533
534/// Marker trait for types that can be safely used in `#[ram(persistent)]`.
535///
536/// # Safety
537///
538/// - The type must be inhabited
539/// - The type must be valid for any bit pattern of its backing memory in case a reset occurs during
540///   a write or a reset interrupts the zero initialization on first boot.
541/// - Structs must contain only `Persistable` fields and padding
542#[instability::unstable]
543pub unsafe trait Persistable: Sized {}
544
545macro_rules! impl_persistable {
546    ($($t:ty),+) => {$(
547        unsafe impl Persistable for $t {}
548    )+};
549    (atomic $($t:ident),+) => {$(
550        unsafe impl Persistable for portable_atomic::$t {}
551    )+};
552}
553
554impl_persistable!(
555    u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize, f32, f64
556);
557impl_persistable!(atomic AtomicU8, AtomicI8, AtomicU16, AtomicI16, AtomicU32, AtomicI32, AtomicUsize, AtomicIsize);
558
559unsafe impl<T: Persistable, const N: usize> Persistable for [T; N] {}
560
561#[doc(hidden)]
562pub mod __macro_implementation {
563    //! Private implementation details of esp-hal-procmacros.
564
565    #[instability::unstable]
566    pub const fn assert_is_zeroable<T: bytemuck::Zeroable>() {}
567
568    #[instability::unstable]
569    pub const fn assert_is_persistable<T: super::Persistable>() {}
570
571    #[cfg(feature = "rt")]
572    #[cfg(riscv)]
573    pub use esp_riscv_rt::entry as __entry;
574    #[cfg(feature = "rt")]
575    #[cfg(xtensa)]
576    pub use xtensa_lx_rt::entry as __entry;
577}
578
579use crate::clock::CpuClock;
580#[cfg(feature = "unstable")]
581use crate::config::{WatchdogConfig, WatchdogStatus};
582#[cfg(feature = "rt")]
583use crate::{clock::Clocks, peripherals::Peripherals};
584
585/// System configuration.
586///
587/// This `struct` is marked with `#[non_exhaustive]` and can't be instantiated
588/// directly. This is done to prevent breaking changes when new fields are added
589/// to the `struct`. Instead, use the [`Config::default()`] method to create a
590/// new instance.
591///
592/// For usage examples, see the [config module documentation](crate::config).
593#[non_exhaustive]
594#[derive(Default, Clone, Copy, procmacros::BuilderLite)]
595pub struct Config {
596    /// The CPU clock configuration.
597    cpu_clock: CpuClock,
598
599    /// Enable watchdog timer(s).
600    #[cfg(feature = "unstable")]
601    #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
602    #[builder_lite(unstable)]
603    watchdog: WatchdogConfig,
604
605    /// PSRAM configuration.
606    #[cfg(feature = "unstable")]
607    #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
608    #[cfg(feature = "psram")]
609    #[builder_lite(unstable)]
610    psram: psram::PsramConfig,
611}
612
613#[procmacros::doc_replace]
614/// Initialize the system.
615///
616/// This function sets up the CPU clock and watchdog, then, returns the
617/// peripherals and clocks.
618///
619/// # Example
620///
621/// ```rust, no_run
622/// # {before_snippet}
623/// use esp_hal::{Config, init};
624/// let peripherals = init(Config::default());
625/// # {after_snippet}
626/// ```
627#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
628#[cfg(feature = "rt")]
629pub fn init(config: Config) -> Peripherals {
630    crate::soc::pre_init();
631
632    system::disable_peripherals();
633
634    let mut peripherals = Peripherals::take();
635
636    // RTC domain must be enabled before we try to disable
637    let mut rtc = crate::rtc_cntl::Rtc::new(peripherals.LPWR.reborrow());
638
639    // Handle watchdog configuration with defaults
640    cfg_if::cfg_if! {
641        if #[cfg(feature = "unstable")]
642        {
643            #[cfg(not(any(esp32, esp32s2)))]
644            if config.watchdog.swd() {
645                rtc.swd.enable();
646            } else {
647                rtc.swd.disable();
648            }
649
650            match config.watchdog.rwdt() {
651                WatchdogStatus::Enabled(duration) => {
652                    rtc.rwdt.enable();
653                    rtc.rwdt
654                        .set_timeout(crate::rtc_cntl::RwdtStage::Stage0, duration);
655                }
656                WatchdogStatus::Disabled => {
657                    rtc.rwdt.disable();
658                }
659            }
660
661            #[cfg(timergroup_timg0)]
662            match config.watchdog.timg0() {
663                WatchdogStatus::Enabled(duration) => {
664                    let mut timg0_wd = crate::timer::timg::Wdt::<crate::peripherals::TIMG0<'static>>::new();
665                    timg0_wd.enable();
666                    timg0_wd.set_timeout(crate::timer::timg::MwdtStage::Stage0, duration);
667                }
668                WatchdogStatus::Disabled => {
669                    crate::timer::timg::Wdt::<crate::peripherals::TIMG0<'static>>::new().disable();
670                }
671            }
672
673            #[cfg(timergroup_timg1)]
674            match config.watchdog.timg1() {
675                WatchdogStatus::Enabled(duration) => {
676                    let mut timg1_wd = crate::timer::timg::Wdt::<crate::peripherals::TIMG1<'static>>::new();
677                    timg1_wd.enable();
678                    timg1_wd.set_timeout(crate::timer::timg::MwdtStage::Stage0, duration);
679                }
680                WatchdogStatus::Disabled => {
681                    crate::timer::timg::Wdt::<crate::peripherals::TIMG1<'static>>::new().disable();
682                }
683            }
684        }
685        else
686        {
687            #[cfg(not(any(esp32, esp32s2)))]
688            rtc.swd.disable();
689
690            rtc.rwdt.disable();
691
692            #[cfg(timergroup_timg0)]
693            crate::timer::timg::Wdt::<crate::peripherals::TIMG0<'static>>::new().disable();
694
695            #[cfg(timergroup_timg1)]
696            crate::timer::timg::Wdt::<crate::peripherals::TIMG1<'static>>::new().disable();
697        }
698    }
699
700    Clocks::init(config.cpu_clock);
701
702    #[cfg(esp32)]
703    crate::time::time_init();
704
705    crate::gpio::interrupt::bind_default_interrupt_handler();
706
707    #[cfg(feature = "psram")]
708    crate::psram::init_psram(config.psram);
709
710    peripherals
711}