Skip to content

Commit 2173373

Browse files
authored
ledc.c: Fix analogWrite() last channel available verification (espressif#8509)
* Fix analogWrite channel available verification The last channel allocated is number 0, which conflicted with the value given to an uninitialized pin, giving the "No more analogWrite channels available!" error when trying to use it Pins are now given the value -1 to indicate that they are not used so channel 0 can be used without errors. * Fix incorrect array initialization Keeping array of zeros for `pin_to_channel` and shifting stored channel values by +1 to keep the pin with channel 0 from being interpreted as unused. ref: espressif#8509 (comment)
1 parent e2c4799 commit 2173373

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

cores/esp32/esp32-hal-ledc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ void analogWrite(uint8_t pin, int value) {
234234
return;
235235
}
236236
ledcAttachPin(pin, channel);
237-
pin_to_channel[pin] = channel;
237+
pin_to_channel[pin] = channel + 1;
238238
ledcWrite(channel, value);
239239
}
240240
}
241241

242242
int8_t analogGetChannel(uint8_t pin) {
243-
return pin_to_channel[pin];
243+
return pin_to_channel[pin] - 1;
244244
}
245245

246246
void analogWriteFrequency(uint32_t freq) {

0 commit comments

Comments
 (0)