Skip to content

Commit 2b67a4e

Browse files
authored
HW TEST - Timer (#6754)
* Added HW timer unit test * Adde setUp and tearDown functions
1 parent 722c464 commit 2b67a4e

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

Diff for: tests/timer/test_timer.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_timer(dut):
2+
dut.expect_unity_test_output(timeout=240)

Diff for: tests/timer/timer.ino

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* HW Timer test */
2+
#include <unity.h>
3+
4+
#define TIMER_DIVIDER 16
5+
#define TIMER_SCALE (APB_CLK_FREQ / TIMER_DIVIDER)
6+
7+
hw_timer_t * timer = NULL;
8+
static volatile bool alarm_flag;
9+
10+
/* These functions are intended to be called before and after each test. */
11+
void setUp(void) {
12+
timer = timerBegin(0, TIMER_DIVIDER, true);
13+
timerStop(timer);
14+
timerRestart(timer);
15+
}
16+
17+
void tearDown(void){
18+
timerEnd(timer);
19+
}
20+
21+
22+
23+
void ARDUINO_ISR_ATTR onTimer(){
24+
alarm_flag = true;
25+
}
26+
27+
28+
void timer_interrupt_test(void){
29+
30+
alarm_flag = false;
31+
timerAttachInterrupt(timer, &onTimer, true);
32+
timerAlarmWrite(timer, (1.2 * TIMER_SCALE), true);
33+
timerAlarmEnable(timer);
34+
timerStart(timer);
35+
36+
delay(2000);
37+
38+
TEST_ASSERT_EQUAL(true, alarm_flag);
39+
40+
timerStop(timer);
41+
timerRestart(timer);
42+
alarm_flag = false;
43+
timerAlarmDisable(timer);
44+
timerStart(timer);
45+
46+
delay(2000);
47+
TEST_ASSERT_EQUAL(false, alarm_flag);
48+
}
49+
50+
void timer_divider_test(void){
51+
52+
uint64_t time_val;
53+
uint64_t comp_time_val;
54+
55+
timerStart(timer);
56+
57+
delay(1000);
58+
time_val = timerRead(timer);
59+
60+
// compare divider 16 and 8, value should be double
61+
timerStop(timer);
62+
timerSetDivider(timer,8);
63+
timerRestart(timer);
64+
timerStart(timer);
65+
66+
delay(1000);
67+
comp_time_val = timerRead(timer);
68+
69+
TEST_ASSERT_INT_WITHIN(5000, 5000000, time_val);
70+
TEST_ASSERT_INT_WITHIN(10000, 10000000, comp_time_val);
71+
72+
// divider is 256, value should be 2^4
73+
timerStop(timer);
74+
timerSetDivider(timer,256);
75+
timerRestart(timer);
76+
timerStart(timer);
77+
delay(1000);
78+
comp_time_val = timerRead(timer);
79+
TEST_ASSERT_INT_WITHIN(5000, 5000000, time_val);
80+
TEST_ASSERT_INT_WITHIN(3126, 312500, comp_time_val);
81+
}
82+
83+
void timer_read_test(void){
84+
85+
uint64_t set_timer_val = 0xFF;
86+
uint64_t get_timer_val = 0;
87+
88+
timerWrite(timer,set_timer_val);
89+
get_timer_val = timerRead(timer);
90+
91+
TEST_ASSERT_EQUAL(set_timer_val, get_timer_val);
92+
}
93+
94+
void setup(){
95+
96+
// Open serial communications and wait for port to open:
97+
Serial.begin(115200);
98+
while (!Serial) {
99+
;
100+
}
101+
102+
UNITY_BEGIN();
103+
RUN_TEST(timer_read_test);
104+
RUN_TEST(timer_interrupt_test);
105+
RUN_TEST(timer_divider_test);
106+
UNITY_END();
107+
}
108+
109+
void loop(){
110+
}

0 commit comments

Comments
 (0)