diff --git a/pkg/micropython/Makefile b/pkg/micropython/Makefile new file mode 100644 index 0000000000..f29342605e --- /dev/null +++ b/pkg/micropython/Makefile @@ -0,0 +1,14 @@ +PKG_NAME=micropython +PKG_URL=https://github.com/kaspar030/micropython +PKG_VERSION=5c45688d431a4d0f626d86478ad490cfb6d8ac30 +PKG_LICENSE=MIT + +CFLAGS += -Wno-implicit-fallthrough -Wno-unused-parameter -Wno-error + +.PHONY: all + +all: + @mkdir -p $(PKG_BUILDDIR)/tmp + BUILD=$(PKG_BUILDDIR) "$(MAKE)" -C $(PKG_BUILDDIR)/ports/riot + +include $(RIOTBASE)/pkg/pkg.mk diff --git a/pkg/micropython/Makefile.dep b/pkg/micropython/Makefile.dep new file mode 100644 index 0000000000..af55a86953 --- /dev/null +++ b/pkg/micropython/Makefile.dep @@ -0,0 +1,11 @@ +USEMODULE += xtimer +USEMODULE += stdin + +# MicroPython doesn't compile for <32bit platforms +FEATURES_BLACKLIST += arch_8bit arch_16bit + +# This port currently requires ISR_STACKSIZE and thread_isr_stack_start +FEATURES_BLACKLIST += arch_arm7 arch_esp32 arch_esp8266 arch_riscv + +# The port currently doesn't compile for mips +FEATURES_BLACKLIST += arch_mips32r2 diff --git a/pkg/micropython/Makefile.include b/pkg/micropython/Makefile.include new file mode 100644 index 0000000000..b19a7006b7 --- /dev/null +++ b/pkg/micropython/Makefile.include @@ -0,0 +1,12 @@ +# configuration +MP_RIOT_HEAPSIZE ?= 16384U + +CFLAGS += -DMP_RIOT_HEAPSIZE=$(MP_RIOT_HEAPSIZE) + +# include paths +INCLUDES += -I$(RIOTBASE)/pkg/micropython/include +INCLUDES += -I$(PKGDIRBASE)/micropython +INCLUDES += -I$(PKGDIRBASE)/micropython/ports/riot + +# The port currently doesn't build with llvm +TOOLCHAINS_BLACKLIST += llvm diff --git a/pkg/micropython/doc.txt b/pkg/micropython/doc.txt new file mode 100644 index 0000000000..4daafcf9de --- /dev/null +++ b/pkg/micropython/doc.txt @@ -0,0 +1,87 @@ +/** + * @defgroup pkg_micropython MicroPython RIOT port + * @ingroup pkg + * @brief MicroPython - Python for microcontrollers + * + * # MicroPython RIOT package + * + * "MicroPython is a lean and efficient implementation of the Python 3 + * programming language that includes a small subset of the Python standard + * library and is optimised to run on microcontrollers and in constrained + * environments." + * + * @see https://micropython.org + * + * ## Status + * + * MicroPython on RIOT has to be considered experimental. While the basic + * interpreter works fairly well on native and Cortex-M, it has not seen much + * testing. + * + * ## Configuration options + * + * Use the following environment variables in the application Makefile + * or from the command line to configure MicroPython: + * + * MP_RIOT_HEAPSIZE: heap size for MicroPython, in bytes. Defaults to 16KiB. + * + * Example on the command line: + * ``` + * MP_RIOT_HEAPSIZE=2048 make -C examples/micropython + * ``` + * + * ## Implementation details + * + * The RIOT port of MicroPython currently resides in a fork at + * https://github.com/kaspar030/micropython (in branch add_riot_port). It is + * based on Micropython's "ports/minimal" with some extra modules enabled. + * It re-uses the gc_collect code from ports/unix, which has special support + * for i386 and Cortex-M. On other platforms, it uses setjmp() to collect + * registers. + * + * ## MicroPython's test suite + * + * It is possible to run MicroPython's test suite for testing this port. + * + * Steps: + * + * 1. make -Cexamples/micropython flash + * 2. cd examples/micropython/bin/pkg/${BOARD}/micropython + * 3. git apply ports/riot/slow_uart_writes.patch + * 4. cd tests + * 5. ./run-tests --target pyboard --device ${PORT} + * + * ## MicroPython modules + * + * Currently, the port enables only a subset of the available MycroPython + * modules. See "ports/riot/mpconfigport.h" for details. + * + * For now, the utime module has RIOT specific code and should work as expected. + * + * ## RIOT specific modules + * + * Currently, these are implemented: + * + * ### thread_getpid() + * + * >>> import riot + * >>> print(riot.thread_getpid()) + + * ### xtimer + * + * >>> import xtimer + * >>> + * >>> a = 0 + * >>> def inc_a(): + * >>> global a + * >>> a += 1 + * >>> + * >>> t = xtimer.xtimer(inc_a) + * >>> t.set(100000) + * >>> print(a) + * + * ## How to use + * + * See examples/micropython for example code. + * + */ diff --git a/pkg/micropython/include/micropython.h b/pkg/micropython/include/micropython.h new file mode 100644 index 0000000000..9c4839166c --- /dev/null +++ b/pkg/micropython/include/micropython.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2019 Kaspar Schleiser + * 2019 Inria + * 2019 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup pkg_micropython + * + * @{ + * + * @file + * @brief MicroPython RIOT specific API + * + * @author Kaspar Schleiser + */ + +#ifndef MICROPYTHON_H +#define MICROPYTHON_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef MP_RIOT_HEAPSIZE +/* use a reasonable default heap size */ +#define MP_RIOT_HEAPSIZE (16U*1024) +#endif + +#ifndef MP_STACK_SAFEAREA +#define MP_STACK_SAFEAREA (128U) +#endif + +/** + * @brief Initialize RIOT MicroPython port + * + * @param[in] heap ptr to heap MicroPython should use + * @param[in] heap_size size of heap + */ +void mp_riot_init(char* heap, size_t heap_size); + +/** + * @brief Execute a string as MicroPython code + * + * The string will be executed on the global MicroPython instance. + * + * @param[in] src pointer to Python code + * @param[in] len length of src + */ +void mp_do_str(const char *src, int len); + +#endif /* MICROPYTHON_H */ +/** @} */