1 /* FreeRTOS timer tests
2 */
3 #include <stdio.h>
4 #include "unity.h"
5 #include "freertos/FreeRTOS.h"
6 #include "freertos/task.h"
7 #include "freertos/timers.h"
8 
timer_callback(TimerHandle_t timer)9 static void timer_callback(TimerHandle_t timer)
10 {
11     volatile int *count;
12     count = (volatile int *)pvTimerGetTimerID( timer );
13     (*count)++;
14     printf("Callback timer %p count %p = %d\n", timer, count, *count);
15 }
16 
17 TEST_CASE("Oneshot FreeRTOS timers", "[freertos]")
18 {
19     volatile int count = 0;
20     TimerHandle_t oneshot = xTimerCreate("oneshot", 100 / portTICK_PERIOD_MS, pdFALSE,
21                                          (void *)&count, timer_callback);
22     TEST_ASSERT(oneshot);
23     TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(oneshot));
24     TEST_ASSERT_EQUAL(0, count);
25 
26     TEST_ASSERT( xTimerStart(oneshot, 1) );
27     vTaskDelay(2); /* give the timer task a chance to process the message */
28 
29     TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(oneshot));
30     TEST_ASSERT_EQUAL(0, count);
31 
32     vTaskDelay(250 / portTICK_PERIOD_MS); // 2.5 timer periods
33 
34     TEST_ASSERT_EQUAL(1, count);
35     TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(oneshot));
36 
37     TEST_ASSERT( xTimerDelete(oneshot, 1) );
38 }
39 
40 
41 TEST_CASE("Recurring FreeRTOS timers", "[freertos]")
42 {
43     volatile int count = 0;
44     TimerHandle_t recurring = xTimerCreate("oneshot", 100 / portTICK_PERIOD_MS, pdTRUE,
45                                           (void *)&count, timer_callback);
46     TEST_ASSERT(recurring);
47     TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(recurring));
48     TEST_ASSERT_EQUAL(0, count);
49 
50     TEST_ASSERT( xTimerStart(recurring, 1) );
51 
52     vTaskDelay(2); // let timer task process the queue
53     TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(recurring));
54     TEST_ASSERT_EQUAL(0, count);
55 
56     vTaskDelay(250 / portTICK_PERIOD_MS); // 2.5 timer periods
57 
58     TEST_ASSERT_EQUAL(2, count);
59     TEST_ASSERT_EQUAL(pdTRUE, xTimerIsTimerActive(recurring));
60 
61     TEST_ASSERT( xTimerStop(recurring, 1) );
62 
63     TEST_ASSERT_EQUAL(2, count);
64 
65     vTaskDelay(100 / portTICK_PERIOD_MS); // One more timer period
66     TEST_ASSERT_EQUAL(2, count); // hasn't gone up
67     TEST_ASSERT_EQUAL(pdFALSE, xTimerIsTimerActive(recurring));
68 
69     TEST_ASSERT( xTimerDelete(recurring, 1) );
70 }
71 
72 TEST_CASE("Static timer creation", "[freertos]")
73 {
74     StaticTimer_t static_timer;
75     TimerHandle_t created_timer;
76     volatile int count = 0;
77 
78     created_timer = xTimerCreateStatic("oneshot", 100 / portTICK_PERIOD_MS,
79                                     pdTRUE,
80                                     (void *)&count,
81                                     timer_callback,
82                                     &static_timer);
83 
84     TEST_ASSERT_NOT_NULL(created_timer);
85 }
86