4242#include "esp_private/periph_ctrl.h"
4343#include "machine_timer.h"
4444
45+ #define TIMER_CLK_SRC GPTIMER_CLK_SRC_DEFAULT
4546#define TIMER_DIVIDER 8
4647
47- // TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly
48- #define TIMER_SCALE (APB_CLK_FREQ / TIMER_DIVIDER)
49-
5048#define TIMER_FLAGS 0
5149
5250const mp_obj_type_t machine_timer_type ;
5351
5452static 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 );
5553static mp_obj_t machine_timer_deinit (mp_obj_t self_in );
5654
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+
5763void machine_timer_deinit_all (void ) {
5864 // Disable, deallocate and remove all timers from list
5965 machine_timer_obj_t * * t = & MP_STATE_PORT (machine_timer_obj_head );
@@ -68,7 +74,7 @@ void machine_timer_deinit_all(void) {
6874static void machine_timer_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
6975 machine_timer_obj_t * self = self_in ;
7076 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
7278 #if SOC_TIMER_GROUP_TIMERS_PER_GROUP == 1
7379 mp_printf (print , "Timer(%u, mode=%q, period=%lu)" , self -> group , mode , period );
7480 #else
@@ -174,8 +180,8 @@ void machine_timer_enable(machine_timer_obj_t *self) {
174180 }
175181
176182 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 );
179185 timer_ll_enable_clock (self -> hal_context .dev , self -> index , true);
180186 timer_ll_set_clock_prescale (self -> hal_context .dev , self -> index , TIMER_DIVIDER );
181187 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
236242
237243 #if MICROPY_PY_BUILTINS_FLOAT
238244 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 ));
240246 }
241247 #else
242248 if (args [ARG_freq ].u_int != 0xffffffff ) {
243249 self -> period = TIMER_SCALE / ((uint64_t )args [ARG_freq ].u_int );
244250 }
245251 #endif
246252 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 ;
248254 }
249255
250256 self -> repeat = args [ARG_mode ].u_int ;
@@ -280,7 +286,7 @@ static mp_obj_t machine_timer_value(mp_obj_t self_in) {
280286 mp_raise_ValueError (MP_ERROR_TEXT ("timer not set" ));
281287 }
282288 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
284290}
285291static MP_DEFINE_CONST_FUN_OBJ_1 (machine_timer_value_obj , machine_timer_value ) ;
286292
0 commit comments