-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve time keeping with Timer and RTC #94
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
07e1221
New timer methods
thalesfragoso a10e734
Adds micros_since method to timers
thalesfragoso 8eed700
adds select_frequency method to rtc
thalesfragoso 1b6a080
Rename rtc methods
thalesfragoso 91847a3
Change Timer<SYST> to match Timer<TIMx>
thalesfragoso f91f704
small changes
thalesfragoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
use crate::hal::timer::{CountDown, Periodic}; | ||
use crate::pac::{DBGMCU as DBG, TIM1, TIM2, TIM3, TIM4}; | ||
use cast::{u16, u32}; | ||
use cast::{u16, u32, u64}; | ||
use cortex_m::peripheral::syst::SystClkSource; | ||
use cortex_m::peripheral::SYST; | ||
use nb; | ||
|
@@ -43,6 +43,10 @@ impl Timer<SYST> { | |
timer.start(timeout); | ||
timer | ||
} | ||
|
||
pub fn release(self) -> SYST { | ||
self.tim | ||
} | ||
} | ||
|
||
impl CountDownTimer<SYST> { | ||
|
@@ -60,15 +64,36 @@ impl CountDownTimer<SYST> { | |
} | ||
} | ||
|
||
/// Resets the timer | ||
pub fn reset(&mut self) { | ||
// According to the Cortex-M3 Generic User Guide, the interrupt request is only generated | ||
// when the counter goes from 1 to 0, so writing zero should not trigger an interrupt | ||
self.tim.clear_current(); | ||
} | ||
|
||
/// Returns the number of microseconds since the last update event. | ||
/// *NOTE:* This method is not a very good candidate to keep track of time, because | ||
/// it is very easy to lose an update event. | ||
pub fn micros_since(&self) -> u32 { | ||
let reload_value = SYST::get_reload(); | ||
let timer_clock = u64(self.clk.0); | ||
let ticks = u64(reload_value - SYST::get_current()); | ||
|
||
// It is safe to make this cast since the maximum ticks is (2^24 - 1) and the minimum sysclk | ||
// is 4Mhz, which gives a maximum period of ~4.2 seconds which is < (2^32 - 1) microsenconds | ||
u32(1_000_000 * ticks / timer_clock).unwrap() | ||
} | ||
|
||
/// Stops the timer | ||
pub fn stop(&mut self) { | ||
pub fn stop(mut self) -> Timer<SYST> { | ||
self.tim.disable_counter(); | ||
let Self {tim, clk} = self; | ||
Timer {tim, clk} | ||
} | ||
|
||
/// Releases the SYST | ||
pub fn release(mut self) -> SYST { | ||
self.stop(); | ||
self.tim | ||
pub fn release(self) -> SYST { | ||
self.stop().release() | ||
} | ||
} | ||
|
||
|
@@ -157,8 +182,7 @@ macro_rules! hal { | |
} | ||
|
||
/// Stops the timer | ||
pub fn stop(mut self) -> Timer<$TIMX> { | ||
self.unlisten(Event::Update); | ||
pub fn stop(self) -> Timer<$TIMX> { | ||
self.tim.cr1.modify(|_, w| w.cen().clear_bit()); | ||
let Self { tim, clk } = self; | ||
Timer { tim, clk } | ||
|
@@ -173,6 +197,33 @@ macro_rules! hal { | |
pub fn release(self) -> $TIMX { | ||
self.stop().release() | ||
} | ||
|
||
/// Returns the number of microseconds since the last update event. | ||
/// *NOTE:* This method is not a very good candidate to keep track of time, because | ||
/// it is very easy to lose an update event. | ||
pub fn micros_since(&self) -> u32 { | ||
let timer_clock = self.clk.0; | ||
let psc = u32(self.tim.psc.read().psc().bits()); | ||
|
||
// freq_divider is always bigger than 0, since (psc + 1) is always less than | ||
// timer_clock | ||
let freq_divider = u64(timer_clock / (psc + 1)); | ||
let cnt = u64(self.tim.cnt.read().cnt().bits()); | ||
|
||
// It is safe to make this cast, because the maximum timer period in this HAL is | ||
// 1s (1Hz), then 1 second < (2^32 - 1) microseconds | ||
u32(1_000_000 * cnt / freq_divider).unwrap() | ||
} | ||
|
||
/// Resets the counter and generates an update event | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment out of date, if I'm reading the function correctly, it does not generate an update event There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, sorry, probably missed that |
||
pub fn reset(&mut self) { | ||
// Sets the URS bit to prevent an interrupt from being triggered by | ||
// the UG bit | ||
self.tim.cr1.modify(|_, w| w.urs().set_bit()); | ||
|
||
self.tim.egr.write(|w| w.ug().set_bit()); | ||
self.tim.cr1.modify(|_, w| w.urs().clear_bit()); | ||
} | ||
} | ||
|
||
impl CountDown for CountDownTimer<$TIMX> { | ||
|
@@ -197,11 +248,7 @@ macro_rules! hal { | |
self.tim.arr.write(|w| w.arr().bits(arr) ); | ||
|
||
// Trigger an update event to load the prescaler value to the clock | ||
self.tim.egr.write(|w| w.ug().set_bit()); | ||
// The above line raises an update event which will indicate | ||
// that the timer is already finished. Since this is not the case, | ||
// it should be cleared | ||
self.clear_update_interrupt_flag(); | ||
self.reset(); | ||
|
||
// start counter | ||
self.tim.cr1.modify(|_, w| w.cen().set_bit()); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, this comment is a bit hard to understand without knowlege of the RTC because it's not clear what
counter
is. Perhaps change it toSelects the frequency of the timer
, though that doesn't feel quite right to me either