mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +00:00
rp/gpio: add Flex.
This commit is contained in:
parent
d2a622b3d0
commit
5cc5961c94
@ -30,187 +30,228 @@ pub enum Bank {
|
||||
}
|
||||
|
||||
pub struct Input<'d, T: Pin> {
|
||||
pin: T,
|
||||
phantom: PhantomData<&'d mut T>,
|
||||
pin: Flex<'d, T>,
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Input<'d, T> {
|
||||
pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self {
|
||||
let mut pin = Flex::new(pin);
|
||||
pin.set_as_input();
|
||||
pin.set_pull(pull);
|
||||
Self { pin }
|
||||
}
|
||||
|
||||
pub fn is_high(&self) -> bool {
|
||||
self.pin.is_high()
|
||||
}
|
||||
|
||||
pub fn is_low(&self) -> bool {
|
||||
self.pin.is_low()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Output<'d, T: Pin> {
|
||||
pin: Flex<'d, T>,
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Output<'d, T> {
|
||||
#[inline]
|
||||
pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level) -> Self {
|
||||
let mut pin = Flex::new(pin);
|
||||
match initial_output {
|
||||
Level::High => pin.set_high(),
|
||||
Level::Low => pin.set_low(),
|
||||
}
|
||||
|
||||
pin.set_as_output();
|
||||
Self { pin }
|
||||
}
|
||||
|
||||
/// Set the output as high.
|
||||
#[inline]
|
||||
pub fn set_high(&mut self) {
|
||||
self.pin.set_high()
|
||||
}
|
||||
|
||||
/// Set the output as low.
|
||||
#[inline]
|
||||
pub fn set_low(&mut self) {
|
||||
self.pin.set_low()
|
||||
}
|
||||
|
||||
/// Is the output pin set as high?
|
||||
#[inline]
|
||||
pub fn is_set_high(&self) -> bool {
|
||||
self.pin.is_set_high()
|
||||
}
|
||||
|
||||
/// Is the output pin set as low?
|
||||
#[inline]
|
||||
pub fn is_set_low(&self) -> bool {
|
||||
self.pin.is_set_low()
|
||||
}
|
||||
|
||||
/// Toggle pin output
|
||||
#[inline]
|
||||
pub fn toggle(&mut self) {
|
||||
self.pin.toggle()
|
||||
}
|
||||
}
|
||||
|
||||
/// GPIO output open-drain.
|
||||
pub struct OutputOpenDrain<'d, T: Pin> {
|
||||
pin: Flex<'d, T>,
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> OutputOpenDrain<'d, T> {
|
||||
#[inline]
|
||||
pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level) -> Self {
|
||||
let mut pin = Flex::new(pin);
|
||||
pin.set_low();
|
||||
match initial_output {
|
||||
Level::High => pin.set_as_input(),
|
||||
Level::Low => pin.set_as_output(),
|
||||
}
|
||||
Self { pin }
|
||||
}
|
||||
|
||||
/// Set the output as high.
|
||||
#[inline]
|
||||
pub fn set_high(&mut self) {
|
||||
// For Open Drain High, disable the output pin.
|
||||
self.pin.set_as_input()
|
||||
}
|
||||
|
||||
/// Set the output as low.
|
||||
#[inline]
|
||||
pub fn set_low(&mut self) {
|
||||
// For Open Drain Low, enable the output pin.
|
||||
self.pin.set_as_output()
|
||||
}
|
||||
|
||||
/// Is the output level high?
|
||||
#[inline]
|
||||
pub fn is_set_high(&self) -> bool {
|
||||
!self.is_set_low()
|
||||
}
|
||||
|
||||
/// Is the output level low?
|
||||
#[inline]
|
||||
pub fn is_set_low(&self) -> bool {
|
||||
self.pin.is_set_as_output()
|
||||
}
|
||||
|
||||
/// Toggle pin output
|
||||
#[inline]
|
||||
pub fn toggle(&mut self) {
|
||||
self.pin.toggle_set_as_output()
|
||||
}
|
||||
}
|
||||
|
||||
/// GPIO flexible pin.
|
||||
///
|
||||
/// This pin can be either an input or output pin. The output level register bit will remain
|
||||
/// set while not in output mode, so the pin's level will be 'remembered' when it is not in output
|
||||
/// mode.
|
||||
pub struct Flex<'d, T: Pin> {
|
||||
pin: T,
|
||||
phantom: PhantomData<&'d mut T>,
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Flex<'d, T> {
|
||||
#[inline]
|
||||
pub fn new(pin: impl Unborrow<Target = T> + 'd) -> Self {
|
||||
unborrow!(pin);
|
||||
|
||||
unsafe {
|
||||
pin.pad_ctrl().write(|w| {
|
||||
w.set_ie(true);
|
||||
});
|
||||
|
||||
pin.io().ctrl().write(|w| {
|
||||
w.set_funcsel(pac::io::vals::Gpio0CtrlFuncsel::SIO_0.0);
|
||||
});
|
||||
}
|
||||
|
||||
Self {
|
||||
pin,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn bit(&self) -> u32 {
|
||||
1 << self.pin.pin()
|
||||
}
|
||||
|
||||
/// Set the pin's pull.
|
||||
#[inline]
|
||||
pub fn set_pull(&mut self, pull: Pull) {
|
||||
unsafe {
|
||||
self.pin.pad_ctrl().write(|w| {
|
||||
w.set_ie(true);
|
||||
match pull {
|
||||
Pull::Up => w.set_pue(true),
|
||||
Pull::Down => w.set_pde(true),
|
||||
Pull::None => {}
|
||||
}
|
||||
});
|
||||
|
||||
// disable output in SIO, to use it as input
|
||||
pin.sio_oe().value_clr().write_value(1 << pin.pin());
|
||||
|
||||
pin.io().ctrl().write(|w| {
|
||||
w.set_funcsel(pac::io::vals::Gpio0CtrlFuncsel::SIO_0.0);
|
||||
});
|
||||
}
|
||||
|
||||
Self {
|
||||
pin,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Put the pin into input mode.
|
||||
///
|
||||
/// The pull setting is left unchanged.
|
||||
#[inline]
|
||||
pub fn set_as_input(&mut self) {
|
||||
unsafe { self.pin.sio_oe().value_clr().write_value(self.bit()) }
|
||||
}
|
||||
|
||||
/// Put the pin into output mode.
|
||||
///
|
||||
/// The pin level will be whatever was set before (or low by default). If you want it to begin
|
||||
/// at a specific level, call `set_high`/`set_low` on the pin first.
|
||||
#[inline]
|
||||
pub fn set_as_output(&mut self) {
|
||||
unsafe { self.pin.sio_oe().value_set().write_value(self.bit()) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_set_as_output(&self) -> bool {
|
||||
unsafe { (self.pin.sio_oe().value().read() & self.bit()) != 0 }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn toggle_set_as_output(&mut self) {
|
||||
unsafe { self.pin.sio_oe().value_xor().write_value(self.bit()) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_high(&self) -> bool {
|
||||
!self.is_low()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_low(&self) -> bool {
|
||||
let val = 1 << self.pin.pin();
|
||||
unsafe { self.pin.sio_in().read() & val == 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Drop for Input<'d, T> {
|
||||
fn drop(&mut self) {
|
||||
// todo
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Output<'d, T: Pin> {
|
||||
pin: T,
|
||||
phantom: PhantomData<&'d mut T>,
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Output<'d, T> {
|
||||
pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level) -> Self {
|
||||
unborrow!(pin);
|
||||
|
||||
unsafe {
|
||||
match initial_output {
|
||||
Level::High => pin.sio_out().value_set().write_value(1 << pin.pin()),
|
||||
Level::Low => pin.sio_out().value_clr().write_value(1 << pin.pin()),
|
||||
}
|
||||
pin.sio_oe().value_set().write_value(1 << pin.pin());
|
||||
|
||||
pin.io().ctrl().write(|w| {
|
||||
w.set_funcsel(pac::io::vals::Gpio0CtrlFuncsel::SIO_0.0);
|
||||
});
|
||||
}
|
||||
|
||||
Self {
|
||||
pin,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the output as high.
|
||||
pub fn set_high(&mut self) {
|
||||
let val = 1 << self.pin.pin();
|
||||
unsafe { self.pin.sio_out().value_set().write_value(val) };
|
||||
}
|
||||
|
||||
/// Set the output as low.
|
||||
pub fn set_low(&mut self) {
|
||||
let val = 1 << self.pin.pin();
|
||||
unsafe { self.pin.sio_out().value_clr().write_value(val) };
|
||||
}
|
||||
|
||||
/// Is the output pin set as high?
|
||||
pub fn is_set_high(&self) -> bool {
|
||||
!self.is_set_low()
|
||||
}
|
||||
|
||||
/// Is the output pin set as low?
|
||||
pub fn is_set_low(&self) -> bool {
|
||||
// Reading from SIO: GPIO_OUT gives the last value written.
|
||||
let val = 1 << self.pin.pin();
|
||||
unsafe { (self.pin.sio_out().value().read() & val) == 0 }
|
||||
}
|
||||
|
||||
/// Toggle pin output
|
||||
#[inline]
|
||||
pub fn toggle(&mut self) {
|
||||
let val = 1 << self.pin.pin();
|
||||
unsafe {
|
||||
self.pin.sio_out().value_xor().write_value(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Drop for Output<'d, T> {
|
||||
fn drop(&mut self) {
|
||||
let val = 1 << self.pin.pin();
|
||||
unsafe {
|
||||
self.pin.sio_out().value_clr().write_value(val);
|
||||
self.pin.sio_oe().value_clr().write_value(val);
|
||||
self.pin.io().ctrl().write(|w| {
|
||||
w.set_funcsel(pac::io::vals::Gpio0CtrlFuncsel::NULL.0);
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// GPIO output open-drain.
|
||||
pub struct OutputOpenDrain<'d, T: Pin> {
|
||||
pin: T,
|
||||
phantom: PhantomData<&'d mut T>,
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> OutputOpenDrain<'d, T> {
|
||||
#[inline]
|
||||
pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level) -> Self {
|
||||
unborrow!(pin);
|
||||
|
||||
unsafe {
|
||||
let val = 1 << pin.pin();
|
||||
pin.io().ctrl().write(|w| {
|
||||
w.set_funcsel(pac::io::vals::Gpio0CtrlFuncsel::SIO_0.0);
|
||||
});
|
||||
pin.sio_out().value_clr().write_value(val);
|
||||
|
||||
match initial_output {
|
||||
Level::High => {
|
||||
// For Open Drain High, disable the output pin.
|
||||
pin.sio_oe().value_clr().write_value(val);
|
||||
}
|
||||
Level::Low => {
|
||||
// For Open Drain Low, enable the output pin.
|
||||
pin.sio_oe().value_set().write_value(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
pin,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
unsafe { self.pin.sio_in().read() & self.bit() == 0 }
|
||||
}
|
||||
|
||||
/// Set the output as high.
|
||||
#[inline]
|
||||
pub fn set_high(&mut self) {
|
||||
// For Open Drain High, disable the output pin.
|
||||
unsafe {
|
||||
self.pin.sio_oe().value_clr().write_value(1 << self.pin.pin());
|
||||
}
|
||||
unsafe { self.pin.sio_out().value_set().write_value(self.bit()) }
|
||||
}
|
||||
|
||||
/// Set the output as low.
|
||||
#[inline]
|
||||
pub fn set_low(&mut self) {
|
||||
// For Open Drain Low, enable the output pin.
|
||||
unsafe {
|
||||
self.pin.sio_oe().value_set().write_value(1 << self.pin.pin());
|
||||
}
|
||||
unsafe { self.pin.sio_out().value_clr().write_value(self.bit()) }
|
||||
}
|
||||
|
||||
/// Is the output level high?
|
||||
#[inline]
|
||||
pub fn is_set_high(&self) -> bool {
|
||||
let val = 1 << self.pin.pin();
|
||||
unsafe { (self.pin.sio_oe().value().read() & val) == 0 }
|
||||
unsafe { (self.pin.sio_out().value().read() & self.bit()) == 0 }
|
||||
}
|
||||
|
||||
/// Is the output level low?
|
||||
@ -222,9 +263,18 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
|
||||
/// Toggle pin output
|
||||
#[inline]
|
||||
pub fn toggle(&mut self) {
|
||||
let val = 1 << self.pin.pin();
|
||||
unsafe { self.pin.sio_out().value_xor().write_value(self.bit()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Drop for Flex<'d, T> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
self.pin.sio_out().value_xor().write_value(val);
|
||||
self.pin.pad_ctrl().write(|_| {});
|
||||
self.pin.io().ctrl().write(|w| {
|
||||
w.set_funcsel(pac::io::vals::Gpio0CtrlFuncsel::NULL.0);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -428,6 +478,48 @@ mod eh02 {
|
||||
Ok(self.toggle())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_02::digital::v2::InputPin for Flex<'d, T> {
|
||||
type Error = Infallible;
|
||||
|
||||
fn is_high(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_high())
|
||||
}
|
||||
|
||||
fn is_low(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_02::digital::v2::OutputPin for Flex<'d, T> {
|
||||
type Error = Infallible;
|
||||
|
||||
fn set_high(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.set_high())
|
||||
}
|
||||
|
||||
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.set_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_02::digital::v2::StatefulOutputPin for Flex<'d, T> {
|
||||
fn is_set_high(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_set_high())
|
||||
}
|
||||
|
||||
fn is_set_low(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_set_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_02::digital::v2::ToggleableOutputPin for Flex<'d, T> {
|
||||
type Error = Infallible;
|
||||
#[inline]
|
||||
fn toggle(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.toggle())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable-traits")]
|
||||
@ -471,4 +563,80 @@ mod eh1 {
|
||||
Ok(self.is_set_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::blocking::ToggleableOutputPin for Output<'d, T> {
|
||||
fn toggle(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.toggle())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::ErrorType for OutputOpenDrain<'d, T> {
|
||||
type Error = Infallible;
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::blocking::OutputPin for OutputOpenDrain<'d, T> {
|
||||
fn set_high(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.set_high())
|
||||
}
|
||||
|
||||
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.set_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::blocking::StatefulOutputPin for OutputOpenDrain<'d, T> {
|
||||
fn is_set_high(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_set_high())
|
||||
}
|
||||
|
||||
fn is_set_low(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_set_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::blocking::ToggleableOutputPin for OutputOpenDrain<'d, T> {
|
||||
fn toggle(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.toggle())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::ErrorType for Flex<'d, T> {
|
||||
type Error = Infallible;
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::blocking::InputPin for Flex<'d, T> {
|
||||
fn is_high(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_high())
|
||||
}
|
||||
|
||||
fn is_low(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::blocking::OutputPin for Flex<'d, T> {
|
||||
fn set_high(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.set_high())
|
||||
}
|
||||
|
||||
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.set_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::blocking::StatefulOutputPin for Flex<'d, T> {
|
||||
fn is_set_high(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_set_high())
|
||||
}
|
||||
|
||||
fn is_set_low(&self) -> Result<bool, Self::Error> {
|
||||
Ok(self.is_set_low())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> embedded_hal_1::digital::blocking::ToggleableOutputPin for Flex<'d, T> {
|
||||
fn toggle(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(self.toggle())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user