diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index ffb24db4599..e04da7f841d 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -186,7 +186,7 @@ static bool ledcDetachBus(void *bus) { return true; } -bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel) { +bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel, bool inverted = false) { if (channel >= LEDC_CHANNELS) { log_e("Channel %u is not available (maximum %u)!", channel, LEDC_CHANNELS); return false; @@ -256,6 +256,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c ledc_channel.gpio_num = pin; ledc_channel.duty = duty; ledc_channel.hpoint = 0; + ledc_channel.flags.output_invert = inverted ? 1 : 0; ledc_channel_config(&ledc_channel); } @@ -289,7 +290,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c return true; } -bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) { +bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution, bool inverted = false) { int free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels + 1); if (free_channel == 0) { log_e("No more LEDC channels available! (maximum is %u channels)", LEDC_CHANNELS); @@ -298,7 +299,7 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) { uint8_t channel = __builtin_ctz(free_channel); // Convert the free_channel bit to channel number // Try the first available channel - if (ledcAttachChannel(pin, freq, resolution, channel)) { + if (ledcAttachChannel(pin, freq, resolution, channel, inverted)) { return true; } @@ -311,7 +312,7 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) { int group1_free_channel = (~ledc_handle.used_channels) & group1_mask; if (group1_free_channel != 0) { uint8_t group1_channel = __builtin_ctz(group1_free_channel); - if (ledcAttachChannel(pin, freq, resolution, group1_channel)) { + if (ledcAttachChannel(pin, freq, resolution, group1_channel, inverted)) { return true; } } diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index 1663b884a3f..e18a4d3f14b 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -83,10 +83,11 @@ bool ledcSetClockSource(ledc_clk_cfg_t source); * @param pin GPIO pin * @param freq frequency of PWM signal * @param resolution resolution for LEDC pin - * + * @param inverted if true, output signal will be inverted (default: false) + * * @return true if configuration is successful and pin was successfully attached, false otherwise. */ -bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); +bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution, bool inverted = false); /** * @brief Attach a pin to the LEDC driver, with a given frequency, resolution and channel. @@ -95,10 +96,11 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); * @param freq frequency of PWM signal * @param resolution resolution for LEDC pin * @param channel LEDC channel to attach to + * @param inverted if true, output signal will be inverted (default: false) * * @return true if configuration is successful and pin was successfully attached, false otherwise. */ -bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel); +bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel, bool inverted = false); /** * @brief Set the duty cycle of a given pin.