embassy-stm32 0.6.0

Embassy Hardware Abstraction Layer (HAL) for ST STM32 series microcontrollers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! External Interrupts (EXTI)
use core::convert::Infallible;
use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{Context, Poll};

use embassy_hal_internal::PeripheralType;
use embassy_sync::waitqueue::AtomicWaker;
use futures_util::FutureExt;

use crate::gpio::{AnyPin, ExtiPin, Flex, Input, Level, Pin as GpioPin, PinNumber, Pull};
use crate::interrupt::Interrupt as InterruptEnum;
use crate::interrupt::typelevel::{Binding, Handler, Interrupt as InterruptType};
use crate::mode::{Async, Blocking, Mode as PeriMode};
use crate::pac::EXTI;
use crate::{Peri, pac};

#[macro_use]
mod low_level;
pub use low_level::{InterruptState, TriggerEdge};

pub mod blocking;

const EXTI_COUNT: usize = 16;
static EXTI_WAKERS: [AtomicWaker; EXTI_COUNT] = [const { AtomicWaker::new() }; EXTI_COUNT];

#[cfg(all(exti_w, feature = "_core-cm0p"))]
fn cpu_regs() -> pac::exti::Cpu {
    EXTI.cpu(1)
}

#[cfg(all(exti_w, not(feature = "_core-cm0p")))]
fn cpu_regs() -> pac::exti::Cpu {
    EXTI.cpu(0)
}

#[cfg(not(exti_w))]
fn cpu_regs() -> pac::exti::Exti {
    EXTI
}

#[cfg(not(any(
    exti_c0, exti_g0, exti_u0, exti_l5, gpio_v1, exti_u5, exti_u3, exti_h5, exti_h50, exti_n6
)))]
fn exticr_regs() -> pac::syscfg::Syscfg {
    pac::SYSCFG
}
#[cfg(any(exti_c0, exti_g0, exti_u0, exti_l5, exti_u5, exti_u3, exti_h5, exti_h50, exti_n6))]
fn exticr_regs() -> pac::exti::Exti {
    EXTI
}
#[cfg(gpio_v1)]
fn exticr_regs() -> pac::afio::Afio {
    pac::AFIO
}

unsafe fn on_irq() {
    cfg_no_rpr_fpr! {
        let bits = EXTI.pr(0).read().0;
    }
    cfg_has_rpr_fpr! {
        let bits = EXTI.rpr(0).read().0 | EXTI.fpr(0).read().0;
    }

    // We don't handle or change any EXTI lines above 16.
    let bits = bits & 0x0000FFFF;

    // Mask all the channels that fired.
    cpu_regs().imr(0).modify(|w| w.0 &= !bits);

    // Wake the tasks
    for pin in BitIter(bits) {
        EXTI_WAKERS[pin as usize].wake();
    }

    // Clear pending
    low_level::clear_exti_pending_mask(bits);
}

struct BitIter(u32);

impl Iterator for BitIter {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        match self.0.trailing_zeros() {
            32 => None,
            b => {
                self.0 &= !(1 << b);
                Some(b)
            }
        }
    }
}

/// EXTI input driver.
///
/// This driver augments a GPIO `Input` with EXTI functionality. EXTI is not
/// built into `Input` itself because it needs to take ownership of the corresponding
/// 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, Mode: PeriMode> {
    pin: Input<'d>,
    _kind: PhantomData<Mode>,
}

impl<'d, Mode: PeriMode> Unpin for ExtiInput<'d, Mode> {}

impl<'d, Mode: PeriMode> ExtiInput<'d, Mode> {
    /// Get whether the pin is high.
    pub fn is_high(&self) -> bool {
        self.pin.is_high()
    }

    /// Get whether the pin is low.
    pub fn is_low(&self) -> bool {
        self.pin.is_low()
    }

    /// Get the pin level.
    pub fn get_level(&self) -> Level {
        self.pin.get_level()
    }
}

impl<'d> ExtiInput<'d, Async> {
    /// Create an EXTI input.
    ///
    /// The Binding must bind the Channel's IRQ to [InterruptHandler].
    pub fn new<T: ExtiPin + GpioPin>(
        pin: Peri<'d, T>,
        _ch: Peri<'d, T::ExtiChannel>,
        pull: Pull,
        _irq: impl Binding<
            <<T as ExtiPin>::ExtiChannel as Channel>::IRQ,
            InterruptHandler<<<T as ExtiPin>::ExtiChannel as Channel>::IRQ>,
        >,
    ) -> Self {
        Self {
            pin: Input::new(pin, pull),
            _kind: PhantomData,
        }
    }

    /// Create an EXTI input from an existing [`Input`] pin.
    ///
    /// Useful when a pin was previously used as a plain [`Input`] and needs to
    /// be upgraded to interrupt-driven mode without re-acquiring the peripheral
    /// token. The pin retains its current pull configuration.
    ///
    /// The Binding must bind the Channel's IRQ to [InterruptHandler].
    pub fn from_input<C: Channel>(
        pin: Input<'d>,
        _ch: Peri<'d, C>,
        _irq: impl Binding<C::IRQ, InterruptHandler<C::IRQ>>,
    ) -> Self {
        Self {
            pin,
            _kind: PhantomData,
        }
    }

    /// Create an EXTI input from an existing [`Flex`] pin.
    ///
    /// Useful when a pin was previously used in bidirectional mode (e.g.,
    /// driven as an output for a hardware entry sequence) and needs to be
    /// switched to interrupt-driven input mode without re-acquiring the
    /// peripheral token.
    ///
    /// The pin should be in input mode (configured via [`Flex::set_as_input()`])
    /// before calling this.
    ///
    /// The Binding must bind the Channel's IRQ to [InterruptHandler].
    pub fn from_flex<C: Channel>(
        pin: Flex<'d>,
        _ch: Peri<'d, C>,
        _irq: impl Binding<C::IRQ, InterruptHandler<C::IRQ>>,
    ) -> Self {
        Self {
            pin: Input::from_flex(pin),
            _kind: PhantomData,
        }
    }

    /// Asynchronously wait until the pin is high.
    ///
    /// This returns immediately if the pin is already high.
    pub async fn wait_for_high(&mut self) {
        let fut = ExtiInputFuture::new(&self.pin, TriggerEdge::Rising, true);
        if self.is_high() {
            return;
        }
        fut.await
    }

    /// Asynchronously wait until the pin is low.
    ///
    /// This returns immediately if the pin is already low.
    pub async fn wait_for_low(&mut self) {
        let fut = ExtiInputFuture::new(&self.pin, TriggerEdge::Falling, true);
        if self.is_low() {
            return;
        }
        fut.await
    }

    /// Asynchronously wait until the pin sees a rising edge.
    ///
    /// If the pin is already high, it will wait for it to go low then back high.
    pub async fn wait_for_rising_edge(&mut self) {
        ExtiInputFuture::new(&self.pin, TriggerEdge::Rising, true).await
    }

    /// Asynchronously wait until the pin sees a rising edge.
    ///
    /// If the pin is already high, it will wait for it to go low then back high.
    pub fn poll_for_rising_edge<'a>(&mut self, cx: &mut Context<'a>) {
        let _ = ExtiInputFuture::new(&self.pin, TriggerEdge::Rising, false).poll_unpin(cx);
    }

    /// Asynchronously wait until the pin sees a falling edge.
    ///
    /// If the pin is already low, it will wait for it to go high then back low.
    pub async fn wait_for_falling_edge(&mut self) {
        ExtiInputFuture::new(&self.pin, TriggerEdge::Falling, true).await
    }

    /// Asynchronously wait until the pin sees a falling edge.
    ///
    /// If the pin is already low, it will wait for it to go high then back low.
    pub fn poll_for_falling_edge<'a>(&mut self, cx: &mut Context<'a>) {
        let _ = ExtiInputFuture::new(&self.pin, TriggerEdge::Falling, false).poll_unpin(cx);
    }

    /// Asynchronously wait until the pin sees any edge (either rising or falling).
    pub async fn wait_for_any_edge(&mut self) {
        ExtiInputFuture::new(&self.pin, TriggerEdge::Any, true).await
    }

    /// Asynchronously wait until the pin sees any edge (either rising or falling).
    pub fn poll_for_any_edge<'a>(&mut self, cx: &mut Context<'a>) {
        let _ = ExtiInputFuture::new(&self.pin, TriggerEdge::Any, false).poll_unpin(cx);
    }
}

impl<'d> ExtiInput<'d, Blocking> {
    /// Creates a new EXTI input for use with manual interrupt handling.
    ///
    /// This configures and enables the EXTI interrupt for the pin, but does not
    /// provide async methods for waiting on events. You must provide your own
    /// interrupt handler to service EXTI events and clear pending flags.
    ///
    /// For async/await integration with Embassy's executor, use `ExtiInput<Async>` instead.
    ///
    /// # Arguments
    /// * `pin` - The GPIO pin to use
    /// * `ch` - The EXTI channel corresponding to the pin (consumed for ownership tracking)
    /// * `pull` - The pull configuration for the pin
    /// * `trigger_edge` - The edge triggering mode (falling, rising, or any)
    ///
    /// # Returns
    /// A new `ExtiInput` instance with interrupts enabled
    pub fn new_blocking<T: GpioPin + ExtiPin>(
        pin: Peri<'d, T>,
        _ch: Peri<'d, T::ExtiChannel>, // Consumed for ownership tracking
        pull: Pull,
        trigger_edge: TriggerEdge,
    ) -> Self {
        let pin = Input::new(pin, pull);

        low_level::configure_and_enable_exti(&pin, trigger_edge);

        Self {
            pin,
            _kind: PhantomData,
        }
    }

    /// Reconfigures the edge detection mode for this pin's EXTI line
    ///
    /// This method updates which edges (rising, falling, or any) will trigger
    /// interrupts for this pin.
    /// Note that reconfiguring the edge detection will clear any pending
    /// interrupt flag for this pin.
    pub fn set_edge_detection(&mut self, trigger_edge: TriggerEdge) {
        let pin_num = self.pin.pin.pin.pin();
        let port_num = self.pin.pin.pin.port();
        low_level::configure_exti_pin(pin_num, port_num, trigger_edge);
    }

    /// Enables the EXTI interrupt for this pin
    pub fn enable_interrupt(&mut self) {
        let pin_num = self.pin.pin.pin.pin();
        low_level::set_exti_interrupt_enabled(pin_num, InterruptState::Enabled);
    }

    /// Disables the EXTI interrupt for this pin
    pub fn disable_interrupt(&mut self) {
        let pin_num = self.pin.pin.pin.pin();
        low_level::set_exti_interrupt_enabled(pin_num, InterruptState::Disabled);
    }

    /// Clears any pending interrupt for this pin
    ///
    /// This method clears the pending interrupt flag for the EXTI line
    /// associated with this pin. This should typically be called from
    /// the interrupt handler after processing an interrupt.
    pub fn clear_pending(&mut self) {
        let pin_num = self.pin.pin.pin.pin();
        low_level::clear_exti_pending(pin_num);
    }

    /// Checks if an interrupt is pending for the current pin
    ///
    /// This method checks if there is a pending interrupt on the EXTI line
    /// associated with this pin.
    ///
    /// # Returns
    /// `true` if an interrupt is pending, `false` otherwise
    pub fn is_pending(&self) -> bool {
        let pin_num = self.pin.pin.pin.pin();
        low_level::is_exti_pending(pin_num)
    }

    fn pin_mask(&self) -> u32 {
        1 << self.pin.pin.pin.pin()
    }
}

impl<'d, Mode: PeriMode> embedded_hal_02::digital::v2::InputPin for ExtiInput<'d, Mode> {
    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, Mode: PeriMode> embedded_hal_1::digital::ErrorType for ExtiInput<'d, Mode> {
    type Error = Infallible;
}

impl<'d, Mode: PeriMode> embedded_hal_1::digital::InputPin for ExtiInput<'d, Mode> {
    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_async::digital::Wait for ExtiInput<'d, Async> {
    async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
        self.wait_for_high().await;
        Ok(())
    }

    async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
        self.wait_for_low().await;
        Ok(())
    }

    async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
        self.wait_for_rising_edge().await;
        Ok(())
    }

    async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
        self.wait_for_falling_edge().await;
        Ok(())
    }

    async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
        self.wait_for_any_edge().await;
        Ok(())
    }
}

#[must_use = "futures do nothing unless you `.await` or poll them"]
struct ExtiInputFuture<'a> {
    pin: PinNumber,
    drop: bool,
    phantom: PhantomData<&'a mut AnyPin>,
}

impl<'a> ExtiInputFuture<'a> {
    fn new(pin: &Input, trigger_edge: TriggerEdge, drop: bool) -> Self {
        low_level::configure_and_enable_exti(pin, trigger_edge);

        Self {
            pin: pin.pin.pin.pin(),
            drop,
            phantom: PhantomData,
        }
    }
}

impl<'a> Drop for ExtiInputFuture<'a> {
    fn drop(&mut self) {
        if self.drop {
            critical_section::with(|_| {
                let pin = self.pin as _;
                cpu_regs().imr(0).modify(|w| w.set_line(pin, false));
            });
        }
    }
}

impl<'a> Future for ExtiInputFuture<'a> {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        EXTI_WAKERS[self.pin as usize].register(cx.waker());

        let imr = cpu_regs().imr(0).read();
        if !imr.line(self.pin as _) {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

macro_rules! foreach_exti_irq {
    ($action:ident) => {
        foreach_interrupt!(
            (EXTI0)  => { $action!(EXTI0); };
            (EXTI1)  => { $action!(EXTI1); };
            (EXTI2)  => { $action!(EXTI2); };
            (EXTI3)  => { $action!(EXTI3); };
            (EXTI4)  => { $action!(EXTI4); };
            (EXTI5)  => { $action!(EXTI5); };
            (EXTI6)  => { $action!(EXTI6); };
            (EXTI7)  => { $action!(EXTI7); };
            (EXTI8)  => { $action!(EXTI8); };
            (EXTI9)  => { $action!(EXTI9); };
            (EXTI10) => { $action!(EXTI10); };
            (EXTI11) => { $action!(EXTI11); };
            (EXTI12) => { $action!(EXTI12); };
            (EXTI13) => { $action!(EXTI13); };
            (EXTI14) => { $action!(EXTI14); };
            (EXTI15) => { $action!(EXTI15); };

            // plus the weird ones
            (EXTI0_1)   => { $action!(EXTI0_1); };
            (EXTI15_10) => { $action!(EXTI15_10); };
            (EXTI15_4)  => { $action!(EXTI15_4); };
            (EXTI1_0)   => { $action!(EXTI1_0); };
            (EXTI2_3)   => { $action!(EXTI2_3); };
            (EXTI2_TSC) => { $action!(EXTI2_TSC); };
            (EXTI3_2)   => { $action!(EXTI3_2); };
            (EXTI4_15)  => { $action!(EXTI4_15); };
            (EXTI9_5)   => { $action!(EXTI9_5); };
        );
    };
}

///EXTI interrupt handler. All EXTI interrupt vectors should be bound to this handler.
///
/// It is generic over the [Interrupt](InterruptType) rather
/// than the [Channel] because it should not be bound multiple
/// times to the same vector on chips which multiplex multiple EXTI interrupts into one vector.
//
// It technically doesn't need to be generic at all, except to satisfy the generic argument
// of [Handler]. All EXTI interrupts eventually land in the same on_irq() function.
pub struct InterruptHandler<T: crate::interrupt::typelevel::Interrupt> {
    _phantom: PhantomData<T>,
}

impl<T: InterruptType> Handler<T> for InterruptHandler<T> {
    unsafe fn on_interrupt() {
        on_irq()
    }
}

trait SealedChannel {}

/// EXTI channel trait.
#[allow(private_bounds)]
pub trait Channel: PeripheralType + SealedChannel + Sized {
    /// EXTI channel number.
    fn number(&self) -> PinNumber;
    /// [Enum-level Interrupt](InterruptEnum), which may be the same for multiple channels.
    fn irq(&self) -> InterruptEnum;
    /// [Type-level Interrupt](InterruptType), which may be the same for multiple channels.
    type IRQ: InterruptType;
}

//Doc isn't hidden in order to surface the explanation to users, even though it's completely inoperable, not just deprecated.
//Entire type along with doc can probably be removed after deprecation has appeared in a release once.
/// Deprecated type-erased EXTI channel.
///
/// Support for AnyChannel was removed in order to support manually bindable EXTI interrupts via bind_interrupts; [ExtiInput::new()]
/// must know the required IRQ at compile time, and therefore cannot support type-erased channels.
#[deprecated = "type-erased EXTI channels are no longer supported, in order to support manually bindable EXTI interrupts (more info: https://github.com/embassy-rs/embassy/pull/4922)"]
pub struct AnyChannel {
    #[allow(unused)]
    number: PinNumber,
}

macro_rules! impl_exti {
    ($type:ident, $number:expr) => {
        impl SealedChannel for crate::peripherals::$type {}
        impl Channel for crate::peripherals::$type {
            fn number(&self) -> PinNumber {
                $number
            }
            fn irq(&self) -> InterruptEnum {
                crate::_generated::peripheral_interrupts::EXTI::$type::IRQ
            }
            type IRQ = crate::_generated::peripheral_interrupts::EXTI::$type;
        }

        //Still here to surface deprecation messages to the user - remove when removing AnyChannel
        #[allow(deprecated)]
        impl From<crate::peripherals::$type> for AnyChannel {
            fn from(_val: crate::peripherals::$type) -> Self {
                Self { number: $number }
            }
        }
    };
}

impl_exti!(EXTI0, 0);
impl_exti!(EXTI1, 1);
impl_exti!(EXTI2, 2);
impl_exti!(EXTI3, 3);
impl_exti!(EXTI4, 4);
impl_exti!(EXTI5, 5);
impl_exti!(EXTI6, 6);
impl_exti!(EXTI7, 7);
impl_exti!(EXTI8, 8);
impl_exti!(EXTI9, 9);
impl_exti!(EXTI10, 10);
impl_exti!(EXTI11, 11);
impl_exti!(EXTI12, 12);
impl_exti!(EXTI13, 13);
impl_exti!(EXTI14, 14);
impl_exti!(EXTI15, 15);

macro_rules! enable_irq {
    ($e:ident) => {
        crate::interrupt::typelevel::$e::enable();
    };
}

/// safety: must be called only once
pub(crate) unsafe fn init(_cs: critical_section::CriticalSection) {
    use crate::interrupt::typelevel::Interrupt;

    foreach_exti_irq!(enable_irq);
}