misc: Add MicroPython GC helper functions.

This commit is contained in:
iabdalkader 2024-06-22 14:25:43 +02:00
parent 7bf5b3fa7e
commit 79019cce0d
4 changed files with 78 additions and 23 deletions

View File

@ -26,7 +26,7 @@ SRCS += $(addprefix common/, \
usbdbg.c \ usbdbg.c \
tinyusb_debug.c \ tinyusb_debug.c \
file_utils.c \ file_utils.c \
boot_utils.c \ mp_utils.c \
sensor_utils.c \ sensor_utils.c \
) )

View File

@ -1,16 +0,0 @@
/*
* This file is part of the OpenMV project.
*
* Copyright (c) 2013-2023 Ibrahim Abdelkader <iabdalkader@openmv.io>
* Copyright (c) 2013-2023 Kwabena W. Agyeman <kwagyeman@openmv.io>
*
* This work is licensed under the MIT license, see the file LICENSE for details.
*
* Boot util functions.
*/
#ifndef __BOOT_UTILS_H__
#define __BOOT_UTILS_H__
typedef struct _fs_user_mount_t fs_user_mount_t;
int bootutils_init_filesystem(fs_user_mount_t *vfs);
bool bootutils_exec_bootscript(const char *path, bool interruptible, bool wifidbg_enabled);
#endif // __BOOT_UTILS_H__

View File

@ -1,18 +1,23 @@
/* /*
* This file is part of the OpenMV project. * This file is part of the OpenMV project.
* *
* Copyright (c) 2013-2023 Ibrahim Abdelkader <iabdalkader@openmv.io> * Copyright (c) 2013-2024 Ibrahim Abdelkader <iabdalkader@openmv.io>
* Copyright (c) 2013-2023 Kwabena W. Agyeman <kwagyeman@openmv.io> * Copyright (c) 2013-2024 Kwabena W. Agyeman <kwagyeman@openmv.io>
* *
* This work is licensed under the MIT license, see the file LICENSE for details. * This work is licensed under the MIT license, see the file LICENSE for details.
* *
* Boot util functions. * Common MicroPython utility functions.
*/ */
#include <stdio.h> #include <stdio.h>
#include "py/runtime.h" #include "py/runtime.h"
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "py/objstr.h" #include "py/objstr.h"
#include "py/mpthread.h"
#include "py/gc.h"
#include "py/stackctrl.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/softtimer.h"
#include "shared/runtime/pyexec.h" #include "shared/runtime/pyexec.h"
#if MICROPY_HW_USB_MSC #if MICROPY_HW_USB_MSC
#include "extmod/vfs.h" #include "extmod/vfs.h"
@ -27,12 +32,33 @@
#include "wifidbg.h" #include "wifidbg.h"
#endif #endif
#include "file_utils.h" #include "file_utils.h"
#include "boot_utils.h" #include "mp_utils.h"
void __attribute__((weak)) gc_collect(void) {
// start the GC
gc_collect_start();
// trace the stack and registers
gc_helper_collect_regs_and_stack();
// trace root pointers from any threads
#if MICROPY_PY_THREAD
mp_thread_gc_others();
#endif
// trace soft timer nodes
#ifndef MP_PORT_NO_SOFTTIMER
soft_timer_gc_mark_all();
#endif
// end the GC
gc_collect_end();
}
#if MICROPY_VFS_FAT #if MICROPY_VFS_FAT
extern void __fatal_error(); extern void __fatal_error();
int bootutils_init_filesystem(fs_user_mount_t *vfs) { int mp_init_filesystem(fs_user_mount_t *vfs) {
FIL fp; UINT n; FIL fp; UINT n;
uint8_t working_buf[FF_MAX_SS]; uint8_t working_buf[FF_MAX_SS];
if (f_mkfs(&vfs->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)) != FR_OK) { if (f_mkfs(&vfs->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)) != FR_OK) {
@ -59,7 +85,7 @@ int bootutils_init_filesystem(fs_user_mount_t *vfs) {
} }
#endif #endif
bool bootutils_exec_bootscript(const char *path, bool interruptible, bool wifidbg_enabled) { bool mp_exec_bootscript(const char *path, bool interruptible, bool wifidbg_enabled) {
nlr_buf_t nlr; nlr_buf_t nlr;
bool interrupted = false; bool interrupted = false;
@ -93,3 +119,31 @@ bool bootutils_exec_bootscript(const char *path, bool interruptible, bool wifidb
return interrupted; return interrupted;
} }
void mp_init_gc_stack(void *sstack, void *estack, void *heap_start, void *heap_end, size_t stack_limit) {
// Initialize the stack.
mp_stack_set_top(estack);
// Set stack limit
// Stack limit should be less than real stack size, so we have a
// chance to recover from limit hit. (Limit is measured in bytes)
mp_stack_set_limit(estack - sstack - stack_limit);
// Initialize gc memory.
gc_init(heap_start, heap_end);
// Add GC blocks (if enabled).
#ifdef OMV_GC_BLOCK0_MEMORY
typedef struct {
uint32_t *addr;
uint32_t size;
} gc_blocks_table_t;
extern const gc_blocks_table_t _gc_blocks_table_start;
extern const gc_blocks_table_t _gc_blocks_table_end;
for (gc_blocks_table_t const *block = &_gc_blocks_table_start; block < &_gc_blocks_table_end; block++) {
gc_add(block->addr, block->addr + block->size);
}
#endif
}

17
src/omv/common/mp_utils.h Normal file
View File

@ -0,0 +1,17 @@
/*
* This file is part of the OpenMV project.
*
* Copyright (c) 2013-2024 Ibrahim Abdelkader <iabdalkader@openmv.io>
* Copyright (c) 2013-2024 Kwabena W. Agyeman <kwagyeman@openmv.io>
*
* This work is licensed under the MIT license, see the file LICENSE for details.
*
* Common MicroPython utility functions.
*/
#ifndef __MP_UTILS_H__
#define __MP_UTILS_H__
typedef struct _fs_user_mount_t fs_user_mount_t;
void mp_init_gc_stack(void *stack_start, void *stack_end, void *heap_start, void *heap_end, size_t stack_limit);
int mp_init_filesystem(fs_user_mount_t *vfs);
bool mp_exec_bootscript(const char *path, bool interruptible, bool wifidbg_enabled);
#endif // __MP_UTILS_H__