#![macro_use]
use core::convert::Infallible;
use core::hint::unreachable_unchecked;
use cfg_if::cfg_if;
use embassy_hal_internal::{Peri, PeripheralType, impl_peripheral};
use crate::pac;
use crate::pac::common::{RW, Reg};
use crate::pac::gpio;
use crate::pac::gpio::vals;
#[cfg(not(feature = "_nrf51"))]
use crate::pac::shared::{regs::Psel, vals::Connect};
#[derive(Debug, Eq, PartialEq)]
pub enum Port {
Port0,
#[cfg(feature = "_gpio-p1")]
Port1,
#[cfg(feature = "_gpio-p2")]
Port2,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Pull {
None,
Up,
Down,
}
pub struct Input<'d> {
pub(crate) pin: Flex<'d>,
}
impl<'d> Input<'d> {
#[inline]
pub fn new(pin: Peri<'d, impl Pin>, pull: Pull) -> Self {
let mut pin = Flex::new(pin);
pin.set_as_input(pull);
Self { pin }
}
#[inline]
pub fn is_high(&self) -> bool {
self.pin.is_high()
}
#[inline]
pub fn is_low(&self) -> bool {
self.pin.is_low()
}
#[inline]
pub fn get_level(&self) -> Level {
self.pin.get_level()
}
}
impl Input<'static> {
pub fn persist(self) {
self.pin.persist()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Level {
Low,
High,
}
impl From<bool> for Level {
fn from(val: bool) -> Self {
match val {
true => Self::High,
false => Self::Low,
}
}
}
impl From<Level> for bool {
fn from(level: Level) -> bool {
match level {
Level::Low => false,
Level::High => true,
}
}
}
#[cfg(feature = "_nrf54l")]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum LevelDrive {
Disconnect = 2,
Standard = 0,
High = 1,
ExtraHigh = 3,
}
#[cfg(feature = "_nrf54l")]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct OutputDrive {
low: LevelDrive,
high: LevelDrive,
}
#[cfg(feature = "_nrf54l")]
#[allow(non_upper_case_globals)]
impl OutputDrive {
pub const Standard: Self = Self {
low: LevelDrive::Standard,
high: LevelDrive::Standard,
};
pub const HighDrive0Standard1: Self = Self {
low: LevelDrive::High,
high: LevelDrive::Standard,
};
pub const Standard0HighDrive1: Self = Self {
low: LevelDrive::Standard,
high: LevelDrive::High,
};
pub const HighDrive: Self = Self {
low: LevelDrive::High,
high: LevelDrive::High,
};
pub const Disconnect0Standard1: Self = Self {
low: LevelDrive::Disconnect,
high: LevelDrive::Standard,
};
pub const Disconnect0HighDrive1: Self = Self {
low: LevelDrive::Disconnect,
high: LevelDrive::High,
};
pub const Standard0Disconnect1: Self = Self {
low: LevelDrive::Standard,
high: LevelDrive::Disconnect,
};
pub const HighDrive0Disconnect1: Self = Self {
low: LevelDrive::High,
high: LevelDrive::Disconnect,
};
}
#[cfg(not(feature = "_nrf54l"))]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum OutputDrive {
Standard = 0,
HighDrive0Standard1 = 1,
Standard0HighDrive1 = 2,
HighDrive = 3,
Disconnect0Standard1 = 4,
Disconnect0HighDrive1 = 5,
Standard0Disconnect1 = 6,
HighDrive0Disconnect1 = 7,
}
pub struct Output<'d> {
pub(crate) pin: Flex<'d>,
}
impl<'d> Output<'d> {
#[inline]
pub fn new(pin: Peri<'d, impl Pin>, initial_output: Level, drive: OutputDrive) -> Self {
let mut pin = Flex::new(pin);
match initial_output {
Level::High => pin.set_high(),
Level::Low => pin.set_low(),
}
pin.set_as_output(drive);
Self { pin }
}
#[inline]
pub fn set_high(&mut self) {
self.pin.set_high()
}
#[inline]
pub fn set_low(&mut self) {
self.pin.set_low()
}
#[inline]
pub fn toggle(&mut self) {
self.pin.toggle()
}
#[inline]
pub fn set_level(&mut self, level: Level) {
self.pin.set_level(level)
}
#[inline]
pub fn is_set_high(&self) -> bool {
self.pin.is_set_high()
}
#[inline]
pub fn is_set_low(&self) -> bool {
self.pin.is_set_low()
}
#[inline]
pub fn get_output_level(&self) -> Level {
self.pin.get_output_level()
}
}
impl Output<'static> {
pub fn persist(self) {
self.pin.persist()
}
}
pub(crate) fn convert_drive(w: &mut pac::gpio::regs::PinCnf, drive: OutputDrive) {
#[cfg(not(feature = "_nrf54l"))]
{
let drive = match drive {
OutputDrive::Standard => vals::Drive::S0S1,
OutputDrive::HighDrive0Standard1 => vals::Drive::H0S1,
OutputDrive::Standard0HighDrive1 => vals::Drive::S0H1,
OutputDrive::HighDrive => vals::Drive::H0H1,
OutputDrive::Disconnect0Standard1 => vals::Drive::D0S1,
OutputDrive::Disconnect0HighDrive1 => vals::Drive::D0H1,
OutputDrive::Standard0Disconnect1 => vals::Drive::S0D1,
OutputDrive::HighDrive0Disconnect1 => vals::Drive::H0D1,
};
w.set_drive(drive);
}
#[cfg(feature = "_nrf54l")]
{
fn convert(d: LevelDrive) -> vals::Drive {
match d {
LevelDrive::Disconnect => vals::Drive::D,
LevelDrive::Standard => vals::Drive::S,
LevelDrive::High => vals::Drive::H,
LevelDrive::ExtraHigh => vals::Drive::E,
}
}
w.set_drive0(convert(drive.low));
w.set_drive1(convert(drive.high));
}
}
fn convert_pull(pull: Pull) -> vals::Pull {
match pull {
Pull::None => vals::Pull::DISABLED,
Pull::Up => vals::Pull::PULLUP,
Pull::Down => vals::Pull::PULLDOWN,
}
}
pub struct Flex<'d> {
pub(crate) pin: Peri<'d, AnyPin>,
}
impl<'d> Flex<'d> {
#[inline]
pub fn new(pin: Peri<'d, impl Pin>) -> Self {
Self { pin: pin.into() }
}
#[inline]
pub fn set_as_input(&mut self, pull: Pull) {
self.pin.conf().write(|w| {
w.set_dir(vals::Dir::INPUT);
w.set_input(vals::Input::CONNECT);
w.set_pull(convert_pull(pull));
convert_drive(w, OutputDrive::Standard);
w.set_sense(vals::Sense::DISABLED);
});
}
#[inline]
pub fn set_as_output(&mut self, drive: OutputDrive) {
self.pin.conf().write(|w| {
w.set_dir(vals::Dir::OUTPUT);
w.set_input(vals::Input::DISCONNECT);
w.set_pull(vals::Pull::DISABLED);
convert_drive(w, drive);
w.set_sense(vals::Sense::DISABLED);
});
}
#[inline]
pub fn set_as_input_output(&mut self, pull: Pull, drive: OutputDrive) {
self.pin.conf().write(|w| {
w.set_dir(vals::Dir::OUTPUT);
w.set_input(vals::Input::CONNECT);
w.set_pull(convert_pull(pull));
convert_drive(w, drive);
w.set_sense(vals::Sense::DISABLED);
});
}
#[inline]
pub fn set_as_disconnected(&mut self) {
self.pin.conf().write(|w| {
w.set_input(vals::Input::DISCONNECT);
});
}
#[inline]
pub fn is_high(&self) -> bool {
self.pin.block().in_().read().pin(self.pin.pin() as _)
}
#[inline]
pub fn is_low(&self) -> bool {
!self.is_high()
}
#[inline]
pub fn get_level(&self) -> Level {
self.is_high().into()
}
#[inline]
pub fn set_high(&mut self) {
self.pin.set_high()
}
#[inline]
pub fn set_low(&mut self) {
self.pin.set_low()
}
#[inline]
pub fn toggle(&mut self) {
if self.is_set_low() {
self.set_high()
} else {
self.set_low()
}
}
#[inline]
pub fn set_level(&mut self, level: Level) {
match level {
Level::Low => self.pin.set_low(),
Level::High => self.pin.set_high(),
}
}
#[inline]
pub fn is_set_high(&self) -> bool {
self.pin.block().out().read().pin(self.pin.pin() as _)
}
#[inline]
pub fn is_set_low(&self) -> bool {
!self.is_set_high()
}
#[inline]
pub fn get_output_level(&self) -> Level {
self.is_set_high().into()
}
}
impl Flex<'static> {
pub fn persist(self) {
core::mem::forget(self);
}
}
impl<'d> Drop for Flex<'d> {
fn drop(&mut self) {
self.set_as_disconnected();
}
}
pub(crate) trait SealedPin {
fn pin_port(&self) -> u8;
#[inline]
fn _pin(&self) -> u8 {
cfg_if! {
if #[cfg(feature = "_gpio-p1")] {
self.pin_port() % 32
} else {
self.pin_port()
}
}
}
#[inline]
fn block(&self) -> gpio::Gpio {
match self.pin_port() / 32 {
#[cfg(feature = "_nrf51")]
0 => pac::GPIO,
#[cfg(not(feature = "_nrf51"))]
0 => pac::P0,
#[cfg(feature = "_gpio-p1")]
1 => pac::P1,
#[cfg(feature = "_gpio-p2")]
2 => pac::P2,
_ => unsafe { unreachable_unchecked() },
}
}
#[inline]
fn conf(&self) -> Reg<gpio::regs::PinCnf, RW> {
self.block().pin_cnf(self._pin() as usize)
}
#[inline]
fn set_high(&self) {
self.block().outset().write(|w| w.set_pin(self._pin() as _, true))
}
#[inline]
fn set_low(&self) {
self.block().outclr().write(|w| w.set_pin(self._pin() as _, true))
}
}
#[allow(private_bounds)]
pub trait Pin: PeripheralType + Into<AnyPin> + SealedPin + Sized + 'static {
#[inline]
fn pin(&self) -> u8 {
self._pin()
}
#[inline]
fn port(&self) -> Port {
match self.pin_port() / 32 {
0 => Port::Port0,
#[cfg(feature = "_gpio-p1")]
1 => Port::Port1,
#[cfg(feature = "_gpio-p2")]
2 => Port::Port2,
_ => unsafe { unreachable_unchecked() },
}
}
#[inline]
#[cfg(not(feature = "_nrf51"))]
fn psel_bits(&self) -> pac::shared::regs::Psel {
pac::shared::regs::Psel(self.pin_port() as u32)
}
}
pub struct AnyPin {
pub(crate) pin_port: u8,
}
impl AnyPin {
#[inline]
pub unsafe fn steal(pin_port: u8) -> Peri<'static, Self> {
Peri::new_unchecked(Self { pin_port })
}
}
impl_peripheral!(AnyPin);
impl Pin for AnyPin {}
impl SealedPin for AnyPin {
#[inline]
fn pin_port(&self) -> u8 {
self.pin_port
}
}
#[cfg(not(feature = "_nrf51"))]
pub(crate) trait PselBits {
fn psel_bits(&self) -> pac::shared::regs::Psel;
}
#[cfg(not(feature = "_nrf51"))]
impl<'a, P: Pin> PselBits for Option<Peri<'a, P>> {
#[inline]
fn psel_bits(&self) -> pac::shared::regs::Psel {
match self {
Some(pin) => pin.psel_bits(),
None => DISCONNECTED,
}
}
}
#[cfg(not(feature = "_nrf51"))]
pub(crate) const DISCONNECTED: Psel = Psel(1 << 31);
#[cfg(not(feature = "_nrf51"))]
#[allow(dead_code)]
pub(crate) fn deconfigure_pin(psel: Psel) {
if psel.connect() == Connect::DISCONNECTED {
return;
}
unsafe { AnyPin::steal(psel.0 as _) }.conf().write(|w| {
w.set_input(vals::Input::DISCONNECT);
})
}
macro_rules! impl_pin {
($type:ident, $port_num:expr, $pin_num:expr) => {
impl crate::gpio::Pin for peripherals::$type {}
impl crate::gpio::SealedPin for peripherals::$type {
#[inline]
fn pin_port(&self) -> u8 {
$port_num * 32 + $pin_num
}
}
impl From<peripherals::$type> for crate::gpio::AnyPin {
fn from(_val: peripherals::$type) -> Self {
Self {
pin_port: $port_num * 32 + $pin_num,
}
}
}
};
}
mod eh02 {
use super::*;
impl<'d> embedded_hal_02::digital::v2::InputPin for Input<'d> {
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> embedded_hal_02::digital::v2::OutputPin for Output<'d> {
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_low();
Ok(())
}
}
impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Output<'d> {
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> embedded_hal_02::digital::v2::ToggleableOutputPin for Output<'d> {
type Error = Infallible;
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
self.toggle();
Ok(())
}
}
impl<'d> embedded_hal_02::digital::v2::InputPin for Flex<'d> {
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> embedded_hal_02::digital::v2::OutputPin for Flex<'d> {
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_low();
Ok(())
}
}
impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Flex<'d> {
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> embedded_hal_02::digital::v2::ToggleableOutputPin for Flex<'d> {
type Error = Infallible;
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
self.toggle();
Ok(())
}
}
}
impl<'d> embedded_hal_1::digital::ErrorType for Input<'d> {
type Error = Infallible;
}
impl<'d> embedded_hal_1::digital::InputPin for Input<'d> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}
impl<'d> embedded_hal_1::digital::ErrorType for Output<'d> {
type Error = Infallible;
}
impl<'d> embedded_hal_1::digital::OutputPin for Output<'d> {
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_low();
Ok(())
}
}
impl<'d> embedded_hal_1::digital::StatefulOutputPin for Output<'d> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}
impl<'d> embedded_hal_1::digital::ErrorType for Flex<'d> {
type Error = Infallible;
}
impl<'d> embedded_hal_1::digital::InputPin for Flex<'d> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}
impl<'d> embedded_hal_1::digital::OutputPin for Flex<'d> {
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_low();
Ok(())
}
}
impl<'d> embedded_hal_1::digital::StatefulOutputPin for Flex<'d> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}