diff --git a/examples/src/bin/adc.rs b/examples/src/bin/adc.rs index 00d528c17ba..d057ac52627 100644 --- a/examples/src/bin/adc.rs +++ b/examples/src/bin/adc.rs @@ -19,20 +19,19 @@ use esp_backtrace as _; use esp_hal::{ analog::adc::{Adc, AdcConfig, Attenuation}, - clock::ClockControl, delay::Delay, gpio::Io, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); cfg_if::cfg_if! { diff --git a/examples/src/bin/adc_cal.rs b/examples/src/bin/adc_cal.rs index 8b8689e1128..fcb5afc0b51 100644 --- a/examples/src/bin/adc_cal.rs +++ b/examples/src/bin/adc_cal.rs @@ -15,20 +15,19 @@ use esp_backtrace as _; use esp_hal::{ analog::adc::{Adc, AdcConfig, Attenuation}, - clock::ClockControl, delay::Delay, gpio::Io, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); cfg_if::cfg_if! { diff --git a/examples/src/bin/advanced_serial.rs b/examples/src/bin/advanced_serial.rs index 12b4d455151..8575a2d7153 100644 --- a/examples/src/bin/advanced_serial.rs +++ b/examples/src/bin/advanced_serial.rs @@ -13,23 +13,17 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - gpio::Io, - peripherals::Peripherals, - prelude::*, - system::SystemControl, - uart::Uart, -}; +use esp_hal::{delay::Delay, gpio::Io, prelude::*, uart::Uart}; use esp_println::println; use nb::block; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/blinky.rs b/examples/src/bin/blinky.rs index 4509656832f..4afe1fa4f8b 100644 --- a/examples/src/bin/blinky.rs +++ b/examples/src/bin/blinky.rs @@ -10,19 +10,18 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio::{Io, Level, Output}, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); // Set GPIO0 as an output, and set its state high initially. let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/blinky_erased_pins.rs b/examples/src/bin/blinky_erased_pins.rs index d5c6519e065..9a7f219e6e6 100644 --- a/examples/src/bin/blinky_erased_pins.rs +++ b/examples/src/bin/blinky_erased_pins.rs @@ -13,19 +13,18 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio::{AnyInput, AnyOutput, Io, Level, Pull}, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/dac.rs b/examples/src/bin/dac.rs index 7ad9c00226a..16844e69ad7 100644 --- a/examples/src/bin/dac.rs +++ b/examples/src/bin/dac.rs @@ -19,21 +19,15 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - analog::dac::Dac, - clock::ClockControl, - delay::Delay, - gpio::Io, - peripherals::Peripherals, - prelude::*, - system::SystemControl, -}; +use esp_hal::{analog::dac::Dac, delay::Delay, gpio::Io, prelude::*}; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/debug_assist.rs b/examples/src/bin/debug_assist.rs index 8b7e09772ca..77576276f6b 100644 --- a/examples/src/bin/debug_assist.rs +++ b/examples/src/bin/debug_assist.rs @@ -11,22 +11,18 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; -use esp_hal::{ - assist_debug::DebugAssist, - clock::ClockControl, - peripherals::Peripherals, - prelude::*, - system::SystemControl, -}; +use esp_hal::{assist_debug::DebugAssist, prelude::*}; use esp_println::println; static DA: Mutex>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let _clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let mut da = DebugAssist::new(peripherals.ASSIST_DEBUG); da.set_interrupt_handler(interrupt_handler); diff --git a/examples/src/bin/dma_extmem2mem.rs b/examples/src/bin/dma_extmem2mem.rs index 63360a776cf..83346f0f0cf 100644 --- a/examples/src/bin/dma_extmem2mem.rs +++ b/examples/src/bin/dma_extmem2mem.rs @@ -9,13 +9,10 @@ use aligned::{Aligned, A64}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, dma::{Dma, DmaPriority, Mem2Mem}, dma_descriptors_chunk_size, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use log::{error, info}; extern crate alloc; @@ -65,10 +62,13 @@ fn init_heap(psram: impl esp_hal::peripheral::Peripheral

! { esp_println::logger::init_logger(log::LevelFilter::Info); - let peripherals = Peripherals::take(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); + init_heap(peripherals.PSRAM); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); let delay = Delay::new(&clocks); let mut extram_buffer: &mut [u8] = dma_alloc_buffer!(DATA_SIZE, 64); diff --git a/examples/src/bin/dma_mem2mem.rs b/examples/src/bin/dma_mem2mem.rs index 6374d03c7bb..e4606010ae1 100644 --- a/examples/src/bin/dma_mem2mem.rs +++ b/examples/src/bin/dma_mem2mem.rs @@ -8,13 +8,10 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, dma::{Dma, DmaPriority, Mem2Mem}, dma_buffers, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use log::{error, info}; @@ -24,9 +21,12 @@ const DATA_SIZE: usize = 1024 * 10; fn main() -> ! { esp_println::logger::init_logger(log::LevelFilter::Info); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); + let delay = Delay::new(&clocks); let (tx_buffer, tx_descriptors, mut rx_buffer, rx_descriptors) = dma_buffers!(DATA_SIZE); diff --git a/examples/src/bin/embassy_hello_world.rs b/examples/src/bin/embassy_hello_world.rs index 9f289cb230a..7eea9c96362 100644 --- a/examples/src/bin/embassy_hello_world.rs +++ b/examples/src/bin/embassy_hello_world.rs @@ -12,12 +12,7 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{prelude::*, timer::timg::TimerGroup}; #[embassy_executor::task] async fn run() { @@ -30,11 +25,13 @@ async fn run() { #[esp_hal_embassy::main] async fn main(spawner: Spawner) { esp_println::logger::init_logger_from_env(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); esp_println::println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_i2c.rs b/examples/src/bin/embassy_i2c.rs index 9ef50be70d6..a6d703763e8 100644 --- a/examples/src/bin/embassy_i2c.rs +++ b/examples/src/bin/embassy_i2c.rs @@ -19,22 +19,16 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - gpio::Io, - i2c::I2C, - peripherals::Peripherals, - prelude::*, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{gpio::Io, i2c::I2C, prelude::*, timer::timg::TimerGroup}; use lis3dh_async::{Lis3dh, Range, SlaveAddr}; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_i2c_bmp180_calibration_data.rs b/examples/src/bin/embassy_i2c_bmp180_calibration_data.rs index 1e4482f101d..9537856f413 100644 --- a/examples/src/bin/embassy_i2c_bmp180_calibration_data.rs +++ b/examples/src/bin/embassy_i2c_bmp180_calibration_data.rs @@ -20,21 +20,15 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - gpio::Io, - i2c::I2C, - peripherals::Peripherals, - prelude::*, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{gpio::Io, i2c::I2C, prelude::*, timer::timg::TimerGroup}; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_i2s_read.rs b/examples/src/bin/embassy_i2s_read.rs index edffeec2885..4c2c80d9e18 100644 --- a/examples/src/bin/embassy_i2s_read.rs +++ b/examples/src/bin/embassy_i2s_read.rs @@ -20,14 +20,11 @@ use embassy_executor::Spawner; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority}, dma_buffers, gpio::Io, i2s::{asynch::*, DataFormat, I2s, Standard}, - peripherals::Peripherals, prelude::*, - system::SystemControl, timer::timg::TimerGroup, }; use esp_println::println; @@ -35,9 +32,11 @@ use esp_println::println; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_i2s_sound.rs b/examples/src/bin/embassy_i2s_sound.rs index bc136a6d6b2..8c54d1ce44e 100644 --- a/examples/src/bin/embassy_i2s_sound.rs +++ b/examples/src/bin/embassy_i2s_sound.rs @@ -34,14 +34,11 @@ use embassy_executor::Spawner; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority}, dma_buffers, gpio::Io, i2s::{asynch::*, DataFormat, I2s, Standard}, - peripherals::Peripherals, prelude::*, - system::SystemControl, timer::timg::TimerGroup, }; use esp_println::println; @@ -57,9 +54,11 @@ const SINE: [i16; 64] = [ #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_multicore.rs b/examples/src/bin/embassy_multicore.rs index ca0638b2808..7ff678e3215 100644 --- a/examples/src/bin/embassy_multicore.rs +++ b/examples/src/bin/embassy_multicore.rs @@ -19,12 +19,9 @@ use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal} use embassy_time::{Duration, Ticker}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, cpu_control::{CpuControl, Stack}, get_core, gpio::{AnyOutput, Io, Level}, - peripherals::Peripherals, - system::SystemControl, timer::{timg::TimerGroup, ErasedTimer, OneShotTimer}, }; use esp_hal_embassy::Executor; @@ -64,9 +61,11 @@ async fn control_led( #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/embassy_multicore_interrupt.rs b/examples/src/bin/embassy_multicore_interrupt.rs index 648747dd37f..f650a05e6d0 100644 --- a/examples/src/bin/embassy_multicore_interrupt.rs +++ b/examples/src/bin/embassy_multicore_interrupt.rs @@ -18,14 +18,11 @@ use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal} use embassy_time::{Duration, Ticker}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, cpu_control::{CpuControl, Stack}, get_core, gpio::{AnyOutput, Io, Level}, interrupt::Priority, - peripherals::Peripherals, prelude::*, - system::SystemControl, timer::{timg::TimerGroup, ErasedTimer, OneShotTimer}, }; use esp_hal_embassy::InterruptExecutor; @@ -84,9 +81,11 @@ async fn enable_disable_led(control: &'static Signal ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/embassy_multiprio.rs b/examples/src/bin/embassy_multiprio.rs index c068be165c8..152ef03eb1b 100644 --- a/examples/src/bin/embassy_multiprio.rs +++ b/examples/src/bin/embassy_multiprio.rs @@ -24,10 +24,7 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Instant, Ticker, Timer}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, interrupt::Priority, - peripherals::Peripherals, - system::SystemControl, timer::{timg::TimerGroup, ErasedTimer, OneShotTimer}, }; use esp_hal_embassy::InterruptExecutor; @@ -83,27 +80,29 @@ async fn low_prio_async() { async fn main(low_prio_spawner: Spawner) { esp_println::logger::init_logger_from_env(); println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); let timer0: ErasedTimer = timg0.timer0.into(); let timer0 = OneShotTimer::new(timer0); - #[cfg(not(feature = "esp32c2"))] - let timer1 = { - let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); - let timer0: ErasedTimer = timg1.timer0.into(); - OneShotTimer::new(timer0) - }; - #[cfg(feature = "esp32c2")] - let timer1 = { - let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) - .split::(); - let alarm0: ErasedTimer = systimer.alarm0.into(); - OneShotTimer::new(alarm0) - }; + cfg_if::cfg_if! { + if #[cfg(feature = "esp32c2")] { + let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) + .split::(); + let alarm0: ErasedTimer = systimer.alarm0.into(); + let timer1 = OneShotTimer::new(alarm0); + } else { + let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); + let timer0: ErasedTimer = timg1.timer0.into(); + let timer1 = OneShotTimer::new(timer0); + } + } let timers = mk_static!([OneShotTimer; 2], [timer0, timer1]); esp_hal_embassy::init(&clocks, timers); diff --git a/examples/src/bin/embassy_parl_io_rx.rs b/examples/src/bin/embassy_parl_io_rx.rs index 43328f2e037..03393706f98 100644 --- a/examples/src/bin/embassy_parl_io_rx.rs +++ b/examples/src/bin/embassy_parl_io_rx.rs @@ -14,14 +14,11 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority}, dma_buffers, gpio::Io, parl_io::{no_clk_pin, BitPackOrder, ParlIoRxOnly, RxFourBits}, - peripherals::Peripherals, prelude::*, - system::SystemControl, timer::systimer::{SystemTimer, Target}, }; use esp_println::println; @@ -29,9 +26,11 @@ use esp_println::println; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { esp_println::println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); esp_hal_embassy::init(&clocks, systimer.alarm0); diff --git a/examples/src/bin/embassy_parl_io_tx.rs b/examples/src/bin/embassy_parl_io_tx.rs index ae747156dfc..2b5f96a8814 100644 --- a/examples/src/bin/embassy_parl_io_tx.rs +++ b/examples/src/bin/embassy_parl_io_tx.rs @@ -18,7 +18,6 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority}, dma_buffers, gpio::Io, @@ -30,9 +29,7 @@ use esp_hal::{ TxFourBits, TxPinConfigWithValidPin, }, - peripherals::Peripherals, prelude::*, - system::SystemControl, timer::systimer::{SystemTimer, Target}, }; use esp_println::println; @@ -40,9 +37,11 @@ use esp_println::println; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { esp_println::println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); esp_hal_embassy::init(&clocks, systimer.alarm0); diff --git a/examples/src/bin/embassy_rmt_rx.rs b/examples/src/bin/embassy_rmt_rx.rs index 16d48dce79c..4c91b896fcc 100644 --- a/examples/src/bin/embassy_rmt_rx.rs +++ b/examples/src/bin/embassy_rmt_rx.rs @@ -13,12 +13,9 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::{Gpio5, Io, Level, Output}, - peripherals::Peripherals, prelude::*, rmt::{asynch::RxChannelAsync, PulseCode, Rmt, RxChannelConfig, RxChannelCreatorAsync}, - system::SystemControl, timer::timg::TimerGroup, }; use esp_println::{print, println}; @@ -42,9 +39,11 @@ async fn signal_task(mut pin: Output<'static, Gpio5>) { #[esp_hal_embassy::main] async fn main(spawner: Spawner) { println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_rmt_tx.rs b/examples/src/bin/embassy_rmt_tx.rs index 8c179bcd02a..8b2e43fbf42 100644 --- a/examples/src/bin/embassy_rmt_tx.rs +++ b/examples/src/bin/embassy_rmt_tx.rs @@ -15,12 +15,9 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::Peripherals, prelude::*, rmt::{asynch::TxChannelAsync, PulseCode, Rmt, TxChannelConfig, TxChannelCreatorAsync}, - system::SystemControl, timer::timg::TimerGroup, }; use esp_println::println; @@ -28,9 +25,12 @@ use esp_println::println; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_serial.rs b/examples/src/bin/embassy_serial.rs index fc9ea25a82b..cffc78d2bf7 100644 --- a/examples/src/bin/embassy_serial.rs +++ b/examples/src/bin/embassy_serial.rs @@ -1,7 +1,7 @@ //! embassy serial //! //! This is an example of running the embassy executor and asynchronously -//! writing to and reading from uart +//! writing to and reading from UART. //% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3 //% FEATURES: async embassy embassy-generic-timers @@ -13,10 +13,8 @@ use embassy_executor::Spawner; use embassy_sync::{blocking_mutex::raw::NoopRawMutex, signal::Signal}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::{Peripherals, UART0}, - system::SystemControl, + peripherals::UART0, timer::timg::TimerGroup, uart::{ config::{AtCmdConfig, Config}, @@ -80,9 +78,11 @@ async fn reader( #[esp_hal_embassy::main] async fn main(spawner: Spawner) { esp_println::println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); @@ -90,20 +90,23 @@ async fn main(spawner: Spawner) { let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); // Default pins for Uart/Serial communication - #[cfg(feature = "esp32")] - let (tx_pin, rx_pin) = (io.pins.gpio1, io.pins.gpio3); - #[cfg(feature = "esp32c2")] - let (tx_pin, rx_pin) = (io.pins.gpio20, io.pins.gpio19); - #[cfg(feature = "esp32c3")] - let (tx_pin, rx_pin) = (io.pins.gpio21, io.pins.gpio20); - #[cfg(feature = "esp32c6")] - let (tx_pin, rx_pin) = (io.pins.gpio16, io.pins.gpio17); - #[cfg(feature = "esp32h2")] - let (tx_pin, rx_pin) = (io.pins.gpio24, io.pins.gpio23); - #[cfg(feature = "esp32s2")] - let (tx_pin, rx_pin) = (io.pins.gpio43, io.pins.gpio44); - #[cfg(feature = "esp32s3")] - let (tx_pin, rx_pin) = (io.pins.gpio43, io.pins.gpio44); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let (tx_pin, rx_pin) = (io.pins.gpio1, io.pins.gpio3); + } else if #[cfg(feature = "esp32c2")] { + let (tx_pin, rx_pin) = (io.pins.gpio20, io.pins.gpio19); + } else if #[cfg(feature = "esp32c3")] { + let (tx_pin, rx_pin) = (io.pins.gpio21, io.pins.gpio20); + } else if #[cfg(feature = "esp32c6")] { + let (tx_pin, rx_pin) = (io.pins.gpio16, io.pins.gpio17); + } else if #[cfg(feature = "esp32h2")] { + let (tx_pin, rx_pin) = (io.pins.gpio24, io.pins.gpio23); + } else if #[cfg(feature = "esp32s2")] { + let (tx_pin, rx_pin) = (io.pins.gpio43, io.pins.gpio44); + } else if #[cfg(feature = "esp32s3")] { + let (tx_pin, rx_pin) = (io.pins.gpio43, io.pins.gpio44); + } + } let config = Config::default().rx_fifo_full_threshold(READ_BUF_SIZE as u16); diff --git a/examples/src/bin/embassy_spi.rs b/examples/src/bin/embassy_spi.rs index 8f150e22245..19f3bf939f5 100644 --- a/examples/src/bin/embassy_spi.rs +++ b/examples/src/bin/embassy_spi.rs @@ -22,23 +22,22 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, dma::*, dma_buffers, gpio::Io, - peripherals::Peripherals, prelude::*, spi::{master::Spi, SpiMode}, - system::SystemControl, timer::timg::TimerGroup, }; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { esp_println::println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); @@ -51,10 +50,13 @@ async fn main(_spawner: Spawner) { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(32000); let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); diff --git a/examples/src/bin/embassy_touch.rs b/examples/src/bin/embassy_touch.rs index eb686243575..33399587ae6 100644 --- a/examples/src/bin/embassy_touch.rs +++ b/examples/src/bin/embassy_touch.rs @@ -17,11 +17,8 @@ use embassy_futures::select::{select, Either}; use embassy_time::{Duration, Timer}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::Peripherals, rtc_cntl::Rtc, - system::SystemControl, timer::timg::TimerGroup, touch::{Touch, TouchConfig, TouchPad}, }; @@ -31,9 +28,11 @@ use esp_println::println; async fn main(_spawner: Spawner) { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_twai.rs b/examples/src/bin/embassy_twai.rs index 6c0984d4db6..deb3a40c55d 100644 --- a/examples/src/bin/embassy_twai.rs +++ b/examples/src/bin/embassy_twai.rs @@ -25,11 +25,9 @@ use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Channel}; use embedded_can::{Frame, Id}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::Io, interrupt, - peripherals::{self, Peripherals, TWAI0}, - system::SystemControl, + peripherals::{self, TWAI0}, timer::timg::TimerGroup, twai::{self, EspTwaiFrame, TwaiMode, TwaiRx, TwaiTx}, }; @@ -84,9 +82,11 @@ async fn transmitter( #[esp_hal_embassy::main] async fn main(spawner: Spawner) { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_usb_serial.rs b/examples/src/bin/embassy_usb_serial.rs index cd37616c907..670eb07e42e 100644 --- a/examples/src/bin/embassy_usb_serial.rs +++ b/examples/src/bin/embassy_usb_serial.rs @@ -21,23 +21,22 @@ use embassy_usb::{ }; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::Io, otg_fs::{ asynch::{Config, Driver}, Usb, }, - peripherals::Peripherals, - system::SystemControl, timer::timg::TimerGroup, }; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) -> () { esp_println::println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_usb_serial_jtag.rs b/examples/src/bin/embassy_usb_serial_jtag.rs index 4282ed4c971..ea18f2f87e3 100644 --- a/examples/src/bin/embassy_usb_serial_jtag.rs +++ b/examples/src/bin/embassy_usb_serial_jtag.rs @@ -12,9 +12,6 @@ use embassy_executor::Spawner; use embassy_sync::{blocking_mutex::raw::NoopRawMutex, signal::Signal}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - system::SystemControl, timer::timg::TimerGroup, usb_serial_jtag::{UsbSerialJtag, UsbSerialJtagRx, UsbSerialJtagTx}, Async, @@ -65,9 +62,11 @@ async fn reader( #[esp_hal_embassy::main] async fn main(spawner: Spawner) -> () { esp_println::println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/embassy_wait.rs b/examples/src/bin/embassy_wait.rs index 82e8d3c86b2..a366f2adf9a 100644 --- a/examples/src/bin/embassy_wait.rs +++ b/examples/src/bin/embassy_wait.rs @@ -12,19 +12,18 @@ use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::{Input, Io, Pull}, - peripherals::Peripherals, - system::SystemControl, timer::timg::TimerGroup, }; #[esp_hal_embassy::main] async fn main(_spawner: Spawner) { esp_println::println!("Init!"); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); esp_hal_embassy::init(&clocks, timg0.timer0); diff --git a/examples/src/bin/etm_blinky_systimer.rs b/examples/src/bin/etm_blinky_systimer.rs index 89d5ce953ee..bc8f56eacb7 100644 --- a/examples/src/bin/etm_blinky_systimer.rs +++ b/examples/src/bin/etm_blinky_systimer.rs @@ -17,7 +17,6 @@ use esp_hal::{ Level, Pull, }, - peripherals::Peripherals, prelude::*, timer::systimer::{etm::SysTimerEtmEvent, Periodic, SystemTimer}, }; @@ -25,7 +24,7 @@ use fugit::ExtU32; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let syst = SystemTimer::new(peripherals.SYSTIMER); let syst_alarms = syst.split::(); diff --git a/examples/src/bin/etm_gpio.rs b/examples/src/bin/etm_gpio.rs index ecced8d304e..0f4f874a59f 100644 --- a/examples/src/bin/etm_gpio.rs +++ b/examples/src/bin/etm_gpio.rs @@ -17,13 +17,12 @@ use esp_hal::{ Level, Pull, }, - peripherals::Peripherals, prelude::*, }; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let mut led = io.pins.gpio1; diff --git a/examples/src/bin/etm_timer.rs b/examples/src/bin/etm_timer.rs index 802588313a2..5d752415194 100644 --- a/examples/src/bin/etm_timer.rs +++ b/examples/src/bin/etm_timer.rs @@ -11,12 +11,10 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, etm::Etm, - peripherals::{Peripherals, TIMG0}, + peripherals::TIMG0, prelude::*, - system::SystemControl, timer::timg::{ etm::{TimerEtmEvents, TimerEtmTasks}, Timer, @@ -30,9 +28,11 @@ static TIMER0: Mutex, esp_hal::Blocking>>>> = #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); let timer0 = timg0.timer0; diff --git a/examples/src/bin/gpio_interrupt.rs b/examples/src/bin/gpio_interrupt.rs index a4f0cfad313..4769bb272fa 100644 --- a/examples/src/bin/gpio_interrupt.rs +++ b/examples/src/bin/gpio_interrupt.rs @@ -16,13 +16,10 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio::{self, Event, Input, Io, Level, Output, Pull}, macros::ram, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] @@ -32,9 +29,11 @@ static BUTTON: Mutex>>> = Mutex::new(RefCell:: #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); // Set GPIO2 as an output, and set its state high initially. let mut io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/hello_rgb.rs b/examples/src/bin/hello_rgb.rs index e1223a06e5c..5fb99a6951a 100644 --- a/examples/src/bin/hello_rgb.rs +++ b/examples/src/bin/hello_rgb.rs @@ -25,15 +25,7 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - gpio::Io, - peripherals::Peripherals, - prelude::*, - rmt::Rmt, - system::SystemControl, -}; +use esp_hal::{delay::Delay, gpio::Io, prelude::*, rmt::Rmt}; use esp_hal_smartled::{smartLedBuffer, SmartLedsAdapter}; use smart_leds::{ brightness, @@ -44,9 +36,11 @@ use smart_leds::{ #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/hello_world.rs b/examples/src/bin/hello_world.rs index 59a819914a9..a37e96d8175 100644 --- a/examples/src/bin/hello_world.rs +++ b/examples/src/bin/hello_world.rs @@ -15,41 +15,38 @@ use core::fmt::Write; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - gpio::Io, - peripherals::Peripherals, - prelude::*, - system::SystemControl, - uart::Uart, -}; +use esp_hal::{delay::Delay, gpio::Io, prelude::*, uart::Uart}; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); // Default pins for Uart/Serial communication - #[cfg(feature = "esp32")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio1, io.pins.gpio3); - #[cfg(feature = "esp32c2")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio20, io.pins.gpio19); - #[cfg(feature = "esp32c3")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio21, io.pins.gpio20); - #[cfg(feature = "esp32c6")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio16, io.pins.gpio17); - #[cfg(feature = "esp32h2")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio24, io.pins.gpio23); - #[cfg(feature = "esp32s2")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio43, io.pins.gpio44); - #[cfg(feature = "esp32s3")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio43, io.pins.gpio44); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio1, io.pins.gpio3); + } else if #[cfg(feature = "esp32c2")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio20, io.pins.gpio19); + } else if #[cfg(feature = "esp32c3")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio21, io.pins.gpio20); + } else if #[cfg(feature = "esp32c6")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio16, io.pins.gpio17); + } else if #[cfg(feature = "esp32h2")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio24, io.pins.gpio23); + } else if #[cfg(feature = "esp32s2")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio43, io.pins.gpio44); + } else if #[cfg(feature = "esp32s3")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio43, io.pins.gpio44); + } + } let mut uart0 = Uart::new_with_default_pins(peripherals.UART0, &clocks, &mut tx_pin, &mut rx_pin).unwrap(); diff --git a/examples/src/bin/hmac.rs b/examples/src/bin/hmac.rs index 3df3ef86400..d4c520b2830 100644 --- a/examples/src/bin/hmac.rs +++ b/examples/src/bin/hmac.rs @@ -59,12 +59,9 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, hmac::{Hmac, HmacPurpose, KeyId}, - peripherals::Peripherals, prelude::*, rng::Rng, - system::SystemControl, timer::systimer::SystemTimer, }; use esp_println::println; @@ -76,9 +73,7 @@ type HmacSha256 = HmacSw; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let _clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let mut rng = Rng::new(peripherals.RNG); diff --git a/examples/src/bin/i2c_bmp180_calibration_data.rs b/examples/src/bin/i2c_bmp180_calibration_data.rs index adf19b1da9c..f80ba7b0c57 100644 --- a/examples/src/bin/i2c_bmp180_calibration_data.rs +++ b/examples/src/bin/i2c_bmp180_calibration_data.rs @@ -12,21 +12,16 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - gpio::Io, - i2c::I2C, - peripherals::Peripherals, - prelude::*, - system::SystemControl, -}; +use esp_hal::{gpio::Io, i2c::I2C, prelude::*}; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/i2c_display.rs b/examples/src/bin/i2c_display.rs index 7b874872a25..c9332482747 100644 --- a/examples/src/bin/i2c_display.rs +++ b/examples/src/bin/i2c_display.rs @@ -23,22 +23,16 @@ use embedded_graphics::{ text::{Alignment, Text}, }; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - gpio::Io, - i2c::I2C, - peripherals::Peripherals, - prelude::*, - system::SystemControl, -}; +use esp_hal::{delay::Delay, gpio::Io, i2c::I2C, prelude::*}; use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306}; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/i2s_read.rs b/examples/src/bin/i2s_read.rs index 395e1b23257..ecf20f1ff6b 100644 --- a/examples/src/bin/i2s_read.rs +++ b/examples/src/bin/i2s_read.rs @@ -18,22 +18,21 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority}, dma_buffers, gpio::Io, i2s::{DataFormat, I2s, I2sReadDma, Standard}, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/i2s_sound.rs b/examples/src/bin/i2s_sound.rs index 991b39d3381..cee33aa8454 100644 --- a/examples/src/bin/i2s_sound.rs +++ b/examples/src/bin/i2s_sound.rs @@ -1,7 +1,7 @@ -//! This shows how to transmit data continously via I2S. +//! This shows how to transmit data continuously via I2S. //! //! Without an additional I2S sink device you can inspect the MCLK, BCLK, WS -//! andDOUT with a logic analyzer. +//! and DOUT with a logic analyzer. //! //! You can also connect e.g. a PCM510x to hear an annoying loud sine tone (full //! scale), so turn down the volume before running this example. @@ -32,14 +32,11 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority}, dma_buffers, gpio::Io, i2s::{DataFormat, I2s, I2sWriteDma, Standard}, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; const SINE: [i16; 64] = [ @@ -52,9 +49,11 @@ const SINE: [i16; 64] = [ #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/ieee802154_receive_all_frames.rs b/examples/src/bin/ieee802154_receive_all_frames.rs index 5adb3c0ad0c..63283096c28 100644 --- a/examples/src/bin/ieee802154_receive_all_frames.rs +++ b/examples/src/bin/ieee802154_receive_all_frames.rs @@ -4,13 +4,13 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{peripherals::Peripherals, prelude::*}; +use esp_hal::prelude::*; use esp_ieee802154::*; use esp_println::println; #[entry] fn main() -> ! { - let mut peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, &mut peripherals.RADIO_CLK); ieee802154.set_config(Config { diff --git a/examples/src/bin/ieee802154_receive_frame.rs b/examples/src/bin/ieee802154_receive_frame.rs index 07e5de3f071..8d8cc9a7a97 100644 --- a/examples/src/bin/ieee802154_receive_frame.rs +++ b/examples/src/bin/ieee802154_receive_frame.rs @@ -4,13 +4,15 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{peripherals::Peripherals, prelude::*}; +use esp_hal::prelude::*; use esp_ieee802154::*; use esp_println::println; #[entry] fn main() -> ! { - let mut peripherals = Peripherals::take(); + let System { + mut peripherals, .. + } = esp_hal::init(CpuClock::boot_default()); let mut ieee802154 = Ieee802154::new(peripherals.IEEE802154, &mut peripherals.RADIO_CLK); ieee802154.set_config(Config { diff --git a/examples/src/bin/ieee802154_send_broadcast_frame.rs b/examples/src/bin/ieee802154_send_broadcast_frame.rs index 070cf8e7983..cb6c3f83e2f 100644 --- a/examples/src/bin/ieee802154_send_broadcast_frame.rs +++ b/examples/src/bin/ieee802154_send_broadcast_frame.rs @@ -4,13 +4,7 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - peripherals::Peripherals, - prelude::*, - system::SystemControl, -}; +use esp_hal::{delay::Delay, prelude::*}; use esp_ieee802154::*; use esp_println::println; use ieee802154::mac::{ @@ -25,9 +19,11 @@ use ieee802154::mac::{ #[entry] fn main() -> ! { - let mut peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + mut peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); diff --git a/examples/src/bin/ieee802154_send_frame.rs b/examples/src/bin/ieee802154_send_frame.rs index 7992a71694e..0b58de65f59 100644 --- a/examples/src/bin/ieee802154_send_frame.rs +++ b/examples/src/bin/ieee802154_send_frame.rs @@ -4,13 +4,7 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - peripherals::Peripherals, - prelude::*, - system::SystemControl, -}; +use esp_hal::{delay::Delay, prelude::*}; use esp_ieee802154::*; use esp_println::println; use ieee802154::mac::{ @@ -25,9 +19,11 @@ use ieee802154::mac::{ #[entry] fn main() -> ! { - let mut peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + mut peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); diff --git a/examples/src/bin/ieee802154_sniffer.rs b/examples/src/bin/ieee802154_sniffer.rs index 09bd2dd84bb..f6adeea22af 100644 --- a/examples/src/bin/ieee802154_sniffer.rs +++ b/examples/src/bin/ieee802154_sniffer.rs @@ -8,31 +8,28 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - gpio::Io, - peripherals::Peripherals, - prelude::*, - reset::software_reset, - system::SystemControl, - uart::Uart, -}; +use esp_hal::{gpio::Io, prelude::*, reset::software_reset, uart::Uart}; use esp_ieee802154::*; use esp_println::println; #[entry] fn main() -> ! { - let mut peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + mut peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); // Default pins for Uart/Serial communication - #[cfg(feature = "esp32c6")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio16, io.pins.gpio17); - #[cfg(feature = "esp32h2")] - let (mut tx_pin, mut rx_pin) = (io.pins.gpio24, io.pins.gpio23); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32c6")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio16, io.pins.gpio17); + } else if #[cfg(feature = "esp32h2")] { + let (mut tx_pin, mut rx_pin) = (io.pins.gpio24, io.pins.gpio23); + } + } let mut uart0 = Uart::new_with_default_pins(peripherals.UART0, &clocks, &mut tx_pin, &mut rx_pin).unwrap(); diff --git a/examples/src/bin/lcd_cam_ov2640.rs b/examples/src/bin/lcd_cam_ov2640.rs index 07f12f8a1cf..66af3da963e 100644 --- a/examples/src/bin/lcd_cam_ov2640.rs +++ b/examples/src/bin/lcd_cam_ov2640.rs @@ -25,7 +25,6 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, dma::{Dma, DmaPriority}, dma_buffers, @@ -36,18 +35,18 @@ use esp_hal::{ cam::{Camera, RxEightBits}, LcdCam, }, - peripherals::Peripherals, prelude::*, - system::SystemControl, Blocking, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/lcd_i8080.rs b/examples/src/bin/lcd_i8080.rs index b3b02a06292..f332e1c3b48 100644 --- a/examples/src/bin/lcd_i8080.rs +++ b/examples/src/bin/lcd_i8080.rs @@ -23,7 +23,6 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, dma::{Dma, DmaPriority}, dma_buffers, @@ -32,17 +31,17 @@ use esp_hal::{ lcd::i8080::{Config, TxEightBits, I8080}, LcdCam, }, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/ledc.rs b/examples/src/bin/ledc.rs index 94d8dbdb3c9..197e488b972 100644 --- a/examples/src/bin/ledc.rs +++ b/examples/src/bin/ledc.rs @@ -11,7 +11,6 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::Io, ledc::{ channel::{self, ChannelIFace}, @@ -20,16 +19,16 @@ use esp_hal::{ Ledc, LowSpeed, }, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let led = io.pins.gpio0; diff --git a/examples/src/bin/lp_core_basic.rs b/examples/src/bin/lp_core_basic.rs index 81dc1a7a632..a86affe6ca9 100644 --- a/examples/src/bin/lp_core_basic.rs +++ b/examples/src/bin/lp_core_basic.rs @@ -17,14 +17,13 @@ use esp_backtrace as _; use esp_hal::{ gpio::{lp_io::LowPowerOutput, Io}, lp_core::{LpCore, LpCoreWakeupSource}, - peripherals::Peripherals, prelude::*, }; use esp_println::{print, println}; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); // configure GPIO 1 as LP output pin let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/lp_core_i2c.rs b/examples/src/bin/lp_core_i2c.rs index b285b4b172d..b384f91a5d1 100644 --- a/examples/src/bin/lp_core_i2c.rs +++ b/examples/src/bin/lp_core_i2c.rs @@ -19,14 +19,13 @@ use esp_hal::{ gpio::{lp_io::LowPowerOutputOpenDrain, Io}, i2c::lp_i2c::LpI2c, lp_core::{LpCore, LpCoreWakeupSource}, - peripherals::Peripherals, prelude::*, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/lp_core_uart.rs b/examples/src/bin/lp_core_uart.rs index df795db9682..5ca98a3e743 100644 --- a/examples/src/bin/lp_core_uart.rs +++ b/examples/src/bin/lp_core_uart.rs @@ -16,24 +16,23 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::{ lp_io::{LowPowerInput, LowPowerOutput}, Io, }, lp_core::{LpCore, LpCoreWakeupSource}, - peripherals::Peripherals, prelude::*, - system::SystemControl, uart::{config::Config, lp_uart::LpUart, Uart}, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/mcpwm.rs b/examples/src/bin/mcpwm.rs index 2e81f992f4b..848db4884ad 100644 --- a/examples/src/bin/mcpwm.rs +++ b/examples/src/bin/mcpwm.rs @@ -11,28 +11,30 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::Io, mcpwm::{operator::PwmPinConfig, timer::PwmWorkingMode, McPwm, PeripheralClockConfig}, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let pin = io.pins.gpio0; // initialize peripheral - #[cfg(feature = "esp32h2")] - let clock_cfg = PeripheralClockConfig::with_frequency(&clocks, 40.MHz()).unwrap(); - #[cfg(not(feature = "esp32h2"))] - let clock_cfg = PeripheralClockConfig::with_frequency(&clocks, 32.MHz()).unwrap(); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32h2")] { + let clock_cfg = PeripheralClockConfig::with_frequency(&clocks, 40.MHz()).unwrap(); + } else { + let clock_cfg = PeripheralClockConfig::with_frequency(&clocks, 32.MHz()).unwrap(); + } + } let mut mcpwm = McPwm::new(peripherals.MCPWM0, clock_cfg); diff --git a/examples/src/bin/multicore.rs b/examples/src/bin/multicore.rs index 1d028bdc9b9..da1c30bfcb5 100644 --- a/examples/src/bin/multicore.rs +++ b/examples/src/bin/multicore.rs @@ -13,12 +13,9 @@ use core::{cell::RefCell, ptr::addr_of_mut}; use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, cpu_control::{CpuControl, Stack}, delay::Delay, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use esp_println::println; @@ -26,9 +23,11 @@ static mut APP_CORE_STACK: Stack<8192> = Stack::new(); #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); diff --git a/examples/src/bin/parl_io_rx.rs b/examples/src/bin/parl_io_rx.rs index 0606667f75f..1ac2a674653 100644 --- a/examples/src/bin/parl_io_rx.rs +++ b/examples/src/bin/parl_io_rx.rs @@ -11,23 +11,22 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, dma::{Dma, DmaPriority}, dma_buffers, gpio::Io, parl_io::{no_clk_pin, BitPackOrder, ParlIoRxOnly, RxFourBits}, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/parl_io_tx.rs b/examples/src/bin/parl_io_tx.rs index 60536033a9e..21350a45b96 100644 --- a/examples/src/bin/parl_io_tx.rs +++ b/examples/src/bin/parl_io_tx.rs @@ -15,7 +15,6 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, dma::{Dma, DmaPriority}, dma_buffers, @@ -28,17 +27,17 @@ use esp_hal::{ TxFourBits, TxPinConfigWithValidPin, }, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/pcnt_encoder.rs b/examples/src/bin/pcnt_encoder.rs index 8e22d236b77..c97eb848699 100644 --- a/examples/src/bin/pcnt_encoder.rs +++ b/examples/src/bin/pcnt_encoder.rs @@ -1,6 +1,6 @@ //! PCNT Encoder Demo //! -//! This example decodes a quadrature encoder +//! This example decodes a quadrature encoder. //! //! Since the PCNT units reset to zero when they reach their limits //! we enable an interrupt on the upper and lower limits and @@ -27,7 +27,6 @@ use esp_hal::{ unit, Pcnt, }, - peripherals::Peripherals, prelude::*, }; use esp_println::println; @@ -38,7 +37,7 @@ static VALUE: AtomicI32 = AtomicI32::new(0); #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/psram_octal.rs b/examples/src/bin/psram_octal.rs index 1916ba4d0c3..9f1be148c08 100644 --- a/examples/src/bin/psram_octal.rs +++ b/examples/src/bin/psram_octal.rs @@ -13,7 +13,7 @@ extern crate alloc; use alloc::{string::String, vec::Vec}; use esp_backtrace as _; -use esp_hal::{peripherals::Peripherals, prelude::*, psram}; +use esp_hal::{prelude::*, psram}; use esp_println::println; #[global_allocator] @@ -30,7 +30,7 @@ fn main() -> ! { #[cfg(debug_assertions)] compile_error!("This example MUST be built in release mode!"); - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); psram::init_psram(peripherals.PSRAM); init_psram_heap(); diff --git a/examples/src/bin/psram_quad.rs b/examples/src/bin/psram_quad.rs index 47dfc69ceb8..7a4eb58d2c2 100644 --- a/examples/src/bin/psram_quad.rs +++ b/examples/src/bin/psram_quad.rs @@ -13,13 +13,7 @@ extern crate alloc; use alloc::{string::String, vec::Vec}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - prelude::*, - psram, - system::SystemControl, -}; +use esp_hal::{clock::ClockControl, prelude::*, psram}; use esp_println::println; #[global_allocator] @@ -36,14 +30,11 @@ fn main() -> ! { #[cfg(debug_assertions)] compile_error!("PSRAM example must be built in release mode!"); - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); psram::init_psram(peripherals.PSRAM); init_psram_heap(); - let system = SystemControl::new(peripherals.SYSTEM); - let _clocks = ClockControl::max(system.clock_control).freeze(); - println!("Going to access PSRAM"); let mut large_vec = Vec::::with_capacity(500 * 1024 / 4); diff --git a/examples/src/bin/qspi_flash.rs b/examples/src/bin/qspi_flash.rs index 10b2e6b0b0e..7d575a14878 100644 --- a/examples/src/bin/qspi_flash.rs +++ b/examples/src/bin/qspi_flash.rs @@ -29,27 +29,26 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, dma::{Dma, DmaPriority, DmaRxBuf, DmaTxBuf}, dma_buffers, gpio::Io, - peripherals::Peripherals, prelude::*, spi::{ master::{Address, Command, Spi}, SpiDataMode, SpiMode, }, - system::SystemControl, }; use esp_println::{print, println}; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); cfg_if::cfg_if! { @@ -71,10 +70,14 @@ fn main() -> ! { } let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(256, 320); let mut dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); diff --git a/examples/src/bin/ram.rs b/examples/src/bin/ram.rs index bd165d99e2d..d5218a644a4 100644 --- a/examples/src/bin/ram.rs +++ b/examples/src/bin/ram.rs @@ -17,15 +17,7 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - macros::ram, - peripherals::Peripherals, - prelude::*, - rtc_cntl::Rtc, - system::SystemControl, -}; +use esp_hal::{delay::Delay, macros::ram, prelude::*, rtc_cntl::Rtc}; use esp_println::println; #[ram(rtc_fast)] @@ -39,9 +31,11 @@ static mut SOME_ZEROED_DATA: [u8; 8] = [0; 8]; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); diff --git a/examples/src/bin/rmt_rx.rs b/examples/src/bin/rmt_rx.rs index ca9984cec28..6bda14a9988 100644 --- a/examples/src/bin/rmt_rx.rs +++ b/examples/src/bin/rmt_rx.rs @@ -13,13 +13,10 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio::Io, - peripherals::Peripherals, prelude::*, rmt::{PulseCode, Rmt, RxChannel, RxChannelConfig, RxChannelCreator}, - system::SystemControl, }; use esp_println::{print, println}; @@ -27,9 +24,11 @@ const WIDTH: usize = 80; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let mut out = io.pins.gpio5; diff --git a/examples/src/bin/rmt_tx.rs b/examples/src/bin/rmt_tx.rs index d6ef6cf3953..eb1e556c568 100644 --- a/examples/src/bin/rmt_tx.rs +++ b/examples/src/bin/rmt_tx.rs @@ -12,20 +12,19 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio::Io, - peripherals::Peripherals, prelude::*, rmt::{PulseCode, Rmt, TxChannel, TxChannelConfig, TxChannelCreator}, - system::SystemControl, }; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/rng.rs b/examples/src/bin/rng.rs index fc87b7d8fda..2fb85892846 100644 --- a/examples/src/bin/rng.rs +++ b/examples/src/bin/rng.rs @@ -6,12 +6,12 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{peripherals::Peripherals, prelude::*, rng::Rng}; +use esp_hal::{prelude::*, rng::Rng}; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let mut rng = Rng::new(peripherals.RNG); // Generate a random word (u32): diff --git a/examples/src/bin/rtc_time.rs b/examples/src/bin/rtc_time.rs index c4e9b876b06..8a30b8aa3d6 100644 --- a/examples/src/bin/rtc_time.rs +++ b/examples/src/bin/rtc_time.rs @@ -6,20 +6,15 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - peripherals::Peripherals, - prelude::*, - rtc_cntl::Rtc, - system::SystemControl, -}; +use esp_hal::{delay::Delay, prelude::*, rtc_cntl::Rtc}; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let rtc = Rtc::new(peripherals.LPWR); let delay = Delay::new(&clocks); diff --git a/examples/src/bin/rtc_watchdog.rs b/examples/src/bin/rtc_watchdog.rs index cddd09dcf8e..47fbee2fe47 100644 --- a/examples/src/bin/rtc_watchdog.rs +++ b/examples/src/bin/rtc_watchdog.rs @@ -1,4 +1,4 @@ -//! This demos the RTC Watchdog Timer (RWDT). +//! This example demonstrates the RTC Watchdog Timer (RWDT). //! //! The RWDT is initially configured to trigger an interrupt after a given //! timeout. Then, upon expiration, the RWDT is restarted and then reconfigured @@ -14,16 +14,17 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ - peripherals::Peripherals, + interrupt::Priority, prelude::*, rtc_cntl::{Rtc, Rwdt}, }; +use esp_println::println; static RWDT: Mutex>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let mut rtc = Rtc::new(peripherals.LPWR); rtc.set_interrupt_handler(interrupt_handler); @@ -35,16 +36,16 @@ fn main() -> ! { loop {} } -#[handler(priority = esp_hal::interrupt::Priority::min())] +#[handler(priority = Priority::min())] fn interrupt_handler() { critical_section::with(|cs| { - esp_println::println!("RWDT Interrupt"); + println!("RWDT Interrupt"); let mut rwdt = RWDT.borrow_ref_mut(cs); let rwdt = rwdt.as_mut().unwrap(); rwdt.clear_interrupt(); - esp_println::println!("Restarting in 5 seconds..."); + println!("Restarting in 5 seconds..."); rwdt.set_timeout(5000.millis()); rwdt.unlisten(); diff --git a/examples/src/bin/serial_interrupts.rs b/examples/src/bin/serial_interrupts.rs index 8af7b9a31e8..04b6de195fc 100644 --- a/examples/src/bin/serial_interrupts.rs +++ b/examples/src/bin/serial_interrupts.rs @@ -12,12 +12,10 @@ use core::{cell::RefCell, fmt::Write}; use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio::Io, - peripherals::{Peripherals, UART0}, + peripherals::UART0, prelude::*, - system::SystemControl, uart::{ config::{AtCmdConfig, Config}, Uart, @@ -29,29 +27,35 @@ static SERIAL: Mutex>>> = Mutex::new(RefCel #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); // Default pins for Uart/Serial communication - #[cfg(feature = "esp32")] - let (tx_pin, rx_pin) = (io.pins.gpio1, io.pins.gpio3); - #[cfg(feature = "esp32c2")] - let (tx_pin, rx_pin) = (io.pins.gpio20, io.pins.gpio19); - #[cfg(feature = "esp32c3")] - let (tx_pin, rx_pin) = (io.pins.gpio21, io.pins.gpio20); - #[cfg(feature = "esp32c6")] - let (tx_pin, rx_pin) = (io.pins.gpio16, io.pins.gpio17); - #[cfg(feature = "esp32h2")] - let (tx_pin, rx_pin) = (io.pins.gpio24, io.pins.gpio23); - #[cfg(feature = "esp32s2")] - let (tx_pin, rx_pin) = (io.pins.gpio43, io.pins.gpio44); - #[cfg(feature = "esp32s3")] - let (tx_pin, rx_pin) = (io.pins.gpio43, io.pins.gpio44); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let (tx_pin, rx_pin) = (io.pins.gpio1, io.pins.gpio3); + } else if #[cfg(feature = "esp32c2")] { + let (tx_pin, rx_pin) = (io.pins.gpio20, io.pins.gpio19); + } else if #[cfg(feature = "esp32c3")] { + let (tx_pin, rx_pin) = (io.pins.gpio21, io.pins.gpio20); + } else if #[cfg(feature = "esp32c6")] { + let (tx_pin, rx_pin) = (io.pins.gpio16, io.pins.gpio17); + } else if #[cfg(feature = "esp32h2")] { + let (tx_pin, rx_pin) = (io.pins.gpio24, io.pins.gpio23); + } else if #[cfg(feature = "esp32s2")] { + let (tx_pin, rx_pin) = (io.pins.gpio43, io.pins.gpio44); + } else if #[cfg(feature = "esp32s3")] { + let (tx_pin, rx_pin) = (io.pins.gpio43, io.pins.gpio44); + } + } + let config = Config::default().rx_fifo_full_threshold(30); let mut uart0 = diff --git a/examples/src/bin/sleep_timer.rs b/examples/src/bin/sleep_timer.rs index fa882bd9ac4..19c6eaf2abc 100644 --- a/examples/src/bin/sleep_timer.rs +++ b/examples/src/bin/sleep_timer.rs @@ -9,21 +9,20 @@ use core::time::Duration; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, entry, - peripherals::Peripherals, rtc_cntl::{get_reset_reason, get_wakeup_cause, sleep::TimerWakeupSource, Rtc, SocResetReason}, - system::SystemControl, Cpu, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); let mut rtc = Rtc::new(peripherals.LPWR); diff --git a/examples/src/bin/sleep_timer_ext0.rs b/examples/src/bin/sleep_timer_ext0.rs index b54e646871a..56b47302a8b 100644 --- a/examples/src/bin/sleep_timer_ext0.rs +++ b/examples/src/bin/sleep_timer_ext0.rs @@ -12,11 +12,9 @@ use core::time::Duration; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, entry, gpio::Io, - peripherals::Peripherals, rtc_cntl::{ get_reset_reason, get_wakeup_cause, @@ -24,16 +22,17 @@ use esp_hal::{ Rtc, SocResetReason, }, - system::SystemControl, Cpu, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let mut rtc = Rtc::new(peripherals.LPWR); diff --git a/examples/src/bin/sleep_timer_ext1.rs b/examples/src/bin/sleep_timer_ext1.rs index 2f702642ffe..e2a5602140e 100644 --- a/examples/src/bin/sleep_timer_ext1.rs +++ b/examples/src/bin/sleep_timer_ext1.rs @@ -12,11 +12,9 @@ use core::time::Duration; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, entry, gpio::{Io, RtcPin}, - peripherals::Peripherals, rtc_cntl::{ get_reset_reason, get_wakeup_cause, @@ -24,16 +22,17 @@ use esp_hal::{ Rtc, SocResetReason, }, - system::SystemControl, Cpu, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let mut rtc = Rtc::new(peripherals.LPWR); diff --git a/examples/src/bin/sleep_timer_lpio.rs b/examples/src/bin/sleep_timer_lpio.rs index 63c7074f422..cd1b6de2dd6 100644 --- a/examples/src/bin/sleep_timer_lpio.rs +++ b/examples/src/bin/sleep_timer_lpio.rs @@ -13,11 +13,9 @@ use core::time::Duration; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, entry, gpio::{Io, RtcPinWithResistors}, - peripherals::Peripherals, rtc_cntl::{ get_reset_reason, get_wakeup_cause, @@ -25,16 +23,17 @@ use esp_hal::{ Rtc, SocResetReason, }, - system::SystemControl, Cpu, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let mut rtc = Rtc::new(peripherals.LPWR); diff --git a/examples/src/bin/sleep_timer_rtcio.rs b/examples/src/bin/sleep_timer_rtcio.rs index b8e6e727448..e099ac37a96 100644 --- a/examples/src/bin/sleep_timer_rtcio.rs +++ b/examples/src/bin/sleep_timer_rtcio.rs @@ -16,12 +16,10 @@ use core::time::Duration; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, entry, gpio, gpio::Io, - peripherals::Peripherals, rtc_cntl::{ get_reset_reason, get_wakeup_cause, @@ -29,16 +27,17 @@ use esp_hal::{ Rtc, SocResetReason, }, - system::SystemControl, Cpu, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let mut io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let mut rtc = Rtc::new(peripherals.LPWR); @@ -52,15 +51,17 @@ fn main() -> ! { let delay = Delay::new(&clocks); let timer = TimerWakeupSource::new(Duration::from_secs(10)); - #[cfg(feature = "esp32c3")] - let wakeup_pins: &mut [(&mut dyn gpio::RtcPinWithResistors, WakeupLevel)] = &mut [ - (&mut io.pins.gpio2, WakeupLevel::Low), - (&mut io.pins.gpio3, WakeupLevel::High), - ]; - - #[cfg(feature = "esp32s3")] - let wakeup_pins: &mut [(&mut dyn gpio::RtcPin, WakeupLevel)] = - &mut [(&mut io.pins.gpio18, WakeupLevel::Low)]; + cfg_if::cfg_if! { + if #[cfg(feature = "esp32c3")] { + let wakeup_pins: &mut [(&mut dyn gpio::RtcPinWithResistors, WakeupLevel)] = &mut [ + (&mut io.pins.gpio2, WakeupLevel::Low), + (&mut io.pins.gpio3, WakeupLevel::High), + ]; + } else if #[cfg(feature = "esp32s3")] { + let wakeup_pins: &mut [(&mut dyn gpio::RtcPin, WakeupLevel)] = + &mut [(&mut io.pins.gpio18, WakeupLevel::Low)]; + } + } let rtcio = RtcioWakeupSource::new(wakeup_pins); println!("sleeping!"); diff --git a/examples/src/bin/software_interrupts.rs b/examples/src/bin/software_interrupts.rs index 5fa0f5252b9..b15e233cde4 100644 --- a/examples/src/bin/software_interrupts.rs +++ b/examples/src/bin/software_interrupts.rs @@ -13,13 +13,7 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - peripherals::Peripherals, - prelude::*, - system::{SoftwareInterrupt, SystemControl}, -}; +use esp_hal::{delay::Delay, prelude::*, system::SoftwareInterrupt}; static SWINT0: Mutex>>> = Mutex::new(RefCell::new(None)); static SWINT1: Mutex>>> = Mutex::new(RefCell::new(None)); @@ -28,11 +22,12 @@ static SWINT3: Mutex>>> = Mutex::new(RefCell #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + clocks, + software_interrupt_control: mut sw_int, + .. + } = esp_hal::init(CpuClock::boot_default()); - let mut sw_int = system.software_interrupt_control; critical_section::with(|cs| { sw_int .software_interrupt0 diff --git a/examples/src/bin/spi_halfduplex_read_manufacturer_id.rs b/examples/src/bin/spi_halfduplex_read_manufacturer_id.rs index 8e56ab17b17..e0a01e1c3ce 100644 --- a/examples/src/bin/spi_halfduplex_read_manufacturer_id.rs +++ b/examples/src/bin/spi_halfduplex_read_manufacturer_id.rs @@ -29,25 +29,24 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio::Io, - peripherals::Peripherals, prelude::*, spi::{ master::{Address, Command, HalfDuplexReadWrite, Spi}, SpiDataMode, SpiMode, }, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); cfg_if::cfg_if! { diff --git a/examples/src/bin/spi_loopback.rs b/examples/src/bin/spi_loopback.rs index 735321644a1..edc914acf3c 100644 --- a/examples/src/bin/spi_loopback.rs +++ b/examples/src/bin/spi_loopback.rs @@ -20,21 +20,20 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio::{any_pin::AnyPin, Io}, - peripherals::Peripherals, prelude::*, spi::{master::Spi, SpiMode}, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; diff --git a/examples/src/bin/spi_loopback_dma.rs b/examples/src/bin/spi_loopback_dma.rs index 09b65c76ede..454a3286ecd 100644 --- a/examples/src/bin/spi_loopback_dma.rs +++ b/examples/src/bin/spi_loopback_dma.rs @@ -34,9 +34,11 @@ use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; @@ -46,10 +48,13 @@ fn main() -> ! { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(32000); let mut dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); diff --git a/examples/src/bin/spi_slave_dma.rs b/examples/src/bin/spi_slave_dma.rs index 6ab99df99a1..2070b000b1d 100644 --- a/examples/src/bin/spi_slave_dma.rs +++ b/examples/src/bin/spi_slave_dma.rs @@ -31,26 +31,25 @@ use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, dma::{Dma, DmaPriority}, dma_buffers, gpio::{Gpio4, Gpio5, Gpio8, Gpio9, Input, Io, Level, Output, Pull}, - peripherals::Peripherals, prelude::*, spi::{ slave::{prelude::*, Spi}, SpiMode, }, - system::SystemControl, }; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let slave_sclk = io.pins.gpio0; diff --git a/examples/src/bin/systimer.rs b/examples/src/bin/systimer.rs index cdbf345a96b..05b11715e7f 100644 --- a/examples/src/bin/systimer.rs +++ b/examples/src/bin/systimer.rs @@ -12,11 +12,8 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, - peripherals::Peripherals, prelude::*, - system::SystemControl, timer::systimer::{ Alarm, FrozenUnit, @@ -50,9 +47,11 @@ static ALARM2: Mutex< #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let systimer = SystemTimer::new(peripherals.SYSTIMER); println!("SYSTIMER Current value = {}", SystemTimer::now()); diff --git a/examples/src/bin/timer_interrupt.rs b/examples/src/bin/timer_interrupt.rs index b22d10aadd4..fe78ef8fb6b 100644 --- a/examples/src/bin/timer_interrupt.rs +++ b/examples/src/bin/timer_interrupt.rs @@ -12,11 +12,9 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, interrupt::{self, Priority}, - peripherals::{Interrupt, Peripherals, TIMG0}, + peripherals::{Interrupt, TIMG0}, prelude::*, - system::SystemControl, timer::timg::{Timer, Timer0, TimerGroup}, }; @@ -25,9 +23,11 @@ static TIMER0: Mutex, esp_hal::Blocking>>>> = #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); let timer0 = timg0.timer0; diff --git a/examples/src/bin/touch.rs b/examples/src/bin/touch.rs index f7870b07c22..5a7d69e7c99 100644 --- a/examples/src/bin/touch.rs +++ b/examples/src/bin/touch.rs @@ -16,15 +16,12 @@ use core::cell::RefCell; use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, gpio, gpio::Io, macros::ram, - peripherals::Peripherals, prelude::*, rtc_cntl::Rtc, - system::SystemControl, touch::{Continous, Touch, TouchConfig, TouchPad}, Blocking, }; @@ -51,9 +48,11 @@ fn interrupt_handler() { #[entry] fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/twai.rs b/examples/src/bin/twai.rs index 374285b3e5c..546b978d3db 100644 --- a/examples/src/bin/twai.rs +++ b/examples/src/bin/twai.rs @@ -26,11 +26,8 @@ const IS_FIRST_SENDER: bool = true; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::Peripherals, prelude::*, - system::SystemControl, twai::{self, filter::SingleStandardFilter, EspTwaiFrame, StandardId, TwaiMode}, }; use esp_println::println; @@ -38,9 +35,11 @@ use nb::block; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/ulp_riscv_core_basic.rs b/examples/src/bin/ulp_riscv_core_basic.rs index 578e7e6a31d..5477b94d671 100644 --- a/examples/src/bin/ulp_riscv_core_basic.rs +++ b/examples/src/bin/ulp_riscv_core_basic.rs @@ -14,7 +14,6 @@ use esp_backtrace as _; use esp_hal::{ gpio::{rtc_io::*, Io}, - peripherals::Peripherals, prelude::*, ulp_core, }; @@ -22,7 +21,7 @@ use esp_println::{print, println}; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let pin = LowPowerOutput::new(io.pins.gpio1); diff --git a/examples/src/bin/usb_serial.rs b/examples/src/bin/usb_serial.rs index 4032bc8a0e4..fc3cbde68a7 100644 --- a/examples/src/bin/usb_serial.rs +++ b/examples/src/bin/usb_serial.rs @@ -17,7 +17,6 @@ use esp_backtrace as _; use esp_hal::{ gpio::Io, otg_fs::{Usb, UsbBus}, - peripherals::Peripherals, prelude::*, }; use usb_device::prelude::{UsbDeviceBuilder, UsbVidPid}; @@ -27,7 +26,7 @@ static mut EP_MEMORY: [u32; 1024] = [0; 1024]; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/examples/src/bin/usb_serial_jtag.rs b/examples/src/bin/usb_serial_jtag.rs index d87ab1a7978..a79dfbc11be 100644 --- a/examples/src/bin/usb_serial_jtag.rs +++ b/examples/src/bin/usb_serial_jtag.rs @@ -16,24 +16,18 @@ use core::{cell::RefCell, fmt::Write}; use critical_section::Mutex; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - peripherals::Peripherals, - prelude::*, - system::SystemControl, - usb_serial_jtag::UsbSerialJtag, - Blocking, -}; +use esp_hal::{delay::Delay, prelude::*, usb_serial_jtag::UsbSerialJtag, Blocking}; static USB_SERIAL: Mutex>>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); diff --git a/examples/src/bin/watchdog.rs b/examples/src/bin/watchdog.rs index 00ba024813c..9813f620fd7 100644 --- a/examples/src/bin/watchdog.rs +++ b/examples/src/bin/watchdog.rs @@ -9,21 +9,16 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - peripherals::Peripherals, - prelude::*, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{delay::Delay, prelude::*, timer::timg::TimerGroup}; use esp_println::println; #[entry] fn main() -> ! { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let delay = Delay::new(&clocks); diff --git a/examples/src/bin/wifi_80211_tx.rs b/examples/src/bin/wifi_80211_tx.rs index d3d70ee062a..59ecc0a05a9 100644 --- a/examples/src/bin/wifi_80211_tx.rs +++ b/examples/src/bin/wifi_80211_tx.rs @@ -12,12 +12,9 @@ use core::marker::PhantomData; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, delay::Delay, - peripherals::Peripherals, prelude::*, rng::Rng, - system::SystemControl, timer::{timg::TimerGroup, ErasedTimer, PeriodicTimer}, }; use esp_wifi::{initialize, wifi, EspWifiInitFor}; @@ -38,10 +35,11 @@ const MAC_ADDRESS: [u8; 6] = [0x00, 0x80, 0x41, 0x13, 0x37, 0x42]; fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let delay = Delay::new(&clocks); diff --git a/examples/src/bin/wifi_access_point.rs b/examples/src/bin/wifi_access_point.rs index d9807afa65f..b394e91ca19 100644 --- a/examples/src/bin/wifi_access_point.rs +++ b/examples/src/bin/wifi_access_point.rs @@ -15,14 +15,7 @@ use embedded_io::*; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - prelude::*, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_println::{print, println}; use esp_wifi::{ current_millis, @@ -42,10 +35,11 @@ use smoltcp::iface::SocketStorage; fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); diff --git a/examples/src/bin/wifi_access_point_with_sta.rs b/examples/src/bin/wifi_access_point_with_sta.rs index 9ca5aaccb41..8e09695fb5d 100644 --- a/examples/src/bin/wifi_access_point_with_sta.rs +++ b/examples/src/bin/wifi_access_point_with_sta.rs @@ -16,14 +16,7 @@ use embedded_io::*; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - prelude::*, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_println::{print, println}; use esp_wifi::{ current_millis, @@ -49,10 +42,11 @@ const PASSWORD: &str = env!("PASSWORD"); fn main() -> ! { esp_println::logger::init_logger(log::LevelFilter::Info); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); diff --git a/examples/src/bin/wifi_bench.rs b/examples/src/bin/wifi_bench.rs index 5427e3b11e6..2f688e4ab06 100644 --- a/examples/src/bin/wifi_bench.rs +++ b/examples/src/bin/wifi_bench.rs @@ -15,15 +15,7 @@ use embedded_io::*; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - delay::Delay, - peripherals::Peripherals, - prelude::*, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{delay::Delay, prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_println::println; use esp_wifi::{ current_millis, @@ -60,10 +52,11 @@ const UPLOAD_DOWNLOAD_PORT: u16 = 4323; fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let server_address: Ipv4Address = HOST_IP.parse().expect("Invalid HOST_IP address"); diff --git a/examples/src/bin/wifi_ble.rs b/examples/src/bin/wifi_ble.rs index 34bf83e936f..8ac846a5b11 100644 --- a/examples/src/bin/wifi_ble.rs +++ b/examples/src/bin/wifi_ble.rs @@ -24,12 +24,10 @@ use bleps::{ }; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::{Input, Io, Pull}, peripherals::*, prelude::*, rng::Rng, - system::SystemControl, timer::timg::TimerGroup, }; use esp_println::println; @@ -39,10 +37,11 @@ use esp_wifi::{ble::controller::BleConnector, initialize, EspWifiInitFor}; fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -56,15 +55,14 @@ fn main() -> ! { .unwrap(); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] - let button = Input::new(io.pins.gpio0, Pull::Down); - #[cfg(any( - feature = "esp32c2", - feature = "esp32c3", - feature = "esp32c6", - feature = "esp32h2" - ))] - let button = Input::new(io.pins.gpio9, Pull::Down); + + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] { + let button = Input::new(io.pins.gpio0, Pull::Down); + } else { + let button = Input::new(io.pins.gpio9, Pull::Down); + } + } let mut debounce_cnt = 500; diff --git a/examples/src/bin/wifi_coex.rs b/examples/src/bin/wifi_coex.rs index 4f94af99076..084cca7059b 100644 --- a/examples/src/bin/wifi_coex.rs +++ b/examples/src/bin/wifi_coex.rs @@ -26,14 +26,7 @@ use bleps::{ }; use embedded_io::*; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - prelude::*, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_println::{print, println}; use esp_wifi::{ ble::controller::BleConnector, @@ -55,10 +48,11 @@ const PASSWORD: &str = env!("PASSWORD"); fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); diff --git a/examples/src/bin/wifi_dhcp.rs b/examples/src/bin/wifi_dhcp.rs index 0ae1adb2191..8814172b782 100644 --- a/examples/src/bin/wifi_dhcp.rs +++ b/examples/src/bin/wifi_dhcp.rs @@ -13,14 +13,7 @@ use embedded_io::*; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - prelude::*, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_println::{print, println}; use esp_wifi::{ current_millis, @@ -48,10 +41,11 @@ const PASSWORD: &str = env!("PASSWORD"); fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); diff --git a/examples/src/bin/wifi_embassy_access_point.rs b/examples/src/bin/wifi_embassy_access_point.rs index 6b99c22a985..ad33af13cba 100644 --- a/examples/src/bin/wifi_embassy_access_point.rs +++ b/examples/src/bin/wifi_embassy_access_point.rs @@ -27,13 +27,7 @@ use embassy_net::{ }; use embassy_time::{Duration, Timer}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{rng::Rng, timer::timg::TimerGroup}; use esp_println::{print, println}; use esp_wifi::{ initialize, @@ -63,10 +57,11 @@ macro_rules! mk_static { async fn main(spawner: Spawner) -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -83,17 +78,15 @@ async fn main(spawner: Spawner) -> ! { let (wifi_interface, controller) = esp_wifi::wifi::new_with_mode(&init, wifi, WifiApDevice).unwrap(); - #[cfg(feature = "esp32")] - { - let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); - esp_hal_embassy::init(&clocks, timg1.timer0); - } - - #[cfg(not(feature = "esp32"))] - { - let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) - .split::(); - esp_hal_embassy::init(&clocks, systimer.alarm0); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); + esp_hal_embassy::init(&clocks, timg1.timer0); + } else { + use esp_hal::timer::systimer::{SystemTimer, Target}; + let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); + esp_hal_embassy::init(&clocks, systimer.alarm0); + } } let config = Config::ipv4_static(StaticConfigV4 { diff --git a/examples/src/bin/wifi_embassy_access_point_with_sta.rs b/examples/src/bin/wifi_embassy_access_point_with_sta.rs index 6bc52865a8c..220707fbb85 100644 --- a/examples/src/bin/wifi_embassy_access_point_with_sta.rs +++ b/examples/src/bin/wifi_embassy_access_point_with_sta.rs @@ -71,10 +71,11 @@ macro_rules! mk_static { async fn main(spawner: Spawner) -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -91,17 +92,15 @@ async fn main(spawner: Spawner) -> ! { let (wifi_ap_interface, wifi_sta_interface, mut controller) = esp_wifi::wifi::new_ap_sta(&init, wifi).unwrap(); - #[cfg(feature = "esp32")] - { - let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); - esp_hal_embassy::init(&clocks, timg1.timer0); - } - - #[cfg(not(feature = "esp32"))] - { - let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) - .split::(); - esp_hal_embassy::init(&clocks, systimer.alarm0); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); + esp_hal_embassy::init(&clocks, timg1.timer0); + } else { + use esp_hal::timer::systimer::{SystemTimer, Target}; + let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); + esp_hal_embassy::init(&clocks, systimer.alarm0); + } } let ap_config = Config::ipv4_static(StaticConfigV4 { diff --git a/examples/src/bin/wifi_embassy_bench.rs b/examples/src/bin/wifi_embassy_bench.rs index 81fce5ae720..5031f7cb09a 100644 --- a/examples/src/bin/wifi_embassy_bench.rs +++ b/examples/src/bin/wifi_embassy_bench.rs @@ -21,13 +21,7 @@ use embassy_futures::join::join; use embassy_net::{tcp::TcpSocket, Config, Ipv4Address, Stack, StackResources}; use embassy_time::{with_timeout, Duration, Timer}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{rng::Rng, timer::timg::TimerGroup}; use esp_println::println; use esp_wifi::{ initialize, @@ -73,10 +67,11 @@ static mut TX_BUFFER: [u8; TX_BUFFER_SIZE] = [0; TX_BUFFER_SIZE]; async fn main(spawner: Spawner) -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let server_address: Ipv4Address = HOST_IP.parse().expect("Invalid HOST_IP address"); @@ -95,17 +90,15 @@ async fn main(spawner: Spawner) -> ! { let (wifi_interface, controller) = esp_wifi::wifi::new_with_mode(&init, wifi, WifiStaDevice).unwrap(); - #[cfg(feature = "esp32")] - { - let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); - esp_hal_embassy::init(&clocks, timg1.timer0); - } - - #[cfg(not(feature = "esp32"))] - { - let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) - .split::(); - esp_hal_embassy::init(&clocks, systimer.alarm0); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); + esp_hal_embassy::init(&clocks, timg1.timer0); + } else { + use esp_hal::timer::systimer::{SystemTimer, Target}; + let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); + esp_hal_embassy::init(&clocks, systimer.alarm0); + } } let config = Config::dhcpv4(Default::default()); diff --git a/examples/src/bin/wifi_embassy_ble.rs b/examples/src/bin/wifi_embassy_ble.rs index 89fb122ac67..054926eae4e 100644 --- a/examples/src/bin/wifi_embassy_ble.rs +++ b/examples/src/bin/wifi_embassy_ble.rs @@ -27,11 +27,9 @@ use bleps::{ use embassy_executor::Spawner; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, gpio::{Input, Io, Pull}, peripherals::*, rng::Rng, - system::SystemControl, timer::timg::TimerGroup, }; use esp_println::println; @@ -41,10 +39,11 @@ use esp_wifi::{ble::controller::asynch::BleConnector, initialize, EspWifiInitFor async fn main(_spawner: Spawner) -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -58,27 +57,23 @@ async fn main(_spawner: Spawner) -> ! { .unwrap(); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] - let button = Input::new(io.pins.gpio0, Pull::Down); - #[cfg(any( - feature = "esp32c2", - feature = "esp32c3", - feature = "esp32c6", - feature = "esp32h2" - ))] - let button = Input::new(io.pins.gpio9, Pull::Down); - - #[cfg(feature = "esp32")] - { - let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); - esp_hal_embassy::init(&clocks, timg1.timer0); + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] { + let button = Input::new(io.pins.gpio0, Pull::Down); + } else { + let button = Input::new(io.pins.gpio9, Pull::Down); + } } - #[cfg(not(feature = "esp32"))] - { - let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) - .split::(); - esp_hal_embassy::init(&clocks, systimer.alarm0); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); + esp_hal_embassy::init(&clocks, timg1.timer0); + } else { + use esp_hal::timer::systimer::{SystemTimer, Target}; + let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); + esp_hal_embassy::init(&clocks, systimer.alarm0); + } } let mut bluetooth = peripherals.BT; diff --git a/examples/src/bin/wifi_embassy_dhcp.rs b/examples/src/bin/wifi_embassy_dhcp.rs index 961ef175cdb..15146591e65 100644 --- a/examples/src/bin/wifi_embassy_dhcp.rs +++ b/examples/src/bin/wifi_embassy_dhcp.rs @@ -17,13 +17,7 @@ use embassy_executor::Spawner; use embassy_net::{tcp::TcpSocket, Config, Ipv4Address, Stack, StackResources}; use embassy_time::{Duration, Timer}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{rng::Rng, timer::timg::TimerGroup}; use esp_println::println; use esp_wifi::{ initialize, @@ -56,10 +50,11 @@ const PASSWORD: &str = env!("PASSWORD"); async fn main(spawner: Spawner) -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -76,17 +71,15 @@ async fn main(spawner: Spawner) -> ! { let (wifi_interface, controller) = esp_wifi::wifi::new_with_mode(&init, wifi, WifiStaDevice).unwrap(); - #[cfg(feature = "esp32")] - { - let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); - esp_hal_embassy::init(&clocks, timg1.timer0); - } - - #[cfg(not(feature = "esp32"))] - { - let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) - .split::(); - esp_hal_embassy::init(&clocks, systimer.alarm0); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); + esp_hal_embassy::init(&clocks, timg1.timer0); + } else { + use esp_hal::timer::systimer::{SystemTimer, Target}; + let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); + esp_hal_embassy::init(&clocks, systimer.alarm0); + } } let config = Config::dhcpv4(Default::default()); diff --git a/examples/src/bin/wifi_embassy_esp_now.rs b/examples/src/bin/wifi_embassy_esp_now.rs index a00179d0f15..f93916981a6 100644 --- a/examples/src/bin/wifi_embassy_esp_now.rs +++ b/examples/src/bin/wifi_embassy_esp_now.rs @@ -14,13 +14,7 @@ use embassy_executor::Spawner; use embassy_futures::select::{select, Either}; use embassy_time::{Duration, Ticker}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{rng::Rng, timer::timg::TimerGroup}; use esp_println::println; use esp_wifi::{ esp_now::{PeerInfo, BROADCAST_ADDRESS}, @@ -32,10 +26,11 @@ use esp_wifi::{ async fn main(_spawner: Spawner) -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -52,17 +47,15 @@ async fn main(_spawner: Spawner) -> ! { let mut esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); println!("esp-now version {}", esp_now.get_version().unwrap()); - #[cfg(feature = "esp32")] - { - let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); - esp_hal_embassy::init(&clocks, timg1.timer0); - } - - #[cfg(not(feature = "esp32"))] - { - let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) - .split::(); - esp_hal_embassy::init(&clocks, systimer.alarm0); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); + esp_hal_embassy::init(&clocks, timg1.timer0); + } else { + use esp_hal::timer::systimer::{SystemTimer, Target}; + let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); + esp_hal_embassy::init(&clocks, systimer.alarm0); + } } let mut ticker = Ticker::every(Duration::from_secs(5)); diff --git a/examples/src/bin/wifi_embassy_esp_now_duplex.rs b/examples/src/bin/wifi_embassy_esp_now_duplex.rs index e3d02644f24..59c43eaba90 100644 --- a/examples/src/bin/wifi_embassy_esp_now_duplex.rs +++ b/examples/src/bin/wifi_embassy_esp_now_duplex.rs @@ -14,13 +14,7 @@ use embassy_executor::Spawner; use embassy_sync::{blocking_mutex::raw::NoopRawMutex, mutex::Mutex}; use embassy_time::{Duration, Ticker}; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{rng::Rng, timer::timg::TimerGroup}; use esp_println::println; use esp_wifi::{ esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}, @@ -42,10 +36,11 @@ macro_rules! mk_static { async fn main(spawner: Spawner) -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); @@ -62,17 +57,15 @@ async fn main(spawner: Spawner) -> ! { let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); println!("esp-now version {}", esp_now.get_version().unwrap()); - #[cfg(feature = "esp32")] - { - let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); - esp_hal_embassy::init(&clocks, timg1.timer0); - } - - #[cfg(not(feature = "esp32"))] - { - let systimer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER) - .split::(); - esp_hal_embassy::init(&clocks, systimer.alarm0); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let timg1 = TimerGroup::new(peripherals.TIMG1, &clocks); + esp_hal_embassy::init(&clocks, timg1.timer0); + } else { + use esp_hal::timer::systimer::{SystemTimer, Target}; + let systimer = SystemTimer::new(peripherals.SYSTIMER).split::(); + esp_hal_embassy::init(&clocks, systimer.alarm0); + } } let (manager, sender, receiver) = esp_now.split(); diff --git a/examples/src/bin/wifi_esp_now.rs b/examples/src/bin/wifi_esp_now.rs index 9dd365c25a1..1e539415d06 100644 --- a/examples/src/bin/wifi_esp_now.rs +++ b/examples/src/bin/wifi_esp_now.rs @@ -9,14 +9,7 @@ #![no_main] use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - prelude::*, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_println::println; use esp_wifi::{ current_millis, @@ -29,10 +22,11 @@ use esp_wifi::{ fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); diff --git a/examples/src/bin/wifi_sniffer.rs b/examples/src/bin/wifi_sniffer.rs index 90626327507..b8b27b589cb 100644 --- a/examples/src/bin/wifi_sniffer.rs +++ b/examples/src/bin/wifi_sniffer.rs @@ -20,11 +20,8 @@ use critical_section::Mutex; use esp_alloc::heap_allocator; use esp_backtrace as _; use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, prelude::*, rng::Rng, - system::SystemControl, timer::{timg::TimerGroup, ErasedTimer, PeriodicTimer}, }; use esp_println::println; @@ -37,13 +34,15 @@ static KNOWN_SSIDS: Mutex>> = Mutex::new(RefCell::new(B fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); + // Create a heap allocator, with 32kB of space. heap_allocator!(32_168); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); - let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); let timer0: ErasedTimer = timg0.timer0.into(); let timer = PeriodicTimer::new(timer0); diff --git a/examples/src/bin/wifi_static_ip.rs b/examples/src/bin/wifi_static_ip.rs index f4d86620cc7..4075c8749c0 100644 --- a/examples/src/bin/wifi_static_ip.rs +++ b/examples/src/bin/wifi_static_ip.rs @@ -14,14 +14,7 @@ use embedded_io::*; use esp_backtrace as _; -use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - prelude::*, - rng::Rng, - system::SystemControl, - timer::timg::TimerGroup, -}; +use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; use esp_println::{print, println}; use esp_wifi::{ current_millis, @@ -48,10 +41,11 @@ const GATEWAY_IP: &str = env!("GATEWAY_IP"); fn main() -> ! { esp_println::logger::init_logger_from_env(); - let peripherals = Peripherals::take(); - - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::max(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::max()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); diff --git a/hil-test/tests/aes.rs b/hil-test/tests/aes.rs index 812a42ea8b9..9a8dcf1cb66 100644 --- a/hil-test/tests/aes.rs +++ b/hil-test/tests/aes.rs @@ -7,7 +7,7 @@ use esp_hal::{ aes::{Aes, Mode}, - peripherals::Peripherals, + prelude::*, }; use hil_test as _; @@ -17,7 +17,7 @@ struct Context<'a> { impl Context<'_> { pub fn init() -> Self { - let peripherals = Peripherals::take(); + let System { peripherals, .. } = esp_hal::init(CpuClock::max()); let aes = Aes::new(peripherals.AES); Context { aes } diff --git a/hil-test/tests/aes_dma.rs b/hil-test/tests/aes_dma.rs index c253af98603..a63259a06da 100644 --- a/hil-test/tests/aes_dma.rs +++ b/hil-test/tests/aes_dma.rs @@ -27,12 +27,13 @@ mod tests { use super::*; #[init] - fn init() {} + fn init() -> Peripherals { + let system = esp_hal::init(CpuClock::boot_default()); + system.peripherals + } #[test] - fn test_aes_128_dma_encryption() { - let peripherals = Peripherals::take(); - + fn test_aes_128_dma_encryption(peripherals: Peripherals) { let dma = Dma::new(peripherals.DMA); let dma_channel = dma.channel0; @@ -74,9 +75,7 @@ mod tests { } #[test] - fn test_aes_128_dma_decryption() { - let peripherals = Peripherals::take(); - + fn test_aes_128_dma_decryption(peripherals: Peripherals) { let dma = Dma::new(peripherals.DMA); let dma_channel = dma.channel0; @@ -117,9 +116,7 @@ mod tests { } #[test] - fn test_aes_256_dma_encryption() { - let peripherals = Peripherals::take(); - + fn test_aes_256_dma_encryption(peripherals: Peripherals) { let dma = Dma::new(peripherals.DMA); let dma_channel = dma.channel0; @@ -161,9 +158,7 @@ mod tests { } #[test] - fn test_aes_256_dma_decryption() { - let peripherals = Peripherals::take(); - + fn test_aes_256_dma_decryption(peripherals: Peripherals) { let dma = Dma::new(peripherals.DMA); let dma_channel = dma.channel0; diff --git a/hil-test/tests/ecc.rs b/hil-test/tests/ecc.rs index 2ad0fc923d8..5e4cb64f6e7 100644 --- a/hil-test/tests/ecc.rs +++ b/hil-test/tests/ecc.rs @@ -18,7 +18,7 @@ use elliptic_curve::sec1::ToEncodedPoint; use esp_hal::ecc::WorkMode; use esp_hal::{ ecc::{Ecc, EllipticCurve, Error}, - peripherals::Peripherals, + prelude::*, rng::Rng, Blocking, }; @@ -47,16 +47,6 @@ struct Context<'a> { rng: Rng, } -impl Context<'_> { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let ecc = Ecc::new(peripherals.ECC); - let rng = Rng::new(peripherals.RNG); - - Context { ecc, rng } - } -} - #[cfg(test)] #[embedded_test::tests] mod tests { @@ -66,7 +56,12 @@ mod tests { #[init] fn init() -> Context<'static> { - Context::init() + let System { peripherals, .. } = esp_hal::init(CpuClock::max()); + + let ecc = Ecc::new(peripherals.ECC); + let rng = Rng::new(peripherals.RNG); + + Context { ecc, rng } } #[test] diff --git a/hil-test/tests/embassy_interrupt_executor.rs b/hil-test/tests/embassy_interrupt_executor.rs index d1b078677fb..b1a65bae3b9 100644 --- a/hil-test/tests/embassy_interrupt_executor.rs +++ b/hil-test/tests/embassy_interrupt_executor.rs @@ -28,11 +28,7 @@ async fn interrupt_driven_task(signal: &'static Signal Self { - let peripherals = Peripherals::take(); + let System { + peripherals, + software_interrupt_control: sw_int, + .. + } = esp_hal::init(CpuClock::max()); cfg_if::cfg_if! { if #[cfg(any(feature = "esp32c6", feature = "esp32h2"))] { @@ -39,11 +43,6 @@ impl Context { let sw0_trigger_addr = cpu_intr.cpu_intr_from_cpu_0() as *const _ as u32; - let system = SystemControl::new(peripherals.SYSTEM); - let _clocks = ClockControl::max(system.clock_control).freeze(); - - let sw_int = system.software_interrupt_control; - critical_section::with(|cs| { SWINT0 .borrow_ref_mut(cs) diff --git a/hil-test/tests/lcd_cam_i8080.rs b/hil-test/tests/lcd_cam_i8080.rs index 1189bbaab69..6ab5850e02f 100644 --- a/hil-test/tests/lcd_cam_i8080.rs +++ b/hil-test/tests/lcd_cam_i8080.rs @@ -6,20 +6,15 @@ #![no_main] use esp_hal::{ - clock::{ClockControl, Clocks}, + clock::Clocks, dma::{Dma, DmaDescriptor, DmaPriority}, dma_buffers, gpio::dummy_pin::DummyPin, lcd_cam::{ - lcd::{ - i8080, - i8080::{Command, TxEightBits, I8080}, - }, + lcd::i8080::{self, Command, TxEightBits, I8080}, LcdCam, }, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use hil_test as _; @@ -33,16 +28,23 @@ struct Context<'d> { tx_descriptors: &'static mut [DmaDescriptor], } -impl<'d> Context<'d> { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); +#[cfg(test)] +#[embedded_test::tests] +mod tests { + use super::*; + + #[init] + fn init() -> Context<'static> { + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let dma = Dma::new(peripherals.DMA); let lcd_cam = LcdCam::new(peripherals.LCD_CAM); let (tx_buffer, tx_descriptors, _, _) = dma_buffers!(DATA_SIZE, 0); - Self { + Context { lcd_cam, clocks, dma, @@ -50,17 +52,6 @@ impl<'d> Context<'d> { tx_descriptors, } } -} - -#[cfg(test)] -#[embedded_test::tests] -mod tests { - use super::*; - - #[init] - fn init() -> Context<'static> { - Context::init() - } #[test] fn test_i8080_8bit(ctx: Context<'static>) { diff --git a/hil-test/tests/lcd_cam_i8080_async.rs b/hil-test/tests/lcd_cam_i8080_async.rs index cc57f291ab7..ddd2987a994 100644 --- a/hil-test/tests/lcd_cam_i8080_async.rs +++ b/hil-test/tests/lcd_cam_i8080_async.rs @@ -6,7 +6,7 @@ #![no_main] use esp_hal::{ - clock::{ClockControl, Clocks}, + clock::Clocks, dma::{Dma, DmaDescriptor, DmaPriority}, dma_buffers, gpio::dummy_pin::DummyPin, @@ -17,9 +17,7 @@ use esp_hal::{ }, LcdCam, }, - peripherals::Peripherals, prelude::*, - system::SystemControl, }; use hil_test as _; @@ -33,11 +31,19 @@ struct Context<'d> { tx_descriptors: &'static mut [DmaDescriptor], } -impl<'d> Context<'d> { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); +#[cfg(test)] +#[embedded_test::tests(executor = esp_hal_embassy::Executor::new())] +mod tests { + use super::*; + + #[init] + async fn init() -> Context<'static> { + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); + let dma = Dma::new(peripherals.DMA); let lcd_cam = LcdCam::new_async(peripherals.LCD_CAM); let (tx_buffer, tx_descriptors, _, _) = dma_buffers!(DATA_SIZE, 0); @@ -50,17 +56,6 @@ impl<'d> Context<'d> { tx_descriptors, } } -} - -#[cfg(test)] -#[embedded_test::tests(executor = esp_hal_embassy::Executor::new())] -mod tests { - use super::*; - - #[init] - async fn init() -> Context<'static> { - Context::init() - } #[test] async fn test_i8080_8bit(ctx: Context<'static>) { diff --git a/hil-test/tests/pcnt.rs b/hil-test/tests/pcnt.rs index ec25dff4479..0ae11fb2d62 100644 --- a/hil-test/tests/pcnt.rs +++ b/hil-test/tests/pcnt.rs @@ -7,7 +7,15 @@ #![no_std] #![no_main] -use esp_hal::{delay::Delay, gpio::GpioPin, pcnt::Pcnt}; +use esp_hal::{ + delay::Delay, + gpio::{GpioPin, Io, Level, Output, Pull}, + pcnt::{ + channel::{EdgeMode, PcntInputConfig, PcntSource}, + Pcnt, + }, + prelude::*, +}; use hil_test as _; struct Context<'d> { @@ -20,22 +28,15 @@ struct Context<'d> { #[cfg(test)] #[embedded_test::tests] mod tests { - use esp_hal::{ - clock::ClockControl, - delay::Delay, - gpio::{Io, Level, Output, Pull}, - pcnt::channel::{EdgeMode, PcntInputConfig, PcntSource}, - peripherals::Peripherals, - system::SystemControl, - }; - use super::*; #[init] fn init() -> Context<'static> { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/hil-test/tests/rmt.rs b/hil-test/tests/rmt.rs index 30359b3707e..5595651a15b 100644 --- a/hil-test/tests/rmt.rs +++ b/hil-test/tests/rmt.rs @@ -8,12 +8,9 @@ #![no_main] use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::Peripherals, prelude::*, rmt::{PulseCode, Rmt, RxChannel, RxChannelConfig, TxChannel, TxChannelConfig}, - system::SystemControl, }; use hil_test as _; @@ -28,9 +25,11 @@ mod tests { #[test] #[timeout(1)] fn rmt_loopback() { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); diff --git a/hil-test/tests/rsa.rs b/hil-test/tests/rsa.rs index e2a83dbb03b..0c635c91417 100644 --- a/hil-test/tests/rsa.rs +++ b/hil-test/tests/rsa.rs @@ -7,7 +7,6 @@ use crypto_bigint::{Uint, U1024, U512}; use esp_hal::{ - peripherals::Peripherals, prelude::*, rsa::{ operand_sizes, @@ -37,16 +36,6 @@ struct Context<'a> { rsa: Rsa<'a, Blocking>, } -impl Context<'_> { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let mut rsa = Rsa::new(peripherals.RSA); - nb::block!(rsa.ready()).unwrap(); - - Context { rsa } - } -} - const fn compute_r(modulus: &U512) -> U512 { let mut d = [0_u32; U512::LIMBS * 2 + 1]; d[d.len() - 1] = 1; @@ -68,7 +57,11 @@ mod tests { #[init] fn init() -> Context<'static> { - Context::init() + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); + let mut rsa = Rsa::new(peripherals.RSA); + nb::block!(rsa.ready()).unwrap(); + + Context { rsa } } #[test] @@ -145,21 +138,20 @@ mod tests { let operand_a = BIGNUM_1.as_words(); let operand_b = BIGNUM_2.as_words(); - #[cfg(feature = "esp32")] - { - let mut rsamulti = - RsaMultiplication::::new(&mut ctx.rsa); - rsamulti.start_multiplication(operand_a, operand_b); - rsamulti.read_results(&mut outbuf); - } - #[cfg(not(feature = "esp32"))] - { - let mut rsamulti = RsaMultiplication::::new( - &mut ctx.rsa, - operand_a, - ); - rsamulti.start_multiplication(operand_b); - rsamulti.read_results(&mut outbuf); + cfg_if::cfg_if! { + if #[cfg(feature = "esp32")] { + let mut rsamulti = + RsaMultiplication::::new(&mut ctx.rsa); + rsamulti.start_multiplication(operand_a, operand_b); + rsamulti.read_results(&mut outbuf); + } else { + let mut rsamulti = RsaMultiplication::::new( + &mut ctx.rsa, + operand_a, + ); + rsamulti.start_multiplication(operand_b); + rsamulti.read_results(&mut outbuf); + } } assert_eq!(EXPECTED_OUTPUT, outbuf) } diff --git a/hil-test/tests/sha.rs b/hil-test/tests/sha.rs index a1a63a3fd2a..dbce9a3f9c0 100644 --- a/hil-test/tests/sha.rs +++ b/hil-test/tests/sha.rs @@ -7,12 +7,10 @@ use digest::Digest; use esp_hal::{ - clock::ClockControl, peripherals::Peripherals, prelude::*, rng::Rng, sha::{Sha, Sha1, Sha256}, - system::SystemControl, }; use hil_test as _; use nb::block; @@ -47,7 +45,10 @@ mod tests { use super::*; #[init] - fn init() {} + fn init() -> Peripherals { + let System { peripherals, .. } = esp_hal::init(CpuClock::boot_default()); + peripherals + } #[test] fn test_sha_1() { @@ -342,11 +343,7 @@ mod tests { /// A rolling test that loops between hasher for every step to test /// interleaving. This specifically test the Sha trait implementation #[test] - fn test_sha_rolling() { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let _clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - + fn test_sha_rolling(peripherals: Peripherals) { let mut rng = Rng::new(peripherals.RNG); // Fill source data with random data @@ -486,11 +483,7 @@ mod tests { /// A rolling test that loops between hasher for every step to test /// interleaving. This specifically test the Digest trait implementation #[test] - fn test_for_digest_rolling() { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let _clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - + fn test_for_digest_rolling(peripherals: Peripherals) { let mut rng = Rng::new(peripherals.RNG); // Fill source data with random data @@ -611,7 +604,7 @@ mod tests { /// algorithm against a specific size. This will compare the result with a /// software implementation and return false if there's a mismatch fn test_for_size, const N: usize>(size: usize) { - let source_data = unsafe { core::slice::from_raw_parts(CHAR_ARRAY.as_ptr(), size) }; + let source_data = &CHAR_ARRAY[..size]; let mut remaining = source_data; let mut hasher = D::default(); diff --git a/hil-test/tests/spi_full_duplex.rs b/hil-test/tests/spi_full_duplex.rs index fba38765583..89515a63de9 100644 --- a/hil-test/tests/spi_full_duplex.rs +++ b/hil-test/tests/spi_full_duplex.rs @@ -15,12 +15,9 @@ use embedded_hal::spi::SpiBus; use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::Peripherals, prelude::*, spi::{master::Spi, FullDuplexMode, SpiMode}, - system::SystemControl, }; use hil_test as _; @@ -28,11 +25,20 @@ struct Context { spi: Spi<'static, esp_hal::peripherals::SPI2, FullDuplexMode>, } -impl Context { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); +#[cfg(test)] +#[embedded_test::tests] +mod tests { + use defmt::assert_eq; + + use super::*; + + #[init] + fn init() -> Context { + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; @@ -49,19 +55,6 @@ impl Context { Context { spi } } -} - -#[cfg(test)] -#[embedded_test::tests] -mod tests { - use defmt::assert_eq; - - use super::*; - - #[init] - fn init() -> Context { - Context::init() - } #[test] #[timeout(3)] diff --git a/hil-test/tests/spi_full_duplex_dma.rs b/hil-test/tests/spi_full_duplex_dma.rs index 938d2cec22a..f73a20f54d4 100644 --- a/hil-test/tests/spi_full_duplex_dma.rs +++ b/hil-test/tests/spi_full_duplex_dma.rs @@ -22,14 +22,11 @@ #![no_main] use esp_hal::{ - clock::ClockControl, - dma::{Dma, DmaPriority}, + dma::{Dma, DmaPriority, DmaRxBuf, DmaTxBuf}, dma_buffers, gpio::Io, - peripherals::Peripherals, prelude::*, spi::{master::Spi, SpiMode}, - system::SystemControl, }; use hil_test as _; @@ -37,22 +34,25 @@ use hil_test as _; #[embedded_test::tests] mod tests { use defmt::assert_eq; - use esp_hal::{ - dma::{DmaRxBuf, DmaTxBuf}, - spi::master::dma::SpiDmaBus, - }; use super::*; + #[init] + fn init() -> System { + esp_hal::init(CpuClock::boot_default()) + } + #[test] #[timeout(3)] - fn test_symmetric_dma_transfer() { + fn test_symmetric_dma_transfer( + System { + peripherals, + clocks, + .. + }: System, + ) { const DMA_BUFFER_SIZE: usize = 4; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; let miso = io.pins.gpio2; @@ -88,11 +88,13 @@ mod tests { #[timeout(3)] // S3 is disabled due to https://github.com/esp-rs/esp-hal/issues/1524#issuecomment-2255306292 #[cfg(not(feature = "esp32s3"))] - fn test_asymmetric_dma_transfer() { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - + fn test_asymmetric_dma_transfer( + System { + peripherals, + clocks, + .. + }: System, + ) { let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; let miso = io.pins.gpio2; @@ -101,10 +103,13 @@ mod tests { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(4, 2); let mut dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); @@ -126,13 +131,15 @@ mod tests { #[test] #[timeout(3)] - fn test_symmetric_dma_transfer_huge_buffer() { + fn test_symmetric_dma_transfer_huge_buffer( + System { + peripherals, + clocks, + .. + }: System, + ) { const DMA_BUFFER_SIZE: usize = 4096; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; let miso = io.pins.gpio2; @@ -141,10 +148,13 @@ mod tests { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(DMA_BUFFER_SIZE); let mut dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); @@ -174,7 +184,13 @@ mod tests { feature = "esp32h2", feature = "esp32s3" ))] - fn test_dma_read_dma_write_pcnt() { + fn test_dma_read_dma_write_pcnt( + System { + peripherals, + clocks, + .. + }: System, + ) { use esp_hal::{ gpio::{Level, Output, Pull}, pcnt::{ @@ -185,10 +201,6 @@ mod tests { const DMA_BUFFER_SIZE: usize = 5; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let pcnt = Pcnt::new(peripherals.PCNT); let sclk = io.pins.gpio0; @@ -203,10 +215,13 @@ mod tests { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(DMA_BUFFER_SIZE); let mut dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); @@ -249,7 +264,13 @@ mod tests { feature = "esp32h2", feature = "esp32s3" ))] - fn test_dma_read_dma_transfer_pcnt() { + fn test_dma_read_dma_transfer_pcnt( + System { + peripherals, + clocks, + .. + }: System, + ) { use esp_hal::{ gpio::{Level, Output, Pull}, pcnt::{ @@ -260,10 +281,6 @@ mod tests { const DMA_BUFFER_SIZE: usize = 5; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let pcnt = Pcnt::new(peripherals.PCNT); let sclk = io.pins.gpio0; @@ -278,10 +295,13 @@ mod tests { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(DMA_BUFFER_SIZE); let mut dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); @@ -321,13 +341,15 @@ mod tests { #[test] #[timeout(3)] - fn test_dma_bus_symmetric_transfer() { + fn test_dma_bus_symmetric_transfer( + System { + peripherals, + clocks, + .. + }: System, + ) { const DMA_BUFFER_SIZE: usize = 4; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; let miso = io.pins.gpio2; @@ -336,10 +358,13 @@ mod tests { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(DMA_BUFFER_SIZE); let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); @@ -360,11 +385,13 @@ mod tests { #[test] #[timeout(3)] - fn test_dma_bus_asymmetric_transfer() { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - + fn test_dma_bus_asymmetric_transfer( + System { + peripherals, + clocks, + .. + }: System, + ) { let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; let miso = io.pins.gpio2; @@ -373,10 +400,13 @@ mod tests { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(4); let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); @@ -397,13 +427,15 @@ mod tests { #[test] #[timeout(3)] - fn test_dma_bus_symmetric_transfer_huge_buffer() { + fn test_dma_bus_symmetric_transfer_huge_buffer( + System { + peripherals, + clocks, + .. + }: System, + ) { const DMA_BUFFER_SIZE: usize = 4096; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; let miso = io.pins.gpio2; @@ -412,10 +444,13 @@ mod tests { let dma = Dma::new(peripherals.DMA); - #[cfg(any(feature = "esp32", feature = "esp32s2"))] - let dma_channel = dma.spi2channel; - #[cfg(not(any(feature = "esp32", feature = "esp32s2")))] - let dma_channel = dma.channel0; + cfg_if::cfg_if! { + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { + let dma_channel = dma.spi2channel; + } else { + let dma_channel = dma.channel0; + } + } let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(40); let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); diff --git a/hil-test/tests/spi_full_duplex_dma_async.rs b/hil-test/tests/spi_full_duplex_dma_async.rs index cc0c4db5935..8bf3ab346be 100644 --- a/hil-test/tests/spi_full_duplex_dma_async.rs +++ b/hil-test/tests/spi_full_duplex_dma_async.rs @@ -1,6 +1,6 @@ //! SPI Full Duplex DMA Test //! -//! Folowing pins are used: +//! Following pins are used: //! SCLK GPIO0 //! MOSI GPIO3 //! MISO GPIO6 @@ -21,7 +21,6 @@ use embedded_hal_async::spi::SpiBus; use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority}, dma_buffers, gpio::{Io, Level, Output, Pull}, @@ -29,10 +28,8 @@ use esp_hal::{ channel::{EdgeMode, PcntInputConfig, PcntSource}, Pcnt, }, - peripherals::Peripherals, prelude::*, spi::{master::Spi, SpiMode}, - system::SystemControl, }; use hil_test as _; @@ -44,15 +41,22 @@ mod tests { use super::*; + #[init] + fn init() -> System { + esp_hal::init(CpuClock::boot_default()) + } + #[test] #[timeout(3)] - async fn test_async_dma_read_dma_write_pcnt() { + async fn test_async_dma_read_dma_write_pcnt( + System { + peripherals, + clocks, + .. + }: System, + ) { const DMA_BUFFER_SIZE: usize = 5; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let pcnt = Pcnt::new(peripherals.PCNT); let sclk = io.pins.gpio0; @@ -108,13 +112,15 @@ mod tests { #[test] #[timeout(3)] - async fn test_async_dma_read_dma_transfer_pcnt() { + async fn test_async_dma_read_dma_transfer_pcnt( + System { + peripherals, + clocks, + .. + }: System, + ) { const DMA_BUFFER_SIZE: usize = 5; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let pcnt = Pcnt::new(peripherals.PCNT); let sclk = io.pins.gpio0; diff --git a/hil-test/tests/spi_half_duplex_read.rs b/hil-test/tests/spi_half_duplex_read.rs index bcafe7cf922..22cd4204ca3 100644 --- a/hil-test/tests/spi_half_duplex_read.rs +++ b/hil-test/tests/spi_half_duplex_read.rs @@ -19,18 +19,15 @@ use hil_test as _; #[embedded_test::tests] mod tests { use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority, DmaRxBuf}, dma_buffers, gpio::{Io, Level, Output}, - peripherals::Peripherals, - prelude::_fugit_RateExtU32, + prelude::*, spi::{ master::{Address, Command, Spi}, SpiDataMode, SpiMode, }, - system::SystemControl, }; #[init] @@ -41,9 +38,11 @@ mod tests { fn test_spi_reads_correctly_from_gpio_pin() { const DMA_BUFFER_SIZE: usize = 4; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let sclk = io.pins.gpio0; diff --git a/hil-test/tests/spi_half_duplex_write.rs b/hil-test/tests/spi_half_duplex_write.rs index 06bb458be1d..aa93c5f4182 100644 --- a/hil-test/tests/spi_half_duplex_write.rs +++ b/hil-test/tests/spi_half_duplex_write.rs @@ -19,7 +19,6 @@ use hil_test as _; #[embedded_test::tests] mod tests { use esp_hal::{ - clock::ClockControl, dma::{Dma, DmaPriority, DmaTxBuf}, dma_buffers, gpio::{Io, Pull}, @@ -27,27 +26,24 @@ mod tests { channel::{EdgeMode, PcntInputConfig, PcntSource}, Pcnt, }, - peripherals::Peripherals, - prelude::_fugit_RateExtU32, + prelude::*, spi::{ master::{Address, Command, Spi}, SpiDataMode, SpiMode, }, - system::SystemControl, }; - #[init] - fn init() {} - #[test] #[timeout(3)] fn test_spi_writes_are_correctly_by_pcnt() { const DMA_BUFFER_SIZE: usize = 4; - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let pcnt = Pcnt::new(peripherals.PCNT); diff --git a/hil-test/tests/systimer.rs b/hil-test/tests/systimer.rs index e8360539d85..3b0116c48ca 100644 --- a/hil-test/tests/systimer.rs +++ b/hil-test/tests/systimer.rs @@ -11,11 +11,9 @@ use core::cell::RefCell; use critical_section::Mutex; use embedded_hal::delay::DelayNs; use esp_hal::{ - clock::{ClockControl, Clocks}, + clock::Clocks, delay::Delay, - peripherals::Peripherals, prelude::*, - system::SystemControl, timer::systimer::{ Alarm, FrozenUnit, @@ -46,27 +44,6 @@ struct Context { clocks: Clocks<'static>, } -impl Context { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - - let systimer = SystemTimer::new(peripherals.SYSTIMER); - static UNIT0: StaticCell> = StaticCell::new(); - - let unit0 = UNIT0.init(systimer.unit0); - let frozen_unit = FrozenUnit::new(unit0); - - Context { - clocks, - unit: frozen_unit, - comparator0: systimer.comparator0, - comparator1: systimer.comparator1, - } - } -} - #[handler(priority = esp_hal::interrupt::Priority::min())] fn pass_test_if_called() { critical_section::with(|cs| { @@ -127,7 +104,24 @@ mod tests { #[init] fn init() -> Context { - Context::init() + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); + + let systimer = SystemTimer::new(peripherals.SYSTIMER); + static UNIT0: StaticCell> = StaticCell::new(); + + let unit0 = UNIT0.init(systimer.unit0); + let frozen_unit = FrozenUnit::new(unit0); + + Context { + clocks, + unit: frozen_unit, + comparator0: systimer.comparator0, + comparator1: systimer.comparator1, + } } #[test] diff --git a/hil-test/tests/twai.rs b/hil-test/tests/twai.rs index c410c9fd590..efc6df74962 100644 --- a/hil-test/tests/twai.rs +++ b/hil-test/tests/twai.rs @@ -13,11 +13,9 @@ use embedded_hal_02::can::Frame; use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::{Peripherals, TWAI0}, + peripherals::TWAI0, prelude::*, - system::SystemControl, twai::{self, filter::SingleStandardFilter, EspTwaiFrame, StandardId, TwaiMode}, Blocking, }; @@ -28,25 +26,32 @@ struct Context { twai: twai::Twai<'static, TWAI0, Blocking>, } -impl Context { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); +#[cfg(test)] +#[embedded_test::tests] +mod tests { + use defmt::assert_eq; + + use super::*; + + #[init] + fn init() -> Context { + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let can_tx_pin = io.pins.gpio2; let can_rx_pin = io.pins.gpio3; - const CAN_BAUDRATE: twai::BaudRate = twai::BaudRate::B1000K; - let mut config = twai::TwaiConfiguration::new( peripherals.TWAI0, can_tx_pin, can_rx_pin, &clocks, - CAN_BAUDRATE, + twai::BaudRate::B1000K, TwaiMode::SelfTest, ); @@ -58,19 +63,6 @@ impl Context { Context { twai } } -} - -#[cfg(test)] -#[embedded_test::tests] -mod tests { - use defmt::assert_eq; - - use super::*; - - #[init] - fn init() -> Context { - Context::init() - } #[test] #[timeout(3)] diff --git a/hil-test/tests/uart.rs b/hil-test/tests/uart.rs index cb87373ffad..06fc2f122fa 100644 --- a/hil-test/tests/uart.rs +++ b/hil-test/tests/uart.rs @@ -13,11 +13,10 @@ use embedded_hal_02::serial::{Read, Write}; use esp_hal::{ - clock::{ClockControl, Clocks}, + clock::Clocks, gpio::Io, - peripherals::{Peripherals, UART1}, + peripherals::UART1, prelude::*, - system::SystemControl, uart::{ClockSource, Uart}, Blocking, }; @@ -29,20 +28,6 @@ struct Context { uart: Uart<'static, UART1, Blocking>, } -impl Context { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - - let uart = Uart::new(peripherals.UART1, &clocks, io.pins.gpio2, io.pins.gpio3).unwrap(); - - Context { clocks, uart } - } -} - #[cfg(test)] #[embedded_test::tests] mod tests { @@ -52,7 +37,17 @@ mod tests { #[init] fn init() -> Context { - Context::init() + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); + + let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); + + let uart = Uart::new(peripherals.UART1, &clocks, io.pins.gpio2, io.pins.gpio3).unwrap(); + + Context { clocks, uart } } #[test] diff --git a/hil-test/tests/uart_async.rs b/hil-test/tests/uart_async.rs index f7b8222953c..67923bdfb8b 100644 --- a/hil-test/tests/uart_async.rs +++ b/hil-test/tests/uart_async.rs @@ -12,12 +12,11 @@ #![no_main] use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::{Peripherals, UART0}, - system::SystemControl, + peripherals::UART0, uart::Uart, Async, + prelude::*, }; use hil_test as _; @@ -25,20 +24,6 @@ struct Context { uart: Uart<'static, UART0, Async>, } -impl Context { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - - let uart = - Uart::new_async(peripherals.UART0, &clocks, io.pins.gpio2, io.pins.gpio3).unwrap(); - - Context { uart } - } -} - #[cfg(test)] #[embedded_test::tests(executor = esp_hal_embassy::Executor::new())] mod tests { @@ -48,7 +33,18 @@ mod tests { #[init] async fn init() -> Context { - Context::init() + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); + + let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); + + let uart = + Uart::new_async(peripherals.UART0, &clocks, io.pins.gpio2, io.pins.gpio3).unwrap(); + + Context { uart } } #[test] diff --git a/hil-test/tests/uart_tx_rx.rs b/hil-test/tests/uart_tx_rx.rs index 67a56460663..a9b2f494f06 100644 --- a/hil-test/tests/uart_tx_rx.rs +++ b/hil-test/tests/uart_tx_rx.rs @@ -12,11 +12,9 @@ #![no_main] use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::{Peripherals, UART0, UART1}, + peripherals::{UART0, UART1}, prelude::*, - system::SystemControl, uart::{UartRx, UartTx}, Blocking, }; @@ -28,21 +26,6 @@ struct Context { rx: UartRx<'static, UART1, Blocking>, } -impl Context { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - - let tx = UartTx::new(peripherals.UART0, &clocks, io.pins.gpio2).unwrap(); - let rx = UartRx::new(peripherals.UART1, &clocks, io.pins.gpio3).unwrap(); - - Context { tx, rx } - } -} - #[cfg(test)] #[embedded_test::tests] mod tests { @@ -52,7 +35,18 @@ mod tests { #[init] fn init() -> Context { - Context::init() + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); + + let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); + + let tx = UartTx::new(peripherals.UART0, &clocks, io.pins.gpio2).unwrap(); + let rx = UartRx::new(peripherals.UART1, &clocks, io.pins.gpio3).unwrap(); + + Context { tx, rx } } #[test] diff --git a/hil-test/tests/uart_tx_rx_async.rs b/hil-test/tests/uart_tx_rx_async.rs index a74523d1b5f..96f306c0a4d 100644 --- a/hil-test/tests/uart_tx_rx_async.rs +++ b/hil-test/tests/uart_tx_rx_async.rs @@ -12,12 +12,11 @@ #![no_main] use esp_hal::{ - clock::ClockControl, gpio::Io, - peripherals::{Peripherals, UART0, UART1}, - system::SystemControl, + peripherals::{UART0, UART1}, uart::{UartRx, UartTx}, Async, + prelude::*, }; use hil_test as _; @@ -26,21 +25,6 @@ struct Context { rx: UartRx<'static, UART1, Async>, } -impl Context { - pub fn init() -> Self { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - - let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - - let tx = UartTx::new_async(peripherals.UART0, &clocks, io.pins.gpio2).unwrap(); - let rx = UartRx::new_async(peripherals.UART1, &clocks, io.pins.gpio3).unwrap(); - - Context { tx, rx } - } -} - #[cfg(test)] #[embedded_test::tests(executor = esp_hal_embassy::Executor::new())] mod tests { @@ -50,7 +34,18 @@ mod tests { #[init] async fn init() -> Context { - Context::init() + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); + + let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); + + let tx = UartTx::new_async(peripherals.UART0, &clocks, io.pins.gpio2).unwrap(); + let rx = UartRx::new_async(peripherals.UART1, &clocks, io.pins.gpio3).unwrap(); + + Context { tx, rx } } #[test] diff --git a/hil-test/tests/usb_serial_jtag.rs b/hil-test/tests/usb_serial_jtag.rs index bad8fc7fd7b..e4a582ba0c4 100644 --- a/hil-test/tests/usb_serial_jtag.rs +++ b/hil-test/tests/usb_serial_jtag.rs @@ -8,36 +8,19 @@ #[cfg(test)] #[embedded_test::tests] mod tests { - use esp_hal::{ - clock::ClockControl, - peripherals::Peripherals, - system::SystemControl, - timer::{timg::TimerGroup, ErasedTimer, OneShotTimer}, - usb_serial_jtag::UsbSerialJtag, - }; + use esp_hal::{prelude::*, timer::timg::TimerGroup, usb_serial_jtag::UsbSerialJtag}; use hil_test as _; - // When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html - macro_rules! mk_static { - ($t:ty,$val:expr) => {{ - static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new(); - #[deny(unused_attributes)] - let x = STATIC_CELL.uninit().write(($val)); - x - }}; - } - #[test] fn creating_peripheral_does_not_break_debug_connection() { - let peripherals = Peripherals::take(); - let system = SystemControl::new(peripherals.SYSTEM); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let System { + peripherals, + clocks, + .. + } = esp_hal::init(CpuClock::boot_default()); let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); - let timer0: ErasedTimer = timg0.timer0.into(); - let timers = [OneShotTimer::new(timer0)]; - let timers = mk_static!([OneShotTimer; 1], timers); - esp_hal_embassy::init(&clocks, timers); + esp_hal_embassy::init(&clocks, timg0.timer0); _ = UsbSerialJtag::new_async(peripherals.USB_DEVICE).split(); }