Use Power Management Locks

From Section 12.1.1, we understand when being clocked by clock sources except REF_TICK, ESP32-C3's LEDC is affected by dynamic FM, and code for acquiring/releasing the power management locks must be added in the application so the LEDC can function properly. Therefore, a power management lock is required in the application to ensure that the APB clock does not change while the LEDC is operating. Specifically:

  • During the LEDC driver initialisation, initialise the ESP_PM_APB_FREQ_MAX power management lock.

  • When the LEDC starts working (light on), acquire the power management lock.

  • And when the LEDC stops working (light off), release the power management lock. The code to enable Wi-Fi modem-sleep mode for the Smart Light project is as follows:

static esp_pm_lock_handle_t s_pm_apb_lock   = NULL;

if (s_pm_apb_lock == NULL) {
    if (esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "l_apb", &s_pm_apb_lock) ! = ESP_OK) {
        ESP_LOGE(TAG, "esp pm lock create failed");
    }
}

while (1) {
    ESP_ERROR_CHECK(esp_pm_lock_acquire(s_pm_apb_lock));
    ESP_LOGI(TAG, "light turn on");
    for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
        ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, LEDC_TEST_DUTY);
        ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
    }
    vTaskDelay(pdMS_TO_TICKS(5 * 1000));

    ESP_LOGI(TAG, "light turn off");
    for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
        ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, 0);
        ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
    }
    ESP_ERROR_CHECK(esp_pm_lock_release(s_pm_apb_lock));
    vTaskDelay(pdMS_TO_TICKS(5 * 1000));
}