From c55721582282f282d33e35e458a2944890210ebc Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 12 Apr 2014 13:07:45 +0100 Subject: [PATCH] Add 'bare-arm' port: the bare minimum to get it running on an ARM MCU. --- bare-arm/Makefile | 44 +++++++++++++++ bare-arm/main.c | 117 ++++++++++++++++++++++++++++++++++++++++ bare-arm/mpconfigport.h | 34 ++++++++++++ bare-arm/qstrdefsport.h | 1 + bare-arm/stm32f405.ld | 117 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 313 insertions(+) create mode 100644 bare-arm/Makefile create mode 100644 bare-arm/main.c create mode 100644 bare-arm/mpconfigport.h create mode 100644 bare-arm/qstrdefsport.h create mode 100644 bare-arm/stm32f405.ld diff --git a/bare-arm/Makefile b/bare-arm/Makefile new file mode 100644 index 0000000000..0e9f1f7c39 --- /dev/null +++ b/bare-arm/Makefile @@ -0,0 +1,44 @@ +include ../py/mkenv.mk + +# qstr definitions (must come before including py.mk) +QSTR_DEFS = qstrdefsport.h + +# include py core make definitions +include ../py/py.mk + +CROSS_COMPILE = arm-none-eabi- + +CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion +CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 $(CFLAGS_CORTEX_M4) $(COPT) + +#Debugging/Optimization +ifeq ($(DEBUG), 1) +CFLAGS += -O0 -ggdb +else +CFLAGS += -Os -DNDEBUG +endif + +LDFLAGS = --nostdlib -T stm32f405.ld +LIBS = + +SRC_C = \ + main.c \ +# printf.c \ + string0.c \ + malloc0.c \ + gccollect.c \ + +SRC_S = \ +# startup_stm32f40xx.s \ + gchelper.s \ + +OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o)) + +all: $(BUILD)/flash.elf + +$(BUILD)/flash.elf: $(OBJ) + $(ECHO) "LINK $@" + $(Q)$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS) + $(Q)$(SIZE) $@ + +include ../py/mkrules.mk diff --git a/bare-arm/main.c b/bare-arm/main.c new file mode 100644 index 0000000000..8ad9b15870 --- /dev/null +++ b/bare-arm/main.c @@ -0,0 +1,117 @@ +#include +#include +#include + +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "lexer.h" +#include "parse.h" +#include "obj.h" +#include "parsehelper.h" +#include "compile.h" +#include "runtime0.h" +#include "runtime.h" +#include "repl.h" + +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; + } + + mp_parse_error_kind_t 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 + mp_parse_show_exception(lex, parse_error_kind); + mp_lexer_free(lex); + return; + } + + // parse okay + qstr source_name = mp_lexer_source_name(lex); + mp_lexer_free(lex); + 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; + } + + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_call_function_0(module_fun); + nlr_pop(); + } else { + // uncaught exception + mp_obj_print_exception((mp_obj_t)nlr.ret_val); + } +} + +int main(int argc, char **argv) { + qstr_init(); + mp_init(); + do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\n')"); + mp_deinit(); + return 0; +} + +void gc_collect(void) { +} + +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(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) { +} + +/* +int _lseek() {return 0;} +int _read() {return 0;} +int _write() {return 0;} +int _close() {return 0;} +void _exit(int x) {for(;;){}} +int _sbrk() {return 0;} +int _kill() {return 0;} +int _getpid() {return 0;} +int _fstat() {return 0;} +int _isatty() {return 0;} +*/ + +void *malloc(size_t n) {return NULL;} +void *calloc(size_t nmemb, size_t size) {return NULL;} +void *realloc(void *ptr, size_t size) {return NULL;} +void free(void *p) {} +int printf(const char *m, ...) {return 0;} +void *memcpy(void *dest, const void *src, size_t n) {return NULL;} +int memcmp(const void *s1, const void *s2, size_t n) {return 0;} +void *memmove(void *dest, const void *src, size_t n) {return NULL;} +void *memset(void *s, int c, size_t n) {return NULL;} +int strcmp(const char *s1, const char* s2) {return 0;} +int strncmp(const char *s1, const char* s2, size_t n) {return 0;} +size_t strlen(const char *s) {return 0;} +char *strcat(char *dest, const char *src) {return NULL;} +char *strchr(const char *dest, int c) {return NULL;} +#include +int vprintf(const char *format, va_list ap) {return 0;} +int vsnprintf(char *str, size_t size, const char *format, va_list ap) {return 0;} + +#undef putchar +int putchar(int c) {return 0;} +int puts(const char *s) {return 0;} + +void _start(void) {main(0, NULL);} diff --git a/bare-arm/mpconfigport.h b/bare-arm/mpconfigport.h new file mode 100644 index 0000000000..7f4c378901 --- /dev/null +++ b/bare-arm/mpconfigport.h @@ -0,0 +1,34 @@ +#include + +// 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_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 + +#define BYTES_PER_WORD (4) + +#define UINT_FMT "%lu" +#define INT_FMT "%ld" + +typedef int32_t machine_int_t; // must be pointer size +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 }, + diff --git a/bare-arm/qstrdefsport.h b/bare-arm/qstrdefsport.h new file mode 100644 index 0000000000..3ba897069b --- /dev/null +++ b/bare-arm/qstrdefsport.h @@ -0,0 +1 @@ +// qstrs specific to this port diff --git a/bare-arm/stm32f405.ld b/bare-arm/stm32f405.ld new file mode 100644 index 0000000000..345a92d3c1 --- /dev/null +++ b/bare-arm/stm32f405.ld @@ -0,0 +1,117 @@ +/* + GNU linker script for STM32F405 +*/ + +/* Specify the memory areas */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x100000 /* entire flash, 1 MiB */ + FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */ + FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x080000 /* sectors 5,6,7,8, 4*128KiB = 512 KiB (could increase it more) */ + CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 0x010000 /* 64 KiB */ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x020000 /* 128 KiB */ +} + +/* top end of the stack */ +_estack = ORIGIN(RAM) + LENGTH(RAM); + +/* RAM extents for the garbage collector */ +_ram_end = ORIGIN(RAM) + LENGTH(RAM); +_heap_end = 0x2001c000; /* tunable */ + +/* define output sections */ +SECTIONS +{ + /* The startup code goes first into FLASH */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + + . = ALIGN(4); + } >FLASH_ISR + + /* The program code and other data goes into FLASH */ + .text : + { + . = ALIGN(4); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + /* *(.glue_7) */ /* glue arm to thumb code */ + /* *(.glue_7t) */ /* glue thumb to arm code */ + + . = ALIGN(4); + _etext = .; /* define a global symbol at end of code */ + _sidata = _etext; /* This is used by the startup in order to initialize the .data secion */ + } >FLASH_TEXT + + /* + .ARM.extab : + { + *(.ARM.extab* .gnu.linkonce.armextab.*) + } >FLASH + + .ARM : + { + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH + */ + + /* This is the initialized data section + The program executes knowing that the data is in the RAM + but the loader puts the initial values in the FLASH (inidata). + It is one task of the startup to copy the initial values from FLASH to RAM. */ + .data : AT ( _sidata ) + { + . = ALIGN(4); + _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ + _ram_start = .; /* create a global symbol at ram start for garbage collector */ + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + + . = ALIGN(4); + _edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */ + } >RAM + + /* Uninitialized data section */ + .bss : + { + . = ALIGN(4); + _sbss = .; /* define a global symbol at bss start; used by startup code */ + *(.bss) + *(.bss*) + *(COMMON) + + . = ALIGN(4); + _ebss = .; /* define a global symbol at bss end; used by startup code */ + } >RAM + + /* this is to define the start of the heap, and make sure we have a minimum size */ + .heap : + { + . = ALIGN(4); + _heap_start = .; /* define a global symbol at heap start */ + } >RAM + + /* this just checks there is enough RAM for the stack */ + .stack : + { + . = ALIGN(4); + } >RAM + + /* Remove information from the standard libraries */ + /* + /DISCARD/ : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + */ + + .ARM.attributes 0 : { *(.ARM.attributes) } +}