42
42
#include "esp_private/periph_ctrl.h"
43
43
#include "machine_timer.h"
44
44
45
+ #define TIMER_CLK_SRC GPTIMER_CLK_SRC_DEFAULT
45
46
#define TIMER_DIVIDER 8
46
47
47
- // TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly
48
- #define TIMER_SCALE (APB_CLK_FREQ / TIMER_DIVIDER)
49
-
50
48
#define TIMER_FLAGS 0
51
49
52
50
const mp_obj_type_t machine_timer_type ;
53
51
54
52
static mp_obj_t machine_timer_init_helper (machine_timer_obj_t * self , mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args );
55
53
static mp_obj_t machine_timer_deinit (mp_obj_t self_in );
56
54
55
+ uint32_t machine_timer_freq_hz (void ) {
56
+ // The timer source clock is APB or a fixed PLL (depending on chip), both constant frequency.
57
+ uint32_t freq ;
58
+ check_esp_err (esp_clk_tree_src_get_freq_hz (TIMER_CLK_SRC , ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED , & freq ));
59
+ assert (freq % TIMER_DIVIDER == 0 ); // Source clock should divide evenly into TIMER_DIVIDER
60
+ return freq / TIMER_DIVIDER ;
61
+ }
62
+
57
63
void machine_timer_deinit_all (void ) {
58
64
// Disable, deallocate and remove all timers from list
59
65
machine_timer_obj_t * * t = & MP_STATE_PORT (machine_timer_obj_head );
@@ -68,7 +74,7 @@ void machine_timer_deinit_all(void) {
68
74
static void machine_timer_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
69
75
machine_timer_obj_t * self = self_in ;
70
76
qstr mode = self -> repeat ? MP_QSTR_PERIODIC : MP_QSTR_ONE_SHOT ;
71
- uint64_t period = self -> period / (TIMER_SCALE / 1000 ); // convert to ms
77
+ uint64_t period = self -> period / (machine_timer_freq_hz () / 1000 ); // convert to ms
72
78
#if SOC_TIMER_GROUP_TIMERS_PER_GROUP == 1
73
79
mp_printf (print , "Timer(%u, mode=%q, period=%lu)" , self -> group , mode , period );
74
80
#else
@@ -174,8 +180,8 @@ void machine_timer_enable(machine_timer_obj_t *self) {
174
180
}
175
181
176
182
timer_ll_enable_counter (self -> hal_context .dev , self -> index , false);
177
- esp_clk_tree_enable_src (GPTIMER_CLK_SRC_DEFAULT , true);
178
- timer_ll_set_clock_source (self -> hal_context .dev , self -> index , GPTIMER_CLK_SRC_DEFAULT );
183
+ esp_clk_tree_enable_src (TIMER_CLK_SRC , true);
184
+ timer_ll_set_clock_source (self -> hal_context .dev , self -> index , TIMER_CLK_SRC );
179
185
timer_ll_enable_clock (self -> hal_context .dev , self -> index , true);
180
186
timer_ll_set_clock_prescale (self -> hal_context .dev , self -> index , TIMER_DIVIDER );
181
187
timer_hal_set_counter_value (& self -> hal_context , 0 );
@@ -236,15 +242,15 @@ static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n
236
242
237
243
#if MICROPY_PY_BUILTINS_FLOAT
238
244
if (args [ARG_freq ].u_obj != mp_const_none ) {
239
- self -> period = (uint64_t )(TIMER_SCALE / mp_obj_get_float (args [ARG_freq ].u_obj ));
245
+ self -> period = (uint64_t )(machine_timer_freq_hz () / mp_obj_get_float (args [ARG_freq ].u_obj ));
240
246
}
241
247
#else
242
248
if (args [ARG_freq ].u_int != 0xffffffff ) {
243
249
self -> period = TIMER_SCALE / ((uint64_t )args [ARG_freq ].u_int );
244
250
}
245
251
#endif
246
252
else {
247
- self -> period = (((uint64_t )args [ARG_period ].u_int ) * TIMER_SCALE ) / args [ARG_tick_hz ].u_int ;
253
+ self -> period = (((uint64_t )args [ARG_period ].u_int ) * machine_timer_freq_hz () ) / args [ARG_tick_hz ].u_int ;
248
254
}
249
255
250
256
self -> repeat = args [ARG_mode ].u_int ;
@@ -280,7 +286,7 @@ static mp_obj_t machine_timer_value(mp_obj_t self_in) {
280
286
mp_raise_ValueError (MP_ERROR_TEXT ("timer not set" ));
281
287
}
282
288
uint64_t result = timer_ll_get_counter_value (self -> hal_context .dev , self -> index );
283
- return MP_OBJ_NEW_SMALL_INT ((mp_uint_t )(result / (TIMER_SCALE / 1000 ))); // value in ms
289
+ return MP_OBJ_NEW_SMALL_INT ((mp_uint_t )(result / (machine_timer_freq_hz () / 1000 ))); // value in ms
284
290
}
285
291
static MP_DEFINE_CONST_FUN_OBJ_1 (machine_timer_value_obj , machine_timer_value ) ;
286
292
0 commit comments