8000 Add 'bare-arm' port: the bare minimum to get it running on an ARM MCU. · davidgiven/micropython@c557215 · GitHub
[go: up one dir, main page]

Skip to content

Commit c557215

Browse files
committed
Add 'bare-arm' port: the bare minimum to get it running on an ARM MCU.
1 parent f7e4e1c commit c557215

File tree

5 files changed

+313
-0
lines changed

5 files changed

+313
-0
lines changed

bare-arm/Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
include ../py/mkenv.mk
2+
3+
# qstr definitions (must come before including py.mk)
4+
QSTR_DEFS = qstrdefsport.h
5+
6+
# include py core make definitions
7+
include ../py/py.mk
8+
9+
CROSS_COMPILE = arm-none-eabi-
10+
11+
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
12+
CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 $(CFLAGS_CORTEX_M4) $(COPT)
13+
14+
#Debugging/Optimization
15+
ifeq ($(DEBUG), 1)
16+
CFLAGS += -O0 -ggdb
17+
else
18+
CFLAGS += -Os -DNDEBUG
19+
endif
20+
21+
LDFLAGS = --nostdlib -T stm32f405.ld
22+
LIBS =
23+
24+
SRC_C = \
25+
main.c \
26+
# printf.c \
27+
string0.c \
28+
malloc0.c \
29+
gccollect.c \
30+
31+
SRC_S = \
32+
# startup_stm32f40xx.s \
33+
gchelper.s \
34+
35+
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o))
36+
37+
all: $(BUILD)/flash.elf
38+
39+
$(BUILD)/flash.elf: $(OBJ)
40+
$(ECHO) "LINK $@"
41+
$(Q)$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
42+
$(Q)$(SIZE) $@
43+
44+
include ../py/mkrules.mk

bare-arm/main.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include "nlr.h"
6+
#include "misc.h"
7+
#include "mpconfig.h"
8+
#include "qstr.h"
9+
#include "lexer.h"
10+
#include "parse.h"
11+
#include "obj.h"
12+
#include "parsehelper.h"
13+
#include "compile.h"
14+
#include "runtime0.h"
15+
#include "runtime.h"
16+
#include "repl.h"
17+
18+
void do_str(const char *src) {
19+
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
20+
if (lex == NULL) {
21+
return;
22+
}
23+
24+
mp_parse_error_kind_t parse_error_kind;
25+
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT, &parse_error_kind);
26+
27+
if (pn == MP_PARSE_NODE_NULL) {
28+
// parse error
29+
mp_parse_show_exception(lex, parse_error_kind);
30+
mp_lexer_free(lex);
31+
return;
32+
}
33+
34+
// parse okay
35+
qstr source_name = mp_lexer_source_name(lex);
36+
mp_lexer_free(lex);
37+
mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true);
38+
mp_parse_node_free(pn);
39+
40+
if (module_fun == mp_const_none) {
41+
// compile error
42+
return;
43+
}
44+
45+
nlr_buf_t nlr;
46+
if (nlr_push(&nlr) == 0) {
47+
mp_call_function_0(module_fun);
48+
nlr_pop();
49+
} else {
50+
// uncaught exception
51+
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
52+
}
53+
}
54+
55+
int main(int argc, char **argv) {
56+
qstr_init();
57+
mp_init();
58+
do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\n')");
59+
mp_deinit();
60+
return 0;
61+
}
62+
63+
void gc_collect(void) {
64+
}
65+
66+
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
67+
return NULL;
68+
}
69+
70+
mp_import_stat_t mp_import_stat(const char *path) {
71+
return MP_IMPORT_STAT_NO_EXIST;
72+
}
73+
74+
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args) {
75+
return mp_const_none;
76+
}
77+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open);
78+
79+
void nlr_jump_fail(void *val) {
80+
}
81+
82+
/*
83+
int _lseek() {return 0;}
84+
int _read() {return 0;}
85+
int _write() {return 0;}
86+
int _close() {return 0;}
87+
void _exit(int x) {for(;;){}}
88+
int _sbrk() {return 0;}
89+
int _kill() {return 0;}
90+
int _getpid() {return 0;}
91+
int _fstat() {return 0;}
92+
int _isatty() {return 0;}
93+
*/
94+
95+
void *malloc(size_t n) {return NULL;}
96+
void *calloc(size_t nmemb, size_t size) {return NULL;}
97+
void *realloc(void *ptr, size_t size) {return NULL;}
98+
void free(void *p) {}
99+
int printf(const char *m, ...) {return 0;}
100+
void *memcpy(void *dest, const void *src, size_t n) {return NULL;}
101+
int memcmp(const void *s1, const void *s2, size_t n) {return 0;}
102+
void *memmove(void *dest, const void *src, size_t n) {return NULL;}
103+
void *memset(void *s, int c, size_t n) {return NULL;}
104+
int strcmp(const char *s1, const char* s2) {return 0;}
105+
int strncmp(const char *s1, const char* s2, size_t n) {return 0;}
106+
size_t strlen(const char *s) {return 0;}
107+
char *strcat(char *dest, const char *src) {return NULL;}
108+
char *strchr(const char *dest, int c) {return NULL;}
109+
#include <stdarg.h>
110+
int vprintf(const char *format, va_list ap) {return 0;}
111+
int vsnprintf(char *str, size_t size, const char *format, va_list ap) {return 0;}
112+
113+
#undef putchar
114+
int putchar(int c) {return 0;}
115+
int puts(const char *s) {return 0;}
116+
117+
void _start(void) {main(0, NULL);}

bare-arm/mpconfigport.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdint.h>
2+
3+
// options to control how Micro Python is built
4+
5+
#define MICROPY_EMIT_X64 (0)
6+
#define MICROPY_EMIT_THUMB (0)
7+
#define MICROPY_EMIT_INLINE_THUMB (0)
8+
#define MICROPY_MEM_STATS (0)
9+
#define MICROPY_DEBUG_PRINTERS (0)
10+
#define MICROPY_ENABLE_GC (0)
11+
#define MICROPY_ENABLE_REPL_HELPERS (0)
12+
#define MICROPY_ENABLE_LEXER_UNIX (0)
13+
#define MICROPY_ENABLE_SOURCE_LINE (0)
14+
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
15+
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
16+
#define MICROPY_PATH_MAX (512)
17+
18+
// type definitions for the specific machine
19+
20+
#define BYTES_PER_WORD (4)
21+
22+
#define UINT_FMT "%lu"
23+
#define INT_FMT "%ld"
24+
25+
typedef int32_t machine_int_t; // must be pointer size
26+
typedef uint32_t machine_uint_t; // must be pointer size
27+
typedef void *machine_ptr_t; // must be of pointer size
28+
typedef const void *machine_const_ptr_t; // must be of pointer size
29+
30+
// extra built in names to add to the global namespace
31+
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
32+
#define MICROPY_EXTRA_BUILTINS \
33+
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
34+

bare-arm/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// qstrs specific to this port

bare-arm/stm32f405.ld

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
GNU linker script for STM32F405
3+
*/
4+
5+
/* Specify the memory areas */
6+
MEMORY
7+
{
8+
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x100000 /* entire flash, 1 MiB */
9+
FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */
10+
FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x080000 /* sectors 5,6,7,8, 4*128KiB = 512 KiB (could increase it more) */
11+
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 0x010000 /* 64 KiB */
12+
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x020000 /* 128 KiB */
13+
}
14+
15+
/* top end of the stack */
16+
_estack = ORIGIN(RAM) + LENGTH(RAM);
17+
18+
/* RAM extents for the garbage collector */
19+
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
20+
_heap_end = 0x2001c000; /* tunable */
21+
22+
/* define output sections */
23+
SECTIONS
24+
{
25+
/* The startup code goes first into FLASH */
26+
.isr_vector :
27+
{
28+
. = ALIGN(4);
29+
KEEP(*(.isr_vector)) /* Startup code */
30+
31+
. = ALIGN(4);
32+
} >FLASH_ISR
33+
34+
/* The program code and other data goes into FLASH */
35+
.text :
36+
{
37+
. = ALIGN(4);
38+
*(.text) /* .text sections (code) */
39+
*(.text*) /* .text* sections (code) */
40+
*(.rodata) /* .rodata sections (constants, strings, etc.) */
41+
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
42+
/* *(.glue_7) */ /* glue arm to thumb code */
43+
/* *(.glue_7t) */ /* glue thumb to arm code */
44+
45+
. = ALIGN(4);
46+
_etext = .; /* define a global symbol at end of code */
47+
_sidata = _etext; /* This is used by the startup in order to initialize the .data secion */
48+
} >FLASH_TEXT
49+
50+
/*
51+
.ARM.extab :
52+
{
53+
*(.ARM.extab* .gnu.linkonce.armextab.*)
54+
} >FLASH
55+
56+
.ARM :
57+
{
58+
__exidx_start = .;
59+
*(.ARM.exidx*)
60+
__exidx_end = .;
61+
} >FLASH
62+
*/
63+
64+
/* This is the initialized data section
65+
The program executes knowing that the data is in the RAM
66+
but the loader puts the initial values in the FLASH (inidata).
67+
It is one task of the startup to copy the initial values from FLASH to RAM. */
68+
.data : AT ( _sidata )
69+
{
70+
. = ALIGN(4);
71+
_sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */
72+
_ram_start = .; /* create a global symbol at ram start for garbage collector */
73+
*(.data) /* .data sections */
74+
*(.data*) /* .data* sections */
75+
76+
. = ALIGN(4);
77+
_edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */
78+
} >RAM
79+
80+
/* Uninitialized data section */
81+
.bss :
82+
{
83+
. = ALIGN(4);
84+
_sbss = .; /* define a global symbol at bss start; used by startup code */
85+
*(.bss)
86+
*(.bss*)
87+
*(COMMON)
88+
89+
. = ALIGN(4);
90+
_ebss = .; /* define a global symbol at bss end; used by startup code */
91+
} >RAM
92+
93+
/* this is to define the start of the heap, and make sure we have a minimum size */
94+
.heap :
95+
{
96+
. = ALIGN(4);
97+
_heap_start = .; /* define a global symbol at heap start */
98+
} >RAM
99+
100+
/* this just checks there is enough RAM for the stack */
101+
.stack :
102+
{
103+
. = ALIGN(4);
104+
} >RAM
105+
106+
/* Remove information from the standard libraries */
107+
/*
108+
/DISCARD/ :
109+
{
110+
libc.a ( * )
111+
libm.a ( * )
112+
libgcc.a ( * )
113+
}
114+
*/
115+
116+
.ARM.attributes 0 : { *(.ARM.attributes) }
117+
}

0 commit comments

Comments
 (0)
0