Skip to content

Commit 82414b6

Browse files
committed
Add RTC example code
1 parent 5d2f276 commit 82414b6

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

examples/rtc-demo/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "rtc-demo"
3+
version = "0.1.0"
4+
authors = ["Timo Kröger"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
cortex-m = "0.6.2"
11+
cortex-m-rt = "0.6.12"
12+
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
13+
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }
14+
15+
[dependencies.embedded-hal]
16+
version = "0.2.3"
17+
features = ["unproven"]

examples/rtc-demo/Embed.toml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[default.probe]
2+
# USB vendor ID
3+
# usb_vid = "1337"
4+
# USB product ID
5+
# usb_pid = "1337"
6+
# Serial number
7+
# serial = "12345678"
8+
# The protocol to be used for communicating with the target.
9+
protocol = "Swd"
10+
# The speed in kHz of the data link to the target.
11+
# speed = 1337
12+
13+
[default.flashing]
14+
# Whether or not the target should be flashed.
15+
enabled = true
16+
# Whether or not the target should be halted after reset.
17+
# DEPRECATED, moved to reset section
18+
halt_afterwards = false
19+
# Whether or not bytes erased but not rewritten with data from the ELF
20+
# should be restored with their contents before erasing.
21+
restore_unwritten_bytes = false
22+
# The path where an SVG of the assembled flash layout should be written to.
23+
# flash_layout_output_path = "out.svg"
24+
25+
[default.reset]
26+
# Whether or not the target should be reset.
27+
# When flashing is enabled as well, the target will be reset after flashing.
28+
enabled = true
29+
# Whether or not the target should be halted after reset.
30+
halt_afterwards = false
31+
32+
[default.general]
33+
# The chip name of the chip to be debugged.
34+
chip = "nRF52840_xxAA"
35+
# A list of chip descriptions to be loaded during runtime.
36+
chip_descriptions = []
37+
# The default log level to be used. Possible values are one of:
38+
# "OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"
39+
log_level = "WARN"
40+
41+
[default.rtt]
42+
# Whether or not an RTTUI should be opened after flashing.
43+
# This is exclusive and cannot be used with GDB at the moment.
44+
enabled = true
45+
# A list of channel associations to be displayed. If left empty, all channels are displayed.
46+
channels = [
47+
# { up = 0, down = 0, name = "name", format = "String" }
48+
]
49+
# The duration in ms for which the logger should retry to attach to RTT.
50+
timeout = 3000
51+
# Whether timestamps in the RTTUI are enabled
52+
show_timestamps = true
53+
# Whether to save rtt history buffer on exit.
54+
log_enabled = false
55+
# Where to save rtt history buffer relative to manifest path.
56+
log_path = "./logs"
57+
58+
[default.gdb]
59+
# Whether or not a GDB server should be opened after flashing.
60+
# This is exclusive and cannot be used with RTT at the moment.
61+
enabled = false
62+
# The connection string in host:port format wher the GDB server will open a socket.
63+
# gdb_connection_string

examples/rtc-demo/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RTC demo
2+
3+
This example shows how to use the compare functionality of the real-time counter peripheral.
4+
Runs on the nRF52840_DK but can easily be adapted for other hardware.
5+
6+
## Set up with `cargo-embed`
7+
8+
Install `cargo-embed` if you don't have it already:
9+
10+
```console
11+
$ cargo install cargo-embed
12+
```
13+
14+
Then just `cd` to the example folder and run
15+
16+
```console
17+
$ cargo embed --target thumbv7em-none-eabihf
18+
```

examples/rtc-demo/src/main.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use nrf52840_hal as hal;
5+
6+
use hal::rtc::{Rtc, RtcCompareReg, RtcInterrupt};
7+
use rtt_target::{rprintln, rtt_init_print};
8+
9+
#[panic_handler] // panicking behavior
10+
fn panic(_: &core::panic::PanicInfo) -> ! {
11+
loop {
12+
cortex_m::asm::bkpt();
13+
}
14+
}
15+
16+
#[cortex_m_rt::entry]
17+
fn main() -> ! {
18+
rtt_init_print!();
19+
20+
let p = hal::pac::Peripherals::take().unwrap();
21+
22+
// Enable LfClk which is required by the RTC.
23+
let clocks = hal::clocks::Clocks::new(p.CLOCK);
24+
let clocks = clocks.start_lfclk();
25+
26+
// Run RTC for 1 second
27+
let mut rtc = Rtc::new(p.RTC0);
28+
rtc.set_compare(RtcCompareReg::Compare0, 32_768).unwrap();
29+
rtc.enable_event(RtcInterrupt::Compare0);
30+
31+
rprintln!("Starting RTC");
32+
let rtc = rtc.enable_counter();
33+
34+
rprintln!("Waiting for compare match");
35+
while !rtc.is_event_triggered(RtcInterrupt::Compare0) {}
36+
rtc.reset_event(RtcInterrupt::Compare0);
37+
38+
rprintln!("Compare match, stopping RTC");
39+
let rtc = rtc.disable_counter();
40+
41+
rprintln!("Counter stopped at {} ticks", rtc.get_counter());
42+
43+
// Stop LfClk when RTC is not used anymore.
44+
rtc.release();
45+
clocks.stop_lfclk();
46+
47+
loop {
48+
cortex_m::asm::nop();
49+
}
50+
}

xtask/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub static EXAMPLES: &[(&str, &[&str])] = &[
2828
),
2929
("pwm-demo", &[]),
3030
("qdec-demo", &[]),
31+
("rtc-demo", &[]),
3132
("rtic-demo", &["51", "52810", "52811", "52832", "52840"]),
3233
("spi-demo", &[]),
3334
("spis-demo", &[]),

0 commit comments

Comments
 (0)