Skip to content

Commit 3a282f6

Browse files
authored
TIMER - add timer_started flag, fix timerEnd() + timer HW test (#8135)
* Add timer_started flag and stop before disable * Fix timer HW test
1 parent 10ab130 commit 3a282f6

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

Diff for: cores/esp32/esp32-hal-timer.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef struct {
3232
struct timer_struct_t {
3333
gptimer_handle_t timer_handle;
3434
interrupt_config_t interrupt_handle;
35+
bool timer_started;
3536
};
3637

3738
inline uint64_t timerRead(hw_timer_t * timer){
@@ -66,10 +67,12 @@ uint32_t timerGetFrequency(hw_timer_t * timer){
6667

6768
void timerStart(hw_timer_t * timer){
6869
gptimer_start(timer->timer_handle);
70+
timer->timer_started = true;
6971
}
7072

7173
void timerStop(hw_timer_t * timer){
7274
gptimer_stop(timer->timer_handle);
75+
timer->timer_started = false;
7376
}
7477

7578
void timerRestart(hw_timer_t * timer){
@@ -119,11 +122,15 @@ hw_timer_t * timerBegin(uint32_t frequency){
119122
}
120123
gptimer_enable(timer->timer_handle);
121124
gptimer_start(timer->timer_handle);
125+
timer->timer_started = true;
122126
return timer;
123127
}
124128

125129
void timerEnd(hw_timer_t * timer){
126130
esp_err_t err = ESP_OK;
131+
if(timer->timer_started == true){
132+
gptimer_stop(timer->timer_handle);
133+
}
127134
gptimer_disable(timer->timer_handle);
128135
err = gptimer_del_timer(timer->timer_handle);
129136
if (err != ESP_OK){
@@ -155,14 +162,20 @@ void timerAttachInterruptFunctionalArg(hw_timer_t * timer, void (*userFunc)(void
155162
timer->interrupt_handle.fn = (voidFuncPtr)userFunc;
156163
timer->interrupt_handle.arg = arg;
157164

165+
if(timer->timer_started == true){
166+
gptimer_stop(timer->timer_handle);
167+
}
158168
gptimer_disable(timer->timer_handle);
159169
err = gptimer_register_event_callbacks(timer->timer_handle, &cbs, &timer->interrupt_handle);
160170
if (err != ESP_OK){
161171
log_e("Timer Attach Interrupt failed, error num=%d", err);
162172
}
163173
gptimer_enable(timer->timer_handle);
164-
}
174+
if(timer->timer_started == true){
175+
gptimer_start(timer->timer_handle);
176+
}
165177

178+
}
166179

167180
void timerAttachInterruptArg(hw_timer_t * timer, void (*userFunc)(void*), void * arg){
168181
timerAttachInterruptFunctionalArg(timer, userFunc, arg);

Diff for: tests/timer/timer.ino

+15-16
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,29 @@
1111
* S3 - APB + XTAL clk
1212
*/
1313

14-
hw_timer_t * timer = NULL;
15-
hw_timer_t * timer_XTAL = NULL;
14+
static hw_timer_t * timer = NULL;
1615
static volatile bool alarm_flag;
1716

18-
/* These functions are intended to be called before and after each test. */
17+
/* setUp / tearDown functions are intended to be called before / after each test. */
1918
void setUp(void) {
2019
timer = timerBegin(TIMER_FREQUENCY);
20+
if(timer == NULL){
21+
TEST_FAIL_MESSAGE("Timer init failed in setUp()");
22+
}
2123
timerStop(timer);
2224
timerRestart(timer);
2325
}
2426

2527
void tearDown(void){
2628
timerEnd(timer);
27-
timer = NULL;
2829
}
2930

30-
31-
3231
void ARDUINO_ISR_ATTR onTimer(){
3332
alarm_flag = true;
3433
}
3534

36-
3735
void timer_interrupt_test(void){
38-
36+
3937
alarm_flag = false;
4038
timerAttachInterrupt(timer, &onTimer);
4139
timerAlarm(timer, (1.2 * TIMER_FREQUENCY), true, 0);
@@ -67,9 +65,11 @@ void timer_divider_test(void){
6765

6866
// compare divider 16 and 8, value should be double
6967
timerEnd(timer);
70-
timer = NULL;
7168

7269
timer = timerBegin(2 * TIMER_FREQUENCY);
70+
if(timer == NULL){
71+
TEST_FAIL_MESSAGE("Timer init failed!");
72+
}
7373
timerRestart(timer);
7474
delay(1000);
7575
comp_time_val = timerRead(timer);
@@ -79,9 +79,11 @@ void timer_divider_test(void){
7979

8080
// divider is 256, value should be 2^4
8181
timerEnd(timer);
82-
timer = NULL;
8382

8483
timer = timerBegin(TIMER_FREQUENCY / 16);
84+
if(timer == NULL){
85+
TEST_FAIL_MESSAGE("Timer init failed!");
86+
}
8587
timerRestart(timer);
8688
delay(1000);
8789
comp_time_val = timerRead(timer);
@@ -103,13 +105,10 @@ void timer_read_test(void){
103105

104106
void timer_clock_select_test(void){
105107
// Set timer frequency that can be achieved using XTAL clock source (autoselected)
106-
timer_XTAL = timerBegin(TIMER_FREQUENCY_XTAL_CLK);
108+
timer = timerBegin(TIMER_FREQUENCY_XTAL_CLK);
107109

108-
uint32_t resolution = timerGetFrequency(timer_XTAL);
110+
uint32_t resolution = timerGetFrequency(timer);
109111
TEST_ASSERT_EQUAL(TIMER_FREQUENCY_XTAL_CLK,resolution);
110-
111-
timerEnd(timer_XTAL);
112-
timer_XTAL = NULL;
113112
}
114113

115114
void setup(){
@@ -131,4 +130,4 @@ void setup(){
131130
}
132131

133132
void loop(){
134-
}
133+
}

0 commit comments

Comments
 (0)