LEDC in light sleep, PICO-D4

kuhatovuk
Posts: 21
Joined: Thu Aug 01, 2019 8:46 pm

LEDC in light sleep, PICO-D4

Postby kuhatovuk » Thu Sep 26, 2019 12:07 pm

Hello, is it possible to keep LEDC going in light sleep ? Like LPTIM in STOP mode on STM32. It seems that when I enter light sleep, ESP32-PICO-D4 settles on HIGH or LOW instead of keeping duty cycling. Thank you !

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: LEDC in light sleep, PICO-D4

Postby ESP_igrr » Thu Sep 26, 2019 2:33 pm

No, in light sleep mode all the peripherals are clock gated, including the LEDC.
You may implement some form of PWM using the ULP coprocessor, which will keep running even in light sleep.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: LEDC in light sleep, PICO-D4

Postby WiFive » Thu Sep 26, 2019 7:05 pm

The LED PWM module can use RTC8M_CLK as a clock source when APB_CLK is disabled. In other words,
when the system is in low-power consumption mode (see Power Management Chapter), normal peripherals will
be halted (APB_CLK is turned off), but the LED PWM can work normally via RTC8M_CLK.

kuhatovuk
Posts: 21
Joined: Thu Aug 01, 2019 8:46 pm

Re: LEDC in light sleep, PICO-D4

Postby kuhatovuk » Thu Sep 26, 2019 11:19 pm

Thank you very much ! I noticed v4.0 had provisions for it so I updated and gave the ledc example a midnight try by using :
.clk_cfg = LEDC_USE_RTC8M_CLK,
but it does not continue in light sleep and it seems the frequency might not be correct : if I ask 100Hz, ledc_get_freq() returns 11Hz.

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include "esp_err.h"
#include "esp_pm.h"
#include "esp_sleep.h"
#include "driver/uart.h"
#include "esp32/rom/uart.h"

#define LEDC_LS_TIMER          LEDC_TIMER_0
#define LEDC_LS_MODE           LEDC_LOW_SPEED_MODE
#define LEDC_LS_CH2_GPIO       (19)
#define LEDC_LS_CH2_CHANNEL    LEDC_CHANNEL_2

#define LEDC_TEST_CH_NUM       (1)
#define LEDC_TEST_DUTY         (4000)
#define LEDC_TEST_FADE_TIME    (3000)

void app_main()
{
    int ch;

    ledc_timer_config_t ledc_timer = {
        .duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
        .freq_hz = 100,                      // frequency of PWM signal
        .speed_mode = LEDC_LS_MODE,           // timer mode
        .timer_num = LEDC_LS_TIMER,            // timer index
        .clk_cfg = LEDC_USE_RTC8M_CLK,              // Auto select the source clock
    };
    ledc_timer_config(&ledc_timer);

    ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
        {
            .channel    = LEDC_LS_CH2_CHANNEL,
            .duty       = 0,
            .gpio_num   = LEDC_LS_CH2_GPIO,
            .speed_mode = LEDC_LS_MODE,
            .hpoint     = 0,
            .timer_sel  = LEDC_LS_TIMER
        },
    };

    for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
        ledc_channel_config(&ledc_channel[ch]);
    }

    ledc_fade_func_install(0);

    printf("Frequency %u Hz\n", ledc_get_freq(LEDC_LS_MODE, LEDC_LS_TIMER));

    while (1) {
        printf("1. LEDC fade up to duty = %d\n", LEDC_TEST_DUTY);
        for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
            ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
                    ledc_channel[ch].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
            ledc_fade_start(ledc_channel[ch].speed_mode,
                    ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
        }
        vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);

        printf("Sleep\n");
        esp_sleep_enable_timer_wakeup(10000000);
        esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
        fflush(stdout);
        uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
        esp_light_sleep_start();
    }
}

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: LEDC in light sleep, PICO-D4

Postby WiFive » Fri Sep 27, 2019 5:35 am

This might be a workaround

Code: Select all

rtc_clk_slow_freq_set(RTC_SLOW_FREQ_8MD256);

kuhatovuk
Posts: 21
Joined: Thu Aug 01, 2019 8:46 pm

Re: LEDC in light sleep, PICO-D4

Postby kuhatovuk » Fri Sep 27, 2019 12:24 pm

Thank you very much ! It seems the other issue comes from ledc_get_freq() not handling the possibility of LEDC_USE_RTC8M_CLK yet so I hardcoded it to 100 for now.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: LEDC in light sleep, PICO-D4

Postby WiFive » Fri Sep 27, 2019 10:24 pm

Nice! Suggest that you put these issues and the one from the other thread on GitHub.

Who is online

Users browsing this forum: Google [Bot] and 158 guests