init project
This commit is contained in:
74
embassy-time/CHANGELOG.md
Normal file
74
embassy-time/CHANGELOG.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## 0.4.0 - 2025-01-02
|
||||
|
||||
- `embassy-time-driver` updated from v0.1 to v0.2.
|
||||
- embassy-time no longer provides an `embassy-time-queue-driver` implementation
|
||||
|
||||
## 0.3.2 - 2024-08-05
|
||||
|
||||
- Implement with_timeout()/with_deadline() method style call on Future
|
||||
- Add collapse_debuginfo to fmt.rs macros.
|
||||
|
||||
## 0.3.1 - 2024-01-11
|
||||
|
||||
- Add with\_deadline convenience function and example
|
||||
- Implement Clone for Delay
|
||||
- Make Ticker::next Send+Sync
|
||||
- Add timestamp features
|
||||
|
||||
## 0.3.0 - 2024-01-11
|
||||
|
||||
- Update `embedded-hal-async` to `1.0.0`
|
||||
- Update `embedded-hal v1` to `1.0.0`
|
||||
- Split the time driver to a separate `embassy-time-driver` crate.
|
||||
|
||||
## 0.2.0 - 2023-12-04
|
||||
|
||||
- Added tick rates in multiples of 10 kHz
|
||||
- Remove nightly and unstable-traits features in preparation for 1.75.
|
||||
- Update heapless to 0.8.
|
||||
|
||||
## 0.1.5 - 2023-10-16
|
||||
|
||||
- Added `links` key to Cargo.toml, to prevent multiple copies of this crate in the same binary.
|
||||
Needed because different copies might get different tick rates, causing
|
||||
wrong delays if the time driver is using one copy and user code is using another.
|
||||
This is especially common when mixing crates from crates.io and git.
|
||||
|
||||
## 0.1.4 - 2023-10-12
|
||||
|
||||
- Added more tick rates
|
||||
|
||||
## 0.1.3 - 2023-08-28
|
||||
|
||||
- Update `embedded-hal-async` to `1.0.0-rc.2`
|
||||
- Update `embedded-hal v1` to `1.0.0-rc.2`
|
||||
|
||||
## 0.1.2 - 2023-07-05
|
||||
|
||||
- Update `embedded-hal-async` to `0.2.0-alpha.2`.
|
||||
- Update `embedded-hal v1` to `1.0.0-alpha.11`. (Note: v0.2 support is kept unchanged).
|
||||
|
||||
## 0.1.1 - 2023-04-13
|
||||
|
||||
- Update `embedded-hal-async` to `0.2.0-alpha.1` (uses `async fn` in traits).
|
||||
- Update `embedded-hal v1` to `1.0.0-alpha.10`. (Note: v0.2 support is kept unchanged).
|
||||
- Remove dep on `embassy-sync`.
|
||||
- Fix reentrancy issues in the `std` time driver (#1177)
|
||||
- Add `Duration::from_hz()`.
|
||||
- impl `From` conversions to/from `core::time::Duration`.
|
||||
- Add `#[must_use]` to all futures.
|
||||
- Add inherent `async fn tick()` to `Ticker`, so you can use it directly without the `Stream` trait.
|
||||
- Add more tick rates.
|
||||
- impl `Default` for `Signal`
|
||||
- Remove unnecessary uses of `atomic-polyfill`
|
||||
|
||||
## 0.1.0 - 2022-08-26
|
||||
|
||||
- First release
|
||||
442
embassy-time/Cargo.toml
Normal file
442
embassy-time/Cargo.toml
Normal file
@@ -0,0 +1,442 @@
|
||||
[package]
|
||||
name = "embassy-time"
|
||||
version = "0.4.0"
|
||||
edition = "2021"
|
||||
description = "Instant and Duration for embedded no-std systems, with async timer support"
|
||||
repository = "https://github.com/embassy-rs/embassy"
|
||||
documentation = "https://docs.embassy.dev/embassy-time"
|
||||
readme = "README.md"
|
||||
license = "MIT OR Apache-2.0"
|
||||
categories = [
|
||||
"embedded",
|
||||
"no-std",
|
||||
"concurrency",
|
||||
"asynchronous",
|
||||
]
|
||||
|
||||
[package.metadata.embassy_docs]
|
||||
src_base = "https://github.com/embassy-rs/embassy/blob/embassy-time-v$VERSION/embassy-time/src/"
|
||||
src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-time/src/"
|
||||
features = ["defmt", "std"]
|
||||
target = "x86_64-unknown-linux-gnu"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["defmt", "std"]
|
||||
|
||||
[features]
|
||||
## Display the time since startup next to defmt log messages.
|
||||
## At most 1 `defmt-timestamp-uptime-*` feature can be used.
|
||||
## `defmt-timestamp-uptime` is provided for backwards compatibility (provides the same format as `uptime-us`).
|
||||
## To use this you must have a time driver provided.
|
||||
defmt-timestamp-uptime = ["defmt"]
|
||||
defmt-timestamp-uptime-s = ["defmt"]
|
||||
defmt-timestamp-uptime-ms = ["defmt"]
|
||||
defmt-timestamp-uptime-us = ["defmt"]
|
||||
defmt-timestamp-uptime-ts = ["defmt"]
|
||||
defmt-timestamp-uptime-tms = ["defmt"]
|
||||
defmt-timestamp-uptime-tus = ["defmt"]
|
||||
|
||||
#! ### Time Drivers
|
||||
|
||||
#! Usually, time drivers are defined by a HAL, or a companion crate to the HAL. For `std` and WASM
|
||||
#! environments, as well as for testing purposes, `embassy-time` provides some default time drivers
|
||||
#! that may be suitable for your use case. You can enable one of the following features to use them.
|
||||
|
||||
## Create a `MockDriver` that can be manually advanced for testing purposes.
|
||||
mock-driver = ["tick-hz-1_000_000", "dep:embassy-time-queue-utils"]
|
||||
## Create a time driver for `std` environments.
|
||||
std = ["tick-hz-1_000_000", "critical-section/std", "dep:embassy-time-queue-utils"]
|
||||
## Create a time driver for WASM.
|
||||
wasm = ["dep:wasm-bindgen", "dep:js-sys", "dep:wasm-timer", "tick-hz-1_000_000", "dep:embassy-time-queue-utils"]
|
||||
|
||||
#! ### Generic Queue
|
||||
|
||||
#! By default embassy-time uses a timer queue implementation that is faster but depends on `embassy-executor`.
|
||||
#! It will panic if you try to await any timer when using another executor.
|
||||
#!
|
||||
#! Alternatively, you can choose to use a "generic" timer queue implementation that works on any executor.
|
||||
#! To enable it, enable any of the features below.
|
||||
#!
|
||||
#! The features also set how many timers are used for the generic queue. At most one
|
||||
#! `generic-queue-*` feature can be enabled. If none is enabled, `queue_integrated` is used.
|
||||
#!
|
||||
#! When using embassy-time from libraries, you should *not* enable any `generic-queue-*` feature, to allow the
|
||||
#! end user to pick.
|
||||
|
||||
## Generic Queue with 8 timers
|
||||
generic-queue-8 = ["embassy-time-queue-utils/generic-queue-8"]
|
||||
## Generic Queue with 16 timers
|
||||
generic-queue-16 = ["embassy-time-queue-utils/generic-queue-16"]
|
||||
## Generic Queue with 32 timers
|
||||
generic-queue-32 = ["embassy-time-queue-utils/generic-queue-32"]
|
||||
## Generic Queue with 64 timers
|
||||
generic-queue-64 = ["embassy-time-queue-utils/generic-queue-64"]
|
||||
## Generic Queue with 128 timers
|
||||
generic-queue-128 = ["embassy-time-queue-utils/generic-queue-128"]
|
||||
|
||||
#! ### Tick Rate
|
||||
#!
|
||||
#! At most 1 `tick-*` feature can be enabled. If none is enabled, a default of 1MHz is used.
|
||||
#!
|
||||
#! If the time driver in use supports using arbitrary tick rates, you can enable one `tick-*`
|
||||
#! feature from your binary crate to set the tick rate. The driver will use configured tick rate.
|
||||
#! If the time driver supports a fixed tick rate, it will enable one feature itself, so you should
|
||||
#! not enable one. Check the time driver documentation for details.
|
||||
#!
|
||||
#! When using embassy-time from libraries, you should *not* enable any `tick-*` feature, to allow the
|
||||
#! end user or the driver to pick.
|
||||
#! <details>
|
||||
#! <summary>Available tick rates:</summary>
|
||||
#! <!-- Next line must be left empty for the features to render correctly! -->
|
||||
#!
|
||||
|
||||
# BEGIN TICKS
|
||||
# Generated by gen_tick.py. DO NOT EDIT.
|
||||
## 1Hz Tick Rate
|
||||
tick-hz-1 = ["embassy-time-driver/tick-hz-1"]
|
||||
## 2Hz Tick Rate
|
||||
tick-hz-2 = ["embassy-time-driver/tick-hz-2"]
|
||||
## 4Hz Tick Rate
|
||||
tick-hz-4 = ["embassy-time-driver/tick-hz-4"]
|
||||
## 8Hz Tick Rate
|
||||
tick-hz-8 = ["embassy-time-driver/tick-hz-8"]
|
||||
## 10Hz Tick Rate
|
||||
tick-hz-10 = ["embassy-time-driver/tick-hz-10"]
|
||||
## 16Hz Tick Rate
|
||||
tick-hz-16 = ["embassy-time-driver/tick-hz-16"]
|
||||
## 32Hz Tick Rate
|
||||
tick-hz-32 = ["embassy-time-driver/tick-hz-32"]
|
||||
## 64Hz Tick Rate
|
||||
tick-hz-64 = ["embassy-time-driver/tick-hz-64"]
|
||||
## 100Hz Tick Rate
|
||||
tick-hz-100 = ["embassy-time-driver/tick-hz-100"]
|
||||
## 128Hz Tick Rate
|
||||
tick-hz-128 = ["embassy-time-driver/tick-hz-128"]
|
||||
## 256Hz Tick Rate
|
||||
tick-hz-256 = ["embassy-time-driver/tick-hz-256"]
|
||||
## 512Hz Tick Rate
|
||||
tick-hz-512 = ["embassy-time-driver/tick-hz-512"]
|
||||
## 1.0kHz Tick Rate
|
||||
tick-hz-1_000 = ["embassy-time-driver/tick-hz-1_000"]
|
||||
## 1.024kHz Tick Rate
|
||||
tick-hz-1_024 = ["embassy-time-driver/tick-hz-1_024"]
|
||||
## 2.0kHz Tick Rate
|
||||
tick-hz-2_000 = ["embassy-time-driver/tick-hz-2_000"]
|
||||
## 2.048kHz Tick Rate
|
||||
tick-hz-2_048 = ["embassy-time-driver/tick-hz-2_048"]
|
||||
## 4.0kHz Tick Rate
|
||||
tick-hz-4_000 = ["embassy-time-driver/tick-hz-4_000"]
|
||||
## 4.096kHz Tick Rate
|
||||
tick-hz-4_096 = ["embassy-time-driver/tick-hz-4_096"]
|
||||
## 8.0kHz Tick Rate
|
||||
tick-hz-8_000 = ["embassy-time-driver/tick-hz-8_000"]
|
||||
## 8.192kHz Tick Rate
|
||||
tick-hz-8_192 = ["embassy-time-driver/tick-hz-8_192"]
|
||||
## 10.0kHz Tick Rate
|
||||
tick-hz-10_000 = ["embassy-time-driver/tick-hz-10_000"]
|
||||
## 16.0kHz Tick Rate
|
||||
tick-hz-16_000 = ["embassy-time-driver/tick-hz-16_000"]
|
||||
## 16.384kHz Tick Rate
|
||||
tick-hz-16_384 = ["embassy-time-driver/tick-hz-16_384"]
|
||||
## 20.0kHz Tick Rate
|
||||
tick-hz-20_000 = ["embassy-time-driver/tick-hz-20_000"]
|
||||
## 32.0kHz Tick Rate
|
||||
tick-hz-32_000 = ["embassy-time-driver/tick-hz-32_000"]
|
||||
## 32.768kHz Tick Rate
|
||||
tick-hz-32_768 = ["embassy-time-driver/tick-hz-32_768"]
|
||||
## 40.0kHz Tick Rate
|
||||
tick-hz-40_000 = ["embassy-time-driver/tick-hz-40_000"]
|
||||
## 64.0kHz Tick Rate
|
||||
tick-hz-64_000 = ["embassy-time-driver/tick-hz-64_000"]
|
||||
## 65.536kHz Tick Rate
|
||||
tick-hz-65_536 = ["embassy-time-driver/tick-hz-65_536"]
|
||||
## 80.0kHz Tick Rate
|
||||
tick-hz-80_000 = ["embassy-time-driver/tick-hz-80_000"]
|
||||
## 100.0kHz Tick Rate
|
||||
tick-hz-100_000 = ["embassy-time-driver/tick-hz-100_000"]
|
||||
## 128.0kHz Tick Rate
|
||||
tick-hz-128_000 = ["embassy-time-driver/tick-hz-128_000"]
|
||||
## 131.072kHz Tick Rate
|
||||
tick-hz-131_072 = ["embassy-time-driver/tick-hz-131_072"]
|
||||
## 160.0kHz Tick Rate
|
||||
tick-hz-160_000 = ["embassy-time-driver/tick-hz-160_000"]
|
||||
## 256.0kHz Tick Rate
|
||||
tick-hz-256_000 = ["embassy-time-driver/tick-hz-256_000"]
|
||||
## 262.144kHz Tick Rate
|
||||
tick-hz-262_144 = ["embassy-time-driver/tick-hz-262_144"]
|
||||
## 320.0kHz Tick Rate
|
||||
tick-hz-320_000 = ["embassy-time-driver/tick-hz-320_000"]
|
||||
## 512.0kHz Tick Rate
|
||||
tick-hz-512_000 = ["embassy-time-driver/tick-hz-512_000"]
|
||||
## 524.288kHz Tick Rate
|
||||
tick-hz-524_288 = ["embassy-time-driver/tick-hz-524_288"]
|
||||
## 640.0kHz Tick Rate
|
||||
tick-hz-640_000 = ["embassy-time-driver/tick-hz-640_000"]
|
||||
## 1.0MHz Tick Rate
|
||||
tick-hz-1_000_000 = ["embassy-time-driver/tick-hz-1_000_000"]
|
||||
## 1.024MHz Tick Rate
|
||||
tick-hz-1_024_000 = ["embassy-time-driver/tick-hz-1_024_000"]
|
||||
## 1.048576MHz Tick Rate
|
||||
tick-hz-1_048_576 = ["embassy-time-driver/tick-hz-1_048_576"]
|
||||
## 1.28MHz Tick Rate
|
||||
tick-hz-1_280_000 = ["embassy-time-driver/tick-hz-1_280_000"]
|
||||
## 2.0MHz Tick Rate
|
||||
tick-hz-2_000_000 = ["embassy-time-driver/tick-hz-2_000_000"]
|
||||
## 2.048MHz Tick Rate
|
||||
tick-hz-2_048_000 = ["embassy-time-driver/tick-hz-2_048_000"]
|
||||
## 2.097152MHz Tick Rate
|
||||
tick-hz-2_097_152 = ["embassy-time-driver/tick-hz-2_097_152"]
|
||||
## 2.56MHz Tick Rate
|
||||
tick-hz-2_560_000 = ["embassy-time-driver/tick-hz-2_560_000"]
|
||||
## 3.0MHz Tick Rate
|
||||
tick-hz-3_000_000 = ["embassy-time-driver/tick-hz-3_000_000"]
|
||||
## 4.0MHz Tick Rate
|
||||
tick-hz-4_000_000 = ["embassy-time-driver/tick-hz-4_000_000"]
|
||||
## 4.096MHz Tick Rate
|
||||
tick-hz-4_096_000 = ["embassy-time-driver/tick-hz-4_096_000"]
|
||||
## 4.194304MHz Tick Rate
|
||||
tick-hz-4_194_304 = ["embassy-time-driver/tick-hz-4_194_304"]
|
||||
## 5.12MHz Tick Rate
|
||||
tick-hz-5_120_000 = ["embassy-time-driver/tick-hz-5_120_000"]
|
||||
## 6.0MHz Tick Rate
|
||||
tick-hz-6_000_000 = ["embassy-time-driver/tick-hz-6_000_000"]
|
||||
## 8.0MHz Tick Rate
|
||||
tick-hz-8_000_000 = ["embassy-time-driver/tick-hz-8_000_000"]
|
||||
## 8.192MHz Tick Rate
|
||||
tick-hz-8_192_000 = ["embassy-time-driver/tick-hz-8_192_000"]
|
||||
## 8.388608MHz Tick Rate
|
||||
tick-hz-8_388_608 = ["embassy-time-driver/tick-hz-8_388_608"]
|
||||
## 9.0MHz Tick Rate
|
||||
tick-hz-9_000_000 = ["embassy-time-driver/tick-hz-9_000_000"]
|
||||
## 10.0MHz Tick Rate
|
||||
tick-hz-10_000_000 = ["embassy-time-driver/tick-hz-10_000_000"]
|
||||
## 10.24MHz Tick Rate
|
||||
tick-hz-10_240_000 = ["embassy-time-driver/tick-hz-10_240_000"]
|
||||
## 12.0MHz Tick Rate
|
||||
tick-hz-12_000_000 = ["embassy-time-driver/tick-hz-12_000_000"]
|
||||
## 16.0MHz Tick Rate
|
||||
tick-hz-16_000_000 = ["embassy-time-driver/tick-hz-16_000_000"]
|
||||
## 16.384MHz Tick Rate
|
||||
tick-hz-16_384_000 = ["embassy-time-driver/tick-hz-16_384_000"]
|
||||
## 16.777216MHz Tick Rate
|
||||
tick-hz-16_777_216 = ["embassy-time-driver/tick-hz-16_777_216"]
|
||||
## 18.0MHz Tick Rate
|
||||
tick-hz-18_000_000 = ["embassy-time-driver/tick-hz-18_000_000"]
|
||||
## 20.0MHz Tick Rate
|
||||
tick-hz-20_000_000 = ["embassy-time-driver/tick-hz-20_000_000"]
|
||||
## 20.48MHz Tick Rate
|
||||
tick-hz-20_480_000 = ["embassy-time-driver/tick-hz-20_480_000"]
|
||||
## 24.0MHz Tick Rate
|
||||
tick-hz-24_000_000 = ["embassy-time-driver/tick-hz-24_000_000"]
|
||||
## 30.0MHz Tick Rate
|
||||
tick-hz-30_000_000 = ["embassy-time-driver/tick-hz-30_000_000"]
|
||||
## 32.0MHz Tick Rate
|
||||
tick-hz-32_000_000 = ["embassy-time-driver/tick-hz-32_000_000"]
|
||||
## 32.768MHz Tick Rate
|
||||
tick-hz-32_768_000 = ["embassy-time-driver/tick-hz-32_768_000"]
|
||||
## 36.0MHz Tick Rate
|
||||
tick-hz-36_000_000 = ["embassy-time-driver/tick-hz-36_000_000"]
|
||||
## 40.0MHz Tick Rate
|
||||
tick-hz-40_000_000 = ["embassy-time-driver/tick-hz-40_000_000"]
|
||||
## 40.96MHz Tick Rate
|
||||
tick-hz-40_960_000 = ["embassy-time-driver/tick-hz-40_960_000"]
|
||||
## 48.0MHz Tick Rate
|
||||
tick-hz-48_000_000 = ["embassy-time-driver/tick-hz-48_000_000"]
|
||||
## 50.0MHz Tick Rate
|
||||
tick-hz-50_000_000 = ["embassy-time-driver/tick-hz-50_000_000"]
|
||||
## 60.0MHz Tick Rate
|
||||
tick-hz-60_000_000 = ["embassy-time-driver/tick-hz-60_000_000"]
|
||||
## 64.0MHz Tick Rate
|
||||
tick-hz-64_000_000 = ["embassy-time-driver/tick-hz-64_000_000"]
|
||||
## 65.536MHz Tick Rate
|
||||
tick-hz-65_536_000 = ["embassy-time-driver/tick-hz-65_536_000"]
|
||||
## 70.0MHz Tick Rate
|
||||
tick-hz-70_000_000 = ["embassy-time-driver/tick-hz-70_000_000"]
|
||||
## 72.0MHz Tick Rate
|
||||
tick-hz-72_000_000 = ["embassy-time-driver/tick-hz-72_000_000"]
|
||||
## 80.0MHz Tick Rate
|
||||
tick-hz-80_000_000 = ["embassy-time-driver/tick-hz-80_000_000"]
|
||||
## 81.92MHz Tick Rate
|
||||
tick-hz-81_920_000 = ["embassy-time-driver/tick-hz-81_920_000"]
|
||||
## 90.0MHz Tick Rate
|
||||
tick-hz-90_000_000 = ["embassy-time-driver/tick-hz-90_000_000"]
|
||||
## 96.0MHz Tick Rate
|
||||
tick-hz-96_000_000 = ["embassy-time-driver/tick-hz-96_000_000"]
|
||||
## 100.0MHz Tick Rate
|
||||
tick-hz-100_000_000 = ["embassy-time-driver/tick-hz-100_000_000"]
|
||||
## 110.0MHz Tick Rate
|
||||
tick-hz-110_000_000 = ["embassy-time-driver/tick-hz-110_000_000"]
|
||||
## 120.0MHz Tick Rate
|
||||
tick-hz-120_000_000 = ["embassy-time-driver/tick-hz-120_000_000"]
|
||||
## 128.0MHz Tick Rate
|
||||
tick-hz-128_000_000 = ["embassy-time-driver/tick-hz-128_000_000"]
|
||||
## 130.0MHz Tick Rate
|
||||
tick-hz-130_000_000 = ["embassy-time-driver/tick-hz-130_000_000"]
|
||||
## 131.072MHz Tick Rate
|
||||
tick-hz-131_072_000 = ["embassy-time-driver/tick-hz-131_072_000"]
|
||||
## 140.0MHz Tick Rate
|
||||
tick-hz-140_000_000 = ["embassy-time-driver/tick-hz-140_000_000"]
|
||||
## 144.0MHz Tick Rate
|
||||
tick-hz-144_000_000 = ["embassy-time-driver/tick-hz-144_000_000"]
|
||||
## 150.0MHz Tick Rate
|
||||
tick-hz-150_000_000 = ["embassy-time-driver/tick-hz-150_000_000"]
|
||||
## 160.0MHz Tick Rate
|
||||
tick-hz-160_000_000 = ["embassy-time-driver/tick-hz-160_000_000"]
|
||||
## 163.84MHz Tick Rate
|
||||
tick-hz-163_840_000 = ["embassy-time-driver/tick-hz-163_840_000"]
|
||||
## 170.0MHz Tick Rate
|
||||
tick-hz-170_000_000 = ["embassy-time-driver/tick-hz-170_000_000"]
|
||||
## 180.0MHz Tick Rate
|
||||
tick-hz-180_000_000 = ["embassy-time-driver/tick-hz-180_000_000"]
|
||||
## 190.0MHz Tick Rate
|
||||
tick-hz-190_000_000 = ["embassy-time-driver/tick-hz-190_000_000"]
|
||||
## 192.0MHz Tick Rate
|
||||
tick-hz-192_000_000 = ["embassy-time-driver/tick-hz-192_000_000"]
|
||||
## 200.0MHz Tick Rate
|
||||
tick-hz-200_000_000 = ["embassy-time-driver/tick-hz-200_000_000"]
|
||||
## 210.0MHz Tick Rate
|
||||
tick-hz-210_000_000 = ["embassy-time-driver/tick-hz-210_000_000"]
|
||||
## 220.0MHz Tick Rate
|
||||
tick-hz-220_000_000 = ["embassy-time-driver/tick-hz-220_000_000"]
|
||||
## 230.0MHz Tick Rate
|
||||
tick-hz-230_000_000 = ["embassy-time-driver/tick-hz-230_000_000"]
|
||||
## 240.0MHz Tick Rate
|
||||
tick-hz-240_000_000 = ["embassy-time-driver/tick-hz-240_000_000"]
|
||||
## 250.0MHz Tick Rate
|
||||
tick-hz-250_000_000 = ["embassy-time-driver/tick-hz-250_000_000"]
|
||||
## 256.0MHz Tick Rate
|
||||
tick-hz-256_000_000 = ["embassy-time-driver/tick-hz-256_000_000"]
|
||||
## 260.0MHz Tick Rate
|
||||
tick-hz-260_000_000 = ["embassy-time-driver/tick-hz-260_000_000"]
|
||||
## 262.144MHz Tick Rate
|
||||
tick-hz-262_144_000 = ["embassy-time-driver/tick-hz-262_144_000"]
|
||||
## 270.0MHz Tick Rate
|
||||
tick-hz-270_000_000 = ["embassy-time-driver/tick-hz-270_000_000"]
|
||||
## 280.0MHz Tick Rate
|
||||
tick-hz-280_000_000 = ["embassy-time-driver/tick-hz-280_000_000"]
|
||||
## 288.0MHz Tick Rate
|
||||
tick-hz-288_000_000 = ["embassy-time-driver/tick-hz-288_000_000"]
|
||||
## 290.0MHz Tick Rate
|
||||
tick-hz-290_000_000 = ["embassy-time-driver/tick-hz-290_000_000"]
|
||||
## 300.0MHz Tick Rate
|
||||
tick-hz-300_000_000 = ["embassy-time-driver/tick-hz-300_000_000"]
|
||||
## 320.0MHz Tick Rate
|
||||
tick-hz-320_000_000 = ["embassy-time-driver/tick-hz-320_000_000"]
|
||||
## 327.68MHz Tick Rate
|
||||
tick-hz-327_680_000 = ["embassy-time-driver/tick-hz-327_680_000"]
|
||||
## 340.0MHz Tick Rate
|
||||
tick-hz-340_000_000 = ["embassy-time-driver/tick-hz-340_000_000"]
|
||||
## 360.0MHz Tick Rate
|
||||
tick-hz-360_000_000 = ["embassy-time-driver/tick-hz-360_000_000"]
|
||||
## 380.0MHz Tick Rate
|
||||
tick-hz-380_000_000 = ["embassy-time-driver/tick-hz-380_000_000"]
|
||||
## 384.0MHz Tick Rate
|
||||
tick-hz-384_000_000 = ["embassy-time-driver/tick-hz-384_000_000"]
|
||||
## 400.0MHz Tick Rate
|
||||
tick-hz-400_000_000 = ["embassy-time-driver/tick-hz-400_000_000"]
|
||||
## 420.0MHz Tick Rate
|
||||
tick-hz-420_000_000 = ["embassy-time-driver/tick-hz-420_000_000"]
|
||||
## 440.0MHz Tick Rate
|
||||
tick-hz-440_000_000 = ["embassy-time-driver/tick-hz-440_000_000"]
|
||||
## 460.0MHz Tick Rate
|
||||
tick-hz-460_000_000 = ["embassy-time-driver/tick-hz-460_000_000"]
|
||||
## 480.0MHz Tick Rate
|
||||
tick-hz-480_000_000 = ["embassy-time-driver/tick-hz-480_000_000"]
|
||||
## 500.0MHz Tick Rate
|
||||
tick-hz-500_000_000 = ["embassy-time-driver/tick-hz-500_000_000"]
|
||||
## 512.0MHz Tick Rate
|
||||
tick-hz-512_000_000 = ["embassy-time-driver/tick-hz-512_000_000"]
|
||||
## 520.0MHz Tick Rate
|
||||
tick-hz-520_000_000 = ["embassy-time-driver/tick-hz-520_000_000"]
|
||||
## 524.288MHz Tick Rate
|
||||
tick-hz-524_288_000 = ["embassy-time-driver/tick-hz-524_288_000"]
|
||||
## 540.0MHz Tick Rate
|
||||
tick-hz-540_000_000 = ["embassy-time-driver/tick-hz-540_000_000"]
|
||||
## 560.0MHz Tick Rate
|
||||
tick-hz-560_000_000 = ["embassy-time-driver/tick-hz-560_000_000"]
|
||||
## 576.0MHz Tick Rate
|
||||
tick-hz-576_000_000 = ["embassy-time-driver/tick-hz-576_000_000"]
|
||||
## 580.0MHz Tick Rate
|
||||
tick-hz-580_000_000 = ["embassy-time-driver/tick-hz-580_000_000"]
|
||||
## 600.0MHz Tick Rate
|
||||
tick-hz-600_000_000 = ["embassy-time-driver/tick-hz-600_000_000"]
|
||||
## 620.0MHz Tick Rate
|
||||
tick-hz-620_000_000 = ["embassy-time-driver/tick-hz-620_000_000"]
|
||||
## 640.0MHz Tick Rate
|
||||
tick-hz-640_000_000 = ["embassy-time-driver/tick-hz-640_000_000"]
|
||||
## 655.36MHz Tick Rate
|
||||
tick-hz-655_360_000 = ["embassy-time-driver/tick-hz-655_360_000"]
|
||||
## 660.0MHz Tick Rate
|
||||
tick-hz-660_000_000 = ["embassy-time-driver/tick-hz-660_000_000"]
|
||||
## 680.0MHz Tick Rate
|
||||
tick-hz-680_000_000 = ["embassy-time-driver/tick-hz-680_000_000"]
|
||||
## 700.0MHz Tick Rate
|
||||
tick-hz-700_000_000 = ["embassy-time-driver/tick-hz-700_000_000"]
|
||||
## 720.0MHz Tick Rate
|
||||
tick-hz-720_000_000 = ["embassy-time-driver/tick-hz-720_000_000"]
|
||||
## 740.0MHz Tick Rate
|
||||
tick-hz-740_000_000 = ["embassy-time-driver/tick-hz-740_000_000"]
|
||||
## 760.0MHz Tick Rate
|
||||
tick-hz-760_000_000 = ["embassy-time-driver/tick-hz-760_000_000"]
|
||||
## 768.0MHz Tick Rate
|
||||
tick-hz-768_000_000 = ["embassy-time-driver/tick-hz-768_000_000"]
|
||||
## 780.0MHz Tick Rate
|
||||
tick-hz-780_000_000 = ["embassy-time-driver/tick-hz-780_000_000"]
|
||||
## 800.0MHz Tick Rate
|
||||
tick-hz-800_000_000 = ["embassy-time-driver/tick-hz-800_000_000"]
|
||||
## 820.0MHz Tick Rate
|
||||
tick-hz-820_000_000 = ["embassy-time-driver/tick-hz-820_000_000"]
|
||||
## 840.0MHz Tick Rate
|
||||
tick-hz-840_000_000 = ["embassy-time-driver/tick-hz-840_000_000"]
|
||||
## 860.0MHz Tick Rate
|
||||
tick-hz-860_000_000 = ["embassy-time-driver/tick-hz-860_000_000"]
|
||||
## 880.0MHz Tick Rate
|
||||
tick-hz-880_000_000 = ["embassy-time-driver/tick-hz-880_000_000"]
|
||||
## 900.0MHz Tick Rate
|
||||
tick-hz-900_000_000 = ["embassy-time-driver/tick-hz-900_000_000"]
|
||||
## 920.0MHz Tick Rate
|
||||
tick-hz-920_000_000 = ["embassy-time-driver/tick-hz-920_000_000"]
|
||||
## 940.0MHz Tick Rate
|
||||
tick-hz-940_000_000 = ["embassy-time-driver/tick-hz-940_000_000"]
|
||||
## 960.0MHz Tick Rate
|
||||
tick-hz-960_000_000 = ["embassy-time-driver/tick-hz-960_000_000"]
|
||||
## 980.0MHz Tick Rate
|
||||
tick-hz-980_000_000 = ["embassy-time-driver/tick-hz-980_000_000"]
|
||||
## 1.0GHz Tick Rate
|
||||
tick-hz-1_000_000_000 = ["embassy-time-driver/tick-hz-1_000_000_000"]
|
||||
## 1.31072GHz Tick Rate
|
||||
tick-hz-1_310_720_000 = ["embassy-time-driver/tick-hz-1_310_720_000"]
|
||||
## 2.62144GHz Tick Rate
|
||||
tick-hz-2_621_440_000 = ["embassy-time-driver/tick-hz-2_621_440_000"]
|
||||
## 5.24288GHz Tick Rate
|
||||
tick-hz-5_242_880_000 = ["embassy-time-driver/tick-hz-5_242_880_000"]
|
||||
# END TICKS
|
||||
|
||||
#! </details>
|
||||
|
||||
[dependencies]
|
||||
embassy-time-driver = { version = "0.2", path = "../embassy-time-driver" }
|
||||
embassy-time-queue-utils = { version = "0.1", path = "../embassy-time-queue-utils", optional = true}
|
||||
|
||||
defmt = { version = "0.3", optional = true }
|
||||
log = { version = "0.4.14", optional = true }
|
||||
|
||||
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6" }
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
|
||||
embedded-hal-async = { version = "1.0" }
|
||||
|
||||
futures-util = { version = "0.3.17", default-features = false }
|
||||
critical-section = "1.1"
|
||||
cfg-if = "1.0.0"
|
||||
|
||||
document-features = "0.2.7"
|
||||
|
||||
# WASM dependencies
|
||||
wasm-bindgen = { version = "0.2.81", optional = true }
|
||||
js-sys = { version = "0.3", optional = true }
|
||||
wasm-timer = { version = "0.2.5", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serial_test = "0.9"
|
||||
critical-section = { version = "1.1", features = ["std"] }
|
||||
embassy-executor = { version = "0.7.0", path = "../embassy-executor" }
|
||||
47
embassy-time/README.md
Normal file
47
embassy-time/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# embassy-time
|
||||
|
||||
Timekeeping, delays and timeouts.
|
||||
|
||||
Timekeeping is done with elapsed time since system boot. Time is represented in
|
||||
ticks, where the tick rate is defined either by the driver (in the case of a fixed-rate
|
||||
tick) or chosen by the user with a [tick rate](#tick-rate) feature. The chosen
|
||||
tick rate applies to everything in `embassy-time` and thus determines the maximum
|
||||
timing resolution of <code>(1 / tick_rate) seconds</code>.
|
||||
|
||||
Tick counts are 64 bits. The default tick rate of 1Mhz supports
|
||||
representing time spans of up to ~584558 years, which is big enough for all practical
|
||||
purposes and allows not having to worry about overflows.
|
||||
|
||||
## Global time driver
|
||||
|
||||
The `time` module is backed by a global "time driver" specified at build time.
|
||||
Only one driver can be active in a program.
|
||||
|
||||
All methods and structs transparently call into the active driver. This makes it
|
||||
possible for libraries to use `embassy_time` in a driver-agnostic way without
|
||||
requiring generic parameters.
|
||||
|
||||
For more details, check the [`embassy_time_driver`](https://crates.io/crates/embassy-time-driver) crate.
|
||||
|
||||
## Instants and Durations
|
||||
|
||||
[`Instant`] represents a given instant of time (relative to system boot), and [`Duration`]
|
||||
represents the duration of a span of time. They implement the math operations you'd expect,
|
||||
like addition and substraction.
|
||||
|
||||
## Delays and timeouts
|
||||
|
||||
[`Timer`] allows performing async delays. [`Ticker`] allows periodic delays without drifting over time.
|
||||
|
||||
An implementation of the `embedded-hal` delay traits is provided by [`Delay`], for compatibility
|
||||
with libraries from the ecosystem.
|
||||
|
||||
## Wall-clock time
|
||||
|
||||
The `time` module deals exclusively with a monotonically increasing tick count.
|
||||
Therefore it has no direct support for wall-clock time ("real life" datetimes
|
||||
like `2021-08-24 13:33:21`).
|
||||
|
||||
If persistence across reboots is not needed, support can be built on top of
|
||||
`embassy_time` by storing the offset between "seconds elapsed since boot"
|
||||
and "seconds since unix epoch".
|
||||
81
embassy-time/src/delay.rs
Normal file
81
embassy-time/src/delay.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use super::{Duration, Instant};
|
||||
use crate::Timer;
|
||||
|
||||
/// Blocks for at least `duration`.
|
||||
pub fn block_for(duration: Duration) {
|
||||
let expires_at = Instant::now() + duration;
|
||||
while Instant::now() < expires_at {}
|
||||
}
|
||||
|
||||
/// Type implementing async delays and blocking `embedded-hal` delays.
|
||||
///
|
||||
/// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least
|
||||
/// the amount provided, but accuracy can be affected by many factors, including interrupt usage.
|
||||
/// Make sure to use a suitable tick rate for your use case. The tick rate is defined by the currently
|
||||
/// active driver.
|
||||
#[derive(Clone)]
|
||||
pub struct Delay;
|
||||
|
||||
impl embedded_hal_1::delay::DelayNs for Delay {
|
||||
fn delay_ns(&mut self, ns: u32) {
|
||||
block_for(Duration::from_nanos(ns as u64))
|
||||
}
|
||||
|
||||
fn delay_us(&mut self, us: u32) {
|
||||
block_for(Duration::from_micros(us as u64))
|
||||
}
|
||||
|
||||
fn delay_ms(&mut self, ms: u32) {
|
||||
block_for(Duration::from_millis(ms as u64))
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal_async::delay::DelayNs for Delay {
|
||||
async fn delay_ns(&mut self, ns: u32) {
|
||||
Timer::after_nanos(ns as _).await
|
||||
}
|
||||
|
||||
async fn delay_us(&mut self, us: u32) {
|
||||
Timer::after_micros(us as _).await
|
||||
}
|
||||
|
||||
async fn delay_ms(&mut self, ms: u32) {
|
||||
Timer::after_millis(ms as _).await
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal_02::blocking::delay::DelayMs<u8> for Delay {
|
||||
fn delay_ms(&mut self, ms: u8) {
|
||||
block_for(Duration::from_millis(ms as u64))
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal_02::blocking::delay::DelayMs<u16> for Delay {
|
||||
fn delay_ms(&mut self, ms: u16) {
|
||||
block_for(Duration::from_millis(ms as u64))
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal_02::blocking::delay::DelayMs<u32> for Delay {
|
||||
fn delay_ms(&mut self, ms: u32) {
|
||||
block_for(Duration::from_millis(ms as u64))
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal_02::blocking::delay::DelayUs<u8> for Delay {
|
||||
fn delay_us(&mut self, us: u8) {
|
||||
block_for(Duration::from_micros(us as u64))
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal_02::blocking::delay::DelayUs<u16> for Delay {
|
||||
fn delay_us(&mut self, us: u16) {
|
||||
block_for(Duration::from_micros(us as u64))
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal_02::blocking::delay::DelayUs<u32> for Delay {
|
||||
fn delay_us(&mut self, us: u32) {
|
||||
block_for(Duration::from_micros(us as u64))
|
||||
}
|
||||
}
|
||||
145
embassy-time/src/driver_mock.rs
Normal file
145
embassy-time/src/driver_mock.rs
Normal file
@@ -0,0 +1,145 @@
|
||||
use core::cell::RefCell;
|
||||
use core::task::Waker;
|
||||
|
||||
use critical_section::Mutex as CsMutex;
|
||||
use embassy_time_driver::Driver;
|
||||
use embassy_time_queue_utils::Queue;
|
||||
|
||||
use crate::{Duration, Instant};
|
||||
|
||||
/// A mock driver that can be manually advanced.
|
||||
/// This is useful for testing code that works with [`Instant`] and [`Duration`].
|
||||
///
|
||||
/// This driver can also be used to test runtime functionality, such as
|
||||
/// timers, delays, etc.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// fn has_a_second_passed(reference: Instant) -> bool {
|
||||
/// Instant::now().duration_since(reference) >= Duration::from_secs(1)
|
||||
/// }
|
||||
///
|
||||
/// fn test_second_passed() {
|
||||
/// let driver = embassy_time::MockDriver::get();
|
||||
/// let reference = Instant::now();
|
||||
/// assert_eq!(false, has_a_second_passed(reference));
|
||||
/// driver.advance(Duration::from_secs(1));
|
||||
/// assert_eq!(true, has_a_second_passed(reference));
|
||||
/// }
|
||||
/// ```
|
||||
pub struct MockDriver(CsMutex<RefCell<InnerMockDriver>>);
|
||||
|
||||
embassy_time_driver::time_driver_impl!(static DRIVER: MockDriver = MockDriver::new());
|
||||
|
||||
impl MockDriver {
|
||||
/// Creates a new mock driver.
|
||||
pub const fn new() -> Self {
|
||||
Self(CsMutex::new(RefCell::new(InnerMockDriver::new())))
|
||||
}
|
||||
|
||||
/// Gets a reference to the global mock driver.
|
||||
pub fn get() -> &'static MockDriver {
|
||||
&DRIVER
|
||||
}
|
||||
|
||||
/// Resets the internal state of the mock driver
|
||||
/// This will clear and deallocate all alarms, and reset the current time to 0.
|
||||
pub fn reset(&self) {
|
||||
critical_section::with(|cs| {
|
||||
self.0.borrow(cs).replace(InnerMockDriver::new());
|
||||
});
|
||||
}
|
||||
|
||||
/// Advances the time by the specified [`Duration`].
|
||||
/// Calling any alarm callbacks that are due.
|
||||
pub fn advance(&self, duration: Duration) {
|
||||
critical_section::with(|cs| {
|
||||
let inner = &mut *self.0.borrow_ref_mut(cs);
|
||||
|
||||
inner.now += duration;
|
||||
// wake expired tasks.
|
||||
inner.queue.next_expiration(inner.now.as_ticks());
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Driver for MockDriver {
|
||||
fn now(&self) -> u64 {
|
||||
critical_section::with(|cs| self.0.borrow_ref(cs).now).as_ticks()
|
||||
}
|
||||
|
||||
fn schedule_wake(&self, at: u64, waker: &Waker) {
|
||||
critical_section::with(|cs| {
|
||||
let inner = &mut *self.0.borrow_ref_mut(cs);
|
||||
// enqueue it
|
||||
inner.queue.schedule_wake(at, waker);
|
||||
// wake it if it's in the past.
|
||||
inner.queue.next_expiration(inner.now.as_ticks());
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct InnerMockDriver {
|
||||
now: Instant,
|
||||
queue: Queue,
|
||||
}
|
||||
|
||||
impl InnerMockDriver {
|
||||
const fn new() -> Self {
|
||||
Self {
|
||||
now: Instant::from_ticks(0),
|
||||
queue: Queue::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::task::Wake;
|
||||
|
||||
use serial_test::serial;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn setup() {
|
||||
DRIVER.reset();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_advance() {
|
||||
setup();
|
||||
|
||||
let driver = MockDriver::get();
|
||||
let reference = driver.now();
|
||||
driver.advance(Duration::from_secs(1));
|
||||
assert_eq!(Duration::from_secs(1).as_ticks(), driver.now() - reference);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_schedule_wake() {
|
||||
setup();
|
||||
|
||||
static CALLBACK_CALLED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
struct MockWaker;
|
||||
|
||||
impl Wake for MockWaker {
|
||||
fn wake(self: Arc<Self>) {
|
||||
CALLBACK_CALLED.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
let waker = Arc::new(MockWaker).into();
|
||||
|
||||
let driver = MockDriver::get();
|
||||
|
||||
driver.schedule_wake(driver.now() + 1, &waker);
|
||||
assert_eq!(false, CALLBACK_CALLED.load(Ordering::Relaxed));
|
||||
driver.advance(Duration::from_secs(1));
|
||||
assert_eq!(true, CALLBACK_CALLED.load(Ordering::Relaxed));
|
||||
}
|
||||
}
|
||||
104
embassy-time/src/driver_std.rs
Normal file
104
embassy-time/src/driver_std.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use std::sync::{Condvar, Mutex};
|
||||
use std::thread;
|
||||
use std::time::{Duration as StdDuration, Instant as StdInstant};
|
||||
|
||||
use embassy_time_driver::Driver;
|
||||
use embassy_time_queue_utils::Queue;
|
||||
|
||||
struct TimeDriver {
|
||||
signaler: Signaler,
|
||||
inner: Mutex<Inner>,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
zero_instant: Option<StdInstant>,
|
||||
queue: Queue,
|
||||
}
|
||||
|
||||
embassy_time_driver::time_driver_impl!(static DRIVER: TimeDriver = TimeDriver {
|
||||
inner: Mutex::new(Inner{
|
||||
zero_instant: None,
|
||||
queue: Queue::new(),
|
||||
}),
|
||||
signaler: Signaler::new(),
|
||||
});
|
||||
|
||||
impl Inner {
|
||||
fn init(&mut self) -> StdInstant {
|
||||
*self.zero_instant.get_or_insert_with(|| {
|
||||
thread::spawn(alarm_thread);
|
||||
StdInstant::now()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Driver for TimeDriver {
|
||||
fn now(&self) -> u64 {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
let zero = inner.init();
|
||||
StdInstant::now().duration_since(zero).as_micros() as u64
|
||||
}
|
||||
|
||||
fn schedule_wake(&self, at: u64, waker: &core::task::Waker) {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
inner.init();
|
||||
if inner.queue.schedule_wake(at, waker) {
|
||||
self.signaler.signal();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn alarm_thread() {
|
||||
let zero = DRIVER.inner.lock().unwrap().zero_instant.unwrap();
|
||||
loop {
|
||||
let now = DRIVER.now();
|
||||
|
||||
let next_alarm = DRIVER.inner.lock().unwrap().queue.next_expiration(now);
|
||||
|
||||
// Ensure we don't overflow
|
||||
let until = zero
|
||||
.checked_add(StdDuration::from_micros(next_alarm))
|
||||
.unwrap_or_else(|| StdInstant::now() + StdDuration::from_secs(1));
|
||||
|
||||
DRIVER.signaler.wait_until(until);
|
||||
}
|
||||
}
|
||||
|
||||
struct Signaler {
|
||||
mutex: Mutex<bool>,
|
||||
condvar: Condvar,
|
||||
}
|
||||
|
||||
impl Signaler {
|
||||
const fn new() -> Self {
|
||||
Self {
|
||||
mutex: Mutex::new(false),
|
||||
condvar: Condvar::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn wait_until(&self, until: StdInstant) {
|
||||
let mut signaled = self.mutex.lock().unwrap();
|
||||
while !*signaled {
|
||||
let now = StdInstant::now();
|
||||
|
||||
if now >= until {
|
||||
break;
|
||||
}
|
||||
|
||||
let dur = until - now;
|
||||
let (signaled2, timeout) = self.condvar.wait_timeout(signaled, dur).unwrap();
|
||||
signaled = signaled2;
|
||||
if timeout.timed_out() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
*signaled = false;
|
||||
}
|
||||
|
||||
fn signal(&self) {
|
||||
let mut signaled = self.mutex.lock().unwrap();
|
||||
*signaled = true;
|
||||
self.condvar.notify_one();
|
||||
}
|
||||
}
|
||||
103
embassy-time/src/driver_wasm.rs
Normal file
103
embassy-time/src/driver_wasm.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use std::sync::Mutex;
|
||||
|
||||
use embassy_time_driver::Driver;
|
||||
use embassy_time_queue_utils::Queue;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_timer::Instant as StdInstant;
|
||||
|
||||
struct AlarmState {
|
||||
token: Option<f64>,
|
||||
}
|
||||
|
||||
impl AlarmState {
|
||||
const fn new() -> Self {
|
||||
Self { token: None }
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
fn setTimeout(closure: &Closure<dyn FnMut()>, millis: u32) -> f64;
|
||||
fn clearTimeout(token: f64);
|
||||
}
|
||||
|
||||
struct TimeDriver {
|
||||
inner: Mutex<Inner>,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
alarm: AlarmState,
|
||||
zero_instant: Option<StdInstant>,
|
||||
queue: Queue,
|
||||
closure: Option<Closure<dyn FnMut()>>,
|
||||
}
|
||||
|
||||
unsafe impl Send for Inner {}
|
||||
|
||||
embassy_time_driver::time_driver_impl!(static DRIVER: TimeDriver = TimeDriver {
|
||||
inner: Mutex::new(Inner{
|
||||
zero_instant: None,
|
||||
queue: Queue::new(),
|
||||
alarm: AlarmState::new(),
|
||||
closure: None,
|
||||
}),
|
||||
});
|
||||
|
||||
impl Inner {
|
||||
fn init(&mut self) -> StdInstant {
|
||||
*self.zero_instant.get_or_insert_with(StdInstant::now)
|
||||
}
|
||||
|
||||
fn now(&mut self) -> u64 {
|
||||
StdInstant::now().duration_since(self.zero_instant.unwrap()).as_micros() as u64
|
||||
}
|
||||
|
||||
fn set_alarm(&mut self, timestamp: u64) -> bool {
|
||||
if let Some(token) = self.alarm.token {
|
||||
clearTimeout(token);
|
||||
}
|
||||
|
||||
let now = self.now();
|
||||
if timestamp <= now {
|
||||
false
|
||||
} else {
|
||||
let timeout = (timestamp - now) as u32;
|
||||
let closure = self.closure.get_or_insert_with(|| Closure::new(dispatch));
|
||||
self.alarm.token = Some(setTimeout(closure, timeout / 1000));
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Driver for TimeDriver {
|
||||
fn now(&self) -> u64 {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
let zero = inner.init();
|
||||
StdInstant::now().duration_since(zero).as_micros() as u64
|
||||
}
|
||||
|
||||
fn schedule_wake(&self, at: u64, waker: &core::task::Waker) {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
inner.init();
|
||||
if inner.queue.schedule_wake(at, waker) {
|
||||
let now = inner.now();
|
||||
let mut next = inner.queue.next_expiration(now);
|
||||
while !inner.set_alarm(next) {
|
||||
let now = inner.now();
|
||||
next = inner.queue.next_expiration(now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn dispatch() {
|
||||
let inner = &mut *DRIVER.inner.lock().unwrap();
|
||||
|
||||
let now = inner.now();
|
||||
let mut next = inner.queue.next_expiration(now);
|
||||
while !inner.set_alarm(next) {
|
||||
let now = inner.now();
|
||||
next = inner.queue.next_expiration(now);
|
||||
}
|
||||
}
|
||||
295
embassy-time/src/duration.rs
Normal file
295
embassy-time/src/duration.rs
Normal file
@@ -0,0 +1,295 @@
|
||||
use core::fmt;
|
||||
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
|
||||
use super::{GCD_1K, GCD_1M, TICK_HZ};
|
||||
use crate::GCD_1G;
|
||||
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
/// Represents the difference between two [Instant](struct.Instant.html)s
|
||||
pub struct Duration {
|
||||
pub(crate) ticks: u64,
|
||||
}
|
||||
|
||||
impl Duration {
|
||||
/// The smallest value that can be represented by the `Duration` type.
|
||||
pub const MIN: Duration = Duration { ticks: u64::MIN };
|
||||
/// The largest value that can be represented by the `Duration` type.
|
||||
pub const MAX: Duration = Duration { ticks: u64::MAX };
|
||||
|
||||
/// Tick count of the `Duration`.
|
||||
pub const fn as_ticks(&self) -> u64 {
|
||||
self.ticks
|
||||
}
|
||||
|
||||
/// Convert the `Duration` to seconds, rounding down.
|
||||
pub const fn as_secs(&self) -> u64 {
|
||||
self.ticks / TICK_HZ
|
||||
}
|
||||
|
||||
/// Convert the `Duration` to milliseconds, rounding down.
|
||||
pub const fn as_millis(&self) -> u64 {
|
||||
self.ticks * (1000 / GCD_1K) / (TICK_HZ / GCD_1K)
|
||||
}
|
||||
|
||||
/// Convert the `Duration` to microseconds, rounding down.
|
||||
pub const fn as_micros(&self) -> u64 {
|
||||
self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
|
||||
}
|
||||
|
||||
/// Creates a duration from the specified number of clock ticks
|
||||
pub const fn from_ticks(ticks: u64) -> Duration {
|
||||
Duration { ticks }
|
||||
}
|
||||
|
||||
/// Creates a duration from the specified number of seconds, rounding up.
|
||||
pub const fn from_secs(secs: u64) -> Duration {
|
||||
Duration { ticks: secs * TICK_HZ }
|
||||
}
|
||||
|
||||
/// Creates a duration from the specified number of milliseconds, rounding up.
|
||||
pub const fn from_millis(millis: u64) -> Duration {
|
||||
Duration {
|
||||
ticks: div_ceil(millis * (TICK_HZ / GCD_1K), 1000 / GCD_1K),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a duration from the specified number of microseconds, rounding up.
|
||||
/// NOTE: Delays this small may be inaccurate.
|
||||
pub const fn from_micros(micros: u64) -> Duration {
|
||||
Duration {
|
||||
ticks: div_ceil(micros * (TICK_HZ / GCD_1M), 1_000_000 / GCD_1M),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a duration from the specified number of nanoseconds, rounding up.
|
||||
/// NOTE: Delays this small may be inaccurate.
|
||||
pub const fn from_nanos(nanoseconds: u64) -> Duration {
|
||||
Duration {
|
||||
ticks: div_ceil(nanoseconds * (TICK_HZ / GCD_1G), 1_000_000_000 / GCD_1G),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a duration from the specified number of seconds, rounding down.
|
||||
pub const fn from_secs_floor(secs: u64) -> Duration {
|
||||
Duration { ticks: secs * TICK_HZ }
|
||||
}
|
||||
|
||||
/// Creates a duration from the specified number of milliseconds, rounding down.
|
||||
pub const fn from_millis_floor(millis: u64) -> Duration {
|
||||
Duration {
|
||||
ticks: millis * (TICK_HZ / GCD_1K) / (1000 / GCD_1K),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a duration from the specified number of microseconds, rounding down.
|
||||
/// NOTE: Delays this small may be inaccurate.
|
||||
pub const fn from_micros_floor(micros: u64) -> Duration {
|
||||
Duration {
|
||||
ticks: micros * (TICK_HZ / GCD_1M) / (1_000_000 / GCD_1M),
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to create a duration from the specified number of seconds, rounding up.
|
||||
/// Fails if the number of seconds is too large.
|
||||
pub const fn try_from_secs(secs: u64) -> Option<Duration> {
|
||||
let Some(ticks) = secs.checked_mul(TICK_HZ) else {
|
||||
return None;
|
||||
};
|
||||
Some(Duration { ticks })
|
||||
}
|
||||
|
||||
/// Try to create a duration from the specified number of milliseconds, rounding up.
|
||||
/// Fails if the number of milliseconds is too large.
|
||||
pub const fn try_from_millis(millis: u64) -> Option<Duration> {
|
||||
let Some(value) = millis.checked_mul(TICK_HZ / GCD_1K) else {
|
||||
return None;
|
||||
};
|
||||
Some(Duration {
|
||||
ticks: div_ceil(value, 1000 / GCD_1K),
|
||||
})
|
||||
}
|
||||
|
||||
/// Try to create a duration from the specified number of microseconds, rounding up.
|
||||
/// Fails if the number of microseconds is too large.
|
||||
/// NOTE: Delays this small may be inaccurate.
|
||||
pub const fn try_from_micros(micros: u64) -> Option<Duration> {
|
||||
let Some(value) = micros.checked_mul(TICK_HZ / GCD_1M) else {
|
||||
return None;
|
||||
};
|
||||
Some(Duration {
|
||||
ticks: div_ceil(value, 1_000_000 / GCD_1M),
|
||||
})
|
||||
}
|
||||
|
||||
/// Try to create a duration from the specified number of nanoseconds, rounding up.
|
||||
/// Fails if the number of nanoseconds is too large.
|
||||
/// NOTE: Delays this small may be inaccurate.
|
||||
pub const fn try_from_nanos(nanoseconds: u64) -> Option<Duration> {
|
||||
let Some(value) = nanoseconds.checked_mul(TICK_HZ / GCD_1G) else {
|
||||
return None;
|
||||
};
|
||||
Some(Duration {
|
||||
ticks: div_ceil(value, 1_000_000_000 / GCD_1G),
|
||||
})
|
||||
}
|
||||
|
||||
/// Try to create a duration from the specified number of seconds, rounding down.
|
||||
/// Fails if the number of seconds is too large.
|
||||
pub const fn try_from_secs_floor(secs: u64) -> Option<Duration> {
|
||||
let Some(ticks) = secs.checked_mul(TICK_HZ) else {
|
||||
return None;
|
||||
};
|
||||
Some(Duration { ticks })
|
||||
}
|
||||
|
||||
/// Try to create a duration from the specified number of milliseconds, rounding down.
|
||||
/// Fails if the number of milliseconds is too large.
|
||||
pub const fn try_from_millis_floor(millis: u64) -> Option<Duration> {
|
||||
let Some(value) = millis.checked_mul(TICK_HZ / GCD_1K) else {
|
||||
return None;
|
||||
};
|
||||
Some(Duration {
|
||||
ticks: value / (1000 / GCD_1K),
|
||||
})
|
||||
}
|
||||
|
||||
/// Try to create a duration from the specified number of microseconds, rounding down.
|
||||
/// Fails if the number of microseconds is too large.
|
||||
/// NOTE: Delays this small may be inaccurate.
|
||||
pub const fn try_from_micros_floor(micros: u64) -> Option<Duration> {
|
||||
let Some(value) = micros.checked_mul(TICK_HZ / GCD_1M) else {
|
||||
return None;
|
||||
};
|
||||
Some(Duration {
|
||||
ticks: value / (1_000_000 / GCD_1M),
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates a duration corresponding to the specified Hz.
|
||||
/// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1
|
||||
/// tick. Doing so will not deadlock, but will certainly not produce the desired output.
|
||||
pub const fn from_hz(hz: u64) -> Duration {
|
||||
let ticks = {
|
||||
if hz >= TICK_HZ {
|
||||
1
|
||||
} else {
|
||||
(TICK_HZ + hz / 2) / hz
|
||||
}
|
||||
};
|
||||
Duration { ticks }
|
||||
}
|
||||
|
||||
/// Adds one Duration to another, returning a new Duration or None in the event of an overflow.
|
||||
pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
|
||||
self.ticks.checked_add(rhs.ticks).map(|ticks| Duration { ticks })
|
||||
}
|
||||
|
||||
/// Subtracts one Duration to another, returning a new Duration or None in the event of an overflow.
|
||||
pub fn checked_sub(self, rhs: Duration) -> Option<Duration> {
|
||||
self.ticks.checked_sub(rhs.ticks).map(|ticks| Duration { ticks })
|
||||
}
|
||||
|
||||
/// Multiplies one Duration by a scalar u32, returning a new Duration or None in the event of an overflow.
|
||||
pub fn checked_mul(self, rhs: u32) -> Option<Duration> {
|
||||
self.ticks.checked_mul(rhs as _).map(|ticks| Duration { ticks })
|
||||
}
|
||||
|
||||
/// Divides one Duration a scalar u32, returning a new Duration or None in the event of an overflow.
|
||||
pub fn checked_div(self, rhs: u32) -> Option<Duration> {
|
||||
self.ticks.checked_div(rhs as _).map(|ticks| Duration { ticks })
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Duration {
|
||||
type Output = Duration;
|
||||
|
||||
fn add(self, rhs: Duration) -> Duration {
|
||||
self.checked_add(rhs).expect("overflow when adding durations")
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for Duration {
|
||||
fn add_assign(&mut self, rhs: Duration) {
|
||||
*self = *self + rhs;
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Duration {
|
||||
type Output = Duration;
|
||||
|
||||
fn sub(self, rhs: Duration) -> Duration {
|
||||
self.checked_sub(rhs).expect("overflow when subtracting durations")
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign for Duration {
|
||||
fn sub_assign(&mut self, rhs: Duration) {
|
||||
*self = *self - rhs;
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<u32> for Duration {
|
||||
type Output = Duration;
|
||||
|
||||
fn mul(self, rhs: u32) -> Duration {
|
||||
self.checked_mul(rhs)
|
||||
.expect("overflow when multiplying duration by scalar")
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Duration> for u32 {
|
||||
type Output = Duration;
|
||||
|
||||
fn mul(self, rhs: Duration) -> Duration {
|
||||
rhs * self
|
||||
}
|
||||
}
|
||||
|
||||
impl MulAssign<u32> for Duration {
|
||||
fn mul_assign(&mut self, rhs: u32) {
|
||||
*self = *self * rhs;
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<u32> for Duration {
|
||||
type Output = Duration;
|
||||
|
||||
fn div(self, rhs: u32) -> Duration {
|
||||
self.checked_div(rhs)
|
||||
.expect("divide by zero error when dividing duration by scalar")
|
||||
}
|
||||
}
|
||||
|
||||
impl DivAssign<u32> for Duration {
|
||||
fn div_assign(&mut self, rhs: u32) {
|
||||
*self = *self / rhs;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Duration {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{} ticks", self.ticks)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
const fn div_ceil(num: u64, den: u64) -> u64 {
|
||||
(num + den - 1) / den
|
||||
}
|
||||
|
||||
impl TryFrom<core::time::Duration> for Duration {
|
||||
type Error = <u64 as TryFrom<u128>>::Error;
|
||||
|
||||
/// Converts using [`Duration::from_micros`]. Fails if value can not be represented as u64.
|
||||
fn try_from(value: core::time::Duration) -> Result<Self, Self::Error> {
|
||||
Ok(Self::from_micros(value.as_micros().try_into()?))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Duration> for core::time::Duration {
|
||||
/// Converts using [`Duration::as_micros`].
|
||||
fn from(value: Duration) -> Self {
|
||||
core::time::Duration::from_micros(value.as_micros())
|
||||
}
|
||||
}
|
||||
270
embassy-time/src/fmt.rs
Normal file
270
embassy-time/src/fmt.rs
Normal file
@@ -0,0 +1,270 @@
|
||||
#![macro_use]
|
||||
#![allow(unused)]
|
||||
|
||||
use core::fmt::{Debug, Display, LowerHex};
|
||||
|
||||
#[cfg(all(feature = "defmt", feature = "log"))]
|
||||
compile_error!("You may not enable both `defmt` and `log` features.");
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! assert {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::assert!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::assert!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! assert_eq {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::assert_eq!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::assert_eq!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! assert_ne {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::assert_ne!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::assert_ne!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! debug_assert {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::debug_assert!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::debug_assert!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! debug_assert_eq {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::debug_assert_eq!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::debug_assert_eq!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! debug_assert_ne {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::debug_assert_ne!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::debug_assert_ne!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! todo {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::todo!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::todo!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! unreachable {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::unreachable!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::unreachable!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! panic {
|
||||
($($x:tt)*) => {
|
||||
{
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
::core::panic!($($x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::panic!($($x)*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! trace {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
{
|
||||
#[cfg(feature = "log")]
|
||||
::log::trace!($s $(, $x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::trace!($s $(, $x)*);
|
||||
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||
let _ = ($( & $x ),*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! debug {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
{
|
||||
#[cfg(feature = "log")]
|
||||
::log::debug!($s $(, $x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::debug!($s $(, $x)*);
|
||||
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||
let _ = ($( & $x ),*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! info {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
{
|
||||
#[cfg(feature = "log")]
|
||||
::log::info!($s $(, $x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::info!($s $(, $x)*);
|
||||
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||
let _ = ($( & $x ),*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! warn {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
{
|
||||
#[cfg(feature = "log")]
|
||||
::log::warn!($s $(, $x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::warn!($s $(, $x)*);
|
||||
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||
let _ = ($( & $x ),*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! error {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
{
|
||||
#[cfg(feature = "log")]
|
||||
::log::error!($s $(, $x)*);
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::error!($s $(, $x)*);
|
||||
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||
let _ = ($( & $x ),*);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(feature = "defmt")]
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! unwrap {
|
||||
($($x:tt)*) => {
|
||||
::defmt::unwrap!($($x)*)
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! unwrap {
|
||||
($arg:expr) => {
|
||||
match $crate::fmt::Try::into_result($arg) {
|
||||
::core::result::Result::Ok(t) => t,
|
||||
::core::result::Result::Err(e) => {
|
||||
::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
|
||||
}
|
||||
}
|
||||
};
|
||||
($arg:expr, $($msg:expr),+ $(,)? ) => {
|
||||
match $crate::fmt::Try::into_result($arg) {
|
||||
::core::result::Result::Ok(t) => t,
|
||||
::core::result::Result::Err(e) => {
|
||||
::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub struct NoneError;
|
||||
|
||||
pub trait Try {
|
||||
type Ok;
|
||||
type Error;
|
||||
fn into_result(self) -> Result<Self::Ok, Self::Error>;
|
||||
}
|
||||
|
||||
impl<T> Try for Option<T> {
|
||||
type Ok = T;
|
||||
type Error = NoneError;
|
||||
|
||||
#[inline]
|
||||
fn into_result(self) -> Result<T, NoneError> {
|
||||
self.ok_or(NoneError)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> Try for Result<T, E> {
|
||||
type Ok = T;
|
||||
type Error = E;
|
||||
|
||||
#[inline]
|
||||
fn into_result(self) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Bytes<'a>(pub &'a [u8]);
|
||||
|
||||
impl<'a> Debug for Bytes<'a> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "{:#02x?}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Display for Bytes<'a> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "{:#02x?}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> LowerHex for Bytes<'a> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "{:#02x?}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "defmt")]
|
||||
impl<'a> defmt::Format for Bytes<'a> {
|
||||
fn format(&self, fmt: defmt::Formatter) {
|
||||
defmt::write!(fmt, "{:02x}", self.0)
|
||||
}
|
||||
}
|
||||
205
embassy-time/src/instant.rs
Normal file
205
embassy-time/src/instant.rs
Normal file
@@ -0,0 +1,205 @@
|
||||
use core::fmt;
|
||||
use core::ops::{Add, AddAssign, Sub, SubAssign};
|
||||
|
||||
use super::{Duration, GCD_1K, GCD_1M, TICK_HZ};
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
/// An Instant in time, based on the MCU's clock ticks since startup.
|
||||
pub struct Instant {
|
||||
ticks: u64,
|
||||
}
|
||||
|
||||
impl Instant {
|
||||
/// The smallest (earliest) value that can be represented by the `Instant` type.
|
||||
pub const MIN: Instant = Instant { ticks: u64::MIN };
|
||||
/// The largest (latest) value that can be represented by the `Instant` type.
|
||||
pub const MAX: Instant = Instant { ticks: u64::MAX };
|
||||
|
||||
/// Returns an Instant representing the current time.
|
||||
#[inline]
|
||||
pub fn now() -> Instant {
|
||||
Instant {
|
||||
ticks: embassy_time_driver::now(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an Instant from a tick count since system boot.
|
||||
pub const fn from_ticks(ticks: u64) -> Self {
|
||||
Self { ticks }
|
||||
}
|
||||
|
||||
/// Create an Instant from a microsecond count since system boot.
|
||||
pub const fn from_micros(micros: u64) -> Self {
|
||||
Self {
|
||||
ticks: micros * (TICK_HZ / GCD_1M) / (1_000_000 / GCD_1M),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an Instant from a millisecond count since system boot.
|
||||
pub const fn from_millis(millis: u64) -> Self {
|
||||
Self {
|
||||
ticks: millis * (TICK_HZ / GCD_1K) / (1000 / GCD_1K),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an Instant from a second count since system boot.
|
||||
pub const fn from_secs(seconds: u64) -> Self {
|
||||
Self {
|
||||
ticks: seconds * TICK_HZ,
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to create an Instant from a microsecond count since system boot.
|
||||
/// Fails if the number of microseconds is too large.
|
||||
pub const fn try_from_micros(micros: u64) -> Option<Self> {
|
||||
let Some(value) = micros.checked_mul(TICK_HZ / GCD_1M) else {
|
||||
return None;
|
||||
};
|
||||
Some(Self {
|
||||
ticks: value / (1_000_000 / GCD_1M),
|
||||
})
|
||||
}
|
||||
|
||||
/// Try to create an Instant from a millisecond count since system boot.
|
||||
/// Fails if the number of milliseconds is too large.
|
||||
pub const fn try_from_millis(millis: u64) -> Option<Self> {
|
||||
let Some(value) = millis.checked_mul(TICK_HZ / GCD_1K) else {
|
||||
return None;
|
||||
};
|
||||
Some(Self {
|
||||
ticks: value / (1000 / GCD_1K),
|
||||
})
|
||||
}
|
||||
|
||||
/// Try to create an Instant from a second count since system boot.
|
||||
/// Fails if the number of seconds is too large.
|
||||
pub const fn try_from_secs(seconds: u64) -> Option<Self> {
|
||||
let Some(ticks) = seconds.checked_mul(TICK_HZ) else {
|
||||
return None;
|
||||
};
|
||||
Some(Self { ticks })
|
||||
}
|
||||
|
||||
/// Tick count since system boot.
|
||||
pub const fn as_ticks(&self) -> u64 {
|
||||
self.ticks
|
||||
}
|
||||
|
||||
/// Seconds since system boot.
|
||||
pub const fn as_secs(&self) -> u64 {
|
||||
self.ticks / TICK_HZ
|
||||
}
|
||||
|
||||
/// Milliseconds since system boot.
|
||||
pub const fn as_millis(&self) -> u64 {
|
||||
self.ticks * (1000 / GCD_1K) / (TICK_HZ / GCD_1K)
|
||||
}
|
||||
|
||||
/// Microseconds since system boot.
|
||||
pub const fn as_micros(&self) -> u64 {
|
||||
self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
|
||||
}
|
||||
|
||||
/// Duration between this Instant and another Instant
|
||||
/// Panics on over/underflow.
|
||||
pub fn duration_since(&self, earlier: Instant) -> Duration {
|
||||
Duration {
|
||||
ticks: unwrap!(self.ticks.checked_sub(earlier.ticks)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Duration between this Instant and another Instant
|
||||
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
|
||||
if self.ticks < earlier.ticks {
|
||||
None
|
||||
} else {
|
||||
Some(Duration {
|
||||
ticks: self.ticks - earlier.ticks,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the duration since the "earlier" Instant.
|
||||
/// If the "earlier" instant is in the future, the duration is set to zero.
|
||||
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
|
||||
Duration {
|
||||
ticks: if self.ticks < earlier.ticks {
|
||||
0
|
||||
} else {
|
||||
self.ticks - earlier.ticks
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Duration elapsed since this Instant.
|
||||
pub fn elapsed(&self) -> Duration {
|
||||
Instant::now() - *self
|
||||
}
|
||||
|
||||
/// Adds one Duration to self, returning a new `Instant` or None in the event of an overflow.
|
||||
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
|
||||
self.ticks.checked_add(duration.ticks).map(|ticks| Instant { ticks })
|
||||
}
|
||||
|
||||
/// Subtracts one Duration to self, returning a new `Instant` or None in the event of an overflow.
|
||||
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
|
||||
self.ticks.checked_sub(duration.ticks).map(|ticks| Instant { ticks })
|
||||
}
|
||||
|
||||
/// Adds a Duration to self. In case of overflow, the maximum value is returned.
|
||||
pub fn saturating_add(mut self, duration: Duration) -> Self {
|
||||
self.ticks = self.ticks.saturating_add(duration.ticks);
|
||||
self
|
||||
}
|
||||
|
||||
/// Subtracts a Duration from self. In case of overflow, the minimum value is returned.
|
||||
pub fn saturating_sub(mut self, duration: Duration) -> Self {
|
||||
self.ticks = self.ticks.saturating_sub(duration.ticks);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Duration> for Instant {
|
||||
type Output = Instant;
|
||||
|
||||
fn add(self, other: Duration) -> Instant {
|
||||
self.checked_add(other)
|
||||
.expect("overflow when adding duration to instant")
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<Duration> for Instant {
|
||||
fn add_assign(&mut self, other: Duration) {
|
||||
*self = *self + other;
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<Duration> for Instant {
|
||||
type Output = Instant;
|
||||
|
||||
fn sub(self, other: Duration) -> Instant {
|
||||
self.checked_sub(other)
|
||||
.expect("overflow when subtracting duration from instant")
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<Duration> for Instant {
|
||||
fn sub_assign(&mut self, other: Duration) {
|
||||
*self = *self - other;
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<Instant> for Instant {
|
||||
type Output = Duration;
|
||||
|
||||
fn sub(self, other: Instant) -> Duration {
|
||||
self.duration_since(other)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Instant {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{} ticks", self.ticks)
|
||||
}
|
||||
}
|
||||
63
embassy-time/src/lib.rs
Normal file
63
embassy-time/src/lib.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
#![cfg_attr(not(any(feature = "std", feature = "wasm", test)), no_std)]
|
||||
#![allow(async_fn_in_trait)]
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![allow(clippy::new_without_default)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
//! ## Feature flags
|
||||
#![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)]
|
||||
|
||||
// This mod MUST go first, so that the others see its macros.
|
||||
pub(crate) mod fmt;
|
||||
|
||||
mod delay;
|
||||
mod duration;
|
||||
mod instant;
|
||||
mod timer;
|
||||
|
||||
#[cfg(feature = "mock-driver")]
|
||||
mod driver_mock;
|
||||
|
||||
#[cfg(feature = "mock-driver")]
|
||||
pub use driver_mock::MockDriver;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod driver_std;
|
||||
#[cfg(feature = "wasm")]
|
||||
mod driver_wasm;
|
||||
|
||||
pub use delay::{block_for, Delay};
|
||||
pub use duration::Duration;
|
||||
pub use embassy_time_driver::TICK_HZ;
|
||||
pub use instant::Instant;
|
||||
pub use timer::{with_deadline, with_timeout, Ticker, TimeoutError, Timer, WithTimeout};
|
||||
|
||||
const fn gcd(a: u64, b: u64) -> u64 {
|
||||
if b == 0 {
|
||||
a
|
||||
} else {
|
||||
gcd(b, a % b)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const GCD_1K: u64 = gcd(TICK_HZ, 1_000);
|
||||
pub(crate) const GCD_1M: u64 = gcd(TICK_HZ, 1_000_000);
|
||||
pub(crate) const GCD_1G: u64 = gcd(TICK_HZ, 1_000_000_000);
|
||||
|
||||
#[cfg(feature = "defmt-timestamp-uptime-s")]
|
||||
defmt::timestamp! {"{=u64}", Instant::now().as_secs() }
|
||||
|
||||
#[cfg(feature = "defmt-timestamp-uptime-ms")]
|
||||
defmt::timestamp! {"{=u64:ms}", Instant::now().as_millis() }
|
||||
|
||||
#[cfg(any(feature = "defmt-timestamp-uptime", feature = "defmt-timestamp-uptime-us"))]
|
||||
defmt::timestamp! {"{=u64:us}", Instant::now().as_micros() }
|
||||
|
||||
#[cfg(feature = "defmt-timestamp-uptime-ts")]
|
||||
defmt::timestamp! {"{=u64:ts}", Instant::now().as_secs() }
|
||||
|
||||
#[cfg(feature = "defmt-timestamp-uptime-tms")]
|
||||
defmt::timestamp! {"{=u64:tms}", Instant::now().as_millis() }
|
||||
|
||||
#[cfg(feature = "defmt-timestamp-uptime-tus")]
|
||||
defmt::timestamp! {"{=u64:tus}", Instant::now().as_micros() }
|
||||
276
embassy-time/src/timer.rs
Normal file
276
embassy-time/src/timer.rs
Normal file
@@ -0,0 +1,276 @@
|
||||
use core::future::{poll_fn, Future};
|
||||
use core::pin::{pin, Pin};
|
||||
use core::task::{Context, Poll};
|
||||
|
||||
use futures_util::future::{select, Either};
|
||||
use futures_util::stream::FusedStream;
|
||||
use futures_util::Stream;
|
||||
|
||||
use crate::{Duration, Instant};
|
||||
|
||||
/// Error returned by [`with_timeout`] and [`with_deadline`] on timeout.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub struct TimeoutError;
|
||||
|
||||
/// Runs a given future with a timeout.
|
||||
///
|
||||
/// If the future completes before the timeout, its output is returned. Otherwise, on timeout,
|
||||
/// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned.
|
||||
pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> {
|
||||
let timeout_fut = Timer::after(timeout);
|
||||
match select(pin!(fut), timeout_fut).await {
|
||||
Either::Left((r, _)) => Ok(r),
|
||||
Either::Right(_) => Err(TimeoutError),
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs a given future with a deadline time.
|
||||
///
|
||||
/// If the future completes before the deadline, its output is returned. Otherwise, on timeout,
|
||||
/// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned.
|
||||
pub async fn with_deadline<F: Future>(at: Instant, fut: F) -> Result<F::Output, TimeoutError> {
|
||||
let timeout_fut = Timer::at(at);
|
||||
match select(pin!(fut), timeout_fut).await {
|
||||
Either::Left((r, _)) => Ok(r),
|
||||
Either::Right(_) => Err(TimeoutError),
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides functions to run a given future with a timeout or a deadline.
|
||||
pub trait WithTimeout {
|
||||
/// Output type of the future.
|
||||
type Output;
|
||||
|
||||
/// Runs a given future with a timeout.
|
||||
///
|
||||
/// If the future completes before the timeout, its output is returned. Otherwise, on timeout,
|
||||
/// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned.
|
||||
async fn with_timeout(self, timeout: Duration) -> Result<Self::Output, TimeoutError>;
|
||||
|
||||
/// Runs a given future with a deadline time.
|
||||
///
|
||||
/// If the future completes before the deadline, its output is returned. Otherwise, on timeout,
|
||||
/// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned.
|
||||
async fn with_deadline(self, at: Instant) -> Result<Self::Output, TimeoutError>;
|
||||
}
|
||||
|
||||
impl<F: Future> WithTimeout for F {
|
||||
type Output = F::Output;
|
||||
|
||||
async fn with_timeout(self, timeout: Duration) -> Result<Self::Output, TimeoutError> {
|
||||
with_timeout(timeout, self).await
|
||||
}
|
||||
|
||||
async fn with_deadline(self, at: Instant) -> Result<Self::Output, TimeoutError> {
|
||||
with_deadline(at, self).await
|
||||
}
|
||||
}
|
||||
|
||||
/// A future that completes at a specified [Instant](struct.Instant.html).
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct Timer {
|
||||
expires_at: Instant,
|
||||
yielded_once: bool,
|
||||
}
|
||||
|
||||
impl Timer {
|
||||
/// Expire at specified [Instant](struct.Instant.html)
|
||||
pub fn at(expires_at: Instant) -> Self {
|
||||
Self {
|
||||
expires_at,
|
||||
yielded_once: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Expire after specified [Duration](struct.Duration.html).
|
||||
/// This can be used as a `sleep` abstraction.
|
||||
///
|
||||
/// Example:
|
||||
/// ``` no_run
|
||||
/// use embassy_time::{Duration, Timer};
|
||||
///
|
||||
/// #[embassy_executor::task]
|
||||
/// async fn demo_sleep_seconds() {
|
||||
/// // suspend this task for one second.
|
||||
/// Timer::after(Duration::from_secs(1)).await;
|
||||
/// }
|
||||
/// ```
|
||||
pub fn after(duration: Duration) -> Self {
|
||||
Self {
|
||||
expires_at: Instant::now() + duration,
|
||||
yielded_once: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Expire after the specified number of ticks.
|
||||
///
|
||||
/// This method is a convenience wrapper for calling `Timer::after(Duration::from_ticks())`.
|
||||
/// For more details, refer to [`Timer::after()`] and [`Duration::from_ticks()`].
|
||||
#[inline]
|
||||
pub fn after_ticks(ticks: u64) -> Self {
|
||||
Self::after(Duration::from_ticks(ticks))
|
||||
}
|
||||
|
||||
/// Expire after the specified number of nanoseconds.
|
||||
///
|
||||
/// This method is a convenience wrapper for calling `Timer::after(Duration::from_nanos())`.
|
||||
/// For more details, refer to [`Timer::after()`] and [`Duration::from_nanos()`].
|
||||
#[inline]
|
||||
pub fn after_nanos(nanos: u64) -> Self {
|
||||
Self::after(Duration::from_nanos(nanos))
|
||||
}
|
||||
|
||||
/// Expire after the specified number of microseconds.
|
||||
///
|
||||
/// This method is a convenience wrapper for calling `Timer::after(Duration::from_micros())`.
|
||||
/// For more details, refer to [`Timer::after()`] and [`Duration::from_micros()`].
|
||||
#[inline]
|
||||
pub fn after_micros(micros: u64) -> Self {
|
||||
Self::after(Duration::from_micros(micros))
|
||||
}
|
||||
|
||||
/// Expire after the specified number of milliseconds.
|
||||
///
|
||||
/// This method is a convenience wrapper for calling `Timer::after(Duration::from_millis())`.
|
||||
/// For more details, refer to [`Timer::after`] and [`Duration::from_millis()`].
|
||||
#[inline]
|
||||
pub fn after_millis(millis: u64) -> Self {
|
||||
Self::after(Duration::from_millis(millis))
|
||||
}
|
||||
|
||||
/// Expire after the specified number of seconds.
|
||||
///
|
||||
/// This method is a convenience wrapper for calling `Timer::after(Duration::from_secs())`.
|
||||
/// For more details, refer to [`Timer::after`] and [`Duration::from_secs()`].
|
||||
#[inline]
|
||||
pub fn after_secs(secs: u64) -> Self {
|
||||
Self::after(Duration::from_secs(secs))
|
||||
}
|
||||
}
|
||||
|
||||
impl Unpin for Timer {}
|
||||
|
||||
impl Future for Timer {
|
||||
type Output = ();
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
if self.yielded_once && self.expires_at <= Instant::now() {
|
||||
Poll::Ready(())
|
||||
} else {
|
||||
embassy_time_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker());
|
||||
self.yielded_once = true;
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Asynchronous stream that yields every Duration, indefinitely.
|
||||
///
|
||||
/// This stream will tick at uniform intervals, even if blocking work is performed between ticks.
|
||||
///
|
||||
/// For instance, consider the following code fragment.
|
||||
/// ``` no_run
|
||||
/// use embassy_time::{Duration, Timer};
|
||||
/// # fn foo() {}
|
||||
///
|
||||
/// #[embassy_executor::task]
|
||||
/// async fn ticker_example_0() {
|
||||
/// loop {
|
||||
/// foo();
|
||||
/// Timer::after(Duration::from_secs(1)).await;
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This fragment will not call `foo` every second.
|
||||
/// Instead, it will call it every second + the time it took to previously call `foo`.
|
||||
///
|
||||
/// Example using ticker, which will consistently call `foo` once a second.
|
||||
///
|
||||
/// ``` no_run
|
||||
/// use embassy_time::{Duration, Ticker};
|
||||
/// # fn foo(){}
|
||||
///
|
||||
/// #[embassy_executor::task]
|
||||
/// async fn ticker_example_1() {
|
||||
/// let mut ticker = Ticker::every(Duration::from_secs(1));
|
||||
/// loop {
|
||||
/// foo();
|
||||
/// ticker.next().await;
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ## Cancel safety
|
||||
/// It is safe to cancel waiting for the next tick,
|
||||
/// meaning no tick is lost if the Future is dropped.
|
||||
pub struct Ticker {
|
||||
expires_at: Instant,
|
||||
duration: Duration,
|
||||
}
|
||||
|
||||
impl Ticker {
|
||||
/// Creates a new ticker that ticks at the specified duration interval.
|
||||
pub fn every(duration: Duration) -> Self {
|
||||
let expires_at = Instant::now() + duration;
|
||||
Self { expires_at, duration }
|
||||
}
|
||||
|
||||
/// Resets the ticker back to its original state.
|
||||
/// This causes the ticker to go back to zero, even if the current tick isn't over yet.
|
||||
pub fn reset(&mut self) {
|
||||
self.expires_at = Instant::now() + self.duration;
|
||||
}
|
||||
|
||||
/// Reset the ticker at the deadline.
|
||||
/// If the deadline is in the past, the ticker will fire instantly.
|
||||
pub fn reset_at(&mut self, deadline: Instant) {
|
||||
self.expires_at = deadline + self.duration;
|
||||
}
|
||||
|
||||
/// Resets the ticker, after the specified duration has passed.
|
||||
/// If the specified duration is zero, the next tick will be after the duration of the ticker.
|
||||
pub fn reset_after(&mut self, after: Duration) {
|
||||
self.expires_at = Instant::now() + after + self.duration;
|
||||
}
|
||||
|
||||
/// Waits for the next tick.
|
||||
///
|
||||
/// ## Cancel safety
|
||||
/// The produced Future is cancel safe, meaning no tick is lost if the Future is dropped.
|
||||
pub fn next(&mut self) -> impl Future<Output = ()> + Send + Sync + '_ {
|
||||
poll_fn(|cx| {
|
||||
if self.expires_at <= Instant::now() {
|
||||
let dur = self.duration;
|
||||
self.expires_at += dur;
|
||||
Poll::Ready(())
|
||||
} else {
|
||||
embassy_time_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker());
|
||||
Poll::Pending
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Unpin for Ticker {}
|
||||
|
||||
impl Stream for Ticker {
|
||||
type Item = ();
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
if self.expires_at <= Instant::now() {
|
||||
let dur = self.duration;
|
||||
self.expires_at += dur;
|
||||
Poll::Ready(Some(()))
|
||||
} else {
|
||||
embassy_time_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker());
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FusedStream for Ticker {
|
||||
fn is_terminated(&self) -> bool {
|
||||
// `Ticker` keeps yielding values until dropped, it never terminates.
|
||||
false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user