From 9911a1e3591008f95cb3dc56b6aa1ff192721a84 Mon Sep 17 00:00:00 2001 From: Juraj Sadel Date: Tue, 30 Aug 2022 13:28:56 +0200 Subject: [PATCH] Replace all float calculations with fixed ones --- esp-hal-common/src/ledc/channel.rs | 10 +++++----- esp-hal-common/src/ledc/mod.rs | 4 ++-- esp-hal-common/src/rtc_cntl.rs | 6 +++--- esp-hal-common/src/timer.rs | 7 +++---- esp32-hal/examples/ledc.rs | 4 ++-- esp32c3-hal/examples/ledc.rs | 4 ++-- esp32s2-hal/examples/ledc.rs | 4 ++-- esp32s3-hal/examples/ledc.rs | 4 ++-- 8 files changed, 21 insertions(+), 22 deletions(-) diff --git a/esp-hal-common/src/ledc/channel.rs b/esp-hal-common/src/ledc/channel.rs index 646c9c1c28c..4b46490b6f0 100644 --- a/esp-hal-common/src/ledc/channel.rs +++ b/esp-hal-common/src/ledc/channel.rs @@ -45,7 +45,7 @@ pub mod config { #[derive(Copy, Clone)] pub struct Config<'a, S: TimerSpeed> { pub timer: &'a dyn TimerIFace, - pub duty_pct: f32, + pub duty_pct: u8, } } @@ -58,7 +58,7 @@ where fn configure(&mut self, config: config::Config<'a, S>) -> Result<(), Error>; /// Set channel duty HW - fn set_duty(&self, duty_pct: f32) -> Result<(), Error>; + fn set_duty(&self, duty_pct: u8) -> Result<(), Error>; } /// Channel HW interface @@ -107,7 +107,7 @@ where } /// Set duty % of channel - fn set_duty(&self, duty_pct: f32) -> Result<(), Error> { + fn set_duty(&self, duty_pct: u8) -> Result<(), Error> { let duty_exp; if let Some(timer) = self.timer { if let Some(timer_duty) = timer.get_duty() { @@ -120,9 +120,9 @@ where } let duty_range = 2u32.pow(duty_exp); - let duty_value = (duty_range as f32 * duty_pct) as u32; + let duty_value = (duty_range * duty_pct as u32) as u32 / 100; - if duty_value == 0 || duty_pct > 1.0 { + if duty_value == 0 || duty_pct > 100u8 { // Not enough bits to represent the requested duty % or duty_pct greater than // 1.0 return Err(Error::Duty); diff --git a/esp-hal-common/src/ledc/mod.rs b/esp-hal-common/src/ledc/mod.rs index 70f94377278..04efb51df39 100644 --- a/esp-hal-common/src/ledc/mod.rs +++ b/esp-hal-common/src/ledc/mod.rs @@ -27,7 +27,7 @@ //! channel0 //! .configure(channel::config::Config { //! timer: &lstimer0, -//! duty: 0.1, +//! duty: 10, //! }) //! .unwrap(); //! ``` @@ -53,7 +53,7 @@ //! channel0 //! .configure(channel::config::Config { //! timer: &hstimer0, -//! duty: 0.1, +//! duty: 10, //! }) //! .unwrap(); //! ``` diff --git a/esp-hal-common/src/rtc_cntl.rs b/esp-hal-common/src/rtc_cntl.rs index 4ff36d06e51..da599624fb5 100644 --- a/esp-hal-common/src/rtc_cntl.rs +++ b/esp-hal-common/src/rtc_cntl.rs @@ -418,10 +418,10 @@ impl RtcClock { 1024, ); - let q_to_float = |val| (val as f32) / ((1 << RtcClock::CAL_FRACT) as f32); - let period = q_to_float(period_13q19); + // 100_000_000 is used to get rid of `float` calculations + let period = (100_000_000 * period_13q19 as u64) / (1 << RtcClock::CAL_FRACT); - (1000f32 / period) as u16 + (100_000_000 * 1000 / period) as u16 } fn estimate_xtal_frequency() -> u32 { diff --git a/esp-hal-common/src/timer.rs b/esp-hal-common/src/timer.rs index 27c15e018a6..ea356997d04 100644 --- a/esp-hal-common/src/timer.rs +++ b/esp-hal-common/src/timer.rs @@ -445,10 +445,9 @@ where let clock: HertzU32 = clock.into(); - // TODO can we get this to not use doubles/floats - let period = 1_000_000f64 / (clock.to_Hz() as f64 / divider as f64); // micros - - (micros as f64 / period) as u64 + // 1_000_000 is used to get rid of `float` calculations + let period: u64 = 1_000_000 * 1_000_000 / (clock.to_Hz() as u64 / divider as u64); + (1_000_000 * micros / period as u64) as u64 } impl CountDown for Timer diff --git a/esp32-hal/examples/ledc.rs b/esp32-hal/examples/ledc.rs index 38e35736cea..1ffeaa29a12 100644 --- a/esp32-hal/examples/ledc.rs +++ b/esp32-hal/examples/ledc.rs @@ -1,5 +1,5 @@ //! Turns on LED with the option to change LED intensity depending on `duty` -//! value. +//! value. Possible values (`u32`) are in range 0..100. //! //! This assumes that a LED is connected to the pin assigned to `led`. (GPIO4) @@ -62,7 +62,7 @@ fn main() -> ! { channel0 .configure(channel::config::Config { timer: &hstimer0, - duty_pct: 0.1, + duty_pct: 10, }) .unwrap(); diff --git a/esp32c3-hal/examples/ledc.rs b/esp32c3-hal/examples/ledc.rs index 53f118d0667..62d2576e11d 100644 --- a/esp32c3-hal/examples/ledc.rs +++ b/esp32c3-hal/examples/ledc.rs @@ -1,5 +1,5 @@ //! Turns on LED with the option to change LED intensity depending on `duty` -//! value. +//! value. Possible values (`u32`) are in range 0..100. //! //! This assumes that a LED is connected to the pin assigned to `led`. (GPIO4) @@ -67,7 +67,7 @@ fn main() -> ! { channel0 .configure(channel::config::Config { timer: &lstimer0, - duty_pct: 0.1, + duty_pct: 10, }) .unwrap(); diff --git a/esp32s2-hal/examples/ledc.rs b/esp32s2-hal/examples/ledc.rs index 4d1cff871fc..7fbf579d8f2 100644 --- a/esp32s2-hal/examples/ledc.rs +++ b/esp32s2-hal/examples/ledc.rs @@ -1,5 +1,5 @@ //! Turns on LED with the option to change LED intensity depending on `duty` -//! value. +//! value. Possible values (`u32`) are in range 0..100. //! //! This assumes that a LED is connected to the pin assigned to `led`. (GPIO4) @@ -65,7 +65,7 @@ fn main() -> ! { channel0 .configure(channel::config::Config { timer: &lstimer0, - duty_pct: 0.1, + duty_pct: 10, }) .unwrap(); diff --git a/esp32s3-hal/examples/ledc.rs b/esp32s3-hal/examples/ledc.rs index b0a02e48d7a..52d09c317d2 100644 --- a/esp32s3-hal/examples/ledc.rs +++ b/esp32s3-hal/examples/ledc.rs @@ -1,5 +1,5 @@ //! Turns on LED with the option to change LED intensity depending on `duty` -//! value. +//! value. Possible values (`u32`) are in range 0..100. //! //! This assumes that a LED is connected to the pin assigned to `led`. (GPIO4) @@ -65,7 +65,7 @@ fn main() -> ! { channel0 .configure(channel::config::Config { timer: &lstimer0, - duty_pct: 0.1, + duty_pct: 10, }) .unwrap();