diff options
| author | Ulf Lilleengen <[email protected]> | 2024-05-18 10:17:03 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2024-05-21 10:05:21 +0200 |
| commit | 739e5861c2e47db251725163fcd91cd822cf97b7 (patch) | |
| tree | 947dc7961ca7c42ec216056fc2adf616ab812b10 /docs/examples/layer-by-layer/blinky-irq/src | |
| parent | 51d553092550059afb22b2620cea14bbed21abff (diff) | |
convert from antora to asciidoctor
Diffstat (limited to 'docs/examples/layer-by-layer/blinky-irq/src')
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-irq/src/main.rs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/docs/examples/layer-by-layer/blinky-irq/src/main.rs b/docs/examples/layer-by-layer/blinky-irq/src/main.rs new file mode 100644 index 000000000..743c9d99b --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-irq/src/main.rs | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use core::cell::RefCell; | ||
| 5 | |||
| 6 | use cortex_m::interrupt::Mutex; | ||
| 7 | use cortex_m::peripheral::NVIC; | ||
| 8 | use cortex_m_rt::entry; | ||
| 9 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 10 | use embassy_stm32::{interrupt, pac}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | static BUTTON: Mutex<RefCell<Option<Input<'static>>>> = Mutex::new(RefCell::new(None)); | ||
| 14 | static LED: Mutex<RefCell<Option<Output<'static>>>> = Mutex::new(RefCell::new(None)); | ||
| 15 | |||
| 16 | #[entry] | ||
| 17 | fn main() -> ! { | ||
| 18 | let p = embassy_stm32::init(Default::default()); | ||
| 19 | let led = Output::new(p.PB14, Level::Low, Speed::Low); | ||
| 20 | let mut button = Input::new(p.PC13, Pull::Up); | ||
| 21 | |||
| 22 | cortex_m::interrupt::free(|cs| { | ||
| 23 | enable_interrupt(&mut button); | ||
| 24 | |||
| 25 | LED.borrow(cs).borrow_mut().replace(led); | ||
| 26 | BUTTON.borrow(cs).borrow_mut().replace(button); | ||
| 27 | |||
| 28 | unsafe { NVIC::unmask(pac::Interrupt::EXTI15_10) }; | ||
| 29 | }); | ||
| 30 | |||
| 31 | loop { | ||
| 32 | cortex_m::asm::wfe(); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | #[interrupt] | ||
| 37 | fn EXTI15_10() { | ||
| 38 | cortex_m::interrupt::free(|cs| { | ||
| 39 | let mut button = BUTTON.borrow(cs).borrow_mut(); | ||
| 40 | let button = button.as_mut().unwrap(); | ||
| 41 | |||
| 42 | let mut led = LED.borrow(cs).borrow_mut(); | ||
| 43 | let led = led.as_mut().unwrap(); | ||
| 44 | if check_interrupt(button) { | ||
| 45 | if button.is_low() { | ||
| 46 | led.set_high(); | ||
| 47 | } else { | ||
| 48 | led.set_low(); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | clear_interrupt(button); | ||
| 52 | }); | ||
| 53 | } | ||
| 54 | // | ||
| 55 | // | ||
| 56 | // | ||
| 57 | // | ||
| 58 | // | ||
| 59 | // | ||
| 60 | // "Hidden" HAL-like methods for doing interrupts with embassy. Hardcode pin just to give audience an idea of what it looks like | ||
| 61 | |||
| 62 | const PORT: u8 = 2; | ||
| 63 | const PIN: usize = 13; | ||
| 64 | fn check_interrupt(_pin: &mut Input<'static>) -> bool { | ||
| 65 | let exti = pac::EXTI; | ||
| 66 | let pin = PIN; | ||
| 67 | let lines = exti.pr(0).read(); | ||
| 68 | lines.line(pin) | ||
| 69 | } | ||
| 70 | |||
| 71 | fn clear_interrupt(_pin: &mut Input<'static>) { | ||
| 72 | let exti = pac::EXTI; | ||
| 73 | let pin = PIN; | ||
| 74 | let mut lines = exti.pr(0).read(); | ||
| 75 | lines.set_line(pin, true); | ||
| 76 | exti.pr(0).write_value(lines); | ||
| 77 | } | ||
| 78 | |||
| 79 | fn enable_interrupt(_pin: &mut Input<'static>) { | ||
| 80 | cortex_m::interrupt::free(|_| { | ||
| 81 | let rcc = pac::RCC; | ||
| 82 | rcc.apb2enr().modify(|w| w.set_syscfgen(true)); | ||
| 83 | |||
| 84 | let port = PORT; | ||
| 85 | let pin = PIN; | ||
| 86 | let syscfg = pac::SYSCFG; | ||
| 87 | let exti = pac::EXTI; | ||
| 88 | syscfg.exticr(pin / 4).modify(|w| w.set_exti(pin % 4, port)); | ||
| 89 | exti.imr(0).modify(|w| w.set_line(pin, true)); | ||
| 90 | exti.rtsr(0).modify(|w| w.set_line(pin, true)); | ||
| 91 | exti.ftsr(0).modify(|w| w.set_line(pin, true)); | ||
| 92 | }); | ||
| 93 | } | ||
