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 all 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
31 changes: 28 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,25 @@ 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) {
for (int channel = LEDC_CHANNELS - 1; channel >= cnt_channel; channel--) {
ledcChangeFrequency(channel, freq, analog_resolution);
}
}
analog_frequency = freq;
}

void analogWriteResolution(uint8_t bits) {
if(bits > LEDC_MAX_BIT_WIDTH) {
log_w("analogWrite resolution width too big! Setting to maximum %u bits)", LEDC_MAX_BIT_WIDTH);
bits = LEDC_MAX_BIT_WIDTH;
}
if (cnt_channel != LEDC_CHANNELS) {
for (int channel = LEDC_CHANNELS - 1; channel >= cnt_channel; channel--) {
ledcChangeFrequency(channel, analog_frequency, bits);
}
}
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