From d290357bba3d7da10099effccebeb803984b06fc Mon Sep 17 00:00:00 2001 From: Lorenz <43540048+lkies@users.noreply.github.com> Date: Tue, 11 Jun 2024 18:43:55 +0200 Subject: [PATCH] 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 --- src/ledc.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ledc.rs b/src/ledc.rs index 469f8a22c05..1e6c9033558 100644 --- a/src/ledc.rs +++ b/src/ledc.rs @@ -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 {