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

Replace all float calculations with fixed ones #173

Merged
merged 1 commit into from
Aug 30, 2022
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
10 changes: 5 additions & 5 deletions esp-hal-common/src/ledc/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub mod config {
#[derive(Copy, Clone)]
pub struct Config<'a, S: TimerSpeed> {
pub timer: &'a dyn TimerIFace<S>,
pub duty_pct: f32,
pub duty_pct: u8,
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions esp-hal-common/src/ledc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! channel0
//! .configure(channel::config::Config {
//! timer: &lstimer0,
//! duty: 0.1,
//! duty: 10,
//! })
//! .unwrap();
//! ```
Expand All @@ -53,7 +53,7 @@
//! channel0
//! .configure(channel::config::Config {
//! timer: &hstimer0,
//! duty: 0.1,
//! duty: 10,
//! })
//! .unwrap();
//! ```
Expand Down
6 changes: 3 additions & 3 deletions esp-hal-common/src/rtc_cntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions esp-hal-common/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> CountDown for Timer<T>
Expand Down
4 changes: 2 additions & 2 deletions esp32-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -62,7 +62,7 @@ fn main() -> ! {
channel0
.configure(channel::config::Config {
timer: &hstimer0,
duty_pct: 0.1,
duty_pct: 10,
})
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions esp32c3-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -67,7 +67,7 @@ fn main() -> ! {
channel0
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 0.1,
duty_pct: 10,
})
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions esp32s2-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -65,7 +65,7 @@ fn main() -> ! {
channel0
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 0.1,
duty_pct: 10,
})
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions esp32s3-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -65,7 +65,7 @@ fn main() -> ! {
channel0
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 0.1,
duty_pct: 10,
})
.unwrap();

Expand Down