You've already forked Core2forAWS-MicroPython
mirror of
https://github.com/m5stack/Core2forAWS-MicroPython.git
synced 2026-05-20 10:30:31 -07:00
cortex-m3-qemu: refactor the port.
Switch from CodeSourcery to ARM GCC and clean-up some stale files, also copy `main.c` and `mpconfigport.h` from bare-arm.
This commit is contained in:
+15
-16
@@ -10,7 +10,8 @@ include ../py/py.mk
|
||||
CROSS_COMPILE = arm-none-eabi-
|
||||
|
||||
CFLAGS_CORTEX_M3 = -mthumb -mcpu=cortex-m3
|
||||
CFLAGS = -I. -I$(PY_SRC) -Wall -ansi -std=gnu99 $(CFLAGS_CORTEX_M3) $(COPT)
|
||||
CFLAGS = -I. -I$(PY_SRC) -Wall -ansi -std=gnu99 $(CFLAGS_CORTEX_M3) $(COPT) \
|
||||
-flto -ffunction-sections -fdata-sections
|
||||
|
||||
#Debugging/Optimization
|
||||
ifeq ($(DEBUG), 1)
|
||||
@@ -20,35 +21,33 @@ else
|
||||
COPT += -Os -DNDEBUG
|
||||
endif
|
||||
|
||||
#LDFLAGS = --nostdlib -T stm32f405.ld -Map=$(@:.elf=.map) --cref
|
||||
LDFLAGS = -lm -T generic-m-hosted.ld # -Map=$(@:.elf=.map) --cref
|
||||
LIBS =
|
||||
|
||||
# uncomment this if you want libgcc
|
||||
#LIBS += $(shell $(CC) -print-libgcc-file-name)
|
||||
## With CoudeSourcery it's actually a little different, you just need `-T generic-m-hosted.ld`.
|
||||
## Although for some reason `$(LD)` will not find that linker script, it works with `$(CC)`.
|
||||
## It turns out that this is specific to CoudeSourcery, and ARM version of GCC ships something
|
||||
## else instead and according to the following files, this is what we need to pass to `$(CC).
|
||||
## - gcc-arm-none-eabi-4_8-2014q1/share/gcc-arm-none-eabi/samples/src/makefile.conf
|
||||
## - gcc-arm-none-eabi-4_8-2014q1/share/gcc-arm-none-eabi/samples/src/qemu/Makefile
|
||||
LDFLAGS= --specs=nano.specs --specs=rdimon.specs -Wl,--gc-sections -Wl,-Map=$(@:.elf=.map)
|
||||
|
||||
SRC_C = \
|
||||
help.c \
|
||||
math.c \
|
||||
|
||||
#gccollect.c \
|
||||
#pyexec.c \
|
||||
main.c \
|
||||
|
||||
SRC_S = \
|
||||
gchelper.s \
|
||||
|
||||
OBJ =
|
||||
OBJ += $(PY_O)
|
||||
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
|
||||
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
|
||||
|
||||
all: $(BUILD)/flash.elf
|
||||
all: run
|
||||
|
||||
run: $(BUILD)/flash.elf
|
||||
|
||||
run:
|
||||
qemu-system-arm -cpu cortex-m3 -nographic -monitor null -serial null -semihosting -kernel $(BUILD)/flash.elf
|
||||
|
||||
## `$(LD)` doesn't seem to like `--specs` for some reason, but we can just use `$(CC)` here.
|
||||
$(BUILD)/flash.elf: $(OBJ)
|
||||
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) main.c -o $@ $(OBJ) $(LIBS)
|
||||
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
|
||||
$(Q)$(SIZE) $@
|
||||
|
||||
include ../py/mkrules.mk
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
//#include <stm32f4xx_hal.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "qstr.h"
|
||||
#include "obj.h"
|
||||
#include "gc.h"
|
||||
#include "gccollect.h"
|
||||
|
||||
machine_uint_t gc_helper_get_regs_and_sp(machine_uint_t *regs);
|
||||
|
||||
// obsolete
|
||||
// void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end);
|
||||
|
||||
void gc_collect(void) {
|
||||
// get current time, in case we want to time the GC
|
||||
uint32_t start = HAL_GetTick();
|
||||
|
||||
// start the GC
|
||||
gc_collect_start();
|
||||
|
||||
// scan everything in RAM before the heap
|
||||
// this includes the data and bss segments
|
||||
// TODO possibly don't need to scan data, since all pointers should start out NULL and be in bss
|
||||
gc_collect_root((void**)&_ram_start, ((uint32_t)&_ebss - (uint32_t)&_ram_start) / sizeof(uint32_t));
|
||||
|
||||
// get the registers and the sp
|
||||
machine_uint_t regs[10];
|
||||
machine_uint_t sp = gc_helper_get_regs_and_sp(regs);
|
||||
|
||||
// trace the stack, including the registers (since they live on the stack in this function)
|
||||
gc_collect_root((void**)sp, ((uint32_t)&_ram_end - sp) / sizeof(uint32_t));
|
||||
|
||||
// end the GC
|
||||
gc_collect_end();
|
||||
|
||||
if (0) {
|
||||
// print GC info
|
||||
uint32_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly
|
||||
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);
|
||||
@@ -1,17 +0,0 @@
|
||||
// variables defining memory layout
|
||||
// (these probably belong somewhere else...)
|
||||
extern uint32_t _etext;
|
||||
extern uint32_t _sidata;
|
||||
extern uint32_t _ram_start;
|
||||
extern uint32_t _sdata;
|
||||
extern uint32_t _edata;
|
||||
extern uint32_t _sbss;
|
||||
extern uint32_t _ebss;
|
||||
extern uint32_t _heap_start;
|
||||
extern uint32_t _heap_end;
|
||||
extern uint32_t _estack;
|
||||
extern uint32_t _ram_end;
|
||||
|
||||
void gc_collect(void);
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ(pyb_gc_obj);
|
||||
@@ -1,62 +0,0 @@
|
||||
.syntax unified
|
||||
.cpu cortex-m4
|
||||
.thumb
|
||||
.text
|
||||
.align 2
|
||||
|
||||
@ uint gc_helper_get_regs_and_sp(r0=uint regs[10])
|
||||
.global gc_helper_get_regs_and_sp
|
||||
.thumb
|
||||
.thumb_func
|
||||
.type gc_helper_get_regs_and_sp, %function
|
||||
gc_helper_get_regs_and_sp:
|
||||
@ store registers into given array
|
||||
str r4, [r0], #4
|
||||
str r5, [r0], #4
|
||||
str r6, [r0], #4
|
||||
str r7, [r0], #4
|
||||
str r8, [r0], #4
|
||||
str r9, [r0], #4
|
||||
str r10, [r0], #4
|
||||
str r11, [r0], #4
|
||||
str r12, [r0], #4
|
||||
str r13, [r0], #4
|
||||
|
||||
@ return the sp
|
||||
mov r0, sp
|
||||
bx lr
|
||||
|
||||
|
||||
@ this next function is now obsolete
|
||||
|
||||
.size gc_helper_get_regs_and_clean_stack, .-gc_helper_get_regs_and_clean_stack
|
||||
@ void gc_helper_get_regs_and_clean_stack(r0=uint regs[10], r1=heap_end)
|
||||
.global gc_helper_get_regs_and_clean_stack
|
||||
.thumb
|
||||
.thumb_func
|
||||
.type gc_helper_get_regs_and_clean_stack, %function
|
||||
gc_helper_get_regs_and_clean_stack:
|
||||
@ store registers into given array
|
||||
str r4, [r0], #4
|
||||
str r5, [r0], #4
|
||||
str r6, [r0], #4
|
||||
str r7, [r0], #4
|
||||
str r8, [r0], #4
|
||||
str r9, [r0], #4
|
||||
str r10, [r0], #4
|
||||
str r11, [r0], #4
|
||||
str r12, [r0], #4
|
||||
str r13, [r0], #4
|
||||
|
||||
@ clean the stack from given pointer up to current sp
|
||||
movs r0, #0
|
||||
mov r2, sp
|
||||
b.n .entry
|
||||
.loop:
|
||||
str r0, [r1], #4
|
||||
.entry:
|
||||
cmp r1, r2
|
||||
bcc.n .loop
|
||||
bx lr
|
||||
|
||||
.size gc_helper_get_regs_and_clean_stack, .-gc_helper_get_regs_and_clean_stack
|
||||
@@ -1,95 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "nlr.h"
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "qstr.h"
|
||||
#include "obj.h"
|
||||
|
||||
STATIC const char *help_text =
|
||||
"Welcome to Micro Python!\n"
|
||||
"\n"
|
||||
"For online help please visit http://micropython.org/help/.\n"
|
||||
"\n"
|
||||
"Specific commands for the board:\n"
|
||||
" pyb.info() -- print some general information\n"
|
||||
" pyb.gc() -- run the garbage collector\n"
|
||||
" pyb.repl_info(val) -- enable/disable printing of info after each command\n"
|
||||
" pyb.delay(n) -- wait for n milliseconds\n"
|
||||
" pyb.udelay(n) -- wait for n microseconds\n"
|
||||
" pyb.switch() -- return True/False if switch pressed or not\n"
|
||||
" pyb.switch(f) -- call the given function when the switch is pressed\n"
|
||||
" pyb.Led(n) -- create Led object for LED n (n=1,2,3,4)\n"
|
||||
" Led methods: on(), off(), toggle(), intensity(<n>)\n"
|
||||
" pyb.Servo(n) -- create Servo object for servo n (n=1,2,3,4)\n"
|
||||
" Servo methods: calibrate(...), pulse_width([p]), angle([x, [t]]), speed([x, [t]])\n"
|
||||
" pyb.Accel() -- create an Accelerometer object\n"
|
||||
" Accelerometer methods: x(), y(), z(), tilt(), filtered_xyz()\n"
|
||||
" pyb.rng() -- get a 30-bit hardware random number\n"
|
||||
" pyb.gpio_in(port, [m]) -- set IO port to input, mode m\n"
|
||||
" pyb.gpio_out(port, [m]) -- set IO port to output, mode m\n"
|
||||
" pyb.gpio(port) -- get digital port value\n"
|
||||
" pyb.gpio(port, val) -- set digital port value, True or False, 1 or 0\n"
|
||||
" pyb.ADC(port) -- make an analog port object\n"
|
||||
" ADC methods: read()\n"
|
||||
"\n"
|
||||
"Ports are numbered X1-X12, X17-X22, Y1-Y12, or by their MCU name\n"
|
||||
"Port input modes are: pyb.PULL_NONE, pyb.PULL_UP, pyb.PULL_DOWN\n"
|
||||
"Port output modes are: pyb.PUSH_PULL, pyb.OPEN_DRAIN\n"
|
||||
"\n"
|
||||
"Control commands:\n"
|
||||
" CTRL-A -- on a blank line, enter raw REPL mode\n"
|
||||
" CTRL-B -- on a blank line, enter normal REPL mode\n"
|
||||
" CTRL-C -- interrupt a running program\n"
|
||||
" CTRL-D -- on a blank line, do a soft reset of the board\n"
|
||||
"\n"
|
||||
"For further help on a specific object, type help(obj)\n"
|
||||
;
|
||||
|
||||
STATIC void pyb_help_print_info_about_object(mp_obj_t name_o, mp_obj_t value) {
|
||||
printf(" ");
|
||||
mp_obj_print(name_o, PRINT_STR);
|
||||
printf(" -- ");
|
||||
mp_obj_print(value, PRINT_STR);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
STATIC mp_obj_t pyb_help(uint n_args, const mp_obj_t *args) {
|
||||
if (n_args == 0) {
|
||||
// print a general help message
|
||||
printf("%s", help_text);
|
||||
|
||||
} else {
|
||||
// try to print something sensible about the given object
|
||||
|
||||
printf("object ");
|
||||
mp_obj_print(args[0], PRINT_STR);
|
||||
printf(" is of type %s\n", mp_obj_get_type_str(args[0]));
|
||||
|
||||
mp_map_t *map = NULL;
|
||||
if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
|
||||
map = mp_obj_dict_get_map(mp_obj_module_get_globals(args[0]));
|
||||
} else {
|
||||
mp_obj_type_t *type;
|
||||
if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
|
||||
type = args[0];
|
||||
} else {
|
||||
type = mp_obj_get_type(args[0]);
|
||||
}
|
||||
if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
|
||||
map = mp_obj_dict_get_map(type->locals_dict);
|
||||
}
|
||||
}
|
||||
if (map != NULL) {
|
||||
for (uint i = 0; i < map->alloc; i++) {
|
||||
if (map->table[i].key != MP_OBJ_NULL) {
|
||||
pyb_help_print_info_about_object(map->table[i].key, map->table[i].value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, pyb_help);
|
||||
+24
-71
@@ -1,18 +1,12 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "nlr.h"
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "qstr.h"
|
||||
#include "lexer.h"
|
||||
#include "lexerunix.h"
|
||||
#include "parse.h"
|
||||
#include "obj.h"
|
||||
#include "parsehelper.h"
|
||||
@@ -20,37 +14,15 @@
|
||||
#include "runtime0.h"
|
||||
#include "runtime.h"
|
||||
#include "repl.h"
|
||||
#include "gc.h"
|
||||
|
||||
mp_obj_t mem_info(void) {
|
||||
printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t qstr_info(void) {
|
||||
uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
|
||||
qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
|
||||
printf("qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
|
||||
void do_str(const char *src) {
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
|
||||
if (lex == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (0) {
|
||||
// just tokenise
|
||||
while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
|
||||
mp_token_show(mp_lexer_cur(lex));
|
||||
mp_lexer_to_next(lex);
|
||||
}
|
||||
mp_lexer_free(lex);
|
||||
return;
|
||||
}
|
||||
|
||||
mp_parse_error_kind_t parse_error_kind;
|
||||
mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
|
||||
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT, &parse_error_kind);
|
||||
|
||||
if (pn == MP_PARSE_NODE_NULL) {
|
||||
// parse error
|
||||
@@ -59,23 +31,17 @@ static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
|
||||
return;
|
||||
}
|
||||
|
||||
// parse okay
|
||||
qstr source_name = mp_lexer_source_name(lex);
|
||||
mp_lexer_free(lex);
|
||||
|
||||
/*
|
||||
printf("----------------\n");
|
||||
mp_parse_node_print(pn, 0);
|
||||
printf("----------------\n");
|
||||
*/
|
||||
|
||||
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_ASM_THUMB, is_repl);
|
||||
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
|
||||
mp_parse_node_free(pn);
|
||||
|
||||
if (module_fun == mp_const_none) {
|
||||
// compile error
|
||||
return;
|
||||
}
|
||||
|
||||
// execute it
|
||||
nlr_buf_t nlr;
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
mp_call_function_0(module_fun);
|
||||
@@ -86,42 +52,29 @@ static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
|
||||
}
|
||||
}
|
||||
|
||||
mp_import_stat_t mp_import_stat(const char *path) {
|
||||
struct stat st;
|
||||
if (stat(path, &st) == 0) {
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
return MP_IMPORT_STAT_DIR;
|
||||
} else if (S_ISREG(st.st_mode)) {
|
||||
return MP_IMPORT_STAT_FILE;
|
||||
}
|
||||
}
|
||||
return MP_IMPORT_STAT_NO_EXIST;
|
||||
int main(int argc, char **argv) {
|
||||
qstr_init();
|
||||
mp_init();
|
||||
do_str("print('hello world!')");
|
||||
mp_deinit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void do_str(const char *str) {
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
|
||||
execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
|
||||
}
|
||||
|
||||
void nlr_jump_fail(void *val) {
|
||||
printf("FATAL: uncaught exception %p\n", val);
|
||||
//__fatal_error("");
|
||||
void gc_collect(void) {
|
||||
}
|
||||
|
||||
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
qstr_init();
|
||||
mp_init();
|
||||
|
||||
//mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
|
||||
//mp_obj_t py_argv = mp_obj_new_list(0, NULL);
|
||||
//mp_store_attr(m_sys, MP_QSTR_argv, py_argv);
|
||||
|
||||
mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
|
||||
mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
|
||||
|
||||
do_str("print(123)");
|
||||
mp_import_stat_t mp_import_stat(const char *path) {
|
||||
return MP_IMPORT_STAT_NO_EXIST;
|
||||
}
|
||||
|
||||
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args) {
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open);
|
||||
|
||||
void nlr_jump_fail(void *val) {
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,22 +2,18 @@
|
||||
|
||||
// options to control how Micro Python is built
|
||||
|
||||
#define MICROPY_EMIT_X64 (0)
|
||||
#define MICROPY_EMIT_THUMB (0)
|
||||
#define MICROPY_EMIT_INLINE_THUMB (0)
|
||||
#define MICROPY_MEM_STATS (0)
|
||||
#define MICROPY_DEBUG_PRINTERS (0)
|
||||
#define MICROPY_ENABLE_GC (0)
|
||||
#define MICROPY_ENABLE_FINALISER (0)
|
||||
#define MICROPY_ENABLE_REPL_HELPERS (1)
|
||||
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
|
||||
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
|
||||
#define MICROPY_PATH_MAX (128)
|
||||
#define MICROPY_ENABLE_MOD_IO (0)
|
||||
|
||||
// extra built in names to add to the global namespace
|
||||
extern const struct _mp_obj_fun_native_t mp_builtin_help_obj;
|
||||
#define MICROPY_EXTRA_BUILTINS \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_help), (mp_obj_t)&mp_builtin_help_obj },
|
||||
#define MICROPY_ENABLE_REPL_HELPERS (0)
|
||||
#define MICROPY_ENABLE_LEXER_UNIX (0)
|
||||
#define MICROPY_ENABLE_SOURCE_LINE (0)
|
||||
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
|
||||
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
|
||||
#define MICROPY_PATH_MAX (512)
|
||||
|
||||
// type definitions for the specific machine
|
||||
|
||||
@@ -31,3 +27,8 @@ typedef uint32_t machine_uint_t; // must be pointer size
|
||||
typedef void *machine_ptr_t; // must be of pointer size
|
||||
typedef const void *machine_const_ptr_t; // must be of pointer size
|
||||
|
||||
// extra built in names to add to the global namespace
|
||||
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
|
||||
#define MICROPY_EXTRA_BUILTINS \
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
|
||||
|
||||
|
||||
@@ -1,237 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stm32f4xx_hal.h>
|
||||
|
||||
#include "nlr.h"
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "qstr.h"
|
||||
#include "misc.h"
|
||||
#include "lexer.h"
|
||||
#include "parse.h"
|
||||
#include "obj.h"
|
||||
#include "parsehelper.h"
|
||||
#include "compile.h"
|
||||
#include "runtime.h"
|
||||
#include "repl.h"
|
||||
#include "gc.h"
|
||||
#include "gccollect.h"
|
||||
#include "systick.h"
|
||||
#include "pybstdio.h"
|
||||
#include "readline.h"
|
||||
#include "pyexec.h"
|
||||
#include "storage.h"
|
||||
#include "usb.h"
|
||||
#include "build/py/py-version.h"
|
||||
|
||||
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
|
||||
STATIC bool repl_display_debugging_info = 0;
|
||||
|
||||
// parses, compiles and executes the code in the lexer
|
||||
// frees the lexer before returning
|
||||
bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
|
||||
mp_parse_error_kind_t parse_error_kind;
|
||||
mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
|
||||
qstr source_name = mp_lexer_source_name(lex);
|
||||
|
||||
if (pn == MP_PARSE_NODE_NULL) {
|
||||
// parse error
|
||||
mp_parse_show_exception(lex, parse_error_kind);
|
||||
mp_lexer_free(lex);
|
||||
return false;
|
||||
}
|
||||
|
||||
mp_lexer_free(lex);
|
||||
|
||||
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, is_repl);
|
||||
mp_parse_node_free(pn);
|
||||
|
||||
if (module_fun == mp_const_none) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nlr_buf_t nlr;
|
||||
bool ret;
|
||||
uint32_t start = HAL_GetTick();
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C); // allow ctrl-C to interrupt us
|
||||
mp_call_function_0(module_fun);
|
||||
usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt
|
||||
nlr_pop();
|
||||
ret = true;
|
||||
} else {
|
||||
// uncaught exception
|
||||
// FIXME it could be that an interrupt happens just before we disable it here
|
||||
usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt
|
||||
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
|
||||
ret = false;
|
||||
}
|
||||
|
||||
// display debugging info if wanted
|
||||
if (is_repl && repl_display_debugging_info) {
|
||||
uint32_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly
|
||||
printf("took %lu ms\n", ticks);
|
||||
gc_collect();
|
||||
// qstr info
|
||||
{
|
||||
uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
|
||||
qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
|
||||
printf("qstr:\n n_pool=%u\n n_qstr=%u\n n_str_data_bytes=%u\n n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
|
||||
}
|
||||
|
||||
// GC info
|
||||
{
|
||||
gc_info_t info;
|
||||
gc_info(&info);
|
||||
printf("GC:\n");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int pyexec_raw_repl(void) {
|
||||
vstr_t line;
|
||||
vstr_init(&line, 32);
|
||||
|
||||
raw_repl_reset:
|
||||
stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
|
||||
|
||||
for (;;) {
|
||||
vstr_reset(&line);
|
||||
stdout_tx_str(">");
|
||||
for (;;) {
|
||||
char c = stdin_rx_chr();
|
||||
if (c == VCP_CHAR_CTRL_A) {
|
||||
// reset raw REPL
|
||||
goto raw_repl_reset;
|
||||
} else if (c == VCP_CHAR_CTRL_B) {
|
||||
// change to friendly REPL
|
||||
stdout_tx_str("\r\n");
|
||||
vstr_clear(&line);
|
||||
pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
|
||||
return 0;
|
||||
} else if (c == VCP_CHAR_CTRL_C) {
|
||||
// clear line
|
||||
vstr_reset(&line);
|
||||
} else if (c == VCP_CHAR_CTRL_D) {
|
||||
// input finished
|
||||
break;
|
||||
} else if (c <= 127) {
|
||||
// let through any other ASCII character
|
||||
vstr_add_char(&line, c);
|
||||
}
|
||||
}
|
||||
|
||||
// indicate reception of command
|
||||
stdout_tx_str("OK");
|
||||
|
||||
if (line.len == 0) {
|
||||
// exit for a soft reset
|
||||
stdout_tx_str("\r\n");
|
||||
vstr_clear(&line);
|
||||
return 1;
|
||||
}
|
||||
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, 0);
|
||||
parse_compile_execute(lex, MP_PARSE_FILE_INPUT, false);
|
||||
|
||||
// indicate end of output with EOF character
|
||||
stdout_tx_str("\004");
|
||||
}
|
||||
}
|
||||
|
||||
int pyexec_friendly_repl(void) {
|
||||
vstr_t line;
|
||||
vstr_init(&line, 32);
|
||||
|
||||
#if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
|
||||
// in host mode, we enable the LCD for the repl
|
||||
mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD")));
|
||||
mp_call_function_1(mp_load_attr(lcd_o, qstr_from_str("light")), mp_const_true);
|
||||
#endif
|
||||
|
||||
friendly_repl_reset:
|
||||
stdout_tx_str("Micro Python build " MICROPY_GIT_HASH " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with STM32F405RG\r\n");
|
||||
stdout_tx_str("Type \"help()\" for more information.\r\n");
|
||||
|
||||
// to test ctrl-C
|
||||
/*
|
||||
{
|
||||
uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef};
|
||||
for (;;) {
|
||||
nlr_buf_t nlr;
|
||||
printf("pyexec_repl: %p\n", x);
|
||||
usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C);
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
for (;;) {
|
||||
}
|
||||
} else {
|
||||
printf("break\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
for (;;) {
|
||||
vstr_reset(&line);
|
||||
int ret = readline(&line, ">>> ");
|
||||
|
||||
if (ret == VCP_CHAR_CTRL_A) {
|
||||
// change to raw REPL
|
||||
stdout_tx_str("\r\n");
|
||||
vstr_clear(&line);
|
||||
pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
|
||||
return 0;
|
||||
} else if (ret == VCP_CHAR_CTRL_B) {
|
||||
// reset friendly REPL
|
||||
stdout_tx_str("\r\n");
|
||||
goto friendly_repl_reset;
|
||||
} else if (ret == VCP_CHAR_CTRL_C) {
|
||||
// break
|
||||
stdout_tx_str("\r\n");
|
||||
continue;
|
||||
} else if (ret == VCP_CHAR_CTRL_D) {
|
||||
// exit for a soft reset
|
||||
stdout_tx_str("\r\n");
|
||||
vstr_clear(&line);
|
||||
return 1;
|
||||
} else if (vstr_len(&line) == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
while (mp_repl_continue_with_input(vstr_str(&line))) {
|
||||
vstr_add_char(&line, '\n');
|
||||
int ret = readline(&line, "... ");
|
||||
if (ret == VCP_CHAR_CTRL_D) {
|
||||
// stop entering compound statement
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0);
|
||||
parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, true);
|
||||
}
|
||||
}
|
||||
|
||||
bool pyexec_file(const char *filename) {
|
||||
mp_lexer_t *lex = mp_lexer_new_from_file(filename);
|
||||
|
||||
if (lex == NULL) {
|
||||
printf("could not open file '%s' for reading\n", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, false);
|
||||
}
|
||||
|
||||
mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
|
||||
repl_display_debugging_info = mp_obj_get_int(o_value);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info);
|
||||
@@ -1,12 +0,0 @@
|
||||
typedef enum {
|
||||
PYEXEC_MODE_RAW_REPL,
|
||||
PYEXEC_MODE_FRIENDLY_REPL,
|
||||
} pyexec_mode_kind_t;
|
||||
|
||||
extern pyexec_mode_kind_t pyexec_mode_kind;
|
||||
|
||||
int pyexec_raw_repl(void);
|
||||
int pyexec_friendly_repl(void);
|
||||
bool pyexec_file(const char *filename);
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ(pyb_set_repl_info_obj);
|
||||
@@ -1,148 +1 @@
|
||||
// qstrs specific to this port
|
||||
|
||||
Q(help)
|
||||
Q(pyb)
|
||||
Q(info)
|
||||
Q(sd_test)
|
||||
Q(present)
|
||||
Q(power)
|
||||
Q(stop)
|
||||
Q(standby)
|
||||
Q(source_dir)
|
||||
Q(main)
|
||||
Q(usb_mode)
|
||||
Q(sync)
|
||||
Q(gc)
|
||||
Q(repl_info)
|
||||
Q(delay)
|
||||
Q(udelay)
|
||||
Q(switch)
|
||||
Q(SW)
|
||||
Q(servo)
|
||||
Q(pwm)
|
||||
Q(read)
|
||||
Q(readall)
|
||||
Q(readline)
|
||||
Q(write)
|
||||
Q(hid)
|
||||
Q(time)
|
||||
Q(rng)
|
||||
Q(LCD)
|
||||
Q(SD)
|
||||
Q(SDcard)
|
||||
Q(gpio)
|
||||
Q(gpio_in)
|
||||
Q(gpio_out)
|
||||
Q(FileIO)
|
||||
// Entries for sys.path
|
||||
Q(0:/)
|
||||
Q(0:/src)
|
||||
Q(0:/lib)
|
||||
Q(Pin)
|
||||
Q(PinMap)
|
||||
Q(PinAF)
|
||||
Q(PinNamed)
|
||||
Q(rtc_info)
|
||||
Q(millis)
|
||||
Q(PULL_NONE)
|
||||
Q(PULL_UP)
|
||||
Q(PULL_DOWN)
|
||||
Q(PUSH_PULL)
|
||||
Q(OPEN_DRAIN)
|
||||
|
||||
// for Led object
|
||||
Q(Led)
|
||||
Q(on)
|
||||
Q(off)
|
||||
Q(toggle)
|
||||
Q(intensity)
|
||||
|
||||
// for Usart object
|
||||
Q(Usart)
|
||||
Q(status)
|
||||
Q(recv_chr)
|
||||
Q(send_chr)
|
||||
Q(send)
|
||||
|
||||
// for exti object
|
||||
Q(Exti)
|
||||
Q(line)
|
||||
Q(enable)
|
||||
Q(disable)
|
||||
Q(swint)
|
||||
Q(regs)
|
||||
Q(MODE_IRQ_RISING)
|
||||
Q(MODE_IRQ_FALLING)
|
||||
Q(MODE_IRQ_RISING_FALLING)
|
||||
Q(MODE_EVT_RISING)
|
||||
Q(MODE_EVT_FALLING)
|
||||
Q(MODE_EVT_RISING_FALLING)
|
||||
|
||||
// for I2C object
|
||||
Q(I2C)
|
||||
Q(is_ready)
|
||||
Q(mem_read)
|
||||
Q(mem_write)
|
||||
|
||||
// for Accel object
|
||||
Q(Accel)
|
||||
Q(x)
|
||||
Q(y)
|
||||
Q(z)
|
||||
Q(tilt)
|
||||
Q(filtered_xyz)
|
||||
|
||||
// for ADC object
|
||||
Q(ADC)
|
||||
Q(ADC_all)
|
||||
Q(read_channel)
|
||||
Q(read_core_temp)
|
||||
Q(read_core_vbat)
|
||||
Q(read_core_vref)
|
||||
|
||||
// for DAC object
|
||||
Q(DAC)
|
||||
Q(noise)
|
||||
Q(triangle)
|
||||
Q(dma)
|
||||
|
||||
// for Servo object
|
||||
Q(Servo)
|
||||
Q(pulse_width)
|
||||
Q(calibrate)
|
||||
Q(angle)
|
||||
Q(speed)
|
||||
|
||||
// for os module
|
||||
Q(os)
|
||||
Q(/)
|
||||
Q(listdir)
|
||||
Q(mkdir)
|
||||
Q(remove)
|
||||
Q(rmdir)
|
||||
Q(unlink)
|
||||
Q(sep)
|
||||
Q(urandom)
|
||||
|
||||
// for time module
|
||||
Q(time)
|
||||
Q(sleep)
|
||||
|
||||
// for input
|
||||
Q(input)
|
||||
|
||||
// for stm module
|
||||
Q(stm)
|
||||
Q(read8)
|
||||
Q(read16)
|
||||
Q(read32)
|
||||
Q(write8)
|
||||
Q(write16)
|
||||
Q(write32)
|
||||
Q(GPIOA)
|
||||
Q(GPIOB)
|
||||
Q(GPIOC)
|
||||
Q(GPIOD)
|
||||
Q(GPIO_IDR)
|
||||
Q(GPIO_BSRRL)
|
||||
Q(GPIO_BSRRH)
|
||||
|
||||
Reference in New Issue
Block a user