Skip to content

Instantly share code, notes, and snippets.

@daschl
Created December 2, 2020 18:32
Show Gist options
  • Save daschl/afd7de26f87b004c695f3a1ec48ca06a to your computer and use it in GitHub Desktop.
Save daschl/afd7de26f87b004c695f3a1ec48ca06a to your computer and use it in GitHub Desktop.
#![no_main]
#![cfg_attr(not(test), no_std)]
#[allow(unused_imports)]
use defmt_rtt as _;
#[allow(unused_imports)]
use nrf52840_hal as _;
use nrf52840_hal::gpio::{Level, Output, Pin, PushPull};
use nrf52840_hal::prelude::OutputPin;
use core::sync::atomic::{AtomicUsize, Ordering};
use nrf52840_hal::clocks::HFCLK_FREQ as CPU_FREQ;
use nrf52840_hal::gpio::p1;
use nrf52840_hal::prelude::StatefulOutputPin;
use panic_probe as _;
use rtic::cyccnt::U32Ext;
const ONE_SECOND: u32 = CPU_FREQ; // 1s => CPU runs at 64 mhz
#[rtic::app(device = nrf52840_hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
const APP: () = {
struct Resources {
led: Pin<Output<PushPull>>,
}
#[init(spawn = [blinky])]
fn init(ctx: init::Context) -> init::LateResources {
let port1 = p1::Parts::new(ctx.device.P1);
let led = port1.p1_10.into_push_pull_output(Level::High).degrade();
ctx.spawn.blinky().unwrap();
init::LateResources { led }
}
#[task(schedule = [blinky], resources = [led])]
fn blinky(ctx: blinky::Context) {
defmt::info!("Blinky!");
if ctx.resources.led.is_set_high().unwrap() {
ctx.resources.led.set_low().unwrap();
} else {
ctx.resources.led.set_high().unwrap();
}
ctx.schedule
.blinky(ctx.scheduled + ONE_SECOND.cycles())
.unwrap();
}
#[idle]
fn idle(_: idle::Context) -> ! {
loop {
cortex_m::asm::wfi();
}
}
extern "C" {
fn SWI0_EGU0();
}
};
#[defmt::timestamp]
fn timestamp() -> u64 {
static COUNT: AtomicUsize = AtomicUsize::new(0);
// NOTE(no-CAS) `timestamps` runs with interrupts disabled
let n = COUNT.load(Ordering::Relaxed);
COUNT.store(n + 1, Ordering::Relaxed);
n as u64
}
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment