Skip to content
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

Provide a delay driver based on SysTick #257

Merged
merged 5 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
various NVIC functions, replacing the previous use of `Nr` from bare-metal.
- Associated const `PTR` is introduced to Core Peripherals to
eventually replace the existing `ptr()` API.
- A delay driver based on SysTick.

## [v0.6.2] - 2020-01-12

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ links = "cortex-m" # prevent multiple versions of this crate to be linked toget
bare-metal = { version = "0.2.0", features = ["const-fn"] }
volatile-register = "0.2.0"
bitfield = "0.13.2"
embedded-hal = "0.2.4"

[package.metadata.docs.rs]
targets = [
Expand Down
119 changes: 119 additions & 0 deletions src/delay.rs
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))
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mod macros;
pub mod asm;
#[cfg(armv8m)]
pub mod cmse;
pub mod delay;
pub mod interrupt;
#[cfg(all(not(armv6m), not(armv8m_base)))]
pub mod itm;
Expand Down