#![allow(clippy::identity_op)]
use pastey::paste;
use rp2350_pac::sio::RegisterBlock as SioRegisterBlock;
use rp2350_pac::{IO_BANK0, PADS_BANK0};
use crate::reset::{self, Resettable};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Function {
Hstx = 0,
Spi = 1,
Uart = 2,
I2c = 3,
Pwm = 4,
Sio = 5,
Pio0 = 6,
Pio1 = 7,
Pio2 = 8,
Gpck = 9,
Usb = 10,
UartAux = 11,
Null = 31,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Pull {
None,
Up,
Down,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DriveStrength {
_2mA = 0,
_4mA = 1,
_8mA = 2,
_12mA = 3,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SlewRate {
Slow,
Fast,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum OutputOverride {
Normal = 0,
Invert = 1,
Low = 2,
High = 3,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum OutputEnableOverride {
Normal = 0,
Invert = 1,
Disable = 2,
Enable = 3,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum InputOverride {
Normal = 0,
Invert = 1,
Low = 2,
High = 3,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IrqOverride {
Normal = 0,
Invert = 1,
Low = 2,
High = 3,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum EdgeLevel {
LevelLow,
LevelHigh,
EdgeLow,
EdgeHigh,
}
macro_rules! impl_pin {
($n:literal, low) => {
paste! {
impl Pin<$n> {
pub const PIN: u32 = $n;
pub const MASK: u32 = 1u32 << $n;
#[inline]
pub const unsafe fn steal() -> crate::Steal<Self> {
crate::Steal(Self { _marker: core::marker::PhantomData })
}
#[inline(always)]
fn io_bank(&self) -> rp2350_pac::IO_BANK0 {
unsafe { IO_BANK0::steal() }
}
#[inline(always)]
fn pads_bank(&self) -> rp2350_pac::PADS_BANK0 {
unsafe { PADS_BANK0::steal() }
}
#[inline(always)]
fn sio(&self) -> &'static SioRegisterBlock {
unsafe { &*crate::sio::REGS }
}
#[inline]
pub fn set_function(&mut self, func: Function) {
self.io_bank().[<gpio $n _ctrl>].write_funcsel(func as u32);
}
#[inline]
pub fn function(&self) -> u32 {
self.io_bank().[<gpio $n _ctrl>].funcsel()
}
#[inline]
pub fn set_function_sio(&mut self) {
self.set_function(Function::Sio);
}
#[inline]
pub fn set_function_pio0(&mut self) {
self.set_function(Function::Pio0);
}
#[inline]
pub fn set_function_pio1(&mut self) {
self.set_function(Function::Pio1);
}
#[inline]
pub fn set_function_pio2(&mut self) {
self.set_function(Function::Pio2);
}
#[inline]
pub fn set_function_null(&mut self) {
self.set_function(Function::Null);
}
#[inline]
pub fn init_pad(&mut self) {
let pad = &self.pads_bank().[<gpio $n>];
pad.clr_iso(); pad.set_ie(); pad.clr_od(); }
#[inline]
pub fn set_pull(&mut self, pull: Pull) {
let pad = &self.pads_bank().[<gpio $n>];
match pull {
Pull::None => {
pad.clr_pue();
pad.clr_pde();
}
Pull::Up => {
pad.set_pue();
pad.clr_pde();
}
Pull::Down => {
pad.clr_pue();
pad.set_pde();
}
}
}
#[inline]
pub fn set_pull_up(&mut self) {
self.set_pull(Pull::Up);
}
#[inline]
pub fn set_pull_down(&mut self) {
self.set_pull(Pull::Down);
}
#[inline]
pub fn set_pull_none(&mut self) {
self.set_pull(Pull::None);
}
#[inline]
pub fn set_drive_strength(&mut self, strength: DriveStrength) {
self.pads_bank().[<gpio $n>].write_drive(strength as u32);
}
#[inline]
pub fn set_slew_rate(&mut self, rate: SlewRate) {
let pad = &self.pads_bank().[<gpio $n>];
match rate {
SlewRate::Slow => pad.clr_slewfast(),
SlewRate::Fast => pad.set_slewfast(),
}
}
#[inline]
pub fn set_schmitt(&mut self, enable: bool) {
let pad = &self.pads_bank().[<gpio $n>];
if enable {
pad.set_schmitt();
} else {
pad.clr_schmitt();
}
}
#[inline]
pub fn enable_input(&mut self) {
self.pads_bank().[<gpio $n>].set_ie();
}
#[inline]
pub fn disable_input(&mut self) {
self.pads_bank().[<gpio $n>].clr_ie();
}
#[inline]
pub fn set_output_disable(&mut self, disable: bool) {
let pad = &self.pads_bank().[<gpio $n>];
if disable {
pad.set_od();
} else {
pad.clr_od();
}
}
#[inline]
pub fn set_output_override(&mut self, ov: OutputOverride) {
self.io_bank().[<gpio $n _ctrl>].write_outover(ov as u32);
}
#[inline]
pub fn set_output_enable_override(&mut self, ov: OutputEnableOverride) {
self.io_bank().[<gpio $n _ctrl>].write_oeover(ov as u32);
}
#[inline]
pub fn set_input_override(&mut self, ov: InputOverride) {
self.io_bank().[<gpio $n _ctrl>].write_inover(ov as u32);
}
#[inline]
pub fn set_irq_override(&mut self, ov: IrqOverride) {
self.io_bank().[<gpio $n _ctrl>].write_irqover(ov as u32);
}
#[inline]
pub fn set_direction_out(&mut self) {
self.sio().gpio_oe_set.write_gpio_oe_set(Self::MASK);
}
#[inline]
pub fn set_direction_in(&mut self) {
self.sio().gpio_oe_clr.write_gpio_oe_clr(Self::MASK);
}
#[inline]
pub fn is_output(&self) -> bool {
(self.sio().gpio_oe.gpio_oe() & Self::MASK) != 0
}
#[inline]
pub fn get(&self) -> bool {
(self.sio().gpio_in.gpio_in() & Self::MASK) != 0
}
#[inline]
pub fn set_high(&self) {
self.sio().gpio_out_set.write_gpio_out_set(Self::MASK);
}
#[inline]
pub fn set_low(&self) {
self.sio().gpio_out_clr.write_gpio_out_clr(Self::MASK);
}
#[inline]
pub fn toggle(&self) {
self.sio().gpio_out_xor.write_gpio_out_xor(Self::MASK);
}
#[inline]
pub fn set(&self, high: bool) {
if high {
self.set_high();
} else {
self.set_low();
}
}
#[inline]
pub fn output_level(&self) -> bool {
(self.sio().gpio_out.gpio_out() & Self::MASK) != 0
}
#[inline]
pub fn into_output(&mut self) -> &Self {
self.init_pad();
self.set_function_sio();
self.set_direction_out();
self
}
#[inline]
pub fn into_input(&mut self) -> &Self {
self.init_pad();
self.set_function_sio();
self.set_direction_in();
self
}
#[inline]
pub fn into_input_pullup(&mut self) -> &Self {
self.into_input();
self.set_pull_up();
self
}
#[inline]
pub fn into_input_pulldown(&mut self) -> &Self {
self.into_input();
self.set_pull_down();
self
}
#[inline]
pub fn into_pio0(&mut self) -> &Self {
self.init_pad();
self.set_function_pio0();
self
}
#[inline]
pub fn into_pio1(&mut self) -> &Self {
self.init_pad();
self.set_function_pio1();
self
}
#[inline]
pub fn into_pio2(&mut self) -> &Self {
self.init_pad();
self.set_function_pio2();
self
}
#[inline]
pub fn into_pwm(&mut self) -> &Self {
self.init_pad();
self.set_function(Function::Pwm);
self
}
#[inline]
pub fn into_i2c(&mut self) -> &Self {
self.init_pad();
self.set_function(Function::I2c);
self.set_pull_up();
self
}
#[inline]
pub fn into_spi(&mut self) -> &Self {
self.init_pad();
self.set_function(Function::Spi);
self
}
#[inline]
pub fn into_uart(&mut self) -> &Self {
self.init_pad();
self.set_function(Function::Uart);
self
}
#[inline]
pub fn enable_interrupt(&self, edge: EdgeLevel) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
match REG_IDX {
0 => io.proc0_inte0.set_bits(bit),
1 => io.proc0_inte1.set_bits(bit),
2 => io.proc0_inte2.set_bits(bit),
3 => io.proc0_inte3.set_bits(bit),
_ => {} }
}
#[inline]
pub fn disable_interrupt(&self, edge: EdgeLevel) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
match REG_IDX {
0 => io.proc0_inte0.clr_bits(bit),
1 => io.proc0_inte1.clr_bits(bit),
2 => io.proc0_inte2.clr_bits(bit),
3 => io.proc0_inte3.clr_bits(bit),
_ => {}
}
}
#[inline]
pub fn interrupt_status(&self, edge: EdgeLevel) -> bool {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
let status = match REG_IDX {
0 => io.proc0_ints0.read_bits(),
1 => io.proc0_ints1.read_bits(),
2 => io.proc0_ints2.read_bits(),
3 => io.proc0_ints3.read_bits(),
_ => 0,
};
(status & bit) != 0
}
#[inline]
pub fn clear_interrupt(&self, edge: EdgeLevel) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
match REG_IDX {
0 => io.intr0.write_bits(bit),
1 => io.intr1.write_bits(bit),
2 => io.intr2.write_bits(bit),
3 => io.intr3.write_bits(bit),
_ => {}
}
}
#[inline]
pub fn disable_all_interrupts(&self) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
const MASK: u32 = 0xF << BIT_OFFSET;
let io = self.io_bank();
match REG_IDX {
0 => io.proc0_inte0.clr_bits(MASK),
1 => io.proc0_inte1.clr_bits(MASK),
2 => io.proc0_inte2.clr_bits(MASK),
3 => io.proc0_inte3.clr_bits(MASK),
_ => {}
}
}
#[inline]
pub fn pending_flags(&self) -> u32 {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let io = self.io_bank();
let raw = match REG_IDX {
0 => io.proc0_ints0.read_bits(),
1 => io.proc0_ints1.read_bits(),
2 => io.proc0_ints2.read_bits(),
3 => io.proc0_ints3.read_bits(),
_ => 0,
};
(raw >> BIT_OFFSET) & 0xF
}
#[inline]
pub fn clear_all_interrupts(&self) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
const MASK: u32 = 0xF << BIT_OFFSET;
let io = self.io_bank();
match REG_IDX {
0 => io.intr0.write_bits(MASK),
1 => io.intr1.write_bits(MASK),
2 => io.intr2.write_bits(MASK),
3 => io.intr3.write_bits(MASK),
_ => {}
}
}
#[inline]
pub fn check_and_clear(&self, edge: EdgeLevel) -> bool {
if self.interrupt_status(edge) {
self.clear_interrupt(edge);
true
} else {
false
}
}
#[inline]
pub fn set_dormant_wake(&self, edge: EdgeLevel) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
match REG_IDX {
0 => io.dormant_wake_inte0.set_bits(bit),
1 => io.dormant_wake_inte1.set_bits(bit),
2 => io.dormant_wake_inte2.set_bits(bit),
3 => io.dormant_wake_inte3.set_bits(bit),
_ => {} }
}
#[inline]
pub fn clear_dormant_wake(&self) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
const MASK: u32 = 0xF << BIT_OFFSET;
let io = self.io_bank();
match REG_IDX {
0 => io.dormant_wake_inte0.clr_bits(MASK),
1 => io.dormant_wake_inte1.clr_bits(MASK),
2 => io.dormant_wake_inte2.clr_bits(MASK),
3 => io.dormant_wake_inte3.clr_bits(MASK),
_ => {}
}
}
}
impl core::fmt::Debug for Pin<$n> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Pin")
.field("number", &$n)
.field("function", &self.function())
.field("is_output", &self.is_output())
.finish()
}
}
}
};
($n:literal, high) => {
paste! {
impl Pin<$n> {
pub const PIN: u32 = $n;
pub const MASK: u32 = 1u32 << ($n - 32);
#[inline]
pub const unsafe fn steal() -> crate::Steal<Self> {
crate::Steal(Self { _marker: core::marker::PhantomData })
}
#[inline(always)]
fn io_bank(&self) -> rp2350_pac::IO_BANK0 {
unsafe { IO_BANK0::steal() }
}
#[inline(always)]
fn pads_bank(&self) -> rp2350_pac::PADS_BANK0 {
unsafe { PADS_BANK0::steal() }
}
#[inline(always)]
fn sio(&self) -> &'static SioRegisterBlock {
unsafe { &*crate::sio::REGS }
}
#[inline]
pub fn set_function(&mut self, func: Function) {
self.io_bank().[<gpio $n _ctrl>].write_funcsel(func as u32);
}
#[inline]
pub fn function(&self) -> u32 {
self.io_bank().[<gpio $n _ctrl>].funcsel()
}
#[inline]
pub fn set_function_sio(&mut self) {
self.set_function(Function::Sio);
}
#[inline]
pub fn set_function_pio0(&mut self) {
self.set_function(Function::Pio0);
}
#[inline]
pub fn set_function_pio1(&mut self) {
self.set_function(Function::Pio1);
}
#[inline]
pub fn set_function_pio2(&mut self) {
self.set_function(Function::Pio2);
}
#[inline]
pub fn set_function_null(&mut self) {
self.set_function(Function::Null);
}
#[inline]
pub fn init_pad(&mut self) {
let pad = &self.pads_bank().[<gpio $n>];
pad.clr_iso();
pad.set_ie();
pad.clr_od();
}
#[inline]
pub fn set_pull(&mut self, pull: Pull) {
let pad = &self.pads_bank().[<gpio $n>];
match pull {
Pull::None => {
pad.clr_pue();
pad.clr_pde();
}
Pull::Up => {
pad.set_pue();
pad.clr_pde();
}
Pull::Down => {
pad.clr_pue();
pad.set_pde();
}
}
}
#[inline]
pub fn set_pull_up(&mut self) {
self.set_pull(Pull::Up);
}
#[inline]
pub fn set_pull_down(&mut self) {
self.set_pull(Pull::Down);
}
#[inline]
pub fn set_pull_none(&mut self) {
self.set_pull(Pull::None);
}
#[inline]
pub fn set_drive_strength(&mut self, strength: DriveStrength) {
self.pads_bank().[<gpio $n>].write_drive(strength as u32);
}
#[inline]
pub fn set_slew_rate(&mut self, rate: SlewRate) {
let pad = &self.pads_bank().[<gpio $n>];
match rate {
SlewRate::Slow => pad.clr_slewfast(),
SlewRate::Fast => pad.set_slewfast(),
}
}
#[inline]
pub fn set_schmitt(&mut self, enable: bool) {
let pad = &self.pads_bank().[<gpio $n>];
if enable {
pad.set_schmitt();
} else {
pad.clr_schmitt();
}
}
#[inline]
pub fn enable_input(&mut self) {
self.pads_bank().[<gpio $n>].set_ie();
}
#[inline]
pub fn disable_input(&mut self) {
self.pads_bank().[<gpio $n>].clr_ie();
}
#[inline]
pub fn set_output_disable(&mut self, disable: bool) {
let pad = &self.pads_bank().[<gpio $n>];
if disable {
pad.set_od();
} else {
pad.clr_od();
}
}
#[inline]
pub fn set_output_override(&mut self, ov: OutputOverride) {
self.io_bank().[<gpio $n _ctrl>].write_outover(ov as u32);
}
#[inline]
pub fn set_output_enable_override(&mut self, ov: OutputEnableOverride) {
self.io_bank().[<gpio $n _ctrl>].write_oeover(ov as u32);
}
#[inline]
pub fn set_input_override(&mut self, ov: InputOverride) {
self.io_bank().[<gpio $n _ctrl>].write_inover(ov as u32);
}
#[inline]
pub fn set_irq_override(&mut self, ov: IrqOverride) {
self.io_bank().[<gpio $n _ctrl>].write_irqover(ov as u32);
}
#[inline]
pub fn set_direction_out(&mut self) {
self.sio().gpio_hi_oe_set.write_gpio(Self::MASK);
}
#[inline]
pub fn set_direction_in(&mut self) {
self.sio().gpio_hi_oe_clr.write_gpio(Self::MASK);
}
#[inline]
pub fn is_output(&self) -> bool {
(self.sio().gpio_hi_oe.gpio() & Self::MASK) != 0
}
#[inline]
pub fn get(&self) -> bool {
(self.sio().gpio_hi_in.gpio() & Self::MASK) != 0
}
#[inline]
pub fn set_high(&self) {
self.sio().gpio_hi_out_set.write_gpio(Self::MASK);
}
#[inline]
pub fn set_low(&self) {
self.sio().gpio_hi_out_clr.write_gpio(Self::MASK);
}
#[inline]
pub fn toggle(&self) {
self.sio().gpio_hi_out_xor.write_gpio(Self::MASK);
}
#[inline]
pub fn set(&self, high: bool) {
if high {
self.set_high();
} else {
self.set_low();
}
}
#[inline]
pub fn output_level(&self) -> bool {
(self.sio().gpio_hi_out.gpio() & Self::MASK) != 0
}
#[inline]
pub fn into_output(&mut self) -> &Self {
self.init_pad();
self.set_function_sio();
self.set_direction_out();
self
}
#[inline]
pub fn into_input(&mut self) -> &Self {
self.init_pad();
self.set_function_sio();
self.set_direction_in();
self
}
#[inline]
pub fn into_input_pullup(&mut self) -> &Self {
self.into_input();
self.set_pull_up();
self
}
#[inline]
pub fn into_input_pulldown(&mut self) -> &Self {
self.into_input();
self.set_pull_down();
self
}
#[inline]
pub fn into_pio0(&mut self) -> &Self {
self.init_pad();
self.set_function_pio0();
self
}
#[inline]
pub fn into_pio1(&mut self) -> &Self {
self.init_pad();
self.set_function_pio1();
self
}
#[inline]
pub fn into_pio2(&mut self) -> &Self {
self.init_pad();
self.set_function_pio2();
self
}
#[inline]
pub fn into_pwm(&mut self) -> &Self {
self.init_pad();
self.set_function(Function::Pwm);
self
}
#[inline]
pub fn into_i2c(&mut self) -> &Self {
self.init_pad();
self.set_function(Function::I2c);
self.set_pull_up();
self
}
#[inline]
pub fn into_spi(&mut self) -> &Self {
self.init_pad();
self.set_function(Function::Spi);
self
}
#[inline]
pub fn into_uart(&mut self) -> &Self {
self.init_pad();
self.set_function(Function::Uart);
self
}
#[inline]
pub fn enable_interrupt(&self, edge: EdgeLevel) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
match REG_IDX {
4 => io.proc0_inte4.set_bits(bit),
5 => io.proc0_inte5.set_bits(bit),
_ => {}
}
}
#[inline]
pub fn disable_interrupt(&self, edge: EdgeLevel) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
match REG_IDX {
4 => io.proc0_inte4.clr_bits(bit),
5 => io.proc0_inte5.clr_bits(bit),
_ => {}
}
}
#[inline]
pub fn interrupt_status(&self, edge: EdgeLevel) -> bool {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
let status = match REG_IDX {
4 => io.proc0_ints4.read_bits(),
5 => io.proc0_ints5.read_bits(),
_ => 0,
};
(status & bit) != 0
}
#[inline]
pub fn clear_interrupt(&self, edge: EdgeLevel) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
match REG_IDX {
4 => io.intr4.write_bits(bit),
5 => io.intr5.write_bits(bit),
_ => {}
}
}
#[inline]
pub fn disable_all_interrupts(&self) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
const MASK: u32 = 0xF << BIT_OFFSET;
let io = self.io_bank();
match REG_IDX {
4 => io.proc0_inte4.clr_bits(MASK),
5 => io.proc0_inte5.clr_bits(MASK),
_ => {}
}
}
#[inline]
pub fn pending_flags(&self) -> u32 {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let io = self.io_bank();
let raw = match REG_IDX {
4 => io.proc0_ints4.read_bits(),
5 => io.proc0_ints5.read_bits(),
_ => 0,
};
(raw >> BIT_OFFSET) & 0xF
}
#[inline]
pub fn clear_all_interrupts(&self) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
const MASK: u32 = 0xF << BIT_OFFSET;
let io = self.io_bank();
match REG_IDX {
4 => io.intr4.write_bits(MASK),
5 => io.intr5.write_bits(MASK),
_ => {}
}
}
#[inline]
pub fn check_and_clear(&self, edge: EdgeLevel) -> bool {
if self.interrupt_status(edge) {
self.clear_interrupt(edge);
true
} else {
false
}
}
#[inline]
pub fn set_dormant_wake(&self, edge: EdgeLevel) {
const REG_IDX: usize = $n / 8; const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
let bit = match edge {
EdgeLevel::LevelLow => 1 << (BIT_OFFSET + 0),
EdgeLevel::LevelHigh => 1 << (BIT_OFFSET + 1),
EdgeLevel::EdgeLow => 1 << (BIT_OFFSET + 2),
EdgeLevel::EdgeHigh => 1 << (BIT_OFFSET + 3),
};
let io = self.io_bank();
match REG_IDX {
4 => io.dormant_wake_inte4.set_bits(bit),
5 => io.dormant_wake_inte5.set_bits(bit),
_ => {}
}
}
#[inline]
pub fn clear_dormant_wake(&self) {
const REG_IDX: usize = $n / 8;
const BIT_OFFSET: u32 = (($n % 8) * 4) as u32;
const MASK: u32 = 0xF << BIT_OFFSET;
let io = self.io_bank();
match REG_IDX {
4 => io.dormant_wake_inte4.clr_bits(MASK),
5 => io.dormant_wake_inte5.clr_bits(MASK),
_ => {}
}
}
}
impl core::fmt::Debug for Pin<$n> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Pin")
.field("number", &$n)
.field("function", &self.function())
.field("is_output", &self.is_output())
.finish()
}
}
}
};
}
pub struct Pin<const N: u32> {
_marker: core::marker::PhantomData<*const ()>,
}
unsafe impl<const N: u32> Send for Pin<N> {}
impl_pin!(0, low);
impl_pin!(1, low);
impl_pin!(2, low);
impl_pin!(3, low);
impl_pin!(4, low);
impl_pin!(5, low);
impl_pin!(6, low);
impl_pin!(7, low);
impl_pin!(8, low);
impl_pin!(9, low);
impl_pin!(10, low);
impl_pin!(11, low);
impl_pin!(12, low);
impl_pin!(13, low);
impl_pin!(14, low);
impl_pin!(15, low);
impl_pin!(16, low);
impl_pin!(17, low);
impl_pin!(18, low);
impl_pin!(19, low);
impl_pin!(20, low);
impl_pin!(21, low);
impl_pin!(22, low);
impl_pin!(23, low);
impl_pin!(24, low);
impl_pin!(25, low);
impl_pin!(26, low);
impl_pin!(27, low);
impl_pin!(28, low);
impl_pin!(29, low);
#[cfg(feature = "rp2350b")]
impl_pin!(30, low);
#[cfg(feature = "rp2350b")]
impl_pin!(31, low);
#[cfg(feature = "rp2350b")]
impl_pin!(32, high);
#[cfg(feature = "rp2350b")]
impl_pin!(33, high);
#[cfg(feature = "rp2350b")]
impl_pin!(34, high);
#[cfg(feature = "rp2350b")]
impl_pin!(35, high);
#[cfg(feature = "rp2350b")]
impl_pin!(36, high);
#[cfg(feature = "rp2350b")]
impl_pin!(37, high);
#[cfg(feature = "rp2350b")]
impl_pin!(38, high);
#[cfg(feature = "rp2350b")]
impl_pin!(39, high);
#[cfg(feature = "rp2350b")]
impl_pin!(40, high);
#[cfg(feature = "rp2350b")]
impl_pin!(41, high);
#[cfg(feature = "rp2350b")]
impl_pin!(42, high);
#[cfg(feature = "rp2350b")]
impl_pin!(43, high);
#[cfg(feature = "rp2350b")]
impl_pin!(44, high);
#[cfg(feature = "rp2350b")]
impl_pin!(45, high);
#[cfg(feature = "rp2350b")]
impl_pin!(46, high);
#[cfg(feature = "rp2350b")]
impl_pin!(47, high);
pub struct Pins {
_io_bank0: IO_BANK0,
_pads_bank0: PADS_BANK0,
}
impl Pins {
#[inline]
pub fn init(io_bank0: IO_BANK0, pads_bank0: PADS_BANK0) -> Self {
let mask = rp2350_pac::IO_BANK0::RESET_MASK | rp2350_pac::PADS_BANK0::RESET_MASK;
unsafe {
reset::reset(mask);
reset::unreset(mask);
}
Self {
_io_bank0: io_bank0,
_pads_bank0: pads_bank0,
}
}
#[inline]
pub fn pin<const N: u32>(&self) -> Pin<N> {
#[cfg(not(feature = "rp2350b"))]
const {
assert!(N < 30, "GPIO pin number must be 0-29 (RP2350A)")
};
#[cfg(feature = "rp2350b")]
const {
assert!(N < 48, "GPIO pin number must be 0-47 (RP2350B)")
};
Pin {
_marker: core::marker::PhantomData,
}
}
}
pub mod pins;
pub use pins::{
ErasedPin,
I2c0SclPin,
I2c0SdaPin,
I2c1SclPin,
I2c1SdaPin,
Pio0Pin,
Pio1Pin,
Pio2Pin,
PwmAPin,
PwmBPin,
Spi0CsnPin,
Spi0RxPin,
Spi0SckPin,
Spi0TxPin,
Spi1CsnPin,
Spi1RxPin,
Spi1SckPin,
Spi1TxPin,
Uart0CtsPin,
Uart0RtsPin,
Uart0RxPin,
Uart0TxPin,
Uart1CtsPin,
Uart1RtsPin,
Uart1RxPin,
Uart1TxPin,
};
#[cfg(feature = "rp2350a")]
pub const GPIO_PIN_MAX: u32 = 29;
#[cfg(feature = "rp2350b")]
pub const GPIO_PIN_MAX: u32 = 47;
#[inline(always)]
pub(crate) fn assert_pin(pin: u32) {
debug_assert!(pin <= GPIO_PIN_MAX);
}
#[inline]
pub unsafe fn get(pin: u32) -> bool {
assert_pin(pin);
if pin < 32 {
(unsafe { &*crate::sio::REGS }.gpio_in.gpio_in() & (1 << pin)) != 0
} else {
(unsafe { &*crate::sio::REGS }.gpio_hi_in.gpio() & (1 << (pin - 32))) != 0
}
}
#[inline]
pub unsafe fn set_high(pin: u32) {
assert_pin(pin);
if pin < 32 {
unsafe { &*crate::sio::REGS }
.gpio_out_set
.write_gpio_out_set(1 << pin);
} else {
unsafe { &*crate::sio::REGS }
.gpio_hi_out_set
.write_gpio(1 << (pin - 32));
}
}
#[inline]
pub unsafe fn set_low(pin: u32) {
assert_pin(pin);
if pin < 32 {
unsafe { &*crate::sio::REGS }
.gpio_out_clr
.write_gpio_out_clr(1 << pin);
} else {
unsafe { &*crate::sio::REGS }
.gpio_hi_out_clr
.write_gpio(1 << (pin - 32));
}
}
#[inline]
pub unsafe fn toggle(pin: u32) {
assert_pin(pin);
if pin < 32 {
unsafe { &*crate::sio::REGS }
.gpio_out_xor
.write_gpio_out_xor(1 << pin);
} else {
unsafe { &*crate::sio::REGS }
.gpio_hi_out_xor
.write_gpio(1 << (pin - 32));
}
}
#[inline]
pub unsafe fn set(pin: u32, high: bool) {
if high {
unsafe { set_high(pin) };
} else {
unsafe { set_low(pin) };
}
}
#[inline]
pub unsafe fn output(pin: u32) -> bool {
assert_pin(pin);
if pin < 32 {
(unsafe { &*crate::sio::REGS }.gpio_out.gpio_out() & (1 << pin)) != 0
} else {
(unsafe { &*crate::sio::REGS }.gpio_hi_out.gpio() & (1 << (pin - 32))) != 0
}
}
#[inline]
pub unsafe fn enable_output(pin: u32) {
assert_pin(pin);
if pin < 32 {
unsafe { &*crate::sio::REGS }
.gpio_oe_set
.write_gpio_oe_set(1 << pin);
} else {
unsafe { &*crate::sio::REGS }
.gpio_hi_oe_set
.write_gpio(1 << (pin - 32));
}
}
#[inline]
pub unsafe fn disable_output(pin: u32) {
assert_pin(pin);
if pin < 32 {
unsafe { &*crate::sio::REGS }
.gpio_oe_clr
.write_gpio_oe_clr(1 << pin);
} else {
unsafe { &*crate::sio::REGS }
.gpio_hi_oe_clr
.write_gpio(1 << (pin - 32));
}
}
#[inline]
pub unsafe fn set_output_enable(pin: u32, output: bool) {
unsafe {
if output {
enable_output(pin);
} else {
disable_output(pin);
}
}
}
#[inline]
pub unsafe fn toggle_output_enable(pin: u32) {
assert_pin(pin);
if pin < 32 {
unsafe { &*crate::sio::REGS }
.gpio_oe_xor
.write_gpio_oe_xor(1 << pin);
} else {
unsafe { &*crate::sio::REGS }
.gpio_hi_oe_xor
.write_gpio(1 << (pin - 32));
}
}
#[inline]
pub unsafe fn is_output(pin: u32) -> bool {
assert_pin(pin);
if pin < 32 {
(unsafe { &*crate::sio::REGS }.gpio_oe.gpio_oe() & (1 << pin)) != 0
} else {
(unsafe { &*crate::sio::REGS }.gpio_hi_oe.gpio() & (1 << (pin - 32))) != 0
}
}
#[inline]
pub unsafe fn mask_input_low() -> u32 {
unsafe { &*crate::sio::REGS }.gpio_in.gpio_in()
}
#[inline]
pub unsafe fn mask_output_low() -> u32 {
unsafe { &*crate::sio::REGS }.gpio_out.gpio_out()
}
#[inline]
pub unsafe fn mask_write_output_low(value: u32) {
unsafe { &*crate::sio::REGS }.gpio_out.write_gpio_out(value);
}
#[inline]
pub unsafe fn mask_set_low(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_out_set
.write_gpio_out_set(mask);
}
#[inline]
pub unsafe fn mask_clr_lo(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_out_clr
.write_gpio_out_clr(mask);
}
#[inline]
pub unsafe fn mask_xor_lo(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_out_xor
.write_gpio_out_xor(mask);
}
#[inline]
pub unsafe fn mask_output_enable_lo() -> u32 {
unsafe { &*crate::sio::REGS }.gpio_oe.gpio_oe()
}
#[inline]
pub unsafe fn mask_write_output_enable_lo(value: u32) {
unsafe { &*crate::sio::REGS }.gpio_oe.write_gpio_oe(value);
}
#[inline]
pub unsafe fn mask_set_output_enable_lo(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_oe_set
.write_gpio_oe_set(mask);
}
#[inline]
pub unsafe fn mask_clear_output_enable_lo(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_oe_clr
.write_gpio_oe_clr(mask);
}
#[inline]
pub unsafe fn mask_toggle_output_enable_lo(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_oe_xor
.write_gpio_oe_xor(mask);
}
#[inline]
pub unsafe fn mask_input_hi() -> u32 {
unsafe { &*crate::sio::REGS }.gpio_hi_in.gpio()
}
#[inline]
pub unsafe fn mask_output_hi() -> u32 {
unsafe { &*crate::sio::REGS }.gpio_hi_out.gpio()
}
#[inline]
pub unsafe fn mask_write_output_hi(value: u32) {
unsafe { &*crate::sio::REGS }.gpio_hi_out.write_gpio(value);
}
#[inline]
pub unsafe fn mask_set_hi(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_hi_out_set
.write_gpio(mask);
}
#[inline]
pub unsafe fn mask_clr_hi(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_hi_out_clr
.write_gpio(mask);
}
#[inline]
pub unsafe fn mask_xor_hi(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_hi_out_xor
.write_gpio(mask);
}
#[inline]
pub unsafe fn mask_output_enable_hi() -> u32 {
unsafe { &*crate::sio::REGS }.gpio_hi_oe.gpio()
}
#[inline]
pub unsafe fn mask_write_output_enable_hi(value: u32) {
unsafe { &*crate::sio::REGS }.gpio_hi_oe.write_gpio(value);
}
#[inline]
pub unsafe fn mask_set_output_enable_hi(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_hi_oe_set
.write_gpio(mask);
}
#[inline]
pub unsafe fn mask_clear_output_enable_hi(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_hi_oe_clr
.write_gpio(mask);
}
#[inline]
pub unsafe fn mask_toggle_output_enable_hi(mask: u32) {
unsafe { &*crate::sio::REGS }
.gpio_hi_oe_xor
.write_gpio(mask);
}
#[inline]
pub unsafe fn out64_put(value: u64) {
unsafe {
mask_write_output_low(value as u32);
mask_write_output_hi((value >> 32) as u32);
}
}
#[inline]
pub unsafe fn out64_xor(mask: u64) {
unsafe {
mask_xor_lo(mask as u32);
mask_xor_hi((mask >> 32) as u32);
}
}
#[inline]
pub unsafe fn out64_set(mask: u64) {
unsafe {
mask_set_low(mask as u32);
mask_set_hi((mask >> 32) as u32);
}
}
#[inline]
pub unsafe fn out64_clr(mask: u64) {
unsafe {
mask_clr_lo(mask as u32);
mask_clr_hi((mask >> 32) as u32);
}
}
#[inline]
pub unsafe fn out64_get() -> u64 {
unsafe {
let lo = mask_output_low() as u64;
let hi = (mask_output_hi() as u64) << 32;
lo | hi
}
}
#[inline]
pub unsafe fn output_enable64_put(value: u64) {
unsafe {
mask_write_output_enable_lo(value as u32);
mask_write_output_enable_hi((value >> 32) as u32);
}
}
#[inline]
pub unsafe fn output_enable64_xor(mask: u64) {
unsafe {
mask_toggle_output_enable_lo(mask as u32);
mask_toggle_output_enable_hi((mask >> 32) as u32);
}
}
#[inline]
pub unsafe fn output_enable64_set(mask: u64) {
unsafe {
mask_set_output_enable_lo(mask as u32);
mask_set_output_enable_hi((mask >> 32) as u32);
}
}
#[inline]
pub unsafe fn output_enable64_clr(mask: u64) {
unsafe {
mask_clear_output_enable_lo(mask as u32);
mask_clear_output_enable_hi((mask >> 32) as u32);
}
}
#[inline]
pub unsafe fn output_enable64_get() -> u64 {
unsafe {
let lo = mask_output_enable_lo() as u64;
let hi = (mask_output_enable_hi() as u64) << 32;
lo | hi
}
}
#[inline]
pub unsafe fn in64() -> u64 {
unsafe {
let lo = mask_input_low() as u64;
let hi = (mask_input_hi() as u64) << 32;
lo | hi
}
}
#[inline]
pub unsafe fn in_pin_lo(pin: u32) -> bool {
assert_pin(pin);
unsafe { (mask_input_low() & (1 << pin)) != 0 }
}
#[inline]
pub unsafe fn in_pin_hi(pin: u32) -> bool {
assert_pin(pin);
unsafe { (mask_input_hi() & (1 << (pin - 32))) != 0 }
}