]> granicus.if.org Git - esp-idf/commitdiff
bugfix(ledc): fix bugs reported from unit-test and github.
authorWangjialin <wangjialin@espressif.com>
Mon, 9 Jul 2018 17:25:31 +0000 (01:25 +0800)
committerWangjialin <wangjialin@espressif.com>
Mon, 9 Jul 2018 18:22:23 +0000 (02:22 +0800)
1. In ledc_set_duty_and_update, return if duty updated. Close https://github.com/espressif/esp-idf/issues/2082
2. fix ledc_set_freq function for low speed mode.

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

index 8f70b1c0033e2a4b83769390930d8f7f35f2f5bf..6a82c19a2f97dc4818a89c55e9209e97118d5dbf 100644 (file)
@@ -455,7 +455,7 @@ void ledc_fade_func_uninstall();
 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
+ * @brief A thread-safe API to set duty for LEDC channel and return when duty updated.
  * @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.
  *
index 6891877c865f3cca09a6e9e1b826f7e12f670b93..c2cb94e59128443325f758ccf4e4ab437b8669f6 100644 (file)
@@ -449,6 +449,7 @@ esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t
         ret = ESP_FAIL;
     }
     LEDC.timer_group[speed_mode].timer[timer_num].conf.clock_divider = clock_divider;
+    ledc_ls_timer_update(speed_mode, timer_num);
     portEXIT_CRITICAL(&ledc_spinlock);
     return ret;
 }
@@ -587,7 +588,7 @@ static esp_err_t _ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t
 {
     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.
+    // When duty == max_duty, meanwhile, if scale == 1 and fade_down == 1, counter would overflow.
     if (duty_cur == ledc_get_max_duty(speed_mode, channel)) {
         duty_cur -= 1;
     }
@@ -609,6 +610,7 @@ static esp_err_t _ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t
             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);
@@ -736,18 +738,16 @@ 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_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
+    uint32_t cur_duty = ledc_get_duty(speed_mode, channel);
+    if (duty == cur_duty) {
+        return ESP_OK;
+    }
     _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);
+    int scale = cur_duty > duty ? cur_duty - duty : duty - cur_duty;
+    _ledc_set_fade_with_step(speed_mode, channel, duty, scale, 1);
+    _ledc_fade_start(speed_mode, channel, LEDC_FADE_WAIT_DONE);
     _ledc_fade_hw_release(speed_mode, channel);
     _ledc_op_lock_release(speed_mode, channel);
     return ESP_OK;