1
1
/* HW Timer test */
2
2
#include < unity.h>
3
3
4
- #define TIMER_DIVIDER 16
5
- #define TIMER_SCALE (APB_CLK_FREQ / TIMER_DIVIDER)
6
-
7
- hw_timer_t * timer = NULL ;
4
+ #define TIMER_FREQUENCY 5000000
5
+ #define TIMER_FREQUENCY_XTAL_CLK 1000
6
+
7
+ /*
8
+ * ESP32 - APB clk only (1kHz not possible)
9
+ * C3 - APB + XTAL clk
10
+ * S2 - APB + XTAL clk
11
+ * S3 - APB + XTAL clk
12
+ */
13
+
14
+ hw_timer_t timer = NULL ;
15
+ hw_timer_t timer_XTAL = NULL ;
8
16
static volatile bool alarm_flag;
9
17
10
18
/* These functions are intended to be called before and after each test. */
11
19
void setUp (void ) {
12
- timer = timerBegin (0 , TIMER_DIVIDER , true );
20
+ timer = timerBegin (TIMER_FREQUENCY , true );
13
21
timerStop (timer);
14
22
timerRestart (timer);
15
23
}
@@ -28,9 +36,8 @@ void ARDUINO_ISR_ATTR onTimer(){
28
36
void timer_interrupt_test (void ){
29
37
30
38
alarm_flag = false ;
31
- timerAttachInterrupt (timer, &onTimer, true );
32
- timerAlarmWrite (timer, (1.2 * TIMER_SCALE), true );
33
- timerAlarmEnable (timer);
39
+ timerAttachInterrupt (timer, &onTimer);
40
+ timerAlarmWrite (timer, (1.2 * TIMER_FREQUENCY), true , 0 );
34
41
timerStart (timer);
35
42
36
43
delay (2000 );
@@ -40,7 +47,7 @@ void timer_interrupt_test(void){
40
47
timerStop (timer);
41
48
timerRestart (timer);
42
49
alarm_flag = false ;
43
- timerAlarmDisable (timer);
50
+ timerDetachInterrupt (timer);
44
51
timerStart (timer);
45
52
46
53
delay (2000 );
@@ -58,10 +65,9 @@ void timer_divider_test(void){
58
65
time_val = timerRead (timer);
59
66
60
67
// compare divider 16 and 8, value should be double
61
- timerStop (timer);
62
- timerSetDivider ( timer, 8 );
68
+ timerEnd (timer);
69
+ timer = timerBegin ( 2 * TIMER_FREQUENCY, true );
63
70
timerRestart (timer);
64
- timerStart (timer);
65
71
66
72
delay (1000 );
67
73
comp_time_val = timerRead (timer);
@@ -70,27 +76,36 @@ void timer_divider_test(void){
70
76
TEST_ASSERT_INT_WITHIN (10000 , 10000000 , comp_time_val);
71
77
72
78
// divider is 256, value should be 2^4
73
- timerStop (timer);
74
- timerSetDivider ( timer, 256 );
79
+ timerEnd (timer);
80
+ timer = timerBegin (TIMER_FREQUENCY / 16 , true );
75
81
timerRestart (timer);
76
- timerStart (timer);
77
82
delay (1000 );
78
83
comp_time_val = timerRead (timer);
79
84
TEST_ASSERT_INT_WITHIN (5000 , 5000000 , time_val);
80
- TEST_ASSERT_INT_WITHIN (3126 , 312500 , comp_time_val);
85
+ TEST_ASSERT_INT_WITHIN (3125 , 312500 , comp_time_val);
81
86
}
82
87
83
88
void timer_read_test (void ){
84
89
85
90
uint64_t set_timer_val = 0xFF ;
86
91
uint64_t get_timer_val = 0 ;
87
92
88
- timerWrite (timer,set_timer_val);
93
+ timerWrite (timer, set_timer_val);
89
94
get_timer_val = timerRead (timer);
90
95
91
96
TEST_ASSERT_EQUAL (set_timer_val, get_timer_val);
92
97
}
93
98
99
+ void timer_clock_select_test (void ){
100
+ // Set timer frequency that can be achieved using XTAL clock source (autoselected)
101
+ timer_XTAL = timerBegin (TIMER_FREQUENCY_XTAL_CLK, true );
102
+
103
+ uint32_t resolution = timerGetResolution (timer_XTAL);
104
+ TEST_ASSERT_EQUAL (TIMER_FREQUENCY_XTAL_CLK,resolution);
105
+
106
+ timerEnd (timer_XTAL);
107
+ }
108
+
94
109
void setup (){
95
110
96
111
// Open serial communications and wait for port to open:
@@ -103,6 +118,9 @@ void setup(){
103
118
RUN_TEST (timer_read_test);
104
119
RUN_TEST (timer_interrupt_test);
105
120
RUN_TEST (timer_divider_test);
121
+ #if !CONFIG_IDF_TARGET_ESP32
122
+ RUN_TEST (timer_clock_select_test);
123
+ #endif
106
124
UNITY_END ();
107
125
}
108
126
0 commit comments