Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Integrate micropython #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "user/mpy/micropython"]
path = user/mpy/micropython
url = [email protected]:micropython/micropython.git
ignore = untracked
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NAME = piko

# select QEMU when the target is unspecified
TARGET ?= stm32f429
TARGET ?= stm32p103
CMSIS = external/cmsis

# The platform Makefile contains hw details and flags
Expand Down Expand Up @@ -32,20 +32,27 @@ CSRC += \
libc/piko/mman.c \
$(LIBPIKO_CSRC)

.PHONY: all check clean distclean

all: $(CMSIS)/$(TARGET) $(USER)
$(MAKE) $(NAME).lds
$(MAKE) $(NAME).bin

# micropython
include user/mpy/build.mk

OBJS += $(SSRC:.S=.o) $(CSRC:.c=.o)
OBJS := $(sort $(OBJS))

deps := $(OBJS:%.o=.%.o.d)

.PHONY: all check clean distclean

all: $(CMSIS)/$(TARGET) $(NAME).lds $(NAME).bin

# generic build rules
include mk/flags.mk
include mk/rules.mk
include mk/cmsis.mk


prebuild: $(CMSIS)/$(TARGET)

check:
Expand All @@ -57,6 +64,9 @@ clean:
ifneq "$(wildcard $(CMSIS) )" ""
find $(CMSIS) -name "*.o" -type f -delete
endif
rm -rf user/mpy/micropython/build
rm -f user/mpy/micropython/*.o.d
rm -f user/mpy/micropython/py/*.o
rm -f $(NAME).map $(NAME).lds
rm -f $(NAME).elf $(NAME).bin

Expand Down
112 changes: 112 additions & 0 deletions user/mpy/_frozen_mpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "py/mpconfig.h"
#include "py/objint.h"
#include "py/objstr.h"
#include "py/emitglue.h"

#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
#error "MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE not supported with frozen mpy files"
#endif

#if MICROPY_LONGINT_IMPL != 0
#error "incompatible MICROPY_LONGINT_IMPL"
#endif

#if MICROPY_PY_BUILTINS_FLOAT
typedef struct _mp_obj_float_t {
mp_obj_base_t base;
mp_float_t value;
} mp_obj_float_t;
#endif

enum {
MP_QSTR_frozentest_dot_py = MP_QSTRnumber_of,
MP_QSTR_uPy,
MP_QSTR_i,
};

extern const qstr_pool_t mp_qstr_const_pool;
const qstr_pool_t mp_qstr_frozen_const_pool = {
(qstr_pool_t*)&mp_qstr_const_pool, // previous pool
MP_QSTRnumber_of, // previous pool size
3, // allocated entries
3, // used entries
{
(const byte*)"\xfe\x0d" "frozentest.py",
(const byte*)"\xf9\x03" "uPy",
(const byte*)"\xcc\x01" "i",
},
};

// frozen bytecode for file frozentest.py, scope frozentest_<module>
STATIC const byte bytecode_data_frozentest__lt_module_gt_[92] = {
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d,
MP_QSTR__lt_module_gt_ & 0xff, MP_QSTR__lt_module_gt_ >> 8,
MP_QSTR_frozentest_dot_py & 0xff, MP_QSTR_frozentest_dot_py >> 8,
0x2a, 0x28, 0x28, 0x28, 0x2b, 0x28, 0x00, 0x00, 0xff,
0x1c, MP_QSTR_print & 0xff, MP_QSTR_print >> 8,
0x16, MP_QSTR_uPy & 0xff, MP_QSTR_uPy >> 8,
0x64, 0x01,
0x32,
0x1c, MP_QSTR_print & 0xff, MP_QSTR_print >> 8,
0x17, 0x00,
0x64, 0x01,
0x32,
0x1c, MP_QSTR_print & 0xff, MP_QSTR_print >> 8,
0x17, 0x01,
0x64, 0x01,
0x32,
0x1c, MP_QSTR_print & 0xff, MP_QSTR_print >> 8,
0x17, 0x02,
0x64, 0x01,
0x32,
0x1c, MP_QSTR_print & 0xff, MP_QSTR_print >> 8,
0x14, 0xba, 0xef, 0x9a, 0x15,
0x64, 0x01,
0x32,
0x80,
0x35, 0x0f, 0x80,
0x30,
0x24, MP_QSTR_i & 0xff, MP_QSTR_i >> 8,
0x1c, MP_QSTR_print & 0xff, MP_QSTR_print >> 8,
0x1c, MP_QSTR_i & 0xff, MP_QSTR_i >> 8,
0x64, 0x01,
0x32,
0x81,
0xe9,
0x30,
0x84,
0xf0,
0x36, 0xeb, 0x7f,
0x32,
0x11,
0x5b,
};
STATIC const mp_obj_str_t const_obj_frozentest__lt_module_gt__0 = {{&mp_type_str}, 0, 34, (const byte*)"\x61\x20\x6c\x6f\x6e\x67\x20\x73\x74\x72\x69\x6e\x67\x20\x74\x68\x61\x74\x20\x69\x73\x20\x6e\x6f\x74\x20\x69\x6e\x74\x65\x72\x6e\x65\x64"};
STATIC const mp_obj_str_t const_obj_frozentest__lt_module_gt__1 = {{&mp_type_str}, 0, 38, (const byte*)"\x61\x20\x73\x74\x72\x69\x6e\x67\x20\x74\x68\x61\x74\x20\x68\x61\x73\x20\x75\x6e\x69\x63\x6f\x64\x65\x20\xce\xb1\xce\xb2\xce\xb3\x20\x63\x68\x61\x72\x73"};
STATIC const mp_obj_str_t const_obj_frozentest__lt_module_gt__2 = {{&mp_type_bytes}, 0, 11, (const byte*)"\x62\x79\x74\x65\x73\x20\x31\x32\x33\x34\x01"};
STATIC const mp_uint_t const_table_data_frozentest__lt_module_gt_[3] = {
(mp_uint_t)&const_obj_frozentest__lt_module_gt__0,
(mp_uint_t)&const_obj_frozentest__lt_module_gt__1,
(mp_uint_t)&const_obj_frozentest__lt_module_gt__2,
};
const mp_raw_code_t raw_code_frozentest__lt_module_gt_ = {
.kind = MP_CODE_BYTECODE,
.scope_flags = 0x00,
.n_pos_args = 0,
.data.u_byte = {
.bytecode = bytecode_data_frozentest__lt_module_gt_,
.const_table = const_table_data_frozentest__lt_module_gt_,
#if MICROPY_PERSISTENT_CODE_SAVE
.bc_len = 92,
.n_obj = 3,
.n_raw_code = 0,
#endif
},
};

const char mp_frozen_mpy_names[] = {
"frozentest.py\0"
"\0"};
const mp_raw_code_t *const mp_frozen_mpy_content[] = {
&raw_code_frozentest__lt_module_gt_,
};
70 changes: 70 additions & 0 deletions user/mpy/build.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
TOP = user/mpy/micropython
PY_SRC ?= $(TOP)/py
BUILD = $(TOP)/build
PY_BUILD = $(BUILD)/py

# where autogenerated header files go
HEADER_BUILD = $(BUILD)/genhdr

# file containing qstr defs for the core Python bit
PY_QSTR_DEFS = $(PY_SRC)/qstrdefs.h

$(USER): | $(HEADER_BUILD)/qstrdefs.generated.h $(HEADER_BUILD)/mpversion.h

include $(TOP)/../py.mk

# If qstr autogeneration is not disabled we specify the output header
# for all collected qstrings.
ifneq ($(QSTR_AUTOGEN_DISABLE),1)
QSTR_DEFS_COLLECTED = $(HEADER_BUILD)/qstrdefs.collected.h
endif

CP = cp
MKDIR = mkdir
SED = sed
PYTHON = python3

CFLAGS += \
-Iuser/mpy \
-I$(TOP) \
-I$(BUILD) \

CSRC += \
$(wildcard $(PY_SRC)/*.c) \
user/mpy/mphalport.c \
user/mpy/_frozen_mpy.c \
$(TOP)/lib/utils/pyexec.c \
$(TOP)/lib/mp-readline/readline.c \
# $(TOP)/extmod/moductypes.o \
# $(TOP)/extmod/modujson.o \
# $(TOP)/extmod/modure.o \
# $(TOP)/extmod/moduzlib.o \
# $(TOP)/extmod/moduheapq.o \
# $(TOP)/extmod/modutimeq.o \
# $(TOP)/extmod/moduhashlib.o \
# $(TOP)/extmod/modubinascii.o \
# $(TOP)/extmod/virtpin.o \
# $(TOP)/extmod/machine_mem.o \
# $(TOP)/extmod/machine_pinbase.o \
# $(TOP)/extmod/machine_signal.o \
# $(TOP)/extmod/machine_pulse.o \
# $(TOP)/extmod/machine_i2c.o \
# $(TOP)/extmod/machine_spi.o \
# $(TOP)/extmod/modussl_axtls.o \
# $(TOP)/extmod/modussl_mbedtls.o \
# $(TOP)/extmod/modurandom.o \
# $(TOP)/extmod/moduselect.o \
# $(TOP)/extmod/modwebsocket.o \
# $(TOP)/extmod/modwebrepl.o \
# $(TOP)/extmod/modframebuf.o \
# $(TOP)/extmod/vfs.o \
# $(TOP)/extmod/vfs_reader.o \
# $(TOP)/extmod/vfs_fat.o \
# $(TOP)/extmod/vfs_fat_diskio.o \
# $(TOP)/extmod/vfs_fat_file.o \
# $(TOP)/extmod/vfs_fat_misc.o \
# $(TOP)/extmod/utime_mphal.o \
# $(TOP)/extmod/uos_dupterm.o \
# $(TOP)/lib/embed/abort_.o \

include $(TOP)/py/mkrules.mk
16 changes: 16 additions & 0 deletions user/mpy/forzentest.mpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
4d02 021f 5c03 0000 0000 000d 0a00 f700
2a28 2828 2b28 0000 ff1b c700 16f8 0064
0132 1bc7 0017 0064 0132 1bc7 0017 0164
0132 1bc7 0017 0264 0132 1bc7 0014 baef
9a15 6401 3280 350f 8030 24f9 001b c700
1bf9 0064 0132 81e9 3084 f036 eb7f 3211
5b08 3c6d 6f64 756c 653e 0d66 726f 7a65
6e74 6573 742e 7079 0570 7269 6e74 0375
5079 0570 7269 6e74 0570 7269 6e74 0570
7269 6e74 0570 7269 6e74 0169 0570 7269
6e74 0169 0300 7322 6120 6c6f 6e67 2073
7472 696e 6720 7468 6174 2069 7320 6e6f
7420 696e 7465 726e 6564 7326 6120 7374
7269 6e67 2074 6861 7420 6861 7320 756e
6963 6f64 6520 ceb1 ceb2 ceb3 2063 6861
7273 620b 6279 7465 7320 3132 3334 01
1 change: 1 addition & 0 deletions user/mpy/micropython
Submodule micropython added at d5191e
16 changes: 16 additions & 0 deletions user/mpy/mkrules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$(HEADER_BUILD)/qstr.i.last: $(SRC_QSTR) | $(HEADER_BUILD)/mpversion.h
$(ECHO) "GEN $@"
$(Q)$(CPP) $(QSTR_GEN_EXTRA_CFLAGS) $(CFLAGS) $(if $?,$?,$^) >$(HEADER_BUILD)/qstr.i.last;

$(HEADER_BUILD)/qstr.split: $(HEADER_BUILD)/qstr.i.last
$(ECHO) "GEN $@"
$(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py split $(HEADER_BUILD)/qstr.i.last $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)
$(Q)touch $@

$(QSTR_DEFS_COLLECTED): $(HEADER_BUILD)/qstr.split
$(ECHO) "GEN $@"
$(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py cat $(HEADER_BUILD)/qstr.i.last $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)

$(BUILD)/_frozen_mpy.c: user/mpy/forzentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h
@echo "MISC freezing bytecode"
user/mpy/tools/mpy-tool.py -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=none $< > $@
97 changes: 97 additions & 0 deletions user/mpy/mpconfigport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <stdint.h>

// options to control how MicroPython is built

// You can disable the built-in MicroPython compiler by setting the following
// config option to 0. If you do this then you won't get a REPL prompt, but you
// will still be able to execute pre-compiled scripts, compiled with mpy-cross.
#define MICROPY_ENABLE_COMPILER (1)

#define MICROPY_QSTR_BYTES_IN_HASH (1)
#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool
#define MICROPY_ALLOC_PATH_MAX (256)
#define MICROPY_ALLOC_PARSE_CHUNK_INIT (16)
#define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (0)
#define MICROPY_EMIT_INLINE_THUMB (0)
#define MICROPY_COMP_MODULE_CONST (0)
#define MICROPY_COMP_CONST (0)
#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0)
#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (0)
#define MICROPY_MEM_STATS (0)
#define MICROPY_DEBUG_PRINTERS (0)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_GC_ALLOC_THRESHOLD (0)
#define MICROPY_REPL_EVENT_DRIVEN (0)
#define MICROPY_HELPER_REPL (1)
#define MICROPY_HELPER_LEXER_UNIX (0)
#define MICROPY_ENABLE_SOURCE_LINE (0)
#define MICROPY_ENABLE_DOC_STRING (0)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
#define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0)
#define MICROPY_PY_ASYNC_AWAIT (0)
#define MICROPY_PY_BUILTINS_BYTEARRAY (0)
#define MICROPY_PY_BUILTINS_MEMORYVIEW (0)
#define MICROPY_PY_BUILTINS_ENUMERATE (0)
#define MICROPY_PY_BUILTINS_FILTER (0)
#define MICROPY_PY_BUILTINS_FROZENSET (0)
#define MICROPY_PY_BUILTINS_REVERSED (0)
#define MICROPY_PY_BUILTINS_SET (0)
#define MICROPY_PY_BUILTINS_SLICE (0)
#define MICROPY_PY_BUILTINS_PROPERTY (0)
#define MICROPY_PY_BUILTINS_MIN_MAX (0)
#define MICROPY_PY___FILE__ (0)
#define MICROPY_PY_GC (0)
#define MICROPY_PY_ARRAY (0)
#define MICROPY_PY_ATTRTUPLE (0)
#define MICROPY_PY_COLLECTIONS (0)
#define MICROPY_PY_MATH (0)
#define MICROPY_PY_CMATH (0)
#define MICROPY_PY_IO (0)
#define MICROPY_PY_STRUCT (0)
#define MICROPY_PY_SYS (0)
#define MICROPY_MODULE_FROZEN_MPY (1)
#define MICROPY_CPYTHON_COMPAT (0)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)

// type definitions for the specific machine

#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1))

// This port is intended to be 32-bit, but unfortunately, int32_t for
// different targets may be defined in different ways - either as int
// or as long. This requires different printf formatting specifiers
// to print such value. So, we avoid int32_t and use int directly.
#define UINT_FMT "%u"
#define INT_FMT "%d"
typedef int mp_int_t; // must be pointer size
typedef unsigned mp_uint_t; // must be pointer size

typedef long mp_off_t;

#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)

// extra built in names to add to the global namespace
#define MICROPY_PORT_BUILTINS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },

// We need to provide a declaration/definition of alloca()
#include <alloca.h>

#define MICROPY_HW_BOARD_NAME "minimal"
#define MICROPY_HW_MCU_NAME "unknown-cpu"

#ifdef __linux__
#define MICROPY_MIN_USE_STDOUT (1)
#endif

#ifdef __thumb__
#define MICROPY_MIN_USE_CORTEX_CPU (1)
#define MICROPY_MIN_USE_STM32_MCU (1)
#endif

#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8];
18 changes: 18 additions & 0 deletions user/mpy/mphalport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "platform.h"
#include "py/mphal.h"


int mp_hal_stdin_rx_chr() {
char c;
int fd = open("/dev/ttyS0", 0);

read(fd, &c, 1);
close(fd);
return c;
}

void mp_hal_stdout_tx_strn(const char *s, mp_uint_t n) {
while (n--) {
__printk_putchar(*s++);
}
}
4 changes: 4 additions & 0 deletions user/mpy/mphalport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#define mp_hal_stdout_tx_strn_cooked(s, l) printk(s);
#define mp_hal_stdout_tx_str(s) printk(s);
#define mp_hal_set_interrupt_char(n) (0)
#define mp_hal_ticks_ms() (0)
Loading