Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b68bfd8

Browse files
committedJul 15, 2024·
feat(ledc): Allow attaching multiple pins to 1 channel
1 parent 4b7b5d3 commit b68bfd8

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed
 

‎cores/esp32/esp32-hal-ledc.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,26 @@ static bool fade_initialized = false;
4848

4949
static bool ledcDetachBus(void *bus) {
5050
ledc_channel_handle_t *handle = (ledc_channel_handle_t *)bus;
51-
ledc_handle.used_channels &= ~(1UL << handle->channel);
51+
bool channel_found = false;
52+
// Check if more pins are attached to the same ledc channel
53+
for (uint8_t i = 0; i < SOC_GPIO_PIN_COUNT; i++) {
54+
if (!perimanPinIsValid(i)) {
55+
continue; //invalid pin, skip
56+
}
57+
peripheral_bus_type_t type = perimanGetPinBusType(i);
58+
if (type == ESP32_BUS_TYPE_LEDC) {
59+
ledc_channel_handle_t *bus_check = (ledc_channel_handle_t *)perimanGetPinBus(i, ESP32_BUS_TYPE_LEDC);
60+
if (bus_check->channel == handle->channel) {
61+
channel_found = true;
62+
break;
63+
}
64+
}
65+
}
5266
pinMatrixOutDetach(handle->pin, false, false);
5367
free(handle);
68+
if (!channel_found) {
69+
ledc_handle.used_channels &= ~(1UL << handle->channel);
70+
}
5471
if (ledc_handle.used_channels == 0) {
5572
ledc_fade_func_uninstall();
5673
fade_initialized = false;
@@ -59,8 +76,8 @@ static bool ledcDetachBus(void *bus) {
5976
}
6077

6178
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel) {
62-
if (channel >= LEDC_CHANNELS || ledc_handle.used_channels & (1UL << channel)) {
63-
log_e("Channel %u is not available (maximum %u) or already used!", channel, LEDC_CHANNELS);
79+
if (channel >= LEDC_CHANNELS) { //|| ledc_handle.used_channels & (1UL << channel)) {
80+
log_e("Channel %u is not available (maximum %u) TODO: delete (or already used)!", channel, LEDC_CHANNELS);
6481
return false;
6582
}
6683
if (freq == 0) {

0 commit comments

Comments
 (0)
Please sign in to comment.