This repository has been archived by the owner on May 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Timers #12
Open
droogmic
wants to merge
17
commits into
nrf-rs:master
Choose a base branch
from
droogmic:timers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Timers #12
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2554f1d
Allow multiple timers in Delay and Timer
cgm616 86fd1ed
Merge branch 'master' of https://github.com/cgm616/nrf51-hal into timers
droogmic 1af2775
Configure delay and timer for TIMER{0-3}
droogmic d43f331
Major TIMER update, moving everything to timer.rs
droogmic be7ebde
Add support for RTC and refactor
droogmic 28a0573
fixes, get RTC working
droogmic 3347294
add TIMER periodic, comment for cancel
droogmic 35ad9ae
Rewrite time and fixes after review by @therealprof
droogmic 2f48eb8
Merge branch 'master' into timers
droogmic 0a6911b
Merge branch 'master' into timers
droogmic 1095368
fix Millis->Lfticks using f64
droogmic 42c91c6
Changes to timers and counters
droogmic d457597
Merge branch 'master' into timers
droogmic a6c97ab
Attempt to implement type states
droogmic a0d1b89
add Duration support to time
droogmic 0890315
rearrange imports in delay
droogmic 551feb1
further improvements
droogmic 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,55 @@ | ||
//! Delays | ||
|
||
use cast::u32; | ||
use nrf51::TIMER0; | ||
use nrf51::{TIMER0, TIMER1, TIMER2}; | ||
|
||
use hal::blocking::delay::{DelayMs, DelayUs}; | ||
|
||
/// System timer `TIMER0` as a delay provider | ||
pub struct Delay { | ||
timer: TIMER0, | ||
} | ||
|
||
impl Delay { | ||
/// Configures the TIMER0 as a delay provider | ||
pub fn new(timer: TIMER0) -> Self { | ||
timer.tasks_stop.write(|w| unsafe { w.bits(1) }); | ||
|
||
// Set counter to 24bit mode | ||
timer.bitmode.write(|w| unsafe { w.bits(2) }); | ||
|
||
// Set prescaler to 4 == 1MHz timer | ||
timer.prescaler.write(|w| unsafe { w.bits(4) }); | ||
|
||
Delay { timer } | ||
} | ||
|
||
pub fn free(self) -> TIMER0 { | ||
self.timer | ||
} | ||
} | ||
|
||
impl DelayMs<u32> for Delay { | ||
fn delay_ms(&mut self, ms: u32) { | ||
self.delay_us(ms * 1_000); | ||
} | ||
} | ||
|
||
impl DelayMs<u16> for Delay { | ||
fn delay_ms(&mut self, ms: u16) { | ||
self.delay_ms(u32(ms)); | ||
} | ||
} | ||
|
||
impl DelayMs<u8> for Delay { | ||
fn delay_ms(&mut self, ms: u8) { | ||
self.delay_ms(u32(ms)); | ||
} | ||
} | ||
|
||
impl DelayUs<u32> for Delay { | ||
fn delay_us(&mut self, us: u32) { | ||
/* Clear event in case it was used before */ | ||
self.timer.events_compare[0].write(|w| unsafe { w.bits(0) }); | ||
|
||
/* Program counter compare register with value */ | ||
self.timer.cc[0].write(|w| unsafe { w.bits(us) }); | ||
|
||
/* Clear current counter value */ | ||
self.timer.tasks_clear.write(|w| unsafe { w.bits(1) }); | ||
|
||
/* Start counting */ | ||
self.timer.tasks_start.write(|w| unsafe { w.bits(1) }); | ||
|
||
/* Busy wait for event to happen */ | ||
while self.timer.events_compare[0].read().bits() == 0 {} | ||
|
||
/* Stop counting */ | ||
self.timer.tasks_stop.write(|w| unsafe { w.bits(1) }); | ||
} | ||
} | ||
|
||
impl DelayUs<u16> for Delay { | ||
fn delay_us(&mut self, us: u16) { | ||
self.delay_us(u32(us)) | ||
} | ||
} | ||
|
||
impl DelayUs<u8> for Delay { | ||
fn delay_us(&mut self, us: u8) { | ||
self.delay_us(u32(us)) | ||
} | ||
} | ||
pub use timer::{Timer, BitMode}; | ||
pub use timer::{micros, hertz}; | ||
|
||
macro_rules! delay { | ||
( $($TIM:ty),+ ) => { | ||
$( | ||
impl DelayMs<u32> for Timer<$TIM> { | ||
fn delay_ms(&mut self, ms: u32) { | ||
self.delay_us(ms * 1_000); | ||
} | ||
} | ||
|
||
impl DelayMs<u16> for Timer<$TIM> { | ||
fn delay_ms(&mut self, ms: u16) { | ||
self.delay_ms(u32(ms)); | ||
} | ||
} | ||
|
||
impl DelayMs<u8> for Timer<$TIM> { | ||
fn delay_ms(&mut self, ms: u8) { | ||
self.delay_ms(u32(ms)); | ||
} | ||
} | ||
|
||
impl DelayUs<u32> for Timer<$TIM> { | ||
fn delay_us(&mut self, us: u32) { | ||
|
||
self.delay(micros(us)); | ||
} | ||
} | ||
|
||
impl DelayUs<u16> for Timer<$TIM> { | ||
fn delay_us(&mut self, us: u16) { | ||
self.delay_us(u32(us)) | ||
} | ||
} | ||
|
||
impl DelayUs<u8> for Timer<$TIM> { | ||
fn delay_us(&mut self, us: u8) { | ||
self.delay_us(u32(us)) | ||
} | ||
} | ||
|
||
)+ | ||
}; | ||
} | ||
|
||
delay!(TIMER0, TIMER1, TIMER2); |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
#![no_std] | ||
#![cfg_attr(feature = "rt", feature(global_asm))] | ||
#![cfg_attr(feature = "rt", feature(used))] | ||
#![feature(const_fn)] | ||
#![feature(try_from)] | ||
#![feature(duration_as_u128)] | ||
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. We should try to get rid of all those features. This should eventually be Rust 2018 stable-able. 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. Ah, I had forgotten about that:
I used |
||
#![allow(non_camel_case_types)] | ||
|
||
extern crate cast; | ||
|
Oops, something went wrong.
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.
So you've renamed
Delay
toTimer
? That'll break all some stuff, including your own examples inmicrobit
.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'll create an alias to Delay as a structural fix, as that what clients will expect. Behind the scenes though it makes no sense to have a distinction.