diff --git a/components/driver/test/test_timer.c b/components/driver/test/test_timer.c index 5f9d4396e..8001f7e25 100644 --- a/components/driver/test/test_timer.c +++ b/components/driver/test/test_timer.c @@ -1,21 +1,31 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "freertos/queue.h" #include "esp_system.h" #include "unity.h" #include "nvs_flash.h" #include "driver/timer.h" +#include "soc/rtc.h" -#define TIMER_DIVIDER 16 +#define TIMER_DIVIDER 16 #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) /*!< used to calculate counter value */ -#define TIMER_DELTA 0.001 +#define TIMER_DELTA 0.001 static bool alarm_flag; +static xQueueHandle timer_queue; typedef struct { timer_group_t timer_group; timer_idx_t timer_idx; } timer_info_t; +typedef struct { + timer_autoreload_t type; // the type of timer's event + timer_group_t timer_group; + timer_idx_t timer_idx; + uint64_t timer_counter_value; +} timer_event_t; + #define TIMER_INFO_INIT(TG, TID) {.timer_group = (TG), .timer_idx = (TID),} static timer_info_t timer_info[4] = { @@ -27,312 +37,290 @@ static timer_info_t timer_info[4] = { #define GET_TIMER_INFO(TG, TID) (&timer_info[(TG)*2+(TID)]) -// timer group interruption -static void test_timer_group_isr(void *para) +// timer group interruption handle callback +static bool test_timer_group_isr_cb(void *arg) { - timer_info_t* info = (timer_info_t*) para; + bool is_awoken = false; + timer_info_t* info = (timer_info_t*) arg; const timer_group_t timer_group = info->timer_group; const timer_idx_t timer_idx = info->timer_idx; uint64_t timer_val; double time; uint64_t alarm_value; + timer_event_t evt; alarm_flag = true; - if (timer_group_get_auto_reload_in_isr(timer_group, timer_idx)) { + if (timer_group_get_auto_reload_in_isr(timer_group, timer_idx)) { // For autoreload mode, the counter value has been cleared timer_group_clr_intr_status_in_isr(timer_group, timer_idx); ets_printf("This is TG%d timer[%d] reload-timer alarm!\n", timer_group, timer_idx); timer_get_counter_value(timer_group, timer_idx, &timer_val); timer_get_counter_time_sec(timer_group, timer_idx, &time); - ets_printf("time: %.8f S\n", time); + evt.type = TIMER_AUTORELOAD_EN; } else { timer_group_clr_intr_status_in_isr(timer_group, timer_idx); ets_printf("This is TG%d timer[%d] count-up-timer alarm!\n", timer_group, timer_idx); timer_get_counter_value(timer_group, timer_idx, &timer_val); timer_get_counter_time_sec(timer_group, timer_idx, &time); timer_get_alarm_value(timer_group, timer_idx, &alarm_value); - ets_printf("time: %.8f S\n", time); - double alarm_time = (double) alarm_value / TIMER_SCALE; - ets_printf("alarm_time: %.8f S\n", alarm_time); + timer_set_counter_value(timer_group, timer_idx, 0); + evt.type = TIMER_AUTORELOAD_DIS; } + evt.timer_group = timer_group; + evt.timer_idx = timer_idx; + evt.timer_counter_value = timer_val; + if (timer_queue != NULL) { + BaseType_t awoken = pdFALSE; + BaseType_t ret = xQueueSendFromISR(timer_queue, &evt, &awoken); + TEST_ASSERT_EQUAL(pdTRUE, ret); + if (awoken) is_awoken = true; + } + return is_awoken; } -// initialize exact timer group -static void tg_timer_init(int timer_group, int timer_idx, double alarm_time) +// timer group interruption handle +static void test_timer_group_isr(void *arg) { - timer_pause(timer_group, timer_idx); - timer_set_counter_value(timer_group, timer_idx, 0x0); - timer_set_alarm_value(timer_group, timer_idx, alarm_time * TIMER_SCALE); - timer_enable_intr(timer_group, timer_idx); - timer_isr_register(timer_group, timer_idx, test_timer_group_isr, GET_TIMER_INFO(timer_group, timer_idx), ESP_INTR_FLAG_LOWMED, NULL); - timer_start(timer_group, timer_idx); + if (test_timer_group_isr_cb(arg)) { + portYIELD_FROM_ISR(); + } } // initialize all timer -static void all_timer_init(timer_config_t config, bool flag) +static void all_timer_init(timer_config_t *config, bool expect_init) { - esp_err_t ret; - ret = timer_init(TIMER_GROUP_0, TIMER_0, &config); - if (flag) { - TEST_ASSERT(ret == ESP_OK); - } else { - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); + for (uint32_t tg_idx=0; tg_idx> 32), (uint32_t)(evt.timer_counter_value >> 32)); + TEST_ASSERT_UINT32_WITHIN(1000, (uint32_t)(alarm_cnt_val), (uint32_t)(evt.timer_counter_value)); +} + +static void timer_intr_enable_disable_test(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_cnt_val) +{ + alarm_flag = false; + TEST_ESP_OK(timer_set_counter_value(group_num, timer_num, 0)); + TEST_ESP_OK(timer_set_alarm(group_num, timer_num, TIMER_ALARM_EN)); + TEST_ESP_OK(timer_enable_intr(group_num, timer_num)); + TEST_ESP_OK(timer_start(group_num, timer_num)); + timer_isr_check(group_num, timer_num, TIMER_AUTORELOAD_DIS, alarm_cnt_val); + TEST_ASSERT_EQUAL(true, alarm_flag); + + // disable interrupt of tg0_timer0 + alarm_flag = false; + TEST_ESP_OK(timer_pause(group_num, timer_num)); + TEST_ESP_OK(timer_set_counter_value(group_num, timer_num, 0)); + TEST_ESP_OK(timer_disable_intr(group_num, timer_num)); + TEST_ESP_OK(timer_start(group_num, timer_num)); + vTaskDelay(2000 / portTICK_PERIOD_MS); + TEST_ASSERT_EQUAL(false, alarm_flag); } TEST_CASE("Timer init", "[hw_timer]") { - esp_err_t ret; - - // Test init 1:config para - // empty para + // Test init 1:config parameter + // empty parameter timer_config_t config0 = { }; - all_timer_init(config0, false); + all_timer_init(&config0, false); - // only one para + // only one parameter timer_config_t config1 = { - .auto_reload = 1 + .auto_reload = TIMER_AUTORELOAD_EN }; - all_timer_init(config1, false); + all_timer_init(&config1, false); - // lack one para + // lack one parameter timer_config_t config2 = { - .auto_reload = 1, + .auto_reload = TIMER_AUTORELOAD_EN, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_START, .intr_type = TIMER_INTR_LEVEL }; - all_timer_init(config2, true); + all_timer_init(&config2, true); - config2.counter_en = 0; - all_timer_init(config2, true); + config2.counter_en = TIMER_PAUSE; + all_timer_init(&config2, true); - - // error config para + // error config parameter timer_config_t config3 = { - .alarm_en = 3, //error para - .auto_reload = 1, + .alarm_en = 3, //error parameter + .auto_reload = TIMER_AUTORELOAD_EN, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_START, .intr_type = TIMER_INTR_LEVEL }; - all_timer_init(config3, true); + all_timer_init(&config3, true); timer_config_t get_config; - timer_get_config(TIMER_GROUP_1, TIMER_1, &get_config); + TEST_ESP_OK(timer_get_config(TIMER_GROUP_1, TIMER_1, &get_config)); printf("Error config alarm_en is %d\n", get_config.alarm_en); - TEST_ASSERT(config3.alarm_en != get_config.alarm_en); + TEST_ASSERT_NOT_EQUAL(config3.alarm_en, get_config.alarm_en); // Test init 2: init uint64_t set_timer_val = 0x0; timer_config_t config = { - .alarm_en = 0, - .auto_reload = 1, + .alarm_en = TIMER_ALARM_DIS, + .auto_reload = TIMER_AUTORELOAD_EN, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_START, .intr_type = TIMER_INTR_LEVEL }; // judge get config parameters - timer_init(TIMER_GROUP_0, TIMER_0, &config); - timer_get_config(TIMER_GROUP_0, TIMER_0, &get_config); - TEST_ASSERT(config.alarm_en == get_config.alarm_en); - TEST_ASSERT(config.auto_reload == get_config.auto_reload); - TEST_ASSERT(config.counter_dir == get_config.counter_dir); - TEST_ASSERT(config.counter_en == get_config.counter_en); - TEST_ASSERT(config.intr_type == get_config.intr_type); - TEST_ASSERT(config.divider == get_config.divider); + TEST_ESP_OK(timer_init(TIMER_GROUP_0, TIMER_0, &config)); + TEST_ESP_OK(timer_get_config(TIMER_GROUP_0, TIMER_0, &get_config)); + TEST_ASSERT_EQUAL(config.alarm_en, get_config.alarm_en); + TEST_ASSERT_EQUAL(config.auto_reload, get_config.auto_reload); + TEST_ASSERT_EQUAL(config.counter_dir, get_config.counter_dir); + TEST_ASSERT_EQUAL(config.counter_en, get_config.counter_en); + TEST_ASSERT_EQUAL(config.intr_type, get_config.intr_type); + TEST_ASSERT_EQUAL(config.divider, get_config.divider); - all_timer_init(config, true); + all_timer_init(&config, true); all_timer_pause(); all_timer_set_counter_value(set_timer_val); all_timer_start(); all_timer_get_counter_value(set_timer_val, false, NULL); - // Test init 3: wrong para - ret = timer_init(-1, TIMER_1, &config); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_init(TIMER_GROUP_1, 2, &config); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_init(TIMER_GROUP_1, -1, &config); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_init(2, TIMER_1, &config); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - + // Test init 3: wrong parameter + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_init(-1, TIMER_1, &config)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_init(TIMER_GROUP_1, 2, &config)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_init(TIMER_GROUP_1, -1, &config)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_init(2, TIMER_1, &config)); + all_timer_deinit(); } /** @@ -343,16 +331,16 @@ TEST_CASE("Timer init", "[hw_timer]") TEST_CASE("Timer read counter value", "[hw_timer]") { timer_config_t config = { - .alarm_en = 1, - .auto_reload = 1, + .alarm_en = TIMER_ALARM_EN, + .auto_reload = TIMER_AUTORELOAD_EN, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_START, .intr_type = TIMER_INTR_LEVEL }; uint64_t set_timer_val = 0x0; - all_timer_init(config, true); + all_timer_init(&config, true); // Test read value 1: start timer get counter value all_timer_set_counter_value(set_timer_val); @@ -370,42 +358,39 @@ TEST_CASE("Timer read counter value", "[hw_timer]") all_timer_set_counter_value(set_timer_val); all_timer_start(); vTaskDelay(1000 / portTICK_PERIOD_MS); - all_timer_get_counter_time_sec(true, 1); + all_timer_get_counter_time_sec(1); + all_timer_deinit(); } /** * start timer case: * 1. normal start - * 2. error start para + * 2. error start parameter * */ TEST_CASE("Timer start", "[hw_timer]") { - esp_err_t ret; timer_config_t config = { - .alarm_en = 1, - .auto_reload = 1, + .alarm_en = TIMER_ALARM_EN, + .auto_reload = TIMER_AUTORELOAD_EN, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_START, .intr_type = TIMER_INTR_LEVEL }; uint64_t set_timer_val = 0x0; - all_timer_init(config, true); + all_timer_init(&config, true); //Test start 1: normal start all_timer_start(); all_timer_set_counter_value(set_timer_val); all_timer_get_counter_value(set_timer_val, false, NULL); - //Test start 2:wrong para - ret = timer_start(2, TIMER_1); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_start(-1, TIMER_1); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_start(TIMER_GROUP_1, 2); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_start(TIMER_GROUP_1, -1); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); + //Test start 2:wrong parameter + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_start(2, TIMER_1)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_start(-1, TIMER_1)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_start(TIMER_GROUP_1, 2)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_start(TIMER_GROUP_1, -1)); + all_timer_deinit(); } /** @@ -415,48 +400,43 @@ TEST_CASE("Timer start", "[hw_timer]") */ TEST_CASE("Timer pause", "[hw_timer]") { - esp_err_t ret; timer_config_t config = { - .alarm_en = 1, - .auto_reload = 1, + .alarm_en = TIMER_ALARM_EN, + .auto_reload = TIMER_AUTORELOAD_EN, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_START, .intr_type = TIMER_INTR_LEVEL }; uint64_t set_timer_val = 0x0; - all_timer_init(config, true); + all_timer_init(&config, true); - //Test pause 1: right para + //Test pause 1: right parameter all_timer_pause(); all_timer_set_counter_value(set_timer_val); all_timer_get_counter_value(set_timer_val, true, NULL); - //Test pause 2: wrong para - ret = timer_pause(-1, TIMER_0); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_pause(TIMER_GROUP_0, -1); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_pause(2, TIMER_0); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_pause(TIMER_GROUP_1, 2); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); + //Test pause 2: wrong parameter + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_pause(-1, TIMER_0)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_pause(TIMER_GROUP_0, -1)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_pause(2, TIMER_0)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_pause(TIMER_GROUP_1, 2)); + all_timer_deinit(); } // positive mode and negative mode TEST_CASE("Timer counter mode (up / down)", "[hw_timer]") { - esp_err_t ret; timer_config_t config = { - .alarm_en = 1, - .auto_reload = 1, + .alarm_en = TIMER_ALARM_EN, + .auto_reload = TIMER_AUTORELOAD_EN, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_START, .intr_type = TIMER_INTR_LEVEL }; uint64_t set_timer_val = 0x0; - all_timer_init(config, true); + all_timer_init(&config, true); all_timer_pause(); // Test counter mode 1: TIMER_COUNT_UP @@ -464,7 +444,7 @@ TEST_CASE("Timer counter mode (up / down)", "[hw_timer]") all_timer_set_counter_value(set_timer_val); all_timer_start(); vTaskDelay(1000 / portTICK_PERIOD_MS); - all_timer_get_counter_time_sec(true, 1); + all_timer_get_counter_time_sec(1); // Test counter mode 2: TIMER_COUNT_DOWN all_timer_pause(); @@ -473,20 +453,19 @@ TEST_CASE("Timer counter mode (up / down)", "[hw_timer]") all_timer_set_counter_value(set_timer_val); all_timer_start(); vTaskDelay(1000 / portTICK_PERIOD_MS); - all_timer_get_counter_time_sec(true, 2); + all_timer_get_counter_time_sec(2); - // Test counter mode 3 : wrong para - ret = timer_set_counter_mode(TIMER_GROUP_0, TIMER_0, -1); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); - ret = timer_set_counter_mode(TIMER_GROUP_0, TIMER_0, 2); - TEST_ASSERT(ret == ESP_ERR_INVALID_ARG); + // Test counter mode 3 : wrong parameter + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_counter_mode(TIMER_GROUP_0, TIMER_0, -1)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_counter_mode(TIMER_GROUP_0, TIMER_0, 2)); + all_timer_deinit(); } /** * divider case: * 1. different divider, read value * Note: divide 0 = divide max, divide 1 = divide 2 - * 2. error para + * 2. error parameter * * the frequency(timer counts in one sec): * 80M/divider = 800*100000 @@ -496,17 +475,17 @@ TEST_CASE("Timer divider", "[hw_timer]") { int i; timer_config_t config = { - .alarm_en = 1, - .auto_reload = 1, + .alarm_en = TIMER_ALARM_EN, + .auto_reload = TIMER_AUTORELOAD_EN, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_START, .intr_type = TIMER_INTR_LEVEL }; uint64_t set_timer_val = 0; uint64_t time_val[4]; uint64_t comp_time_val[4]; - all_timer_init(config, true); + all_timer_init(&config, true); all_timer_pause(); all_timer_set_counter_value(set_timer_val); @@ -565,16 +544,17 @@ TEST_CASE("Timer divider", "[hw_timer]") // divider is 1 should be equal with 2 all_timer_pause(); - TEST_ASSERT(timer_set_divider(TIMER_GROUP_0, TIMER_0, 1) == ESP_ERR_INVALID_ARG) ; - TEST_ASSERT(timer_set_divider(TIMER_GROUP_1, TIMER_0, 1) == ESP_ERR_INVALID_ARG) ; - TEST_ASSERT(timer_set_divider(TIMER_GROUP_0, TIMER_1, 1) == ESP_ERR_INVALID_ARG) ; - TEST_ASSERT(timer_set_divider(TIMER_GROUP_1, TIMER_1, 1) == ESP_ERR_INVALID_ARG) ; + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_divider(TIMER_GROUP_0, TIMER_0, 1)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_divider(TIMER_GROUP_1, TIMER_0, 1)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_divider(TIMER_GROUP_0, TIMER_1, 1)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_divider(TIMER_GROUP_1, TIMER_1, 1)); all_timer_pause(); - TEST_ASSERT(timer_set_divider(TIMER_GROUP_0, TIMER_0, 65537) == ESP_ERR_INVALID_ARG) ; - TEST_ASSERT(timer_set_divider(TIMER_GROUP_1, TIMER_0, 65537) == ESP_ERR_INVALID_ARG) ; - TEST_ASSERT(timer_set_divider(TIMER_GROUP_0, TIMER_1, 65537) == ESP_ERR_INVALID_ARG) ; - TEST_ASSERT(timer_set_divider(TIMER_GROUP_1, TIMER_1, 65537) == ESP_ERR_INVALID_ARG) ; + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_divider(TIMER_GROUP_0, TIMER_0, 65537)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_divider(TIMER_GROUP_1, TIMER_0, 65537)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_divider(TIMER_GROUP_0, TIMER_1, 65537)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, timer_set_divider(TIMER_GROUP_1, TIMER_1, 65537)); + all_timer_deinit(); } /** @@ -585,41 +565,44 @@ TEST_CASE("Timer divider", "[hw_timer]") TEST_CASE("Timer enable alarm", "[hw_timer]") { timer_config_t config_test = { - .alarm_en = 1, - .auto_reload = 1, + .alarm_en = TIMER_ALARM_DIS, + .auto_reload = TIMER_AUTORELOAD_DIS, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_PAUSE, .intr_type = TIMER_INTR_LEVEL }; - all_timer_init(config_test, true); - - // enable alarm + all_timer_init(&config_test, true); + all_timer_isr_reg(); + + // enable alarm of tg0_timer1 alarm_flag = false; - tg_timer_init(TIMER_GROUP_0, TIMER_1, 1.2); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == true); + TEST_ESP_OK(timer_set_alarm(TIMER_GROUP_0, TIMER_1, TIMER_ALARM_EN)); + timer_intr_enable_and_start(TIMER_GROUP_0, TIMER_1, 1.2); + timer_isr_check(TIMER_GROUP_0, TIMER_1, TIMER_AUTORELOAD_DIS, 1.2 * TIMER_SCALE); + TEST_ASSERT_EQUAL(true, alarm_flag); - // disable alarm + // disable alarm of tg0_timer1 alarm_flag = false; - timer_set_alarm(TIMER_GROUP_0, TIMER_1, TIMER_ALARM_DIS); - tg_timer_init(TIMER_GROUP_0, TIMER_1, 1.2); + TEST_ESP_OK(timer_set_alarm(TIMER_GROUP_0, TIMER_1, TIMER_ALARM_DIS)); + timer_intr_enable_and_start(TIMER_GROUP_0, TIMER_1, 1.2); vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == false); + TEST_ASSERT_EQUAL(false, alarm_flag); - // enable alarm + // enable alarm of tg1_timer0 alarm_flag = false; - timer_set_alarm(TIMER_GROUP_1, TIMER_0, TIMER_ALARM_EN); - tg_timer_init(TIMER_GROUP_1, TIMER_0, 1.2); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == true); + TEST_ESP_OK(timer_set_alarm(TIMER_GROUP_1, TIMER_0, TIMER_ALARM_EN)); + timer_intr_enable_and_start(TIMER_GROUP_1, TIMER_0, 1.2); + timer_isr_check(TIMER_GROUP_1, TIMER_0, TIMER_AUTORELOAD_DIS, 1.2 * TIMER_SCALE); + TEST_ASSERT_EQUAL(true, alarm_flag); - // disable alarm + // disable alarm of tg1_timer0 alarm_flag = false; - timer_set_alarm(TIMER_GROUP_1, TIMER_0, TIMER_ALARM_DIS); - tg_timer_init(TIMER_GROUP_1, TIMER_0, 1.2); + TEST_ESP_OK(timer_set_alarm(TIMER_GROUP_1, TIMER_0, TIMER_ALARM_DIS)); + timer_intr_enable_and_start(TIMER_GROUP_1, TIMER_0, 1.2); vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == false); + TEST_ASSERT_EQUAL(false, alarm_flag); + all_timer_deinit(); } /** @@ -629,37 +612,35 @@ TEST_CASE("Timer enable alarm", "[hw_timer]") */ TEST_CASE("Timer set alarm value", "[hw_timer]") { - esp_err_t ret; int i; uint64_t alarm_val[4]; timer_config_t config = { - .alarm_en = 1, + .alarm_en = TIMER_ALARM_EN, .auto_reload = TIMER_AUTORELOAD_DIS, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 0, + .counter_en = TIMER_PAUSE, .intr_type = TIMER_INTR_LEVEL }; - all_timer_init(config, true); + all_timer_init(&config, true); + all_timer_isr_reg(); // set and get alarm value - all_timer_set_alarm_value(3); - ret = timer_get_alarm_value(TIMER_GROUP_0, TIMER_0, &alarm_val[0]); - TEST_ASSERT(ret == ESP_OK); - ret = timer_get_alarm_value(TIMER_GROUP_0, TIMER_1, &alarm_val[1]); - TEST_ASSERT(ret == ESP_OK); - ret = timer_get_alarm_value(TIMER_GROUP_1, TIMER_0, &alarm_val[2]); - TEST_ASSERT(ret == ESP_OK); - ret = timer_get_alarm_value(TIMER_GROUP_1, TIMER_1, &alarm_val[3]); - TEST_ASSERT(ret == ESP_OK); + all_timer_set_alarm_value(3 * TIMER_SCALE); + TEST_ESP_OK(timer_get_alarm_value(TIMER_GROUP_0, TIMER_0, &alarm_val[0])); + TEST_ESP_OK(timer_get_alarm_value(TIMER_GROUP_0, TIMER_1, &alarm_val[1])); + TEST_ESP_OK(timer_get_alarm_value(TIMER_GROUP_1, TIMER_0, &alarm_val[2])); + TEST_ESP_OK(timer_get_alarm_value(TIMER_GROUP_1, TIMER_1, &alarm_val[3])); for (i = 0; i < 4; i++) { - TEST_ASSERT_EQUAL_UINT32(alarm_val[i] , TIMER_SCALE * 3); + TEST_ASSERT_EQUAL_UINT32(3 * TIMER_SCALE, (uint32_t)alarm_val[i]); } // set interrupt read alarm value - tg_timer_init(TIMER_GROUP_0, TIMER_1, 2.4); - tg_timer_init(TIMER_GROUP_1, TIMER_0, 1.4); - vTaskDelay(3000 / portTICK_PERIOD_MS); + timer_intr_enable_and_start(TIMER_GROUP_0, TIMER_1, 2.4); + timer_isr_check(TIMER_GROUP_0, TIMER_1, TIMER_AUTORELOAD_DIS, 2.4 * TIMER_SCALE); + timer_intr_enable_and_start(TIMER_GROUP_1, TIMER_0, 1.4); + timer_isr_check(TIMER_GROUP_1, TIMER_0, TIMER_AUTORELOAD_DIS, 1.4 * TIMER_SCALE); + all_timer_deinit(); } /** @@ -670,26 +651,30 @@ TEST_CASE("Timer set alarm value", "[hw_timer]") TEST_CASE("Timer auto reload", "[hw_timer]") { timer_config_t config = { - .alarm_en = 1, + .alarm_en = TIMER_ALARM_EN, .auto_reload = TIMER_AUTORELOAD_DIS, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 1, + .counter_en = TIMER_PAUSE, .intr_type = TIMER_INTR_LEVEL }; - all_timer_init(config, true); + all_timer_init(&config, true); + all_timer_isr_reg(); // test disable auto_reload - tg_timer_init(TIMER_GROUP_0, TIMER_0, 1.14); - tg_timer_init(TIMER_GROUP_1, TIMER_1, 1.14); - vTaskDelay(2000 / portTICK_PERIOD_MS); + timer_intr_enable_and_start(TIMER_GROUP_0, TIMER_0, 1.14); + timer_isr_check(TIMER_GROUP_0, TIMER_0, TIMER_AUTORELOAD_DIS, 1.14 * TIMER_SCALE); + timer_intr_enable_and_start(TIMER_GROUP_1, TIMER_1, 1.14); + timer_isr_check(TIMER_GROUP_1, TIMER_1, TIMER_AUTORELOAD_DIS, 1.14 * TIMER_SCALE); //test enable auto_reload - timer_set_auto_reload(TIMER_GROUP_0, TIMER_1, TIMER_AUTORELOAD_EN); - tg_timer_init(TIMER_GROUP_0, TIMER_1, 1.4); - timer_set_auto_reload(TIMER_GROUP_1, TIMER_0, TIMER_AUTORELOAD_EN); - tg_timer_init(TIMER_GROUP_1, TIMER_0, 1.4); - vTaskDelay(2000 / portTICK_PERIOD_MS); + TEST_ESP_OK(timer_set_auto_reload(TIMER_GROUP_0, TIMER_1, TIMER_AUTORELOAD_EN)); + timer_intr_enable_and_start(TIMER_GROUP_0, TIMER_1, 1.4); + timer_isr_check(TIMER_GROUP_0, TIMER_1, TIMER_AUTORELOAD_EN, 0); + TEST_ESP_OK(timer_set_auto_reload(TIMER_GROUP_1, TIMER_0, TIMER_AUTORELOAD_EN)); + timer_intr_enable_and_start(TIMER_GROUP_1, TIMER_0, 1.4); + timer_isr_check(TIMER_GROUP_1, TIMER_0, TIMER_AUTORELOAD_EN, 0); + all_timer_deinit(); } /** @@ -699,65 +684,33 @@ TEST_CASE("Timer auto reload", "[hw_timer]") */ TEST_CASE("Timer enable timer interrupt", "[hw_timer]") { - alarm_flag = false; timer_config_t config = { - .alarm_en = 1, - .auto_reload = TIMER_AUTORELOAD_DIS, + .alarm_en = TIMER_ALARM_DIS, .counter_dir = TIMER_COUNT_UP, + .auto_reload = TIMER_AUTORELOAD_DIS, .divider = TIMER_DIVIDER, .counter_en = TIMER_PAUSE, .intr_type = TIMER_INTR_LEVEL }; - uint64_t set_timer_val = 0x0; - all_timer_init(config, true); + + all_timer_init(&config, true); all_timer_pause(); - all_timer_set_counter_value(set_timer_val); - all_timer_set_alarm_value(1.2); + all_timer_set_alarm_value(1.2 * TIMER_SCALE); + all_timer_set_counter_value(0); + all_timer_isr_reg(); + timer_intr_enable_disable_test(TIMER_GROUP_0, TIMER_0, 1.2 * TIMER_SCALE); + timer_intr_enable_disable_test(TIMER_GROUP_1, TIMER_1, 1.2 * TIMER_SCALE); - // enable timer_intr0 - timer_set_counter_value(TIMER_GROUP_0, TIMER_0, set_timer_val); - timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 1.2 * TIMER_SCALE); - timer_isr_register(TIMER_GROUP_0, TIMER_0, test_timer_group_isr, - GET_TIMER_INFO(TIMER_GROUP_0, TIMER_0), ESP_INTR_FLAG_LOWMED, NULL); - timer_start(TIMER_GROUP_0, TIMER_0); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == true) - - // disable timer_intr0 + // enable interrupt of tg1_timer1 again alarm_flag = false; - timer_set_counter_value(TIMER_GROUP_0, TIMER_0, set_timer_val); - timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 1.2 * TIMER_SCALE); - timer_disable_intr(TIMER_GROUP_0, TIMER_0); - timer_start(TIMER_GROUP_0, TIMER_0); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == false) - - // enable timer_intr1 - timer_set_counter_value(TIMER_GROUP_1, TIMER_1, set_timer_val); - timer_set_alarm_value(TIMER_GROUP_1, TIMER_1, 1.2 * TIMER_SCALE); - timer_isr_register(TIMER_GROUP_1, TIMER_1, test_timer_group_isr, - GET_TIMER_INFO(TIMER_GROUP_1, TIMER_1), ESP_INTR_FLAG_LOWMED, NULL); - timer_start(TIMER_GROUP_1, TIMER_1); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == true) - - // disable timer_intr1 - alarm_flag = false; - timer_set_counter_value(TIMER_GROUP_1, TIMER_1, set_timer_val); - timer_set_alarm_value(TIMER_GROUP_1, TIMER_1, 1.2 * TIMER_SCALE); - timer_disable_intr(TIMER_GROUP_1, TIMER_1); - timer_start(TIMER_GROUP_1, TIMER_1); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == false); - - //enable timer_intr1 again - timer_init(TIMER_GROUP_1, TIMER_1, &config); - timer_set_counter_value(TIMER_GROUP_1, TIMER_1, set_timer_val); - timer_set_alarm_value(TIMER_GROUP_1, TIMER_1, 1.2 * TIMER_SCALE); - timer_enable_intr(TIMER_GROUP_1, TIMER_1); - timer_start(TIMER_GROUP_1, TIMER_1); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == true) + TEST_ESP_OK(timer_pause(TIMER_GROUP_1, TIMER_1)); + TEST_ESP_OK(timer_set_counter_value(TIMER_GROUP_1, TIMER_1, 0)); + TEST_ESP_OK(timer_set_alarm(TIMER_GROUP_1, TIMER_1, TIMER_ALARM_EN)); + TEST_ESP_OK(timer_enable_intr(TIMER_GROUP_1, TIMER_1)); + TEST_ESP_OK(timer_start(TIMER_GROUP_1, TIMER_1)); + timer_isr_check(TIMER_GROUP_1, TIMER_1, TIMER_AUTORELOAD_DIS, 1.2 * TIMER_SCALE); + TEST_ASSERT_EQUAL(true, alarm_flag); + all_timer_deinit(); } /** @@ -769,50 +722,44 @@ TEST_CASE("Timer enable timer group interrupt", "[hw_timer][ignore]") { alarm_flag = false; timer_config_t config = { - .alarm_en = 1, + .alarm_en = TIMER_ALARM_EN, .auto_reload = TIMER_AUTORELOAD_DIS, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, - .counter_en = 0, + .counter_en = TIMER_PAUSE, .intr_type = TIMER_INTR_LEVEL }; uint64_t set_timer_val = 0x0; - all_timer_init(config, true); + all_timer_init(&config, true); all_timer_pause(); all_timer_set_counter_value(set_timer_val); - all_timer_set_alarm_value(1.2); + all_timer_set_alarm_value(1.2 * TIMER_SCALE); - // enable timer group - timer_group_intr_enable(TIMER_GROUP_0, TIMER_INTR_T0); - timer_isr_register(TIMER_GROUP_0, TIMER_0, test_timer_group_isr, GET_TIMER_INFO(TIMER_GROUP_0, TIMER_0), ESP_INTR_FLAG_LOWMED, NULL); - timer_start(TIMER_GROUP_0, TIMER_0); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == true); + // enable interrupt of tg0_timer0 + TEST_ESP_OK(timer_group_intr_enable(TIMER_GROUP_0, TIMER_INTR_T0)); + TEST_ESP_OK(timer_isr_register(TIMER_GROUP_0, TIMER_0, test_timer_group_isr, + GET_TIMER_INFO(TIMER_GROUP_0, TIMER_0), ESP_INTR_FLAG_LOWMED, NULL)); + TEST_ESP_OK(timer_start(TIMER_GROUP_0, TIMER_0)); + timer_isr_check(TIMER_GROUP_0, TIMER_0, TIMER_AUTORELOAD_DIS, 1.2 * TIMER_SCALE); + TEST_ASSERT_EQUAL(true, alarm_flag); - //test enable auto_reload + // disable interrupt of tg0_timer0 alarm_flag = false; - timer_group_intr_disable(TIMER_GROUP_0, TIMER_INTR_T0); - timer_start(TIMER_GROUP_0, TIMER_0); + TEST_ESP_OK(timer_set_counter_value(TIMER_GROUP_0, TIMER_0, set_timer_val)); + TEST_ESP_OK(timer_group_intr_disable(TIMER_GROUP_0, TIMER_INTR_T0)); + TEST_ESP_OK(timer_start(TIMER_GROUP_0, TIMER_0)); vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == false); - - timer_group_intr_enable(TIMER_GROUP_0, TIMER_INTR_T0); - timer_isr_register(TIMER_GROUP_0, TIMER_0, test_timer_group_isr, GET_TIMER_INFO(TIMER_GROUP_0, TIMER_0), ESP_INTR_FLAG_LOWMED, NULL); - timer_start(TIMER_GROUP_0, TIMER_0); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(alarm_flag == true); + TEST_ASSERT_EQUAL(false, alarm_flag); } /** * isr_register case: * Cycle register 15 times, compare the heap size to ensure no memory leaks */ -TEST_CASE("Timer interrupt register", "[hw_timer]") +TEST_CASE("Timer interrupt register", "[hw_timer][leaks=200]") { - int i; - int heap_size = 0; timer_config_t config = { - .alarm_en = 1, + .alarm_en = TIMER_ALARM_DIS, .auto_reload = TIMER_AUTORELOAD_DIS, .counter_dir = TIMER_COUNT_UP, .divider = TIMER_DIVIDER, @@ -820,21 +767,157 @@ TEST_CASE("Timer interrupt register", "[hw_timer]") .intr_type = TIMER_INTR_LEVEL }; - for (i = 0; i < 15; i++) { - all_timer_init(config, true); - tg_timer_init(TIMER_GROUP_0, TIMER_0, 0.54); - tg_timer_init(TIMER_GROUP_1, TIMER_1, 0.34); - - timer_set_auto_reload(TIMER_GROUP_0, TIMER_1, TIMER_AUTORELOAD_EN); - tg_timer_init(TIMER_GROUP_0, TIMER_1, 0.4); - timer_set_auto_reload(TIMER_GROUP_1, TIMER_0, TIMER_AUTORELOAD_EN); - tg_timer_init(TIMER_GROUP_1, TIMER_0, 0.6); - vTaskDelay(1000 / portTICK_PERIOD_MS); - if (heap_size == 0) { - heap_size = esp_get_free_heap_size(); + for (int i = 0; i < 15; i++) { + all_timer_init(&config, true); + timer_isr_handle_t timer_isr_handle[TIMER_GROUP_MAX * TIMER_MAX]; + for (uint32_t tg_idx=0; tg_idx