|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +#[cfg(not(any( |
| 5 | + feature = "51", |
| 6 | + feature = "52810", |
| 7 | + feature = "52832", |
| 8 | + feature = "52833", |
| 9 | + feature = "52840" |
| 10 | +)))] |
| 11 | +compile_error!( |
| 12 | + "This example requires one of the following device features enabled: |
| 13 | + 51 |
| 14 | + 52810 |
| 15 | + 52832 |
| 16 | + 52833 |
| 17 | + 52840" |
| 18 | +); |
| 19 | + |
| 20 | +// Import the right HAL/PAC crate, depending on the target chip |
| 21 | +#[cfg(feature = "51")] |
| 22 | +pub use nrf51_hal as hal; |
| 23 | +#[cfg(feature = "52810")] |
| 24 | +pub use nrf52810_hal as hal; |
| 25 | +#[cfg(feature = "52832")] |
| 26 | +pub use nrf52832_hal as hal; |
| 27 | +#[cfg(feature = "52833")] |
| 28 | +pub use nrf52833_hal as hal; |
| 29 | +#[cfg(feature = "52840")] |
| 30 | +pub use nrf52840_hal as hal; |
| 31 | + |
| 32 | +use { |
| 33 | + core::{ |
| 34 | + cell::RefCell, |
| 35 | + panic::PanicInfo, |
| 36 | + sync::atomic::{compiler_fence, Ordering}, |
| 37 | + }, |
| 38 | + cortex_m::interrupt::Mutex, |
| 39 | + cortex_m_rt::entry, |
| 40 | + hal::{ |
| 41 | + pac::{interrupt, Interrupt, RADIO}, |
| 42 | + ppi, |
| 43 | + prelude::*, |
| 44 | + timer::Timer, |
| 45 | + Clocks, |
| 46 | + }, |
| 47 | + rtt_target::{rprintln, rtt_init_print}, |
| 48 | +}; |
| 49 | + |
| 50 | +static RADIO_REGS: Mutex<RefCell<Option<RADIO>>> = Mutex::new(RefCell::new(None)); |
| 51 | + |
| 52 | +#[entry] |
| 53 | +fn main() -> ! { |
| 54 | + let p = hal::pac::Peripherals::take().unwrap(); |
| 55 | + |
| 56 | + let _clocks = Clocks::new(p.CLOCK).enable_ext_hfosc(); |
| 57 | + rtt_init_print!(); |
| 58 | + |
| 59 | + let ppi_channels = ppi::Parts::new(p.PPI); |
| 60 | + let mut channel0 = ppi_channels.ppi0; |
| 61 | + |
| 62 | + channel0.set_task_endpoint(&p.RADIO.tasks_disable as *const _ as u32); |
| 63 | + channel0.set_event_endpoint(&p.TIMER0.events_compare[0] as *const _ as u32); |
| 64 | + channel0.enable(); |
| 65 | + |
| 66 | + let radio = p.RADIO; |
| 67 | + radio.intenset.write(|w| w.disabled().set()); |
| 68 | + cortex_m::interrupt::free(|cs| RADIO_REGS.borrow(cs).replace(Some(radio))); |
| 69 | + // NOTE(unsafe) There isn't any abstraction depending on this interrupt being masked |
| 70 | + unsafe { |
| 71 | + cortex_m::peripheral::NVIC::unmask(Interrupt::RADIO); |
| 72 | + } |
| 73 | + |
| 74 | + let mut timer = Timer::one_shot(p.TIMER0); |
| 75 | + timer.start(0xFFFFu32); |
| 76 | + |
| 77 | + loop { |
| 78 | + // Prevent empty loop optimizations |
| 79 | + compiler_fence(Ordering::SeqCst); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[interrupt] |
| 84 | +fn RADIO() { |
| 85 | + cortex_m::interrupt::free(|cs| { |
| 86 | + if let Some(regs) = RADIO_REGS.borrow(cs).borrow_mut().as_mut() { |
| 87 | + if regs.events_disabled.read().bits() == 1 { |
| 88 | + rprintln!("We hit the RADIO disabled interrupt"); |
| 89 | + |
| 90 | + // Clear the disabled flag |
| 91 | + // NOTE(unsafe) 0 is a valid value to write to this register |
| 92 | + regs.events_disabled.write(|w| unsafe { w.bits(0) }); |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +#[inline(never)] |
| 99 | +#[panic_handler] |
| 100 | +fn panic(info: &PanicInfo) -> ! { |
| 101 | + cortex_m::interrupt::disable(); |
| 102 | + rprintln!("{}", info); |
| 103 | + loop { |
| 104 | + compiler_fence(Ordering::SeqCst); |
| 105 | + } |
| 106 | +} |
0 commit comments