From ab16ecae760cdee6453b814186e662f4d66c851c Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Fri, 16 Feb 2024 14:07:09 +0100 Subject: [PATCH 1/9] feat(ledc): Add output invert option for LEDC pin + minor fixes --- cores/esp32/esp32-hal-ledc.c | 43 +++++++++++++++---- cores/esp32/esp32-hal-ledc.h | 2 +- cores/esp32/io_pin_remap.h | 18 ++++---- docs/en/api/ledc.rst | 30 +++++++++---- .../examples/AnalogOut/LEDCFade/LEDCFade.ino | 2 +- .../AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino | 2 +- 6 files changed, 69 insertions(+), 28 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 76d522c2313..16ad5a8ab9b 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -19,6 +19,8 @@ #include "esp32-hal-ledc.h" #include "driver/ledc.h" #include "esp32-hal-periman.h" +#include "soc/gpio_sig_map.h" +#include "esp_rom_gpio.h" #ifdef SOC_LEDC_SUPPORT_HS_MODE #define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM<<1) @@ -40,7 +42,7 @@ typedef struct { int used_channels : LEDC_CHANNELS; // Used channels as a bits } ledc_periph_t; -ledc_periph_t ledc_handle; +ledc_periph_t ledc_handle = {0}; static bool fade_initialized = false; @@ -58,15 +60,25 @@ static bool ledcDetachBus(void * bus){ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel) { - if (channel >= LEDC_CHANNELS || resolution > LEDC_MAX_BIT_WIDTH) - { - log_e("Channel %u is not available! (maximum %u) or bit width too big (maximum %u)", channel, LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH); + if(channel >= LEDC_CHANNELS || ledc_handle.used_channels & (1UL << channel)){ + log_e("Channel %u is not available (maximum %u) or already used!", channel, LEDC_CHANNELS); + return false; + } + + if (resolution > LEDC_MAX_BIT_WIDTH){ + log_e("LEDC resolution too big (maximum %u)", LEDC_MAX_BIT_WIDTH); return false; } perimanSetBusDeinit(ESP32_BUS_TYPE_LEDC, ledcDetachBus); ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC); - if(bus != NULL && !perimanClearPinBus(pin)){ + if(bus != NULL){ + log_e("Pin %u is already attached to LEDC (channel %u, resolution %u)", pin, bus->channel, bus->channel_resolution); + return false; + } + + if(!perimanClearPinBus(pin)){ + log_e("Pin %u is already attached to another bus and failed to detach", pin); return false; } @@ -120,11 +132,11 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) { uint8_t free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1); - if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH){ - log_e("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH); + if (free_channel == 0){ + log_e("No more LEDC channels available! (maximum is %u channels)", LEDC_CHANNELS); return false; } - int channel = log2(free_channel & -free_channel); + uint8_t channel = log2(free_channel & -free_channel); return ledcAttachChannel(pin, freq, resolution, channel); } @@ -265,6 +277,21 @@ uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution) return 0; } +bool ledcOutputInvert(uint8_t pin, bool out_invert) +{ + ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC); + if(bus != NULL){ + gpio_set_level(pin, out_invert); + #ifdef SOC_LEDC_SUPPORT_HS_MODE + esp_rom_gpio_connect_out_signal(pin, ((bus->channel/8 == 0) ? LEDC_HS_SIG_OUT0_IDX : LEDC_LS_SIG_OUT0_IDX) + ((bus->channel)%8), out_invert, 0); + #else + esp_rom_gpio_connect_out_signal(pin, LEDC_LS_SIG_OUT0_IDX + ((bus->channel)%8), out_invert, 0); + #endif + return true; + } + return false; +} + static IRAM_ATTR bool ledcFnWrapper(const ledc_cb_param_t *param, void *user_arg) { if (param->event == LEDC_FADE_END_EVT) { diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index 2d2c42e9369..4dadf0c3451 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -45,7 +45,6 @@ typedef struct { #endif } ledc_channel_handle_t; -//channel 0-15 resolution 1-16bits freq limits depend on resolution bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel); bool ledcWrite(uint8_t pin, uint32_t duty); @@ -55,6 +54,7 @@ uint32_t ledcRead(uint8_t pin); uint32_t ledcReadFreq(uint8_t pin); bool ledcDetach(uint8_t pin); uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution); +bool ledcOutputInvert(uint8_t pin, bool out_invert); //Fade functions bool ledcFade(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms); diff --git a/cores/esp32/io_pin_remap.h b/cores/esp32/io_pin_remap.h index 63fb16cefd0..049da110740 100644 --- a/cores/esp32/io_pin_remap.h +++ b/cores/esp32/io_pin_remap.h @@ -53,14 +53,16 @@ int8_t gpioNumberToDigitalPin(int8_t gpioNumber); #define i2cSlaveInit(num, sda, scl, slaveID, frequency, rx_len, tx_len) i2cSlaveInit(num, digitalPinToGPIONumber(sda), digitalPinToGPIONumber(scl), slaveID, frequency, rx_len, tx_len) // cores/esp32/esp32-hal-ledc.h -#define ledcAttach(pin, freq, resolution) ledcAttach(digitalPinToGPIONumber(pin), freq, resolution) -#define ledcWrite(pin, duty) ledcWrite(digitalPinToGPIONumber(pin), duty) -#define ledcWriteTone(pin, freq) ledcWriteTone(digitalPinToGPIONumber(pin), freq) -#define ledcWriteNote(pin, note, octave) ledcWriteNote(digitalPinToGPIONumber(pin), note, octave) -#define ledcRead(pin) ledcRead(digitalPinToGPIONumber(pin)) -#define ledcReadFreq(pin) ledcReadFreq(digitalPinToGPIONumber(pin)) -#define ledcDetach(pin) ledcDetach(digitalPinToGPIONumber(pin)) -#define ledcChangeFrequency(pin, freq, resolution) ledcChangeFrequency(digitalPinToGPIONumber(pin), freq, resolution) +#define ledcAttach(pin, freq, resolution) ledcAttach(digitalPinToGPIONumber(pin), freq, resolution) +#define ledcAttachChannel(pin, freq, resolution, channel) ledcAttachChannel(digitalPinToGPIONumber(pin), freq, resolution, channel) +#define ledcWrite(pin, duty) ledcWrite(digitalPinToGPIONumber(pin), duty) +#define ledcWriteTone(pin, freq) ledcWriteTone(digitalPinToGPIONumber(pin), freq) +#define ledcWriteNote(pin, note, octave) ledcWriteNote(digitalPinToGPIONumber(pin), note, octave) +#define ledcRead(pin) ledcRead(digitalPinToGPIONumber(pin)) +#define ledcReadFreq(pin) ledcReadFreq(digitalPinToGPIONumber(pin)) +#define ledcDetach(pin) ledcDetach(digitalPinToGPIONumber(pin)) +#define ledcChangeFrequency(pin, freq, resolution) ledcChangeFrequency(digitalPinToGPIONumber(pin), freq, resolution) +#define ledcOutputInvert(pin, out_invert) ledcOutputInvert(digitalPinToGPIONumber(pin), out_invert) #define ledcFade(pin, start_duty, target_duty, max_fade_time_ms) ledcFade(digitalPinToGPIONumber(pin), start_duty, target_duty, max_fade_time_ms) #define ledcFadeWithInterrupt(pin, start_duty, target_duty, max_fade_time_ms, userFunc) ledcFadeWithInterrupt(digitalPinToGPIONumber(pin), start_duty, target_duty, max_fade_time_ms, userFunc) diff --git a/docs/en/api/ledc.rst b/docs/en/api/ledc.rst index 1abeea2f6ee..08912d880dd 100644 --- a/docs/en/api/ledc.rst +++ b/docs/en/api/ledc.rst @@ -26,19 +26,17 @@ Arduino-ESP32 LEDC API ledcAttach ********** -This function is used to setup LEDC pin with given frequency and resolution. -LEDC channel will be selected automatically. +This function is used to setup LEDC pin with given frequency and resolution. LEDC channel will be selected automatically. .. code-block:: arduino - bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); + bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution, int8_t channel); * ``pin`` select LEDC pin. * ``freq`` select frequency of pwm. * ``resolution`` select resolution for LEDC channel. - * range is 1-14 bits (1-20 bits for ESP32). - + This function will return ``true`` if configuration is successful. If ``false`` is returned, error occurs and LEDC channel was not configured. @@ -49,15 +47,14 @@ This function is used to setup LEDC pin with given frequency, resolution and cha .. code-block:: arduino - bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel); + bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution, int8_t channel); * ``pin`` select LEDC pin. * ``freq`` select frequency of pwm. * ``resolution`` select resolution for LEDC channel. -* ``channel`` select LEDC channel. - * range is 1-14 bits (1-20 bits for ESP32). - +* ``channel`` select LEDC channel. + This function will return ``true`` if configuration is successful. If ``false`` is returned, error occurs and LEDC channel was not configured. @@ -171,6 +168,21 @@ This function is used to set frequency for the LEDC pin. This function will return ``frequency`` configured for the LEDC channel. If ``0`` is returned, error occurs and the LEDC channel frequency was not set. +ledcOutputInvert +**************** + +This function is used to set inverting output for the LEDC pin. + +.. code-block:: arduino + + bool ledcOutputInvert(uint8_t pin, bool out_invert); + +* ``pin`` select LEDC pin. +* ``out_invert`` select, if output should be inverted (1 = inverting output). + +This function returns ``true`` if setting inverting output was successful. +If ``false`` is returned, an error occurred and the inverting output was not set. + ledcFade ******** diff --git a/libraries/ESP32/examples/AnalogOut/LEDCFade/LEDCFade.ino b/libraries/ESP32/examples/AnalogOut/LEDCFade/LEDCFade.ino index 002df746014..19c2be3428d 100644 --- a/libraries/ESP32/examples/AnalogOut/LEDCFade/LEDCFade.ino +++ b/libraries/ESP32/examples/AnalogOut/LEDCFade/LEDCFade.ino @@ -33,7 +33,7 @@ void setup() { Serial.begin(115200); while(!Serial) delay(10); - // Setup timer and attach timer to a led pins + // Setup timer with given frequency, resolution and attach it to a led pin with auto-selected channel ledcAttach(LED_PIN, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT); // Setup and start fade on led (duty from 0 to 4095) diff --git a/libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino b/libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino index 1104a6b4c63..0135ab1d0c1 100644 --- a/libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino +++ b/libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino @@ -26,7 +26,7 @@ void setup() delay(10); // Initialize pins as LEDC channels - // resolution 1-16 bits, freq limits depend on resolution + // resolution 1-16 bits, freq limits depend on resolution, channel is automatically selected ledcAttach(ledR, 12000, 8); // 12 kHz PWM, 8-bit resolution ledcAttach(ledG, 12000, 8); ledcAttach(ledB, 12000, 8); From 994e3f27fca9f7eeac8546cc7888546757306f4c Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Fri, 16 Feb 2024 19:49:51 +0100 Subject: [PATCH 2/9] docs(ledc): Document LEDC functions in header file --- cores/esp32/esp32-hal-ledc.h | 150 ++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 10 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index 4dadf0c3451..798ecb8f21b 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -45,20 +45,150 @@ typedef struct { #endif } ledc_channel_handle_t; -bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); -bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel); -bool ledcWrite(uint8_t pin, uint32_t duty); -uint32_t ledcWriteTone(uint8_t pin, uint32_t freq); -uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave); -uint32_t ledcRead(uint8_t pin); -uint32_t ledcReadFreq(uint8_t pin); -bool ledcDetach(uint8_t pin); -uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution); -bool ledcOutputInvert(uint8_t pin, bool out_invert); +/** + * @brief Attach a pin to the LEDC driver, with a given frequency and resolution. + * Channel is automatically assigned. + * + * @param pin GPIO pin + * @param freq frequency of PWM signal + * @param resolution resolution for LEDC pin + * + * @return true if configuration is successful and pin was successfully attached, false otherwise. + */ +bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); + +/** + * @brief Attach a pin to the LEDC driver, with a given frequency, resolution and channel. + * + * @param pin GPIO pin + * @param freq frequency of PWM signal + * @param resolution resolution for LEDC pin + * @param channel LEDC channel to attach to + * + * @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); + +/** + * @brief Set the duty cycle of a given pin. + * + * @param pin GPIO pin + * @param duty duty cycle to set + * + * @return true if duty cycle was successfully set, false otherwise. + */ +bool ledcWrite(uint8_t pin, uint32_t duty); + +/** + * @brief Sets the duty to 50 % PWM tone on selected frequency. + * + * @param pin GPIO pin + * @param freq select frequency of pwm signal. If frequency is 0, duty will be set to 0. + * + * @return frequency if tone was successfully set. + * If ``0`` is returned, error occurs and LEDC pin was not configured. + */ +uint32_t ledcWriteTone(uint8_t pin, uint32_t freq); + +/** + * @brief Sets the LEDC pin to specific note. + * + * @param pin GPIO pin + * @param note select note to be set (NOTE_C, NOTE_Cs, NOTE_D, NOTE_Eb, NOTE_E, NOTE_F, NOTE_Fs, NOTE_G, NOTE_Gs, NOTE_A, NOTE_Bb, NOTE_B). + * @param octave select octave for note. + * + * @return frequency if note was successfully set. + * If ``0`` is returned, error occurs and LEDC pin was not configured. + */ +uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave); + +/** + * @brief Read the duty cycle of a given LEDC pin. + * + * @param pin GPIO pin + * + * @return duty cycle of selected LEDC pin. + */ +uint32_t ledcRead(uint8_t pin); + +/** + * @brief Read the frequency of a given LEDC pin. + * + * @param pin GPIO pin + * + * @return frequency of selected LEDC pin. + */ +uint32_t ledcReadFreq(uint8_t pin); + +/** + * @brief Detach a pin from the LEDC driver. + * + * @param pin GPIO pin + * + * @return true if pin was successfully detached, false otherwise. + */ +bool ledcDetach(uint8_t pin); + +/** + * @brief Change the frequency and resolution of a given LEDC pin. + * + * @param pin GPIO pin + * @param freq frequency of PWM signal + * @param resolution resolution for LEDC pin + * + * @return frequency configured for the LEDC channel. + * If ``0`` is returned, error occurs and LEDC pin was not configured. + */ +uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution); + +/** + * @brief Sets inverting of the output signal for a given LEDC pin. + * + * @param pin GPIO pin + * @param out_invert select, if output should be inverted (1 = inverting output). + * + * @return true if output inverting was successfully set, false otherwise. + */ +bool ledcOutputInvert(uint8_t pin, bool out_invert); //Fade functions +/** + * @brief Setup and start a fade on a given LEDC pin. + * + * @param pin GPIO pin + * @param start_duty initial duty cycle of the fade + * @param target_duty target duty cycle of the fade + * @param max_fade_time_ms maximum fade time in milliseconds + * + * @return true if fade was successfully set and started, false otherwise. + */ bool ledcFade(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms); + +/** + * @brief Setup and start a fade on a given LEDC pin with a callback function. + * + * @param pin GPIO pin + * @param start_duty initial duty cycle of the fade + * @param target_duty target duty cycle of the fade + * @param max_fade_time_ms maximum fade time in milliseconds + * @param userFunc callback function to be called after fade is finished + * + * @return true if fade was successfully set and started, false otherwise. + */ bool ledcFadeWithInterrupt(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void)); + +/** + * @brief Setup and start a fade on a given LEDC pin with a callback function and argument. + * + * @param pin GPIO pin + * @param start_duty initial duty cycle of the fade + * @param target_duty target duty cycle of the fade + * @param max_fade_time_ms maximum fade time in milliseconds + * @param userFunc callback function to be called after fade is finished + * @param arg argument to be passed to the callback function + * + * @return true if fade was successfully set and started, false otherwise. + */ bool ledcFadeWithInterruptArg(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void*), void * arg); #ifdef __cplusplus From 1acdd7819b343f59523fc25978f6f3b6f337714b Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:37:13 +0100 Subject: [PATCH 3/9] feat(ledc): Replace log2 with __builtin_ctz --- cores/esp32/esp32-hal-ledc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 16ad5a8ab9b..70793156e9f 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -136,7 +136,7 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) log_e("No more LEDC channels available! (maximum is %u channels)", LEDC_CHANNELS); return false; } - uint8_t channel = log2(free_channel & -free_channel); + uint8_t channel = __builtin_ctz(free_channel); // Convert the free_channel bit to channel number return ledcAttachChannel(pin, freq, resolution, channel); } From 651d19fc49da4c95bfc901eaeb5ce3c4fcb2fb1c Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 19 Feb 2024 15:03:56 +0100 Subject: [PATCH 4/9] fix(ledc): Fixing free_channel for > 8 supported channels --- cores/esp32/esp32-hal-ledc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 70793156e9f..9051a77bd6d 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -131,7 +131,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) { - uint8_t free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1); + 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); return false; From 68abeaeb1f3f3b8dd60bab1003b74df1c3301317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 20 Feb 2024 13:39:45 +0100 Subject: [PATCH 5/9] fix(ledc): Apply suggestions from code review Co-authored-by: Rodrigo Garcia --- cores/esp32/esp32-hal-ledc.c | 9 ++++++--- cores/esp32/esp32-hal-ledc.h | 2 +- docs/en/api/ledc.rst | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 9051a77bd6d..02557502e5e 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -64,9 +64,12 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c log_e("Channel %u is not available (maximum %u) or already used!", channel, LEDC_CHANNELS); return false; } - - if (resolution > LEDC_MAX_BIT_WIDTH){ - log_e("LEDC resolution too big (maximum %u)", LEDC_MAX_BIT_WIDTH); + if (freq == 0) { + log_e("LEDC pin %u - frequency can't be zero.", pin); + return false; + } + if (resolution == 0 || resolution > LEDC_MAX_BIT_WIDTH){ + log_e("LEDC pin %u - resolution is zero or it is too big (maximum %u)", pin, LEDC_MAX_BIT_WIDTH); return false; } diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index 798ecb8f21b..94dcf7509d1 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -145,7 +145,7 @@ uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution); * @brief Sets inverting of the output signal for a given LEDC pin. * * @param pin GPIO pin - * @param out_invert select, if output should be inverted (1 = inverting output). + * @param out_invert select, if output should be inverted (true = inverting output). * * @return true if output inverting was successfully set, false otherwise. */ diff --git a/docs/en/api/ledc.rst b/docs/en/api/ledc.rst index 08912d880dd..16db9677114 100644 --- a/docs/en/api/ledc.rst +++ b/docs/en/api/ledc.rst @@ -178,7 +178,7 @@ This function is used to set inverting output for the LEDC pin. bool ledcOutputInvert(uint8_t pin, bool out_invert); * ``pin`` select LEDC pin. -* ``out_invert`` select, if output should be inverted (1 = inverting output). +* ``out_invert`` select, if output should be inverted (true = inverting output). This function returns ``true`` if setting inverting output was successful. If ``false`` is returned, an error occurred and the inverting output was not set. From a02e31d15178ffdc5fe3f29d11c8731f4cee6a0c Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 20 Feb 2024 13:43:53 +0100 Subject: [PATCH 6/9] fix(ledc): Added freq check to ledcChangeFrequency --- cores/esp32/esp32-hal-ledc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 02557502e5e..2005777d7bd 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -254,9 +254,12 @@ uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution) { ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC); if(bus != NULL){ - - if(resolution > LEDC_MAX_BIT_WIDTH){ - log_e("LEDC resolution too big (maximum %u)", LEDC_MAX_BIT_WIDTH); + if (freq == 0) { + log_e("LEDC pin %u - frequency can't be zero.", pin); + return 0; + } + if (resolution == 0 || resolution > LEDC_MAX_BIT_WIDTH){ + log_e("LEDC pin %u - resolution is zero or it is too big (maximum %u)", pin, LEDC_MAX_BIT_WIDTH); return 0; } uint8_t group=(bus->channel/8), timer=((bus->channel/2)%4); From c5ca2840a3f0601e77ebb1d3caa5278a7b279338 Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 20 Feb 2024 13:49:44 +0100 Subject: [PATCH 7/9] docs(ledc): Fix ledc documentation formatting --- docs/en/api/ledc.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/en/api/ledc.rst b/docs/en/api/ledc.rst index 16db9677114..d7ee816d668 100644 --- a/docs/en/api/ledc.rst +++ b/docs/en/api/ledc.rst @@ -34,7 +34,8 @@ This function is used to setup LEDC pin with given frequency and resolution. LED * ``pin`` select LEDC pin. * ``freq`` select frequency of pwm. -* ``resolution`` select resolution for LEDC channel. +* ``resolution`` select resolution for LEDC channel. + * range is 1-14 bits (1-20 bits for ESP32). This function will return ``true`` if configuration is successful. @@ -51,8 +52,10 @@ This function is used to setup LEDC pin with given frequency, resolution and cha * ``pin`` select LEDC pin. * ``freq`` select frequency of pwm. -* ``resolution`` select resolution for LEDC channel. +* ``resolution`` select resolution for LEDC channel. + * range is 1-14 bits (1-20 bits for ESP32). + * ``channel`` select LEDC channel. This function will return ``true`` if configuration is successful. From 5ded6ff89f7a8122032725c6103e28d073832211 Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 20 Feb 2024 13:57:50 +0100 Subject: [PATCH 8/9] docs(migration): Add new functions to the migration guide --- docs/en/migration_guides/2.x_to_3.0.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/en/migration_guides/2.x_to_3.0.rst b/docs/en/migration_guides/2.x_to_3.0.rst index 3ccbfba5ffb..8be295cde75 100644 --- a/docs/en/migration_guides/2.x_to_3.0.rst +++ b/docs/en/migration_guides/2.x_to_3.0.rst @@ -63,8 +63,10 @@ New APIs ******** * ``ledcAttach`` used to set up the LEDC pin (merged ``ledcSetup`` and ``ledcAttachPin`` functions). -* ``timerGetFrequency`` used to get the actual frequency of the timer. -* ``timerAttachInterruptArg`` used to attach the interrupt to a timer using arguments. +* ``ledcOutputInvert`` used to attach the interrupt to a timer using arguments. +* ``ledcFade`` used to set up and start a fade on a given LEDC pin. +* ``ledcFadeWithInterrupt`` used to set up and start a fade on a given LEDC pin with an interrupt. +* ``ledcFadeWithInterruptArg`` used to set up and start a fade on a given LEDC pin with an interrupt using arguments. Changes in APIs *************** From 25a785ca2bab1538322054fdf8b20bcdf6def319 Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 20 Feb 2024 16:00:29 +0100 Subject: [PATCH 9/9] docs(ledc): Fix functions name and parameters --- docs/en/api/ledc.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/api/ledc.rst b/docs/en/api/ledc.rst index d7ee816d668..200e19f81e5 100644 --- a/docs/en/api/ledc.rst +++ b/docs/en/api/ledc.rst @@ -30,7 +30,7 @@ This function is used to setup LEDC pin with given frequency and resolution. LED .. code-block:: arduino - bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution, int8_t channel); + bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); * ``pin`` select LEDC pin. * ``freq`` select frequency of pwm. @@ -48,7 +48,7 @@ This function is used to setup LEDC pin with given frequency, resolution and cha .. code-block:: arduino - bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution, int8_t channel); + bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int8_t channel); * ``pin`` select LEDC pin. * ``freq`` select frequency of pwm.