-
Notifications
You must be signed in to change notification settings - Fork 205
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
Add initial support for embedded-hal
version 1.0
#715
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,13 @@ use cortex_m::asm; | |
use fugit::ExtU32; | ||
|
||
use crate::ehal::blocking::delay::{DelayMs, DelayUs}; | ||
#[cfg(feature = "embedded-hal-1")] | ||
use crate::ehal1::delay::DelayNs; | ||
use crate::timer_traits::InterruptDrivenTimer; | ||
|
||
const NUM_US_IN_S: u32 = 1_000_000; | ||
#[cfg(feature = "embedded-hal-1")] | ||
const NUM_NS_IN_S: u32 = 1_000_000_000; | ||
|
||
/// Delay and sleep while we do (WFI) using a timer | ||
pub struct SleepingDelay<TIM> { | ||
|
@@ -35,6 +39,34 @@ where | |
} | ||
} | ||
|
||
#[cfg(feature = "embedded-hal-1")] | ||
impl<TIM> DelayNs for SleepingDelay<TIM> | ||
where | ||
TIM: InterruptDrivenTimer, | ||
{ | ||
fn delay_ns(&mut self, ns: u32) { | ||
// Determine how many cycles we need to run for this delay, if any | ||
// Avoid timers that run longer than a second because for 48 MHz-based timers, | ||
// there is no valid divisor + cycle count greater than ~1.3s, so we'd panic. | ||
let mut loop_count: u32 = 1 + (ns / NUM_NS_IN_S); | ||
|
||
// Start the timer and sleep! | ||
self.timer.start((ns / loop_count).nanos()); | ||
self.timer.enable_interrupt(); | ||
loop { | ||
asm::wfi(); | ||
if self.timer.wait().is_ok() || self.interrupt_fired.load(atomic::Ordering::Relaxed) { | ||
self.interrupt_fired.store(false, atomic::Ordering::Relaxed); | ||
loop_count -= 1; | ||
if loop_count == 0 { | ||
break; | ||
} | ||
} | ||
} | ||
self.timer.disable_interrupt(); | ||
} | ||
} | ||
|
||
impl<TIM, TYPE> DelayUs<TYPE> for SleepingDelay<TIM> | ||
where | ||
TIM: InterruptDrivenTimer, | ||
|
@@ -49,6 +81,7 @@ where | |
let mut count: u32 = 1 + (us / NUM_US_IN_S); | ||
|
||
// Start the timer and sleep! | ||
// TODO: why does this use nanos? | ||
self.timer.start((us / count).nanos()); | ||
Comment on lines
+84
to
85
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. I must not understand fugit correctly, but it looks to me like this is treating microseconds as nanos with no conversion. What is going on here? |
||
self.timer.enable_interrupt(); | ||
loop { | ||
|
@@ -71,6 +104,6 @@ where | |
TYPE: Into<u32>, | ||
{ | ||
fn delay_ms(&mut self, ms: TYPE) { | ||
self.delay_us(ms.into() * 1_000_u32); | ||
<Self as DelayUs<_>>::delay_us(self, ms.into() * 1_000_u32); | ||
} | ||
} |
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.
I think our timers take a u32 nanosecond count, so this loop may be able to be removed. Need to verify