use crate::peripherals::UlpGpio;
use core::marker::PhantomData;
pub struct Input;
pub struct Output;
pub struct UlpGpioPin<'d, MODE> {
bit: u8,
_mode: PhantomData<&'d MODE>,
}
fn regs() -> &'static ws63_pac::gpio0::RegisterBlock {
unsafe { &*UlpGpio::ptr() }
}
impl<MODE> UlpGpioPin<'_, MODE> {
pub fn number(&self) -> u8 {
107 + self.bit
}
}
impl UlpGpioPin<'_, Output> {
pub fn set_high(&mut self) {
unsafe { regs().gpio_data_set().write(|w| w.bits(1 << self.bit)) };
}
pub fn set_low(&mut self) {
unsafe { regs().gpio_data_clr().write(|w| w.bits(1 << self.bit)) };
}
pub fn toggle(&mut self) {
let val = regs().gpio_sw_out().read().bits();
if val & (1 << self.bit) != 0 {
unsafe { regs().gpio_data_clr().write(|w| w.bits(1 << self.bit)) };
} else {
unsafe { regs().gpio_data_set().write(|w| w.bits(1 << self.bit)) };
}
}
pub fn is_set_high(&self) -> bool {
(regs().gpio_sw_out().read().bits() >> self.bit) & 1 != 0
}
pub fn into_input(self) -> UlpGpioPin<'static, Input> {
regs().gpio_sw_oen().modify(|r, w| unsafe { w.bits(r.bits() | (1 << self.bit)) });
UlpGpioPin { bit: self.bit, _mode: PhantomData }
}
}
impl UlpGpioPin<'_, Input> {
pub fn is_high(&self) -> bool {
(regs().gpio_sw_out().read().bits() >> self.bit) & 1 != 0
}
pub fn is_low(&self) -> bool {
!self.is_high()
}
pub fn enable_interrupt(&self) {
regs().gpio_int_en().modify(|r, w| unsafe { w.bits(r.bits() | (1 << self.bit)) });
}
pub fn disable_interrupt(&self) {
regs().gpio_int_en().modify(|r, w| unsafe { w.bits(r.bits() & !(1 << self.bit)) });
}
pub fn clear_interrupt(&self) {
unsafe { regs().gpio_int_eoi().write(|w| w.bits(1 << self.bit)) };
}
pub fn interrupt_pending(&self) -> bool {
(regs().gpio_int_raw().read().bits() >> self.bit) & 1 != 0
}
pub fn into_output(self) -> UlpGpioPin<'static, Output> {
regs().gpio_sw_oen().modify(|r, w| unsafe { w.bits(r.bits() & !(1 << self.bit)) });
UlpGpioPin { bit: self.bit, _mode: PhantomData }
}
}
impl embedded_hal::digital::ErrorType for UlpGpioPin<'_, Output> {
type Error = core::convert::Infallible;
}
impl embedded_hal::digital::OutputPin for UlpGpioPin<'_, Output> {
fn set_low(&mut self) -> Result<(), Self::Error> {
UlpGpioPin::set_low(self);
Ok(())
}
fn set_high(&mut self) -> Result<(), Self::Error> {
UlpGpioPin::set_high(self);
Ok(())
}
}
impl embedded_hal::digital::StatefulOutputPin for UlpGpioPin<'_, Output> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(UlpGpioPin::is_set_high(self))
}
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(!UlpGpioPin::is_set_high(self))
}
}
impl embedded_hal::digital::ErrorType for UlpGpioPin<'_, Input> {
type Error = core::convert::Infallible;
}
impl embedded_hal::digital::InputPin for UlpGpioPin<'_, Input> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(UlpGpioPin::is_high(self))
}
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(!UlpGpioPin::is_high(self))
}
}
pub fn create_input_pin(pin: u8) -> UlpGpioPin<'static, Input> {
let bit = pin - 107;
assert!(bit < 8, "ULP GPIO pin must be 107-114");
regs().gpio_sw_oen().modify(|r, w| unsafe { w.bits(r.bits() | (1 << bit)) });
UlpGpioPin { bit, _mode: PhantomData }
}
pub fn create_output_pin(pin: u8) -> UlpGpioPin<'static, Output> {
let bit = pin - 107;
assert!(bit < 8, "ULP GPIO pin must be 107-114");
regs().gpio_sw_oen().modify(|r, w| unsafe { w.bits(r.bits() & !(1 << bit)) });
UlpGpioPin { bit, _mode: PhantomData }
}