use crate::traits::wg::digital::v2::{toggleable, InputPin, OutputPin, StatefulOutputPin};
use crate::typestates::{
pin::{
gpio::{direction, Level},
state, PinId,
},
reg_proxy::RegClusterProxy,
};
use super::Pin;
use crate::{
raw::gpio::{
CLR,
DIRCLR,
DIRSET,
PIN,
SET,
},
reg_cluster,
};
reg_cluster!(DIRSET, DIRSET, raw::GPIO, dirset);
reg_cluster!(DIRCLR, DIRCLR, raw::GPIO, dirclr);
reg_cluster!(PIN, PIN, raw::GPIO, pin);
reg_cluster!(SET, SET, raw::GPIO, set);
reg_cluster!(CLR, CLR, raw::GPIO, clr);
impl<T> OutputPin for Pin<T, state::Gpio<direction::Output>>
where
T: PinId,
{
type Error = core::convert::Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
self.state.set[T::PORT].write(|w| unsafe { w.setp().bits(T::MASK) });
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.state.clr[T::PORT].write(|w| unsafe { w.clrp().bits(T::MASK) });
Ok(())
}
}
impl<T> StatefulOutputPin for Pin<T, state::Gpio<direction::Output>>
where
T: PinId,
{
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.state.pin[T::PORT].read().port().bits() & T::MASK == T::MASK)
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(!self.state.pin[T::PORT].read().port().bits() & T::MASK == T::MASK)
}
}
impl<T: PinId> toggleable::Default for Pin<T, state::Gpio<direction::Output>> {}
impl<T> InputPin for Pin<T, state::Gpio<direction::Input>>
where
T: PinId,
{
type Error = core::convert::Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.state.pin[T::PORT].read().port().bits() & T::MASK == T::MASK)
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(!self.state.pin[T::PORT].read().port().bits() & T::MASK == T::MASK)
}
}
impl<T, D> Pin<T, state::Gpio<D>>
where
T: PinId,
D: direction::NotOutput,
{
pub fn into_output_high(self) -> Pin<T, state::Gpio<direction::Output>> {
self.into_output(Level::High)
}
pub fn into_output_low(self) -> Pin<T, state::Gpio<direction::Output>> {
self.into_output(Level::Low)
}
pub fn into_output(self, initial: Level) -> Pin<T, state::Gpio<direction::Output>> {
match initial {
Level::High => self.state.set[T::PORT].write(|w| unsafe { w.setp().bits(T::MASK) }),
Level::Low => self.state.clr[T::PORT].write(|w| unsafe { w.clrp().bits(T::MASK) }),
}
self.state.dirset[T::PORT].write(|w| unsafe { w.dirsetp().bits(T::MASK) });
Pin {
id: self.id,
state: state::Gpio {
dirset: RegClusterProxy::new(),
dirclr: RegClusterProxy::new(),
pin: RegClusterProxy::new(),
set: RegClusterProxy::new(),
clr: RegClusterProxy::new(),
_direction: direction::Output,
},
}
}
}
impl<T, D> Pin<T, state::Gpio<D>>
where
T: PinId,
D: direction::NotInput,
{
pub fn into_input(self) -> Pin<T, state::Gpio<direction::Input>> {
self.state.dirclr[T::PORT].write(|w| unsafe { w.dirclrp().bits(T::MASK) });
Pin {
id: self.id,
state: state::Gpio {
dirset: RegClusterProxy::new(),
dirclr: RegClusterProxy::new(),
pin: RegClusterProxy::new(),
set: RegClusterProxy::new(),
clr: RegClusterProxy::new(),
_direction: direction::Input,
},
}
}
}
impl<T, D> Pin<T, state::Analog<D>>
where
T: PinId,
D: direction::NotInput,
{
pub fn into_input(self) -> Pin<T, state::Analog<direction::Input>> {
self.state.dirclr[T::PORT].write(|w| unsafe { w.dirclrp().bits(T::MASK) });
Pin {
id: self.id,
state: state::Analog {
channel: self.state.channel,
dirclr: RegClusterProxy::new(),
_direction: direction::Input,
},
}
}
}