]> granicus.if.org Git - esp-idf/commitdiff
driver(ledc): fix duty and fade issues and add thread-safe version APIs.
authorkooho <2229179028@qq.com>
Thu, 12 Apr 2018 07:38:39 +0000 (15:38 +0800)
committerWangjialin <wangjialin@espressif.com>
Mon, 14 May 2018 04:27:21 +0000 (12:27 +0800)
1. Fix the duty fade check issue reported from https://github.com/espressif/esp-idf/issues/1914
2. Fix the duty overflow issue when duty_scale is set to 1.
3. Fix the duty fade error when a fade operation is under way. We must configure a new duty setting after the previous fade operation has finished due to hardware limit.
4. Add thread-safe version APIs to set duty and fade.
5. Correct the duty range in driver.

components/driver/include/driver/ledc.h
components/driver/ledc.c

index c5250213d6c441fe5fc17b340b74a0901113bdff..8f70b1c0033e2a4b83769390930d8f7f35f2f5bf 100644 (file)
@@ -27,6 +27,7 @@ extern "C" {
 #define LEDC_APB_CLK_HZ (APB_CLK_FREQ)
 #define LEDC_REF_CLK_HZ (1*1000000)
 #define LEDC_ERR_DUTY   (0xFFFFFFFF)
+#define LEDC_ERR_VAL    (-1)
 
 typedef enum {
     LEDC_HIGH_SPEED_MODE = 0, /*!< LEDC high speed speed_mode */
@@ -42,6 +43,7 @@ typedef enum {
 typedef enum {
     LEDC_DUTY_DIR_DECREASE = 0,    /*!< LEDC duty decrease direction */
     LEDC_DUTY_DIR_INCREASE = 1,    /*!< LEDC duty increase direction */
+    LEDC_DUTY_DIR_MAX,
 } ledc_duty_direction_t;
 
 typedef enum  {
@@ -54,6 +56,7 @@ typedef enum {
     LEDC_TIMER_1,     /*!< LEDC timer 1 */
     LEDC_TIMER_2,     /*!< LEDC timer 2 */
     LEDC_TIMER_3,     /*!< LEDC timer 3 */
+    LEDC_TIMER_MAX,
 } ledc_timer_t;
 
 typedef enum {
@@ -69,12 +72,27 @@ typedef enum {
 } ledc_channel_t;
 
 typedef enum {
-    LEDC_TIMER_10_BIT = 10, /*!< LEDC PWM duty resolution of 10 bits */
-    LEDC_TIMER_11_BIT = 11, /*!< LEDC PWM duty resolution of 11 bits */
-    LEDC_TIMER_12_BIT = 12, /*!< LEDC PWM duty resolution of 12 bits */
-    LEDC_TIMER_13_BIT = 13, /*!< LEDC PWM duty resolution of 13 bits */
-    LEDC_TIMER_14_BIT = 14, /*!< LEDC PWM duty resolution of 14 bits */
-    LEDC_TIMER_15_BIT = 15, /*!< LEDC PWM duty resolution of 15 bits */
+    LEDC_TIMER_1_BIT = 1,   /*!< LEDC PWM duty resolution of  1 bits */
+    LEDC_TIMER_2_BIT,       /*!< LEDC PWM duty resolution of  2 bits */
+    LEDC_TIMER_3_BIT,       /*!< LEDC PWM duty resolution of  3 bits */
+    LEDC_TIMER_4_BIT,       /*!< LEDC PWM duty resolution of  4 bits */
+    LEDC_TIMER_5_BIT,       /*!< LEDC PWM duty resolution of  5 bits */
+    LEDC_TIMER_6_BIT,       /*!< LEDC PWM duty resolution of  6 bits */
+    LEDC_TIMER_7_BIT,       /*!< LEDC PWM duty resolution of  7 bits */
+    LEDC_TIMER_8_BIT,       /*!< LEDC PWM duty resolution of  8 bits */
+    LEDC_TIMER_9_BIT,       /*!< LEDC PWM duty resolution of  9 bits */
+    LEDC_TIMER_10_BIT,      /*!< LEDC PWM duty resolution of 10 bits */
+    LEDC_TIMER_11_BIT,      /*!< LEDC PWM duty resolution of 11 bits */
+    LEDC_TIMER_12_BIT,      /*!< LEDC PWM duty resolution of 12 bits */
+    LEDC_TIMER_13_BIT,      /*!< LEDC PWM duty resolution of 13 bits */
+    LEDC_TIMER_14_BIT,      /*!< LEDC PWM duty resolution of 14 bits */
+    LEDC_TIMER_15_BIT,      /*!< LEDC PWM duty resolution of 15 bits */
+    LEDC_TIMER_16_BIT,      /*!< LEDC PWM duty resolution of 16 bits */
+    LEDC_TIMER_17_BIT,      /*!< LEDC PWM duty resolution of 17 bits */
+    LEDC_TIMER_18_BIT,      /*!< LEDC PWM duty resolution of 18 bits */
+    LEDC_TIMER_19_BIT,      /*!< LEDC PWM duty resolution of 19 bits */
+    LEDC_TIMER_20_BIT,      /*!< LEDC PWM duty resolution of 20 bits */
+    LEDC_TIMER_BIT_MAX,
 } ledc_timer_bit_t;
 
 typedef enum {
@@ -92,7 +110,8 @@ typedef struct {
     ledc_channel_t channel;         /*!< LEDC channel (0 - 7) */
     ledc_intr_type_t intr_type;     /*!< configure interrupt, Fade interrupt enable  or Fade interrupt disable */
     ledc_timer_t timer_sel;         /*!< Select the timer source of channel (0 - 3) */
-    uint32_t duty;                  /*!< LEDC channel duty, the range of duty setting is [0, (2**duty_resolution) - 1] */
+    uint32_t duty;                  /*!< LEDC channel duty, the range of duty setting is [0, (2**duty_resolution)] */
+    int hpoint;                     /*!< LEDC channel hpoint value, the max value is 0xfffff */
 } ledc_channel_config_t;
 
 /**
@@ -137,9 +156,11 @@ esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf);
 
 /**
  * @brief LEDC update channel parameters
- *        Call this function to activate the LEDC updated parameters.
- *        After ledc_set_duty, ledc_set_fade, we need to call this function to update the settings.
- *
+ * @note  Call this function to activate the LEDC updated parameters.
+ *        After ledc_set_duty, we need to call this function to update the settings.
+ * @note  ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to
+ *        control one LEDC channel in different tasks at the same time.
+ *        A thread-safe version of API is ledc_set_duty_and_update
  * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode,
  * @param channel LEDC channel (0-7), select from ledc_channel_t
  *
@@ -191,12 +212,47 @@ esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t
 uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num);
 
 /**
- * @brief LEDC set duty
+ * @brief LEDC set duty and hpoint value
  *        Only after calling ledc_update_duty will the duty update.
+ * @note  ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to
+ *        control one LEDC channel in different tasks at the same time.
+ *        A thread-safe version of API is ledc_set_duty_and_update
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
+ * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
+ * @param channel LEDC channel (0-7), select from ledc_channel_t
+ * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
+ * @param hpoint Set the LEDC hpoint value(max: 0xfffff)
+ *
+ * @return
+ *     - ESP_OK Success
+ *     - ESP_ERR_INVALID_ARG Parameter error
+ */
+esp_err_t ledc_set_duty_with_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint);
+
+/**
+ * @brief LEDC get hpoint value, the counter value when the output is set high level.
  *
  * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  * @param channel LEDC channel (0-7), select from ledc_channel_t
- * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution) - 1]
+ * @return
+ *     - LEDC_ERR_VAL if parameter error
+ *     - Others Current hpoint value of LEDC channel
+ */
+int ledc_get_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel);
+
+/**
+ * @brief LEDC set duty
+ *        This function do not change the hpoint value of this channel. if needed, please call ledc_set_duty_with_hpoint.
+ *        only after calling ledc_update_duty will the duty update.
+ * @note  ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to
+ *        control one LEDC channel in different tasks at the same time.
+ *        A thread-safe version of API is ledc_set_duty_and_update.
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
+ * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
+ * @param channel LEDC channel (0-7), select from ledc_channel_t
+ * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
  *
  * @return
  *     - ESP_OK Success
@@ -219,20 +275,21 @@ uint32_t ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel);
 /**
  * @brief LEDC set gradient
  *        Set LEDC gradient, After the function calls the ledc_update_duty function, the function can take effect.
- *
- * @param  speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
- * @param  channel LEDC channel (0-7), select from ledc_channel_t
- * @param  duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution) - 1]
- * @param  gradule_direction Set the direction of the gradient
- * @param  step_num Set the number of the gradient
- * @param  duty_cyle_num Set how many LEDC tick each time the gradient lasts
- * @param  duty_scale Set gradient change amplitude
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
+ * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
+ * @param channel LEDC channel (0-7), select from ledc_channel_t
+ * @param duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution)]
+ * @param fade_direction Set the direction of the gradient
+ * @param step_num Set the number of the gradient
+ * @param duty_cyle_num Set how many LEDC tick each time the gradient lasts
+ * @param duty_scale Set gradient change amplitude
  *
  * @return
  *     - ESP_OK Success
  *     - ESP_ERR_INVALID_ARG Parameter error
  */
-esp_err_t ledc_set_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, ledc_duty_direction_t gradule_direction,
+esp_err_t ledc_set_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, ledc_duty_direction_t fade_direction,
                         uint32_t step_num, uint32_t duty_cyle_num, uint32_t duty_scale);
 
 /**
@@ -259,7 +316,7 @@ esp_err_t ledc_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags,
  * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  * @param timer_sel  Timer index (0-3), there are 4 timers in LEDC module
  * @param clock_divider Timer clock divide value, the timer clock is divided from the selected clock source
- * @param duty_resolution Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution) - 1]
+ * @param duty_resolution Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution)]
  * @param clk_src Select LEDC source clock.
  *
  * @return
@@ -319,9 +376,14 @@ esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel);
 esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx);
 
 /**
- * @brief Set LEDC fade function. Should call ledc_fade_func_install() before calling this function.
+ * @brief Set LEDC fade function.
+ * @note  Call ledc_fade_func_install() once before calling this function.
  *        Call ledc_fade_start() after this to start fading.
- *
+ * @note  ledc_set_fade_with_step, ledc_set_fade_with_time and ledc_fade_start are not thread-safe, do not call these functions to
+ *        control one LEDC channel in different tasks at the same time.
+ *        A thread-safe version of API is ledc_set_fade_step_and_start
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
  * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode,
  * @param channel LEDC channel index (0-7), select from ledc_channel_t
  * @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
@@ -334,12 +396,17 @@ esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint
  *     - ESP_ERR_INVALID_STATE Fade function not installed.
  *     - ESP_FAIL Fade function init error
  */
-esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int scale, int cycle_num);
+esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num);
 
 /**
- * @brief Set LEDC fade function, with a limited time. Should call ledc_fade_func_install() before calling this function.
+ * @brief Set LEDC fade function, with a limited time.
+ * @note  Call ledc_fade_func_install() once before calling this function.
  *        Call ledc_fade_start() after this to start fading.
- *
+ * @note  ledc_set_fade_with_step, ledc_set_fade_with_time and ledc_fade_start are not thread-safe, do not call these functions to
+ *        control one LEDC channel in different tasks at the same time.
+ *        A thread-safe version of API is ledc_set_fade_step_and_start
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
  * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode,
  * @param channel LEDC channel index (0-7), select from ledc_channel_t
  * @param target_duty Target duty of fading.( 0 - (2 ** duty_resolution - 1)))
@@ -354,8 +421,7 @@ esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel
 esp_err_t ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int max_fade_time_ms);
 
 /**
- * @brief Install ledc fade function. This function will occupy interrupt of LEDC module.
- *
+ * @brief Install LEDC fade function. This function will occupy interrupt of LEDC module.
  * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  *        ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  *
@@ -373,18 +439,70 @@ void ledc_fade_func_uninstall();
 
 /**
  * @brief Start LEDC fading.
- *
+ * @note  Call ledc_fade_func_install() once before calling this function.
+ *        Call this API right after ledc_set_fade_with_time or ledc_set_fade_with_step before to start fading.
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
  * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  * @param channel LEDC channel number
- * @param wait_done Whether to block until fading done.
+ * @param fade_mode Whether to block until fading done.
  *
  * @return
  *     - ESP_OK Success
  *     - ESP_ERR_INVALID_STATE Fade function not installed.
  *     - ESP_ERR_INVALID_ARG Parameter error.
  */
-esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t wait_done);
+esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t fade_mode);
+
+/**
+ * @brief A thread-safe API to set duty for LEDC channel and update the settings immediately
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
+ *
+ * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
+ * @param channel LEDC channel (0-7), select from ledc_channel_t
+ * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
+ * @param hpoint Set the LEDC hpoint value(max: 0xfffff)
+ *
+ */
+esp_err_t ledc_set_duty_and_update(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint);
 
+/**
+ * @brief A thread-safe API to set and start LEDC fade function, with a limited time.
+ * @note  Call ledc_fade_func_install() once, before calling this function.
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
+ * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode,
+ * @param channel LEDC channel index (0-7), select from ledc_channel_t
+ * @param target_duty Target duty of fading.( 0 - (2 ** duty_resolution - 1)))
+ * @param max_fade_time_ms The maximum time of the fading ( ms ).
+ * @param fade_mode choose blocking or non-blocking mode
+ * @return
+ *     - ESP_ERR_INVALID_ARG Parameter error
+ *     - ESP_OK Success
+ *     - ESP_ERR_INVALID_STATE Fade function not installed.
+ *     - ESP_FAIL Fade function init error
+ */
+esp_err_t ledc_set_fade_time_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t max_fade_time_ms, ledc_fade_mode_t fade_mode);
+
+/**
+ * @brief A thread-safe API to set and start LEDC fade function.
+ * @note  Call ledc_fade_func_install() once before calling this function.
+ * @note  If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
+ *        Other duty operations will have to wait until the fade operation has finished.
+ * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode,
+ * @param channel LEDC channel index (0-7), select from ledc_channel_t
+ * @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
+ * @param scale Controls the increase or decrease step scale.
+ * @param cycle_num increase or decrease the duty every cycle_num cycles
+ * @param fade_mode choose blocking or non-blocking mode
+ * @return
+ *     - ESP_ERR_INVALID_ARG Parameter error
+ *     - ESP_OK Success
+ *     - ESP_ERR_INVALID_STATE Fade function not installed.
+ *     - ESP_FAIL Fade function init error
+ */
+esp_err_t ledc_set_fade_step_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num, ledc_fade_mode_t fade_mode);
 #ifdef __cplusplus
 }
 #endif
index 05ab95499ddc420e40d3f4564b150155b294c813..6891877c865f3cca09a6e9e1b826f7e12f670b93 100644 (file)
@@ -51,12 +51,14 @@ typedef struct {
 static ledc_fade_t *s_ledc_fade_rec[LEDC_SPEED_MODE_MAX][LEDC_CHANNEL_MAX];
 static ledc_isr_handle_t s_ledc_fade_isr_handle = NULL;
 
-#define LEDC_VAL_NO_CHANGE   (-1)
-#define LEDC_STEP_NUM_MAX    (1023)
-#define LEDC_DUTY_DECIMAL_BIT_NUM  (4)
-
+#define LEDC_VAL_NO_CHANGE        (-1)
+#define LEDC_STEP_NUM_MAX         (1023)
+#define LEDC_DUTY_DECIMAL_BIT_NUM (4)
+#define LEDC_HPOINT_VAL_MAX       (LEDC_HPOINT_HSCH1_V)
+#define LEDC_FADE_TOO_SLOW_STR    "LEDC FADE TOO SLOW"
+#define LEDC_FADE_TOO_FAST_STR    "LEDC FADE TOO FAST"
 static const char *LEDC_FADE_SERVICE_ERR_STR = "LEDC fade service not installed";
-static const char *LEDC_FADE_INIT_ERROR_STR = "LEDC fade channel init error";
+static const char *LEDC_FADE_INIT_ERROR_STR = "LEDC fade channel init error, not enough memory or service not installed";
 
 static void ledc_ls_timer_update(ledc_mode_t speed_mode, ledc_timer_t timer_sel)
 {
@@ -72,11 +74,72 @@ static IRAM_ATTR void ledc_ls_channel_update(ledc_mode_t speed_mode, ledc_channe
     }
 }
 
+static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, uint32_t channel, ledc_intr_type_t type)
+{
+    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
+    uint32_t value;
+    uint32_t intr_type = type;
+    portENTER_CRITICAL(&ledc_spinlock);
+    value = LEDC.int_ena.val;
+    uint8_t int_en_base = LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S;
+    if (speed_mode == LEDC_LOW_SPEED_MODE) {
+        int_en_base = LEDC_DUTY_CHNG_END_LSCH0_INT_ENA_S;
+    }
+    if (intr_type == LEDC_INTR_FADE_END) {
+        LEDC.int_ena.val = value | BIT(int_en_base + channel);
+    } else {
+        LEDC.int_ena.val = (value & (~(BIT(int_en_base + channel))));
+    }
+    portEXIT_CRITICAL(&ledc_spinlock);
+    return ESP_OK;
+}
+
+static void _ledc_fade_hw_acquire(ledc_mode_t mode, ledc_channel_t channel)
+{
+    ledc_fade_t* fade = s_ledc_fade_rec[mode][channel];
+    if (fade) {
+        xSemaphoreTake(fade->ledc_fade_sem, portMAX_DELAY);
+        ledc_enable_intr_type(mode, channel, LEDC_INTR_DISABLE);
+    }
+}
+
+static void _ledc_fade_hw_release(ledc_mode_t mode, ledc_channel_t channel)
+{
+    ledc_fade_t* fade = s_ledc_fade_rec[mode][channel];
+    if (fade) {
+        xSemaphoreGive(fade->ledc_fade_sem);
+    }
+}
+
+static void _ledc_op_lock_acquire(ledc_mode_t mode, ledc_channel_t channel)
+{
+    ledc_fade_t* fade = s_ledc_fade_rec[mode][channel];
+    if (fade) {
+        xSemaphoreTake(fade->ledc_fade_mux, portMAX_DELAY);
+    }
+}
+
+static void _ledc_op_lock_release(ledc_mode_t mode, ledc_channel_t channel)
+{
+    ledc_fade_t* fade = s_ledc_fade_rec[mode][channel];
+    if (fade) {
+        xSemaphoreGive(fade->ledc_fade_mux);
+    }
+}
+
+static int ledc_get_max_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
+{
+    // The arguments are checked before internally calling this function.
+    int timer_sel = LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel;
+    int max_duty = (1 << (LEDC.timer_group[speed_mode].timer[timer_sel].conf.duty_resolution));
+    return max_duty;
+}
+
 esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t clock_divider, uint32_t duty_resolution,
         ledc_clk_src_t clk_src)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(timer_sel <= LEDC_TIMER_3, "timer_sel")
+    LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
     portENTER_CRITICAL(&ledc_spinlock);
     LEDC.timer_group[speed_mode].timer[timer_sel].conf.clock_divider = clock_divider;
     LEDC.timer_group[speed_mode].timer[timer_sel].conf.tick_sel = clk_src;
@@ -86,14 +149,16 @@ esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_
     return ESP_OK;
 }
 
-static IRAM_ATTR esp_err_t ledc_duty_config(ledc_mode_t speed_mode, ledc_channel_t channel_num, int hpoint_val, uint32_t duty_val,
+static IRAM_ATTR esp_err_t ledc_duty_config(ledc_mode_t speed_mode, ledc_channel_t channel_num, int hpoint_val, int duty_val,
     uint32_t duty_direction, uint32_t duty_num, uint32_t duty_cycle, uint32_t duty_scale)
 {
     portENTER_CRITICAL(&ledc_spinlock);
     if (hpoint_val >= 0) {
-        LEDC.channel_group[speed_mode].channel[channel_num].hpoint.hpoint = hpoint_val;
+        LEDC.channel_group[speed_mode].channel[channel_num].hpoint.hpoint = hpoint_val & LEDC_HPOINT_HSCH1_V;
+    }
+    if (duty_val >= 0) {
+        LEDC.channel_group[speed_mode].channel[channel_num].duty.duty = duty_val;
     }
-    LEDC.channel_group[speed_mode].channel[channel_num].duty.duty = duty_val;
     LEDC.channel_group[speed_mode].channel[channel_num].conf1.val = ((duty_direction & LEDC_DUTY_INC_HSCH0_V) << LEDC_DUTY_INC_HSCH0_S) |
                                                                     ((duty_num & LEDC_DUTY_NUM_HSCH0_V) << LEDC_DUTY_NUM_HSCH0_S) |
                                                                     ((duty_cycle & LEDC_DUTY_CYCLE_HSCH0_V) << LEDC_DUTY_CYCLE_HSCH0_S) |
@@ -106,8 +171,7 @@ static IRAM_ATTR esp_err_t ledc_duty_config(ledc_mode_t speed_mode, ledc_channel
 esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t timer_idx)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(timer_idx <= LEDC_TIMER_3, "timer_idx");
-    portENTER_CRITICAL(&ledc_spinlock);
+    LEDC_ARG_CHECK(timer_idx < LEDC_TIMER_MAX, "timer_select");    portENTER_CRITICAL(&ledc_spinlock);
     LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel = timer_idx;
     ledc_ls_channel_update(speed_mode, channel);
     portEXIT_CRITICAL(&ledc_spinlock);
@@ -117,7 +181,7 @@ esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, ledc_channel_t channel
 esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(timer_sel <= LEDC_TIMER_3, "timer_sel");
+    LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
     portENTER_CRITICAL(&ledc_spinlock);
     LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 1;
     LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 0;
@@ -129,7 +193,7 @@ esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel)
 esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(timer_sel <= LEDC_TIMER_3, "timer_sel");
+    LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
     portENTER_CRITICAL(&ledc_spinlock);
     LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 1;
     ledc_ls_timer_update(speed_mode, timer_sel);
@@ -140,7 +204,7 @@ esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel)
 esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(timer_sel <= LEDC_TIMER_3, "timer_sel");
+    LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
     portENTER_CRITICAL(&ledc_spinlock);
     LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 0;
     ledc_ls_timer_update(speed_mode, timer_sel);
@@ -148,26 +212,6 @@ esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel)
     return ESP_OK;
 }
 
-static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, uint32_t channel, ledc_intr_type_t type)
-{
-    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    uint32_t value;
-    uint32_t intr_type = type;
-    portENTER_CRITICAL(&ledc_spinlock);
-    value = LEDC.int_ena.val;
-    uint8_t int_en_base = LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S;
-    if (speed_mode == LEDC_LOW_SPEED_MODE) {
-        int_en_base = LEDC_DUTY_CHNG_END_LSCH0_INT_ENA_S;
-    }
-    if (intr_type == LEDC_INTR_FADE_END) {
-        LEDC.int_ena.val = value | BIT(int_en_base + channel);
-    } else {
-        LEDC.int_ena.val = (value & (~(BIT(int_en_base + channel))));
-    }
-    portEXIT_CRITICAL(&ledc_spinlock);
-    return ESP_OK;
-}
-
 esp_err_t ledc_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, ledc_isr_handle_t *handle)
 {
     esp_err_t ret;
@@ -180,13 +224,14 @@ esp_err_t ledc_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags,
 
 esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf)
 {
-    int freq_hz = timer_conf->freq_hz;
-    int duty_resolution = timer_conf->duty_resolution;
-    int timer_num = timer_conf->timer_num;
-    int speed_mode = timer_conf->speed_mode;
+    LEDC_ARG_CHECK(timer_conf != NULL, "timer_conf");
+    uint32_t freq_hz = timer_conf->freq_hz;
+    uint32_t duty_resolution = timer_conf->duty_resolution;
+    uint32_t timer_num = timer_conf->timer_num;
+    uint32_t speed_mode = timer_conf->speed_mode;
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
     periph_module_enable(PERIPH_LEDC_MODULE);
-    if (freq_hz == 0 || duty_resolution == 0 || duty_resolution > LEDC_TIMER_15_BIT) {
+    if (freq_hz == 0 || duty_resolution == 0 || duty_resolution >= LEDC_TIMER_BIT_MAX) {
         ESP_LOGE(LEDC_TAG, "freq_hz=%u duty_resolution=%u", freq_hz, duty_resolution);
         return ESP_ERR_INVALID_ARG;
     }
@@ -230,7 +275,7 @@ esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf)
 
 esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc_channel)
 {
-    LEDC_ARG_CHECK(ledc_channel <= LEDC_CHANNEL_7, "ledc_channel");
+    LEDC_ARG_CHECK(ledc_channel < LEDC_CHANNEL_MAX, "ledc_channel");
     LEDC_ARG_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "gpio_num");
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
     PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
@@ -245,22 +290,24 @@ esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc
 
 esp_err_t ledc_channel_config(const ledc_channel_config_t* ledc_conf)
 {
+    LEDC_ARG_CHECK(ledc_conf, "ledc_conf");
     uint32_t speed_mode = ledc_conf->speed_mode;
     uint32_t gpio_num = ledc_conf->gpio_num;
     uint32_t ledc_channel = ledc_conf->channel;
     uint32_t timer_select = ledc_conf->timer_sel;
     uint32_t intr_type = ledc_conf->intr_type;
     uint32_t duty = ledc_conf->duty;
-    LEDC_ARG_CHECK(ledc_channel <= LEDC_CHANNEL_7, "ledc_channel");
+    uint32_t hpoint = ledc_conf->hpoint;
+    LEDC_ARG_CHECK(ledc_channel < LEDC_CHANNEL_MAX, "ledc_channel");
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
     LEDC_ARG_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "gpio_num");
-    LEDC_ARG_CHECK(timer_select <= LEDC_TIMER_3, "timer_select");
+    LEDC_ARG_CHECK(timer_select < LEDC_TIMER_MAX, "timer_select");
     periph_module_enable(PERIPH_LEDC_MODULE);
     esp_err_t ret = ESP_OK;
     /*set channel parameters*/
     /*   channel parameters decide how the waveform looks like in one period*/
-    /*   set channel duty, duty range is (0 ~ ((2 ** duty_resolution) - 1))*/
-    ledc_set_duty(speed_mode, ledc_channel, duty);
+    /*   set channel duty and hpoint value, duty range is (0 ~ ((2 ** duty_resolution) - 1)), max hpoint value is 0xfffff*/
+    ledc_set_duty_with_hpoint(speed_mode, ledc_channel, duty, hpoint);
     /*update duty settings*/
     ledc_update_duty(speed_mode, ledc_channel);
     /*bind the channel with the timer*/
@@ -284,7 +331,7 @@ esp_err_t ledc_channel_config(const ledc_channel_config_t* ledc_conf)
 esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(channel <= LEDC_CHANNEL_7, "channel");
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
     portENTER_CRITICAL(&ledc_spinlock);
     LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 1;
     LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 1;
@@ -296,7 +343,7 @@ esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
 esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(channel <= LEDC_CHANNEL_7, "channel");
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
     portENTER_CRITICAL(&ledc_spinlock);
     LEDC.channel_group[speed_mode].channel[channel].conf0.idle_lv = idle_level & 0x1;
     LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 0;
@@ -310,42 +357,61 @@ esp_err_t ledc_set_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t
     uint32_t step_num, uint32_t duty_cyle_num, uint32_t duty_scale)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(channel <= LEDC_CHANNEL_7, "channel");
-    LEDC_ARG_CHECK(fade_direction <= LEDC_DUTY_DIR_INCREASE, "fade_direction");
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
+    LEDC_ARG_CHECK(fade_direction < LEDC_DUTY_DIR_MAX, "fade_direction");
     LEDC_ARG_CHECK(step_num <= LEDC_DUTY_NUM_HSCH0_V, "step_num");
     LEDC_ARG_CHECK(duty_cyle_num <= LEDC_DUTY_CYCLE_HSCH0_V, "duty_cycle_num");
     LEDC_ARG_CHECK(duty_scale <= LEDC_DUTY_SCALE_HSCH0_V, "duty_scale");
-    if (s_ledc_fade_rec[speed_mode][channel]) {
-        ledc_enable_intr_type(speed_mode, channel, LEDC_INTR_DISABLE);
-    }
+    _ledc_fade_hw_acquire(speed_mode, channel);
     ledc_duty_config(speed_mode,
                      channel,        //uint32_t chan_num,
-                     0,              //uint32_t hpoint_val,
+                     LEDC_VAL_NO_CHANGE,
                      duty << 4,      //uint32_t duty_val,the least 4 bits are decimal part
                      fade_direction, //uint32_t increase,
                      step_num,       //uint32_t duty_num,
                      duty_cyle_num,  //uint32_t duty_cycle,
                      duty_scale      //uint32_t duty_scale
                      );
+    _ledc_fade_hw_release(speed_mode, channel);
+    return ESP_OK;
+}
+
+esp_err_t ledc_set_duty_with_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint)
+{
+    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
+    LEDC_ARG_CHECK(hpoint <= LEDC_HPOINT_VAL_MAX, "hpoint");
+    /* The channel configuration should not be changed before the fade operation is done. */
+    _ledc_fade_hw_acquire(speed_mode, channel);
+    ledc_duty_config(speed_mode,
+                     channel,         //uint32_t chan_num,
+                     hpoint,          //uint32_t hpoint_val,
+                     duty << 4,       //uint32_t duty_val,the least 4 bits are decimal part
+                     1,               //uint32_t increase,
+                     1,               //uint32_t duty_num,
+                     1,               //uint32_t duty_cycle,
+                     0                //uint32_t duty_scale
+                     );
+    _ledc_fade_hw_release(speed_mode, channel);
     return ESP_OK;
 }
 
 esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(channel <= LEDC_CHANNEL_7, "channel");
-    if (s_ledc_fade_rec[speed_mode][channel]) {
-        ledc_enable_intr_type(speed_mode, channel, LEDC_INTR_DISABLE);
-    }
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
+    /* The channel configuration should not be changed before the fade operation is done. */
+    _ledc_fade_hw_acquire(speed_mode, channel);
     ledc_duty_config(speed_mode,
                      channel,         //uint32_t chan_num,
-                     0,               //uint32_t hpoint_val,
+                     LEDC_VAL_NO_CHANGE,
                      duty << 4,       //uint32_t duty_val,the least 4 bits are decimal part
                      1,               //uint32_t increase,
                      1,               //uint32_t duty_num,
                      1,               //uint32_t duty_cycle,
                      0                //uint32_t duty_scale
                      );
+    _ledc_fade_hw_release(speed_mode, channel);
     return ESP_OK;
 }
 
@@ -356,6 +422,14 @@ uint32_t ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
     return duty;
 }
 
+int ledc_get_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel)
+{
+    LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode argument is invalid", LEDC_ERR_VAL);
+    LEDC_CHECK(channel < LEDC_CHANNEL_MAX, "channel argument is invalid", LEDC_ERR_VAL);
+    uint32_t hpoint = LEDC.channel_group[speed_mode].channel[channel].hpoint.hpoint;
+    return hpoint;
+}
+
 esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
@@ -417,17 +491,16 @@ void IRAM_ATTR ledc_fade_isr(void* arg)
             }
             uint32_t duty_cur = LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> LEDC_DUTY_DECIMAL_BIT_NUM;
             if (duty_cur == s_ledc_fade_rec[speed_mode][channel]->target_duty) {
-                if (s_ledc_fade_rec[speed_mode][channel]->mode == LEDC_FADE_WAIT_DONE) {
-                    xSemaphoreGiveFromISR(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem, &HPTaskAwoken);
-                    if (HPTaskAwoken == pdTRUE) {
-                        portYIELD_FROM_ISR();
-                    }
+                xSemaphoreGiveFromISR(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem, &HPTaskAwoken);
+                if (HPTaskAwoken == pdTRUE) {
+                    portYIELD_FROM_ISR();
                 }
                 continue;
             }
             uint32_t duty_tar = s_ledc_fade_rec[speed_mode][channel]->target_duty;
             int scale = s_ledc_fade_rec[speed_mode][channel]->scale;
             if (scale == 0) {
+                xSemaphoreGiveFromISR(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem, &HPTaskAwoken);
                 continue;
             }
             int cycle = s_ledc_fade_rec[speed_mode][channel]->cycle_num;
@@ -479,6 +552,10 @@ static esp_err_t ledc_fade_channel_deinit(ledc_mode_t speed_mode, ledc_channel_t
 
 static esp_err_t ledc_fade_channel_init_check(ledc_mode_t speed_mode, ledc_channel_t channel)
 {
+    if (s_ledc_fade_isr_handle == NULL) {
+        ESP_LOGE(LEDC_TAG, "Fade service not installed, call ledc_fade_func_install");
+        return ESP_FAIL;
+    }
     if (s_ledc_fade_rec[speed_mode][channel] == NULL) {
 #if CONFIG_SPIRAM_USE_MALLOC
         s_ledc_fade_rec[speed_mode][channel] = (ledc_fade_t *) heap_caps_calloc(1, sizeof(ledc_fade_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
@@ -494,6 +571,7 @@ static esp_err_t ledc_fade_channel_init_check(ledc_mode_t speed_mode, ledc_chann
         s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem = xSemaphoreCreateBinary();
 #endif
         s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux = xSemaphoreCreateMutex();
+        xSemaphoreGive(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem);
     }
     if (s_ledc_fade_rec[speed_mode][channel]
         && s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux
@@ -505,94 +583,123 @@ static esp_err_t ledc_fade_channel_init_check(ledc_mode_t speed_mode, ledc_chann
     }
 }
 
-esp_err_t ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int max_fade_time_ms)
+static esp_err_t _ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int scale, int cycle_num)
 {
-    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
-    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
-    LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK, LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
+    portENTER_CRITICAL(&ledc_spinlock);
+    uint32_t duty_cur = LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> LEDC_DUTY_DECIMAL_BIT_NUM;
+    // if duty == max_duty and scale and fade_down == 1, counter would overflow.
+    if (duty_cur == ledc_get_max_duty(speed_mode, channel)) {
+        duty_cur -= 1;
+    }
+    s_ledc_fade_rec[speed_mode][channel]->speed_mode = speed_mode;
+    s_ledc_fade_rec[speed_mode][channel]->target_duty = target_duty;
+    s_ledc_fade_rec[speed_mode][channel]->cycle_num = cycle_num;
+    s_ledc_fade_rec[speed_mode][channel]->scale = scale;
+    int step_num = 0;
+    int dir = LEDC_DUTY_DIR_DECREASE;
+    if (scale > 0) {
+        if (duty_cur > target_duty) {
+            s_ledc_fade_rec[speed_mode][channel]->direction = LEDC_DUTY_DIR_DECREASE;
+            step_num = (duty_cur - target_duty) / scale;
+            step_num = step_num > LEDC_STEP_NUM_MAX ? LEDC_STEP_NUM_MAX : step_num;
+        } else {
+            s_ledc_fade_rec[speed_mode][channel]->direction = LEDC_DUTY_DIR_INCREASE;
+            dir = LEDC_DUTY_DIR_INCREASE;
+            step_num = (target_duty - duty_cur) / scale;
+            step_num = step_num > LEDC_STEP_NUM_MAX ? LEDC_STEP_NUM_MAX : step_num;
+        }
+    }
+    portEXIT_CRITICAL(&ledc_spinlock);
+    if (scale > 0 && step_num > 0) {
+        ledc_duty_config(speed_mode, channel, LEDC_VAL_NO_CHANGE, duty_cur << 4, dir, step_num, cycle_num, scale);
+        ESP_LOGD(LEDC_TAG, "cur duty: %d; target: %d, step: %d, cycle: %d; scale: %d; dir: %d\n",
+                duty_cur, target_duty, step_num, cycle_num, scale, dir);
+    } else {
+        ledc_duty_config(speed_mode, channel, LEDC_VAL_NO_CHANGE, target_duty << 4, dir, 0, 1, 0);
+        ESP_LOGD(LEDC_TAG, "Set to target duty: %d", target_duty);
+    }
+    return ESP_OK;
+}
 
+static esp_err_t _ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int max_fade_time_ms)
+{
     int timer_sel = LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel;
-    uint32_t max_duty = (1 << (LEDC.timer_group[speed_mode].timer[timer_sel].conf.duty_resolution)) - 1;
-    LEDC_ARG_CHECK(target_duty <= max_duty, "target_duty");
-
     uint32_t freq = ledc_get_freq(speed_mode, timer_sel);
     uint32_t duty_cur = LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> LEDC_DUTY_DECIMAL_BIT_NUM;
     uint32_t duty_delta = target_duty > duty_cur ? target_duty - duty_cur : duty_cur - target_duty;
 
     if (duty_delta == 0) {
-        return ESP_OK;
+        return _ledc_set_fade_with_step(speed_mode, channel, target_duty, 0, 0);
     }
     int total_cycles = max_fade_time_ms * freq / 1000;
     if (total_cycles == 0) {
-        return ledc_set_duty(speed_mode, channel, target_duty);
+        ESP_LOGW(LEDC_TAG, LEDC_FADE_TOO_FAST_STR);
+        return _ledc_set_fade_with_step(speed_mode, channel, target_duty, 0, 0);
     }
     int scale, cycle_num;
     if (total_cycles > duty_delta) {
         scale = 1;
         cycle_num = total_cycles / duty_delta;
+        if (cycle_num > LEDC_DUTY_NUM_HSCH0_V) {
+            ESP_LOGW(LEDC_TAG, LEDC_FADE_TOO_SLOW_STR);
+            cycle_num = LEDC_DUTY_NUM_HSCH0_V;
+        }
     } else {
         cycle_num = 1;
-        scale = (duty_delta + total_cycles - 1) / total_cycles;
+        scale = duty_delta / total_cycles;
     }
-    return ledc_set_fade_with_step(speed_mode, channel, target_duty, scale, cycle_num);
+    return _ledc_set_fade_with_step(speed_mode, channel, target_duty, scale, cycle_num);
 }
 
-esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int scale, int cycle_num)
+static void _ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t fade_mode)
+{
+    s_ledc_fade_rec[speed_mode][channel]->mode = fade_mode;
+    // Clear interrupt status of channel
+    int duty_resolution_ch0 = (speed_mode == LEDC_HIGH_SPEED_MODE) ? LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S : LEDC_DUTY_CHNG_END_LSCH0_INT_ENA_S;
+    LEDC.int_clr.val |= BIT(duty_resolution_ch0 + channel);
+    // Enable interrupt for channel
+    ledc_enable_intr_type(speed_mode, channel, LEDC_INTR_FADE_END);
+    ledc_update_duty(speed_mode, channel);
+    if (fade_mode == LEDC_FADE_WAIT_DONE) {
+        xSemaphoreTake(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem, portMAX_DELAY);
+    }
+}
+
+esp_err_t ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int max_fade_time_ms)
 {
     LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
     LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
+    LEDC_ARG_CHECK(target_duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
     LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
-    LEDC_ARG_CHECK(scale > 0, "scale");
-    LEDC_ARG_CHECK(cycle_num > 0, "cycle_num");
 
-    int timer_sel = LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel;
-    uint32_t max_duty = (1 << (LEDC.timer_group[speed_mode].timer[timer_sel].conf.duty_resolution)) - 1;
-    LEDC_ARG_CHECK(target_duty <= max_duty, "target_duty");
-    //disable the interrupt, so the operation will not mess up
-    ledc_enable_intr_type(speed_mode, channel, LEDC_INTR_DISABLE);
+    _ledc_fade_hw_acquire(speed_mode, channel);
+    _ledc_set_fade_with_time(speed_mode, channel, target_duty, max_fade_time_ms);
+    _ledc_fade_hw_release(speed_mode, channel);
+    return ESP_OK;
+}
 
-    portENTER_CRITICAL(&ledc_spinlock);
-    uint32_t duty_cur = LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> LEDC_DUTY_DECIMAL_BIT_NUM;
-    uint32_t duty_delta = target_duty > duty_cur ? target_duty - duty_cur : duty_cur - target_duty;
-    if (duty_delta == 0) {
-        portEXIT_CRITICAL(&ledc_spinlock);
-        return ESP_OK;
-    }
-    s_ledc_fade_rec[speed_mode][channel]->speed_mode = speed_mode;
-    s_ledc_fade_rec[speed_mode][channel]->target_duty = target_duty;
-    s_ledc_fade_rec[speed_mode][channel]->cycle_num = cycle_num;
-    s_ledc_fade_rec[speed_mode][channel]->scale = scale;
-    int step_num;
-    if (duty_cur > target_duty) {
-        s_ledc_fade_rec[speed_mode][channel]->direction = LEDC_DUTY_DIR_DECREASE;
-        step_num = (duty_cur - target_duty) / scale;
-        step_num = step_num > LEDC_STEP_NUM_MAX ? LEDC_STEP_NUM_MAX : step_num;
-    } else {
-        s_ledc_fade_rec[speed_mode][channel]->direction = LEDC_DUTY_DIR_INCREASE;
-        step_num = (target_duty - duty_cur) / scale;
-        step_num = step_num > LEDC_STEP_NUM_MAX ? LEDC_STEP_NUM_MAX : step_num;
-    }
-    portEXIT_CRITICAL(&ledc_spinlock);
+esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num)
+{
+    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
+    LEDC_ARG_CHECK((scale > 0) && (scale <= LEDC_DUTY_SCALE_HSCH0_V), "fade scale");
+    LEDC_ARG_CHECK((cycle_num > 0) && (cycle_num <= LEDC_DUTY_CYCLE_HSCH0_V), "cycle_num");
+    LEDC_ARG_CHECK(target_duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
+    LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
 
-    ledc_set_fade(
-        speed_mode,
-        channel,
-        duty_cur,
-        s_ledc_fade_rec[speed_mode][channel]->direction,
-        step_num,
-        s_ledc_fade_rec[speed_mode][channel]->cycle_num,
-        s_ledc_fade_rec[speed_mode][channel]->scale
-        );
-    ESP_LOGD(LEDC_TAG, "cur duty: %d; target: %d, step: %d, cycle: %d; scale: %d\n",
-        LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> LEDC_DUTY_DECIMAL_BIT_NUM,
-        target_duty,
-        step_num,
-        s_ledc_fade_rec[speed_mode][channel]->cycle_num,
-        s_ledc_fade_rec[speed_mode][channel]->scale
-        );
-    int duty_resolution_ch0 = (speed_mode == LEDC_HIGH_SPEED_MODE) ? LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S : LEDC_DUTY_CHNG_END_LSCH0_INT_ENA_S;
-    LEDC.int_clr.val |= BIT(duty_resolution_ch0 + channel);
-    ledc_enable_intr_type(speed_mode, channel, LEDC_INTR_FADE_END);
+    _ledc_fade_hw_acquire(speed_mode, channel);
+    _ledc_set_fade_with_step(speed_mode, channel, target_duty, scale, cycle_num);
+    _ledc_fade_hw_release(speed_mode, channel);
+    return ESP_OK;
+}
+
+esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t fade_mode)
+{
+    LEDC_CHECK(s_ledc_fade_rec != NULL, LEDC_FADE_SERVICE_ERR_STR, ESP_ERR_INVALID_STATE);
+    LEDC_ARG_CHECK(fade_mode < LEDC_FADE_MAX, "fade_mode");
+    _ledc_fade_hw_acquire(speed_mode, channel);
+    _ledc_fade_start(speed_mode, channel, fade_mode);
+    _ledc_fade_hw_release(speed_mode, channel);
     return ESP_OK;
 }
 
@@ -620,19 +727,66 @@ void ledc_fade_func_uninstall()
     return;
 }
 
-esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t wait_done)
+/*
+ * The functions below are thread-safe version of APIs for duty and fade control.
+ * These APIs can be called from different tasks.
+ */
+esp_err_t ledc_set_duty_and_update(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint)
 {
-    LEDC_CHECK(s_ledc_fade_rec != NULL, LEDC_FADE_SERVICE_ERR_STR, ESP_ERR_INVALID_STATE);
-    LEDC_ARG_CHECK(wait_done < LEDC_FADE_MAX, "wait_done");
-    xSemaphoreTake(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux, portMAX_DELAY);
-    if (wait_done == LEDC_FADE_WAIT_DONE) {
-        s_ledc_fade_rec[speed_mode][channel]->mode = LEDC_FADE_WAIT_DONE;
-        ledc_update_duty(speed_mode, channel);
-        xSemaphoreTake(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem, portMAX_DELAY);
-    } else {
-        s_ledc_fade_rec[speed_mode][channel]->mode = LEDC_FADE_NO_WAIT;
-        ledc_update_duty(speed_mode, channel);
+    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
+    LEDC_ARG_CHECK(duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
+    _ledc_op_lock_acquire(speed_mode, channel);
+    _ledc_fade_hw_acquire(speed_mode, channel);
+    ledc_duty_config(speed_mode,
+                     channel,         //uint32_t chan_num,
+                     hpoint,          //uint32_t hpoint_val,
+                     duty << 4,       //uint32_t duty_val,the least 4 bits are decimal part
+                     1,               //uint32_t increase,
+                     1,               //uint32_t duty_num,
+                     1,               //uint32_t duty_cycle,
+                     0                //uint32_t duty_scale
+                     );
+    ledc_update_duty(speed_mode, channel);
+    _ledc_fade_hw_release(speed_mode, channel);
+    _ledc_op_lock_release(speed_mode, channel);
+    return ESP_OK;
+}
+
+esp_err_t ledc_set_fade_time_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t max_fade_time_ms, ledc_fade_mode_t fade_mode)
+{
+    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
+    LEDC_ARG_CHECK(fade_mode < LEDC_FADE_MAX, "fade_mode");
+    LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
+    LEDC_ARG_CHECK(target_duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
+    _ledc_op_lock_acquire(speed_mode, channel);
+    _ledc_fade_hw_acquire(speed_mode, channel);
+    _ledc_set_fade_with_time(speed_mode, channel, target_duty, max_fade_time_ms);
+    _ledc_fade_start(speed_mode, channel, fade_mode);
+    if (fade_mode == LEDC_FADE_WAIT_DONE) {
+        _ledc_fade_hw_release(speed_mode, channel);
+    }
+    _ledc_op_lock_release(speed_mode, channel);
+    return ESP_OK;
+}
+
+esp_err_t ledc_set_fade_step_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num, ledc_fade_mode_t fade_mode)
+{
+    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
+    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
+    LEDC_ARG_CHECK(fade_mode < LEDC_FADE_MAX, "fade_mode");
+    LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
+    LEDC_ARG_CHECK((scale > 0) && (scale <= LEDC_DUTY_SCALE_HSCH0_V), "fade scale");
+    LEDC_ARG_CHECK((cycle_num > 0) && (cycle_num <= LEDC_DUTY_CYCLE_HSCH0_V), "cycle_num");
+    LEDC_ARG_CHECK(target_duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
+    _ledc_op_lock_acquire(speed_mode, channel);
+    _ledc_fade_hw_acquire(speed_mode, channel);
+    _ledc_set_fade_with_step(speed_mode, channel, target_duty, scale, cycle_num);
+    _ledc_fade_start(speed_mode, channel, fade_mode);
+    if (fade_mode == LEDC_FADE_WAIT_DONE) {
+        _ledc_fade_hw_release(speed_mode, channel);
     }
-    xSemaphoreGive(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux);
+    _ledc_op_lock_release(speed_mode, channel);
     return ESP_OK;
 }