LEDC - AnalogWrite new API + ledcAttachPin duty fix (#7346)
This commit is contained in:
parent
bcc1d758fc
commit
9762b2392a
2 changed files with 30 additions and 3 deletions
|
|
@ -166,6 +166,7 @@ void ledcAttachPin(uint8_t pin, uint8_t chan)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint8_t group=(chan/8), channel=(chan%8), timer=((chan/2)%4);
|
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 = {
|
ledc_channel_config_t ledc_channel = {
|
||||||
.speed_mode = group,
|
.speed_mode = group,
|
||||||
|
|
@ -173,7 +174,7 @@ void ledcAttachPin(uint8_t pin, uint8_t chan)
|
||||||
.timer_sel = timer,
|
.timer_sel = timer,
|
||||||
.intr_type = LEDC_INTR_DISABLE,
|
.intr_type = LEDC_INTR_DISABLE,
|
||||||
.gpio_num = pin,
|
.gpio_num = pin,
|
||||||
.duty = 0,
|
.duty = duty,
|
||||||
.hpoint = 0
|
.hpoint = 0
|
||||||
};
|
};
|
||||||
ledc_channel_config(&ledc_channel);
|
ledc_channel_config(&ledc_channel);
|
||||||
|
|
@ -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 int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 };
|
||||||
static int cnt_channel = LEDC_CHANNELS;
|
static int cnt_channel = LEDC_CHANNELS;
|
||||||
|
static uint8_t analog_resolution = 8;
|
||||||
|
static int analog_frequency = 1000;
|
||||||
void analogWrite(uint8_t pin, int value) {
|
void analogWrite(uint8_t pin, int value) {
|
||||||
// Use ledc hardware for internal pins
|
// Use ledc hardware for internal pins
|
||||||
if (pin < SOC_GPIO_PIN_COUNT) {
|
if (pin < SOC_GPIO_PIN_COUNT) {
|
||||||
|
|
@ -220,8 +223,8 @@ void analogWrite(uint8_t pin, int value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pin_to_channel[pin] = cnt_channel--;
|
pin_to_channel[pin] = cnt_channel--;
|
||||||
|
ledcSetup(cnt_channel, analog_frequency, analog_resolution);
|
||||||
ledcAttachPin(pin, cnt_channel);
|
ledcAttachPin(pin, cnt_channel);
|
||||||
ledcSetup(cnt_channel, 1000, 8);
|
|
||||||
}
|
}
|
||||||
ledcWrite(pin_to_channel[pin] - 1, value);
|
ledcWrite(pin_to_channel[pin] - 1, value);
|
||||||
}
|
}
|
||||||
|
|
@ -230,3 +233,25 @@ void analogWrite(uint8_t pin, int value) {
|
||||||
int8_t analogGetChannel(uint8_t pin) {
|
int8_t analogGetChannel(uint8_t pin) {
|
||||||
return pin_to_channel[pin] - 1;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,8 @@ void yield(void);
|
||||||
|
|
||||||
void analogWrite(uint8_t pin, int value);
|
void analogWrite(uint8_t pin, int value);
|
||||||
int8_t analogGetChannel(uint8_t pin);
|
int8_t analogGetChannel(uint8_t pin);
|
||||||
|
void analogWriteFrequency(uint32_t freq);
|
||||||
|
void analogWriteResolution(uint8_t bits);
|
||||||
|
|
||||||
//returns chip temperature in Celsius
|
//returns chip temperature in Celsius
|
||||||
float temperatureRead();
|
float temperatureRead();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue