]> granicus.if.org Git - esp-idf/commitdiff
Unit tests: Add FreeRTOS timer tests
authorAngus Gratton <angus@espressif.com>
Tue, 7 Mar 2017 00:31:46 +0000 (11:31 +1100)
committerAngus Gratton <gus@projectgus.com>
Fri, 5 May 2017 04:38:15 +0000 (14:38 +1000)
components/freertos/test/test_timers.c [new file with mode: 0644]
tools/unit-test-app/sdkconfig

diff --git a/components/freertos/test/test_timers.c b/components/freertos/test/test_timers.c
new file mode 100644 (file)
index 0000000..a46cd84
--- /dev/null
@@ -0,0 +1,70 @@
+/* FreeRTOS timer tests
+*/
+#include <stdio.h>
+#include "unity.h"
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "freertos/timers.h"
+
+static void timer_callback(TimerHandle_t timer)
+{
+    volatile int *count;
+    count = (volatile int *)pvTimerGetTimerID( timer );
+    (*count)++;
+    printf("Callback timer %p count %p = %d\n", timer, count, *count);
+}
+
+TEST_CASE("Oneshot FreeRTOS timers", "[freertos]")
+{
+    volatile int count = 0;
+    TimerHandle_t oneshot = xTimerCreate("oneshot", 100 / portTICK_PERIOD_MS, pdFALSE,
+                                         (void *)&count, timer_callback);
+    TEST_ASSERT(oneshot);
+    TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(oneshot));
+    TEST_ASSERT_EQUAL(0, count);
+
+    TEST_ASSERT( xTimerStart(oneshot, 1) );
+    vTaskDelay(2); /* give the timer task a chance to process the message */
+
+    TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(oneshot));
+    TEST_ASSERT_EQUAL(0, count);
+
+    vTaskDelay(250 / portTICK_PERIOD_MS); // 2.5 timer periods
+
+    TEST_ASSERT_EQUAL(1, count);
+    TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(oneshot));
+
+    TEST_ASSERT( xTimerDelete(oneshot, 1) );
+}
+
+
+TEST_CASE("Recurring FreeRTOS timers", "[freertos]")
+{
+    volatile int count = 0;
+    TimerHandle_t recurring = xTimerCreate("oneshot", 100 / portTICK_PERIOD_MS, pdTRUE,
+                                          (void *)&count, timer_callback);
+    TEST_ASSERT(recurring);
+    TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(recurring));
+    TEST_ASSERT_EQUAL(0, count);
+
+    TEST_ASSERT( xTimerStart(recurring, 1) );
+
+    vTaskDelay(2); // let timer task process the queue
+    TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(recurring));
+    TEST_ASSERT_EQUAL(0, count);
+
+    vTaskDelay(250 / portTICK_PERIOD_MS); // 2.5 timer periods
+
+    TEST_ASSERT_EQUAL(2, count);
+    TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(recurring));
+
+    TEST_ASSERT( xTimerStop(recurring, 1) );
+
+    TEST_ASSERT_EQUAL(2, count);
+
+    vTaskDelay(100 / portTICK_PERIOD_MS); // One more timer period
+    TEST_ASSERT_EQUAL(2, count); // hasn't gone up
+    TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(recurring));
+
+    TEST_ASSERT( xTimerDelete(recurring, 1) );
+}
index 467da45da800254f9824c6e63186039fa8418dcd..e4b89734845a4e3724792708fc33756f879c0032 100644 (file)
@@ -144,6 +144,7 @@ CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y
 # CONFIG_ESP32_TIME_SYSCALL_USE_NONE is not set
 CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_RC=y
 # CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL is not set
+CONFIG_ESP32_RTC_CLK_CAL_CYCLES=1024
 CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY=0
 # CONFIG_ESP32_XTAL_FREQ_40 is not set
 # CONFIG_ESP32_XTAL_FREQ_26 is not set