|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +use cortex_m::asm; |
| 5 | +use cortex_m_rt::entry; |
| 6 | +use panic_rtt_target as _; |
| 7 | +use rtt_target::{rprintln, rtt_init_print}; |
| 8 | + |
| 9 | +use microbit::{ |
| 10 | + Board, |
| 11 | + hal::{ |
| 12 | + gpiote, |
| 13 | + pac::{self, interrupt}, |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +static mut GPIOTE_PERIPHERAL: Option<gpiote::Gpiote> = None; |
| 18 | + |
| 19 | +/// This "function" will be called when an interrupt is |
| 20 | +/// received. For now, just report and panic. |
| 21 | +#[interrupt] |
| 22 | +fn GPIOTE() { |
| 23 | + rprintln!("ouch"); |
| 24 | + // # Safety |
| 25 | + // Ideally, this ISR cannot be called until the global |
| 26 | + // has been properly initialized. This value is never |
| 27 | + // touched globally after that. |
| 28 | + unsafe { |
| 29 | + if let Some(ref mut gpiote) = GPIOTE_PERIPHERAL { |
| 30 | + let channel = gpiote.channel0(); |
| 31 | + channel.reset_events(); |
| 32 | + // panic!(); |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[entry] |
| 38 | +fn main() -> ! { |
| 39 | + rtt_init_print!(); |
| 40 | + |
| 41 | + let board = Board::take().unwrap(); |
| 42 | + let button_a = board.buttons.button_a.into_floating_input(); |
| 43 | + let gpiote = gpiote::Gpiote::new(board.GPIOTE); |
| 44 | + |
| 45 | + // Set up the GPIOTE to generate an interrupt when Button A is pressed (GPIO |
| 46 | + // wire goes low). |
| 47 | + let channel = gpiote.channel0(); |
| 48 | + channel |
| 49 | + .input_pin(&button_a.degrade()) |
| 50 | + .hi_to_lo() |
| 51 | + .enable_interrupt(); |
| 52 | + channel.reset_events(); |
| 53 | + // # Safety |
| 54 | + // GPIOTE cannot be used from interrupt handler yet, |
| 55 | + // since it's not turned on. (I think this is bogus; the |
| 56 | + // write is not guaranteed to complete before the end of |
| 57 | + // the function maybe? An `UnsafeCell` is probably needed |
| 58 | + // here, at bare minimum.) |
| 59 | + unsafe { GPIOTE_PERIPHERAL = Some(gpiote) }; |
| 60 | + |
| 61 | + // Set up the NVIC to handle GPIO interrupts. |
| 62 | + // # Safety |
| 63 | + // Interrupt handler is set up properly. |
| 64 | + unsafe { pac::NVIC::unmask(pac::Interrupt::GPIOTE) }; |
| 65 | + pac::NVIC::unpend(pac::Interrupt::GPIOTE); |
| 66 | + |
| 67 | + loop { |
| 68 | + // "wait for interrupt": CPU goes to sleep until an interrupt. |
| 69 | + asm::wfi(); |
| 70 | + } |
| 71 | +} |
0 commit comments