From 66928e1b7b72fa86d6f5b708c15f62ab220eb69e Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Wed, 29 Nov 2023 13:41:56 +0300 Subject: [PATCH] delay at least --- CHANGELOG.md | 5 ++++- Cargo.toml | 4 ++-- src/dwt.rs | 9 ++++++++- src/timer/hal_02.rs | 24 +++++++++++++++--------- src/timer/hal_1.rs | 24 ++++++++++++++---------- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97e85fa1..781025be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] -- Added an example to show how to do DMA with UART (Rx only) [#698] + - Added an example to show how to do DMA with UART (Rx only) [#698] + - bump embedded-hal to `1.0-rc2` ### Fixed + - Wait at least given time in `embedded-hal` `delay` + ## [v0.18.0] - 2023-11-19 ### Changed diff --git a/Cargo.toml b/Cargo.toml index 1f0085b4..ab54f675 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ void = { default-features = false, version = "1.0.2" } embedded-hal = { features = ["unproven"], version = "0.2.7" } embedded-hal-nb = "1.0.0-rc.1" display-interface = { version = "0.4.1", optional = true } -fugit = "0.3.6" +fugit = "0.3.7" fugit-timer = "0.1.3" rtic-monotonic = { version = "1.0", optional = true } systick-monotonic = { version = "1.0", optional = true } @@ -55,7 +55,7 @@ version = "0.3.14" default-features = false [dependencies.embedded-hal-one] -version = "=1.0.0-rc.1" +version = "=1.0.0-rc.2" package = "embedded-hal" [dependencies.stm32_i2s_v12x] diff --git a/src/dwt.rs b/src/dwt.rs index ed847150..45bad835 100644 --- a/src/dwt.rs +++ b/src/dwt.rs @@ -114,7 +114,14 @@ impl> embedded_hal::blocking::delay::DelayMs for Delay { } } -impl embedded_hal_one::delay::DelayUs for Delay { +impl embedded_hal_one::delay::DelayNs for Delay { + fn delay_ns(&mut self, ns: u32) { + // Convert us to ticks + let start = DWT::cycle_count(); + let ticks = (ns as u64 * self.clock.raw() as u64) / 1_000_000_000; + Delay::delay_ticks(start, ticks); + } + fn delay_us(&mut self, us: u32) { // Convert us to ticks let start = DWT::cycle_count(); diff --git a/src/timer/hal_02.rs b/src/timer/hal_02.rs index f0f234ad..e7417cf8 100644 --- a/src/timer/hal_02.rs +++ b/src/timer/hal_02.rs @@ -7,7 +7,7 @@ use embedded_hal::{ blocking::delay::{DelayMs, DelayUs}, timer::{Cancel, CountDown, Periodic}, }; -use fugit::{ExtU32, HertzU32 as Hertz, TimerDurationU32}; +use fugit::{ExtU32Ceil, HertzU32 as Hertz, TimerDurationU32}; use void::Void; use super::{ @@ -16,36 +16,42 @@ use super::{ }; impl DelayUs for SysDelay { + #[inline] fn delay_us(&mut self, us: u32) { - self.delay(us.micros()) + self.delay(us.micros_at_least()) } } impl DelayMs for SysDelay { + #[inline] fn delay_ms(&mut self, ms: u32) { - self.delay_us(ms * 1_000); + self.delay(ms.millis_at_least()); } } impl DelayUs for SysDelay { + #[inline] fn delay_us(&mut self, us: u16) { self.delay_us(us as u32) } } impl DelayMs for SysDelay { + #[inline] fn delay_ms(&mut self, ms: u16) { self.delay_ms(ms as u32); } } impl DelayUs for SysDelay { + #[inline] fn delay_us(&mut self, us: u8) { self.delay_us(us as u32) } } impl DelayMs for SysDelay { + #[inline] fn delay_ms(&mut self, ms: u8) { self.delay_ms(ms as u32); } @@ -198,40 +204,40 @@ where impl DelayUs for Delay { /// Sleep for `us` microseconds fn delay_us(&mut self, us: u32) { - self.delay(us.micros()) + self.delay(us.micros_at_least()) } } impl DelayMs for Delay { /// Sleep for `ms` milliseconds fn delay_ms(&mut self, ms: u32) { - self.delay(ms.millis()) + self.delay(ms.millis_at_least()) } } impl DelayUs for Delay { /// Sleep for `us` microseconds fn delay_us(&mut self, us: u16) { - self.delay((us as u32).micros()) + self.delay((us as u32).micros_at_least()) } } impl DelayMs for Delay { /// Sleep for `ms` milliseconds fn delay_ms(&mut self, ms: u16) { - self.delay((ms as u32).millis()) + self.delay((ms as u32).millis_at_least()) } } impl DelayUs for Delay { /// Sleep for `us` microseconds fn delay_us(&mut self, us: u8) { - self.delay((us as u32).micros()) + self.delay((us as u32).micros_at_least()) } } impl DelayMs for Delay { /// Sleep for `ms` milliseconds fn delay_ms(&mut self, ms: u8) { - self.delay((ms as u32).millis()) + self.delay((ms as u32).millis_at_least()) } } diff --git a/src/timer/hal_1.rs b/src/timer/hal_1.rs index 3f835473..ad6386ad 100644 --- a/src/timer/hal_1.rs +++ b/src/timer/hal_1.rs @@ -4,28 +4,32 @@ //! a 16-bit prescaler. use core::convert::Infallible; -use embedded_hal_one::delay::DelayUs; +use embedded_hal_one::delay::DelayNs; use super::{Delay, Instance, PwmChannel, SysDelay, WithPwm}; -use fugit::ExtU32; +use fugit::ExtU32Ceil; -impl DelayUs for SysDelay { - fn delay_us(&mut self, us: u32) { - self.delay(us.micros()); +impl DelayNs for SysDelay { + fn delay_ns(&mut self, ns: u32) { + self.delay(ns.nanos_at_least()); } fn delay_ms(&mut self, ms: u32) { - self.delay_us(ms * 1_000); + self.delay(ms.millis_at_least()); } } -impl DelayUs for Delay { +impl DelayNs for Delay { + fn delay_ns(&mut self, ns: u32) { + self.delay(ns.micros_at_least()); + } + fn delay_us(&mut self, us: u32) { - self.delay(us.micros()); + self.delay(us.micros_at_least()); } fn delay_ms(&mut self, ms: u32) { - self.delay(ms.millis()); + self.delay(ms.millis_at_least()); } } @@ -36,7 +40,7 @@ impl embedded_hal_one::pwm::ErrorType for impl embedded_hal_one::pwm::SetDutyCycle for PwmChannel { - fn get_max_duty_cycle(&self) -> u16 { + fn max_duty_cycle(&self) -> u16 { self.get_max_duty() } fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {