Created
November 19, 2020 14:39
-
-
Save lulf/7bc60c745a487e55f9cd4d6950a4501e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![no_main] | |
use nrf51_hal as hal; | |
use panic_halt as _; | |
#[macro_use(singleton)] | |
extern crate cortex_m; | |
use core::cell::Cell; | |
use core::cell::RefCell; | |
use core::sync::atomic::{AtomicUsize, Ordering}; | |
use cortex_m::interrupt::Mutex; | |
use cortex_m::peripheral; | |
use cortex_m_rt::entry; | |
use embedded_hal::digital::v2::OutputPin; | |
use hal::gpio::Level; | |
use hal::pac::interrupt; | |
use hal::rtc::{Rtc, RtcCompareReg, RtcInterrupt}; | |
use rtt_target::{rprintln, rtt_init_print}; | |
static COUNTER: Mutex<Cell<u32>> = Mutex::new(Cell::new(0)); | |
static RTC: Mutex<RefCell<Option<Rtc<hal::pac::RTC0>>>> = Mutex::new(RefCell::new(None)); | |
#[entry] | |
fn main() -> ! { | |
rtt_init_print!(); | |
let mut cp = peripheral::Peripherals::take().unwrap(); | |
let p = hal::pac::Peripherals::take().unwrap(); | |
let port0 = hal::gpio::p0::Parts::new(p.GPIO); | |
let mut led = port0.p0_13.into_push_pull_output(Level::Low); | |
let _ = port0.p0_04.into_push_pull_output(Level::Low); | |
let clocks = hal::clocks::Clocks::new(p.CLOCK).enable_ext_hfosc(); | |
let _clocks = clocks.start_lfclk(); | |
rprintln!("Starting RTC"); | |
let mut rtc = Rtc::new(p.RTC0, 0).unwrap(); | |
rtc.set_compare(RtcCompareReg::Compare0, 32_768 / 3) | |
.unwrap(); | |
rtc.enable_event(RtcInterrupt::Compare0); | |
rtc.enable_counter(); | |
rtc.enable_interrupt(RtcInterrupt::Compare0, Some(&mut cp.NVIC)); | |
cortex_m::interrupt::free(|cs| RTC.borrow(cs).replace(Some(rtc))); | |
unsafe { | |
cortex_m::interrupt::enable(); | |
} | |
loop { | |
let count = cortex_m::interrupt::free(|cs| COUNTER.borrow(cs).get()); | |
if count % 2 == 0 { | |
led.set_high().unwrap(); | |
} else { | |
led.set_low().unwrap(); | |
} | |
} | |
} | |
#[interrupt] | |
fn RTC0() { | |
cortex_m::interrupt::free(|cs| { | |
let rtc = RTC.borrow(cs).borrow(); | |
rtc.as_ref().unwrap().reset_event(RtcInterrupt::Compare0); | |
rtc.as_ref().unwrap().clear_counter(); | |
COUNTER.borrow(cs).set(COUNTER.borrow(cs).get() + 1) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment