Skip to content

Commit 593ae04

Browse files
projectgusdpgeorge
authored andcommitted
esp32/machine_timer: Fix machine.Timer() tick frequency on ESP32C2,C6.
Also future-proofs this code for other chips. Apart form C6 and C2, all currently supported chips use APB clock for GPTIMER_CLK_SRC_DEFAULT. ESP32-C2 uses 40MHz PLL but APB_CLK_FREQ was 26MHz. ESP32-C6 uses 80MHz PLL but APB_CLK_FREQ was 40MHz. Implementation now gets the correct frequency from ESP-IDF. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent ce109af commit 593ae04

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

ports/esp32/machine_timer.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,24 @@
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

5250
const mp_obj_type_t machine_timer_type;
5351

5452
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);
5553
static 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+
5763
void 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) {
6874
static 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
}
285291
static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);
286292

ports/esp32/machine_timer.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@
3434
#include "hal/timer_ll.h"
3535
#include "soc/timer_periph.h"
3636

37-
#define TIMER_DIVIDER 8
38-
39-
// TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly
40-
#define TIMER_SCALE (APB_CLK_FREQ / TIMER_DIVIDER)
41-
42-
#define TIMER_FLAGS 0
43-
4437
typedef struct _machine_timer_obj_t {
4538
mp_obj_base_t base;
4639

@@ -64,4 +57,6 @@ machine_timer_obj_t *machine_timer_create(mp_uint_t timer);
6457
void machine_timer_enable(machine_timer_obj_t *self);
6558
void machine_timer_disable(machine_timer_obj_t *self);
6659

60+
uint32_t machine_timer_freq_hz(void);
61+
6762
#endif // MICROPY_INCLUDED_ESP32_MACHINE_TIMER_H

ports/esp32/machine_uart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#define UART_IRQ_RXIDLE (0x1000)
5959
#define UART_IRQ_BREAK (1 << UART_BREAK)
6060
#define MP_UART_ALLOWED_FLAGS (UART_IRQ_RX | UART_IRQ_RXIDLE | UART_IRQ_BREAK)
61-
#define RXIDLE_TIMER_MIN (5000) // 500 us
61+
#define RXIDLE_TIMER_MIN (machine_timer_freq_hz() * 5 / 10000) // 500us minimum rxidle time
6262
#define UART_QUEUE_SIZE (3)
6363

6464
enum {
@@ -535,7 +535,7 @@ static void uart_irq_configure_timer(machine_uart_obj_t *self, mp_uint_t trigger
535535
self->mp_irq_obj->ishard = false;
536536
uint32_t baudrate;
537537
uart_get_baudrate(self->uart_num, &baudrate);
538-
mp_int_t period = TIMER_SCALE * 20 / baudrate + 1;
538+
mp_int_t period = machine_timer_freq_hz() * 20 / baudrate + 1;
539539
if (period < RXIDLE_TIMER_MIN) {
540540
period = RXIDLE_TIMER_MIN;
541541
}

0 commit comments

Comments
 (0)