espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
12.93k stars 7.09k forks source link

Periodic timer can't wakeup without esp_sleep_enable_timer_wakeup in Light sleep is it correct? (IDFGH-12889) #13850

Open RJSDevel opened 1 month ago

RJSDevel commented 1 month ago

Answers checklist.

IDF version.

v5.2.1

Espressif SoC revision.

ESP32-H2 (revision v0.1)

Operating System used.

Windows

How did you build your project?

Command line with Make

If you are using Windows, please specify command line type.

None

Development Kit.

KIT

Power Supply used.

USB

What is the expected behavior?

Periodic timer wakeup and run

What is the actual behavior?

Periodic timer doesn't wakeup

Steps to reproduce.

#include "esp_log.h"
#include "esp_check.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#include "esp_sleep.h"

#define DEFAULT_MEASUREMENT_INTERVAL pdMS_TO_TICKS(45 * 1000)
#define DEFAULT_INTERVAL_BETWEEN_DEFAULT_MEASUREMENT_INTERVAL (pdMS_TO_TICKS(1 * 60 * 1000))

static void end_measurement_timer_callback(void* arg)
{
    vTaskDelay(pdMS_TO_TICKS(3000));
// If uncomment than works  
//ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(DEFAULT_INTERVAL_BETWEEN_DEFAULT_MEASUREMENT_INTERVAL)); 
    ESP_ERROR_CHECK(esp_light_sleep_start());
}

static void start_measurement_timer_callback(void* arg) {

ESP_LOGI(TAG, "start_measurement_timer_callback");

    esp_timer_handle_t timer;
    const esp_timer_create_args_t timer_args = {
            .callback = &end_measurement_timer_callback,
            .name = "end_measurement_timer"
    };

    ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer));
    ESP_ERROR_CHECK(esp_timer_start_once(timer, DEFAULT_MEASUREMENT_INTERVAL));

    if (xHandle != NULL) {
        xHandle = NULL;
        vTaskDelete(NULL);
    }
}   

void app_main(void) {
esp_timer_handle_t periodic_timer;
    const esp_timer_create_args_t periodic_timer_args = {
            .callback = &start_measurement_timer_callback,
            .name = "start_measurement_timer"
    };

ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, DEFAULT_INTERVAL_BETWEEN_DEFAULT_MEASUREMENT_INTERVAL));

xTaskCreate(start_measurement_timer_callback, "measurement_task", 4096, NULL, tskIDLE_PRIORITY, &xHandle);
}

Debug Logs.

No response

More Information.

No response

igrr commented 1 month ago

Yes, that's the expected behavior.

esp_timer can automatically wake up the chip from light sleep only when automatic light sleep is used.

When you invoke light sleep yourself using esp_light_sleep_start, you are in control of wake up sources and need to call esp_sleep_enable_timer_wakeup if you would like the chip to wake up after some time.

10086loutianhao commented 1 month ago

@RJSDevel please see our example: examples/system/light_sleep in esp-idf, if we directly call esp_light_sleep_start, we must enable some wakeup sources such as gpio\timer\uart first, but if we use auto_light_sleep, we have called esp_sleep_enable_timer_wakeup in vApplicationSleep, which will be called in idle task. so this is not a bug