Skip to content

Commit 980b2e4

Browse files
dtjones-atsemvertescher
authored andcommitted
Added seperate calculations for tim clock freq.
1 parent 07ed598 commit 980b2e4

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

src/rcc.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ impl RccExt for RCC {
1919
pclk1: None,
2020
pclk2: None,
2121
sysclk: None,
22+
timclk1: None,
23+
timclk2: None,
2224
},
2325
}
2426
}
@@ -92,6 +94,8 @@ pub struct CFGR {
9294
pclk1: Option<u32>,
9395
pclk2: Option<u32>,
9496
sysclk: Option<u32>,
97+
timclk1: Option<u32>,
98+
timclk2: Option<u32>,
9599
}
96100

97101
impl CFGR {
@@ -127,6 +131,22 @@ impl CFGR {
127131
self
128132
}
129133

134+
pub fn timclk1<F>(mut self, freq: F) -> Self
135+
where
136+
F: Into<Hertz>,
137+
{
138+
self.timclk1 = Some(freq.into().0);
139+
self
140+
}
141+
142+
pub fn timclk2<F>(mut self, freq: F) -> Self
143+
where
144+
F: Into<Hertz>,
145+
{
146+
self.timclk2 = Some(freq.into().0);
147+
self
148+
}
149+
130150
pub fn freeze(self) -> Clocks {
131151
let flash = unsafe { &(*FLASH::ptr()) };
132152
let rcc = unsafe { &*RCC::ptr() };
@@ -148,6 +168,8 @@ impl CFGR {
148168
pclk1: Hertz(hclk),
149169
pclk2: Hertz(hclk),
150170
sysclk: Hertz(sysclk),
171+
timclk1: Hertz(hclk),
172+
timclk2: Hertz(hclk),
151173
}
152174
} else if sysclk == HSI && hclk < sysclk {
153175
let hpre_bits = match sysclk / hclk {
@@ -180,6 +202,8 @@ impl CFGR {
180202
pclk1: Hertz(hclk),
181203
pclk2: Hertz(hclk),
182204
sysclk: Hertz(sysclk),
205+
timclk1: Hertz(hclk),
206+
timclk2: Hertz(hclk),
183207
}
184208
} else {
185209
assert!(sysclk <= 216_000_000 && sysclk >= 24_000_000);
@@ -242,6 +266,11 @@ impl CFGR {
242266
let pclk1 = hclk / ppre1;
243267
let pclk2 = hclk / ppre2;
244268

269+
270+
//Assumes TIMPRE bit of RCC_DCKCFGR1 is reset (0)
271+
let timclk1 = if ppre1 == 1 {pclk1} else {2 * pclk1};
272+
let timclk2 = if ppre2 == 1 {pclk2} else {2 * pclk2};
273+
245274
// Adjust flash wait states
246275
flash.acr.write(|w| {
247276
w.latency().bits(if sysclk <= 30_000_000 {
@@ -296,6 +325,8 @@ impl CFGR {
296325
pclk1: Hertz(pclk1),
297326
pclk2: Hertz(pclk2),
298327
sysclk: Hertz(sysclk),
328+
timclk1: Hertz(timclk1),
329+
timclk2: Hertz(timclk2),
299330
}
300331
}
301332
}
@@ -310,6 +341,8 @@ pub struct Clocks {
310341
pclk1: Hertz,
311342
pclk2: Hertz,
312343
sysclk: Hertz,
344+
timclk1: Hertz,
345+
timclk2: Hertz,
313346
}
314347

315348
impl Clocks {
@@ -332,4 +365,14 @@ impl Clocks {
332365
pub fn sysclk(&self) -> Hertz {
333366
self.sysclk
334367
}
368+
369+
/// Returns the frequency for timers on APB1
370+
pub fn timclk1(&self) -> Hertz {
371+
self.timclk1
372+
}
373+
374+
/// Returns the frequency for timers on APB1
375+
pub fn timclk2(&self) -> Hertz {
376+
self.timclk2
377+
}
335378
}

src/timer.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub enum Event {
2222
}
2323

2424
macro_rules! hal {
25-
($($TIM:ident: ($tim:ident, $timXen:ident, $timXrst:ident, $apb:ident),)+) => {
25+
($($TIM:ident: ($tim:ident, $timXen:ident, $timXrst:ident, $apb:ident, $timclk:ident),)+) => {
2626
$(
2727
impl Periodic for Timer<$TIM> {}
2828

@@ -39,7 +39,7 @@ macro_rules! hal {
3939

4040
self.timeout = timeout.into();
4141
let frequency = self.timeout.0;
42-
let ticks = self.clocks.pclk1().0 / frequency;
42+
let ticks = self.clocks.$timclk().0 / frequency;
4343
let psc = u16((ticks - 1) / (1 << 16)).unwrap();
4444

4545
self.tim.psc.write(|w| unsafe { w.psc().bits(psc) });
@@ -123,8 +123,8 @@ macro_rules! hal {
123123

124124
// TODO: Add support for missing timers
125125
hal! {
126-
TIM2: (tim2, tim2en, tim2rst, APB1),
127-
TIM3: (tim3, tim3en, tim3rst, APB1),
128-
TIM4: (tim4, tim4en, tim4rst, APB1),
129-
TIM5: (tim5, tim5en, tim5rst, APB1),
126+
TIM2: (tim2, tim2en, tim2rst, APB1, timclk1),
127+
TIM3: (tim3, tim3en, tim3rst, APB1, timclk1),
128+
TIM4: (tim4, tim4en, tim4rst, APB1, timclk1),
129+
TIM5: (tim5, tim5en, tim5rst, APB1, timclk1),
130130
}

0 commit comments

Comments
 (0)