-
Notifications
You must be signed in to change notification settings - Fork 168
Provide a delay driver based on SysTick #257
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
Merged
+126
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//! A delay driver based on SysTick. | ||
|
||
use crate::peripheral::{syst::SystClkSource, SYST}; | ||
use embedded_hal::blocking::delay::{DelayMs, DelayUs}; | ||
|
||
/// System timer (SysTick) as a delay provider. | ||
pub struct Delay { | ||
syst: SYST, | ||
ahb_frequency: u32, | ||
} | ||
|
||
impl Delay { | ||
/// Configures the system timer (SysTick) as a delay provider. | ||
/// | ||
/// `ahb_frequency` is a frequency of the AHB bus in Hz. | ||
#[inline] | ||
pub fn new(mut syst: SYST, ahb_frequency: u32) -> Self { | ||
syst.set_clock_source(SystClkSource::Core); | ||
|
||
Delay { syst, ahb_frequency } | ||
} | ||
|
||
/// Releases the system timer (SysTick) resource. | ||
#[inline] | ||
pub fn free(self) -> SYST { | ||
self.syst | ||
} | ||
|
||
fn _delay_us(&mut self, us: u32) { | ||
let ticks = (us as u64) * (self.ahb_frequency as u64) / 1_000_000; | ||
|
||
let full_cycles = ticks >> 24; | ||
if full_cycles > 0 { | ||
self.syst.set_reload(0xffffff); | ||
self.syst.clear_current(); | ||
self.syst.enable_counter(); | ||
|
||
for _ in 0..full_cycles { | ||
while !self.syst.has_wrapped() {} | ||
} | ||
} | ||
|
||
let ticks = (ticks & 0xffffff) as u32; | ||
if ticks > 1 { | ||
self.syst.set_reload(ticks - 1); | ||
self.syst.clear_current(); | ||
self.syst.enable_counter(); | ||
|
||
while !self.syst.has_wrapped() {} | ||
} | ||
|
||
self.syst.disable_counter(); | ||
} | ||
} | ||
|
||
impl DelayMs<u32> for Delay { | ||
#[inline] | ||
fn delay_ms(&mut self, mut ms: u32) { | ||
// 4294967 is the highest u32 value which you can multiply by 1000 without overflow | ||
while ms > 4294967 { | ||
self.delay_us(4294967000u32); | ||
ms -= 4294967; | ||
} | ||
self.delay_us(ms * 1_000); | ||
} | ||
} | ||
|
||
// This is a workaround to allow `delay_ms(42)` construction without specifying a type. | ||
impl DelayMs<i32> for Delay { | ||
#[inline(always)] | ||
fn delay_ms(&mut self, ms: i32) { | ||
assert!(ms >= 0); | ||
self.delay_ms(ms as u32); | ||
} | ||
} | ||
|
||
impl DelayMs<u16> for Delay { | ||
#[inline(always)] | ||
fn delay_ms(&mut self, ms: u16) { | ||
self.delay_ms(u32::from(ms)); | ||
} | ||
} | ||
|
||
impl DelayMs<u8> for Delay { | ||
#[inline(always)] | ||
fn delay_ms(&mut self, ms: u8) { | ||
self.delay_ms(u32::from(ms)); | ||
} | ||
} | ||
|
||
impl DelayUs<u32> for Delay { | ||
#[inline] | ||
fn delay_us(&mut self, us: u32) { | ||
self._delay_us(us); | ||
} | ||
} | ||
|
||
// This is a workaround to allow `delay_us(42)` construction without specifying a type. | ||
impl DelayUs<i32> for Delay { | ||
#[inline(always)] | ||
fn delay_us(&mut self, us: i32) { | ||
assert!(us >= 0); | ||
self.delay_us(us as u32); | ||
} | ||
} | ||
|
||
impl DelayUs<u16> for Delay { | ||
#[inline(always)] | ||
fn delay_us(&mut self, us: u16) { | ||
self.delay_us(u32::from(us)) | ||
} | ||
} | ||
|
||
impl DelayUs<u8> for Delay { | ||
#[inline(always)] | ||
fn delay_us(&mut self, us: u8) { | ||
self.delay_us(u32::from(us)) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! Prelude | ||
|
||
pub use embedded_hal::prelude::*; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.