esp_timer: Migrate esp_timer unit tests from unit-test-app to component test app

This commit is contained in:
KonstantinKondrashov
2022-10-24 20:35:32 +08:00
parent c546de8d82
commit 9f41525d99
55 changed files with 134 additions and 46 deletions

View File

@@ -0,0 +1,8 @@
#This is the project CMakeLists.txt file for the test subproject
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components")
set(COMPONENTS main)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp_timer_test)

View File

@@ -0,0 +1,3 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- |

View File

@@ -0,0 +1,4 @@
idf_component_register(SRC_DIRS "."
PRIV_INCLUDE_DIRS "../../private_include"
PRIV_REQUIRES cmock test_utils esp_timer spi_flash
WHOLE_ARCHIVE)

View File

@@ -0,0 +1,14 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "unity.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
void app_main(void)
{
vTaskPrioritySet(NULL, CONFIG_UNITY_FREERTOS_PRIORITY);
unity_run_menu();
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/param.h>
#include "esp_timer.h"
#include "unity.h"
#include "esp_rom_sys.h"
#include "esp_sleep.h"
static void timer_cb1(void *arg)
{
++*((int*) arg);
}
TEST_CASE("Test the periodic timer does not handle lost events during light sleep", "[esp_timer][timeout=20]")
{
int count_calls;
const esp_timer_create_args_t timer_args = {
.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);
int period_cb_ms = 10;
int interval_ms = 50;
TEST_ESP_OK(esp_timer_start_periodic(periodic_timer, period_cb_ms * 1000));
TEST_ESP_OK(esp_sleep_enable_timer_wakeup(interval_ms * 1000));
printf("Run light sleep\n");
printf("count_calls should be around = %d\n", interval_ms / period_cb_ms);
for (int i = 0; i < 3; i++) {
count_calls = 0;
TEST_ESP_OK(esp_light_sleep_start());
esp_rom_delay_us(interval_ms * 1000);
printf("count_calls = %d\n", count_calls);
TEST_ASSERT_INT_WITHIN(2, interval_ms / period_cb_ms, count_calls);
TEST_ESP_OK(esp_timer_dump(stdout));
}
TEST_ESP_OK(esp_timer_stop(periodic_timer));
// times_skipped is about 12 (4 from each sleep time).
TEST_ESP_OK(esp_timer_dump(stdout));
TEST_ESP_OK(esp_timer_delete(periodic_timer));
}

View File

@@ -0,0 +1,263 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
#include <sys/time.h>
#include "unity.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "spi_flash_mmap.h"
#include "esp_rom_sys.h"
#include "esp_private/spi_flash_os.h"
#if CONFIG_IDF_TARGET_ESP32
#include "esp32/rom/ets_sys.h" // for ETSTimer type
#elif CONFIG_IDF_TARGET_ESP32S2
#include "esp32s2/rom/ets_sys.h"
#elif CONFIG_IDF_TARGET_ESP32S3
#include "esp32s3/rom/ets_sys.h"
#elif CONFIG_IDF_TARGET_ESP32C3
#include "esp32c3/rom/ets_sys.h"
#elif CONFIG_IDF_TARGET_ESP32H2
#include "esp32h2/rom/ets_sys.h"
#elif CONFIG_IDF_TARGET_ESP32C2
#include "esp32c2/rom/ets_sys.h"
#elif CONFIG_IDF_TARGET_ESP32C6
#include "esp32c6/rom/ets_sys.h"
#endif
static void test_correct_delay_timer_func(void* arg)
{
struct timeval* ptv = (struct timeval*) arg;
gettimeofday(ptv, NULL);
}
TEST_CASE("ets_timer produces correct delay", "[ets_timer]")
{
ETSTimer timer1 = {0};
const int delays_ms[] = {20, 100, 200, 250};
const size_t delays_count = sizeof(delays_ms) / sizeof(delays_ms[0]);
for (size_t i = 0; i < delays_count; ++i) {
struct timeval tv_end = {0};
ets_timer_setfn(&timer1, &test_correct_delay_timer_func, &tv_end);
struct timeval tv_start;
gettimeofday(&tv_start, NULL);
ets_timer_arm(&timer1, delays_ms[i], false);
vTaskDelay(delays_ms[i] * 2 / portTICK_PERIOD_MS);
int32_t ms_diff = (tv_end.tv_sec - tv_start.tv_sec) * 1000 +
(tv_end.tv_usec - tv_start.tv_usec) / 1000;
printf("%d %"PRIi32"\n", delays_ms[i], ms_diff);
TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, delays_ms[i], ms_diff);
}
ets_timer_disarm(&timer1);
ets_timer_done(&timer1);
}
// no, we can't make this a const size_t (§6.7.5.2)
#define NUM_INTERVALS 16
typedef struct {
ETSTimer *timer;
size_t cur_interval;
int intervals[NUM_INTERVALS];
struct timeval tv_start;
} test_periodic_correct_delays_args_t;
static void test_periodic_correct_delays_timer_func(void* arg)
{
test_periodic_correct_delays_args_t *p_args = (test_periodic_correct_delays_args_t *) arg;
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
int32_t ms_diff = (tv_now.tv_sec - p_args->tv_start.tv_sec) * 1000 +
(tv_now.tv_usec - p_args->tv_start.tv_usec) / 1000;
printf("timer #%d %"PRIi32"ms\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);
if (p_args->cur_interval == NUM_INTERVALS) {
printf("done\n");
ets_timer_disarm(p_args->timer);
}
}
TEST_CASE("periodic ets_timer produces correct delays", "[ets_timer]")
{
const int delay_ms = 100;
ETSTimer timer1 = {0};
test_periodic_correct_delays_args_t args = {0};
args.timer = &timer1;
gettimeofday(&args.tv_start, NULL);
ets_timer_setfn(&timer1, &test_periodic_correct_delays_timer_func, &args);
ets_timer_arm(&timer1, delay_ms, true);
vTaskDelay(delay_ms * (NUM_INTERVALS + 1));
TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
for (size_t i = 0; i < NUM_INTERVALS; ++i) {
TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
}
ets_timer_done(&timer1);
}
#undef NUM_INTERVALS
#define N 5
typedef struct {
const int order[N * 3];
size_t count;
} test_timers_ordered_correctly_common_t;
typedef struct {
int timer_index;
const int intervals[N];
size_t intervals_count;
ETSTimer* timer;
test_timers_ordered_correctly_common_t* common;
bool pass;
SemaphoreHandle_t done;
} test_timers_ordered_correctly_args_t;
static void test_timers_ordered_correctly_timer_func(void* arg)
{
test_timers_ordered_correctly_args_t* p_args = (test_timers_ordered_correctly_args_t*) arg;
// check order
size_t count = p_args->common->count;
int expected_index = p_args->common->order[count];
printf("At count %d, expected timer %d, got timer %d\n",
count, expected_index, p_args->timer_index);
if (expected_index != p_args->timer_index) {
p_args->pass = false;
ets_timer_disarm(p_args->timer);
xSemaphoreGive(p_args->done);
return;
}
p_args->common->count++;
if (++p_args->intervals_count == N) {
ets_timer_disarm(p_args->timer);
xSemaphoreGive(p_args->done);
return;
}
int next_interval = p_args->intervals[p_args->intervals_count];
printf("timer %d interval #%d, %d ms\n",
p_args->timer_index, p_args->intervals_count, next_interval);
ets_timer_arm(p_args->timer, next_interval, false);
}
TEST_CASE("multiple ETSTimers are ordered correctly", "[ets_timer]")
{
ETSTimer timer1;
ETSTimer timer2;
ETSTimer timer3;
test_timers_ordered_correctly_common_t common = {
.order = {1, 2, 3, 2, 1, 3, 1, 2, 1, 3, 2, 1, 3, 3, 2},
.count = 0
};
SemaphoreHandle_t done = xSemaphoreCreateCounting(3, 0);
test_timers_ordered_correctly_args_t args1 = {
.timer_index = 1,
.intervals = {10, 40, 20, 40, 30},
.timer = &timer1,
.common = &common,
.pass = true,
.done = done
};
test_timers_ordered_correctly_args_t args2 = {
.timer_index = 2,
.intervals = {20, 20, 60, 30, 40},
.timer = &timer2,
.common = &common,
.pass = true,
.done = done
};
test_timers_ordered_correctly_args_t args3 = {
.timer_index = 3,
.intervals = {30, 30, 60, 30, 10},
.timer = &timer3,
.common = &common,
.pass = true,
.done = done
};
ets_timer_setfn(&timer1, &test_timers_ordered_correctly_timer_func, &args1);
ets_timer_setfn(&timer2, &test_timers_ordered_correctly_timer_func, &args2);
ets_timer_setfn(&timer3, &test_timers_ordered_correctly_timer_func, &args3);
ets_timer_arm(&timer1, args1.intervals[0], false);
ets_timer_arm(&timer2, args2.intervals[0], false);
ets_timer_arm(&timer3, args3.intervals[0], false);
for (int i = 0; i < 3; ++i) {
int result = xSemaphoreTake(done, 180 / portTICK_PERIOD_MS);
TEST_ASSERT_TRUE(result == pdPASS);
}
TEST_ASSERT_TRUE(args1.pass);
TEST_ASSERT_TRUE(args2.pass);
TEST_ASSERT_TRUE(args3.pass);
ets_timer_done(&timer1);
ets_timer_done(&timer2);
ets_timer_done(&timer3);
}
#undef N
static void IRAM_ATTR test_iram_timer_func(void* arg)
{
volatile bool *b = (volatile bool *)arg;
*b = true;
}
/* WiFi/BT coexistence will sometimes arm/disarm
timers from an ISR where flash may be disabled. */
IRAM_ATTR TEST_CASE("ETSTimers arm & disarm run from IRAM", "[ets_timer]")
{
volatile bool flag = false;
ETSTimer timer1;
const int INTERVAL = 5;
ets_timer_setfn(&timer1, &test_iram_timer_func, (void *)&flag);
/* arm a disabled timer, then disarm a live timer */
spi_flash_guard_get()->start(); // Disables flash cache
ets_timer_arm(&timer1, INTERVAL, false);
// redundant call is deliberate (test code path if already armed)
ets_timer_arm(&timer1, INTERVAL, false);
ets_timer_disarm(&timer1);
spi_flash_guard_get()->end(); // Re-enables flash cache
TEST_ASSERT_FALSE(flag); // didn't expire yet
/* do the same thing but wait for the timer to expire */
spi_flash_guard_get()->start();
ets_timer_arm(&timer1, INTERVAL, false);
spi_flash_guard_get()->end();
vTaskDelay(2 * INTERVAL / portTICK_PERIOD_MS);
TEST_ASSERT_TRUE(flag);
spi_flash_guard_get()->start();
ets_timer_disarm(&timer1);
spi_flash_guard_get()->end();
ets_timer_done(&timer1);
}

View File

@@ -0,0 +1,33 @@
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Unlicense OR CC0-1.0
import pytest
from pytest_embedded import Dut
CONFIGS = [
pytest.param('general', marks=[pytest.mark.supported_targets]),
pytest.param('release', marks=[pytest.mark.supported_targets]),
pytest.param('single_core', marks=[pytest.mark.esp32]),
pytest.param('freertos_compliance', marks=[pytest.mark.esp32]),
pytest.param('isr_dispatch_esp32', marks=[pytest.mark.esp32]),
pytest.param('26mhz_esp32c2', marks=[pytest.mark.esp32c2]),
]
@pytest.mark.generic
@pytest.mark.parametrize('config', CONFIGS, indirect=True)
def test_esp_timer(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('*')
dut.expect_unity_test_output(timeout=240)
@pytest.mark.esp32
@pytest.mark.quad_psram
@pytest.mark.parametrize('config', [
'psram',
], indirect=True)
def test_esp_timer_psram(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('*')
dut.expect_unity_test_output(timeout=240)

View File

@@ -0,0 +1,2 @@
CONFIG_IDF_TARGET="esp32c2"
CONFIG_XTAL_FREQ_26=y

View File

@@ -0,0 +1 @@
CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE=y

View File

@@ -0,0 +1,2 @@
CONFIG_IDF_TARGET="esp32"
CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD=y

View File

@@ -0,0 +1,4 @@
CONFIG_SPIRAM=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=800
CONFIG_SPIRAM_OCCUPY_NO_HOST=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=0

View File

@@ -0,0 +1,3 @@
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

View File

@@ -0,0 +1 @@
CONFIG_FREERTOS_UNICORE=y

View File

@@ -0,0 +1,14 @@
# General options for additional checks
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
CONFIG_COMPILER_WARN_WRITE_STRINGS=y
CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y
CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
CONFIG_COMPILER_STACK_CHECK=y
CONFIG_ESP_TASK_WDT_INIT=n
CONFIG_FREERTOS_HZ=1000
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
CONFIG_ESP_TIMER_PROFILING=y

View File

@@ -0,0 +1 @@
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y

View File

@@ -0,0 +1 @@
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y

View File

@@ -0,0 +1 @@
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y