From 9f3a11a0d68ac6578aa7f6ea55f87d76e73c50e9 Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko Date: Wed, 31 Jul 2024 01:05:13 +0300 Subject: [PATCH] fix(esp32-hal-ledc): Clear bit in used_channels bitmap When we attach pin to LEDC channel we set bit in used_channels bitmap variable. But used_channels bit was not cleared on detach, as result simply repeating attach/detach around 16 times will falsely use all available channels and emit error that channel is not available. This happen for example in IRSenderESP32.cpp. This patch fixes this bug. Signed-off-by: Denys Fedoryshchenko --- cores/esp32/esp32-hal-ledc.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index eb2e51382c9..f71e787ff84 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -271,6 +271,13 @@ uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave) { bool ledcDetach(uint8_t pin) { ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC); if (bus != NULL) { + /* ledc_handle.used_channels use bitmap to indicate if channel is used + bit is set if channel is used, in ledcAttachChannel/ledcAttach + bit is cleared if channel is detached here + */ + if (ledc_handle.used_channels & (1UL << bus->channel)) { + ledc_handle.used_channels &= ~(1UL << bus->channel); + } // will call ledcDetachBus return perimanClearPinBus(pin); } else {