Skip to content

Commit e9bf6cf

Browse files
committed
Move timer modules
1 parent b73e7eb commit e9bf6cf

29 files changed

+1851
-636
lines changed

examples/f103c8/src/main.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use jw_stm32f1_hal as hal;
1111
use jw_stm32f1_hal::{
1212
Heap, Mcu,
1313
afio::{NONE_PIN, RemapDefault},
14-
embedded_hal, embedded_io,
14+
embedded_hal::{self, pwm::SetDutyCycle},
15+
embedded_io,
1516
gpio::PinState,
1617
nvic_scb::PriorityGrouping,
1718
pac::{self, Interrupt},
@@ -70,8 +71,10 @@ fn main() -> ! {
7071
// let pin_rx = hal::afio::NONE_PIN;
7172

7273
let config = uart::Config::default();
73-
let uart1 = dp.USART1.constrain();
74-
let (Some(uart_tx), Some(uart_rx)) = uart1.into_tx_rx((pin_tx, pin_rx), config, &mut mcu)
74+
let (Some(uart_tx), Some(uart_rx)) =
75+
dp.USART1
76+
.constrain()
77+
.into_tx_rx((pin_tx, pin_rx), config, &mut mcu)
7578
else {
7679
panic!()
7780
};
@@ -105,10 +108,10 @@ fn main() -> ! {
105108
else {
106109
panic!()
107110
};
108-
bt.config_freq(1.MHz(), 20.kHz());
111+
bt.config_freq(20.kHz());
109112

110113
ch1.config(PwmMode::Mode1, PwmPolarity::ActiveHigh);
111-
ch1.set_duty(ch1.get_max_duty() / 2);
114+
ch1.set_duty_cycle(ch1.max_duty_cycle() / 2).ok();
112115

113116
bt.start();
114117

scripts/sync_code.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
"src/timer/timer6.rs": "src/timer/timer1.rs",
1616
"src/timer/timer7.rs": "src/timer/timer6.rs",
1717
"src/timer/timer8.rs": "src/timer/timer1.rs",
18-
"src/timer/timer15.rs": "src/timer/timer2.rs",
19-
"src/timer/timer16.rs": "src/timer/timer2.rs",
20-
"src/timer/timer17.rs": "src/timer/timer16.rs",
18+
"src/timer/timer9.rs": "src/timer/timer1.rs",
19+
"src/timer/timer10.rs": "src/timer/timer1.rs",
20+
"src/timer/timer11.rs": "src/timer/timer10.rs",
21+
"src/timer/timer12.rs": "src/timer/timer9.rs",
22+
"src/timer/timer13.rs": "src/timer/timer10.rs",
23+
"src/timer/timer14.rs": "src/timer/timer10.rs",
24+
"src/timer/timer15.rs": "src/timer/timer9.rs",
25+
"src/timer/timer16.rs": "src/timer/timer10.rs",
26+
"src/timer/timer17.rs": "src/timer/timer10.rs",
2127
}
2228
BEGIN_PATTERN = re.compile(r"\/\/ sync .+\s")
2329

src/common/timer/counter.rs

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use super::*;
2-
use fugit::{HertzU32 as Hertz, MicrosDurationU32};
1+
use super::{Error, Event, FTimer, GeneralTimer};
2+
use core::ops::{Deref, DerefMut};
3+
use fugit::{HertzU32 as Hertz, MicrosDurationU32, TimerDurationU32, TimerInstantU32};
34

45
/// Hardware timers
56
pub struct CounterHz<TIM> {
@@ -95,3 +96,101 @@ impl<TIM: GeneralTimer> CounterHz<TIM> {
9596
MicrosDurationU32::from_ticks(u32::try_from(1_000_000 * cnt / freq_divider).unwrap())
9697
}
9798
}
99+
100+
// ----------------------------------------------------------------------------
101+
102+
/// Periodic non-blocking timer that imlements [embedded_hal_02::timer::CountDown]
103+
pub struct Counter<TIM, const FREQ: u32>(pub(super) FTimer<TIM, FREQ>);
104+
105+
impl<T, const FREQ: u32> Deref for Counter<T, FREQ> {
106+
type Target = FTimer<T, FREQ>;
107+
fn deref(&self) -> &Self::Target {
108+
&self.0
109+
}
110+
}
111+
112+
impl<T, const FREQ: u32> DerefMut for Counter<T, FREQ> {
113+
fn deref_mut(&mut self) -> &mut Self::Target {
114+
&mut self.0
115+
}
116+
}
117+
118+
/// `Counter` with precision of 1 μs (1 MHz sampling)
119+
pub type CounterUs<TIM> = Counter<TIM, 1_000_000>;
120+
121+
/// `Counter` with precision of of 1 ms (1 kHz sampling)
122+
///
123+
/// NOTE: don't use this if your system frequency more than 65 MHz
124+
pub type CounterMs<TIM> = Counter<TIM, 1_000>;
125+
126+
impl<TIM: GeneralTimer, const FREQ: u32> Counter<TIM, FREQ> {
127+
/// Releases the TIM peripheral
128+
pub fn release(mut self) -> FTimer<TIM, FREQ> {
129+
// stop counter
130+
self.tim.reset_config();
131+
self.0
132+
}
133+
134+
pub fn now(&self) -> TimerInstantU32<FREQ> {
135+
TimerInstantU32::from_ticks(self.tim.read_count().into())
136+
}
137+
138+
pub fn start(&mut self, timeout: TimerDurationU32<FREQ>) -> Result<(), Error> {
139+
// pause
140+
self.tim.disable_counter();
141+
142+
self.tim.clear_interrupt_flag(Event::Update);
143+
144+
// reset counter
145+
self.tim.reset_counter();
146+
147+
self.tim.set_auto_reload(timeout.ticks() - 1)?;
148+
149+
// Trigger update event to load the registers
150+
self.tim.trigger_update();
151+
152+
// start counter
153+
self.tim.enable_counter();
154+
155+
Ok(())
156+
}
157+
158+
pub fn wait(&mut self) -> nb::Result<(), Error> {
159+
if self.tim.get_interrupt_flag().contains(Event::Update) {
160+
self.tim.clear_interrupt_flag(Event::Update);
161+
Ok(())
162+
} else {
163+
Err(nb::Error::WouldBlock)
164+
}
165+
}
166+
167+
pub fn cancel(&mut self) -> Result<(), Error> {
168+
if !self.tim.is_counter_enabled() {
169+
return Err(Error::Disabled);
170+
}
171+
172+
// disable counter
173+
self.tim.disable_counter();
174+
Ok(())
175+
}
176+
}
177+
178+
impl<TIM: GeneralTimer, const FREQ: u32> fugit_timer::Timer<FREQ> for Counter<TIM, FREQ> {
179+
type Error = Error;
180+
181+
fn now(&mut self) -> TimerInstantU32<FREQ> {
182+
Self::now(self)
183+
}
184+
185+
fn start(&mut self, duration: TimerDurationU32<FREQ>) -> Result<(), Self::Error> {
186+
self.start(duration)
187+
}
188+
189+
fn cancel(&mut self) -> Result<(), Self::Error> {
190+
self.cancel()
191+
}
192+
193+
fn wait(&mut self) -> nb::Result<(), Self::Error> {
194+
self.wait()
195+
}
196+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,22 @@ impl<TIM: GeneralTimer, const FREQ: u32> fugit_timer::Delay<FREQ> for Delay<TIM,
7575
Ok(())
7676
}
7777
}
78+
79+
// ----------------------------------------------------------------------------
80+
81+
use embedded_hal::delay::DelayNs;
82+
use fugit::ExtU32Ceil;
83+
84+
impl<TIM: GeneralTimer, const FREQ: u32> DelayNs for Delay<TIM, FREQ> {
85+
fn delay_ns(&mut self, ns: u32) {
86+
self.delay(ns.micros_at_least());
87+
}
88+
89+
fn delay_us(&mut self, us: u32) {
90+
self.delay(us.micros_at_least());
91+
}
92+
93+
fn delay_ms(&mut self, ms: u32) {
94+
self.delay(ms.millis_at_least());
95+
}
96+
}

src/common/timer/fix_timer.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use super::*;
2+
3+
/// Timer wrapper for fixed precision timers.
4+
///
5+
/// Uses `fugit::TimerDurationU32` for most of operations
6+
pub struct FTimer<TIM, const FREQ: u32> {
7+
pub(crate) tim: TIM,
8+
clk: Hertz,
9+
}
10+
11+
/// `FTimer` with precision of 1 μs (1 MHz sampling)
12+
pub type FTimerUs<TIM> = FTimer<TIM, 1_000_000>;
13+
14+
/// `FTimer` with precision of 1 ms (1 kHz sampling)
15+
///
16+
/// NOTE: don't use this if your system frequency more than 65 MHz
17+
pub type FTimerMs<TIM> = FTimer<TIM, 1_000>;
18+
19+
impl<TIM: GeneralTimer, const FREQ: u32> FTimer<TIM, FREQ> {
20+
/// Initialize timer
21+
pub fn new(tim: TIM, clk: Hertz) -> Self {
22+
let mut t = Self { tim, clk };
23+
t.configure();
24+
t
25+
}
26+
27+
/// Calculate prescaler depending on `Clocks` state
28+
pub fn configure(&mut self) {
29+
assert!(self.clk.raw() % FREQ == 0);
30+
let psc = self.clk.raw() / FREQ;
31+
self.tim.set_prescaler(u16::try_from(psc - 1).unwrap());
32+
}
33+
34+
/// Creates `Counter` that imlements [embedded_hal_02::timer::CountDown]
35+
pub fn counter(self) -> Counter<TIM, FREQ> {
36+
Counter(self)
37+
}
38+
39+
/// Creates `Delay` that imlements [embedded_hal_02::blocking::delay] traits
40+
pub fn delay(self) -> Delay<TIM, FREQ> {
41+
Delay(self)
42+
}
43+
44+
/// Releases the TIM peripheral
45+
pub fn release(self) -> TIM {
46+
self.tim
47+
}
48+
49+
/// Starts listening for an `event`
50+
///
51+
/// Note, you will also have to enable the TIM2 interrupt in the NVIC to start
52+
/// receiving events.
53+
pub fn listen(&mut self, event: Event) {
54+
self.tim.listen_interrupt(event, true);
55+
}
56+
57+
/// Clears interrupt associated with `event`.
58+
///
59+
/// If the interrupt is not cleared, it will immediately retrigger after
60+
/// the ISR has finished.
61+
pub fn clear_interrupt(&mut self, event: Event) {
62+
self.tim.clear_interrupt_flag(event);
63+
}
64+
65+
pub fn get_interrupt(&mut self) -> Event {
66+
self.tim.get_interrupt_flag()
67+
}
68+
69+
/// Stops listening for an `event`
70+
pub fn unlisten(&mut self, event: Event) {
71+
self.tim.listen_interrupt(event, false);
72+
}
73+
74+
/// Stopping timer in debug mode can cause troubles when sampling the signal
75+
pub fn stop_in_debug(&mut self, state: bool) {
76+
self.tim.stop_in_debug(state);
77+
}
78+
}

src/common/timer/mod.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@ pub mod pwm;
22
pub use pwm::*;
33
pub mod counter;
44
pub use counter::*;
5+
pub mod fix_timer;
6+
pub use fix_timer::*;
7+
pub mod delay;
8+
pub use delay::*;
59

610
use crate::time::Hertz;
711

8-
pub trait PwmTimer {
9-
fn start(&mut self);
10-
fn stop(&mut self);
11-
fn config_freq(&mut self, count_freq: Hertz, update_freq: Hertz);
12-
fn get_max_duty(&self) -> u32;
13-
fn get_count_value(&self) -> u32;
14-
}
15-
16-
pub trait PwmChannel {
12+
pub trait PwmChannel: embedded_hal::pwm::SetDutyCycle {
1713
fn config(&mut self, mode: PwmMode, polarity: PwmPolarity);
1814
fn set_enable(&mut self, en: bool);
19-
fn get_max_duty(&self) -> u32;
20-
/// Remember to use `get_max_duty()`
21-
fn set_duty(&mut self, duty: u32);
2215
}
2316

2417
// ----------------------------------------------------------------------------
@@ -37,6 +30,7 @@ pub trait GeneralTimer {
3730
fn read_prescaler(&self) -> u16;
3831
fn read_count(&self) -> u32;
3932
fn trigger_update(&mut self);
33+
fn stop_in_debug(&mut self, state: bool);
4034
fn config_freq(&mut self, clock: Hertz, count_freq: Hertz, update_freq: Hertz);
4135

4236
fn clear_interrupt_flag(&mut self, event: Event);
@@ -69,10 +63,13 @@ pub trait TimerWithPwm2Ch: TimerWithPwm1Ch {
6963
fn get_ch2_cc_value(&self) -> u32;
7064
}
7165

72-
pub trait TimerWithPwm4Ch: TimerWithPwm2Ch {
66+
pub trait TimerWithPwm3Ch: TimerWithPwm2Ch {
7367
fn enable_ch3(&mut self, en: bool);
7468
fn set_ch3_cc_value(&mut self, value: u32);
7569
fn get_ch3_cc_value(&self) -> u32;
70+
}
71+
72+
pub trait TimerWithPwm4Ch: TimerWithPwm3Ch {
7673
fn enable_ch4(&mut self, en: bool);
7774
fn set_ch4_cc_value(&mut self, value: u32);
7875
fn get_ch4_cc_value(&self) -> u32;

0 commit comments

Comments
 (0)