Skip to content

Commit d290357

Browse files
authored
Fix wrong max_duty (#431)
the max duty cycle is 2^N, only when using the maximum timer resolution is must be 2^N-1 to prevent overflow
1 parent e4e6089 commit d290357

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/ledc.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,12 @@ mod chip {
470470
}
471471

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

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

0 commit comments

Comments
 (0)