-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathdelay.rs
294 lines (243 loc) · 6.7 KB
/
delay.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Delay providers.
//!
//! If you don't know how large your delays will be, you'll probably want to
//! use [`Delay`]. Otherwise use [`Ets`] for delays <10ms and
//! [`FreeRtos`] for delays >=10ms.
use core::{cmp::min, time::Duration};
use esp_idf_sys::*;
#[allow(non_upper_case_globals)]
pub const BLOCK: TickType_t = TickType_t::MAX;
#[allow(non_upper_case_globals)]
pub const NON_BLOCK: TickType_t = 0;
#[allow(non_upper_case_globals)]
pub const TICK_PERIOD_MS: u32 = 1000 / configTICK_RATE_HZ;
#[repr(transparent)]
pub struct TickType(pub TickType_t);
impl TickType {
pub const fn new(ticks: TickType_t) -> Self {
Self(ticks)
}
pub const fn ticks(&self) -> TickType_t {
self.0
}
pub fn as_millis(&self) -> u64 {
self.0 as u64 * TICK_PERIOD_MS as u64
}
pub fn as_millis_u32(&self) -> u32 {
min(self.as_millis(), u32::MAX as _) as _
}
}
impl From<TickType_t> for TickType {
fn from(value: TickType_t) -> Self {
Self::new(value)
}
}
impl From<TickType> for TickType_t {
fn from(value: TickType) -> Self {
value.ticks()
}
}
impl From<Duration> for TickType {
fn from(duration: Duration) -> Self {
TickType(
((duration.as_millis() + TICK_PERIOD_MS as u128 - 1) / TICK_PERIOD_MS as u128)
as TickType_t,
)
}
}
impl From<Option<Duration>> for TickType {
fn from(duration: Option<Duration>) -> Self {
if let Some(duration) = duration {
duration.into()
} else {
TickType(BLOCK)
}
}
}
impl From<TickType> for Duration {
fn from(ticks: TickType) -> Self {
Duration::from_millis(ticks.0 as u64 * TICK_PERIOD_MS as u64)
}
}
impl From<TickType> for Option<Duration> {
fn from(ticks: TickType) -> Self {
if ticks.0 == BLOCK {
None
} else {
Some(ticks.into())
}
}
}
/// Espressif built-in delay provider for small delays
///
/// Use only for very small delays (us or a few ms at most), or else the
/// FreeRTOS IDLE tasks' might starve and the IDLE tasks' watchdog will
/// trigger.
pub struct Ets;
// No longer available in the generated bindings for ESP-IDF 5
#[cfg(not(esp_idf_version_major = "4"))]
extern "C" {
pub fn ets_delay_us(us: u32);
}
impl Ets {
pub fn delay_us(us: u32) {
unsafe {
ets_delay_us(us);
}
}
pub fn delay_ms(ms: u32) {
unsafe {
ets_delay_us(ms * 1000);
}
}
}
impl embedded_hal_0_2::blocking::delay::DelayUs<u32> for Ets {
fn delay_us(&mut self, us: u32) {
Ets::delay_us(us);
}
}
impl embedded_hal_0_2::blocking::delay::DelayUs<u16> for Ets {
fn delay_us(&mut self, us: u16) {
Ets::delay_us(us as _);
}
}
impl embedded_hal_0_2::blocking::delay::DelayUs<u8> for Ets {
fn delay_us(&mut self, us: u8) {
Ets::delay_us(us as _);
}
}
impl embedded_hal_0_2::blocking::delay::DelayMs<u32> for Ets {
fn delay_ms(&mut self, ms: u32) {
Ets::delay_ms(ms);
}
}
impl embedded_hal_0_2::blocking::delay::DelayMs<u16> for Ets {
fn delay_ms(&mut self, ms: u16) {
Ets::delay_ms(ms as _);
}
}
impl embedded_hal_0_2::blocking::delay::DelayMs<u8> for Ets {
fn delay_ms(&mut self, ms: u8) {
Ets::delay_ms(ms as _);
}
}
impl embedded_hal::delay::DelayNs for Ets {
fn delay_ns(&mut self, ns: u32) {
Ets::delay_us(ns / 1000)
}
fn delay_us(&mut self, us: u32) {
Ets::delay_us(us)
}
}
/// FreeRTOS-based delay provider for delays larger than 10ms
///
/// Ddelays smaller than 10ms used in a loop would starve the FreeRTOS IDLE
/// tasks' as they are low prio tasks and hence the the IDLE tasks' watchdog
/// will trigger.
pub struct FreeRtos;
impl FreeRtos {
pub fn delay_ms(ms: u32) {
// divide by tick length, rounding up
let ticks = ms.saturating_add(TICK_PERIOD_MS - 1) / TICK_PERIOD_MS;
unsafe {
vTaskDelay(ticks);
}
}
}
impl embedded_hal_0_2::blocking::delay::DelayUs<u32> for FreeRtos {
fn delay_us(&mut self, us: u32) {
FreeRtos::delay_ms(us / 1000);
}
}
impl embedded_hal_0_2::blocking::delay::DelayUs<u16> for FreeRtos {
fn delay_us(&mut self, us: u16) {
FreeRtos::delay_ms(us as _ / 1000);
}
}
impl embedded_hal_0_2::blocking::delay::DelayUs<u8> for FreeRtos {
fn delay_us(&mut self, us: u8) {
FreeRtos::delay_ms(us as _ / 1000);
}
}
impl embedded_hal_0_2::blocking::delay::DelayMs<u32> for FreeRtos {
fn delay_ms(&mut self, ms: u32) {
FreeRtos::delay_ms(ms);
}
}
impl embedded_hal_0_2::blocking::delay::DelayMs<u16> for FreeRtos {
fn delay_ms(&mut self, ms: u16) {
FreeRtos::delay_ms(ms as _);
}
}
impl embedded_hal_0_2::blocking::delay::DelayMs<u8> for FreeRtos {
fn delay_ms(&mut self, ms: u8) {
FreeRtos::delay_ms(ms as _);
}
}
impl embedded_hal::delay::DelayNs for FreeRtos {
fn delay_ns(&mut self, ns: u32) {
FreeRtos::delay_ns(ns / 1000000)
}
fn delay_ms(&mut self, ms: u32) {
FreeRtos::delay_ms(ms)
}
}
/// A delay provider that uses [`Ets`] for delays below a certain threshold
/// and [`FreeRtos`] for delays equal or above the threshold.
#[derive(Copy, Clone)]
pub struct Delay(u32);
impl Delay {
/// Create a delay with a default threshold of 1ms
pub const fn new_default() -> Self {
Self::new(1000)
}
pub const fn new(threshold: u32) -> Self {
Self(threshold)
}
#[deprecated = "Use delay_ns instead"]
pub fn delay_us(&self, us: u32) {
self.delay_ns(us)
}
pub fn delay_ns(&self, ns: u32) {
if ns < self.0 {
Ets::delay_ns(ns);
} else {
FreeRtos::delay_ns(ns);
}
}
pub fn delay_ms(&self, ms: u32) {
if ms * 1000 < self.0 {
Ets::delay_ms(ms);
} else {
FreeRtos::delay_ms(ms);
}
}
}
impl embedded_hal::delay::DelayNs for Delay {
fn delay_ns(&mut self, us: u32) {
Delay::delay_ns(self, us)
}
fn delay_ms(&mut self, ms: u32) {
Delay::delay_ms(self, ms)
}
}
impl embedded_hal_0_2::blocking::delay::DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
Delay::delay_ns(self, us as _);
}
}
impl embedded_hal_0_2::blocking::delay::DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
Delay::delay_ns(self, us);
}
}
impl embedded_hal_0_2::blocking::delay::DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
Delay::delay_ms(self, ms as _)
}
}
impl embedded_hal_0_2::blocking::delay::DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
Delay::delay_ms(self, ms)
}
}