Skip to content

Commit cb3a5c6

Browse files
committed
cortex-m: add support for embedded-hal 1.0 delays
1 parent 6b3a5b7 commit cb3a5c6

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

cortex-m/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Breaking changes
1111

1212
- `NVIC::request()` no longer requires `&mut self`.
13+
- `embedded-hal` version 0.2 delay implementations now required the `eh0` feature.
1314

1415
### Added
1516
- Updated `SCB.ICSR.VECTACTIVE`/`SCB::vect_active()` to be 9 bits instead of 8.
@@ -22,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2223
- Add `std` and `serde` crate features for improved host-side ITM decode functionality when working with the downstream `itm`, `cargo-rtic-scope` crates (#363, #366).
2324
- Added the ability to name the statics generated by `singleton!()` for better debuggability (#364, #380).
2425
- Added `critical-section-single-core` feature which provides an implementation for the `critical_section` crate for single-core systems, based on disabling all interrupts. (#447)
26+
- Added support for `embedded-hal` version 1 delay traits.
2527

2628
### Fixed
2729
- Fixed `singleton!()` statics sometimes ending up in `.data` instead of `.bss` (#364, #380).

cortex-m/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ links = "cortex-m" # prevent multiple versions of this crate to be linked toget
2020
critical-section = "1.0.0"
2121
volatile-register = "0.2.2"
2222
bitfield = "0.13.2"
23-
embedded-hal = "0.2.4"
23+
eh0 = { package = "embedded-hal", version = "0.2.4", optional = true }
24+
eh1 = { package = "embedded-hal", version = "1.0.0" }
2425

2526
[dependencies.serde]
2627
version = "1"

cortex-m/src/delay.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A delay driver based on SysTick.
22
33
use crate::peripheral::{syst::SystClkSource, SYST};
4-
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
4+
use eh1::delay::DelayNs;
55

66
/// System timer (SysTick) as a delay provider.
77
pub struct Delay {
@@ -75,62 +75,87 @@ impl Delay {
7575
}
7676
}
7777

78-
impl DelayMs<u32> for Delay {
78+
#[cfg(feature = "eh0")]
79+
impl eh0::blocking::delay::DelayMs<u32> for Delay {
7980
#[inline]
8081
fn delay_ms(&mut self, ms: u32) {
8182
Delay::delay_ms(self, ms);
8283
}
8384
}
8485

8586
// This is a workaround to allow `delay_ms(42)` construction without specifying a type.
86-
impl DelayMs<i32> for Delay {
87+
#[cfg(feature = "eh0")]
88+
impl eh0::blocking::delay::DelayMs<i32> for Delay {
8789
#[inline(always)]
8890
fn delay_ms(&mut self, ms: i32) {
8991
assert!(ms >= 0);
9092
Delay::delay_ms(self, ms as u32);
9193
}
9294
}
9395

94-
impl DelayMs<u16> for Delay {
96+
#[cfg(feature = "eh0")]
97+
impl eh0::blocking::delay::DelayMs<u16> for Delay {
9598
#[inline(always)]
9699
fn delay_ms(&mut self, ms: u16) {
97100
Delay::delay_ms(self, u32::from(ms));
98101
}
99102
}
100103

101-
impl DelayMs<u8> for Delay {
104+
#[cfg(feature = "eh0")]
105+
impl eh0::blocking::delay::DelayMs<u8> for Delay {
102106
#[inline(always)]
103107
fn delay_ms(&mut self, ms: u8) {
104108
Delay::delay_ms(self, u32::from(ms));
105109
}
106110
}
107111

108-
impl DelayUs<u32> for Delay {
112+
#[cfg(feature = "eh0")]
113+
impl eh0::blocking::delay::DelayUs<u32> for Delay {
109114
#[inline]
110115
fn delay_us(&mut self, us: u32) {
111116
Delay::delay_us(self, us);
112117
}
113118
}
114119

115120
// This is a workaround to allow `delay_us(42)` construction without specifying a type.
116-
impl DelayUs<i32> for Delay {
121+
#[cfg(feature = "eh0")]
122+
impl eh0::blocking::delay::DelayUs<i32> for Delay {
117123
#[inline(always)]
118124
fn delay_us(&mut self, us: i32) {
119125
assert!(us >= 0);
120126
Delay::delay_us(self, us as u32);
121127
}
122128
}
123129

124-
impl DelayUs<u16> for Delay {
130+
#[cfg(feature = "eh0")]
131+
impl eh0::blocking::delay::DelayUs<u16> for Delay {
125132
#[inline(always)]
126133
fn delay_us(&mut self, us: u16) {
127134
Delay::delay_us(self, u32::from(us))
128135
}
129136
}
130137

131-
impl DelayUs<u8> for Delay {
138+
#[cfg(feature = "eh0")]
139+
impl eh0::blocking::delay::DelayUs<u8> for Delay {
132140
#[inline(always)]
133141
fn delay_us(&mut self, us: u8) {
134142
Delay::delay_us(self, u32::from(us))
135143
}
136144
}
145+
146+
impl DelayNs for Delay {
147+
#[inline]
148+
fn delay_ns(&mut self, ns: u32) {
149+
Delay::delay_us(self, ns.saturating_add(999) / 1000)
150+
}
151+
152+
#[inline]
153+
fn delay_us(&mut self, us: u32) {
154+
Delay::delay_us(self, us)
155+
}
156+
157+
#[inline]
158+
fn delay_ms(&mut self, ms: u32) {
159+
Delay::delay_ms(self, ms)
160+
}
161+
}

0 commit comments

Comments
 (0)