Skip to content

Commit be00779

Browse files
authored
feat(ledc): max resolution review
1 parent b333bf2 commit be00779

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

cores/esp32/esp32-hal-ledc.c

+24-23
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,6 @@ bool ledcWrite(uint8_t pin, uint32_t duty) {
184184

185185
uint8_t group = (bus->channel / 8), channel = (bus->channel % 8);
186186

187-
//Fixing if all bits in resolution is set = LEDC FULL ON
188-
uint32_t max_duty = (1 << bus->channel_resolution) - 1;
189-
190-
if ((duty == max_duty) && (max_duty != 1)) {
191-
duty = max_duty + 1;
192-
}
193-
194187
ledc_set_duty(group, channel, duty);
195188
ledc_update_duty(group, channel);
196189

@@ -211,12 +204,6 @@ bool ledcWriteChannel(uint8_t channel, uint32_t duty) {
211204
uint32_t resolution = 0;
212205
ledc_ll_get_duty_resolution(LEDC_LL_GET_HW(), group, timer, &resolution);
213206

214-
uint32_t max_duty = (1 << resolution) - 1;
215-
216-
if ((duty == max_duty) && (max_duty != 1)) {
217-
duty = max_duty + 1;
218-
}
219-
220207
ledc_set_duty(group, channel, duty);
221208
ledc_update_duty(group, channel);
222209

@@ -265,15 +252,16 @@ uint32_t ledcWriteTone(uint8_t pin, uint32_t freq) {
265252
bus->channel_resolution = 10;
266253

267254
uint32_t res_freq = ledc_get_freq(group, timer);
268-
ledcWrite(pin, 0x1FF);
255+
ledcWrite(pin, 0x200); // LEDC 50% duty is 2^10 / 2 = 0x200
269256
return res_freq;
270257
}
271258
return 0;
272259
}
273260

274261
uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave) {
275-
const uint16_t noteFrequencyBase[12] = {// C C# D Eb E F F# G G# A Bb B
276-
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902
262+
const uint16_t noteFrequencyBase[12] = {
263+
// C C# D Eb E F F# G G# A Bb B
264+
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902
277265
};
278266

279267
if (octave > 8 || note >= NOTE_MAX) {
@@ -391,13 +379,15 @@ static bool ledcFadeConfig(uint8_t pin, uint32_t start_duty, uint32_t target_dut
391379
ledc_cbs_t callbacks = {.fade_cb = ledcFnWrapper};
392380
ledc_cb_register(group, channel, &callbacks, (void *)bus);
393381

394-
//Fixing if all bits in resolution is set = LEDC FULL ON
395-
uint32_t max_duty = (1 << bus->channel_resolution) - 1;
382+
uint32_t max_duty = (1 << bus->channel_resolution); // Max LEDC duty
396383

397-
if ((target_duty == max_duty) && (max_duty != 1)) {
398-
target_duty = max_duty + 1;
399-
} else if ((start_duty == max_duty) && (max_duty != 1)) {
400-
start_duty = max_duty + 1;
384+
if (target_duty > max_duty) {
385+
log_w("Target duty %d was adjusted to the maximum duty %d", target_duty, max_duty);
386+
target_duty = max_duty;
387+
}
388+
if (start_duty > max_duty) {
389+
log_w("Starting duty %d was adjusted to the maximum duty %d", start_duty, max_duty);
390+
start_duty = max_duty;
401391
}
402392

403393
#if SOC_LEDC_SUPPORT_FADE_STOP
@@ -411,7 +401,7 @@ static bool ledcFadeConfig(uint8_t pin, uint32_t start_duty, uint32_t target_dut
411401
// Wait for LEDCs next PWM cycle to update duty (~ 1-2 ms)
412402
while (ledc_get_duty(group, channel) != start_duty);
413403

414-
if (ledc_set_fade_time_and_start(group, channel, target_duty, max_fade_time_ms, LEDC_FADE_NO_WAIT) != ESP_OK) {
404+
if (ledc_set_fade_time_and_start(group, channel, target_duty, _fade_time_ms, LEDC_FADE_NO_WAIT) != ESP_OK) {
415405
log_e("ledc_set_fade_time_and_start failed");
416406
return false;
417407
}
@@ -446,6 +436,17 @@ void analogWrite(uint8_t pin, int value) {
446436
return;
447437
}
448438
}
439+
// Arduino API says that duty goes from 0 to (2^resolution) - 1
440+
// But LEDC works with duty from 0 to (2^resolution)
441+
// Therefore, it will adjust Arduino MAX Duty to be the LEDC MAx Duty
442+
uint32_t max_duty = (1 << bus->channel_resolution) - 1;
443+
if (value < 0 || value > max_duty) {
444+
log_w("Duty is out of range. Valid duty range for pin d is 0 to %d", pin, max_duty);
445+
return;
446+
}
447+
if ((value == max_duty) && (max_duty != 1)) {
448+
value = max_duty + 1;
449+
}
449450
ledcWrite(pin, value);
450451
}
451452
}

0 commit comments

Comments
 (0)