2022-08-22 10:36:33 +02:00
|
|
|
//! General purpose input/output for nRF.
|
2021-05-11 03:04:59 +02:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-03-19 04:08:44 +01:00
|
|
|
use core::convert::Infallible;
|
|
|
|
use core::hint::unreachable_unchecked;
|
|
|
|
|
2021-10-28 03:07:06 +02:00
|
|
|
use cfg_if::cfg_if;
|
2022-07-23 14:00:19 +02:00
|
|
|
use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef};
|
2021-03-19 04:08:44 +01:00
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use self::sealed::Pin as _;
|
2021-03-19 04:08:44 +01:00
|
|
|
use crate::pac::p0 as gpio;
|
2022-05-03 00:43:04 +02:00
|
|
|
use crate::pac::p0::pin_cnf::{DRIVE_A, PULL_A};
|
2022-07-23 14:00:19 +02:00
|
|
|
use crate::{pac, Peripheral};
|
2021-05-26 18:09:18 +02:00
|
|
|
|
2021-03-19 04:08:44 +01:00
|
|
|
/// A GPIO port with up to 32 pins.
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub enum Port {
|
2021-10-12 11:43:57 +02:00
|
|
|
/// Port 0, available on nRF9160 and all nRF52 and nRF51 MCUs.
|
2021-03-19 04:08:44 +01:00
|
|
|
Port0,
|
|
|
|
|
2021-10-28 03:07:06 +02:00
|
|
|
/// Port 1, only available on some MCUs.
|
|
|
|
#[cfg(feature = "_gpio-p1")]
|
2021-03-19 04:08:44 +01:00
|
|
|
Port1,
|
|
|
|
}
|
|
|
|
|
2021-03-20 01:36:00 +01:00
|
|
|
/// Pull setting for an input.
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Pull {
|
2022-08-22 10:36:33 +02:00
|
|
|
/// No pull.
|
2021-03-20 01:36:00 +01:00
|
|
|
None,
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Internal pull-up resistor.
|
2021-03-20 01:36:00 +01:00
|
|
|
Up,
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Internal pull-down resistor.
|
2021-03-20 01:36:00 +01:00
|
|
|
Down,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GPIO input driver.
|
2021-03-24 18:31:11 +01:00
|
|
|
pub struct Input<'d, T: Pin> {
|
2022-01-13 21:15:27 +01:00
|
|
|
pub(crate) pin: Flex<'d, T>,
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 18:31:11 +01:00
|
|
|
impl<'d, T: Pin> Input<'d, T> {
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Create GPIO input driver for a [Pin] with the provided [Pull] configuration.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new(pin: impl Peripheral<P = T> + 'd, pull: Pull) -> Self {
|
2022-01-13 21:15:27 +01:00
|
|
|
let mut pin = Flex::new(pin);
|
2021-12-19 23:25:02 +01:00
|
|
|
pin.set_as_input(pull);
|
2021-03-24 18:31:11 +01:00
|
|
|
|
2021-12-19 23:25:02 +01:00
|
|
|
Self { pin }
|
|
|
|
}
|
2021-03-19 04:08:44 +01:00
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Test if current pin level is high.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2022-03-17 23:20:09 +01:00
|
|
|
pub fn is_high(&self) -> bool {
|
2021-12-19 23:25:02 +01:00
|
|
|
self.pin.is_high()
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Test if current pin level is low.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2022-03-17 23:20:09 +01:00
|
|
|
pub fn is_low(&self) -> bool {
|
2021-12-19 23:25:02 +01:00
|
|
|
self.pin.is_low()
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
2022-07-13 01:21:42 +02:00
|
|
|
|
|
|
|
/// Returns current pin level
|
|
|
|
#[inline]
|
|
|
|
pub fn get_level(&self) -> Level {
|
2022-07-13 01:45:37 +02:00
|
|
|
self.pin.get_level()
|
2022-07-13 01:21:42 +02:00
|
|
|
}
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 01:36:00 +01:00
|
|
|
/// Digital input or output level.
|
2022-12-29 17:26:49 +01:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
2021-03-20 01:36:00 +01:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Level {
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Logical low.
|
2021-03-20 01:36:00 +01:00
|
|
|
Low,
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Logical high.
|
2021-03-20 01:36:00 +01:00
|
|
|
High,
|
|
|
|
}
|
|
|
|
|
2022-07-13 01:21:42 +02:00
|
|
|
impl From<bool> for Level {
|
|
|
|
fn from(val: bool) -> Self {
|
|
|
|
match val {
|
|
|
|
true => Self::High,
|
|
|
|
false => Self::Low,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<bool> for Level {
|
|
|
|
fn into(self) -> bool {
|
|
|
|
match self {
|
|
|
|
Level::Low => false,
|
|
|
|
Level::High => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Drive strength settings for an output pin.
|
2021-05-05 15:46:51 +02:00
|
|
|
// These numbers match DRIVE_A exactly so hopefully the compiler will unify them.
|
2021-03-20 01:36:00 +01:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum OutputDrive {
|
|
|
|
/// Standard '0', standard '1'
|
|
|
|
Standard = 0,
|
|
|
|
/// High drive '0', standard '1'
|
|
|
|
HighDrive0Standard1 = 1,
|
|
|
|
/// Standard '0', high drive '1'
|
|
|
|
Standard0HighDrive1 = 2,
|
|
|
|
/// High drive '0', high 'drive '1'
|
|
|
|
HighDrive = 3,
|
|
|
|
/// Disconnect '0' standard '1' (normally used for wired-or connections)
|
|
|
|
Disconnect0Standard1 = 4,
|
|
|
|
/// Disconnect '0', high drive '1' (normally used for wired-or connections)
|
|
|
|
Disconnect0HighDrive1 = 5,
|
|
|
|
/// Standard '0'. disconnect '1' (also known as "open drain", normally used for wired-and connections)
|
|
|
|
Standard0Disconnect1 = 6,
|
|
|
|
/// High drive '0', disconnect '1' (also known as "open drain", normally used for wired-and connections)
|
|
|
|
HighDrive0Disconnect1 = 7,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GPIO output driver.
|
2021-03-24 18:31:11 +01:00
|
|
|
pub struct Output<'d, T: Pin> {
|
2022-01-13 21:15:27 +01:00
|
|
|
pub(crate) pin: Flex<'d, T>,
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 18:31:11 +01:00
|
|
|
impl<'d, T: Pin> Output<'d, T> {
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Create GPIO output driver for a [Pin] with the provided [Level] and [OutputDriver] configuration.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new(pin: impl Peripheral<P = T> + 'd, initial_output: Level, drive: OutputDrive) -> Self {
|
2022-01-13 21:15:27 +01:00
|
|
|
let mut pin = Flex::new(pin);
|
2021-05-05 19:33:45 +02:00
|
|
|
match initial_output {
|
|
|
|
Level::High => pin.set_high(),
|
|
|
|
Level::Low => pin.set_low(),
|
|
|
|
}
|
2021-12-19 23:25:02 +01:00
|
|
|
pin.set_as_output(drive);
|
2021-05-05 19:33:45 +02:00
|
|
|
|
2021-12-19 23:25:02 +01:00
|
|
|
Self { pin }
|
|
|
|
}
|
2021-03-19 04:08:44 +01:00
|
|
|
|
2021-12-19 23:25:02 +01:00
|
|
|
/// Set the output as high.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn set_high(&mut self) {
|
|
|
|
self.pin.set_high()
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 23:25:02 +01:00
|
|
|
/// Set the output as low.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn set_low(&mut self) {
|
|
|
|
self.pin.set_low()
|
|
|
|
}
|
|
|
|
|
2022-07-13 01:21:42 +02:00
|
|
|
/// Set the output level.
|
|
|
|
#[inline]
|
|
|
|
pub fn set_level(&mut self, level: Level) {
|
2022-07-13 01:45:37 +02:00
|
|
|
self.pin.set_level(level)
|
2022-07-13 01:21:42 +02:00
|
|
|
}
|
|
|
|
|
2021-12-19 23:25:02 +01:00
|
|
|
/// Is the output pin set as high?
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn is_set_high(&self) -> bool {
|
|
|
|
self.pin.is_set_high()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is the output pin set as low?
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn is_set_low(&self) -> bool {
|
|
|
|
self.pin.is_set_low()
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
2022-07-13 01:21:42 +02:00
|
|
|
|
|
|
|
/// What level output is set to
|
|
|
|
#[inline]
|
2022-07-13 01:25:09 +02:00
|
|
|
pub fn get_output_level(&self) -> Level {
|
2022-07-13 01:45:37 +02:00
|
|
|
self.pin.get_output_level()
|
2022-07-13 01:21:42 +02:00
|
|
|
}
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
|
2022-05-03 00:43:04 +02:00
|
|
|
fn convert_drive(drive: OutputDrive) -> DRIVE_A {
|
|
|
|
match drive {
|
|
|
|
OutputDrive::Standard => DRIVE_A::S0S1,
|
|
|
|
OutputDrive::HighDrive0Standard1 => DRIVE_A::H0S1,
|
|
|
|
OutputDrive::Standard0HighDrive1 => DRIVE_A::S0H1,
|
|
|
|
OutputDrive::HighDrive => DRIVE_A::H0H1,
|
|
|
|
OutputDrive::Disconnect0Standard1 => DRIVE_A::D0S1,
|
|
|
|
OutputDrive::Disconnect0HighDrive1 => DRIVE_A::D0H1,
|
|
|
|
OutputDrive::Standard0Disconnect1 => DRIVE_A::S0D1,
|
|
|
|
OutputDrive::HighDrive0Disconnect1 => DRIVE_A::H0D1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_pull(pull: Pull) -> PULL_A {
|
|
|
|
match pull {
|
|
|
|
Pull::None => PULL_A::DISABLED,
|
|
|
|
Pull::Up => PULL_A::PULLUP,
|
|
|
|
Pull::Down => PULL_A::PULLDOWN,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 15:46:51 +02:00
|
|
|
/// GPIO flexible pin.
|
|
|
|
///
|
2022-05-03 00:43:04 +02:00
|
|
|
/// This pin can either be a disconnected, input, or output pin, or both. The level register bit will remain
|
2021-05-05 19:33:45 +02:00
|
|
|
/// set while not in output mode, so the pin's level will be 'remembered' when it is not in output
|
|
|
|
/// mode.
|
2022-01-13 21:15:27 +01:00
|
|
|
pub struct Flex<'d, T: Pin> {
|
2022-07-23 14:00:19 +02:00
|
|
|
pub(crate) pin: PeripheralRef<'d, T>,
|
2021-05-05 15:46:51 +02:00
|
|
|
}
|
|
|
|
|
2022-01-13 21:15:27 +01:00
|
|
|
impl<'d, T: Pin> Flex<'d, T> {
|
|
|
|
/// Wrap the pin in a `Flex`.
|
2021-05-05 15:46:51 +02:00
|
|
|
///
|
2021-05-05 19:33:45 +02:00
|
|
|
/// The pin remains disconnected. The initial output level is unspecified, but can be changed
|
|
|
|
/// before the pin is put into output mode.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new(pin: impl Peripheral<P = T> + 'd) -> Self {
|
|
|
|
into_ref!(pin);
|
2021-05-05 15:46:51 +02:00
|
|
|
// Pin will be in disconnected state.
|
2022-07-03 23:16:10 +02:00
|
|
|
Self { pin }
|
2021-05-05 15:46:51 +02:00
|
|
|
}
|
|
|
|
|
2021-05-05 19:33:45 +02:00
|
|
|
/// Put the pin into input mode.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-05-05 19:33:45 +02:00
|
|
|
pub fn set_as_input(&mut self, pull: Pull) {
|
2021-12-19 23:25:02 +01:00
|
|
|
self.pin.conf().write(|w| {
|
|
|
|
w.dir().input();
|
|
|
|
w.input().connect();
|
2022-05-03 00:43:04 +02:00
|
|
|
w.pull().variant(convert_pull(pull));
|
2021-12-19 23:25:02 +01:00
|
|
|
w.drive().s0s1();
|
|
|
|
w.sense().disabled();
|
|
|
|
w
|
|
|
|
});
|
2021-05-05 15:46:51 +02:00
|
|
|
}
|
|
|
|
|
2021-05-05 19:33:45 +02:00
|
|
|
/// 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.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-05-05 19:33:45 +02:00
|
|
|
pub fn set_as_output(&mut self, drive: OutputDrive) {
|
2021-12-19 23:25:02 +01:00
|
|
|
self.pin.conf().write(|w| {
|
|
|
|
w.dir().output();
|
|
|
|
w.input().disconnect();
|
|
|
|
w.pull().disabled();
|
2022-05-03 00:43:04 +02:00
|
|
|
w.drive().variant(convert_drive(drive));
|
|
|
|
w.sense().disabled();
|
|
|
|
w
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2022-05-03 00:43:04 +02:00
|
|
|
pub fn set_as_input_output(&mut self, pull: Pull, drive: OutputDrive) {
|
|
|
|
self.pin.conf().write(|w| {
|
|
|
|
w.dir().output();
|
|
|
|
w.input().connect();
|
|
|
|
w.pull().variant(convert_pull(pull));
|
|
|
|
w.drive().variant(convert_drive(drive));
|
2021-12-19 23:25:02 +01:00
|
|
|
w.sense().disabled();
|
|
|
|
w
|
|
|
|
});
|
2021-05-05 15:46:51 +02:00
|
|
|
}
|
|
|
|
|
2021-05-05 19:33:45 +02:00
|
|
|
/// Put the pin into disconnected mode.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-05-05 19:33:45 +02:00
|
|
|
pub fn set_as_disconnected(&mut self) {
|
2021-05-05 15:46:51 +02:00
|
|
|
self.pin.conf().reset();
|
|
|
|
}
|
2021-12-19 23:25:02 +01:00
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Test if current pin level is high.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn is_high(&self) -> bool {
|
|
|
|
!self.is_low()
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Test if current pin level is low.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn is_low(&self) -> bool {
|
|
|
|
self.pin.block().in_.read().bits() & (1 << self.pin.pin()) == 0
|
|
|
|
}
|
|
|
|
|
2022-07-13 01:21:42 +02:00
|
|
|
/// Returns current pin level
|
|
|
|
#[inline]
|
|
|
|
pub fn get_level(&self) -> Level {
|
|
|
|
self.is_high().into()
|
|
|
|
}
|
|
|
|
|
2021-12-19 23:25:02 +01:00
|
|
|
/// Set the output as high.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn set_high(&mut self) {
|
|
|
|
self.pin.set_high()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the output as low.
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn set_low(&mut self) {
|
|
|
|
self.pin.set_low()
|
|
|
|
}
|
|
|
|
|
2022-07-13 01:21:42 +02:00
|
|
|
/// 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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 23:25:02 +01:00
|
|
|
/// Is the output pin set as high?
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn is_set_high(&self) -> bool {
|
|
|
|
!self.is_set_low()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is the output pin set as low?
|
2022-07-13 01:21:42 +02:00
|
|
|
#[inline]
|
2021-12-19 23:25:02 +01:00
|
|
|
pub fn is_set_low(&self) -> bool {
|
|
|
|
self.pin.block().out.read().bits() & (1 << self.pin.pin()) == 0
|
|
|
|
}
|
2022-07-13 01:21:42 +02:00
|
|
|
|
|
|
|
/// What level output is set to
|
|
|
|
#[inline]
|
2022-07-13 01:25:09 +02:00
|
|
|
pub fn get_output_level(&self) -> Level {
|
2022-07-13 01:21:42 +02:00
|
|
|
self.is_set_high().into()
|
|
|
|
}
|
2021-05-05 15:46:51 +02:00
|
|
|
}
|
|
|
|
|
2022-01-13 21:15:27 +01:00
|
|
|
impl<'d, T: Pin> Drop for Flex<'d, T> {
|
2021-05-05 15:46:51 +02:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.pin.conf().reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 04:08:44 +01:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Pin {
|
|
|
|
fn pin_port(&self) -> u8;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn _pin(&self) -> u8 {
|
2021-10-28 03:07:06 +02:00
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(feature = "_gpio-p1")] {
|
|
|
|
self.pin_port() % 32
|
|
|
|
} else {
|
|
|
|
self.pin_port()
|
|
|
|
}
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2021-03-19 04:08:44 +01:00
|
|
|
fn block(&self) -> &gpio::RegisterBlock {
|
|
|
|
unsafe {
|
|
|
|
match self.pin_port() / 32 {
|
|
|
|
0 => &*pac::P0::ptr(),
|
2021-10-28 03:07:06 +02:00
|
|
|
#[cfg(feature = "_gpio-p1")]
|
2021-03-19 04:08:44 +01:00
|
|
|
1 => &*pac::P1::ptr(),
|
|
|
|
_ => unreachable_unchecked(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2021-03-19 04:08:44 +01:00
|
|
|
fn conf(&self) -> &gpio::PIN_CNF {
|
|
|
|
&self.block().pin_cnf[self._pin() as usize]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the output as high.
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2021-03-19 04:08:44 +01:00
|
|
|
fn set_high(&self) {
|
2022-01-13 21:24:06 +01:00
|
|
|
unsafe { self.block().outset.write(|w| w.bits(1u32 << self._pin())) }
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the output as low.
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2021-03-19 04:08:44 +01:00
|
|
|
fn set_low(&self) {
|
2022-01-13 21:24:06 +01:00
|
|
|
unsafe { self.block().outclr.write(|w| w.bits(1u32 << self._pin())) }
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an [AnyPin].
|
2022-07-23 14:27:45 +02:00
|
|
|
pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + sealed::Pin + Sized + 'static {
|
2021-03-20 01:36:00 +01:00
|
|
|
/// Number of the pin within the port (0..31)
|
2021-03-19 04:08:44 +01:00
|
|
|
#[inline]
|
|
|
|
fn pin(&self) -> u8 {
|
|
|
|
self._pin()
|
|
|
|
}
|
|
|
|
|
2021-03-20 01:36:00 +01:00
|
|
|
/// Port of the pin
|
2021-03-19 04:08:44 +01:00
|
|
|
#[inline]
|
|
|
|
fn port(&self) -> Port {
|
|
|
|
match self.pin_port() / 32 {
|
2021-03-22 02:10:15 +01:00
|
|
|
0 => Port::Port0,
|
2021-10-28 03:07:06 +02:00
|
|
|
#[cfg(feature = "_gpio-p1")]
|
2021-03-19 04:08:44 +01:00
|
|
|
1 => Port::Port1,
|
|
|
|
_ => unsafe { unreachable_unchecked() },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Peripheral port register value
|
2021-03-19 04:08:44 +01:00
|
|
|
#[inline]
|
|
|
|
fn psel_bits(&self) -> u32 {
|
|
|
|
self.pin_port() as u32
|
|
|
|
}
|
|
|
|
|
2021-03-20 01:36:00 +01:00
|
|
|
/// Convert from concrete pin type PX_XX to type erased `AnyPin`.
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2021-03-19 04:08:44 +01:00
|
|
|
fn degrade(self) -> AnyPin {
|
|
|
|
AnyPin {
|
|
|
|
pin_port: self.pin_port(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Type-erased GPIO pin
|
2021-03-19 04:08:44 +01:00
|
|
|
pub struct AnyPin {
|
|
|
|
pin_port: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AnyPin {
|
2022-08-22 10:36:33 +02:00
|
|
|
/// Create an [AnyPin] for a specific pin.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// - `pin_port` should not in use by another driver.
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2021-03-20 03:09:42 +01:00
|
|
|
pub unsafe fn steal(pin_port: u8) -> Self {
|
|
|
|
Self { pin_port }
|
2021-03-19 04:08:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 14:00:19 +02:00
|
|
|
impl_peripheral!(AnyPin);
|
2021-03-19 04:08:44 +01:00
|
|
|
impl Pin for AnyPin {}
|
|
|
|
impl sealed::Pin for AnyPin {
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2021-03-19 04:08:44 +01:00
|
|
|
fn pin_port(&self) -> u8 {
|
|
|
|
self.pin_port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:10:15 +01:00
|
|
|
// ====================
|
|
|
|
|
2022-02-12 01:04:01 +01:00
|
|
|
pub(crate) trait PselBits {
|
|
|
|
fn psel_bits(&self) -> u32;
|
2021-03-22 02:10:15 +01:00
|
|
|
}
|
|
|
|
|
2022-07-23 14:00:19 +02:00
|
|
|
impl<'a, P: Pin> PselBits for Option<PeripheralRef<'a, P>> {
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2022-02-12 01:04:01 +01:00
|
|
|
fn psel_bits(&self) -> u32 {
|
2022-07-03 23:16:10 +02:00
|
|
|
match self {
|
|
|
|
Some(pin) => pin.psel_bits(),
|
|
|
|
None => 1u32 << 31,
|
|
|
|
}
|
2021-03-22 02:10:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 18:09:18 +02:00
|
|
|
pub(crate) fn deconfigure_pin(psel_bits: u32) {
|
|
|
|
if psel_bits & 0x8000_0000 != 0 {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-19 23:25:02 +01:00
|
|
|
unsafe { AnyPin::steal(psel_bits as _).conf().reset() }
|
2021-05-26 18:09:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ====================
|
|
|
|
|
2021-03-29 00:44:11 +02:00
|
|
|
macro_rules! impl_pin {
|
2021-03-19 04:08:44 +01:00
|
|
|
($type:ident, $port_num:expr, $pin_num:expr) => {
|
2021-05-11 03:04:59 +02:00
|
|
|
impl crate::gpio::Pin for peripherals::$type {}
|
|
|
|
impl crate::gpio::sealed::Pin for peripherals::$type {
|
2021-03-22 02:10:15 +01:00
|
|
|
#[inline]
|
2021-03-19 04:08:44 +01:00
|
|
|
fn pin_port(&self) -> u8 {
|
|
|
|
$port_num * 32 + $pin_num
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 14:27:45 +02:00
|
|
|
|
|
|
|
impl From<peripherals::$type> for crate::gpio::AnyPin {
|
|
|
|
fn from(val: peripherals::$type) -> Self {
|
|
|
|
crate::gpio::Pin::degrade(val)
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 04:08:44 +01:00
|
|
|
};
|
|
|
|
}
|
2022-01-13 23:56:25 +01:00
|
|
|
|
|
|
|
// ====================
|
|
|
|
|
|
|
|
mod eh02 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, T: Pin> embedded_hal_02::digital::v2::InputPin for Input<'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 Output<'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 Output<'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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 12:59:18 +02:00
|
|
|
/// Implement [`embedded_hal_02::digital::v2::InputPin`] for [`Flex`];
|
2022-01-13 23:56:25 +01:00
|
|
|
///
|
|
|
|
/// If the pin is not in input mode the result is unspecified.
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "unstable-traits")]
|
|
|
|
mod eh1 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
impl<'d, T: Pin> embedded_hal_1::digital::ErrorType for Input<'d, T> {
|
|
|
|
type Error = Infallible;
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:02:43 +02:00
|
|
|
impl<'d, T: Pin> embedded_hal_1::digital::InputPin for Input<'d, T> {
|
2022-01-13 23:56:25 +01:00
|
|
|
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::ErrorType for Output<'d, T> {
|
|
|
|
type Error = Infallible;
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:02:43 +02:00
|
|
|
impl<'d, T: Pin> embedded_hal_1::digital::OutputPin for Output<'d, T> {
|
2022-01-13 23:56:25 +01:00
|
|
|
fn set_high(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.set_high())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_low(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.set_low())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:02:43 +02:00
|
|
|
impl<'d, T: Pin> embedded_hal_1::digital::StatefulOutputPin for Output<'d, T> {
|
2022-01-13 23:56:25 +01:00
|
|
|
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::ErrorType for Flex<'d, T> {
|
|
|
|
type Error = Infallible;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implement [`InputPin`] for [`Flex`];
|
|
|
|
///
|
|
|
|
/// If the pin is not in input mode the result is unspecified.
|
2022-09-29 11:02:43 +02:00
|
|
|
impl<'d, T: Pin> embedded_hal_1::digital::InputPin for Flex<'d, T> {
|
2022-01-13 23:56:25 +01:00
|
|
|
fn is_high(&self) -> Result<bool, Self::Error> {
|
|
|
|
Ok(self.is_high())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_low(&self) -> Result<bool, Self::Error> {
|
|
|
|
Ok(self.is_low())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:02:43 +02:00
|
|
|
impl<'d, T: Pin> embedded_hal_1::digital::OutputPin for Flex<'d, T> {
|
2022-01-13 23:56:25 +01:00
|
|
|
fn set_high(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.set_high())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_low(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(self.set_low())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:02:43 +02:00
|
|
|
impl<'d, T: Pin> embedded_hal_1::digital::StatefulOutputPin for Flex<'d, T> {
|
2022-01-13 23:56:25 +01:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|