Skip to content

Commit ca6013e

Browse files
committed
Updated timer HW test
1 parent 15a68e6 commit ca6013e

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

tests/timer/timer.ino

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
/* HW Timer test */
22
#include <unity.h>
33

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;
816
static volatile bool alarm_flag;
917

1018
/* These functions are intended to be called before and after each test. */
1119
void setUp(void) {
12-
timer = timerBegin(0, TIMER_DIVIDER, true);
20+
timer = timerBegin(TIMER_FREQUENCY, true);
1321
timerStop(timer);
1422
timerRestart(timer);
1523
}
@@ -28,9 +36,8 @@ void ARDUINO_ISR_ATTR onTimer(){
2836
void timer_interrupt_test(void){
2937

3038
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);
3441
timerStart(timer);
3542

3643
delay(2000);
@@ -40,7 +47,7 @@ void timer_interrupt_test(void){
4047
timerStop(timer);
4148
timerRestart(timer);
4249
alarm_flag = false;
43-
timerAlarmDisable(timer);
50+
timerDetachInterrupt(timer);
4451
timerStart(timer);
4552

4653
delay(2000);
@@ -58,10 +65,9 @@ void timer_divider_test(void){
5865
time_val = timerRead(timer);
5966

6067
// 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);
6370
timerRestart(timer);
64-
timerStart(timer);
6571

6672
delay(1000);
6773
comp_time_val = timerRead(timer);
@@ -70,27 +76,36 @@ void timer_divider_test(void){
7076
TEST_ASSERT_INT_WITHIN(10000, 10000000, comp_time_val);
7177

7278
// 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);
7581
timerRestart(timer);
76-
timerStart(timer);
7782
delay(1000);
7883
comp_time_val = timerRead(timer);
7984
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);
8186
}
8287

8388
void timer_read_test(void){
8489

8590
uint64_t set_timer_val = 0xFF;
8691
uint64_t get_timer_val = 0;
8792

88-
timerWrite(timer,set_timer_val);
93+
timerWrite(timer, set_timer_val);
8994
get_timer_val = timerRead(timer);
9095

9196
TEST_ASSERT_EQUAL(set_timer_val, get_timer_val);
9297
}
9398

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+
94109
void setup(){
95110

96111
// Open serial communications and wait for port to open:
@@ -103,6 +118,9 @@ void setup(){
103118
RUN_TEST(timer_read_test);
104119
RUN_TEST(timer_interrupt_test);
105120
RUN_TEST(timer_divider_test);
121+
#if !CONFIG_IDF_TARGET_ESP32
122+
RUN_TEST(timer_clock_select_test);
123+
#endif
106124
UNITY_END();
107125
}
108126

0 commit comments

Comments
 (0)