Skip to content

Commit 3f69aca

Browse files
committed
Make stm use garbage collector.
1 parent dcced92 commit 3f69aca

7 files changed

Lines changed: 54 additions & 11 deletions

File tree

stm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SRC_S = \
3232

3333
PY_O = \
3434
nlrthumb.o \
35+
gc.o \
3536
malloc.o \
3637
qstr.o \
3738
vstr.o \

stm/main.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "std.h"
77

88
#include "misc.h"
9+
#include "mpyconfig.h"
10+
#include "gc.h"
911
#include "systick.h"
1012
#include "led.h"
1113
#include "lcd.h"
@@ -14,6 +16,8 @@
1416
#include "usb.h"
1517
#include "ff.h"
1618

19+
extern uint32_t _heap_start;
20+
1721
static void impl02_c_version() {
1822
int x = 0;
1923
while (x < 400) {
@@ -163,7 +167,6 @@ static void board_info() {
163167
extern void *_ebss;
164168
extern void *_estack;
165169
extern void *_etext;
166-
extern void *_heap_start;
167170
printf("_sidata=%p\n", &_sidata);
168171
printf("_sdata=%p\n", &_sdata);
169172
printf("_edata=%p\n", &_edata);
@@ -263,6 +266,14 @@ void do_repl() {
263266
}
264267
}
265268

269+
void gc_collect() {
270+
gc_collect_start();
271+
gc_collect_root((void**)0x20000000, (((uint32_t)&_heap_start) - 0x20000000) / 4);
272+
gc_collect_root((void**)(0x20000000 + 0x18000), (0x20000 - 0x18000) / 4);
273+
// TODO registers
274+
gc_collect_end();
275+
}
276+
266277
int main() {
267278
// TODO disable JTAG
268279

@@ -284,6 +295,10 @@ int main() {
284295
lcd_init();
285296
storage_init();
286297

298+
// GC init
299+
gc_init(&_heap_start, (void*)(0x20000000 + 0x18000));
300+
sys_tick_delay_ms(2000);
301+
287302
// Python init
288303
qstr_init();
289304
rt_init();

stm/malloc0.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <stdint.h>
22
#include "std.h"
3+
#include "mpyconfig.h"
4+
#include "gc.h"
35

6+
#if 0
47
static uint32_t mem = 0;
58

69
void *malloc(size_t n) {
@@ -20,6 +23,12 @@ void *malloc(size_t n) {
2023
void free(void *ptr) {
2124
}
2225

26+
void *realloc(void *ptr, size_t n) {
27+
return malloc(n);
28+
}
29+
30+
#endif
31+
2332
void *calloc(size_t sz, size_t n) {
2433
char *ptr = malloc(sz * n);
2534
for (int i = 0; i < sz * n; i++) {
@@ -28,8 +37,15 @@ void *calloc(size_t sz, size_t n) {
2837
return ptr;
2938
}
3039

40+
void *malloc(size_t n) {
41+
return gc_alloc(n);
42+
}
43+
44+
void free(void *ptr) {
45+
}
46+
3147
void *realloc(void *ptr, size_t n) {
32-
return malloc(n);
48+
return gc_realloc(ptr, n);
3349
}
3450

3551
void __assert_func() {

stm/mpyconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
// type definitions for the specific machine
1010

11+
#define BYTES_PER_WORD (4)
12+
1113
typedef int32_t machine_int_t; // must be pointer size
1214
typedef uint32_t machine_uint_t; // must be pointer size
1315
typedef void *machine_ptr_t; // must be of pointer size

stm/printf.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include <stdarg.h>
22
#include "std.h"
3+
#include "misc.h"
4+
#include "lcd.h"
5+
#include "usb.h"
36

47
#define PF_FLAG_LEFT_ADJUST (0x01)
58
#define PF_FLAG_SHOW_SIGN (0x02)
@@ -208,13 +211,13 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) {
208211
return chrs;
209212
}
210213

211-
void lcd_print_strn(const char *str, unsigned int len);
212-
void usb_vcp_send_strn(const char* str, int len);
213-
214214
void stdout_print_strn(void *data, const char *str, unsigned int len) {
215215
// send stdout to LCD and USB CDC VCP
216-
lcd_print_strn(str, len);
217-
usb_vcp_send_strn(str, len);
216+
if (usb_vcp_is_enabled()) {
217+
usb_vcp_send_strn(str, len);
218+
} else {
219+
lcd_print_strn(str, len);
220+
}
218221
}
219222

220223
static const pfenv_t pfenv_stdout = {0, stdout_print_strn};

stm/usb.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
extern CDC_IF_Prop_TypeDef VCP_fops;
1414

15-
int is_enabled = 0;
1615
USB_OTG_CORE_HANDLE USB_OTG_dev;
17-
char rx_buf[64];
18-
int rx_buf_in;
19-
int rx_buf_out;
16+
17+
static int is_enabled = 0;
18+
static char rx_buf[64];
19+
static int rx_buf_in;
20+
static int rx_buf_out;
2021

2122
void usb_init() {
2223
USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
@@ -25,6 +26,10 @@ void usb_init() {
2526
is_enabled = 1;
2627
}
2728

29+
bool usb_vcp_is_enabled() {
30+
return is_enabled;
31+
}
32+
2833
void usb_vcp_receive(const char *buf, uint32_t len) {
2934
if (is_enabled) {
3035
for (int i = 0; i < len; i++) {

stm/usb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
void usb_init();
2+
bool usb_vcp_is_enabled();
23
int usb_vcp_rx_any();
34
char usb_vcp_rx_get();
45
void usb_vcp_send_str(const char* str);

0 commit comments

Comments
 (0)