Skip to content

Commit 5a1b903

Browse files
committed
Switch to systick-monotonic and reenable serial
Completely untested, if somebody has hardware handy please test
1 parent e752720 commit 5a1b903

File tree

3 files changed

+62
-361
lines changed

3 files changed

+62
-361
lines changed

rtic_v1/stm32l0_monotonic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2021"
1010

1111
[dependencies]
1212
cortex-m-rtic = "1.0.0"
13-
embedded-time = "0.12.1"
13+
systick-monotonic = "1.0.1"
1414
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
1515
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
1616
stm32l0xx-hal = { version = "0.9.0", features = ["rt", "mcu-STM32L031K6Tx"] }
Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
#![no_main]
22
#![no_std]
33

4-
mod monotonic_stm32l0;
5-
64
use core::fmt::Write;
75

86
use panic_rtt_target as _;
97
use rtt_target::{rprintln, rtt_init_print};
108

11-
use embedded_time::rate::Baud;
129
use rtic::app;
1310
use stm32l0xx_hal::{pac, prelude::*, rcc::Config, serial};
11+
use systick_monotonic::{
12+
fugit::{ExtU32, MillisDurationU64},
13+
Systick,
14+
};
1415

15-
use monotonic_stm32l0::{Duration, Instant, Tim6Monotonic, U16Ext};
16-
17-
const INTERVAL_MS: u16 = 500;
16+
const INTERVAL_MS: u32 = 500;
1817

1918
#[app(
2019
device = stm32l0xx_hal::pac,
@@ -27,16 +26,16 @@ mod app {
2726
// Setting this monotonic as the default
2827
// enables the shorthand fizzbuzz::spawn_after
2928
// without having to specify `Mono` as fizzbuzz::Mono::spawn_after(
30-
#[monotonic(binds = TIM6, default = true)]
31-
type Mono = Tim6Monotonic;
29+
#[monotonic(binds = SysTick, default = true)]
30+
type Tonic = Systick<1000>;
3231

3332
#[local]
3433
struct Local {
3534
/// Serial debug output
36-
// serial: serial::Serial<pac::USART1>,
35+
serial: serial::Serial<pac::USART2>,
3736

3837
/// Timer interval
39-
interval: Duration,
38+
interval: MillisDurationU64,
4039

4140
/// Counter
4241
counter: usize,
@@ -59,61 +58,83 @@ mod app {
5958
// GPIO
6059
let gpiob = dp.GPIOB.split(&mut rcc);
6160

62-
// Initialize the timer TIM6.
63-
//writeln!(
64-
//serial,
65-
//"Initialize monotonic timer (TIM6) at 7.8125 kHz (128 μs)"
66-
//)
67-
//.unwrap();
68-
let mono = Tim6Monotonic::initialize(dp.TIM6);
69-
70-
let interval = INTERVAL_MS.millis();
71-
//writeln!(
72-
//serial,
73-
//"Schedule task every {} ms / {} ticks",
74-
//INTERVAL_MS,
75-
//interval.as_ticks()
76-
//)
77-
//.unwrap();
61+
// Initialize serial port(s)
62+
let mut serial = serial::Serial::usart2(
63+
dp.USART2,
64+
gpiob.pb6.into_floating_input(),
65+
gpiob.pb7.into_floating_input(),
66+
serial::Config {
67+
baudrate: 57_600.Bd(),
68+
wordlength: serial::WordLength::DataBits8,
69+
parity: serial::Parity::ParityNone,
70+
stopbits: serial::StopBits::STOP1,
71+
},
72+
&mut rcc,
73+
)
74+
.unwrap();
75+
76+
// Initialize the timer
77+
writeln!(serial, "Initialize monotonic timer using SysTick at 1kHz").unwrap();
78+
79+
let mono = Systick::new(cx.core.SYST, 16_000_000);
80+
81+
let interval: MillisDurationU64 = INTERVAL_MS.millis().into();
82+
83+
writeln!(
84+
serial,
85+
"Schedule task every {} ms / {} ticks",
86+
interval,
87+
interval.ticks(),
88+
)
89+
.unwrap();
7890

7991
// Spawn task "fizzbuzz"
8092
let _ = fizzbuzz::spawn();
8193

82-
//writeln!(serial, "== Init done ==").unwrap();
94+
writeln!(serial, "== Init done ==").unwrap();
8395

8496
let local = Local {
85-
//serial,
97+
serial,
8698
interval,
8799
counter: 1,
88100
};
89101

90102
(Shared {}, local, init::Monotonics(mono))
91103
}
92104

93-
#[task(local = [/*serial,*/ interval, counter])]
105+
#[task(local = [serial, interval, counter])]
94106
fn fizzbuzz(cx: fizzbuzz::Context) {
95107
rprintln!("fizzbuzz!");
96108
// Access resources
97-
//let serial = cx.local.serial;
98-
let now = Instant::now().counts();
109+
let serial = cx.local.serial;
110+
let now = monotonics::now();
99111
let counter = cx.local.counter;
112+
let interval = cx.local.interval;
100113

101114
// Fancy fizzbuzz implementation
102115
match (*counter % 3 == 0, *counter % 5 == 0) {
103-
(true, true) => rprintln!("fizzbuzz (now={:05})", now),
104-
(true, false) => rprintln!(" fizz (now={:05})", now),
105-
(false, true) => rprintln!(" buzz (now={:05})", now),
106-
_ => rprintln!("{:08} (now={:05})", *counter, now),
107-
//(true, true) => writeln!(serial, "fizzbuzz (now={:05})", now).unwrap(),
108-
//(true, false) => writeln!(serial, " fizz (now={:05})", now).unwrap(),
109-
//(false, true) => writeln!(serial, " buzz (now={:05})", now).unwrap(),
110-
//_ => writeln!(serial, "{:08} (now={:05})", *counter, now).unwrap(),
116+
(true, true) => {
117+
rprintln!("fizzbuzz (now={:05})", now);
118+
writeln!(serial, "fizzbuzz (now={:05})", now).unwrap();
119+
}
120+
(true, false) => {
121+
rprintln!(" fizz (now={:05})", now);
122+
writeln!(serial, " fizz (now={:05})", now).unwrap();
123+
}
124+
(false, true) => {
125+
rprintln!(" buzz (now={:05})", now);
126+
writeln!(serial, " buzz (now={:05})", now).unwrap();
127+
}
128+
_ => {
129+
rprintln!("{:08} (now={:05})", *counter, now);
130+
writeln!(serial, "{:08} (now={:05})", *counter, now).unwrap();
131+
}
111132
}
112133

113134
// Increment counter
114135
*counter += 1;
115136

116137
// Re-schedule
117-
let _ = fizzbuzz::spawn_after(*cx.local.interval);
138+
let _ = fizzbuzz::spawn_after(*interval);
118139
}
119140
}

0 commit comments

Comments
 (0)