Merge branch 'feature/ledc_add_api_set_hpoint_val' into 'master'
driver(ledc): fix duty and fade issues and add thread-safe version APIs. See merge request idf/esp-idf!2233
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user