Skip to content

Commit dd1cebb

Browse files
authored
Merge pull request #64 from jonas-schievink/exti
Rewrite the EXTI interface
2 parents 777aa51 + 4301d80 commit dd1cebb

File tree

10 files changed

+346
-160
lines changed

10 files changed

+346
-160
lines changed

examples/button_irq.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ use cortex_m::interrupt::Mutex;
1111
use cortex_m::peripheral::NVIC;
1212
use cortex_m_rt::entry;
1313
use stm32l0xx_hal::{
14-
exti::TriggerEdge,
14+
exti::{TriggerEdge, line::{GpioLine, ExtiLine}},
1515
gpio::*,
1616
pac::{self, interrupt, Interrupt, EXTI},
1717
prelude::*,
1818
rcc::Config,
1919
syscfg::SYSCFG,
2020
};
2121

22-
static INT: Mutex<RefCell<Option<EXTI>>> = Mutex::new(RefCell::new(None));
2322
static LED: Mutex<RefCell<Option<gpiob::PB6<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));
2423

2524
#[entry]
@@ -42,13 +41,13 @@ fn main() -> ! {
4241
let mut syscfg = SYSCFG::new(dp.SYSCFG, &mut rcc);
4342

4443
// Configure the external interrupt on the falling edge for the pin 0.
45-
let exti = dp.EXTI;
46-
exti.listen(&mut syscfg, button.port(), button.pin_number(), TriggerEdge::Falling);
44+
let line = GpioLine::from_raw_line(button.pin_number()).unwrap();
45+
let mut exti = dp.EXTI;
46+
exti.listen_gpio(&mut syscfg, button.port(), line, TriggerEdge::Falling);
4747

4848
// Store the external interrupt and LED in mutex reffcells to make them
4949
// available from the interrupt.
5050
cortex_m::interrupt::free(|cs| {
51-
*INT.borrow(cs).borrow_mut() = Some(exti);
5251
*LED.borrow(cs).borrow_mut() = Some(led);
5352
});
5453

@@ -66,19 +65,17 @@ fn EXTI2_3() {
6665
static mut STATE: bool = false;
6766

6867
cortex_m::interrupt::free(|cs| {
69-
if let Some(ref mut exti) = INT.borrow(cs).borrow_mut().deref_mut() {
70-
// Clear the interrupt flag.
71-
exti.clear_irq(2);
72-
73-
// Change the LED state on each interrupt.
74-
if let Some(ref mut led) = LED.borrow(cs).borrow_mut().deref_mut() {
75-
if *STATE {
76-
led.set_low().unwrap();
77-
*STATE = false;
78-
} else {
79-
led.set_high().unwrap();
80-
*STATE = true;
81-
}
68+
// Clear the interrupt flag.
69+
EXTI::unpend(GpioLine::from_raw_line(2).unwrap());
70+
71+
// Change the LED state on each interrupt.
72+
if let Some(ref mut led) = LED.borrow(cs).borrow_mut().deref_mut() {
73+
if *STATE {
74+
led.set_low().unwrap();
75+
*STATE = false;
76+
} else {
77+
led.set_high().unwrap();
78+
*STATE = true;
8279
}
8380
}
8481
});

examples/button_irq_rtfm.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
extern crate panic_halt;
77

88
use rtfm::app;
9-
use stm32l0xx_hal::{exti::TriggerEdge, gpio::*, pac, prelude::*, rcc::Config, syscfg::SYSCFG};
9+
use stm32l0xx_hal::{
10+
exti::{TriggerEdge, EXTI, line::{GpioLine, ExtiLine}},
11+
gpio::*,
12+
pac,
13+
prelude::*,
14+
rcc::Config,
15+
syscfg::SYSCFG,
16+
};
1017

1118
#[app(device = stm32l0xx_hal::pac)]
1219
const APP: () = {
@@ -31,8 +38,9 @@ const APP: () = {
3138
let mut syscfg = SYSCFG::new(device.SYSCFG, &mut rcc);
3239

3340
// Configure the external interrupt on the falling edge for the pin 0.
34-
let exti = device.EXTI;
35-
exti.listen(&mut syscfg, button.port(), button.pin_number(), TriggerEdge::Falling);
41+
let line = GpioLine::from_raw_line(button.pin_number()).unwrap();
42+
let mut exti = device.EXTI;
43+
exti.listen_gpio(&mut syscfg, button.port(), line, TriggerEdge::Falling);
3644

3745
// Return the initialised resources.
3846
init::LateResources {
@@ -46,7 +54,7 @@ const APP: () = {
4654
static mut STATE: bool = false;
4755

4856
// Clear the interrupt flag.
49-
resources.INT.clear_irq(0);
57+
EXTI::unpend(GpioLine::from_raw_line(0).unwrap());
5058

5159
// Change the LED state on each interrupt.
5260
if *STATE {

examples/exti_wakeup.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate panic_halt;
55

66
use cortex_m_rt::entry;
77
use stm32l0xx_hal::{
8-
exti, pac,
8+
exti::{self, line::{GpioLine, ExtiLine}}, pac,
99
prelude::*,
1010
pwr::{self, PWR},
1111
rcc::Config,
@@ -31,16 +31,18 @@ fn main() -> ! {
3131

3232
let mut syscfg = SYSCFG::new(dp.SYSCFG, &mut rcc);
3333

34-
exti.listen(
34+
let line = GpioLine::from_raw_line(button.pin_number()).unwrap();
35+
36+
exti.listen_gpio(
3537
&mut syscfg,
3638
button.port(),
37-
button.pin_number(),
39+
line,
3840
exti::TriggerEdge::Falling,
3941
);
4042

4143
loop {
4244
exti.wait_for_irq(
43-
button.pin_number(),
45+
line,
4446
pwr.stop_mode(
4547
&mut scb,
4648
&mut rcc,

examples/lptim.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use cortex_m::{asm, peripheral::NVIC};
1111
use cortex_m_rt::entry;
1212
use stm32l0xx_hal::{
1313
prelude::*,
14-
exti,
14+
exti::{
15+
line::DirectLine,
16+
},
1517
gpio::{
16-
self,
1718
Output,
1819
PushPull,
1920
gpiob::PB,
@@ -23,13 +24,15 @@ use stm32l0xx_hal::{
2324
LpTimer,
2425
ClockSrc,
2526
},
26-
pac,
27+
pac::{
28+
self,
29+
EXTI,
30+
},
2731
pwr::{
2832
self,
2933
PWR,
3034
},
3135
rcc,
32-
syscfg::SYSCFG,
3336
};
3437

3538

@@ -46,21 +49,15 @@ fn main() -> ! {
4649

4750
let mut led = gpiob.pb2.into_push_pull_output().downgrade();
4851

49-
let mut syscfg = SYSCFG::new(dp.SYSCFG, &mut rcc);
5052
let mut lptim = LpTimer::init_periodic(dp.LPTIM, &mut pwr, &mut rcc, ClockSrc::Lse);
5153

52-
let exti_line = 29; // LPTIM1 wakeup
54+
let exti_line = DirectLine::Lptim1;
5355

5456
lptim.enable_interrupts(lptim::Interrupts {
5557
autoreload_match: true,
5658
..lptim::Interrupts::default()
5759
});
58-
exti.listen(
59-
&mut syscfg,
60-
gpio::Port::PA, // argument ignored; next argument is not a GPIO line
61-
exti_line,
62-
exti::TriggerEdge::Rising,
63-
);
60+
exti.listen_direct(exti_line);
6461

6562
// Blink twice to signal the start of the program
6663
blink(&mut led);
@@ -70,7 +67,7 @@ fn main() -> ! {
7067
lptim.start(1.hz());
7168
block!(lptim.wait()).unwrap();
7269

73-
exti.clear_irq(exti_line);
70+
EXTI::unpend(exti_line);
7471
NVIC::unpend(pac::Interrupt::LPTIM1);
7572

7673
blink(&mut led);
@@ -79,8 +76,8 @@ fn main() -> ! {
7976
pwr.enter_low_power_run_mode(rcc.clocks);
8077
block!(lptim.wait()).unwrap();
8178
pwr.exit_low_power_run_mode();
82-
exti.clear_irq(exti_line);
83-
cortex_m::peripheral::NVIC::unpend(pac::Interrupt::LPTIM1);
79+
EXTI::unpend(exti_line);
80+
NVIC::unpend(pac::Interrupt::LPTIM1);
8481

8582
blink(&mut led);
8683

@@ -90,8 +87,8 @@ fn main() -> ! {
9087
pwr.sleep_mode(&mut scb),
9188
);
9289
lptim.wait().unwrap(); // returns immediately; we just got the interrupt
93-
exti.clear_irq(exti_line);
94-
cortex_m::peripheral::NVIC::unpend(pac::Interrupt::LPTIM1);
90+
EXTI::unpend(exti_line);
91+
NVIC::unpend(pac::Interrupt::LPTIM1);
9592

9693
blink(&mut led);
9794

@@ -101,8 +98,8 @@ fn main() -> ! {
10198
pwr.low_power_sleep_mode(&mut scb, &mut rcc),
10299
);
103100
lptim.wait().unwrap(); // returns immediately; we just got the interrupt
104-
exti.clear_irq(exti_line);
105-
cortex_m::peripheral::NVIC::unpend(pac::Interrupt::LPTIM1);
101+
EXTI::unpend(exti_line);
102+
NVIC::unpend(pac::Interrupt::LPTIM1);
106103

107104
blink(&mut led);
108105

@@ -122,7 +119,7 @@ fn main() -> ! {
122119
blink(&mut led);
123120

124121
// 1 second of standby mode
125-
cortex_m::peripheral::NVIC::unpend(pac::Interrupt::LPTIM1);
122+
NVIC::unpend(pac::Interrupt::LPTIM1);
126123
exti.wait_for_irq(
127124
exti_line,
128125
pwr.standby_mode(&mut scb),

examples/pwr.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
extern crate panic_halt;
66

77

8-
use cortex_m::asm;
8+
use cortex_m::{asm, peripheral::NVIC};
99
use cortex_m_rt::entry;
1010
use stm32l0xx_hal::{
1111
prelude::*,
12-
exti,
13-
gpio::{
12+
exti::{
1413
self,
14+
line::ConfigurableLine,
15+
},
16+
gpio::{
1517
Output,
1618
PushPull,
1719
gpiob::PB,
1820
},
19-
pac,
21+
pac::{
22+
self,
23+
EXTI,
24+
},
2025
pwr::{
2126
self,
2227
PWR,
@@ -27,7 +32,6 @@ use stm32l0xx_hal::{
2732
Instant,
2833
RTC,
2934
},
30-
syscfg::SYSCFG,
3135
};
3236

3337

@@ -44,8 +48,6 @@ fn main() -> ! {
4448

4549
let mut led = gpiob.pb2.into_push_pull_output().downgrade();
4650

47-
let mut syscfg = SYSCFG::new(dp.SYSCFG, &mut rcc);
48-
4951
let instant = Instant::new()
5052
.set_year(19)
5153
.set_month(9)
@@ -56,15 +58,13 @@ fn main() -> ! {
5658

5759
let mut rtc = RTC::new(dp.RTC, &mut rcc, &mut pwr, instant);
5860

59-
let exti_line = 20; // RTC wakeup timer
61+
let exti_line = ConfigurableLine::RtcWakeup;
6062

6163
rtc.enable_interrupts(rtc::Interrupts {
6264
wakeup_timer: true,
6365
..rtc::Interrupts::default()
6466
});
65-
exti.listen(
66-
&mut syscfg,
67-
gpio::Port::PA, // argument ignored; next argument is not a GPIO line
67+
exti.listen_configurable(
6868
exti_line,
6969
exti::TriggerEdge::Rising,
7070
);
@@ -78,17 +78,17 @@ fn main() -> ! {
7878
// 5 seconds of regular run mode
7979
timer.start(5u32);
8080
while let Err(nb::Error::WouldBlock) = timer.wait() {}
81-
exti.clear_irq(exti_line);
82-
cortex_m::peripheral::NVIC::unpend(pac::Interrupt::RTC);
81+
EXTI::unpend(exti_line);
82+
NVIC::unpend(pac::Interrupt::RTC);
8383

8484
blink(&mut led);
8585

8686
// 5 seconds of low-power run mode
8787
pwr.enter_low_power_run_mode(rcc.clocks);
8888
while let Err(nb::Error::WouldBlock) = timer.wait() {}
8989
pwr.exit_low_power_run_mode();
90-
exti.clear_irq(exti_line);
91-
cortex_m::peripheral::NVIC::unpend(pac::Interrupt::RTC);
90+
EXTI::unpend(exti_line);
91+
NVIC::unpend(pac::Interrupt::RTC);
9292

9393
blink(&mut led);
9494

examples/rtc_wakeup.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ extern crate panic_halt;
55

66
use cortex_m_rt::entry;
77
use stm32l0xx_hal::{
8-
exti, gpio, pac,
8+
exti::{self, line::ConfigurableLine}, pac,
99
prelude::*,
1010
pwr::PWR,
1111
rcc,
1212
rtc::{self, Instant, RTC},
13-
syscfg::SYSCFG,
1413
};
1514

1615
#[entry]
@@ -22,8 +21,8 @@ fn main() -> ! {
2221

2322
// Initialize all the GPIO we need
2423
let gpiob = dp.GPIOB.split(&mut rcc);
25-
let mut led = gpiob.pb2.into_push_pull_output();
26-
let button = gpiob.pb5.into_pull_down_input();
24+
let mut led = gpiob.pb6.into_push_pull_output();
25+
let button = gpiob.pb2.into_pull_down_input();
2726

2827
// Enable LED to signal that MCU is running
2928
led.set_high().unwrap();
@@ -32,8 +31,6 @@ fn main() -> ! {
3231
let mut exti = dp.EXTI;
3332
let mut pwr = PWR::new(dp.PWR, &mut rcc);
3433

35-
let mut syscfg = SYSCFG::new(dp.SYSCFG, &mut rcc);
36-
3734
let instant = Instant::new()
3835
.set_year(19)
3936
.set_month(8)
@@ -44,15 +41,13 @@ fn main() -> ! {
4441

4542
let mut rtc = RTC::new(dp.RTC, &mut rcc, &mut pwr, instant);
4643

47-
let exti_line = 20; // RTC wakeup timer
44+
let exti_line = ConfigurableLine::RtcWakeup;
4845

4946
rtc.enable_interrupts(rtc::Interrupts {
5047
wakeup_timer: true,
5148
..rtc::Interrupts::default()
5249
});
53-
exti.listen(
54-
&mut syscfg,
55-
gpio::Port::PA, // argument ignored; next argument is not a GPIO line
50+
exti.listen_configurable(
5651
exti_line,
5752
exti::TriggerEdge::Rising,
5853
);

src/bb.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)