Skip to content

Commit

Permalink
Merge pull request #427 from Kyrne/master
Browse files Browse the repository at this point in the history
RTIC Monotonic for RTC and TIMER
  • Loading branch information
qwandor authored May 24, 2024
2 parents eb8b34a + 4c00992 commit 090ce0e
Show file tree
Hide file tree
Showing 24 changed files with 714 additions and 13 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

## Unreleased

- add support for nRF52805
### New features

- Added support for nRF52805 ([#422]).
- Added implementation of RTIC `Monotonic` for RTC and TIMER, behind new `monotonic` feature ([#427]).

[#422]: https://github.com/nrf-rs/nrf-hal/pull/422
[#427]: https://github.com/nrf-rs/nrf-hal/pull/427

## [0.17.1]

### New features

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

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

Expand Down
10 changes: 10 additions & 0 deletions examples/monotonic-blinky/.cargo/config.toml
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"

1 change: 1 addition & 0 deletions examples/monotonic-blinky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
22 changes: 22 additions & 0 deletions examples/monotonic-blinky/Cargo.toml
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"
31 changes: 31 additions & 0 deletions examples/monotonic-blinky/Embed.toml
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"
16 changes: 16 additions & 0 deletions examples/monotonic-blinky/README.md
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
```

70 changes: 70 additions & 0 deletions examples/monotonic-blinky/src/rtc.rs
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();
}
}
67 changes: 67 additions & 0 deletions examples/monotonic-blinky/src/timer.rs
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();
}
}
19 changes: 12 additions & 7 deletions nrf-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ license = "MIT OR Apache-2.0"
edition = "2018"

[dependencies]
cortex-m = "0.7.7"
nb = "1.1.0"
fixed = "1.25.1"
rand_core = "0.6.4"
cfg-if = "1.0.0"
cortex-m = "0.7.7"
embedded-dma = "0.2.0"
embedded-hal = "1.0.0"
embedded-io = "0.6.1"
embedded-storage = "0.3.1"
fixed = "1.25.1"
nb = "1.1.0"
rand_core = "0.6.4"

[dependencies.void]
default-features = false
Expand Down Expand Up @@ -86,11 +88,13 @@ features = ["unproven"]
version = "0.2.7"
optional = true

[dependencies.embedded-hal]
[dependencies.rtic-monotonic]
version = "1.0.0"
optional = true

[dependencies.embedded-io]
version = "0.6.1"
[dependencies.fugit]
version = "0.3.7"
optional = true

[features]
doc = []
Expand All @@ -104,3 +108,4 @@ doc = []
5340-app = ["nrf5340-app-pac"]
5340-net = ["nrf5340-net-pac"]
9160 = ["nrf9160-pac"]
rtic-monotonic = ["dep:rtic-monotonic", "dep:fugit"]
3 changes: 3 additions & 0 deletions nrf-hal-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#![doc(html_root_url = "https://docs.rs/nrf-hal-common/0.17.1")]
#![no_std]

#[cfg(feature = "rtic-monotonic")]
pub mod monotonic;

#[cfg(feature = "51")]
pub use nrf51_pac as pac;

Expand Down
Loading

0 comments on commit 090ce0e

Please sign in to comment.