Skip to content

Commit d247e5c

Browse files
committed
Add the ability for a port to gc things, collect pin change objects that way
1 parent 47e1abd commit d247e5c

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,8 @@ void gc_collect(void) {
11111111
// have lost their references in the VM even though they are mounted.
11121112
gc_collect_root((void **)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
11131113

1114+
port_gc_collect();
1115+
11141116
background_callback_gc_collect();
11151117

11161118
#if CIRCUITPY_ALARM
@@ -1143,6 +1145,10 @@ void gc_collect(void) {
11431145
gc_collect_end();
11441146
}
11451147

1148+
// Ports may provide an implementation of this function if it is needed
1149+
MP_WEAK void port_gc_collect() {
1150+
}
1151+
11461152
void NORETURN nlr_jump_fail(void *val) {
11471153
reset_into_safe_mode(SAFE_MODE_NLR_JUMP_FAIL);
11481154
while (true) {

ports/mimxrt10xx/common-hal/microcontroller/Pin.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "shared-bindings/microcontroller/__init__.h"
2929
#include "shared-bindings/microcontroller/Pin.h"
3030

31+
#include "py/gc.h"
32+
3133
STATIC bool claimed_pins[IOMUXC_SW_PAD_CTL_PAD_COUNT];
3234
STATIC bool never_reset_pins[IOMUXC_SW_PAD_CTL_PAD_COUNT];
3335

@@ -147,6 +149,11 @@ typedef struct {
147149

148150
volatile static pin_change_interrupt_data pcid[MP_ARRAY_SIZE(s_gpioBases)][32];
149151

152+
// The 'data' pointers may be to gc objects, they must be kept alive.
153+
void pin_gc_collect() {
154+
gc_collect_root((void **)&pcid, sizeof(pcid) / sizeof(void *));
155+
}
156+
150157
void enable_pin_change_interrupt(const mcu_pin_obj_t *pin, gpio_change_interrupt_t func, void *data) {
151158
int instance = GPIO_GetInstance(pin->gpio);
152159
volatile pin_change_interrupt_data *pci = &pcid[instance][pin->number];

ports/mimxrt10xx/common-hal/microcontroller/Pin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ extern bool mimxrt10xx_board_reset_pin_number(const mcu_pin_obj_t *pin);
4848
typedef void (gpio_change_interrupt_t)(void *data);
4949
void disable_pin_change_interrupt(const mcu_pin_obj_t *pin);
5050
void enable_pin_change_interrupt(const mcu_pin_obj_t *pin, gpio_change_interrupt_t func, void *data);
51+
void pin_gc_collect(void);
5152

5253
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_MICROCONTROLLER_PIN_H

ports/mimxrt10xx/supervisor/port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ void port_idle_until_interrupt(void) {
555555
common_hal_mcu_enable_interrupts();
556556
}
557557

558+
void port_gc_collect(void) {
559+
pin_gc_collect();
560+
}
561+
558562
// Catch faults where the memory access violates MPU settings.
559563
void MemManage_Handler(void);
560564
__attribute__((used)) void PLACE_IN_ITCM(MemManage_Handler)(void) {

supervisor/port.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,8 @@ void port_post_boot_py(bool heap_valid);
128128
// A default weak implementation is provided that does nothing.
129129
void port_boot_info(void);
130130

131+
// Some ports want to mark additional pointers as gc roots.
132+
// A default weak implementation is provided that does nothing.
133+
void port_gc_collect(void);
134+
131135
#endif // MICROPY_INCLUDED_SUPERVISOR_PORT_H

0 commit comments

Comments
 (0)