Skip to content

Commit 002fefd

Browse files
committed
Duration u32
1 parent a2bc3c7 commit 002fefd

File tree

19 files changed

+325
-274
lines changed

19 files changed

+325
-274
lines changed

embassy-executor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ trybuild = "1.0"
5858
embassy-sync = { path = "../embassy-sync" }
5959

6060
[features]
61+
tick-u32 = []
6162

6263
## Enable nightly-only features
6364
nightly = ["embassy-executor-macros/nightly"]

embassy-executor/src/raw/timer_queue.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ use core::cell::Cell;
44

55
use super::TaskRef;
66

7+
#[cfg(feature = "tick-u32")]
8+
pub type TickType = u32;
9+
10+
#[cfg(not(feature = "tick-u32"))]
11+
pub type TickType = u64;
12+
713
#[cfg(feature = "_timer-item-payload")]
814
macro_rules! define_opaque {
915
($size:tt) => {
@@ -52,7 +58,7 @@ pub struct TimerQueueItem {
5258
pub next: Cell<Option<TaskRef>>,
5359

5460
/// The time at which this item expires.
55-
pub expires_at: Cell<u64>,
61+
pub expires_at: Cell<TickType>,
5662

5763
/// Some implementation-defined, zero-initialized piece of data.
5864
#[cfg(feature = "_timer-item-payload")]

embassy-net/src/time.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#![allow(unused)]
22

3-
use embassy_time::{Duration, Instant};
3+
use embassy_time::{Duration, Instant, TickType};
44
use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant};
55

66
pub(crate) fn instant_to_smoltcp(instant: Instant) -> SmolInstant {
77
SmolInstant::from_micros(instant.as_micros() as i64)
88
}
99

1010
pub(crate) fn instant_from_smoltcp(instant: SmolInstant) -> Instant {
11-
Instant::from_micros(instant.total_micros() as u64)
11+
Instant::from_micros(instant.total_micros() as TickType)
1212
}
1313

1414
pub(crate) fn duration_to_smoltcp(duration: Duration) -> SmolDuration {
15-
SmolDuration::from_micros(duration.as_micros())
15+
SmolDuration::from_micros(duration.as_micros() as u64)
1616
}
1717

1818
pub(crate) fn duration_from_smoltcp(duration: SmolDuration) -> Duration {
19-
Duration::from_micros(duration.total_micros())
19+
Duration::from_micros(duration.total_micros() as TickType)
2020
}

embassy-stm32/src/adc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(crate) trait SealedAdcChannel<T> {
8484
#[allow(unused)]
8585
pub(crate) fn blocking_delay_us(us: u32) {
8686
#[cfg(feature = "time")]
87-
embassy_time::block_for(embassy_time::Duration::from_micros(us as u64));
87+
embassy_time::block_for(embassy_time::Duration::from_micros(us as embassy_time::DurationType));
8888
#[cfg(not(feature = "time"))]
8989
{
9090
let freq = unsafe { crate::rcc::get_freqs() }.sys.to_hertz().unwrap().0 as u64;

embassy-stm32/src/eth/generic_phy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use core::task::Context;
44

55
#[cfg(feature = "time")]
6-
use embassy_time::{Duration, Timer};
6+
use embassy_time::{Duration, DurationType, Timer};
77
#[cfg(feature = "time")]
88
use futures_util::FutureExt;
99

@@ -79,7 +79,7 @@ impl GenericPhy {
7979
// TODO: Factor out to shared functionality
8080
fn blocking_delay_us(us: u32) {
8181
#[cfg(feature = "time")]
82-
embassy_time::block_for(Duration::from_micros(us as u64));
82+
embassy_time::block_for(Duration::from_micros(us as DurationType));
8383
#[cfg(not(feature = "time"))]
8484
{
8585
let freq = unsafe { crate::rcc::get_freqs() }.sys.to_hertz().unwrap().0 as u64;

embassy-stm32/src/time_driver.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::sync::atomic::{compiler_fence, AtomicU32, Ordering};
66
use critical_section::CriticalSection;
77
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
88
use embassy_sync::blocking_mutex::Mutex;
9-
use embassy_time_driver::{Driver, TICK_HZ};
9+
use embassy_time_driver::{Driver, TICK_HZ, TickType};
1010
use embassy_time_queue_utils::Queue;
1111
use stm32_metapac::timer::{regs, TimGp16};
1212

@@ -190,20 +190,20 @@ fn regs_gp16() -> TimGp16 {
190190
// corresponds to the next period.
191191
//
192192
// `period` is a 32bit integer, so It overflows on 2^32 * 2^15 / 32768 seconds of uptime, which is 136 years.
193-
fn calc_now(period: u32, counter: u16) -> u64 {
194-
((period as u64) << 15) + ((counter as u32 ^ ((period & 1) << 15)) as u64)
193+
fn calc_now(period: u32, counter: u16) -> TickType {
194+
(((period as u64) << 15) + ((counter as u32 ^ ((period & 1) << 15)) as u64)) as TickType
195195
}
196196

197197
struct AlarmState {
198-
timestamp: Cell<u64>,
198+
timestamp: Cell<TickType>,
199199
}
200200

201201
unsafe impl Send for AlarmState {}
202202

203203
impl AlarmState {
204204
const fn new() -> Self {
205205
Self {
206-
timestamp: Cell::new(u64::MAX),
206+
timestamp: Cell::new(TickType::MAX),
207207
}
208208
}
209209
}
@@ -300,7 +300,7 @@ impl RtcDriver {
300300
// We only modify the period from the timer interrupt, so we know this can't race.
301301
let period = self.period.load(Ordering::Relaxed) + 1;
302302
self.period.store(period, Ordering::Relaxed);
303-
let t = (period as u64) << 15;
303+
let t = ((period as u64) << 16) as TickType;
304304

305305
critical_section::with(move |cs| {
306306
r.dier().modify(move |w| {
@@ -445,7 +445,7 @@ impl RtcDriver {
445445
})
446446
}
447447

448-
fn set_alarm(&self, cs: CriticalSection, timestamp: u64) -> bool {
448+
fn set_alarm(&self, cs: CriticalSection, timestamp: TickType) -> bool {
449449
let r = regs_gp16();
450450

451451
let n = 0;
@@ -457,7 +457,7 @@ impl RtcDriver {
457457
// Disarm the alarm and return `false` to indicate that.
458458
r.dier().modify(|w| w.set_ccie(n + 1, false));
459459

460-
self.alarm.borrow(cs).timestamp.set(u64::MAX);
460+
self.alarm.borrow(cs).timestamp.set(TickType::MAX);
461461

462462
return false;
463463
}
@@ -479,7 +479,7 @@ impl RtcDriver {
479479
// It is the caller's responsibility to handle this ambiguity.
480480
r.dier().modify(|w| w.set_ccie(n + 1, false));
481481

482-
self.alarm.borrow(cs).timestamp.set(u64::MAX);
482+
self.alarm.borrow(cs).timestamp.set(TickType::MAX);
483483

484484
return false;
485485
}
@@ -490,7 +490,7 @@ impl RtcDriver {
490490
}
491491

492492
impl Driver for RtcDriver {
493-
fn now(&self) -> u64 {
493+
fn now(&self) -> TickType {
494494
let r = regs_gp16();
495495

496496
let period = self.period.load(Ordering::Relaxed);
@@ -499,7 +499,7 @@ impl Driver for RtcDriver {
499499
calc_now(period, counter)
500500
}
501501

502-
fn schedule_wake(&self, at: u64, waker: &core::task::Waker) {
502+
fn schedule_wake(&self, at: TickType, waker: &core::task::Waker) {
503503
critical_section::with(|cs| {
504504
let mut queue = self.queue.borrow(cs).borrow_mut();
505505

embassy-time-driver/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-time-
2626
target = "x86_64-unknown-linux-gnu"
2727

2828
[features]
29+
tick-u32 = []
30+
2931
#! ### Tick Rate
3032
#!
3133
#! At most 1 `tick-*` feature can be enabled. If none is enabled, a default of 1MHz is used.

embassy-time-driver/src/lib.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
//! struct MyDriver{} // not public!
2828
//!
2929
//! impl Driver for MyDriver {
30-
//! fn now(&self) -> u64 {
30+
//! fn now(&self) -> TickType {
3131
//! todo!()
3232
//! }
3333
//!
34-
//! fn schedule_wake(&self, at: u64, waker: &Waker) {
34+
//! fn schedule_wake(&self, at: TickType, waker: &Waker) {
3535
//! todo!()
3636
//! }
3737
//! }
@@ -62,15 +62,15 @@
6262
//! }
6363
//!
6464
//! impl MyDriver {
65-
//! fn set_alarm(&self, cs: &CriticalSection, at: u64) -> bool {
65+
//! fn set_alarm(&self, cs: &CriticalSection, at: TickType) -> bool {
6666
//! todo!()
6767
//! }
6868
//! }
6969
//!
7070
//! impl Driver for MyDriver {
71-
//! fn now(&self) -> u64 { todo!() }
71+
//! fn now(&self) -> TickType { todo!() }
7272
//!
73-
//! fn schedule_wake(&self, at: u64, waker: &Waker) {
73+
//! fn schedule_wake(&self, at: TickType, waker: &Waker) {
7474
//! critical_section::with(|cs| {
7575
//! let mut queue = self.queue.borrow(cs).borrow_mut();
7676
//! if queue.schedule_wake(at, waker) {
@@ -88,8 +88,8 @@
8888
//!
8989
//! Instead of the usual "trait + generic params" approach, calls from embassy to the driver are done via `extern` functions.
9090
//!
91-
//! `embassy` internally defines the driver function as `extern "Rust" { fn _embassy_time_now() -> u64; }` and calls it.
92-
//! The driver crate defines the function as `#[no_mangle] fn _embassy_time_now() -> u64`. The linker will resolve the
91+
//! `embassy` internally defines the driver function as `extern "Rust" { fn _embassy_time_now() -> TickType; }` and calls it.
92+
//! The driver crate defines the function as `#[no_mangle] fn _embassy_time_now() -> TickType`. The linker will resolve the
9393
//! calls from the `embassy` crate to call into the driver crate.
9494
//!
9595
//! If there is none or multiple drivers in the crate tree, linking will fail.
@@ -112,7 +112,8 @@ mod tick;
112112
/// Ticks per second of the global timebase.
113113
///
114114
/// This value is specified by the [`tick-*` Cargo features](crate#tick-rate)
115-
pub const TICK_HZ: u64 = tick::TICK_HZ;
115+
pub const TICK_HZ: TickType = tick::TICK_HZ;
116+
pub use tick::TickType;
116117

117118
/// Time driver
118119
pub trait Driver: Send + Sync + 'static {
@@ -126,27 +127,27 @@ pub trait Driver: Send + Sync + 'static {
126127
/// 10_000 years from now.). This means if your hardware only has 16bit/32bit timers
127128
/// you MUST extend them to 64-bit, for example by counting overflows in software,
128129
/// or chaining multiple timers together.
129-
fn now(&self) -> u64;
130+
fn now(&self) -> TickType;
130131

131132
/// Schedules a waker to be awoken at moment `at`.
132133
/// If this moment is in the past, the waker might be awoken immediately.
133-
fn schedule_wake(&self, at: u64, waker: &Waker);
134+
fn schedule_wake(&self, at: TickType, waker: &Waker);
134135
}
135136

136137
extern "Rust" {
137-
fn _embassy_time_now() -> u64;
138-
fn _embassy_time_schedule_wake(at: u64, waker: &Waker);
138+
fn _embassy_time_now() -> TickType;
139+
fn _embassy_time_schedule_wake(at: TickType, waker: &Waker);
139140
}
140141

141142
/// See [`Driver::now`]
142143
#[inline]
143-
pub fn now() -> u64 {
144+
pub fn now() -> TickType {
144145
unsafe { _embassy_time_now() }
145146
}
146147

147148
/// Schedule the given waker to be woken at `at`.
148149
#[inline]
149-
pub fn schedule_wake(at: u64, waker: &Waker) {
150+
pub fn schedule_wake(at: TickType, waker: &Waker) {
150151
unsafe { _embassy_time_schedule_wake(at, waker) }
151152
}
152153

@@ -160,13 +161,13 @@ macro_rules! time_driver_impl {
160161

161162
#[no_mangle]
162163
#[inline]
163-
fn _embassy_time_now() -> u64 {
164+
fn _embassy_time_now() -> TickType {
164165
<$t as $crate::Driver>::now(&$name)
165166
}
166167

167168
#[no_mangle]
168169
#[inline]
169-
fn _embassy_time_schedule_wake(at: u64, waker: &core::task::Waker) {
170+
fn _embassy_time_schedule_wake(at: TickType, waker: &core::task::Waker) {
170171
<$t as $crate::Driver>::schedule_wake(&$name, at, waker);
171172
}
172173
};

0 commit comments

Comments
 (0)