Skip to content

Commit 659c19c

Browse files
committed
Merge pull request adafruit#342 from dhylands/stmhal-repl
REPL working on UART6 with STMHAL
2 parents 19438fd + f14b92b commit 659c19c

29 files changed

Lines changed: 2793 additions & 125 deletions

stmhal/Makefile

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CROSS_COMPILE = arm-none-eabi-
1919

2020
INC = -I.
2121
INC += -I$(PY_SRC)
22+
INC += -I$(CMSIS_DIR)
2223
INC += -I$(CMSIS_DIR)/inc
2324
INC += -I$(CMSIS_DIR)/devinc
2425
INC += -I$(HAL_DIR)/inc
@@ -54,28 +55,30 @@ LIBS =
5455

5556
SRC_C = \
5657
main.c \
58+
string0.c \
5759
system_stm32f4xx.c \
5860
stm32f4xx_it.c \
5961
stm32f4xx_hal_msp.c \
62+
systick.c \
63+
led.c \
64+
pin.c \
65+
usart.c \
66+
printf.c \
67+
math.c \
68+
malloc0.c \
69+
gccollect.c \
70+
pyexec.c \
71+
pybmodule.c \
72+
import.c \
73+
lexerfatfs.c \
6074

61-
# printf.c \
62-
# math.c \
63-
# string0.c \
64-
# malloc0.c \
65-
# systick.c \
6675
# pendsv.c \
67-
# gccollect.c \
68-
# lexerfatfs.c \
69-
# import.c \
70-
# pyexec.c \
71-
# led.c \
7276
# gpio.c \
7377
# lcd.c \
7478
# servo.c \
7579
# flash.c \
7680
# storage.c \
7781
# accel.c \
78-
# usart.c \
7982
# usb.c \
8083
# timer.c \
8184
# audio.c \
@@ -84,24 +87,23 @@ SRC_C = \
8487
# adc.c \
8588
# rtc.c \
8689
# file.c \
87-
# pin.c \
8890
# pin_named_pins.c \
8991
# pin_map.c \
9092
# exti.c \
9193
# usrsw.c \
92-
# pybmodule.c \
9394
# pybwlan.c \
9495
9596
SRC_S = \
9697
startup_stm32f40xx.s \
97-
98-
# gchelper.s \
98+
gchelper.s \
9999

100100
SRC_HAL = $(addprefix $(HAL_DIR)/src/,\
101101
stm32f4xx_hal.c \
102102
stm32f4xx_hal_cortex.c \
103-
stm32f4xx_hal_rcc.c \
103+
stm32f4xx_hal_dma.c \
104104
stm32f4xx_hal_gpio.c \
105+
stm32f4xx_hal_rcc.c \
106+
stm32f4xx_hal_uart.c \
105107
)
106108

107109
SRC_STMPERIPH = $(addprefix $(STMPERIPH_DIR)/,\
@@ -182,7 +184,7 @@ SRC_CC3K = $(addprefix $(CC3K_DIR)/,\
182184
)
183185

184186
OBJ =
185-
#OBJ += $(PY_O)
187+
OBJ += $(PY_O)
186188
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
187189
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
188190
OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o))
@@ -191,7 +193,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o))
191193
#OBJ += $(addprefix $(BUILD)/, $(SRC_STMUSBH:.c=.o))
192194
#OBJ += $(addprefix $(BUILD)/, $(SRC_FATFS:.c=.o))
193195
#OBJ += $(addprefix $(BUILD)/, $(SRC_CC3K:.c=.o))
194-
#OBJ += $(BUILD)/pins_$(BOARD).o
196+
OBJ += $(BUILD)/pins_$(BOARD).o
195197

196198
all: $(BUILD)/flash.dfu
197199

@@ -222,7 +224,12 @@ GEN_PINS_HDR = $(BUILD)/pins.h
222224
# any of the objects. The normal dependency generation will deal with the
223225
# case when pins.h is modified. But when it doesn't exist, we don't know
224226
# which source files might need it.
225-
#$(OBJ): | $(BUILD)/pins.h
227+
$(OBJ): | $(BUILD)/pins.h
228+
229+
# temp hack
230+
$(PY_BUILD):
231+
mkdir -p $@
232+
$(OBJ): | $(PY_BUILD) $(PY_BUILD)/qstrdefs.generated.h
226233

227234
# Use a pattern rule here so that make will only call make-pins.py once to make
228235
# both pins_$(BOARD).c and pins.h

stmhal/boards/stm32f4xx-prefix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <stdio.h>
44
#include <stdint.h>
5-
#include <stm32f4xx.h>
5+
#include <stm32f4xx_hal.h>
66

77
#include "misc.h"
88
#include "mpconfig.h"

stmhal/gccollect.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
3+
#include <stm32f4xx_hal.h>
4+
5+
#include "misc.h"
6+
#include "mpconfig.h"
7+
#include "qstr.h"
8+
#include "obj.h"
9+
#include "gc.h"
10+
#include "gccollect.h"
11+
12+
machine_uint_t gc_helper_get_regs_and_sp(machine_uint_t *regs);
13+
14+
// obsolete
15+
// void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end);
16+
17+
void gc_collect(void) {
18+
// get current time, in case we want to time the GC
19+
uint32_t start = HAL_GetTick();
20+
21+
// start the GC
22+
gc_collect_start();
23+
24+
// scan everything in RAM before the heap
25+
// this includes the data and bss segments
26+
// TODO possibly don't need to scan data, since all pointers should start out NULL and be in bss
27+
gc_collect_root((void**)&_ram_start, ((uint32_t)&_ebss - (uint32_t)&_ram_start) / sizeof(uint32_t));
28+
29+
// get the registers and the sp
30+
machine_uint_t regs[10];
31+
machine_uint_t sp = gc_helper_get_regs_and_sp(regs);
32+
33+
// trace the stack, including the registers (since they live on the stack in this function)
34+
gc_collect_root((void**)sp, ((uint32_t)&_ram_end - sp) / sizeof(uint32_t));
35+
36+
// end the GC
37+
gc_collect_end();
38+
39+
if (0) {
40+
// print GC info
41+
uint32_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly
42+
gc_info_t info;
43+
gc_info(&info);
44+
printf("GC@%lu %lums\n", start, ticks);
45+
printf(" %lu total\n", info.total);
46+
printf(" %lu : %lu\n", info.used, info.free);
47+
printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
48+
}
49+
}
50+
51+
static mp_obj_t pyb_gc(void) {
52+
gc_collect();
53+
return mp_const_none;
54+
}
55+
56+
MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);

stmhal/gccollect.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// variables defining memory layout
2+
// (these probably belong somewhere else...)
3+
extern uint32_t _etext;
4+
extern uint32_t _sidata;
5+
extern uint32_t _ram_start;
6+
extern uint32_t _sdata;
7+
extern uint32_t _edata;
8+
extern uint32_t _sbss;
9+
extern uint32_t _ebss;
10+
extern uint32_t _heap_start;
11+
extern uint32_t _heap_end;
12+
extern uint32_t _estack;
13+
extern uint32_t _ram_end;
14+
15+
void gc_collect(void);
16+
17+
MP_DECLARE_CONST_FUN_OBJ(pyb_gc_obj);

stmhal/gchelper.s

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.syntax unified
2+
.cpu cortex-m4
3+
.thumb
4+
.text
5+
.align 2
6+
7+
@ uint gc_helper_get_regs_and_sp(r0=uint regs[10])
8+
.global gc_helper_get_regs_and_sp
9+
.thumb
10+
.thumb_func
11+
.type gc_helper_get_regs_and_sp, %function
12+
gc_helper_get_regs_and_sp:
13+
@ store registers into given array
14+
str r4, [r0], #4
15+
str r5, [r0], #4
16+
str r6, [r0], #4
17+
str r7, [r0], #4
18+
str r8, [r0], #4
19+
str r9, [r0], #4
20+
str r10, [r0], #4
21+
str r11, [r0], #4
22+
str r12, [r0], #4
23+
str r13, [r0], #4
24+
25+
@ return the sp
26+
mov r0, sp
27+
bx lr
28+
29+
30+
@ this next function is now obsolete
31+
32+
.size gc_helper_get_regs_and_clean_stack, .-gc_helper_get_regs_and_clean_stack
33+
@ void gc_helper_get_regs_and_clean_stack(r0=uint regs[10], r1=heap_end)
34+
.global gc_helper_get_regs_and_clean_stack
35+
.thumb
36+
.thumb_func
37+
.type gc_helper_get_regs_and_clean_stack, %function
38+
gc_helper_get_regs_and_clean_stack:
39+
@ store registers into given array
40+
str r4, [r0], #4
41+
str r5, [r0], #4
42+
str r6, [r0], #4
43+
str r7, [r0], #4
44+
str r8, [r0], #4
45+
str r9, [r0], #4
46+
str r10, [r0], #4
47+
str r11, [r0], #4
48+
str r12, [r0], #4
49+
str r13, [r0], #4
50+
51+
@ clean the stack from given pointer up to current sp
52+
movs r0, #0
53+
mov r2, sp
54+
b.n .entry
55+
.loop:
56+
str r0, [r1], #4
57+
.entry:
58+
cmp r1, r2
59+
bcc.n .loop
60+
bx lr
61+
62+
.size gc_helper_get_regs_and_clean_stack, .-gc_helper_get_regs_and_clean_stack

stmhal/import.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdint.h>
2+
3+
#include "misc.h"
4+
#include "mpconfig.h"
5+
#include "qstr.h"
6+
#include "lexer.h"
7+
#if 0
8+
#include "ff.h"
9+
#endif
10+
11+
mp_import_stat_t mp_import_stat(const char *path) {
12+
#if 0
13+
FILINFO fno;
14+
FRESULT res = f_stat(path, &fno);
15+
if (res == FR_OK) {
16+
if ((fno.fattrib & AM_DIR) != 0) {
17+
return MP_IMPORT_STAT_DIR;
18+
} else {
19+
return MP_IMPORT_STAT_FILE;
20+
}
21+
}
22+
#endif
23+
return MP_IMPORT_STAT_NO_EXIST;
24+
}

0 commit comments

Comments
 (0)