Skip to content

[BUG] esp32 ledc skips first channel index #471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ervinoro opened this issue May 18, 2025 · 1 comment
Open

[BUG] esp32 ledc skips first channel index #471

ervinoro opened this issue May 18, 2025 · 1 comment
Assignees

Comments

@ervinoro
Copy link

ervinoro commented May 18, 2025

Describe the bug

Symptom:
It is not possible to initialize two 3PWM drivers on ESP32-C3.
The .init() function of the second driver will print the following error during setup of the 6th pin:
E (1116) ledc: ledc_get_duty(824): channel argument is invalid
E (1116) ledc: ledc_channel_config(663): ledc_channel argument is invalid

Diagnosis:
The _configure3PWM has group_channels_used[group]++; before calling _ledcAttachChannelAdvanced.
Therefore the first pin will use channel 1, and 6th pin will try to use channel 6. However, the channels are 0-indexed, therefore channel 0 will be unused and channel 6 will throw error of invalid argument.

for(int i = 0; i < 3; i++){
group_channels_used[group]++;
if(!_ledcAttachChannelAdvanced(pins[i], group_channels_used[group], group, pwm_frequency, _PWM_RES_BIT, false)){
SIMPLEFOC_DEBUG("EP32-DRV: ERROR - Failed to configure pin:", pins[i]);
return SIMPLEFOC_DRIVER_INIT_FAILED;
}
params->channels[i] = static_cast<ledc_channel_t>(group_channels_used[group]);
params->groups[i] = (ledc_mode_t)group;
}

Solution:
By simply moving the group_channels_used[group]++; statement to the end of that for block, I got two 3PWM drivers initiated.

Describe the hardware setup
ESP32-C3 supermini
Has 6 LEDC channels

IDE you are using
Arduino IDE
esp32 v3.1.3
Simple FOC v2.3.4

@ervinoro ervinoro changed the title [BUG] esp32 ledc channel index skips first [BUG] esp32 ledc skips first channel index May 18, 2025
@runger1101001
Copy link
Member

Thanks for finding and reporting this!

@runger1101001 runger1101001 self-assigned this May 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants