Skip to content

Commit

Permalink
stm32/gpio: remove generics.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Jan 22, 2024
1 parent 20fd03a commit 90f007c
Show file tree
Hide file tree
Showing 29 changed files with 144 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

use embassy_executor::Spawner;
use embassy_stm32::exti::ExtiInput;
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
use embassy_stm32::gpio::{Level, Output, Pull, Speed};
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
let mut led = Output::new(p.PB14, Level::Low, Speed::VeryHigh);
let mut button = ExtiInput::new(Input::new(p.PC13, Pull::Up), p.EXTI13);
let mut button = ExtiInput::new(p.PC13, p.EXTI13, Pull::Up);

loop {
button.wait_for_any_edge().await;
Expand Down
13 changes: 6 additions & 7 deletions docs/modules/ROOT/examples/layer-by-layer/blinky-irq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use core::cell::RefCell;
use cortex_m::interrupt::Mutex;
use cortex_m::peripheral::NVIC;
use cortex_m_rt::entry;
use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed};
use embassy_stm32::peripherals::{PB14, PC13};
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
use embassy_stm32::{interrupt, pac};
use {defmt_rtt as _, panic_probe as _};

static BUTTON: Mutex<RefCell<Option<Input<'static, PC13>>>> = Mutex::new(RefCell::new(None));
static LED: Mutex<RefCell<Option<Output<'static, PB14>>>> = Mutex::new(RefCell::new(None));
static BUTTON: Mutex<RefCell<Option<Input<'static>>>> = Mutex::new(RefCell::new(None));
static LED: Mutex<RefCell<Option<Output<'static>>>> = Mutex::new(RefCell::new(None));

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -62,22 +61,22 @@ fn EXTI15_10() {

const PORT: u8 = 2;
const PIN: usize = 13;
fn check_interrupt<P: Pin>(_pin: &mut Input<'static, P>) -> bool {
fn check_interrupt(_pin: &mut Input<'static>) -> bool {
let exti = pac::EXTI;
let pin = PIN;
let lines = exti.pr(0).read();
lines.line(pin)
}

fn clear_interrupt<P: Pin>(_pin: &mut Input<'static, P>) {
fn clear_interrupt(_pin: &mut Input<'static>) {
let exti = pac::EXTI;
let pin = PIN;
let mut lines = exti.pr(0).read();
lines.set_line(pin, true);
exti.pr(0).write_value(lines);
}

fn enable_interrupt<P: Pin>(_pin: &mut Input<'static, P>) {
fn enable_interrupt(_pin: &mut Input<'static>) {
cortex_m::interrupt::free(|_| {
let rcc = pac::RCC;
rcc.apb2enr().modify(|w| w.set_syscfgen(true));
Expand Down
45 changes: 28 additions & 17 deletions embassy-stm32/src/exti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{Context, Poll};

use embassy_hal_internal::impl_peripheral;
use embassy_hal_internal::{impl_peripheral, into_ref};
use embassy_sync::waitqueue::AtomicWaker;

use crate::gpio::{AnyPin, Input, Level, Pin as GpioPin};
use crate::gpio::{AnyPin, Input, Level, Pin as GpioPin, Pull};
use crate::pac::exti::regs::Lines;
use crate::pac::EXTI;
use crate::{interrupt, pac, peripherals, Peripheral};
Expand Down Expand Up @@ -93,16 +93,27 @@ impl Iterator for BitIter {
/// EXTI channel, which is a limited resource.
///
/// Pins PA5, PB5, PC5... all use EXTI channel 5, so you can't use EXTI on, say, PA5 and PC5 at the same time.
pub struct ExtiInput<'d, T: GpioPin> {
pin: Input<'d, T>,
pub struct ExtiInput<'d> {
pin: Input<'d>,
}

impl<'d, T: GpioPin> Unpin for ExtiInput<'d, T> {}
impl<'d> Unpin for ExtiInput<'d> {}

impl<'d, T: GpioPin> ExtiInput<'d, T> {
impl<'d> ExtiInput<'d> {
/// Create an EXTI input.
pub fn new(pin: Input<'d, T>, _ch: impl Peripheral<P = T::ExtiChannel> + 'd) -> Self {
Self { pin }
pub fn new<T: GpioPin>(
pin: impl Peripheral<P = T> + 'd,
ch: impl Peripheral<P = T::ExtiChannel> + 'd,
pull: Pull,
) -> Self {
into_ref!(pin, ch);

// Needed if using AnyPin+AnyChannel.
assert_eq!(pin.pin(), ch.number());

Self {
pin: Input::new(pin, pull),
}
}

/// Get whether the pin is high.
Expand Down Expand Up @@ -162,7 +173,7 @@ impl<'d, T: GpioPin> ExtiInput<'d, T> {
}
}

impl<'d, T: GpioPin> embedded_hal_02::digital::v2::InputPin for ExtiInput<'d, T> {
impl<'d> embedded_hal_02::digital::v2::InputPin for ExtiInput<'d> {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
Expand All @@ -174,11 +185,11 @@ impl<'d, T: GpioPin> embedded_hal_02::digital::v2::InputPin for ExtiInput<'d, T>
}
}

impl<'d, T: GpioPin> embedded_hal_1::digital::ErrorType for ExtiInput<'d, T> {
impl<'d> embedded_hal_1::digital::ErrorType for ExtiInput<'d> {
type Error = Infallible;
}

impl<'d, T: GpioPin> embedded_hal_1::digital::InputPin for ExtiInput<'d, T> {
impl<'d> embedded_hal_1::digital::InputPin for ExtiInput<'d> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
Expand All @@ -188,7 +199,7 @@ impl<'d, T: GpioPin> embedded_hal_1::digital::InputPin for ExtiInput<'d, T> {
}
}

impl<'d, T: GpioPin> embedded_hal_async::digital::Wait for ExtiInput<'d, T> {
impl<'d> embedded_hal_async::digital::Wait for ExtiInput<'d> {
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
Expand Down Expand Up @@ -326,7 +337,7 @@ pub(crate) mod sealed {
/// EXTI channel trait.
pub trait Channel: sealed::Channel + Sized {
/// Get the EXTI channel number.
fn number(&self) -> usize;
fn number(&self) -> u8;

/// Type-erase (degrade) this channel into an `AnyChannel`.
///
Expand All @@ -350,17 +361,17 @@ pub struct AnyChannel {
impl_peripheral!(AnyChannel);
impl sealed::Channel for AnyChannel {}
impl Channel for AnyChannel {
fn number(&self) -> usize {
self.number as usize
fn number(&self) -> u8 {
self.number
}
}

macro_rules! impl_exti {
($type:ident, $number:expr) => {
impl sealed::Channel for peripherals::$type {}
impl Channel for peripherals::$type {
fn number(&self) -> usize {
$number as usize
fn number(&self) -> u8 {
$number
}
}
};
Expand Down
Loading

0 comments on commit 90f007c

Please sign in to comment.