Skip to content

Commit 9b42ebb

Browse files
authored
Added clocks for timers - timclk1 timclk2 (#301)
Added clocks for timers - timclk1 timclk2
1 parent c637cc6 commit 9b42ebb

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

src/rcc.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,16 @@ impl CFGR {
687687

688688
let pclk2: u32 = hclk / u32(ppre2);
689689

690+
// RM0394 Rev 4, page 188
691+
// 6.2.14 Timer clock
692+
//
693+
// The timer clock frequencies are automatically defined by hardware. There are two cases:
694+
// 1. If the APB prescaler equals 1, the timer clock frequencies are set to the same
695+
// frequency as that of the APB domain.
696+
// 2. Otherwise, they are set to twice (×2) the frequency of the APB domain.
697+
let timclk1 = if ppre1 == 1 { pclk1 } else { 2 * pclk1 };
698+
let timclk2 = if ppre2 == 1 { pclk2 } else { 2 * pclk2 };
699+
690700
assert!(pclk2 <= sysclk);
691701

692702
// adjust flash wait states
@@ -806,6 +816,8 @@ impl CFGR {
806816
ppre1,
807817
ppre2,
808818
sysclk: sysclk.Hz(),
819+
timclk1: timclk1.Hz(),
820+
timclk2: timclk2.Hz(),
809821
pll_source: pllconf.map(|_| pll_source),
810822
}
811823
}
@@ -903,6 +915,8 @@ pub struct Clocks {
903915
ppre1: u8,
904916
ppre2: u8,
905917
sysclk: Hertz,
918+
timclk1: Hertz,
919+
timclk2: Hertz,
906920
pll_source: Option<PllSource>,
907921
}
908922

@@ -962,4 +976,14 @@ impl Clocks {
962976
pub fn sysclk(&self) -> Hertz {
963977
self.sysclk
964978
}
979+
980+
/// Returns the frequency for timers on APB1
981+
pub fn timclk1(&self) -> Hertz {
982+
self.timclk1
983+
}
984+
985+
/// Returns the frequency for timers on APB2
986+
pub fn timclk2(&self) -> Hertz {
987+
self.timclk2
988+
}
965989
}

src/timer.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use fugit::RateExtU32;
6969

7070
/// Hardware timers
7171
pub struct Timer<TIM> {
72-
clocks: Clocks,
72+
clock: Hertz,
7373
tim: TIM,
7474
timeout: Hertz,
7575
}
@@ -81,7 +81,7 @@ pub enum Event {
8181
}
8282

8383
macro_rules! hal {
84-
($($TIM:ident: ($tim:ident, $frname:ident, $apb:ident, $width:ident),)+) => {
84+
($($TIM:ident: ($tim:ident, $frname:ident, $apb:ident, $width:ident, $timclk:ident),)+) => {
8585
$(
8686
impl Periodic for Timer<$TIM> {}
8787

@@ -98,7 +98,7 @@ macro_rules! hal {
9898
self.pause();
9999

100100
self.timeout = timeout.into();
101-
let ticks = self.clocks.pclk1() / self.timeout; // TODO: Check pclk that timer is on.
101+
let ticks = self.clock / self.timeout; // TODO check pclk that timer is on
102102
let psc = u16((ticks - 1) / (1 << 16)).unwrap();
103103

104104
self.tim.psc.write(|w| unsafe { w.psc().bits(psc) });
@@ -150,8 +150,10 @@ macro_rules! hal {
150150
<$TIM>::enable(apb);
151151
<$TIM>::reset(apb);
152152

153+
let clock = clocks.$timclk();
154+
153155
let mut timer = Timer {
154-
clocks,
156+
clock,
155157
tim,
156158
timeout: 0.Hz(),
157159
};
@@ -173,9 +175,11 @@ macro_rules! hal {
173175
<$TIM>::enable(apb);
174176
<$TIM>::reset(apb);
175177

176-
let psc = clocks.pclk1() / frequency - 1;
178+
let clock = clocks.$timclk();
179+
180+
let psc = clock / frequency - 1;
177181

178-
debug_assert!(clocks.pclk1() >= frequency);
182+
debug_assert!(clock >= frequency);
179183
debug_assert!(frequency.raw() > 0);
180184
debug_assert!(psc <= core::u16::MAX.into());
181185

@@ -206,7 +210,7 @@ macro_rules! hal {
206210
});
207211

208212
Timer {
209-
clocks,
213+
clock,
210214
tim,
211215
timeout: frequency,
212216
}
@@ -277,11 +281,11 @@ macro_rules! hal {
277281
}
278282

279283
hal! {
280-
TIM2: (tim2, free_running_tim2, APB1R1, u32),
281-
TIM6: (tim6, free_running_tim6, APB1R1, u16),
282-
//TIM7: (tim7, free_running_tim7, APB1R1, u16),
283-
TIM15: (tim15, free_running_tim15, APB2, u16),
284-
TIM16: (tim16, free_running_tim16, APB2, u16),
284+
TIM2: (tim2, free_running_tim2, APB1R1, u32, timclk1),
285+
TIM6: (tim6, free_running_tim6, APB1R1, u16, timclk1),
286+
//TIM7: (tim7, free_running_tim7, APB1R1, u16, timclk1),
287+
TIM15: (tim15, free_running_tim15, APB2, u16, timclk2),
288+
TIM16: (tim16, free_running_tim16, APB2, u16, timclk2),
285289
}
286290

287291
#[cfg(any(
@@ -305,7 +309,7 @@ hal! {
305309
// feature = "stm32l4s9",
306310
))]
307311
hal! {
308-
TIM3: (tim3, free_running_tim3, APB1R1, u16),
312+
TIM3: (tim3, free_running_tim3, APB1R1, u16, timclk1),
309313
}
310314

311315
#[cfg(not(any(
@@ -316,7 +320,7 @@ hal! {
316320
feature = "stm32l462",
317321
)))]
318322
hal! {
319-
TIM7: (tim7, free_running_tim7, APB1R1, u16),
323+
TIM7: (tim7, free_running_tim7, APB1R1, u16, timclk1),
320324
}
321325

322326
#[cfg(any(
@@ -336,7 +340,7 @@ hal! {
336340
// feature = "stm32l4s9",
337341
))]
338342
hal! {
339-
TIM4: (tim4, free_running_tim4, APB1R1, u16),
340-
TIM5: (tim5, free_running_tim5, APB1R1, u32),
341-
TIM17: (tim17, free_running_tim17, APB2, u16),
343+
TIM4: (tim4, free_running_tim4, APB1R1, u16, timclk1),
344+
TIM5: (tim5, free_running_tim5, APB1R1, u32, timclk1),
345+
TIM17: (tim17, free_running_tim17, APB2, u16, timclk2),
342346
}

0 commit comments

Comments
 (0)