mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
misc: Add MicroPython GC helper functions.
This commit is contained in:
parent
7bf5b3fa7e
commit
79019cce0d
@ -26,7 +26,7 @@ SRCS += $(addprefix common/, \
|
||||
usbdbg.c \
|
||||
tinyusb_debug.c \
|
||||
file_utils.c \
|
||||
boot_utils.c \
|
||||
mp_utils.c \
|
||||
sensor_utils.c \
|
||||
)
|
||||
|
||||
|
||||
@ -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__
|
||||
@ -1,18 +1,23 @@
|
||||
/*
|
||||
* 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>
|
||||
* 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.
|
||||
*
|
||||
* Boot util functions.
|
||||
* Common MicroPython utility functions.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/mphal.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"
|
||||
#if MICROPY_HW_USB_MSC
|
||||
#include "extmod/vfs.h"
|
||||
@ -27,12 +32,33 @@
|
||||
#include "wifidbg.h"
|
||||
#endif
|
||||
#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
|
||||
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;
|
||||
uint8_t working_buf[FF_MAX_SS];
|
||||
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
|
||||
|
||||
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;
|
||||
bool interrupted = false;
|
||||
|
||||
@ -93,3 +119,31 @@ bool bootutils_exec_bootscript(const char *path, bool interruptible, bool wifidb
|
||||
|
||||
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
17
src/omv/common/mp_utils.h
Normal 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__
|
||||
Loading…
Reference in New Issue
Block a user