Skip to content

Commit d79fff0

Browse files
committed
Update gpio interrupt API
1 parent 17cbc88 commit d79fff0

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
8989
- `clear_events()` was added to clear all events at once.
9090
- `is_event_triggered()` can check if an `Event` is triggered.
9191
- `triggered_events` returns an `EnumSet` of triggered events.
92+
- Change gpio interrupt API to be more in line with the new serial interrupt
93+
API. ([#262])
9294

9395
## [v0.7.0] - 2021-06-18
9496

@@ -414,6 +416,7 @@ let clocks = rcc
414416
[defmt]: https://github.com/knurling-rs/defmt
415417
[filter]: https://defmt.ferrous-systems.com/filtering.html
416418

419+
[#262]: https://github.com/stm32-rs/stm32f3xx-hal/pull/262
417420
[#260]: https://github.com/stm32-rs/stm32f3xx-hal/pull/260
418421
[#259]: https://github.com/stm32-rs/stm32f3xx-hal/pull/259
419422
[#257]: https://github.com/stm32-rs/stm32f3xx-hal/pull/257

examples/gpio_interrupts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ fn EXTI0() {
8282
.borrow_mut()
8383
.as_mut()
8484
.unwrap()
85-
.clear_interrupt_pending_bit();
85+
.clear_interrupt();
8686
})
8787
}

src/gpio.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use crate::{
6767
pac::{Interrupt, EXTI},
6868
rcc::AHB,
6969
syscfg::SysCfg,
70+
Toggle,
7071
};
7172

7273
use crate::hal::digital::v2::{toggleable, InputPin, StatefulOutputPin};
@@ -582,7 +583,14 @@ where
582583
}
583584
}
584585

585-
/// Make corresponding EXTI line sensitive to this pin
586+
/// Make corresponding EXTI line sensitive to this pin.
587+
///
588+
/// # Note
589+
///
590+
/// Only **one** Pin index of all banks can be activated
591+
/// for interrupts simultainously.
592+
///
593+
/// For example, only on of `PA1`, `PB1`, `PC1`, ... can be activated.
586594
pub fn make_interrupt_source(&mut self, syscfg: &mut SysCfg) {
587595
let bitwidth = 4;
588596
let index = self.index.index() % 4;
@@ -611,29 +619,37 @@ where
611619
}
612620
}
613621

614-
/// Enable external interrupts from this pin
615-
pub fn enable_interrupt(&mut self, exti: &mut EXTI) {
622+
/// Configure external interrupts from this pin
623+
///
624+
/// Remeber to also configure the interrupt pin on
625+
/// the SysCfg site, with [`Pin::make_interrupt_source()`]
626+
pub fn configure_interrupt(&mut self, exti: &mut EXTI, enable: impl Into<Toggle>) {
627+
let enable: Toggle = enable.into();
628+
let enable: bool = enable.into();
629+
616630
let bitwidth = 1;
617631
let index = self.index.index();
618-
let value = 1;
632+
let value = u32::from(enable);
619633
unsafe { modify_at!(reg_for_cpu!(exti, imr), bitwidth, index, value) };
620634
}
621635

636+
/// Enable external interrupts from this pin
637+
pub fn enable_interrupt(&mut self, exti: &mut EXTI) {
638+
self.configure_interrupt(exti, Toggle::On)
639+
}
640+
622641
/// Disable external interrupts from this pin
623642
pub fn disable_interrupt(&mut self, exti: &mut EXTI) {
624-
let bitwidth = 1;
625-
let index = self.index.index();
626-
let value = 0;
627-
unsafe { modify_at!(reg_for_cpu!(exti, imr), bitwidth, index, value) };
643+
self.configure_interrupt(exti, Toggle::Off)
628644
}
629645

630646
/// Clear the interrupt pending bit for this pin
631-
pub fn clear_interrupt_pending_bit(&mut self) {
647+
pub fn clear_interrupt(&mut self) {
632648
unsafe { reg_for_cpu!((*EXTI::ptr()), pr).write(|w| w.bits(1 << self.index.index())) };
633649
}
634650

635651
/// Reads the interrupt pending bit for this pin
636-
pub fn check_interrupt(&self) -> bool {
652+
pub fn is_interrupt_pending(&self) -> bool {
637653
unsafe { reg_for_cpu!((*EXTI::ptr()), pr).read().bits() & (1 << self.index.index()) != 0 }
638654
}
639655
}

0 commit comments

Comments
 (0)