Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay::delay #1298

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion esp-hal-smartled/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//!
//! ```rust,ignore
//! let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
//! let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).unwrap();
//! let rmt = Rmt::new(peripherals.RMT, 80.MHz(), &clocks).unwrap();
//!
//! let rmt_buffer = smartLedBuffer!(1);
//! let mut led = SmartLedsAdapter::new(rmt.channel0, io.pins.gpio2, rmt_buffer);
Expand Down
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- ESP32-C6 / ESP32-H2: Implement `ETM` for general purpose timers (#1274)
- `interrupt::enable` now has a direct CPU enable counter part, `interrupt::enable_direct` (#1310)
- `Delay::delay(time: fugit::MicrosDurationU64)`

### Fixed

Expand All @@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Remove `Ext32` and `RateExtU64` from prelude
- Prefer mutable references over moving for DMA transactions (#1238)
- Support runtime interrupt binding, adapt GPIO driver (#1231)
- Renamed `eh1` feature to `embedded-hal`, feature-gated `[email protected]` trait implementations (#1273)
Expand Down
21 changes: 19 additions & 2 deletions esp-hal/src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//! [embedded-hal]: https://docs.rs/embedded-hal/0.2.7/embedded_hal/index.html

use fugit::HertzU64;
pub use fugit::MicrosDurationU64;

/// Delay driver
///
Expand All @@ -37,7 +38,7 @@ impl Delay {
/// Delay for the specified number of milliseconds
pub fn delay_millis(&self, ms: u32) {
for _ in 0..ms {
self.delay_micros(1000u32);
self.delay_micros(1000);
}
}
}
Expand All @@ -49,7 +50,7 @@ where
{
fn delay_ms(&mut self, ms: T) {
for _ in 0..ms.into() {
self.delay_micros(1000u32);
self.delay_micros(1000);
}
}
}
Expand Down Expand Up @@ -87,6 +88,15 @@ mod implementation {
}
}

/// Delay for the specified time
pub fn delay(&self, time: MicrosDurationU64) {
let t0 = SystemTimer::now();
let rate: HertzU64 = MicrosDurationU64::from_ticks(1).into_rate();
let clocks = time.ticks() * (self.freq / rate);

while SystemTimer::now().wrapping_sub(t0) & SystemTimer::BIT_MASK <= clocks {}
}

/// Delay for the specified number of microseconds
pub fn delay_micros(&self, us: u32) {
let t0 = SystemTimer::now();
Expand Down Expand Up @@ -118,6 +128,13 @@ mod implementation {
}
}

/// Delay for the specified time
pub fn delay(&self, time: MicrosDurationU64) {
let rate: HertzU64 = MicrosDurationU64::from_ticks(1).into_rate();
let clocks = time.ticks() * (self.freq / rate);
xtensa_lx::timer::delay(clocks as u32);
}

/// Delay for the specified number of microseconds
pub fn delay_micros(&self, us: u32) {
let clocks = us as u64 * (self.freq / HertzU64::MHz(1));
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
//! mosi,
//! miso,
//! cs,
//! 100u32.kHz(),
//! 100.kHz(),
//! SpiMode::Mode0,
//! &clocks,
//! )
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! peripherals.I2C0,
//! io.pins.gpio1,
//! io.pins.gpio2,
//! 100u32.kHz(),
//! 100.kHz(),
//! &clocks,
//! );
//! loop {
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/i2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! peripherals.I2S0,
//! Standard::Philips,
//! DataFormat::Data16Channel16,
//! 44100u32.Hz(),
//! 44100.Hz(),
//! dma_channel.configure(
//! false,
//! &mut tx_descriptors,
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/lcd_cam/lcd/i8080.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! lcd_cam.lcd,
//! channel.tx,
//! tx_pins,
//! 20u32.MHz(),
//! 20.MHz(),
//! Config::default(),
//! &clocks,
//! )
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/ledc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! .configure(timer::config::Config {
//! duty: timer::config::Duty::Duty5Bit,
//! clock_source: timer::LSClockSource::APBClk,
//! frequency: 24u32.kHz(),
//! frequency: 24.kHz(),
//! })
//! .unwrap();
//!
Expand All @@ -44,7 +44,7 @@
//! .configure(timer::config::Config {
//! duty: timer::config::Duty::Duty5Bit,
//! clock_source: timer::HSClockSource::APBClk,
//! frequency: 24u32.kHz(),
//! frequency: 24.kHz(),
//! })
//! .unwrap();
//!
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/mcpwm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! use mcpwm::{operator::PwmPinConfig, timer::PwmWorkingMode, PeripheralClockConfig, MCPWM};
//!
//! // initialize peripheral
//! let clock_cfg = PeripheralClockConfig::with_frequency(&clocks, 40u32.MHz()).unwrap();
//! let clock_cfg = PeripheralClockConfig::with_frequency(&clocks, 40.MHz()).unwrap();
//! let mut mcpwm = MCPWM::new(peripherals.PWM0, clock_cfg);
//!
//! // connect operator0 to timer0
Expand All @@ -44,7 +44,7 @@
//!
//! // start timer with timestamp values in the range of 0..=99 and a frequency of 20 kHz
//! let timer_clock_cfg = clock_cfg
//! .timer_clock_with_frequency(99, PwmWorkingMode::Increase, 20u32.kHz())
//! .timer_clock_with_frequency(99, PwmWorkingMode::Increase, 20.kHz())
//! .unwrap();
//! mcpwm.timer0.start(timer_clock_cfg);
//!
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/parl_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! &mut rx_descriptors,
//! DmaPriority::Priority0,
//! ),
//! 1u32.MHz(),
//! 1.MHz(),
//! &clocks,
//! )
//! .unwrap();
Expand Down Expand Up @@ -60,7 +60,7 @@
//! &mut rx_descriptors,
//! DmaPriority::Priority0,
//! ),
//! 1u32.MHz(),
//! 1.MHz(),
//! &clocks,
//! )
//! .unwrap();
Expand Down
7 changes: 1 addition & 6 deletions esp-hal/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ pub use embedded_dma::{
WriteBuffer as _embedded_dma_WriteBuffer,
WriteTarget as _embedded_dma_WriteTarget,
};
pub use fugit::{
ExtU32 as _fugit_ExtU32,
ExtU64 as _fugit_ExtU64,
RateExtU32 as _fugit_RateExtU32,
RateExtU64 as _fugit_RateExtU64,
};
pub use fugit::{ExtU64 as _fugit_ExtU64, RateExtU32 as _fugit_RateExtU32};
pub use nb;

#[cfg(any(dport, pcr, system))]
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! ### Initialization
//!
//! ```no_run
//! let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &mut clock_control, &clocks).unwrap();
//! let rmt = Rmt::new(peripherals.RMT, 80.MHz(), &mut clock_control, &clocks).unwrap();
//! let mut channel = rmt
//! .channel0
//! .configure(
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/rtc_cntl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl<'d> Rtc<'d> {
}

config.apply();
delay.delay_millis(100u32);
delay.delay_millis(100);

config.start_sleep(wakeup_triggers);
config.finish_sleep();
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/soc/esp32/efuse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ impl Efuse {
let has_low_rating = Self::read_field_le::<bool>(CHIP_CPU_FREQ_LOW);

if has_rating && has_low_rating {
160u32.MHz()
160.MHz()
} else {
240u32.MHz()
240.MHz()
}
}

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//!
//! let mut spi = hal::spi::Spi::new(
//! peripherals.SPI2,
//! 100u32.kHz(),
//! 100.kHz(),
//! SpiMode::Mode0,
//! &mut peripheral_clock_control,
//! &mut clocks,
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pub mod etm {
//! ```no_run
//! let syst = SystemTimer::new(peripherals.SYSTIMER);
//! let mut alarm0 = syst.alarm0.into_periodic();
//! alarm0.set_period(1u32.secs());
//! alarm0.set_period(1.secs());
//!
//! let timer_event = SysTimerEtmEvent::new(&mut alarm0);
//! ```
Expand Down
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ esp-backtrace = { version = "0.11.1", features = ["exception-handler", "pa
esp-hal = { version = "0.16.0", path = "../esp-hal", features = ["log"] }
esp-hal-smartled = { version = "0.9.0", path = "../esp-hal-smartled", optional = true }
esp-println = { version = "0.9.1", features = ["log"] }
fugit = "0.3.7"
heapless = "0.8.0"
hex-literal = "0.4.1"
hmac = { version = "0.12.1", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ fn main() -> ! {
loop {
let pin_value: u16 = nb::block!(adc1.read(&mut adc1_pin)).unwrap();
println!("ADC reading = {}", pin_value);
delay.delay_millis(1500u32);
delay.delay_millis(1500);
}
}
2 changes: 1 addition & 1 deletion examples/src/bin/adc_cal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ fn main() -> ! {
loop {
let pin_mv = nb::block!(adc1.read(&mut adc1_pin)).unwrap();
println!("PIN2 ADC reading = {pin_mv} mV");
delay.delay_millis(1500u32);
delay.delay_millis(1500);
}
}
2 changes: 1 addition & 1 deletion examples/src/bin/advanced_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ fn main() -> ! {
Err(err) => println!("Error {:?}", err),
}

delay.delay_millis(250u32);
delay.delay_millis(250);
}
}
5 changes: 4 additions & 1 deletion examples/src/bin/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ fn main() -> ! {

loop {
led.toggle().unwrap();
delay.delay_millis(500u32);
delay.delay_millis(500);
led.toggle().unwrap();
// or using `fugit` duration
delay.delay(2.secs());
}
}
2 changes: 1 addition & 1 deletion examples/src/bin/blinky_erased_pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() -> ! {

loop {
toggle_pins(&mut pins, &button);
delay.delay_millis(500u32);
delay.delay_millis(500);
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/clock_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> ! {
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let mut rtc = Rtc::new(peripherals.LPWR);
rtc.rwdt.start(2000u64.millis());
rtc.rwdt.start(2000.millis());
rtc.rwdt.listen();

println!(
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ fn main() -> ! {

voltage_dac2 = voltage_dac2.wrapping_sub(1);
dac2.write(voltage_dac2);
delay.delay_millis(50u32);
delay.delay_millis(50);
}
}
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn main(_spawner: Spawner) {
peripherals.I2C0,
io.pins.gpio4,
io.pins.gpio5,
400u32.kHz(),
400.kHz(),
&clocks,
);

Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_i2s_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn main(_spawner: Spawner) {
peripherals.I2S0,
Standard::Philips,
DataFormat::Data16Channel16,
44100u32.Hz(),
44100.Hz(),
dma_channel.configure(
false,
&mut tx_descriptors,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_i2s_sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async fn main(_spawner: Spawner) {
peripherals.I2S0,
Standard::Philips,
DataFormat::Data16Channel16,
44100u32.Hz(),
44100.Hz(),
dma_channel.configure(
false,
&mut tx_descriptors,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_parl_io_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn main(_spawner: Spawner) {
&mut rx_descriptors,
DmaPriority::Priority0,
),
1u32.MHz(),
1.MHz(),
&clocks,
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_parl_io_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn main(_spawner: Spawner) {
&mut rx_descriptors,
DmaPriority::Priority0,
),
1u32.MHz(),
1.MHz(),
&clocks,
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/embassy_rmt_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ async fn main(spawner: Spawner) {

cfg_if::cfg_if! {
if #[cfg(feature = "esp32h2")] {
let freq = 32u32.MHz();
let freq = 32.MHz();
} else {
let freq = 80u32.MHz();
let freq = 80.MHz();
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/embassy_rmt_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ async fn main(_spawner: Spawner) {

cfg_if::cfg_if! {
if #[cfg(feature = "esp32h2")] {
let freq = 32u32.MHz();
let freq = 32.MHz();
} else {
let freq = 80u32.MHz();
let freq = 80.MHz();
}
};

Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async fn main(_spawner: Spawner) {

let (mut descriptors, mut rx_descriptors) = dma_descriptors!(32000);

let mut spi = Spi::new(peripherals.SPI2, 100u32.kHz(), SpiMode::Mode0, &clocks)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(
false,
Expand Down
1 change: 1 addition & 0 deletions examples/src/bin/etm_blinky_systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use esp_hal::{
prelude::*,
systimer::{etm::SysTimerEtmEvent, SystemTimer},
};
use fugit::ExtU32;

#[entry]
fn main() -> ! {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/gpio_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() -> ! {

loop {
led.toggle().unwrap();
delay.delay_millis(500u32);
delay.delay_millis(500);
}
}

Expand Down
Loading
Loading