Skip to content

LEDC - AnalogWrite new API + ledcAttachPin duty fix #7346

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

Merged
merged 8 commits into from
Nov 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix ledc channels number
  • Loading branch information
P-R-O-C-H-Y committed Oct 12, 2022
commit 397e745bf799b1a9936890142afb8b95e418c6e4
4 changes: 2 additions & 2 deletions cores/esp32/esp32-hal-ledc.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void analogWriteFrequency(uint32_t freq) {
if (cnt_channel == LEDC_CHANNELS) {
return; //No channel used for analogWrite
}
for (int channel = 15; channel >= cnt_channel; channel--) {
for (int channel = LEDC_CHANNELS - 1; channel >= cnt_channel; channel--) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is any uint32_t a valid frequency? What happens if we set it to 1,000,000,000 Hz?
Should it be validated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDF is checking the frequency input and validating if its valid to set or not. Same with resolution. IDF will return error that is checked in Arduino.

Copy link
Collaborator

@SuGlider SuGlider Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it with wrong valus and I got no error message.

But if I test it with IDF I get the error...
E (30) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=11

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the comments below :)

ledcChangeFrequency(channel, freq, analog_resolution);
}
analog_frequency = freq;
Expand All @@ -248,7 +248,7 @@ void analogWriteResolution(uint8_t bits) {
if (cnt_channel == LEDC_CHANNELS) {
return; //No channel used for analogWrite
}
for (int channel = 15; channel >= cnt_channel; channel--) {
for (int channel = LEDC_CHANNELS - 1; channel >= cnt_channel; channel--) {
ledcChangeFrequency(channel, analog_frequency, bits);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is any uint8_t a valid resolution? What happens if we set it to 128 bits?
Should it be validated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will get an error that is not valid input.

Copy link
Collaborator

@SuGlider SuGlider Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it with wrong value and I got no error message.
Looking into https://github.com/espressif/esp-idf/blob/release/v4.4/components/driver/ledc.c#L558
I see duty validation, but it doesn't fail...

But if I test it with pure IDF I get the error...
E (30) ledc: freq_hz=1000 duty_resolution=100

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the comments below :)

}
analog_resolution = bits;
Expand Down