//! General purpose input/output (GPIO) driver. #![macro_use] use core::convert::Infallible; use core::hint::unreachable_unchecked; use cfg_if::cfg_if; use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef}; use crate::pac::common::{Reg, RW}; use crate::pac::gpio; use crate::pac::gpio::vals; #[cfg(not(feature = "_nrf51"))] use crate::pac::shared::{regs::Psel, vals::Connect}; use crate::{pac, Peripheral}; /// A GPIO port with up to 32 pins. #[derive(Debug, Eq, PartialEq)] pub enum Port { /// Port 0, available on nRF9160 and all nRF52 and nRF51 MCUs. Port0, /// Port 1, only available on some MCUs. #[cfg(feature = "_gpio-p1")] Port1, /// Port 2, only available on some MCUs. #[cfg(feature = "_gpio-p2")] Port2, } /// Pull setting for an input. #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Pull { /// No pull. None, /// Internal pull-up resistor. Up, /// Internal pull-down resistor. Down, } /// GPIO input driver. pub struct Input<'d> { pub(crate) pin: Flex<'d>, } impl<'d> Input<'d> { /// Create GPIO input driver for a [Pin] with the provided [Pull] configuration. #[inline] pub fn new(pin: impl Peripheral
+ 'd, pull: Pull) -> Self {
let mut pin = Flex::new(pin);
pin.set_as_input(pull);
Self { pin }
}
/// Get whether the pin input level is high.
#[inline]
pub fn is_high(&self) -> bool {
self.pin.is_high()
}
/// Get whether the pin input level is low.
#[inline]
pub fn is_low(&self) -> bool {
self.pin.is_low()
}
/// Get the pin input level.
#[inline]
pub fn get_level(&self) -> Level {
self.pin.get_level()
}
}
/// Digital input or output level.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Level {
/// Logical low.
Low,
/// Logical high.
High,
}
impl From + 'd, 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 }
}
/// 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()
}
/// Toggle the output level.
#[inline]
pub fn toggle(&mut self) {
self.pin.toggle()
}
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
self.pin.set_level(level)
}
/// Get whether the output level is set to high.
#[inline]
pub fn is_set_high(&self) -> bool {
self.pin.is_set_high()
}
/// Get whether the output level is set to low.
#[inline]
pub fn is_set_low(&self) -> bool {
self.pin.is_set_low()
}
/// Get the current output level.
#[inline]
pub fn get_output_level(&self) -> Level {
self.pin.get_output_level()
}
}
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_drive0(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,
}
}
/// GPIO flexible pin.
///
/// This pin can either be a disconnected, input, or output pin, or both. The 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> {
pub(crate) pin: PeripheralRef<'d, AnyPin>,
}
impl<'d> Flex<'d> {
/// Wrap the pin in a `Flex`.
///
/// The pin remains disconnected. The initial output level is unspecified, but can be changed
/// before the pin is put into output mode.
#[inline]
pub fn new(pin: impl Peripheral + 'd) -> Self {
into_ref!(pin);
// Pin will be in disconnected state.
Self { pin: pin.map_into() }
}
/// Put the pin into input mode.
#[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);
});
}
/// 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, 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);
});
}
/// Put the pin into input + output mode.
///
/// This is commonly used for "open drain" mode. If you set `drive = Standard0Disconnect1`,
/// the hardware will drive the line low if you set it to low, and will leave it floating if you set
/// it to high, in which case you can read the input to figure out whether another device
/// is driving the line low.
///
/// 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_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);
});
}
/// Put the pin into disconnected mode.
#[inline]
pub fn set_as_disconnected(&mut self) {
self.pin.conf().write(|w| {
w.set_input(vals::Input::DISCONNECT);
});
}
/// Get whether the pin input level is high.
#[inline]
pub fn is_high(&self) -> bool {
self.pin.block().in_().read().pin(self.pin.pin() as _)
}
/// Get whether the pin input level is low.
#[inline]
pub fn is_low(&self) -> bool {
!self.is_high()
}
/// Get the pin input level.
#[inline]
pub fn get_level(&self) -> Level {
self.is_high().into()
}
/// 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()
}
/// Toggle the output level.
#[inline]
pub fn toggle(&mut self) {
if self.is_set_low() {
self.set_high()
} else {
self.set_low()
}
}
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
match level {
Level::Low => self.pin.set_low(),
Level::High => self.pin.set_high(),
}
}
/// Get whether the output level is set to high.
#[inline]
pub fn is_set_high(&self) -> bool {
self.pin.block().out().read().pin(self.pin.pin() as _)
}
/// Get whether the output level is set to low.
#[inline]
pub fn is_set_low(&self) -> bool {
!self.is_set_high()
}
/// Get the current output level.
#[inline]
pub fn get_output_level(&self) -> Level {
self.is_set_high().into()
}
}
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 + Into