Skip to content

Commit

Permalink
Fix wrong max_duty (#431)
Browse files Browse the repository at this point in the history
the max duty cycle is 2^N, only when using the maximum timer resolution is must be 2^N-1 to prevent overflow
  • Loading branch information
lkies authored Jun 11, 2024
1 parent e4e6089 commit d290357
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,12 @@ mod chip {
}

pub const fn max_duty(&self) -> u32 {
(1 << self.bits()) - 1
// when using the maximum resultion, the duty cycle must not exceed 2^N - 1 to avoid timer overflow
if cfg!(esp32) && self.bits() == 20 || cfg!(not(esp32)) && self.bits() == 14 {
(1 << self.bits()) - 1
} else {
1 << self.bits()
}
}

pub(crate) const fn timer_bits(&self) -> ledc_timer_bit_t {
Expand Down

0 comments on commit d290357

Please sign in to comment.