Skip to content

Commit 82b13e5

Browse files
committed
fix(ledc): Allow setting AnalogWrite freq and res before ledc init
1 parent 956b687 commit 82b13e5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

cores/esp32/esp32-hal-ledc.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -784,17 +784,23 @@ void analogWrite(uint8_t pin, int value) {
784784
}
785785

786786
void analogWriteFrequency(uint8_t pin, uint32_t freq) {
787-
if (ledcChangeFrequency(pin, freq, analog_resolution) == 0) {
788-
log_e("analogWrite frequency cant be set due to selected resolution! Try to adjust resolution first");
789-
return;
787+
ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC);
788+
if (bus != NULL) { // if pin is attached to LEDC change frequency. otherwise update the global frequency
789+
if (ledcChangeFrequency(pin, freq, analog_resolution) == 0) {
790+
log_e("analogWrite frequency cant be set due to selected resolution! Try to adjust resolution first");
791+
return;
792+
}
790793
}
791794
analog_frequency = freq;
792795
}
793796

794797
void analogWriteResolution(uint8_t pin, uint8_t resolution) {
795-
if (ledcChangeFrequency(pin, analog_frequency, resolution) == 0) {
796-
log_e("analogWrite resolution cant be set due to selected frequency! Try to adjust frequency first");
797-
return;
798+
ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC);
799+
if (bus != NULL) { // if pin is attached to LEDC change resolution. otherwise update the global resolution
800+
if (ledcChangeFrequency(pin, analog_frequency, resolution) == 0) {
801+
log_e("analogWrite resolution cant be set due to selected frequency! Try to adjust frequency first");
802+
return;
803+
}
798804
}
799805
analog_resolution = resolution;
800806
}

0 commit comments

Comments
 (0)