4848int errno ;
4949
5050extern uint32_t _heap_start ;
51- extern uint32_t _heap_end ;
5251
5352static FATFS fatfs0 ;
5453
@@ -456,12 +455,21 @@ bool do_file(const char *filename) {
456455 }
457456}
458457
459- mp_obj_t pyb_gc (void ) {
460- uint32_t start ,ticks ;
458+ #define RAM_START (0x20000000) // fixed for chip
459+ #define HEAP_END (0x2001c000) // tunable
460+ #define RAM_END (0x20020000) // fixed for chip
461461
462- start = sys_tick_counter ;
463- gc_collect ();
464- ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
462+ void gc_helper_get_regs_and_clean_stack (machine_uint_t * regs , machine_uint_t heap_end );
463+
464+ void gc_collect (void ) {
465+ uint32_t start = sys_tick_counter ;
466+ gc_collect_start ();
467+ gc_collect_root ((void * * )RAM_START , (((uint32_t )& _heap_start ) - RAM_START ) / 4 );
468+ machine_uint_t regs [10 ];
469+ gc_helper_get_regs_and_clean_stack (regs , HEAP_END );
470+ gc_collect_root ((void * * )HEAP_END , (RAM_END - HEAP_END ) / 4 ); // will trace regs since they now live in this function on the stack
471+ gc_collect_end ();
472+ uint32_t ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
465473
466474 if (0 ) {
467475 // print GC info
@@ -472,7 +480,10 @@ mp_obj_t pyb_gc(void) {
472480 printf (" %lu : %lu\n" , info .used , info .free );
473481 printf (" 1=%lu 2=%lu m=%lu\n" , info .num_1block , info .num_2block , info .max_block );
474482 }
483+ }
475484
485+ mp_obj_t pyb_gc (void ) {
486+ gc_collect ();
476487 return mp_const_none ;
477488}
478489
@@ -598,7 +609,7 @@ int main(void) {
598609soft_reset :
599610
600611 // GC init
601- gc_init (& _heap_start , & _heap_end );
612+ gc_init (& _heap_start , ( void * ) HEAP_END );
602613
603614 // Micro Python init
604615 qstr_init ();
0 commit comments