@@ -37,17 +37,28 @@ struct timer_struct_t {
37
37
};
38
38
39
39
inline uint64_t timerRead (hw_timer_t * timer ) {
40
-
40
+ if (timer == NULL ) {
41
+ log_e ("Timer handle is NULL" );
42
+ return 0 ;
43
+ }
41
44
uint64_t value ;
42
45
gptimer_get_raw_count (timer -> timer_handle , & value );
43
46
return value ;
44
47
}
45
48
46
49
void timerWrite (hw_timer_t * timer , uint64_t val ) {
50
+ if (timer == NULL ) {
51
+ log_e ("Timer handle is NULL" );
52
+ return ;
53
+ }
47
54
gptimer_set_raw_count (timer -> timer_handle , val );
48
55
}
49
56
50
57
void timerAlarm (hw_timer_t * timer , uint64_t alarm_value , bool autoreload , uint64_t reload_count ) {
58
+ if (timer == NULL ) {
59
+ log_e ("Timer handle is NULL" );
60
+ return ;
61
+ }
51
62
esp_err_t err = ESP_OK ;
52
63
gptimer_alarm_config_t alarm_cfg = {
53
64
.alarm_count = alarm_value ,
@@ -61,22 +72,37 @@ void timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64
61
72
}
62
73
63
74
uint32_t timerGetFrequency (hw_timer_t * timer ) {
75
+ if (timer == NULL ) {
76
+ return 0 ;
77
+ }
64
78
uint32_t frequency ;
65
79
gptimer_get_resolution (timer -> timer_handle , & frequency );
66
80
return frequency ;
67
81
}
68
82
69
83
void timerStart (hw_timer_t * timer ) {
84
+ if (timer == NULL ) {
85
+ log_e ("Timer handle is NULL" );
86
+ return ;
87
+ }
70
88
gptimer_start (timer -> timer_handle );
71
89
timer -> timer_started = true;
72
90
}
73
91
74
92
void timerStop (hw_timer_t * timer ) {
93
+ if (timer == NULL ) {
94
+ log_e ("Timer handle is NULL" );
95
+ return ;
96
+ }
75
97
gptimer_stop (timer -> timer_handle );
76
98
timer -> timer_started = false;
77
99
}
78
100
79
101
void timerRestart (hw_timer_t * timer ) {
102
+ if (timer == NULL ) {
103
+ log_e ("Timer handle is NULL" );
104
+ return ;
105
+ }
80
106
gptimer_set_raw_count (timer -> timer_handle , 0 );
81
107
}
82
108
@@ -129,17 +155,19 @@ hw_timer_t *timerBegin(uint32_t frequency) {
129
155
}
130
156
131
157
void timerEnd (hw_timer_t * timer ) {
132
- esp_err_t err = ESP_OK ;
133
- if (timer -> timer_started == true) {
134
- gptimer_stop (timer -> timer_handle );
135
- }
136
- gptimer_disable (timer -> timer_handle );
137
- err = gptimer_del_timer (timer -> timer_handle );
138
- if (err != ESP_OK ) {
139
- log_e ("Failed to destroy GPTimer, error num=%d" , err );
140
- return ;
158
+ if (timer != NULL ) {
159
+ esp_err_t err = ESP_OK ;
160
+ if (timer -> timer_started == true) {
161
+ gptimer_stop (timer -> timer_handle );
162
+ }
163
+ gptimer_disable (timer -> timer_handle );
164
+ err = gptimer_del_timer (timer -> timer_handle );
165
+ if (err != ESP_OK ) {
166
+ log_e ("Failed to destroy GPTimer, error num=%d" , err );
167
+ return ;
168
+ }
169
+ free (timer );
141
170
}
142
- free (timer );
143
171
}
144
172
145
173
bool IRAM_ATTR timerFnWrapper (gptimer_handle_t timer , const gptimer_alarm_event_data_t * edata , void * args ) {
@@ -156,6 +184,10 @@ bool IRAM_ATTR timerFnWrapper(gptimer_handle_t timer, const gptimer_alarm_event_
156
184
}
157
185
158
186
void timerAttachInterruptFunctionalArg (hw_timer_t * timer , void (* userFunc )(void * ), void * arg ) {
187
+ if (timer == NULL ) {
188
+ log_e ("Timer handle is NULL" );
189
+ return ;
190
+ }
159
191
esp_err_t err = ESP_OK ;
160
192
gptimer_event_callbacks_t cbs = {
161
193
.on_alarm = timerFnWrapper ,
@@ -187,6 +219,10 @@ void timerAttachInterrupt(hw_timer_t *timer, voidFuncPtr userFunc) {
187
219
}
188
220
189
221
void timerDetachInterrupt (hw_timer_t * timer ) {
222
+ if (timer == NULL ) {
223
+ log_e ("Timer handle is NULL" );
224
+ return ;
225
+ }
190
226
esp_err_t err = ESP_OK ;
191
227
err = gptimer_set_alarm_action (timer -> timer_handle , NULL );
192
228
timer -> interrupt_handle .fn = NULL ;
@@ -197,18 +233,30 @@ void timerDetachInterrupt(hw_timer_t *timer) {
197
233
}
198
234
199
235
uint64_t timerReadMicros (hw_timer_t * timer ) {
236
+ if (timer == NULL ) {
237
+ log_e ("Timer handle is NULL" );
238
+ return 0 ;
239
+ }
200
240
uint64_t timer_val = timerRead (timer );
201
241
uint32_t frequency = timerGetFrequency (timer );
202
242
return timer_val * 1000000 / frequency ;
203
243
}
204
244
205
245
uint64_t timerReadMilis (hw_timer_t * timer ) {
246
+ if (timer == NULL ) {
247
+ log_e ("Timer handle is NULL" );
248
+ return 0 ;
249
+ }
206
250
uint64_t timer_val = timerRead (timer );
207
251
uint32_t frequency = timerGetFrequency (timer );
208
252
return timer_val * 1000 / frequency ;
209
253
}
210
254
211
255
double timerReadSeconds (hw_timer_t * timer ) {
256
+ if (timer == NULL ) {
257
+ log_e ("Timer handle is NULL" );
258
+ return 0 ;
259
+ }
212
260
uint64_t timer_val = timerRead (timer );
213
261
uint32_t frequency = timerGetFrequency (timer );
214
262
return (double )timer_val / frequency ;
0 commit comments