1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <cmsis_os.h>
9
10 #define ONESHOT_TIME 1000
11 #define PERIOD 500
12 #define NUM_PERIODS 5
13
14 void Timer1_Callback(void const *arg);
15 void Timer2_Callback(void const *arg);
16
17 osTimerDef(Timer1, Timer1_Callback);
18 osTimerDef(Timer2, Timer2_Callback);
19
20 uint32_t num_oneshots_executed;
21 uint32_t num_periods_executed;
22
Timer1_Callback(void const * arg)23 void Timer1_Callback(void const *arg)
24 {
25 uint32_t Tmr = *(uint32_t *)arg;
26
27 num_oneshots_executed++;
28 TC_PRINT("oneshot_callback (Timer %d) = %d\n",
29 Tmr, num_oneshots_executed);
30 }
31
Timer2_Callback(void const * arg)32 void Timer2_Callback(void const *arg)
33 {
34 uint32_t Tmr = *(uint32_t *)arg;
35
36 num_periods_executed++;
37 TC_PRINT("periodic_callback (Timer %d) = %d\n",
38 Tmr, num_periods_executed);
39 }
40
ZTEST(cmsis_timer,test_timer)41 ZTEST(cmsis_timer, test_timer)
42 {
43 osTimerId id1;
44 osTimerId id2;
45 uint32_t exec1;
46 uint32_t exec2;
47 osStatus status;
48 uint32_t timerDelay;
49
50 /* Create one-shot timer */
51 exec1 = 1U;
52 id1 = osTimerCreate(osTimer(Timer1), osTimerOnce, &exec1);
53 zassert_true(id1 != NULL, "error creating one-shot timer");
54
55 /* Stop the timer before start */
56 status = osTimerStop(id1);
57 zassert_true(status == osErrorResource, "error while stopping non-active timer");
58
59 timerDelay = ONESHOT_TIME;
60 status = osTimerStart(id1, timerDelay);
61 zassert_true(status == osOK, "error starting one-shot timer");
62
63 /* Timer should fire only once if setup in one shot
64 * mode. Wait for 3 times the one-shot time to see
65 * if it fires more than once.
66 */
67 osDelay(timerDelay*3U + 100);
68 zassert_true(num_oneshots_executed == 1U,
69 "error setting up one-shot timer");
70
71 status = osTimerStop(id1);
72 zassert_true(status == osOK, "error stopping one-shot timer");
73
74 status = osTimerDelete(id1);
75 zassert_true(status == osOK, "error deleting one-shot timer");
76
77 /* Create periodic timer */
78 exec2 = 2U;
79 id2 = osTimerCreate(osTimer(Timer2), osTimerPeriodic, &exec2);
80 zassert_true(id2 != NULL, "error creating periodic timer");
81
82 timerDelay = PERIOD;
83 status = osTimerStart(id2, timerDelay);
84 zassert_true(status == osOK, "error starting periodic timer");
85
86 /* Timer should fire periodically if setup in periodic
87 * mode. Wait for NUM_PERIODS periods to see if it is
88 * fired NUM_PERIODS times.
89 */
90 osDelay(timerDelay*NUM_PERIODS + 100);
91
92 zassert_true(num_periods_executed == NUM_PERIODS,
93 "error setting up periodic timer");
94
95 /* Delete the timer before stop */
96 status = osTimerDelete(id2);
97 zassert_true(status == osOK, "error deleting periodic timer");
98 }
99 ZTEST_SUITE(cmsis_timer, NULL, NULL, NULL, NULL, NULL);
100