mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
unit_tests: replace legacy timer group with gptimer
This commit is contained in:
@@ -10,9 +10,10 @@
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/timer.h"
|
||||
#include "driver/gptimer.h"
|
||||
#include "driver/rtc_io.h"
|
||||
#include "soc/rtc.h"
|
||||
#include "esp_private/gptimer.h"
|
||||
#include "soc/rtc_periph.h"
|
||||
#include "esp_rom_sys.h"
|
||||
#include "esp_private/esp_clk.h"
|
||||
@@ -53,7 +54,8 @@ static void switch_freq(int mhz)
|
||||
};
|
||||
ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
|
||||
printf("Waiting for frequency to be set to %d MHz...\n", mhz);
|
||||
while (esp_clk_cpu_freq() / MHZ != mhz) {
|
||||
while (esp_clk_cpu_freq() / MHZ != mhz)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
printf("Frequency is %d MHz\n", esp_clk_cpu_freq() / MHZ);
|
||||
}
|
||||
@@ -70,7 +72,7 @@ TEST_CASE("Can switch frequency using esp_pm_configure", "[pm]")
|
||||
{
|
||||
int orig_freq_mhz = esp_clk_cpu_freq() / MHZ;
|
||||
|
||||
for (int i = 0; i < sizeof(test_freqs)/sizeof(int); i++) {
|
||||
for (int i = 0; i < sizeof(test_freqs) / sizeof(int); i++) {
|
||||
switch_freq(test_freqs[i]);
|
||||
}
|
||||
|
||||
@@ -125,16 +127,23 @@ static void light_sleep_disable(void)
|
||||
|
||||
TEST_CASE("Automatic light occurs when tasks are suspended", "[pm]")
|
||||
{
|
||||
/* To figure out if light sleep takes place, use Timer Group timer.
|
||||
gptimer_handle_t gptimer = NULL;
|
||||
/* To figure out if light sleep takes place, use GPTimer
|
||||
* It will stop working while in light sleep.
|
||||
*/
|
||||
timer_config_t config = {
|
||||
.counter_dir = TIMER_COUNT_UP,
|
||||
.divider = 80 /* 1 us per tick */
|
||||
gptimer_config_t config = {
|
||||
.clk_src = GPTIMER_CLK_SRC_APB,
|
||||
.direction = GPTIMER_COUNT_UP,
|
||||
.resolution_hz = 1000000, /* 1 us per tick */
|
||||
};
|
||||
timer_init(TIMER_GROUP_0, TIMER_0, &config);
|
||||
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
|
||||
timer_start(TIMER_GROUP_0, TIMER_0);
|
||||
TEST_ESP_OK(gptimer_new_timer(&config, &gptimer));
|
||||
TEST_ESP_OK(gptimer_start(gptimer));
|
||||
// if GPTimer is clocked from APB, when PM is enabled, the driver will acquire the PM lock
|
||||
// causing the auto light sleep doesn't take effect
|
||||
// so we manually release the lock here
|
||||
esp_pm_lock_handle_t gptimer_pm_lock;
|
||||
TEST_ESP_OK(gptimer_get_pm_lock(gptimer, &gptimer_pm_lock));
|
||||
TEST_ESP_OK(esp_pm_lock_release(gptimer_pm_lock));
|
||||
|
||||
light_sleep_enable();
|
||||
|
||||
@@ -147,11 +156,10 @@ TEST_CASE("Automatic light occurs when tasks are suspended", "[pm]")
|
||||
|
||||
/* The following delay should cause light sleep to start */
|
||||
uint64_t count_start;
|
||||
timer_get_counter_value(TIMER_GROUP_0, TIMER_0, &count_start);
|
||||
vTaskDelay(ticks_to_delay);
|
||||
uint64_t count_end;
|
||||
timer_get_counter_value(TIMER_GROUP_0, TIMER_0, &count_end);
|
||||
|
||||
TEST_ESP_OK(gptimer_get_raw_count(gptimer, &count_start));
|
||||
vTaskDelay(ticks_to_delay);
|
||||
TEST_ESP_OK(gptimer_get_raw_count(gptimer, &count_end));
|
||||
|
||||
int timer_diff_us = (int) (count_end - count_start);
|
||||
const int us_per_tick = 1 * portTICK_PERIOD_MS * 1000;
|
||||
@@ -161,6 +169,9 @@ TEST_CASE("Automatic light occurs when tasks are suspended", "[pm]")
|
||||
}
|
||||
|
||||
light_sleep_disable();
|
||||
TEST_ESP_OK(esp_pm_lock_acquire(gptimer_pm_lock));
|
||||
TEST_ESP_OK(gptimer_stop(gptimer));
|
||||
TEST_ESP_OK(gptimer_del_timer(gptimer));
|
||||
}
|
||||
|
||||
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
|
||||
@@ -185,22 +196,22 @@ TEST_CASE("Can wake up from automatic light sleep by GPIO", "[pm][ignore]")
|
||||
rtc_gpio_set_direction(ext1_wakeup_gpio, RTC_GPIO_MODE_INPUT_OUTPUT);
|
||||
rtc_gpio_set_level(ext1_wakeup_gpio, 0);
|
||||
|
||||
/* Enable wakeup */
|
||||
/* Enable wakeup */
|
||||
TEST_ESP_OK(esp_sleep_enable_ext1_wakeup(1ULL << ext1_wakeup_gpio, ESP_EXT1_WAKEUP_ANY_HIGH));
|
||||
|
||||
/* To simplify test environment, we'll use a ULP program to set GPIO high */
|
||||
ulp_insn_t ulp_code[] = {
|
||||
I_DELAY(65535), /* about 8ms, given 8MHz ULP clock */
|
||||
I_WR_REG_BIT(RTC_CNTL_HOLD_FORCE_REG, RTC_CNTL_PDAC1_HOLD_FORCE_S, 0),
|
||||
I_WR_REG_BIT(RTC_GPIO_OUT_REG, ext_rtc_io + RTC_GPIO_OUT_DATA_S, 1),
|
||||
I_DELAY(1000),
|
||||
I_WR_REG_BIT(RTC_GPIO_OUT_REG, ext_rtc_io + RTC_GPIO_OUT_DATA_S, 0),
|
||||
I_WR_REG_BIT(RTC_CNTL_HOLD_FORCE_REG, RTC_CNTL_PDAC1_HOLD_FORCE_S, 1),
|
||||
I_END(),
|
||||
I_HALT()
|
||||
I_DELAY(65535), /* about 8ms, given 8MHz ULP clock */
|
||||
I_WR_REG_BIT(RTC_CNTL_HOLD_FORCE_REG, RTC_CNTL_PDAC1_HOLD_FORCE_S, 0),
|
||||
I_WR_REG_BIT(RTC_GPIO_OUT_REG, ext_rtc_io + RTC_GPIO_OUT_DATA_S, 1),
|
||||
I_DELAY(1000),
|
||||
I_WR_REG_BIT(RTC_GPIO_OUT_REG, ext_rtc_io + RTC_GPIO_OUT_DATA_S, 0),
|
||||
I_WR_REG_BIT(RTC_CNTL_HOLD_FORCE_REG, RTC_CNTL_PDAC1_HOLD_FORCE_S, 1),
|
||||
I_END(),
|
||||
I_HALT()
|
||||
};
|
||||
TEST_ESP_OK(ulp_set_wakeup_period(0, 1000 /* us */));
|
||||
size_t size = sizeof(ulp_code)/sizeof(ulp_insn_t);
|
||||
size_t size = sizeof(ulp_code) / sizeof(ulp_insn_t);
|
||||
TEST_ESP_OK(ulp_process_macros_and_load(0, ulp_code, &size));
|
||||
|
||||
light_sleep_enable();
|
||||
@@ -249,9 +260,9 @@ typedef struct {
|
||||
SemaphoreHandle_t done;
|
||||
} delay_test_arg_t;
|
||||
|
||||
static void test_delay_task(void* p)
|
||||
static void test_delay_task(void *p)
|
||||
{
|
||||
delay_test_arg_t* arg = (delay_test_arg_t*) p;
|
||||
delay_test_arg_t *arg = (delay_test_arg_t *) p;
|
||||
vTaskDelay(1);
|
||||
|
||||
uint64_t start = esp_clk_rtc_time();
|
||||
@@ -266,9 +277,11 @@ static void test_delay_task(void* p)
|
||||
TEST_CASE("vTaskDelay duration is correct with light sleep enabled", "[pm]")
|
||||
{
|
||||
light_sleep_enable();
|
||||
SemaphoreHandle_t done_sem = xSemaphoreCreateBinary();
|
||||
TEST_ASSERT_NOT_NULL(done_sem);
|
||||
|
||||
delay_test_arg_t args = {
|
||||
.done = xSemaphoreCreateBinary()
|
||||
.done = done_sem,
|
||||
};
|
||||
|
||||
const int delays[] = { 10, 20, 50, 100, 150, 200, 250 };
|
||||
@@ -278,19 +291,19 @@ TEST_CASE("vTaskDelay duration is correct with light sleep enabled", "[pm]")
|
||||
int delay_ms = delays[i];
|
||||
args.delay_us = delay_ms * 1000;
|
||||
|
||||
xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 0);
|
||||
TEST_ASSERT( xSemaphoreTake(args.done, delay_ms * 10 / portTICK_PERIOD_MS) );
|
||||
xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void *) &args, 3, NULL, 0);
|
||||
TEST_ASSERT( xSemaphoreTake(done_sem, delay_ms * 10 / portTICK_PERIOD_MS) );
|
||||
printf("CPU0: %d %d\n", args.delay_us, args.result);
|
||||
TEST_ASSERT_INT32_WITHIN(1000 * portTICK_PERIOD_MS * 2, args.delay_us, args.result);
|
||||
|
||||
#if portNUM_PROCESSORS == 2
|
||||
xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 1);
|
||||
TEST_ASSERT( xSemaphoreTake(args.done, delay_ms * 10 / portTICK_PERIOD_MS) );
|
||||
xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void *) &args, 3, NULL, 1);
|
||||
TEST_ASSERT( xSemaphoreTake(done_sem, delay_ms * 10 / portTICK_PERIOD_MS) );
|
||||
printf("CPU1: %d %d\n", args.delay_us, args.result);
|
||||
TEST_ASSERT_INT32_WITHIN(1000 * portTICK_PERIOD_MS * 2, args.delay_us, args.result);
|
||||
#endif
|
||||
}
|
||||
vSemaphoreDelete(args.done);
|
||||
vSemaphoreDelete(done_sem);
|
||||
|
||||
light_sleep_disable();
|
||||
}
|
||||
@@ -312,16 +325,15 @@ TEST_CASE("esp_timer produces correct delays with light sleep", "[pm]")
|
||||
SemaphoreHandle_t done;
|
||||
} test_args_t;
|
||||
|
||||
void timer_func(void* arg)
|
||||
{
|
||||
test_args_t* p_args = (test_args_t*) arg;
|
||||
void timer_func(void *arg) {
|
||||
test_args_t *p_args = (test_args_t *) arg;
|
||||
int64_t t_end = esp_clk_rtc_time();
|
||||
int32_t ms_diff = (t_end - p_args->t_start) / 1000;
|
||||
printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
|
||||
p_args->intervals[p_args->cur_interval++] = ms_diff;
|
||||
// Deliberately make timer handler run longer.
|
||||
// We check that this doesn't affect the result.
|
||||
esp_rom_delay_us(10*1000);
|
||||
esp_rom_delay_us(10 * 1000);
|
||||
if (p_args->cur_interval == NUM_INTERVALS) {
|
||||
printf("done\n");
|
||||
TEST_ESP_OK(esp_timer_stop(p_args->timer));
|
||||
@@ -335,9 +347,9 @@ TEST_CASE("esp_timer produces correct delays with light sleep", "[pm]")
|
||||
test_args_t args = {0};
|
||||
esp_timer_handle_t timer1;
|
||||
esp_timer_create_args_t create_args = {
|
||||
.callback = &timer_func,
|
||||
.arg = &args,
|
||||
.name = "timer1",
|
||||
.callback = timer_func,
|
||||
.arg = &args,
|
||||
.name = "timer1",
|
||||
};
|
||||
TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
|
||||
|
||||
@@ -365,7 +377,7 @@ TEST_CASE("esp_timer produces correct delays with light sleep", "[pm]")
|
||||
|
||||
static void timer_cb1(void *arg)
|
||||
{
|
||||
++*((int*) arg);
|
||||
++*((int *) arg);
|
||||
}
|
||||
|
||||
TEST_CASE("esp_timer with SKIP_UNHANDLED_EVENTS does not wake up CPU from sleep", "[pm]")
|
||||
@@ -374,10 +386,10 @@ TEST_CASE("esp_timer with SKIP_UNHANDLED_EVENTS does not wake up CPU from sleep"
|
||||
int timer_interval_ms = 50;
|
||||
|
||||
const esp_timer_create_args_t timer_args = {
|
||||
.name = "timer_cb1",
|
||||
.arg = &count_calls,
|
||||
.callback = &timer_cb1,
|
||||
.skip_unhandled_events = true,
|
||||
.name = "timer_cb1",
|
||||
.arg = &count_calls,
|
||||
.callback = &timer_cb1,
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
esp_timer_handle_t periodic_timer;
|
||||
esp_timer_create(&timer_args, &periodic_timer);
|
||||
|
Reference in New Issue
Block a user