diff --git a/examples/blinky.rs b/examples/blinky.rs index efefcb4bb..31422f16d 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -18,9 +18,9 @@ fn main() -> anyhow::Result<()> { loop { led.set_high()?; // we are sleeping here to make sure the watchdog isn't triggered - FreeRtos.delay_ms(1000); + FreeRtos::delay_ms(1000); led.set_low()?; - FreeRtos.delay_ms(1000); + FreeRtos::delay_ms(1000); } } diff --git a/examples/i2c_ssd1306.rs b/examples/i2c_ssd1306.rs index a98028fcd..4f066acd7 100644 --- a/examples/i2c_ssd1306.rs +++ b/examples/i2c_ssd1306.rs @@ -70,9 +70,9 @@ fn main() -> anyhow::Result<()> { loop { // we are sleeping here to make sure the watchdog isn't triggered - FreeRtos.delay_ms(500); + FreeRtos::delay_ms(500); i2c.write(SSD1306_ADDRESS, &[0, 0xa6], BLOCK)?; - FreeRtos.delay_ms(500); + FreeRtos::delay_ms(500); i2c.write(SSD1306_ADDRESS, &[0, 0xa7], BLOCK)?; } } diff --git a/examples/ledc-simple.rs b/examples/ledc-simple.rs index c9e63d884..acc552cd1 100644 --- a/examples/ledc-simple.rs +++ b/examples/ledc-simple.rs @@ -25,10 +25,10 @@ fn main() -> anyhow::Result<()> { for numerator in [0, 1, 2, 3, 4, 5].iter().cycle() { println!("Duty {}/5", numerator); channel.set_duty(max_duty * numerator / 5)?; - FreeRtos.delay_ms(2000)?; + FreeRtos::delay_ms(2000)?; } loop { - FreeRtos.delay_ms(1000)?; + FreeRtos::delay_ms(1000)?; } } diff --git a/examples/rmt_morse_code.rs b/examples/rmt_morse_code.rs index 796c42340..4985a1aec 100644 --- a/examples/rmt_morse_code.rs +++ b/examples/rmt_morse_code.rs @@ -47,7 +47,7 @@ fn main() -> anyhow::Result<()> { println!("Keep sending until pin {} is set low.", stop.pin()); while stop.is_high() { - Ets.delay_ms(100)?; + Ets::delay_ms(100)?; } println!("Pin {} set to low. Stopped.", stop.pin()); @@ -56,7 +56,7 @@ fn main() -> anyhow::Result<()> { drop(tx); // Wait so the messages don't get garbled. - Ets.delay_ms(3000)?; + Ets::delay_ms(3000)?; // Now send a single message and stop. println!("Saying GOODBYE!"); diff --git a/examples/rmt_musical_buzzer.rs b/examples/rmt_musical_buzzer.rs index e9822e8dc..404696625 100644 --- a/examples/rmt_musical_buzzer.rs +++ b/examples/rmt_musical_buzzer.rs @@ -26,7 +26,7 @@ fn main() -> anyhow::Result<()> { loop { play_song(&mut tx, ODE_TO_JOY)?; - Ets.delay_ms(3000)?; + Ets::delay_ms(3000)?; } } @@ -58,11 +58,11 @@ pub fn play_note( // Play the note for the 80% of the duration. tx.start(signal)?; - Ets.delay_ms((80 * duration.as_millis() / 100) as u32)?; + Ets::delay_ms((80 * duration.as_millis() / 100) as u32)?; // Small pause between notes, 20% of the specified duration. tx.stop()?; - Ets.delay_ms((20 * duration.as_millis() / 100) as u32)?; + Ets::delay_ms((20 * duration.as_millis() / 100) as u32)?; Ok(()) } diff --git a/src/delay.rs b/src/delay.rs index 0e7b435ae..c149e8d26 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -49,7 +49,9 @@ impl From for Option { } } -/// Espressif Task Scheduler-based delay provider +/// Espressif built-in delay provider +/// Use only for very small delays (us or a few ms at most), or else the FreeRTOS IDLE tasks' might starve and +/// the IDLE tasks' watchdog will trigger pub struct Ets; // No longer available in the generated bindings for ESP-IDF 5 @@ -59,13 +61,13 @@ extern "C" { } impl Ets { - fn delay_us_internal(&mut self, us: u32) { + pub fn delay_us(us: u32) { unsafe { ets_delay_us(us); } } - fn delay_ms_internal(&mut self, ms: u32) { + pub fn delay_ms(ms: u32) { unsafe { ets_delay_us(ms * 1000); } @@ -74,37 +76,37 @@ impl Ets { impl embedded_hal_0_2::blocking::delay::DelayUs for Ets { fn delay_us(&mut self, us: u32) { - self.delay_us_internal(us); + Ets::delay_us(us); } } impl embedded_hal_0_2::blocking::delay::DelayUs for Ets { fn delay_us(&mut self, us: u16) { - self.delay_us_internal(us as _); + Ets::delay_us(us as _); } } impl embedded_hal_0_2::blocking::delay::DelayUs for Ets { fn delay_us(&mut self, us: u8) { - self.delay_us_internal(us as _); + Ets::delay_us(us as _); } } impl embedded_hal_0_2::blocking::delay::DelayMs for Ets { fn delay_ms(&mut self, ms: u32) { - self.delay_ms_internal(ms); + Ets::delay_ms(ms); } } impl embedded_hal_0_2::blocking::delay::DelayMs for Ets { fn delay_ms(&mut self, ms: u16) { - self.delay_ms_internal(ms as _); + Ets::delay_ms(ms as _); } } impl embedded_hal_0_2::blocking::delay::DelayMs for Ets { fn delay_ms(&mut self, ms: u8) { - self.delay_ms_internal(ms as _); + Ets::delay_ms(ms as _); } } @@ -112,29 +114,32 @@ impl embedded_hal::delay::blocking::DelayUs for Ets { type Error = Infallible; fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> { - self.delay_us_internal(us); + Ets::delay_us(us); Ok(()) } fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { - self.delay_ms_internal(ms); + Ets::delay_ms(ms); Ok(()) } } /// FreeRTOS-based delay provider +/// Use for delays larger than 10ms (delays smaller than 10ms used in a loop would +/// starve the FreeRTOS IDLE tasks' as they are low prio tasks and hence the +/// the IDLE tasks' watchdog will trigger) pub struct FreeRtos; impl FreeRtos { - fn delay_us_internal(&mut self, us: u32) { + fn delay_us(us: u32) { let ms = us / 1000; - Self::delay_ms_internal(self, ms); + Self::delay_ms(ms); } - fn delay_ms_internal(&mut self, ms: u32) { + fn delay_ms(ms: u32) { // divide by tick length, rounding up let ticks = ms.saturating_add(portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS; @@ -146,37 +151,37 @@ impl FreeRtos { impl embedded_hal_0_2::blocking::delay::DelayUs for FreeRtos { fn delay_us(&mut self, us: u32) { - self.delay_us_internal(us); + FreeRtos::delay_us(us); } } impl embedded_hal_0_2::blocking::delay::DelayUs for FreeRtos { fn delay_us(&mut self, us: u16) { - self.delay_us_internal(us as _); + FreeRtos::delay_us(us as _); } } impl embedded_hal_0_2::blocking::delay::DelayUs for FreeRtos { fn delay_us(&mut self, us: u8) { - self.delay_us_internal(us as _); + FreeRtos::delay_us(us as _); } } impl embedded_hal_0_2::blocking::delay::DelayMs for FreeRtos { fn delay_ms(&mut self, ms: u32) { - self.delay_ms_internal(ms); + FreeRtos::delay_ms(ms); } } impl embedded_hal_0_2::blocking::delay::DelayMs for FreeRtos { fn delay_ms(&mut self, ms: u16) { - self.delay_ms_internal(ms as _); + FreeRtos::delay_ms(ms as _); } } impl embedded_hal_0_2::blocking::delay::DelayMs for FreeRtos { fn delay_ms(&mut self, ms: u8) { - self.delay_ms_internal(ms as _); + FreeRtos::delay_ms(ms as _); } } @@ -184,13 +189,13 @@ impl embedded_hal::delay::blocking::DelayUs for FreeRtos { type Error = Infallible; fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> { - self.delay_us_internal(us); + FreeRtos::delay_us(us); Ok(()) } fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { - self.delay_ms_internal(ms); + FreeRtos::delay_ms(ms); Ok(()) }