aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar bors[bot] <26634292+bors[bot]@users.noreply.github.com> 2020-08-25 22:08:29 +0000
committerGravatar GitHub <noreply@github.com> 2020-08-25 22:08:29 +0000
commit13e04bd4c4750c27772dcdb7154e38b9b7776826 (patch)
treebbfb1d2350bc7f2e6e488e5f1a83769ddaaa0add
parent6dcd9f4689a83545f088aa43e3ef3238cac38c08 (diff)
parent2b50dce2d4bdde42ed069f721488179c1a499d3d (diff)
downloadcortex-m-13e04bd4c4750c27772dcdb7154e38b9b7776826.tar.gz
cortex-m-13e04bd4c4750c27772dcdb7154e38b9b7776826.tar.zst
cortex-m-13e04bd4c4750c27772dcdb7154e38b9b7776826.zip
Merge #257
257: Provide a delay driver based on SysTick r=thalesfragoso a=Disasm Co-authored-by: Vadim Kaushan <admin@disasm.info>
-rw-r--r--CHANGELOG.md1
-rw-r--r--Cargo.toml1
-rw-r--r--src/delay.rs119
-rw-r--r--src/lib.rs2
-rw-r--r--src/prelude.rs3
5 files changed, 126 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e6ad454..558f07a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/Cargo.toml b/Cargo.toml
index e995d82..2e55f6a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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 = [
diff --git a/src/delay.rs b/src/delay.rs
new file mode 100644
index 0000000..051151f
--- /dev/null
+++ b/src/delay.rs
@@ -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))
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 89f420d..5dbb8f5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -61,10 +61,12 @@ 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;
pub mod peripheral;
+pub mod prelude;
pub mod register;
pub use crate::peripheral::Peripherals;
diff --git a/src/prelude.rs b/src/prelude.rs
new file mode 100644
index 0000000..bc47cc0
--- /dev/null
+++ b/src/prelude.rs
@@ -0,0 +1,3 @@
+//! Prelude
+
+pub use embedded_hal::prelude::*;