diff --git a/components/driver/include/driver/ledc.h b/components/driver/include/driver/ledc.h index e70f6e25d7..e910b534cc 100644 --- a/components/driver/include/driver/ledc.h +++ b/components/driver/include/driver/ledc.h @@ -116,6 +116,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 @@ -234,6 +235,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 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t diff --git a/components/driver/ledc.c b/components/driver/ledc.c index 5c69bddb58..3847b8ea97 100644 --- a/components/driver/ledc.c +++ b/components/driver/ledc.c @@ -700,8 +700,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); @@ -720,8 +720,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); @@ -1086,12 +1086,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;