ledc: bugfix - Simplify the procedure to perform a one-time duty update

Avoid adding one extra fade cycle when performing a one-time duty update.
Add some notes to ledc_get_duty and ledc_update_duty APIs, so that users
are aware of when the new duty will be effective.

Closes https://github.com/espressif/esp-idf/issues/7288

(cherry picked from commit e175086226)
This commit is contained in:
songruojing
2021-07-28 19:46:33 +08:00
committed by bot
parent 7d65b17898
commit ad3b9a8002
2 changed files with 11 additions and 6 deletions

View File

@ -61,6 +61,7 @@ esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf);
* @brief LEDC update channel parameters
* @note Call this function to activate the LEDC updated parameters.
* After ledc_set_duty, we need to call this function to update the settings.
* And the new LEDC parameters don't take effect until the next PWM cycle.
* @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
@ -178,6 +179,9 @@ esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t
/**
* @brief LEDC get duty
* This function returns the duty at the present PWM cycle.
* You shouldn't expect the function to return the new duty in the same cycle of calling ledc_update_duty,
* because duty update doesn't take effect until the next cycle.
*
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t

View File

@ -474,8 +474,8 @@ esp_err_t ledc_set_duty_with_hpoint(ledc_mode_t speed_mode, ledc_channel_t chann
hpoint, //uint32_t hpoint_val,
duty, //uint32_t duty_val,
1, //uint32_t increase,
1, //uint32_t duty_num,
1, //uint32_t duty_cycle,
0, //uint32_t duty_num,
0, //uint32_t duty_cycle,
0 //uint32_t duty_scale
);
_ledc_fade_hw_release(speed_mode, channel);
@ -494,8 +494,8 @@ esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t
LEDC_VAL_NO_CHANGE,
duty, //uint32_t duty_val,
1, //uint32_t increase,
1, //uint32_t duty_num,
1, //uint32_t duty_cycle,
0, //uint32_t duty_num,
0, //uint32_t duty_cycle,
0 //uint32_t duty_scale
);
_ledc_fade_hw_release(speed_mode, channel);
@ -844,12 +844,13 @@ esp_err_t ledc_set_duty_and_update(ledc_mode_t speed_mode, ledc_channel_t channe
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_ARG_CHECK(hpoint <= LEDC_HPOINT_VAL_MAX, "hpoint");
LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
_ledc_op_lock_acquire(speed_mode, channel);
_ledc_fade_hw_acquire(speed_mode, channel);
_ledc_set_fade_with_step(speed_mode, channel, duty, 0, 1);
_ledc_fade_start(speed_mode, channel, LEDC_FADE_WAIT_DONE);
ledc_duty_config(speed_mode, channel, hpoint, duty, 1, 0, 0, 0);
ledc_update_duty(speed_mode, channel);
_ledc_fade_hw_release(speed_mode, channel);
_ledc_op_lock_release(speed_mode, channel);
return ESP_OK;