diff --git a/zephyr/Makefile b/zephyr/Makefile new file mode 100644 index 0000000000..42897bcaf7 --- /dev/null +++ b/zephyr/Makefile @@ -0,0 +1,69 @@ +# +# This is main Makefile, which uses MicroPython build system, but +# Zephyr arch-specific toolchain (setup by Zephyr's Makefile.toolchain.*). +# Unfortunately, it's currently not possible to get target (as in: specific +# board to run on) specific compile-time options from Zephyr, so these must +# be set (duplicated) in this Makefile. Currently, these configured for +# ARM Cortex-M3. This Makefile builds MicroPython as a library, and then +# calls recursively Makefile.zephyr to build complete application using +# Zephyr build system. +# + +ARCH ?= x86 +BOARD ?= qemu_x86 +# Zephyr 1.5.0 +#OUTDIR_PREFIX = +# Zephyr 1.6.0 +OUTDIR_PREFIX = $(BOARD) + +# Zephyr toolchain config is 2-pass, so included twice +include $(ZEPHYR_BASE)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT) +include $(ZEPHYR_BASE)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT) + +CFLAGS_arm = -mthumb -mcpu=cortex-m3 -mabi=aapcs +CFLAGS_x86 = -fno-asynchronous-unwind-tables -ffreestanding -fno-stack-protector \ + -fno-omit-frame-pointer -mpreferred-stack-boundary=2 -mno-sse -march=pentium +CFLAGS_TARGET = $(CFLAGS_$(ARCH)) + +include ../py/mkenv.mk +include ../py/py.mk + +INC += -I. +INC += -I.. +INC += -I$(BUILD) +INC += -I$(ZEPHYR_BASE)/include -I$(ZEPHYR_BASE) \ + -Ioutdir/$(OUTDIR_PREFIX)/misc/generated/sysgen \ + -I$(dir $(Z_AUTOCONF_H)) + +SRC_C = main.c \ + uart_core.c \ + lib/utils/stdout_helpers.c \ + lib/utils/printf.c \ + lib/utils/pyexec.c \ + lib/mp-readline/readline.c \ + $(SRC_MOD) + +OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) + +COPT = -Os -DNDEBUG -fdata-sections -ffunction-sections +CFLAGS = -std=gnu99 $(TOOLCHAIN_CFLAGS) $(INC) $(CFLAGS_MOD) $(COPT) $(CFLAGS_TARGET) + +include ../py/mkrules.mk + +all: $(LIBMICROPYTHON) + $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) + +qemu: all + $(MAKE) -f Makefile.zephyr qemu BOARD=$(BOARD) + +Z_AUTOCONF_H = outdir/$(OUTDIR_PREFIX)/include/generated/autoconf.h + +$(LIBMICROPYTHON): $(Z_AUTOCONF_H) +build/genhdr/qstr.i.last: $(Z_AUTOCONF_H) + +$(Z_AUTOCONF_H): + rm -f $(LIBMICROPYTHON) + -$(MAKE) -f Makefile.zephyr BOARD=$(BOARD) + +# Clean Zephyr things too +CLEAN_EXTRA = outdir diff --git a/zephyr/main.c b/zephyr/main.c new file mode 100644 index 0000000000..65074b157e --- /dev/null +++ b/zephyr/main.c @@ -0,0 +1,95 @@ +#include +#include +#include + +#include "py/nlr.h" +#include "py/compile.h" +#include "py/runtime.h" +#include "py/repl.h" +#include "py/gc.h" +#include "lib/utils/pyexec.h" + +void do_str(const char *src, mp_parse_input_kind_t input_kind) { + mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); + if (lex == NULL) { + printf("MemoryError: lexer could not allocate memory\n"); + return; + } + + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + qstr source_name = lex->source_name; + mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); + mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true); + mp_call_function_0(module_fun); + nlr_pop(); + } else { + // uncaught exception + mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); + } +} + +static char *stack_top; +static char heap[16 * 1024]; + +int real_main(void) { + int stack_dummy; + stack_top = (char*)&stack_dummy; + + #if MICROPY_ENABLE_GC + gc_init(heap, heap + sizeof(heap)); + #endif + mp_init(); + #if MICROPY_REPL_EVENT_DRIVEN + pyexec_event_repl_init(); + for (;;) { + int c = mp_hal_stdin_rx_chr(); + if (pyexec_event_repl_process_char(c)) { + break; + } + } + #else + pyexec_friendly_repl(); + #endif + //do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT); + //do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT); + mp_deinit(); + return 0; +} + +void gc_collect(void) { + // WARNING: This gc_collect implementation doesn't try to get root + // pointers from CPU registers, and thus may function incorrectly. + void *dummy; + gc_collect_start(); + gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); + gc_collect_end(); + gc_dump_info(); +} + +mp_lexer_t *mp_lexer_new_from_file(const char *filename) { + return NULL; +} + +mp_import_stat_t mp_import_stat(const char *path) { + return MP_IMPORT_STAT_NO_EXIST; +} + +mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); + +void nlr_jump_fail(void *val) { +} + +void NORETURN __fatal_error(const char *msg) { + while (1); +} + +#ifndef NDEBUG +void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { + printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); + __fatal_error("Assertion failed"); +} +#endif diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h new file mode 100644 index 0000000000..cf5884e0e7 --- /dev/null +++ b/zephyr/mpconfigport.h @@ -0,0 +1,49 @@ +#include + +// Saving extra crumbs to make sure binary fits in 128K +#define MICROPY_COMP_CONST_FOLDING (0) +#define MICROPY_COMP_CONST (0) +#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0) + +#define MICROPY_ENABLE_GC (1) +#define MICROPY_HELPER_REPL (1) +#define MICROPY_REPL_AUTO_INDENT (1) +#define MICROPY_CPYTHON_COMPAT (0) +#define MICROPY_PY_ASYNC_AWAIT (0) +#define MICROPY_PY_ATTRTUPLE (0) +#define MICROPY_PY_BUILTINS_ENUMERATE (0) +#define MICROPY_PY_BUILTINS_FILTER (0) +#define MICROPY_PY_BUILTINS_MIN_MAX (0) +#define MICROPY_PY_BUILTINS_PROPERTY (0) +#define MICROPY_PY_BUILTINS_RANGE_ATTRS (0) +#define MICROPY_PY_BUILTINS_REVERSED (0) +#define MICROPY_PY_BUILTINS_SET (0) +#define MICROPY_PY_BUILTINS_SLICE (0) +#define MICROPY_PY_ARRAY (0) +#define MICROPY_PY_COLLECTIONS (0) +#define MICROPY_PY_CMATH (0) +#define MICROPY_PY_IO (0) +#define MICROPY_PY_STRUCT (0) +#define MICROPY_PY_SYS_MODULES (0) +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG) +#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) +#define MICROPY_PY_BUILTINS_COMPLEX (0) +#define MICROPY_HW_BOARD_NAME "zephyr-generic" +#define MICROPY_HW_MCU_NAME "unknown-cpu" + +typedef int mp_int_t; // must be pointer size +typedef unsigned mp_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 +typedef long mp_off_t; + +#define BYTES_PER_WORD (sizeof(mp_int_t)) + +#define MP_STATE_PORT MP_STATE_VM + +#define MICROPY_PORT_ROOT_POINTERS \ + const char *readline_hist[8]; + +// Include Zephyr's autoconf.h, which should be made first by Zephyr makefiles +#include "autoconf.h" diff --git a/zephyr/mphalport.h b/zephyr/mphalport.h new file mode 100644 index 0000000000..60d68bd2d6 --- /dev/null +++ b/zephyr/mphalport.h @@ -0,0 +1,2 @@ +static inline mp_uint_t mp_hal_ticks_ms(void) { return 0; } +static inline void mp_hal_set_interrupt_char(char c) {} diff --git a/zephyr/uart_core.c b/zephyr/uart_core.c new file mode 100644 index 0000000000..5f48f973f6 --- /dev/null +++ b/zephyr/uart_core.c @@ -0,0 +1,22 @@ +#include +#include "py/mpconfig.h" +#include "src/zephyr_getchar.h" + +// Stopgap +extern void printk(const char*, ...); + +/* + * Core UART functions to implement for a port + */ + +// Receive single character +int mp_hal_stdin_rx_chr(void) { + return zephyr_getchar(); +} + +// Send string of given length +void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { + while (len--) { + printk("%c", *str++); + } +}