Skip to content

feat(ledc): Add inverted parameter to ledcAttachPin and ledcAttachPinChannel #11660

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

Closed
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
9 changes: 5 additions & 4 deletions cores/esp32/esp32-hal-ledc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand Down
8 changes: 5 additions & 3 deletions cores/esp32/esp32-hal-ledc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
Loading