-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #427 from Kyrne/master
RTIC Monotonic for RTC and TIMER
- Loading branch information
Showing
24 changed files
with
714 additions
and
13 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
runner = "probe-run --chip nRF52840_xxAA" | ||
|
||
rustflags = [ | ||
"-C", "link-arg=--nmagic", | ||
] | ||
|
||
[build] | ||
target = "thumbv7em-none-eabi" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "monotonic-blinky" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Ivar Jönsson <ivajns-9@student.ltu.se>"] | ||
|
||
[dependencies] | ||
embedded-hal = "1.0.0" | ||
rtt-target = "0.5.0" | ||
panic-halt = "0.2.0" | ||
nrf52840-hal = { path = "../../nrf52840-hal/", features = ["rtic-monotonic"] } | ||
cortex-m-rtic = "1.1.4" | ||
fugit = "0.3.7" | ||
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] } | ||
|
||
[[bin]] | ||
name = "rtc" | ||
path = "src/rtc.rs" | ||
|
||
[[bin]] | ||
name = "timer" | ||
path = "src/timer.rs" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[default.probe] | ||
protocol = "Swd" | ||
|
||
[default.flashing] | ||
enabled = true | ||
halt_afterwards = false | ||
restore_unwritten_bytes = false | ||
do_chip_erase = false | ||
|
||
[default.reset] | ||
enabled = true | ||
halt_afterwards = false | ||
|
||
[default.general] | ||
chip = "nRF52840_xxAA" | ||
chip_descriptions = [] | ||
log_level = "WARN" | ||
connect_under_reset = false | ||
|
||
[default.rtt] | ||
enabled = true | ||
channels = [ | ||
] | ||
timeout = 3000 | ||
show_timestamps = true | ||
log_enabled = false | ||
log_path = "./logs" | ||
|
||
[default.gdb] | ||
enabled = false | ||
gdb_connection_string = "127.0.0.1:1337" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Monotonic demo | ||
|
||
This crate defines a minimal [`corex-m-rtic`](https://docs.rs/cortex-m-rtic/latest/rtic/)-app using the [`rtc`](../../nrf-hal-common/src/rtc.rs) or [`timer`](../../nrf-hal-common/src/timer.rs) | ||
for software task scheduling. This example shows how to use the different clocks and how to switch inbetween them. | ||
|
||
## How to run | ||
|
||
To run the example using the `rtc` | ||
```bash | ||
cargo embed --release --bin rtc | ||
``` | ||
To run the example using the `timer` | ||
```bash | ||
cargo embed --release --bin timer | ||
``` | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! A minimal blinky example using `MonotonicRtc`. | ||
#![no_main] | ||
#![no_std] | ||
|
||
use hal::pac; | ||
use nrf52840_hal as hal; | ||
use panic_halt as _; | ||
#[rtic::app(device = pac, dispatchers = [UARTE1])] | ||
mod app { | ||
use super::*; | ||
use cortex_m::asm; | ||
use embedded_hal::digital::{OutputPin, StatefulOutputPin}; | ||
use hal::{ | ||
gpio::{p0::Parts, Level, Output, Pin, PushPull}, | ||
monotonic::MonotonicRtc, | ||
}; | ||
use pac::RTC0; | ||
use rtt_target::{rprintln, rtt_init_print}; | ||
|
||
#[monotonic(binds = RTC0, default = true)] | ||
type MyMono = MonotonicRtc<RTC0, 32_768>; | ||
|
||
#[shared] | ||
struct Shared {} | ||
|
||
#[local] | ||
struct Local { | ||
led: Pin<Output<PushPull>>, | ||
} | ||
|
||
#[init] | ||
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { | ||
rtt_init_print!(); | ||
rprintln!("init"); | ||
|
||
let p0 = Parts::new(cx.device.P0); | ||
let led = p0.p0_13.into_push_pull_output(Level::High).degrade(); | ||
|
||
let clocks = hal::clocks::Clocks::new(cx.device.CLOCK); | ||
let clocks = clocks.start_lfclk(); | ||
// Will throw error if freq is invalid | ||
let mono = MyMono::new(cx.device.RTC0, &clocks).unwrap(); | ||
|
||
blink::spawn().ok(); | ||
(Shared {}, Local { led }, init::Monotonics(mono)) | ||
} | ||
|
||
#[idle] | ||
fn idle(_: idle::Context) -> ! { | ||
loop { | ||
rprintln!("idle"); | ||
// Put core to sleep until next interrupt | ||
asm::wfe(); | ||
} | ||
} | ||
|
||
#[task(local = [led])] | ||
fn blink(ctx: blink::Context) { | ||
rprintln!("Blink!"); | ||
let led = ctx.local.led; | ||
// Note this unwrap is safe since is_set_low is allways Ok | ||
if led.is_set_low().unwrap() { | ||
led.set_high().ok(); | ||
} else { | ||
led.set_low().ok(); | ||
} | ||
// spawn after current time + 1 second | ||
blink::spawn_after(fugit::ExtU32::millis(1000)).ok(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! A minimal blinky example using `MonotonicTimer`. | ||
#![no_main] | ||
#![no_std] | ||
|
||
use hal::pac; | ||
use nrf52840_hal as hal; | ||
use panic_halt as _; | ||
#[rtic::app(device = pac, dispatchers = [UARTE1])] | ||
mod app { | ||
use super::*; | ||
use cortex_m::asm; | ||
use embedded_hal::digital::{OutputPin, StatefulOutputPin}; | ||
use hal::{ | ||
gpio::{p0::Parts, Level, Output, Pin, PushPull}, | ||
monotonic::MonotonicTimer, | ||
}; | ||
use pac::TIMER0; | ||
use rtt_target::{rprintln, rtt_init_print}; | ||
|
||
#[monotonic(binds = TIMER0, default = true)] | ||
type MyMono = MonotonicTimer<TIMER0, 16_000_000>; | ||
|
||
#[shared] | ||
struct Shared {} | ||
|
||
#[local] | ||
struct Local { | ||
led: Pin<Output<PushPull>>, | ||
} | ||
|
||
#[init] | ||
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { | ||
rtt_init_print!(); | ||
rprintln!("init"); | ||
|
||
let p0 = Parts::new(cx.device.P0); | ||
let led = p0.p0_13.into_push_pull_output(Level::High).degrade(); | ||
|
||
// New does not exists for invalid frequencies | ||
let mono = MyMono::new(cx.device.TIMER0); | ||
blink::spawn().ok(); | ||
(Shared {}, Local { led }, init::Monotonics(mono)) | ||
} | ||
|
||
#[idle] | ||
fn idle(_: idle::Context) -> ! { | ||
loop { | ||
rprintln!("idle"); | ||
// Put core to sleep until next interrupt | ||
asm::wfe(); | ||
} | ||
} | ||
|
||
#[task(local = [led])] | ||
fn blink(ctx: blink::Context) { | ||
rprintln!("Blink!"); | ||
let led = ctx.local.led; | ||
// Note this unwrap is safe since is_set_low is allways Ok | ||
if led.is_set_low().unwrap() { | ||
led.set_high().ok(); | ||
} else { | ||
led.set_low().ok(); | ||
} | ||
// spawn after current time + 1 second | ||
blink::spawn_after(fugit::ExtU32::millis(1000)).ok(); | ||
} | ||
} |
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
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
Oops, something went wrong.