Skip to content

Commit 2e10754

Browse files
usbalbinHenrik Snöman
and
Henrik Snöman
committed
Add src/timer/monotonics.rs from f4xx-hal
Co-authored-by: Henrik Snöman <[email protected]>
1 parent a0f0d55 commit 2e10754

File tree

4 files changed

+361
-1
lines changed

4 files changed

+361
-1
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"rust-analyzer.cargo.target": "thumbv6m-none-eabi",
44
"rust-analyzer.cargo.features": [
55
"rt",
6-
"stm32g081"
6+
"stm32g081",
7+
"rtic2"
78
]
89
}

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ fugit = "0.3.7"
2323
embedded-hal = "1.0.0"
2424
bare-metal = "1.0.0"
2525
portable-atomic = { version = "1.10.0", features = ["critical-section"] }
26+
embedded-hal-async = { version = "1.0.0", optional = true }
27+
atomic-polyfill = { version = "1.0.3", optional = true }
28+
rtic-time = { version = "2.0.0", optional = true }
29+
embedded-time = { version = "0.12.1", optional = true }
2630

2731
[dependencies.stm32g0]
2832
package = "stm32g0-staging"
@@ -61,6 +65,7 @@ stm32g0x1 = []
6165

6266
i2c-blocking = []
6367
i2c-nonblocking = []
68+
rtic2 = ["dep:embedded-hal-async", "dep:atomic-polyfill", "dep:rtic-time"]
6469

6570
[profile.dev]
6671
incremental = false

src/timer/mod.rs

+49
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ use crate::stm32::*;
44
use crate::time::{Hertz, MicroSecond};
55
use core::marker::PhantomData;
66
use fugit::HertzU32;
7+
use fugit::RateExtU32;
78
use void::Void;
89

910
pub mod delay;
11+
#[cfg(feature = "rtic2")]
12+
pub mod monotonics;
1013
pub mod opm;
1114
pub mod pins;
1215
pub mod pwm;
@@ -30,6 +33,46 @@ type Channel2 = Channel<1>;
3033
type Channel3 = Channel<2>;
3134
type Channel4 = Channel<3>;
3235

36+
/// Timer wrapper for fixed precision timers.
37+
///
38+
/// Uses `fugit::TimerDurationU32` for most of operations
39+
pub struct FTimer<TIM, const FREQ: u32> {
40+
tim: TIM,
41+
}
42+
43+
/// `FTimer` with precision of 1 μs (1 MHz sampling)
44+
pub type FTimerUs<TIM> = FTimer<TIM, 1_000_000>;
45+
46+
/// `FTimer` with precision of 1 ms (1 kHz sampling)
47+
///
48+
/// NOTE: don't use this if your system frequency more than 65 MHz
49+
pub type FTimerMs<TIM> = FTimer<TIM, 1_000>;
50+
51+
impl<TIM: private::TimerBase, const FREQ: u32> FTimer<TIM, FREQ> {
52+
/// Initialize timer
53+
pub fn new(mut tim: TIM, rcc: &mut Rcc) -> Self {
54+
tim.init(rcc);
55+
tim.set_freq(FREQ.Hz(), rcc.clocks.apb_tim_clk);
56+
57+
Self { tim }
58+
}
59+
60+
/*/// Creates `Counter` that implements [embedded_hal_02::timer::CountDown]
61+
pub fn counter(self) -> Counter<TIM, FREQ> {
62+
Counter(self)
63+
}
64+
65+
/// Creates `Delay` that implements [embedded_hal_02::blocking::delay] traits
66+
pub fn delay(self) -> Delay<TIM, FREQ> {
67+
Delay(self)
68+
}*/
69+
70+
/// Releases the TIM peripheral
71+
pub fn release(self) -> TIM {
72+
self.tim
73+
}
74+
}
75+
3376
pub struct TimerFrequencySettings {
3477
psc: u16,
3578
arr: u32,
@@ -59,6 +102,8 @@ pub(super) mod private {
59102
use super::{Rcc, TimerFrequencySettings};
60103

61104
pub trait TimerCommon {
105+
type Width: Into<u32> + From<u16>;
106+
62107
fn init(&mut self, rcc: &mut Rcc);
63108

64109
fn set_urs(&mut self);
@@ -80,6 +125,8 @@ pub(super) mod private {
80125
}
81126

82127
impl TimerCommon for SYST {
128+
type Width = u32;
129+
83130
fn init(&mut self, _rcc: &mut Rcc) {
84131
self.set_clock_source(SystClkSource::Core);
85132
}
@@ -144,6 +191,8 @@ macro_rules! timers {
144191
($($TIM:ident: ($tim:ident, $cnt:ident $(,$cnt_h:ident)*),)+) => {
145192
$(
146193
impl private::TimerCommon for $TIM {
194+
type Width = u16; // TODO: Are there any with 32 bits?
195+
147196
fn init(&mut self, rcc: &mut Rcc) {
148197
$TIM::enable(rcc);
149198
$TIM::reset(rcc);

0 commit comments

Comments
 (0)