forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimer.ino
133 lines (101 loc) · 2.78 KB
/
timer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* HW Timer test */
#include <unity.h>
#define TIMER_FREQUENCY 5000000
#define TIMER_FREQUENCY_XTAL_CLK 1000
/*
* ESP32 - APB clk only (1kHz not possible)
* C3 - APB + XTAL clk
* S2 - APB + XTAL clk
* S3 - APB + XTAL clk
*/
static hw_timer_t * timer = NULL;
static volatile bool alarm_flag;
/* setUp / tearDown functions are intended to be called before / after each test. */
void setUp(void) {
timer = timerBegin(TIMER_FREQUENCY);
if(timer == NULL){
TEST_FAIL_MESSAGE("Timer init failed in setUp()");
}
timerStop(timer);
timerRestart(timer);
}
void tearDown(void){
timerEnd(timer);
}
void ARDUINO_ISR_ATTR onTimer(){
alarm_flag = true;
}
void timer_interrupt_test(void){
alarm_flag = false;
timerAttachInterrupt(timer, &onTimer);
timerAlarm(timer, (1.2 * TIMER_FREQUENCY), true, 0);
timerStart(timer);
delay(2000);
TEST_ASSERT_EQUAL(true, alarm_flag);
timerStop(timer);
timerRestart(timer);
alarm_flag = false;
timerDetachInterrupt(timer);
timerStart(timer);
delay(2000);
TEST_ASSERT_EQUAL(false, alarm_flag);
}
void timer_divider_test(void){
uint64_t time_val;
uint64_t comp_time_val;
timerStart(timer);
delay(1000);
time_val = timerRead(timer);
// compare divider 16 and 8, value should be double
timerEnd(timer);
timer = timerBegin(2 * TIMER_FREQUENCY);
if(timer == NULL){
TEST_FAIL_MESSAGE("Timer init failed!");
}
timerRestart(timer);
delay(1000);
comp_time_val = timerRead(timer);
TEST_ASSERT_INT_WITHIN(5000, 5000000, time_val);
TEST_ASSERT_INT_WITHIN(10000, 10000000, comp_time_val);
// divider is 256, value should be 2^4
timerEnd(timer);
timer = timerBegin(TIMER_FREQUENCY / 16);
if(timer == NULL){
TEST_FAIL_MESSAGE("Timer init failed!");
}
timerRestart(timer);
delay(1000);
comp_time_val = timerRead(timer);
TEST_ASSERT_INT_WITHIN(5000, 5000000, time_val);
TEST_ASSERT_INT_WITHIN(3125, 312500, comp_time_val);
}
void timer_read_test(void){
uint64_t set_timer_val = 0xFF;
uint64_t get_timer_val = 0;
timerWrite(timer, set_timer_val);
get_timer_val = timerRead(timer);
TEST_ASSERT_EQUAL(set_timer_val, get_timer_val);
}
void timer_clock_select_test(void){
// Set timer frequency that can be achieved using XTAL clock source (autoselected)
timer = timerBegin(TIMER_FREQUENCY_XTAL_CLK);
uint32_t resolution = timerGetFrequency(timer);
TEST_ASSERT_EQUAL(TIMER_FREQUENCY_XTAL_CLK,resolution);
}
void setup(){
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
;
}
UNITY_BEGIN();
RUN_TEST(timer_read_test);
RUN_TEST(timer_interrupt_test);
RUN_TEST(timer_divider_test);
#if !CONFIG_IDF_TARGET_ESP32
RUN_TEST(timer_clock_select_test);
#endif
UNITY_END();
}
void loop(){
}