Skip to content

Commit

Permalink
Fix for issue #1419 (#1441)
Browse files Browse the repository at this point in the history
* Fix for issue #1419. Removed ESP32 specific code for resolutions > 16 bit in ledc embedded_hal::pwm max_duty_cycle function. Fixed division by zero in ledc embedded_hal::pwm set_duty_cycle function and converted to set_duty_hw instead of set_duty to eliminate loss of granularity.

* Updated change log.

* Fixed indentation in ledc set_duty_cycle function.

* Removed unused ChannelIFace import for ehal mod.
  • Loading branch information
Droog71 authored Apr 22, 2024
1 parent 20c7789 commit 7e5895c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed writes to SPI not flushing before attempting to write, causing corrupted writes (#1381)
- fix AdcConfig::adc_calibrate for xtensa targets (#1379)
- Fixed a divide by zero panic when setting the LEDC duty cycle to 0 with `SetDutyCycle::set_duty_cycle` (#1403)
- Fix for issue #1419. Removed ESP32 specific code for resolutions > 16 bit in ledc embedded_hal::pwm max_duty_cycle function.
- Fix for issue #1419. Fixed division by zero in ledc embedded_hal::pwm set_duty_cycle function and converted to set_duty_hw instead of set_duty to eliminate loss of granularity.
- Support 192 and 256-bit keys for AES (#1316)
- Fixed MCPWM DeadTimeCfg bit values (#1378)
- ESP32 LEDC `set_duty_cycle` used HighSpeedChannel for LowSpeedChannel (#1457)
Expand Down
24 changes: 6 additions & 18 deletions esp-hal/src/ledc/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ where
mod ehal1 {
use embedded_hal::pwm::{self, ErrorKind, ErrorType, SetDutyCycle};

use super::{Channel, ChannelHW, ChannelIFace, Error};
use super::{Channel, ChannelHW, Error};
use crate::{gpio::OutputPin, ledc::timer::TimerSpeed};

impl pwm::Error for Error {
Expand Down Expand Up @@ -310,25 +310,13 @@ mod ehal1 {

let duty_range = 2u32.pow(duty_exp);

#[cfg(esp32)]
{
let duty_range_pct = 100 / (u32::MAX / duty_range);
(u16::MAX / 100) * duty_range_pct as u16
}
#[cfg(not(esp32))]
{
// Supports up to 14 bits
duty_range as u16
}
duty_range as u16
}

fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
if duty == 0 {
self.set_duty(0)?;
} else {
let duty_pct = 100 / (self.max_duty_cycle() / duty);
self.set_duty(duty_pct as u8)?;
}
fn set_duty_cycle(&mut self, mut duty: u16) -> Result<(), Self::Error> {
let max = self.max_duty_cycle();
duty = if duty > max { max } else { duty };
self.set_duty_hw(duty.into());
Ok(())
}
}
Expand Down

0 comments on commit 7e5895c

Please sign in to comment.