Skip to content

Commit 940cf59

Browse files
authored
feat(ledc): check duty limit based on the resolution
1 parent be00779 commit 940cf59

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

cores/esp32/esp32-hal-ledc.c

+11
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ bool ledcWrite(uint8_t pin, uint32_t duty) {
183183
if (bus != NULL) {
184184

185185
uint8_t group = (bus->channel / 8), channel = (bus->channel % 8);
186+
uint32_t max_duty = (1 << bus->channel_resolution); // Max LEDC duty
186187

188+
if (duty > max_duty) {
189+
log_w("Target duty %d was adjusted to the maximum duty %d", duty, max_duty);
190+
duty = max_duty;
191+
}
192+
187193
ledc_set_duty(group, channel, duty);
188194
ledc_update_duty(group, channel);
189195

@@ -203,7 +209,12 @@ bool ledcWriteChannel(uint8_t channel, uint32_t duty) {
203209
//Fixing if all bits in resolution is set = LEDC FULL ON
204210
uint32_t resolution = 0;
205211
ledc_ll_get_duty_resolution(LEDC_LL_GET_HW(), group, timer, &resolution);
212+
uint32_t max_duty = (1 << resolution); // Max LEDC duty
206213

214+
if (duty > max_duty) {
215+
log_w("Target duty %d was adjusted to the maximum duty %d", duty, max_duty);
216+
duty = max_duty;
217+
}
207218
ledc_set_duty(group, channel, duty);
208219
ledc_update_duty(group, channel);
209220

0 commit comments

Comments
 (0)