8000 rp2: BROKEN Disable default alarm pool in pico-sdk. · projectgus/micropython@9c8ddd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c8ddd8

Browse files
committed
rp2: BROKEN Disable default alarm pool in pico-sdk.
- Building pico-sdk without default alarm pool removes internal usages for multicore_lockout_start_blocking() and during USB reset. - Create a custom alarm pool for machine.Timer() as a stop-gap to reimplementing machine.Timer() using soft timers. The known issue with pico-sdk alarm pool should only trigger here if the MicroPython code uses a lot of short lived machine.Timer() instances. BROKEN: THIS PANICS ON USE OF machine.Timer(), as alarm_pool_create() calls through to pico-sdk malloc which is not functional on MicroPython. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 35245a7 commit 9c8ddd8

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ target_compile_definitions(${MICROPY_TARGET} PRIVATE
470470
FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
471471
LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT
472472
LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT
473+
PICO_TIME_DEFAULT_ALARM_POOL_DISABLED=1
473474
PICO_FLOAT_PROPAGATE_NANS=1
474475
PICO_STACK_SIZE=0x2000
475476
PICO_CORE1_STACK_SIZE=0

ports/rp2/machine_timer.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
#define TIMER_MODE_ONE_SHOT (0)
3434
#define TIMER_MODE_PERIODIC (1)
3535

36+
STATIC struct alarm_pool *timer_pool;
37+
3638
typedef struct _machine_timer_obj_t {
3739
mp_obj_base_t base;
38-
struct alarm_pool *pool;
3940
alarm_id_t alarm_id;
4041
uint32_t mode;
4142
uint64_t delta_us; // for periodic mode
@@ -95,8 +96,16 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar
9596
self->delta_us = 1;
9697
}
9798

99+
if (timer_pool == NULL) {
100+
// Lazy initialise the alarm pool on first use
101+
timer_pool = alarm_pool_create(
102+
MICROPY_HW_MACHINE_TIMER_ALARM_NUM,
103+
PICO_TIME_DEFAULT_ALARM_POOL_MAX_TIMERS
104+
);
105+
}
106+
98107
self->callback = args[ARG_callback].u_obj;
99-
self->alarm_id = alarm_pool_add_alarm_in_us(self->pool, self->delta_us, alarm_callback, self, true);
108+
self->alarm_id = alarm_pool_add_alarm_in_us(timer_pool, self->delta_us, alarm_callback, self, true);
100109
if (self->alarm_id == -1) {
101110
mp_raise_OSError(MP_ENOMEM);
102111
}
@@ -107,7 +116,6 @@ STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_ar
107116
STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
108117
machine_timer_obj_t *self = m_new_obj_with_finaliser(machine_timer_obj_t);
109118
self->base.type = &machine_timer_type;
110-
self->pool = alarm_pool_get_default();
111119
self->alarm_id = ALARM_ID_INVALID;
112120

113121
// Get timer id (only soft timer (-1) supported at the moment)
@@ -134,7 +142,7 @@ STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args,
134142
STATIC mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
135143
machine_timer_obj_t *self = MP_OBJ_TO_PTR(args[0]);
136144
if (self->alarm_id != ALARM_ID_INVALID) {
137-
alarm_pool_cancel_alarm(self->pool, self->alarm_id);
145+
alarm_pool_cancel_alarm(timer_pool, self->alarm_id);
138146
self->alarm_id = ALARM_ID_INVALID;
139147
}
140148
return machine_timer_init_helper(self, n_args - 1, args + 1, kw_args);
@@ -144,7 +152,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init)
144152
STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
145153
machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in);
146154
if (self->alarm_id != ALARM_ID_INVALID) {
147-
alarm_pool_cancel_alarm(self->pool, self->alarm_id);
155+
alarm_pool_cancel_alarm(timer_pool, self->alarm_id);
148156
self->alarm_id = ALARM_ID_INVALID;
149157
}
150158
return mp_const_none;

ports/rp2/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
#define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP)
149149

150150
#define MICROPY_HW_SOFT_TIMER_ALARM_NUM (2)
151+
#define MICROPY_HW_MACHINE_TIMER_ALARM_NUM (1)
151152

152153
// fatfs configuration
153154
#define MICROPY_FATFS_ENABLE_LFN (1)

0 commit comments

Comments
 (0)
0