Skip to content

Commit

Permalink
Move RtcIo declaration into the gpio macro
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 16, 2024
1 parent 2090017 commit 5872964
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 76 deletions.
27 changes: 14 additions & 13 deletions esp-hal/src/gpio/lp_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ macro_rules! lp_gpio {
$($gpionum:literal)+
) => {
$(
impl $crate::gpio::RtcPin for GpioPin<$gpionum> {
unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) {
impl GpioPin<$gpionum> {
unsafe fn apply_wakeup_impl(&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) {
fn rtcio_pad_hold_impl(&mut self, enable: bool) {
let mask = 1 << $gpionum;
unsafe {
let lp_aon = $crate::peripherals::LP_AON::steal();
Expand All @@ -231,7 +231,7 @@ macro_rules! lp_gpio {

/// 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) {
fn rtc_set_config_impl(&mut self, input_enable: bool, mux: bool, func: $crate::gpio::RtcFunction) {
let mask = 1 << $gpionum;
unsafe {
// Select LP_IO
Expand All @@ -255,17 +255,18 @@ macro_rules! lp_gpio {
});
}
}
}

impl $crate::gpio::RtcPinWithResistors for GpioPin<$gpionum> {
fn rtcio_pullup(&mut self, enable: bool) {
fn rtcio_pulls(&mut self, up: Option<bool>, down: Option<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::steal() };
lp_io.gpio($gpionum).modify(|_, w| w.fun_wpd().bit(enable));
lp_io.gpio($gpionum).modify(|_, w| {
if let Some(enable) = up {
w.fun_wpu().bit(enable);
}
if let Some(enable) = down {
w.fun_wpd().bit(enable);
}
w
});
}
}
)+
Expand Down
150 changes: 93 additions & 57 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,47 @@ macro_rules! io_type {
}
}
};
(RtcIo, $gpionum:literal) => {};
(RtcIo, $gpionum:literal) => {
$crate::io_type!(RtcIoInput, $gpionum);

impl $crate::gpio::RtcPinWithResistors for GpioPin<$gpionum> {
fn rtcio_pullup(&mut self, enable: bool) {
self.rtcio_pulls(Some(enable), None)
}
fn rtcio_pulldown(&mut self, enable: bool) {
self.rtcio_pulls(None, Some(enable))
}
}
};
(RtcIoInput, $gpionum:literal) => {
impl $crate::gpio::RtcPin for GpioPin<$gpionum> {
#[cfg(xtensa)]
fn rtc_number(&self) -> u8 {
self.rtc_number_impl()
}

/// Set the RTC properties of the pin. If `mux` is true then then pin is
/// routed to RTC, when false it is routed to IO_MUX.
#[cfg(any(xtensa, esp32c6))]
fn rtc_set_config(
&mut self,
input_enable: bool,
mux: bool,
func: $crate::gpio::RtcFunction,
) {
self.rtc_set_config_impl(input_enable, mux, func)
}

fn rtcio_pad_hold(&mut self, enable: bool) {
self.rtcio_pad_hold_impl(enable)
}

#[cfg(any(esp32c2, esp32c3, esp32c6))]
unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) {
self.apply_wakeup_impl(wakeup, level)
}
}
};
}

#[doc(hidden)]
Expand Down Expand Up @@ -1143,47 +1183,46 @@ macro_rules! gpio {
#[doc(hidden)]
#[macro_export]
macro_rules! rtc_pins {
( $pin_num:expr ) => {
impl $crate::gpio::RtcPin for GpioPin<$pin_num> {
unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) {
let rtc_cntl = unsafe { $crate::peripherals::RTC_CNTL::steal() };
cfg_if::cfg_if! {
if #[cfg(esp32c2)] {
let gpio_wakeup = rtc_cntl.cntl_gpio_wakeup();
} else {
let gpio_wakeup = rtc_cntl.gpio_wakeup();
( $( $pin_num:expr )+ ) => {
$(
impl GpioPin<$pin_num> {
unsafe fn apply_wakeup_impl(&mut self, wakeup: bool, level: u8) {
let rtc_cntl = unsafe { $crate::peripherals::RTC_CNTL::steal() };
cfg_if::cfg_if! {
if #[cfg(esp32c2)] {
let gpio_wakeup = rtc_cntl.cntl_gpio_wakeup();
} else {
let gpio_wakeup = rtc_cntl.gpio_wakeup();
}
}
}

paste::paste! {
gpio_wakeup.modify(|_, w| w.[< gpio_pin $pin_num _wakeup_enable >]().bit(wakeup));
gpio_wakeup.modify(|_, w| w.[< gpio_pin $pin_num _int_type >]().bits(level));
paste::paste! {
gpio_wakeup.modify(|_, w| w.[< gpio_pin $pin_num _wakeup_enable >]().bit(wakeup));
gpio_wakeup.modify(|_, w| w.[< gpio_pin $pin_num _int_type >]().bits(level));
}
}
}

fn rtcio_pad_hold(&mut self, enable: bool) {
let rtc_cntl = unsafe { $crate::peripherals::RTC_CNTL::steal() };
paste::paste! {
rtc_cntl.pad_hold().modify(|_, w| w.[< gpio_pin $pin_num _hold >]().bit(enable));
fn rtcio_pad_hold_impl(&mut self, enable: bool) {
let rtc_cntl = unsafe { $crate::peripherals::RTC_CNTL::steal() };
paste::paste! {
rtc_cntl.pad_hold().modify(|_, w| w.[< gpio_pin $pin_num _hold >]().bit(enable));
}
}
}
}

impl $crate::gpio::RtcPinWithResistors for GpioPin<$pin_num> {
fn rtcio_pullup(&mut self, enable: bool) {
let io_mux = unsafe { $crate::peripherals::IO_MUX::steal() };
io_mux.gpio($pin_num).modify(|_, w| w.fun_wpu().bit(enable));
}

fn rtcio_pulldown(&mut self, enable: bool) {
let io_mux = unsafe { $crate::peripherals::IO_MUX::steal() };
io_mux.gpio($pin_num).modify(|_, w| w.fun_wpd().bit(enable));
fn rtcio_pulls(&mut self, up: Option<bool>, down: Option<bool>) {
let io_mux = unsafe { $crate::peripherals::IO_MUX::steal() };
io_mux.gpio($pin_num).modify(|_, w| {
if let Some(enable) = up {
w.fun_wpu().bit(enable);
}
if let Some(enable) = down {
w.fun_wpd().bit(enable);
}
w
});
}
}
}
};

( $( $pin_num:expr )+ ) => {
$( $crate::rtc_pins!($pin_num); )+
)+
};
}

Expand Down Expand Up @@ -1257,15 +1296,15 @@ macro_rules! rtcio_analog {
};

(@rtcio_pin $pin_num:expr, $rtc_pin:expr, $pin_reg:expr, $prefix:pat, $hold:ident $(, $rue:literal)?) => {
impl $crate::gpio::RtcPin for GpioPin<$pin_num>
impl GpioPin<$pin_num>
{
fn rtc_number(&self) -> u8 {
fn rtc_number_impl(&self) -> u8 {
$rtc_pin
}

/// Set the RTC properties of the pin. If `mux` is true then then pin is
/// routed to RTC, when false it is routed to IO_MUX.
fn rtc_set_config(&mut self, input_enable: bool, mux: bool, func: $crate::gpio::RtcFunction) {
fn rtc_set_config_impl(&mut self, input_enable: bool, mux: bool, func: $crate::gpio::RtcFunction) {
let rtcio = unsafe{ $crate::peripherals::RTC_IO::steal() };

$crate::gpio::enable_iomux_clk_gate();
Expand All @@ -1280,7 +1319,7 @@ macro_rules! rtcio_analog {
}
}

fn rtcio_pad_hold(&mut self, enable: bool) {
fn rtcio_pad_hold_impl(&mut self, enable: bool) {
let rtc_ctrl = unsafe { $crate::peripherals::LPWR::steal() };

cfg_if::cfg_if! {
Expand All @@ -1293,30 +1332,27 @@ macro_rules! rtcio_analog {

pad_hold.modify(|_, w| w.$hold().bit(enable));
}
}

$(
// FIXME: replace with $(ignore($rue)) once stable
$crate::rtcio_analog!(@ignore $rue);
impl $crate::gpio::RtcPinWithResistors for GpioPin<$pin_num>
{
fn rtcio_pullup(&mut self, enable: bool) {
let rtcio = unsafe { $crate::peripherals::RTC_IO::steal() };

paste::paste! {
rtcio.$pin_reg.modify(|_, w| w.[< $prefix rue >]().bit(enable));
}
}
$(
// FIXME: replace with $(ignore($rue)) once stable
$crate::rtcio_analog!(@ignore $rue);

fn rtcio_pulldown(&mut self, enable: bool) {
fn rtcio_pulls(&mut self, up: Option<bool>, down: Option<bool>) {
let rtcio = unsafe { $crate::peripherals::RTC_IO::steal() };

paste::paste! {
rtcio.$pin_reg.modify(|_, w| w.[< $prefix rde >]().bit(enable));
rtcio.$pin_reg.modify(|_, w| {
if let Some(enable) = up {
w.[< $prefix rue >]().bit(enable);
}
if let Some(enable) = down {
w.[< $prefix rde >]().bit(enable);
}
w
});
}
}
}
)?
)?
}
};

(
Expand Down
12 changes: 6 additions & 6 deletions esp-hal/src/soc/esp32/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,12 @@ crate::gpio! {
(27, 0, [Input, Output, Analog, RtcIo, Touch] (5 => EMAC_RX_DV) ())
(32, 1, [Input, Output, Analog, RtcIo, Touch])
(33, 1, [Input, Output, Analog, RtcIo, Touch])
(34, 1, [Input, Analog, RtcIo])
(35, 1, [Input, Analog, RtcIo])
(36, 1, [Input, Analog, RtcIo])
(37, 1, [Input, Analog, RtcIo])
(38, 1, [Input, Analog, RtcIo])
(39, 1, [Input, Analog, RtcIo])
(34, 1, [Input, Analog, RtcIoInput])
(35, 1, [Input, Analog, RtcIoInput])
(36, 1, [Input, Analog, RtcIoInput])
(37, 1, [Input, Analog, RtcIoInput])
(38, 1, [Input, Analog, RtcIoInput])
(39, 1, [Input, Analog, RtcIoInput])
}

crate::rtcio_analog! {
Expand Down

0 comments on commit 5872964

Please sign in to comment.