diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index 3216fe0f476..25571bbc170 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -55,13 +55,13 @@ xtensa-lx = { version = "0.9.0", optional = true } # IMPORTANT: # Each supported device MUST have its PAC included below along with a # corresponding feature. -esp32 = { version = "0.33.0", features = ["critical-section", "rt"], optional = true } -esp32c2 = { version = "0.22.0", features = ["critical-section", "rt"], optional = true } -esp32c3 = { version = "0.25.0", features = ["critical-section", "rt"], optional = true } -esp32c6 = { version = "0.16.0", features = ["critical-section", "rt"], optional = true } -esp32h2 = { version = "0.12.0", features = ["critical-section", "rt"], optional = true } -esp32s2 = { version = "0.24.0", features = ["critical-section", "rt"], optional = true } -esp32s3 = { version = "0.28.0", features = ["critical-section", "rt"], optional = true } +esp32 = { version = "0.33.0", git = "https://github.com/esp-rs/esp-pacs.git", rev = "317f89c", features = ["critical-section", "rt"], optional = true } +esp32c2 = { version = "0.22.0", git = "https://github.com/esp-rs/esp-pacs.git", rev = "317f89c", features = ["critical-section", "rt"], optional = true } +esp32c3 = { version = "0.25.0", git = "https://github.com/esp-rs/esp-pacs.git", rev = "317f89c", features = ["critical-section", "rt"], optional = true } +esp32c6 = { version = "0.16.0", git = "https://github.com/esp-rs/esp-pacs.git", rev = "317f89c", features = ["critical-section", "rt"], optional = true } +esp32h2 = { version = "0.12.0", git = "https://github.com/esp-rs/esp-pacs.git", rev = "317f89c", features = ["critical-section", "rt"], optional = true } +esp32s2 = { version = "0.24.0", git = "https://github.com/esp-rs/esp-pacs.git", rev = "317f89c", features = ["critical-section", "rt"], optional = true } +esp32s3 = { version = "0.28.0", git = "https://github.com/esp-rs/esp-pacs.git", rev = "317f89c", features = ["critical-section", "rt"], optional = true } [target.'cfg(target_arch = "riscv32")'.dependencies] esp-riscv-rt = { version = "0.9.0", path = "../esp-riscv-rt" } diff --git a/esp-hal/src/gpio/lp_io.rs b/esp-hal/src/gpio/lp_io.rs index 8f1fd6e7151..cde98a73e55 100644 --- a/esp-hal/src/gpio/lp_io.rs +++ b/esp-hal/src/gpio/lp_io.rs @@ -28,6 +28,12 @@ use core::marker::PhantomData; +use super::{InputPin, OutputPin, RtcPin}; +use crate::{ + peripheral::Peripheral, + peripherals::{GPIO, LP_AON, LP_IO}, +}; + /// A GPIO output pin configured for low power operation pub struct LowPowerOutput<'d, const PIN: u8> { phantom: PhantomData<&'d ()>, @@ -35,11 +41,11 @@ pub struct LowPowerOutput<'d, const PIN: u8> { impl<'d, const PIN: u8> LowPowerOutput<'d, PIN> { /// Create a new output pin for use by the low-power core - pub fn new

(_pin: impl crate::peripheral::Peripheral

+ 'd) -> Self + pub fn new

(_pin: impl Peripheral

+ 'd) -> Self where - P: super::OutputPin + RtcPin, + P: OutputPin + RtcPin, { - crate::gpio::lp_io::init_low_power_pin(PIN); + init_low_power_pin(PIN); let this = Self { phantom: PhantomData, @@ -50,7 +56,7 @@ impl<'d, const PIN: u8> LowPowerOutput<'d, PIN> { } fn output_enable(&self, enable: bool) { - let lp_io = unsafe { &*crate::peripherals::LP_IO::PTR }; + let lp_io = unsafe { LP_IO::steal() }; if enable { lp_io .out_enable_w1ts() @@ -70,11 +76,11 @@ pub struct LowPowerInput<'d, const PIN: u8> { impl<'d, const PIN: u8> LowPowerInput<'d, PIN> { /// Create a new input pin for use by the low-power core - pub fn new

(_pin: impl crate::peripheral::Peripheral

+ 'd) -> Self + pub fn new

(_pin: impl Peripheral

+ 'd) -> Self where - P: super::InputPin + RtcPin, + P: InputPin + RtcPin, { - crate::gpio::lp_io::init_low_power_pin(PIN); + init_low_power_pin(PIN); let this = Self { phantom: PhantomData, @@ -87,17 +93,23 @@ impl<'d, const PIN: u8> LowPowerInput<'d, PIN> { } fn input_enable(&self, enable: bool) { - get_pin_reg(PIN).modify(|_, w| w.fun_ie().bit(enable)); + unsafe { LP_IO::steal() } + .gpio(PIN as usize) + .modify(|_, w| w.fun_ie().bit(enable)); } /// Sets pull-up enable for the pin pub fn pullup_enable(&self, enable: bool) { - get_pin_reg(PIN).modify(|_, w| w.fun_wpu().bit(enable)); + unsafe { LP_IO::steal() } + .gpio(PIN as usize) + .modify(|_, w| w.fun_wpu().bit(enable)); } /// Sets pull-down enable for the pin pub fn pulldown_enable(&self, enable: bool) { - get_pin_reg(PIN).modify(|_, w| w.fun_wpd().bit(enable)); + unsafe { LP_IO::steal() } + .gpio(PIN as usize) + .modify(|_, w| w.fun_wpd().bit(enable)); } } @@ -108,11 +120,11 @@ pub struct LowPowerOutputOpenDrain<'d, const PIN: u8> { impl<'d, const PIN: u8> LowPowerOutputOpenDrain<'d, PIN> { /// Create a new output pin for use by the low-power core - pub fn new

(_pin: impl crate::peripheral::Peripheral

+ 'd) -> Self + pub fn new

(_pin: impl Peripheral

+ 'd) -> Self where - P: super::InputPin + super::OutputPin + RtcPin, + P: InputPin + OutputPin + RtcPin, { - crate::gpio::lp_io::init_low_power_pin(PIN); + init_low_power_pin(PIN); let this = Self { phantom: PhantomData, @@ -128,7 +140,7 @@ impl<'d, const PIN: u8> LowPowerOutputOpenDrain<'d, PIN> { } fn output_enable(&self, enable: bool) { - let lp_io = unsafe { &*crate::peripherals::LP_IO::PTR }; + let lp_io = unsafe { LP_IO::steal() }; if enable { lp_io .out_enable_w1ts() @@ -141,48 +153,40 @@ impl<'d, const PIN: u8> LowPowerOutputOpenDrain<'d, PIN> { } fn input_enable(&self, enable: bool) { - get_pin_reg(PIN).modify(|_, w| w.fun_ie().bit(enable)); + unsafe { LP_IO::steal() } + .gpio(PIN as usize) + .modify(|_, w| w.fun_ie().bit(enable)); } /// Sets pull-up enable for the pin pub fn pullup_enable(&self, enable: bool) { - get_pin_reg(PIN).modify(|_, w| w.fun_wpu().bit(enable)); + unsafe { LP_IO::steal() } + .gpio(PIN as usize) + .modify(|_, w| w.fun_wpu().bit(enable)); } /// Sets pull-down enable for the pin pub fn pulldown_enable(&self, enable: bool) { - get_pin_reg(PIN).modify(|_, w| w.fun_wpd().bit(enable)); + unsafe { LP_IO::steal() } + .gpio(PIN as usize) + .modify(|_, w| w.fun_wpd().bit(enable)); } fn set_open_drain_output(&self, enable: bool) { - use crate::peripherals::GPIO; - let gpio = unsafe { &*GPIO::PTR }; - - gpio.pin(PIN as usize) + unsafe { GPIO::steal() } + .pin(PIN as usize) .modify(|_, w| w.pad_driver().bit(enable)); } } pub(crate) fn init_low_power_pin(pin: u8) { - let lp_aon = unsafe { &*crate::peripherals::LP_AON::PTR }; - - lp_aon + unsafe { LP_AON::steal() } .gpio_mux() .modify(|r, w| unsafe { w.sel().bits(r.sel().bits() | 1 << pin) }); - get_pin_reg(pin).modify(|_, w| unsafe { w.mcu_sel().bits(0) }); -} - -#[inline(always)] -fn get_pin_reg(pin: u8) -> &'static crate::peripherals::lp_io::GPIO0 { - // ideally we should change the SVD and make the GPIOx registers into an - // array - unsafe { - let lp_io = &*crate::peripherals::LP_IO::PTR; - let pin_ptr = (lp_io.gpio0().as_ptr()).add(pin as usize); - - &*(pin_ptr as *const crate::peripherals::lp_io::GPIO0) - } + unsafe { LP_IO::steal() } + .gpio(pin as usize) + .modify(|_, w| unsafe { w.mcu_sel().bits(0) }); } #[doc(hidden)] @@ -191,77 +195,76 @@ macro_rules! lp_gpio { ( $($gpionum:literal)+ ) => { - paste::paste!{ - $( - impl $crate::gpio::RtcPin for GpioPin<$gpionum> { - unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) { - let lp_io = &*$crate::peripherals::LP_IO::ptr(); - lp_io.[< pin $gpionum >]().modify(|_, w| { - w.wakeup_enable().bit(wakeup).int_type().bits(level) + $( + impl $crate::gpio::RtcPin for GpioPin<$gpionum> { + unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) { + let lp_io = $crate::peripherals::LP_IO::steal(); + lp_io.pin($gpionum).modify(|_, w| { + w.wakeup_enable().bit(wakeup).int_type().bits(level) + }); + } + + fn rtcio_pad_hold(&mut self, enable: bool) { + let mask = 1 << $gpionum; + unsafe { + let lp_aon = $crate::peripherals::LP_AON::steal(); + + lp_aon.gpio_hold0().modify(|r, w| { + if enable { + w.gpio_hold0().bits(r.gpio_hold0().bits() | mask) + } else { + w.gpio_hold0().bits(r.gpio_hold0().bits() & !mask) + } }); } + } - fn rtcio_pad_hold(&mut self, enable: bool) { - let mask = 1 << $gpionum; - unsafe { - let lp_aon = &*$crate::peripherals::LP_AON::ptr(); - - lp_aon.gpio_hold0().modify(|r, w| { - if enable { - w.gpio_hold0().bits(r.gpio_hold0().bits() | mask) + /// Set the LP properties of the pin. If `mux` is true then then pin is + /// routed to LP_IO, when false it is routed to IO_MUX. + fn rtc_set_config(&mut self, input_enable: bool, mux: bool, func: $crate::gpio::RtcFunction) { + let mask = 1 << $gpionum; + unsafe { + // Select LP_IO + let lp_aon = $crate::peripherals::LP_AON::steal(); + lp_aon + .gpio_mux() + .modify(|r, w| { + if mux { + w.sel().bits(r.sel().bits() | mask) } else { - w.gpio_hold0().bits(r.gpio_hold0().bits() & !mask) + w.sel().bits(r.sel().bits() & !mask) } }); - } - } - /// Set the LP properties of the pin. If `mux` is true then then pin is - /// routed to LP_IO, when false it is routed to IO_MUX. - fn rtc_set_config(&mut self, input_enable: bool, mux: bool, func: $crate::gpio::RtcFunction) { - let mask = 1 << $gpionum; - unsafe { - // Select LP_IO - let lp_aon = &*$crate::peripherals::LP_AON::ptr(); - lp_aon - .gpio_mux() - .modify(|r, w| { - if mux { - w.sel().bits(r.sel().bits() | mask) - } else { - w.sel().bits(r.sel().bits() & !mask) - } - }); - - // Configure input, function and select normal operation registers - let lp_io = &*$crate::peripherals::LP_IO::ptr(); - lp_io.[< gpio $gpionum >]().modify(|_, w| { - w - .slp_sel().bit(false) - .fun_ie().bit(input_enable) - .mcu_sel().bits(func as u8) - }); - } + // Configure input, function and select normal operation registers + let lp_io = $crate::peripherals::LP_IO::steal(); + lp_io.gpio($gpionum).modify(|_, w| { + w.slp_sel().bit(false); + w.fun_ie().bit(input_enable); + w.mcu_sel().bits(func as u8) + }); } } + } - impl $crate::gpio::RtcPinWithResistors for GpioPin<$gpionum> { - fn rtcio_pullup(&mut self, enable: bool) { - let lp_io = unsafe { &*$crate::peripherals::LP_IO::ptr() }; - lp_io.[< gpio $gpionum >]().modify(|_, w| w.fun_wpu().bit(enable)); - } + impl $crate::gpio::RtcPinWithResistors for GpioPin<$gpionum> { + fn rtcio_pullup(&mut self, enable: bool) { + let lp_io = unsafe { $crate::peripherals::LP_IO::steal() }; + lp_io.gpio($gpionum).modify(|_, w| w.fun_wpu().bit(enable)); + } - fn rtcio_pulldown(&mut self, enable: bool) { - let lp_io = unsafe { &*$crate::peripherals::LP_IO::ptr() }; - lp_io.[< gpio $gpionum >]().modify(|_, w| w.fun_wpd().bit(enable)); - } + fn rtcio_pulldown(&mut self, enable: bool) { + let lp_io = unsafe { $crate::peripherals::LP_IO::steal() }; + lp_io.gpio($gpionum).modify(|_, w| w.fun_wpd().bit(enable)); } - )+ + } + )+ - #[doc(hidden)] - #[macro_export] - macro_rules! handle_rtcio { - ($this:expr, $inner:ident, $code:tt) => { + #[doc(hidden)] + #[macro_export] + macro_rules! handle_rtcio { + ($this:expr, $inner:ident, $code:tt) => { + paste::paste! { match $this { $( AnyPinInner::[]($inner) => { @@ -273,12 +276,10 @@ macro_rules! lp_gpio { } } } - pub(crate) use handle_rtcio; - pub(crate) use handle_rtcio as handle_rtcio_with_resistors; } + pub(crate) use handle_rtcio; + pub(crate) use handle_rtcio as handle_rtcio_with_resistors; } } pub(crate) use lp_gpio; - -use super::RtcPin; diff --git a/esp-hal/src/i2c.rs b/esp-hal/src/i2c.rs index 87d15c9b66a..25a141c08a4 100644 --- a/esp-hal/src/i2c.rs +++ b/esp-hal/src/i2c.rs @@ -2517,48 +2517,49 @@ pub mod lp_i2c { let lp_peri = unsafe { &*crate::peripherals::LP_PERI::PTR }; unsafe { + // FIXME: use GPIO APIs to configure pins lp_aon .gpio_mux() .modify(|r, w| w.sel().bits(r.sel().bits() | (1 << 6))); lp_aon .gpio_mux() .modify(|r, w| w.sel().bits(r.sel().bits() | (1 << 7))); - lp_io.gpio6().modify(|_, w| w.mcu_sel().bits(1)); // TODO + lp_io.gpio(6).modify(|_, w| w.mcu_sel().bits(1)); // TODO - lp_io.gpio7().modify(|_, w| w.mcu_sel().bits(1)); + lp_io.gpio(7).modify(|_, w| w.mcu_sel().bits(1)); // Set output mode to Normal - lp_io.pin6().modify(|_, w| w.pad_driver().set_bit()); + lp_io.pin(6).modify(|_, w| w.pad_driver().set_bit()); // Enable output (writing to write-1-to-set register, then internally the // `GPIO_OUT_REG` will be set) lp_io .out_enable_w1ts() .write(|w| w.enable_w1ts().bits(1 << 6)); // Enable input - lp_io.gpio6().modify(|_, w| w.fun_ie().set_bit()); + lp_io.gpio(6).modify(|_, w| w.fun_ie().set_bit()); // Disable pulldown (enable internal weak pull-down) - lp_io.gpio6().modify(|_, w| w.fun_wpd().clear_bit()); + lp_io.gpio(6).modify(|_, w| w.fun_wpd().clear_bit()); // Enable pullup - lp_io.gpio6().modify(|_, w| w.fun_wpu().set_bit()); + lp_io.gpio(6).modify(|_, w| w.fun_wpu().set_bit()); // Same process for SCL pin - lp_io.pin7().modify(|_, w| w.pad_driver().set_bit()); + lp_io.pin(7).modify(|_, w| w.pad_driver().set_bit()); // Enable output (writing to write-1-to-set register, then internally the // `GPIO_OUT_REG` will be set) lp_io .out_enable_w1ts() .write(|w| w.enable_w1ts().bits(1 << 7)); // Enable input - lp_io.gpio7().modify(|_, w| w.fun_ie().set_bit()); + lp_io.gpio(7).modify(|_, w| w.fun_ie().set_bit()); // Disable pulldown (enable internal weak pull-down) - lp_io.gpio7().modify(|_, w| w.fun_wpd().clear_bit()); + lp_io.gpio(7).modify(|_, w| w.fun_wpd().clear_bit()); // Enable pullup - lp_io.gpio7().modify(|_, w| w.fun_wpu().set_bit()); + lp_io.gpio(7).modify(|_, w| w.fun_wpu().set_bit()); // Select LP I2C function for the SDA and SCL pins - lp_io.gpio6().modify(|_, w| w.mcu_sel().bits(1)); - lp_io.gpio7().modify(|_, w| w.mcu_sel().bits(1)); + lp_io.gpio(6).modify(|_, w| w.mcu_sel().bits(1)); + lp_io.gpio(7).modify(|_, w| w.mcu_sel().bits(1)); } // Initialize LP I2C HAL */ @@ -2584,23 +2585,21 @@ pub mod lp_i2c { // Initialize LP I2C Master mode me.i2c.ctr().modify(|_, w| unsafe { // Clear register - w.bits(0) - // Use open drain output for SDA and SCL - .sda_force_out() - .set_bit() - .scl_force_out() - .set_bit() - // Ensure that clock is enabled - .clk_en() - .set_bit() + w.bits(0); + // Use open drain output for SDA and SCL + w.sda_force_out().set_bit(); + w.scl_force_out().set_bit(); + // Ensure that clock is enabled + w.clk_en().set_bit() }); // First, reset the fifo buffers me.i2c.fifo_conf().modify(|_, w| w.nonfifo_en().clear_bit()); - me.i2c - .ctr() - .modify(|_, w| w.tx_lsb_first().clear_bit().rx_lsb_first().clear_bit()); + me.i2c.ctr().modify(|_, w| { + w.tx_lsb_first().clear_bit(); + w.rx_lsb_first().clear_bit() + }); me.reset_fifo(); @@ -2664,10 +2663,8 @@ pub mod lp_i2c { // Write data to registers unsafe { me.i2c.clk_conf().modify(|_, w| { - w.sclk_sel() - .clear_bit() - .sclk_div_num() - .bits((clkm_div - 1) as u8) + w.sclk_sel().clear_bit(); + w.sclk_div_num().bits((clkm_div - 1) as u8) }); // scl period @@ -2676,10 +2673,8 @@ pub mod lp_i2c { .write(|w| w.scl_low_period().bits(scl_low_period as u16)); me.i2c.scl_high_period().write(|w| { - w.scl_high_period() - .bits(scl_high_period as u16) - .scl_wait_high_period() - .bits(scl_wait_high_period as u8) + w.scl_high_period().bits(scl_high_period as u16); + w.scl_wait_high_period().bits(scl_wait_high_period as u8) }); // sda sample me.i2c @@ -2706,10 +2701,8 @@ pub mod lp_i2c { .write(|w| w.time().bits(scl_stop_hold_time as u16)); me.i2c.to().write(|w| { - w.time_out_en() - .bit(time_out_en) - .time_out_value() - .bits(time_out_value.try_into().unwrap()) + w.time_out_en().bit(time_out_en); + w.time_out_value().bits(time_out_value.try_into().unwrap()) }); } diff --git a/esp-hal/src/twai/mod.rs b/esp-hal/src/twai/mod.rs index 67c57c6fd40..2aefb3ada4a 100644 --- a/esp-hal/src/twai/mod.rs +++ b/esp-hal/src/twai/mod.rs @@ -778,7 +778,7 @@ where // Enable extended register layout T::register_block() .clock_divider() - .modify(|r, w| unsafe { w.bits(r.bits() | 0x80) }); + .modify(|_, w| w.ext_mode().set_bit()); } let rx_pull = if no_transceiver { @@ -863,13 +863,13 @@ where // ESP32 Revision 2 or later. Reserved otherwise. T::register_block() .int_ena() - .modify(|r, w| unsafe { w.bits(r.bits() | 0x10) }); + .modify(|_, w| w.brp_div().set_bit()); prescaler = timing.baud_rate_prescaler / 2; } else { // Disable /2 baudrate divider by clearing brp_div. T::register_block() .int_ena() - .modify(|r, w| unsafe { w.bits(r.bits() & !0x10) }); + .modify(|_, w| w.brp_div().clear_bit()); } } diff --git a/esp-hal/src/uart.rs b/esp-hal/src/uart.rs index 67ee932a03e..27e7e05002f 100644 --- a/esp-hal/src/uart.rs +++ b/esp-hal/src/uart.rs @@ -2436,6 +2436,7 @@ pub mod lp_uart { let lp_io = unsafe { &*crate::peripherals::LP_IO::PTR }; let lp_aon = unsafe { &*crate::peripherals::LP_AON::PTR }; + // FIXME: use GPIO APIs to configure pins lp_aon .gpio_mux() .modify(|r, w| unsafe { w.sel().bits(r.sel().bits() | 1 << 4) }); @@ -2443,8 +2444,8 @@ pub mod lp_uart { .gpio_mux() .modify(|r, w| unsafe { w.sel().bits(r.sel().bits() | 1 << 5) }); - lp_io.gpio4().modify(|_, w| unsafe { w.mcu_sel().bits(1) }); - lp_io.gpio5().modify(|_, w| unsafe { w.mcu_sel().bits(1) }); + lp_io.gpio(4).modify(|_, w| unsafe { w.mcu_sel().bits(1) }); + lp_io.gpio(5).modify(|_, w| unsafe { w.mcu_sel().bits(1) }); Self::new_with_config(uart, Config::default()) }