@@ -377,6 +377,21 @@ static bool ledcFadeConfig(uint8_t pin, uint32_t start_duty, uint32_t target_dut
377
377
#endif
378
378
uint8_t group = (bus -> channel / 8 ), channel = (bus -> channel % 8 );
379
379
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
+
380
395
// Initialize fade service.
381
396
if (!fade_initialized ) {
382
397
ledc_fade_func_install (0 );
@@ -389,17 +404,6 @@ static bool ledcFadeConfig(uint8_t pin, uint32_t start_duty, uint32_t target_dut
389
404
ledc_cbs_t callbacks = {.fade_cb = ledcFnWrapper };
390
405
ledc_cb_register (group , channel , & callbacks , (void * )bus );
391
406
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
-
403
407
#if SOC_LEDC_SUPPORT_FADE_STOP
404
408
ledc_fade_stop (group , channel );
405
409
#endif
@@ -454,7 +458,7 @@ void analogWrite(uint8_t pin, int value) {
454
458
log_w ("Duty is out of range. Valid duty range for pin d is 0 to %d" , pin , max_duty );
455
459
return ;
456
460
}
457
- if (( value == max_duty ) && ( max_duty != 1 ) ) {
461
+ if (value == max_duty ) {
458
462
value = max_duty + 1 ;
459
463
}
460
464
ledcWrite (pin , value );
0 commit comments