Skip to content

Commit 5544239

Browse files
committed
Add inverted option to ledcAttachPin and ledcAttachPinChannel
1 parent c7520cc commit 5544239

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

cores/esp32/esp32-hal-ledc.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static bool ledcDetachBus(void *bus) {
186186
return true;
187187
}
188188

189-
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel) {
189+
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel, bool inverted = false) {
190190
if (channel >= LEDC_CHANNELS) {
191191
log_e("Channel %u is not available (maximum %u)!", channel, LEDC_CHANNELS);
192192
return false;
@@ -256,6 +256,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c
256256
ledc_channel.gpio_num = pin;
257257
ledc_channel.duty = duty;
258258
ledc_channel.hpoint = 0;
259+
ledc_channel.flags.output_invert = inverted ? 1 : 0;
259260

260261
ledc_channel_config(&ledc_channel);
261262
}
@@ -289,7 +290,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c
289290
return true;
290291
}
291292

292-
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) {
293+
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution, bool inverted = false) {
293294
int free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels + 1);
294295
if (free_channel == 0) {
295296
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) {
298299
uint8_t channel = __builtin_ctz(free_channel); // Convert the free_channel bit to channel number
299300

300301
// Try the first available channel
301-
if (ledcAttachChannel(pin, freq, resolution, channel)) {
302+
if (ledcAttachChannel(pin, freq, resolution, channel, inverted)) {
302303
return true;
303304
}
304305

@@ -311,7 +312,7 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) {
311312
int group1_free_channel = (~ledc_handle.used_channels) & group1_mask;
312313
if (group1_free_channel != 0) {
313314
uint8_t group1_channel = __builtin_ctz(group1_free_channel);
314-
if (ledcAttachChannel(pin, freq, resolution, group1_channel)) {
315+
if (ledcAttachChannel(pin, freq, resolution, group1_channel, inverted)) {
315316
return true;
316317
}
317318
}

cores/esp32/esp32-hal-ledc.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ bool ledcSetClockSource(ledc_clk_cfg_t source);
8383
* @param pin GPIO pin
8484
* @param freq frequency of PWM signal
8585
* @param resolution resolution for LEDC pin
86-
*
86+
* @param inverted if true, output signal will be inverted (default: false)
87+
*
8788
* @return true if configuration is successful and pin was successfully attached, false otherwise.
8889
*/
89-
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution);
90+
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution, bool inverted = false);
9091

9192
/**
9293
* @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);
9596
* @param freq frequency of PWM signal
9697
* @param resolution resolution for LEDC pin
9798
* @param channel LEDC channel to attach to
99+
* @param inverted if true, output signal will be inverted (default: false)
98100
*
99101
* @return true if configuration is successful and pin was successfully attached, false otherwise.
100102
*/
101-
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel);
103+
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel, bool inverted = false);
102104

103105
/**
104106
* @brief Set the duty cycle of a given pin.

0 commit comments

Comments
 (0)