Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions embassy-nxp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- next-header -->
## Unreleased - ReleaseDate
- LPC55: Remove internal match_iocon macro
- LPC55: DMA Controller and asynchronous version of USART
- Moved NXP LPC55S69 from `lpc55-pac` to `nxp-pac`
- First release with changelog.
66 changes: 22 additions & 44 deletions embassy-nxp/src/gpio/lpc55.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use embassy_hal_internal::{PeripheralType, impl_peripheral};

use crate::pac::common::{RW, Reg};
use crate::pac::iocon::vals::{PioDigimode, PioMode};
use crate::pac::{GPIO, IOCON, SYSCON};
use crate::pac::{GPIO, IOCON, SYSCON, iocon};
use crate::{Peri, peripherals};

pub(crate) fn init() {
Expand Down Expand Up @@ -109,13 +110,7 @@ impl<'d> Input<'d> {

/// Set the pull configuration for the pin. To disable the pull, use [Pull::None].
pub fn set_pull(&mut self, pull: Pull) {
match_iocon!(register, self.pin.pin_bank(), self.pin.pin_number(), {
register.modify(|w| match pull {
Pull::None => w.set_mode(PioMode::INACTIVE),
Pull::Up => w.set_mode(PioMode::PULL_UP),
Pull::Down => w.set_mode(PioMode::PULL_DOWN),
});
});
self.pin.set_pull(pull);
}

/// Get the current input level of the pin.
Expand Down Expand Up @@ -193,11 +188,20 @@ impl<'d> Flex<'d> {
1 << self.pin.pin_number()
}

/// Set the pull configuration for the pin. To disable the pull, use [Pull::None].
pub fn set_pull(&mut self, pull: Pull) {
self.pin.pio().modify(|w| match pull {
Pull::None => w.set_mode(PioMode::INACTIVE),
Pull::Up => w.set_mode(PioMode::PULL_UP),
Pull::Down => w.set_mode(PioMode::PULL_DOWN),
});
}

/// Set the pin to digital mode. This is required for using a pin as a GPIO pin. The default
/// setting for pins is (usually) non-digital.
fn set_as_digital(&mut self) {
match_iocon!(register, self.pin_bank(), self.pin_number(), {
register.modify(|w| w.set_digimode(PioDigimode::DIGITAL));
self.pin.pio().modify(|w| {
w.set_digimode(PioDigimode::DIGITAL);
});
}

Expand All @@ -220,6 +224,14 @@ impl<'d> Flex<'d> {
pub(crate) trait SealedPin: Sized {
fn pin_bank(&self) -> Bank;
fn pin_number(&self) -> u8;

#[inline]
fn pio(&self) -> Reg<iocon::regs::Pio, RW> {
match self.pin_bank() {
Bank::Bank0 => IOCON.pio0(self.pin_number() as usize),
Bank::Bank1 => IOCON.pio1(self.pin_number() as usize),
}
}
}

/// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an
Expand Down Expand Up @@ -272,40 +284,6 @@ impl SealedPin for AnyPin {
}
}

/// Match the pin bank and number of a pin to the corresponding IOCON register.
///
/// # Example
/// ```
/// use embassy_nxp::gpio::Bank;
/// use embassy_nxp::pac_utils::{iocon_reg, match_iocon};
///
/// // Make pin PIO1_6 digital and set it to pull-down mode.
/// match_iocon!(register, Bank::Bank1, 6, {
/// register.modify(|w|{
/// w.set_mode(PioMode::PULL_DOWN);
/// w.set_digimode(PioDigimode::DIGITAL);
///
/// }
/// });
/// ```
macro_rules! match_iocon {
($register:ident, $pin_bank:expr, $pin_number:expr, $action:expr) => {
match $pin_bank {
Bank::Bank0 => {
let $register = IOCON.pio0($pin_number as usize);
$action;
}

Bank::Bank1 => {
let $register = IOCON.pio1($pin_number as usize);
$action;
}
}
};
}

pub(crate) use match_iocon;

macro_rules! impl_pin {
($name:ident, $bank:expr, $pin_num:expr) => {
impl Pin for peripherals::$name {}
Expand Down
38 changes: 17 additions & 21 deletions embassy-nxp/src/usart/lpc55.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use embassy_sync::waitqueue::AtomicWaker;
use embedded_io::{self, ErrorKind};

use crate::dma::{AnyChannel, Channel};
use crate::gpio::{AnyPin, Bank, SealedPin, match_iocon};
use crate::gpio::{AnyPin, SealedPin};
use crate::interrupt::Interrupt;
use crate::interrupt::typelevel::{Binding, Interrupt as _};
use crate::pac::flexcomm::Flexcomm as FlexcommReg;
Expand Down Expand Up @@ -555,29 +555,25 @@ impl<'d, M: Mode> Usart<'d, M> {

fn pin_config<T: Instance>(tx: Option<Peri<'_, AnyPin>>, rx: Option<Peri<'_, AnyPin>>) {
if let Some(tx_pin) = tx {
match_iocon!(register, tx_pin.pin_bank(), tx_pin.pin_number(), {
register.modify(|w| {
w.set_func(T::tx_pin_func());
w.set_mode(iocon::vals::PioMode::INACTIVE);
w.set_slew(iocon::vals::PioSlew::STANDARD);
w.set_invert(false);
w.set_digimode(iocon::vals::PioDigimode::DIGITAL);
w.set_od(iocon::vals::PioOd::NORMAL);
});
})
tx_pin.pio().modify(|w| {
w.set_func(T::tx_pin_func());
w.set_mode(iocon::vals::PioMode::INACTIVE);
w.set_slew(iocon::vals::PioSlew::STANDARD);
w.set_invert(false);
w.set_digimode(iocon::vals::PioDigimode::DIGITAL);
w.set_od(iocon::vals::PioOd::NORMAL);
});
}

if let Some(rx_pin) = rx {
match_iocon!(register, rx_pin.pin_bank(), rx_pin.pin_number(), {
register.modify(|w| {
w.set_func(T::rx_pin_func());
w.set_mode(iocon::vals::PioMode::INACTIVE);
w.set_slew(iocon::vals::PioSlew::STANDARD);
w.set_invert(false);
w.set_digimode(iocon::vals::PioDigimode::DIGITAL);
w.set_od(iocon::vals::PioOd::NORMAL);
});
})
rx_pin.pio().modify(|w| {
w.set_func(T::rx_pin_func());
w.set_mode(iocon::vals::PioMode::INACTIVE);
w.set_slew(iocon::vals::PioSlew::STANDARD);
w.set_invert(false);
w.set_digimode(iocon::vals::PioDigimode::DIGITAL);
w.set_od(iocon::vals::PioOd::NORMAL);
});
};
}

Expand Down