Skip to content

Commit

Permalink
Add src/timer/monotonics.rs from f4xx-hal
Browse files Browse the repository at this point in the history
Co-authored-by: Henrik Snöman <[email protected]>
  • Loading branch information
usbalbin and Henrik Snöman committed Jan 16, 2025
1 parent a0f0d55 commit 2e10754
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"rust-analyzer.cargo.target": "thumbv6m-none-eabi",
"rust-analyzer.cargo.features": [
"rt",
"stm32g081"
"stm32g081",
"rtic2"
]
}
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ fugit = "0.3.7"
embedded-hal = "1.0.0"
bare-metal = "1.0.0"
portable-atomic = { version = "1.10.0", features = ["critical-section"] }
embedded-hal-async = { version = "1.0.0", optional = true }
atomic-polyfill = { version = "1.0.3", optional = true }
rtic-time = { version = "2.0.0", optional = true }
embedded-time = { version = "0.12.1", optional = true }

[dependencies.stm32g0]
package = "stm32g0-staging"
Expand Down Expand Up @@ -61,6 +65,7 @@ stm32g0x1 = []

i2c-blocking = []
i2c-nonblocking = []
rtic2 = ["dep:embedded-hal-async", "dep:atomic-polyfill", "dep:rtic-time"]

[profile.dev]
incremental = false
Expand Down
49 changes: 49 additions & 0 deletions src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use crate::stm32::*;
use crate::time::{Hertz, MicroSecond};
use core::marker::PhantomData;
use fugit::HertzU32;
use fugit::RateExtU32;
use void::Void;

pub mod delay;
#[cfg(feature = "rtic2")]
pub mod monotonics;
pub mod opm;
pub mod pins;
pub mod pwm;
Expand All @@ -30,6 +33,46 @@ type Channel2 = Channel<1>;
type Channel3 = Channel<2>;
type Channel4 = Channel<3>;

/// Timer wrapper for fixed precision timers.
///
/// Uses `fugit::TimerDurationU32` for most of operations
pub struct FTimer<TIM, const FREQ: u32> {
tim: TIM,
}

/// `FTimer` with precision of 1 μs (1 MHz sampling)
pub type FTimerUs<TIM> = FTimer<TIM, 1_000_000>;

/// `FTimer` with precision of 1 ms (1 kHz sampling)
///
/// NOTE: don't use this if your system frequency more than 65 MHz
pub type FTimerMs<TIM> = FTimer<TIM, 1_000>;

impl<TIM: private::TimerBase, const FREQ: u32> FTimer<TIM, FREQ> {
/// Initialize timer
pub fn new(mut tim: TIM, rcc: &mut Rcc) -> Self {
tim.init(rcc);
tim.set_freq(FREQ.Hz(), rcc.clocks.apb_tim_clk);

Self { tim }
}

/*/// Creates `Counter` that implements [embedded_hal_02::timer::CountDown]
pub fn counter(self) -> Counter<TIM, FREQ> {
Counter(self)
}
/// Creates `Delay` that implements [embedded_hal_02::blocking::delay] traits
pub fn delay(self) -> Delay<TIM, FREQ> {
Delay(self)
}*/

/// Releases the TIM peripheral
pub fn release(self) -> TIM {
self.tim
}
}

pub struct TimerFrequencySettings {
psc: u16,
arr: u32,
Expand Down Expand Up @@ -59,6 +102,8 @@ pub(super) mod private {
use super::{Rcc, TimerFrequencySettings};

pub trait TimerCommon {
type Width: Into<u32> + From<u16>;

fn init(&mut self, rcc: &mut Rcc);

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

impl TimerCommon for SYST {
type Width = u32;

fn init(&mut self, _rcc: &mut Rcc) {
self.set_clock_source(SystClkSource::Core);
}
Expand Down Expand Up @@ -144,6 +191,8 @@ macro_rules! timers {
($($TIM:ident: ($tim:ident, $cnt:ident $(,$cnt_h:ident)*),)+) => {
$(
impl private::TimerCommon for $TIM {
type Width = u16; // TODO: Are there any with 32 bits?

fn init(&mut self, rcc: &mut Rcc) {
$TIM::enable(rcc);
$TIM::reset(rcc);
Expand Down
Loading

0 comments on commit 2e10754

Please sign in to comment.