Skip to content

Commit 090ce0e

Browse files
authored
Merge pull request #427 from Kyrne/master
RTIC Monotonic for RTC and TIMER
2 parents eb8b34a + 4c00992 commit 090ce0e

File tree

24 files changed

+714
-13
lines changed

24 files changed

+714
-13
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
## Unreleased
44

5-
- add support for nRF52805
5+
### New features
6+
7+
- Added support for nRF52805 ([#422]).
8+
- Added implementation of RTIC `Monotonic` for RTC and TIMER, behind new `monotonic` feature ([#427]).
9+
10+
[#422]: https://github.com/nrf-rs/nrf-hal/pull/422
11+
[#427]: https://github.com/nrf-rs/nrf-hal/pull/427
612

713
## [0.17.1]
814

915
### New features
1016

11-
- Implemented `embedded-hal` 1.0 `I2c` trait for `Twi` and `Twim` [#440].
17+
- Implemented `embedded-hal` 1.0 `I2c` trait for `Twi` and `Twim` ([#440]).
1218

1319
[#440]: https://github.com/nrf-rs/nrf-hal/pull/440
1420

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2+
runner = "probe-run --chip nRF52840_xxAA"
3+
4+
rustflags = [
5+
"-C", "link-arg=--nmagic",
6+
]
7+
8+
[build]
9+
target = "thumbv7em-none-eabi"
10+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "monotonic-blinky"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Ivar Jönsson <[email protected]>"]
6+
7+
[dependencies]
8+
embedded-hal = "1.0.0"
9+
rtt-target = "0.5.0"
10+
panic-halt = "0.2.0"
11+
nrf52840-hal = { path = "../../nrf52840-hal/", features = ["rtic-monotonic"] }
12+
cortex-m-rtic = "1.1.4"
13+
fugit = "0.3.7"
14+
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
15+
16+
[[bin]]
17+
name = "rtc"
18+
path = "src/rtc.rs"
19+
20+
[[bin]]
21+
name = "timer"
22+
path = "src/timer.rs"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[default.probe]
2+
protocol = "Swd"
3+
4+
[default.flashing]
5+
enabled = true
6+
halt_afterwards = false
7+
restore_unwritten_bytes = false
8+
do_chip_erase = false
9+
10+
[default.reset]
11+
enabled = true
12+
halt_afterwards = false
13+
14+
[default.general]
15+
chip = "nRF52840_xxAA"
16+
chip_descriptions = []
17+
log_level = "WARN"
18+
connect_under_reset = false
19+
20+
[default.rtt]
21+
enabled = true
22+
channels = [
23+
]
24+
timeout = 3000
25+
show_timestamps = true
26+
log_enabled = false
27+
log_path = "./logs"
28+
29+
[default.gdb]
30+
enabled = false
31+
gdb_connection_string = "127.0.0.1:1337"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Monotonic demo
2+
3+
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)
4+
for software task scheduling. This example shows how to use the different clocks and how to switch inbetween them.
5+
6+
## How to run
7+
8+
To run the example using the `rtc`
9+
```bash
10+
cargo embed --release --bin rtc
11+
```
12+
To run the example using the `timer`
13+
```bash
14+
cargo embed --release --bin timer
15+
```
16+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//! A minimal blinky example using `MonotonicRtc`.
2+
#![no_main]
3+
#![no_std]
4+
5+
use hal::pac;
6+
use nrf52840_hal as hal;
7+
use panic_halt as _;
8+
#[rtic::app(device = pac, dispatchers = [UARTE1])]
9+
mod app {
10+
use super::*;
11+
use cortex_m::asm;
12+
use embedded_hal::digital::{OutputPin, StatefulOutputPin};
13+
use hal::{
14+
gpio::{p0::Parts, Level, Output, Pin, PushPull},
15+
monotonic::MonotonicRtc,
16+
};
17+
use pac::RTC0;
18+
use rtt_target::{rprintln, rtt_init_print};
19+
20+
#[monotonic(binds = RTC0, default = true)]
21+
type MyMono = MonotonicRtc<RTC0, 32_768>;
22+
23+
#[shared]
24+
struct Shared {}
25+
26+
#[local]
27+
struct Local {
28+
led: Pin<Output<PushPull>>,
29+
}
30+
31+
#[init]
32+
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
33+
rtt_init_print!();
34+
rprintln!("init");
35+
36+
let p0 = Parts::new(cx.device.P0);
37+
let led = p0.p0_13.into_push_pull_output(Level::High).degrade();
38+
39+
let clocks = hal::clocks::Clocks::new(cx.device.CLOCK);
40+
let clocks = clocks.start_lfclk();
41+
// Will throw error if freq is invalid
42+
let mono = MyMono::new(cx.device.RTC0, &clocks).unwrap();
43+
44+
blink::spawn().ok();
45+
(Shared {}, Local { led }, init::Monotonics(mono))
46+
}
47+
48+
#[idle]
49+
fn idle(_: idle::Context) -> ! {
50+
loop {
51+
rprintln!("idle");
52+
// Put core to sleep until next interrupt
53+
asm::wfe();
54+
}
55+
}
56+
57+
#[task(local = [led])]
58+
fn blink(ctx: blink::Context) {
59+
rprintln!("Blink!");
60+
let led = ctx.local.led;
61+
// Note this unwrap is safe since is_set_low is allways Ok
62+
if led.is_set_low().unwrap() {
63+
led.set_high().ok();
64+
} else {
65+
led.set_low().ok();
66+
}
67+
// spawn after current time + 1 second
68+
blink::spawn_after(fugit::ExtU32::millis(1000)).ok();
69+
}
70+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//! A minimal blinky example using `MonotonicTimer`.
2+
#![no_main]
3+
#![no_std]
4+
5+
use hal::pac;
6+
use nrf52840_hal as hal;
7+
use panic_halt as _;
8+
#[rtic::app(device = pac, dispatchers = [UARTE1])]
9+
mod app {
10+
use super::*;
11+
use cortex_m::asm;
12+
use embedded_hal::digital::{OutputPin, StatefulOutputPin};
13+
use hal::{
14+
gpio::{p0::Parts, Level, Output, Pin, PushPull},
15+
monotonic::MonotonicTimer,
16+
};
17+
use pac::TIMER0;
18+
use rtt_target::{rprintln, rtt_init_print};
19+
20+
#[monotonic(binds = TIMER0, default = true)]
21+
type MyMono = MonotonicTimer<TIMER0, 16_000_000>;
22+
23+
#[shared]
24+
struct Shared {}
25+
26+
#[local]
27+
struct Local {
28+
led: Pin<Output<PushPull>>,
29+
}
30+
31+
#[init]
32+
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
33+
rtt_init_print!();
34+
rprintln!("init");
35+
36+
let p0 = Parts::new(cx.device.P0);
37+
let led = p0.p0_13.into_push_pull_output(Level::High).degrade();
38+
39+
// New does not exists for invalid frequencies
40+
let mono = MyMono::new(cx.device.TIMER0);
41+
blink::spawn().ok();
42+
(Shared {}, Local { led }, init::Monotonics(mono))
43+
}
44+
45+
#[idle]
46+
fn idle(_: idle::Context) -> ! {
47+
loop {
48+
rprintln!("idle");
49+
// Put core to sleep until next interrupt
50+
asm::wfe();
51+
}
52+
}
53+
54+
#[task(local = [led])]
55+
fn blink(ctx: blink::Context) {
56+
rprintln!("Blink!");
57+
let led = ctx.local.led;
58+
// Note this unwrap is safe since is_set_low is allways Ok
59+
if led.is_set_low().unwrap() {
60+
led.set_high().ok();
61+
} else {
62+
led.set_low().ok();
63+
}
64+
// spawn after current time + 1 second
65+
blink::spawn_after(fugit::ExtU32::millis(1000)).ok();
66+
}
67+
}

nrf-hal-common/Cargo.toml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ license = "MIT OR Apache-2.0"
2020
edition = "2018"
2121

2222
[dependencies]
23-
cortex-m = "0.7.7"
24-
nb = "1.1.0"
25-
fixed = "1.25.1"
26-
rand_core = "0.6.4"
2723
cfg-if = "1.0.0"
24+
cortex-m = "0.7.7"
2825
embedded-dma = "0.2.0"
26+
embedded-hal = "1.0.0"
27+
embedded-io = "0.6.1"
2928
embedded-storage = "0.3.1"
29+
fixed = "1.25.1"
30+
nb = "1.1.0"
31+
rand_core = "0.6.4"
3032

3133
[dependencies.void]
3234
default-features = false
@@ -86,11 +88,13 @@ features = ["unproven"]
8688
version = "0.2.7"
8789
optional = true
8890

89-
[dependencies.embedded-hal]
91+
[dependencies.rtic-monotonic]
9092
version = "1.0.0"
93+
optional = true
9194

92-
[dependencies.embedded-io]
93-
version = "0.6.1"
95+
[dependencies.fugit]
96+
version = "0.3.7"
97+
optional = true
9498

9599
[features]
96100
doc = []
@@ -104,3 +108,4 @@ doc = []
104108
5340-app = ["nrf5340-app-pac"]
105109
5340-net = ["nrf5340-net-pac"]
106110
9160 = ["nrf9160-pac"]
111+
rtic-monotonic = ["dep:rtic-monotonic", "dep:fugit"]

nrf-hal-common/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#![doc(html_root_url = "https://docs.rs/nrf-hal-common/0.17.1")]
55
#![no_std]
66

7+
#[cfg(feature = "rtic-monotonic")]
8+
pub mod monotonic;
9+
710
#[cfg(feature = "51")]
811
pub use nrf51_pac as pac;
912

0 commit comments

Comments
 (0)