From 449dd0a69ecee497c5a7da1da23d8ff53778b363 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 25 Jan 2014 00:44:31 +0000 Subject: [PATCH] stm: Put gc_collect code in separate file; define _ram_start in .ld. To partly address Issue #220. --- stm/Makefile | 1 + stm/gccollect.c | 39 +++++++++++++++++++++++++++++++++++++++ stm/gccollect.h | 9 +++++++++ stm/main.c | 38 +++----------------------------------- stm/stm32f405.ld | 1 + 5 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 stm/gccollect.c create mode 100644 stm/gccollect.h diff --git a/stm/Makefile b/stm/Makefile index b3483e089d..c0ae6a89c0 100644 --- a/stm/Makefile +++ b/stm/Makefile @@ -40,6 +40,7 @@ SRC_C = \ string0.c \ malloc0.c \ systick.c \ + gccollect.c \ lexerfatfs.c \ led.c \ lcd.c \ diff --git a/stm/gccollect.c b/stm/gccollect.c new file mode 100644 index 0000000000..c0f67ac0d5 --- /dev/null +++ b/stm/gccollect.c @@ -0,0 +1,39 @@ +#include + +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "gc.h" +#include "gccollect.h" +#include "systick.h" + +void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end); + +void gc_collect(void) { + uint32_t start = sys_tick_counter; + gc_collect_start(); + gc_collect_root((void**)&_ram_start, ((uint32_t)&_heap_start - (uint32_t)&_ram_start) / 4); + machine_uint_t regs[10]; + gc_helper_get_regs_and_clean_stack(regs, HEAP_END); + gc_collect_root((void**)HEAP_END, (RAM_END - HEAP_END) / 4); // will trace regs since they now live in this function on the stack + gc_collect_end(); + uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly + + if (0) { + // print GC info + gc_info_t info; + gc_info(&info); + printf("GC@%lu %lums\n", start, ticks); + printf(" %lu total\n", info.total); + printf(" %lu : %lu\n", info.used, info.free); + printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block); + } +} + +static mp_obj_t pyb_gc(void) { + gc_collect(); + return mp_const_none; +} + +MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc); diff --git a/stm/gccollect.h b/stm/gccollect.h new file mode 100644 index 0000000000..6467ec7d21 --- /dev/null +++ b/stm/gccollect.h @@ -0,0 +1,9 @@ +#define HEAP_END (0x2001c000) // tunable +#define RAM_END (0x20020000) // fixed for chip + +extern uint32_t _ram_start; +extern uint32_t _heap_start; + +void gc_collect(void); + +MP_DECLARE_CONST_FUN_OBJ(pyb_gc_obj); diff --git a/stm/main.c b/stm/main.c index 2ce2b6dc61..4114ec1979 100644 --- a/stm/main.c +++ b/stm/main.c @@ -28,6 +28,7 @@ #include "runtime.h" #include "repl.h" #include "gc.h" +#include "gccollect.h" #include "systick.h" #include "led.h" #include "servo.h" @@ -47,8 +48,6 @@ int errno; -extern uint32_t _heap_start; - static FATFS fatfs0; void flash_error(int n) { @@ -180,6 +179,7 @@ static mp_obj_t pyb_info(void) { printf("_ebss=%p\n", &_ebss); printf("_estack=%p\n", &_estack); printf("_etext=%p\n", &_etext); + printf("_ram_start=%p\n", &_ram_start); printf("_heap_start=%p\n", &_heap_start); } @@ -455,38 +455,6 @@ bool do_file(const char *filename) { } } -#define RAM_START (0x20000000) // fixed for chip -#define HEAP_END (0x2001c000) // tunable -#define RAM_END (0x20020000) // fixed for chip - -void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end); - -void gc_collect(void) { - uint32_t start = sys_tick_counter; - gc_collect_start(); - gc_collect_root((void**)RAM_START, (((uint32_t)&_heap_start) - RAM_START) / 4); - machine_uint_t regs[10]; - gc_helper_get_regs_and_clean_stack(regs, HEAP_END); - gc_collect_root((void**)HEAP_END, (RAM_END - HEAP_END) / 4); // will trace regs since they now live in this function on the stack - gc_collect_end(); - uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly - - if (0) { - // print GC info - gc_info_t info; - gc_info(&info); - printf("GC@%lu %lums\n", start, ticks); - printf(" %lu total\n", info.total); - printf(" %lu : %lu\n", info.used, info.free); - printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block); - } -} - -mp_obj_t pyb_gc(void) { - gc_collect(); - return mp_const_none; -} - mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args) { //assert(1 <= n_args && n_args <= 2); @@ -655,7 +623,7 @@ soft_reset: rt_store_attr(m, MP_QSTR_source_dir, rt_make_function_n(1, pyb_source_dir)); rt_store_attr(m, MP_QSTR_main, rt_make_function_n(1, pyb_main)); rt_store_attr(m, MP_QSTR_sync, rt_make_function_n(0, pyb_sync)); - rt_store_attr(m, MP_QSTR_gc, rt_make_function_n(0, pyb_gc)); + rt_store_attr(m, MP_QSTR_gc, (mp_obj_t)&pyb_gc_obj); rt_store_attr(m, MP_QSTR_delay, rt_make_function_n(1, pyb_delay)); #if MICROPY_HW_HAS_SWITCH rt_store_attr(m, MP_QSTR_switch, (mp_obj_t)&pyb_switch_obj); diff --git a/stm/stm32f405.ld b/stm/stm32f405.ld index c19e6a1c19..fbfc584f9d 100644 --- a/stm/stm32f405.ld +++ b/stm/stm32f405.ld @@ -69,6 +69,7 @@ SECTIONS { . = ALIGN(4); _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ + _ram_start = .; /* create a global symbol at ram start for garbage collector */ *(.data) /* .data sections */ *(.data*) /* .data* sections */