Skip to content

Commit 5dca501

Browse files
newAMadamgreig
authored andcommitted
cortex-m: add support for embedded-hal 1.0 delays (#504)
1 parent 2ed23a5 commit 5dca501

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

cortex-m/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ bare-metal = { version = "0.2.4", features = ["const-fn"] }
2121
critical-section = { version = "1.0.0", optional = true }
2222
volatile-register = "0.2.0"
2323
bitfield = "0.13.2"
24-
embedded-hal = "0.2.4"
24+
eh0 = { package = "embedded-hal", version = "0.2.4" }
25+
eh1 = { package = "embedded-hal", version = "1.0.0" }
2526

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

cortex-m/src/delay.rs

Lines changed: 30 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,83 @@ impl Delay {
7575
}
7676
}
7777

78-
impl DelayMs<u32> for Delay {
78+
impl eh0::blocking::delay::DelayMs<u32> for Delay {
7979
#[inline]
8080
fn delay_ms(&mut self, ms: u32) {
8181
Delay::delay_ms(self, ms);
8282
}
8383
}
8484

8585
// This is a workaround to allow `delay_ms(42)` construction without specifying a type.
86-
impl DelayMs<i32> for Delay {
86+
impl eh0::blocking::delay::DelayMs<i32> for Delay {
8787
#[inline(always)]
8888
fn delay_ms(&mut self, ms: i32) {
8989
assert!(ms >= 0);
9090
Delay::delay_ms(self, ms as u32);
9191
}
9292
}
9393

94-
impl DelayMs<u16> for Delay {
94+
impl eh0::blocking::delay::DelayMs<u16> for Delay {
9595
#[inline(always)]
9696
fn delay_ms(&mut self, ms: u16) {
9797
Delay::delay_ms(self, u32::from(ms));
9898
}
9999
}
100100

101-
impl DelayMs<u8> for Delay {
101+
impl eh0::blocking::delay::DelayMs<u8> for Delay {
102102
#[inline(always)]
103103
fn delay_ms(&mut self, ms: u8) {
104104
Delay::delay_ms(self, u32::from(ms));
105105
}
106106
}
107107

108-
impl DelayUs<u32> for Delay {
108+
impl eh0::blocking::delay::DelayUs<u32> for Delay {
109109
#[inline]
110110
fn delay_us(&mut self, us: u32) {
111111
Delay::delay_us(self, us);
112112
}
113113
}
114114

115115
// This is a workaround to allow `delay_us(42)` construction without specifying a type.
116-
impl DelayUs<i32> for Delay {
116+
impl eh0::blocking::delay::DelayUs<i32> for Delay {
117117
#[inline(always)]
118118
fn delay_us(&mut self, us: i32) {
119119
assert!(us >= 0);
120120
Delay::delay_us(self, us as u32);
121121
}
122122
}
123123

124-
impl DelayUs<u16> for Delay {
124+
impl eh0::blocking::delay::DelayUs<u16> for Delay {
125125
#[inline(always)]
126126
fn delay_us(&mut self, us: u16) {
127127
Delay::delay_us(self, u32::from(us))
128128
}
129129
}
130130

131-
impl DelayUs<u8> for Delay {
131+
impl eh0::blocking::delay::DelayUs<u8> for Delay {
132132
#[inline(always)]
133133
fn delay_us(&mut self, us: u8) {
134134
Delay::delay_us(self, u32::from(us))
135135
}
136136
}
137+
138+
impl DelayNs for Delay {
139+
#[inline]
140+
fn delay_ns(&mut self, ns: u32) {
141+
// from the rp2040-hal:
142+
let us = ns / 1000 + if ns % 1000 == 0 { 0 } else { 1 };
143+
// With rustc 1.73, this can be replaced by:
144+
// let us = ns.div_ceil(1000);
145+
Delay::delay_us(self, us)
146+
}
147+
148+
#[inline]
149+
fn delay_us(&mut self, us: u32) {
150+
Delay::delay_us(self, us)
151+
}
152+
153+
#[inline]
154+
fn delay_ms(&mut self, ms: u32) {
155+
Delay::delay_ms(self, ms)
156+
}
157+
}

0 commit comments

Comments
 (0)