Skip to content

Commit 1bf2ce2

Browse files
barafaelAfoHT
authored andcommitted
Add stm32l0-monotonic example for RTIC v1.0
ported from rtic_v0.5/stm32l0_monotonic.
1 parent a0694b8 commit 1bf2ce2

File tree

7 files changed

+541
-0
lines changed

7 files changed

+541
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[target.thumbv7m-none-eabi]
2+
# uncomment this to make `cargo run` execute programs on QEMU
3+
# runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
4+
5+
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
6+
# uncomment ONE of these three option to make `cargo run` start a GDB session
7+
# which option to pick depends on your system
8+
# runner = "arm-none-eabi-gdb -q -x jlink.gdb"
9+
# runner = "arm-none-eabi-gdb -q -x openocd.gdb"
10+
# runner = "gdb-multiarch -q -x openocd.gdb"
11+
# runner = "gdb -q -x openocd.gdb"
12+
13+
rustflags = [
14+
# LLD (shipped with the Rust toolchain) is used as the default linker
15+
"-C", "link-arg=-Tlink.x",
16+
17+
# if you run into problems with LLD switch to the GNU linker by commenting out
18+
# this line
19+
# "-C", "linker=arm-none-eabi-ld",
20+
21+
# if you need to link to pre-compiled C libraries provided by a C toolchain
22+
# use GCC as the linker by commenting out both lines above and then
23+
# uncommenting the three lines below
24+
# "-C", "linker=arm-none-eabi-gcc",
25+
# "-C", "link-arg=-Wl,-Tlink.x",
26+
# "-C", "link-arg=-nostartfiles",
27+
28+
# uncomment for unchecked wrapping arithmetics also in dev mode
29+
# "-Z", "force-overflow-checks=off",
30+
]
31+
32+
[build]
33+
# Pick ONE of these compilation targets
34+
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
35+
# target = "thumbv7m-none-eabi" # Cortex-M3
36+
# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
37+
# target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Refer to https://github.com/probe-rs/cargo-embed/blob/master/src/config/default.toml
2+
# for the comprehensive list of options
3+
4+
[default.general]
5+
chip = "STM32L071"
6+
7+
[default.rtt]
8+
enabled = true
9+
show_timestamps = true
10+
11+
[default.gdb]
12+
# Whether or not a GDB server should be opened after flashing.
13+
# This is exclusive and cannot be used with RTT at the moment.
14+
enabled = false
15+
# The connection string in host:port format wher the GDB server will open a socket.
16+
# gdb_connection_string
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "stm32l0_monotonic"
3+
categories = ["embedded", "no-std"]
4+
authors = ["Danilo Bargen <mail@dbrgn.ch>"]
5+
description = "Example Monotonic trait"
6+
keywords = ["arm", "cortex-m"]
7+
license = "MIT OR Apache-2.0"
8+
version = "0.1.0"
9+
edition = "2021"
10+
11+
[dependencies]
12+
cortex-m-rtic = "1.0.0"
13+
embedded-time = "0.12.1"
14+
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
15+
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
16+
stm32l0xx-hal = { version = "0.9.0", features = ["rt", "mcu-STM32L031K6Tx"] }
17+
18+
# this lets you use `cargo fix`!
19+
[[bin]]
20+
name = "stm32l0_monotonic"
21+
test = false
22+
bench = false
23+
24+
[profile.dev]
25+
opt-level = 1
26+
codegen-units = 16
27+
debug = true
28+
lto = false
29+
30+
[profile.release]
31+
opt-level = "s" # optimize for size
32+
codegen-units = 1 # better optimizations
33+
debug = true # symbols are nice and they don't increase the size on Flash
34+
lto = true # better optimizations
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# STM32L0 Monotonic
2+
3+
In this example we show the use of a custom `rtic::Monotonic` implementation
4+
which uses a 16 bit timer of the `STM32L0` MCU.
5+
6+
## Flashing and running
7+
8+
Flashing with a standard STLink v2 is easy with `cargo-embed`:
9+
10+
```shell
11+
$ cargo install cargo-embed
12+
$ cargo embed --release
13+
```
14+
15+
Please review the `.embed.toml` file to change your target IC among other options.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use panic_rtt_target as _;
5+
6+
mod monotonic_stm32l0;
7+
use core::fmt::Write;
8+
use embedded_time::rate::Baud;
9+
use monotonic_stm32l0::{Duration, Instant, Tim6Monotonic, U16Ext};
10+
use rtic::app;
11+
use rtt_target::{rprintln, rtt_init_print};
12+
use stm32l0xx_hal::{pac, prelude::*, rcc::Config, serial};
13+
14+
const INTERVAL_MS: u16 = 500;
15+
16+
#[app(
17+
device = stm32l0xx_hal::pac,
18+
peripherals = true,
19+
dispatchers = [SPI1],
20+
)]
21+
mod app {
22+
use super::*;
23+
24+
// Setting this monotonic as the default
25+
// enables the shorthand fizzbuzz::spawn_after
26+
// without having to specify `Mono` as fizzbuzz::Mono::spawn_after(
27+
#[monotonic(binds = TIM6, default = true)]
28+
type Mono = Tim6Monotonic;
29+
30+
#[local]
31+
struct Local {
32+
/// Serial debug output
33+
// serial: serial::Serial<pac::USART1>,
34+
35+
/// Timer interval
36+
interval: Duration,
37+
38+
/// Counter
39+
counter: usize,
40+
}
41+
42+
#[shared]
43+
struct Shared {}
44+
45+
#[init]
46+
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
47+
// Get peripherals
48+
let dp: pac::Peripherals = cx.device;
49+
50+
// Clock configuration. Use HSI at 16 MHz.
51+
let mut rcc = dp.RCC.freeze(Config::hsi16());
52+
53+
rtt_init_print!();
54+
rprintln!("RTT init");
55+
56+
// GPIO
57+
let gpiob = dp.GPIOB.split(&mut rcc);
58+
59+
// Initialize the timer TIM6.
60+
//writeln!(
61+
//serial,
62+
//"Initialize monotonic timer (TIM6) at 7.8125 kHz (128 μs)"
63+
//)
64+
//.unwrap();
65+
let mono = Tim6Monotonic::initialize(dp.TIM6);
66+
67+
let interval = INTERVAL_MS.millis();
68+
//writeln!(
69+
//serial,
70+
//"Schedule task every {} ms / {} ticks",
71+
//INTERVAL_MS,
72+
//interval.as_ticks()
73+
//)
74+
//.unwrap();
75+
76+
// Spawn task "fizzbuzz"
77+
let _ = fizzbuzz::spawn();
78+
79+
//writeln!(serial, "== Init done ==").unwrap();
80+
81+
let local = Local {
82+
//serial,
83+
interval,
84+
counter: 1,
85+
};
86+
87+
(Shared {}, local, init::Monotonics(mono))
88+
}
89+
90+
#[task(local = [/*serial,*/ interval, counter])]
91+
fn fizzbuzz(cx: fizzbuzz::Context) {
92+
rprintln!("fizzbuzz!");
93+
// Access resources
94+
//let serial = cx.local.serial;
95+
let now = Instant::now().counts();
96+
let counter = cx.local.counter;
97+
98+
// Fancy fizzbuzz implementation
99+
match (*counter % 3 == 0, *counter % 5 == 0) {
100+
(true, true) => rprintln!("fizzbuzz (now={:05})", now),
101+
(true, false) => rprintln!(" fizz (now={:05})", now),
102+
(false, true) => rprintln!(" buzz (now={:05})", now),
103+
_ => rprintln!("{:08} (now={:05})", *counter, now),
104+
//(true, true) => writeln!(serial, "fizzbuzz (now={:05})", now).unwrap(),
105+
//(true, false) => writeln!(serial, " fizz (now={:05})", now).unwrap(),
106+
//(false, true) => writeln!(serial, " buzz (now={:05})", now).unwrap(),
107+
//_ => writeln!(serial, "{:08} (now={:05})", *counter, now).unwrap(),
108+
}
109+
110+
// Increment counter
111+
*counter += 1;
112+
113+
// Re-schedule
114+
let _ = fizzbuzz::spawn_after(*cx.local.interval);
115+
}
116+
}

0 commit comments

Comments
 (0)