Skip to content

Commit cdd04b1

Browse files
authored
feat(ledc): testing values that shall be consistent
1 parent 591a161 commit cdd04b1

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

cores/esp32/esp32-hal-ledc.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,21 @@ static bool ledcFadeConfig(uint8_t pin, uint32_t start_duty, uint32_t target_dut
377377
#endif
378378
uint8_t group = (bus->channel / 8), channel = (bus->channel % 8);
379379

380+
uint32_t max_duty = (1 << bus->channel_resolution); // Max LEDC duty
381+
382+
if (target_duty > max_duty) {
383+
log_w("Final duty %d was adjusted to the maximum duty %d", target_duty, max_duty);
384+
target_duty = max_duty;
385+
}
386+
if (start_duty > max_duty) {
387+
log_w("Starting duty %d was adjusted to the maximum duty %d", start_duty, max_duty);
388+
start_duty = max_duty;
389+
}
390+
if (start_duty >= target_duty) {
391+
log_e("Starting duty must be lower than the final duty");
392+
return false;
393+
}
394+
380395
// Initialize fade service.
381396
if (!fade_initialized) {
382397
ledc_fade_func_install(0);
@@ -389,17 +404,6 @@ static bool ledcFadeConfig(uint8_t pin, uint32_t start_duty, uint32_t target_dut
389404
ledc_cbs_t callbacks = {.fade_cb = ledcFnWrapper};
390405
ledc_cb_register(group, channel, &callbacks, (void *)bus);
391406

392-
uint32_t max_duty = (1 << bus->channel_resolution); // Max LEDC duty
393-
394-
if (target_duty > max_duty) {
395-
log_w("Target duty %d was adjusted to the maximum duty %d", target_duty, max_duty);
396-
target_duty = max_duty;
397-
}
398-
if (start_duty > max_duty) {
399-
log_w("Starting duty %d was adjusted to the maximum duty %d", start_duty, max_duty);
400-
start_duty = max_duty;
401-
}
402-
403407
#if SOC_LEDC_SUPPORT_FADE_STOP
404408
ledc_fade_stop(group, channel);
405409
#endif
@@ -454,7 +458,7 @@ void analogWrite(uint8_t pin, int value) {
454458
log_w("Duty is out of range. Valid duty range for pin d is 0 to %d", pin, max_duty);
455459
return;
456460
}
457-
if ((value == max_duty) && (max_duty != 1)) {
461+
if (value == max_duty) {
458462
value = max_duty + 1;
459463
}
460464
ledcWrite(pin, value);

0 commit comments

Comments
 (0)