Skip to content

Commit 449dd0a

Browse files
committed
stm: Put gc_collect code in separate file; define _ram_start in .ld.
To partly address Issue adafruit#220.
1 parent 2259e62 commit 449dd0a

5 files changed

Lines changed: 53 additions & 35 deletions

File tree

stm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ SRC_C = \
4040
string0.c \
4141
malloc0.c \
4242
systick.c \
43+
gccollect.c \
4344
lexerfatfs.c \
4445
led.c \
4546
lcd.c \

stm/gccollect.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
3+
#include "misc.h"
4+
#include "mpconfig.h"
5+
#include "qstr.h"
6+
#include "obj.h"
7+
#include "gc.h"
8+
#include "gccollect.h"
9+
#include "systick.h"
10+
11+
void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end);
12+
13+
void gc_collect(void) {
14+
uint32_t start = sys_tick_counter;
15+
gc_collect_start();
16+
gc_collect_root((void**)&_ram_start, ((uint32_t)&_heap_start - (uint32_t)&_ram_start) / 4);
17+
machine_uint_t regs[10];
18+
gc_helper_get_regs_and_clean_stack(regs, HEAP_END);
19+
gc_collect_root((void**)HEAP_END, (RAM_END - HEAP_END) / 4); // will trace regs since they now live in this function on the stack
20+
gc_collect_end();
21+
uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
22+
23+
if (0) {
24+
// print GC info
25+
gc_info_t info;
26+
gc_info(&info);
27+
printf("GC@%lu %lums\n", start, ticks);
28+
printf(" %lu total\n", info.total);
29+
printf(" %lu : %lu\n", info.used, info.free);
30+
printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
31+
}
32+
}
33+
34+
static mp_obj_t pyb_gc(void) {
35+
gc_collect();
36+
return mp_const_none;
37+
}
38+
39+
MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);

stm/gccollect.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#define HEAP_END (0x2001c000) // tunable
2+
#define RAM_END (0x20020000) // fixed for chip
3+
4+
extern uint32_t _ram_start;
5+
extern uint32_t _heap_start;
6+
7+
void gc_collect(void);
8+
9+
MP_DECLARE_CONST_FUN_OBJ(pyb_gc_obj);

stm/main.c

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "runtime.h"
2929
#include "repl.h"
3030
#include "gc.h"
31+
#include "gccollect.h"
3132
#include "systick.h"
3233
#include "led.h"
3334
#include "servo.h"
@@ -47,8 +48,6 @@
4748

4849
int errno;
4950

50-
extern uint32_t _heap_start;
51-
5251
static FATFS fatfs0;
5352

5453
void flash_error(int n) {
@@ -180,6 +179,7 @@ static mp_obj_t pyb_info(void) {
180179
printf("_ebss=%p\n", &_ebss);
181180
printf("_estack=%p\n", &_estack);
182181
printf("_etext=%p\n", &_etext);
182+
printf("_ram_start=%p\n", &_ram_start);
183183
printf("_heap_start=%p\n", &_heap_start);
184184
}
185185

@@ -455,38 +455,6 @@ bool do_file(const char *filename) {
455455
}
456456
}
457457

458-
#define RAM_START (0x20000000) // fixed for chip
459-
#define HEAP_END (0x2001c000) // tunable
460-
#define RAM_END (0x20020000) // fixed for chip
461-
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
473-
474-
if (0) {
475-
// print GC info
476-
gc_info_t info;
477-
gc_info(&info);
478-
printf("GC@%lu %lums\n", start, ticks);
479-
printf(" %lu total\n", info.total);
480-
printf(" %lu : %lu\n", info.used, info.free);
481-
printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
482-
}
483-
}
484-
485-
mp_obj_t pyb_gc(void) {
486-
gc_collect();
487-
return mp_const_none;
488-
}
489-
490458
mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args) {
491459
//assert(1 <= n_args && n_args <= 2);
492460

@@ -655,7 +623,7 @@ int main(void) {
655623
rt_store_attr(m, MP_QSTR_source_dir, rt_make_function_n(1, pyb_source_dir));
656624
rt_store_attr(m, MP_QSTR_main, rt_make_function_n(1, pyb_main));
657625
rt_store_attr(m, MP_QSTR_sync, rt_make_function_n(0, pyb_sync));
658-
rt_store_attr(m, MP_QSTR_gc, rt_make_function_n(0, pyb_gc));
626+
rt_store_attr(m, MP_QSTR_gc, (mp_obj_t)&pyb_gc_obj);
659627
rt_store_attr(m, MP_QSTR_delay, rt_make_function_n(1, pyb_delay));
660628
#if MICROPY_HW_HAS_SWITCH
661629
rt_store_attr(m, MP_QSTR_switch, (mp_obj_t)&pyb_switch_obj);

stm/stm32f405.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ SECTIONS
6969
{
7070
. = ALIGN(4);
7171
_sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */
72+
_ram_start = .; /* create a global symbol at ram start for garbage collector */
7273
*(.data) /* .data sections */
7374
*(.data*) /* .data* sections */
7475

0 commit comments

Comments
 (0)