Skip to content

Commit c6d1c1d

Browse files
committed
Added HW timer unit test
1 parent 384dbc2 commit c6d1c1d

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-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

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
void ARDUINO_ISR_ATTR onTimer(){
11+
//Serial.println("Timer ISR triggered");
12+
alarm_flag = true;
13+
}
14+
15+
16+
void timer_interrupt_test(void){
17+
18+
timer = timerBegin(0, TIMER_DIVIDER, true);
19+
timerStop(timer);
20+
timerRestart(timer);
21+
22+
alarm_flag = false;
23+
timerAttachInterrupt(timer, &onTimer, true);
24+
timerAlarmWrite(timer, (1.2 * TIMER_SCALE), true);
25+
timerAlarmEnable(timer);
26+
timerStart(timer);
27+
28+
delay(2000);
29+
30+
TEST_ASSERT_EQUAL(true, alarm_flag);
31+
32+
timerStop(timer);
33+
timerRestart(timer);
34+
alarm_flag = false;
35+
timerAlarmDisable(timer);
36+
timerStart(timer);
37+
38+
delay(2000);
39+
TEST_ASSERT_EQUAL(false, alarm_flag);
40+
41+
timerEnd(timer);
42+
43+
}
44+
45+
void timer_divider_test(void){
46+
47+
uint64_t time_val;
48+
uint64_t comp_time_val;
49+
50+
timer = timerBegin(0, TIMER_DIVIDER, true);
51+
timerStop(timer);
52+
timerRestart(timer);
53+
timerStart(timer);
54+
55+
delay(1000);
56+
time_val = timerRead(timer);
57+
58+
// compare divider 16 and 8, value should be double
59+
timerStop(timer);
60+
timerSetDivider(timer,8);
61+
timerRestart(timer);
62+
timerStart(timer);
63+
64+
delay(1000);
65+
comp_time_val = timerRead(timer);
66+
67+
TEST_ASSERT_INT_WITHIN(5000, 5000000, time_val);
68+
TEST_ASSERT_INT_WITHIN(10000, 10000000, comp_time_val);
69+
70+
// divider is 256, value should be 2^4
71+
timerStop(timer);
72+
timerSetDivider(timer,256);
73+
timerRestart(timer);
74+
timerStart(timer);
75+
delay(1000);
76+
comp_time_val = timerRead(timer);
77+
TEST_ASSERT_INT_WITHIN(5000, 5000000, time_val);
78+
TEST_ASSERT_INT_WITHIN(3126, 312500, comp_time_val);
79+
80+
timerEnd(timer);
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+
timer = timerBegin(0, TIMER_DIVIDER, true);
89+
timerStop(timer);
90+
91+
timerWrite(timer,set_timer_val);
92+
get_timer_val = timerRead(timer);
93+
94+
TEST_ASSERT_EQUAL(set_timer_val, get_timer_val);
95+
96+
timerEnd(timer);
97+
}
98+
99+
void setup(){
100+
101+
// Open serial communications and wait for port to open:
102+
Serial.begin(115200);
103+
while (!Serial) {
104+
;
105+
}
106+
107+
UNITY_BEGIN();
108+
RUN_TEST(timer_read_test);
109+
RUN_TEST(timer_interrupt_test);
110+
RUN_TEST(timer_divider_test);
111+
UNITY_END();
112+
}
113+
114+
void loop(){
115+
}

0 commit comments

Comments
 (0)