Skip to content

Commit b2d1f67

Browse files
authored
Merge branch 'micropython:master' into esp32_bitstream
2 parents 092036e + 593ae04 commit b2d1f67

File tree

8 files changed

+40
-17
lines changed

8 files changed

+40
-17
lines changed

ports/alif/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@
132132
#define MICROPY_PY_MACHINE_PULSE (1)
133133
#define MICROPY_PY_MACHINE_I2C (MICROPY_HW_ENABLE_HW_I2C)
134134
#define MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 (1)
135+
#ifndef MICROPY_PY_MACHINE_I2C_TARGET
135136
#define MICROPY_PY_MACHINE_I2C_TARGET (MICROPY_HW_ENABLE_HW_I2C)
136137
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/alif/machine_i2c_target.c"
137138
#define MICROPY_PY_MACHINE_I2C_TARGET_MAX (4)
138139
#define MICROPY_PY_MACHINE_I2C_TARGET_HARD_IRQ (1)
140+
#endif
139141
#define MICROPY_PY_MACHINE_SOFTI2C (1)
140142
#define MICROPY_PY_MACHINE_SPI (1)
141143
#define MICROPY_PY_MACHINE_SOFTSPI (1)

ports/esp32/machine_timer.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,28 @@
3838
#include "hal/timer_hal.h"
3939
#include "hal/timer_ll.h"
4040
#include "soc/timer_periph.h"
41+
#include "esp_private/esp_clk_tree_common.h"
42+
#include "esp_private/periph_ctrl.h"
4143
#include "machine_timer.h"
4244

45+
#define TIMER_CLK_SRC GPTIMER_CLK_SRC_DEFAULT
4346
#define TIMER_DIVIDER 8
4447

45-
// TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly
46-
#define TIMER_SCALE (APB_CLK_FREQ / TIMER_DIVIDER)
47-
4848
#define TIMER_FLAGS 0
4949

5050
const mp_obj_type_t machine_timer_type;
5151

5252
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);
5353
static mp_obj_t machine_timer_deinit(mp_obj_t self_in);
5454

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+
5563
void machine_timer_deinit_all(void) {
5664
// Disable, deallocate and remove all timers from list
5765
machine_timer_obj_t **t = &MP_STATE_PORT(machine_timer_obj_head);
@@ -66,7 +74,7 @@ void machine_timer_deinit_all(void) {
6674
static void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
6775
machine_timer_obj_t *self = self_in;
6876
qstr mode = self->repeat ? MP_QSTR_PERIODIC : MP_QSTR_ONE_SHOT;
69-
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
7078
#if SOC_TIMER_GROUP_TIMERS_PER_GROUP == 1
7179
mp_printf(print, "Timer(%u, mode=%q, period=%lu)", self->group, mode, period);
7280
#else
@@ -163,8 +171,18 @@ static void machine_timer_isr_handler(machine_timer_obj_t *self) {
163171
void machine_timer_enable(machine_timer_obj_t *self) {
164172
// Initialise the timer.
165173
timer_hal_init(&self->hal_context, self->group, self->index);
174+
175+
PERIPH_RCC_ACQUIRE_ATOMIC(timer_group_periph_signals.groups[self->index].module, ref_count) {
176+
if (ref_count == 0) {
177+
timer_ll_enable_bus_clock(self->index, true);
178+
timer_ll_reset_register(self->index);
179+
}
180+
}
181+
166182
timer_ll_enable_counter(self->hal_context.dev, self->index, false);
167-
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);
185+
timer_ll_enable_clock(self->hal_context.dev, self->index, true);
168186
timer_ll_set_clock_prescale(self->hal_context.dev, self->index, TIMER_DIVIDER);
169187
timer_hal_set_counter_value(&self->hal_context, 0);
170188
timer_ll_set_count_direction(self->hal_context.dev, self->index, GPTIMER_COUNT_UP);
@@ -224,15 +242,15 @@ static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n
224242

225243
#if MICROPY_PY_BUILTINS_FLOAT
226244
if (args[ARG_freq].u_obj != mp_const_none) {
227-
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));
228246
}
229247
#else
230248
if (args[ARG_freq].u_int != 0xffffffff) {
231249
self->period = TIMER_SCALE / ((uint64_t)args[ARG_freq].u_int);
232250
}
233251
#endif
234252
else {
235-
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;
236254
}
237255

238256
self->repeat = args[ARG_mode].u_int;
@@ -268,7 +286,7 @@ static mp_obj_t machine_timer_value(mp_obj_t self_in) {
268286
mp_raise_ValueError(MP_ERROR_TEXT("timer not set"));
269287
}
270288
uint64_t result = timer_ll_get_counter_value(self->hal_context.dev, self->index);
271-
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
272290
}
273291
static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);
274292

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
}

ports/esp32/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@
139139
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/esp32/machine_pwm.c"
140140
#define MICROPY_PY_MACHINE_I2C (1)
141141
#define MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 (1)
142+
#ifndef MICROPY_PY_MACHINE_I2C_TARGET
142143
// I2C target hardware is limited on ESP32 (eg read event comes after the read) so we only support newer SoCs.
143144
// ESP32C6 does not have enough flash space so also disable it on that SoC.
144145
#define MICROPY_PY_MACHINE_I2C_TARGET (SOC_I2C_SUPPORT_SLAVE && !CONFIG_IDF_TARGET_ESP32 && !CONFIG_IDF_TARGET_ESP32C6)
145146
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/esp32/machine_i2c_target.c"
146147
#define MICROPY_PY_MACHINE_I2C_TARGET_MAX (2)
148+
#endif
147149
#define MICROPY_PY_MACHINE_SOFTI2C (1)
148150
#define MICROPY_PY_MACHINE_SPI (1)
149151
#define MICROPY_PY_MACHINE_SOFTSPI (1)

ports/mimxrt/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ uint32_t trng_random_u32(void);
9292
#define MICROPY_PY_MACHINE_PWM (1)
9393
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/mimxrt/machine_pwm.c"
9494
#define MICROPY_PY_MACHINE_I2C (1)
95+
#ifndef MICROPY_PY_MACHINE_I2C_TARGET
9596
#define MICROPY_PY_MACHINE_I2C_TARGET (1)
9697
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/mimxrt/machine_i2c_target.c"
9798
#define MICROPY_PY_MACHINE_I2C_TARGET_MAX (FSL_FEATURE_SOC_LPI2C_COUNT)
9899
#define MICROPY_PY_MACHINE_I2C_TARGET_HARD_IRQ (1)
99100
#define MICROPY_PY_MACHINE_I2C_TARGET_FINALISER (1)
101+
#endif
100102
#ifndef MICROPY_PY_MACHINE_I2S
101103
#define MICROPY_PY_MACHINE_I2S (0)
102104
#endif

ports/rp2/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,12 @@
171171
#define MICROPY_PY_MACHINE_PWM (1)
172172
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/rp2/machine_pwm.c"
173173
#define MICROPY_PY_MACHINE_I2C (1)
174+
#ifndef MICROPY_PY_MACHINE_I2C_TARGET
174175
#define MICROPY_PY_MACHINE_I2C_TARGET (1)
175176
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/rp2/machine_i2c_target.c"
176177
#define MICROPY_PY_MACHINE_I2C_TARGET_MAX (2)
177178
#define MICROPY_PY_MACHINE_I2C_TARGET_HARD_IRQ (1)
179+
#endif
178180
#define MICROPY_PY_MACHINE_SOFTI2C (1)
179181
#define MICROPY_PY_MACHINE_I2S (1)
180182
#define MICROPY_PY_MACHINE_I2S_INCLUDEFILE "ports/rp2/machine_i2s.c"

ports/stm32/mpconfigboard_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,13 @@
639639
#if defined(MICROPY_HW_I2C1_SCL) || defined(MICROPY_HW_I2C2_SCL) \
640640
|| defined(MICROPY_HW_I2C3_SCL) || defined(MICROPY_HW_I2C4_SCL)
641641
#define MICROPY_HW_ENABLE_HW_I2C (1)
642+
#ifndef MICROPY_HW_ENABLE_HW_I2C_TARGET
642643
#if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32WB)
643644
#define MICROPY_HW_ENABLE_HW_I2C_TARGET (1)
644645
#else
645646
#define MICROPY_HW_ENABLE_HW_I2C_TARGET (0)
646647
#endif
648+
#endif
647649
#else
648650
#define MICROPY_HW_ENABLE_HW_I2C (0)
649651
#define MICROPY_HW_ENABLE_HW_I2C_TARGET (0)

0 commit comments

Comments
 (0)