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
Show file tree
Hide file tree
Changes from 4 commits
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
29 changes: 26 additions & 3 deletions cores/esp32/esp32-hal-ledc.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ void ledcAttachPin(uint8_t pin, uint8_t chan)
return;
}
uint8_t group=(chan/8), channel=(chan%8), timer=((chan/2)%4);

uint32_t duty = ledc_get_duty(group,channel);

ledc_channel_config_t ledc_channel = {
.speed_mode = group,
.channel = channel,
.timer_sel = timer,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = pin,
.duty = 0,
.duty = duty,
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
Expand Down Expand Up @@ -211,6 +212,8 @@ uint32_t ledcChangeFrequency(uint8_t chan, uint32_t freq, uint8_t bit_num)

static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 };
static int cnt_channel = LEDC_CHANNELS;
static uint8_t analog_resolution = 8;
static int analog_frequency = 1000;
void analogWrite(uint8_t pin, int value) {
// Use ledc hardware for internal pins
if (pin < SOC_GPIO_PIN_COUNT) {
Expand All @@ -220,8 +223,8 @@ void analogWrite(uint8_t pin, int value) {
return;
}
pin_to_channel[pin] = cnt_channel--;
ledcSetup(cnt_channel, analog_frequency, analog_resolution);
ledcAttachPin(pin, cnt_channel);
ledcSetup(cnt_channel, 1000, 8);
}
ledcWrite(pin_to_channel[pin] - 1, value);
}
Expand All @@ -230,3 +233,23 @@ void analogWrite(uint8_t pin, int value) {
int8_t analogGetChannel(uint8_t pin) {
return pin_to_channel[pin] - 1;
}

void analogWriteFrequency(uint32_t freq) {
if (cnt_channel == LEDC_CHANNELS) {
return; //No channel used for analogWrite
}
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;
}

void analogWriteResolution(uint8_t bits) {
if (cnt_channel == LEDC_CHANNELS) {
return; //No channel used for analogWrite
}
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;
}
2 changes: 2 additions & 0 deletions cores/esp32/esp32-hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ void yield(void);

void analogWrite(uint8_t pin, int value);
int8_t analogGetChannel(uint8_t pin);
void analogWriteFrequency(uint32_t freq);
void analogWriteResolution(uint8_t bits);

//returns chip temperature in Celsius
float temperatureRead();
Expand Down