summaryrefslogtreecommitdiffstatshomepage
path: root/zephyr
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-08-14 12:47:47 +1000
committerDamien George <damien.p.george@gmail.com>2017-08-14 12:47:47 +1000
commitf6e6776d6f8233432615f1ef438cbd1447c51aac (patch)
tree34589f98ab4d89914025b7d4ee10d2801cd80b3d /zephyr
parentd9d9b0a3005a810543724622374cc26a5399bd0e (diff)
parentc8d31585a00616b39839d112b7c696dafed6b08d (diff)
downloadmicropython-f6e6776d6f8233432615f1ef438cbd1447c51aac.tar.gz
micropython-f6e6776d6f8233432615f1ef438cbd1447c51aac.zip
Merge tag 'v1.8.5' into parse-bytecode
New port to Zephyr, upip runs on baremetal, and reduction in code size This release adds a new port of MicroPython to run on top of the Zephyr real-time operating system. As part of this there is now basic support for using mbedTLS as the ussl module. The release also brings initial support for running the package manager upip on baremetal systems with low heap memory (such as esp8266), through a Python module interface. Work has been done in this release to clean up the core, removing redundant and/or unreachable code, and factoring existing duplicated code patterns. This brings a reduction of 828 bytes in code size to the bare-arm port, and 1368 bytes to the minimal port. There is also improved coverage through the addition of new tests for corner cases. The "micropython" module sees the addition of the "const" identity function which should be used as "from micropython import const" in scripts that want to use the MicroPython constant optimisations in the compile stage. There is also the addition of the "opt_level" function to change the parser/compiler optimisation level at runtime. The behaviour of "sys.exit" (and "raise SystemExit") on baremetal is now changed: this function no longer does a soft-reset of the board, rather it just stops the running script and drops to the REPL. In order to do an actual soft reset the "machine.soft_reset" function has been added (to the stmhal port only, for the time being). Following CPython, KeyError exceptions for dictionary lookups now have the failed key stored as the argument of the exception instance, accessible as exc.args[0]. The "ujson.load" function has also been added, to load JSON data from an arbitrary stream. The I2C support in the machine module now has clock stretching, the addition of the "addrsize" parameter in memory transfer methods, and I2C scanning now uses dummy writes instead of dummy reads to make the scanning more reliable. The CMSIS library has been upgrade to v4.30, and the boards section of the stmhal port has been refactored to use a common.ld script. The stmhal port now has a full implementation of the machine.SPI class, with support for both hardware SPI peripherals and fast software SPI. The USB HID driver in stmhal has added support to receive USB HID messages from the host. py core: - asmthumb: flush D-cache, and invalidate I-cache on STM32F7 - makeqstrdefs.py: use python 2.6 syntax for set creation - objnone: remove unnecessary handling of MP_UNARY_OP_BOOL - move frozen modules rules from esp8266 port for reuse across ports - combine 3 comprehension emit functions (list/dict/set) into 1 - combine 3 comprehension opcodes (list/dict/set) into 1 - vstr: remove vstr.had_error flag and inline basic vstr functions - objnone: use mp_generic_unary_op instead of custom one - showbc: make printf's go to the platform print stream - remove 'name' member from mp_obj_module_t struct - builtinimport: fix nanbox build after change to better handle -m modules - stream: remove unnecessary check for NULL return from vstr_extend - py.mk: suppress some compiler warnings when building berkeley-db - shrink mp_arg_t struct by using reduced-size integer members - update opcode format table because 3 opcodes were removed, 1 added - parse: only replace constants that are standalone identifiers - py.mk: add support for building modussl_mbedtls - only store the exception instance on Py stack in bytecode try block - vm: use MP_OBJ_FROM_PTR to cast a type to an object - modmicropython: add micropython.const, alias for identity function - objstr: remove unreachable function used only for terse error msgs - emitbc: remove/refactor unreachable code, to improve coverage - objfun: remove unnecessary check for viper fun with 5 or more args - objfun: use if instead of switch to check return value of VM execute - objset: use mp_check_self() to check args of set/frozenset methods - objset: ensure that use of frozenset.update raises an exception - compile: fix async-for/async-with to work with simpler exc on stack - scope: use lookup-table to determine a scope's simple name - scope: shrink scope_t struct by 1 machine word - scope: factor common code to find locals and close over them - compile: fix typo when checking for parse-node kind - argcheck: simplify if-chain so that the last one is the default - objbool: defer bool's unary op implementation to small int - objbool: make a slight simplification of bool constructor - modstruct: remove unreachable code, and add comment about CPy diff - add mp_raise_OSError(errno) helper function - objstringio: add readinto() method - modmicropython: add micropython.opt_level([value]) function - compile: remove unreachable code - mpz: in divmod, replace check for rhs!=0 with assert - mpz: use assert to verify mpz does not have a fixed digit buffer - factor duplicated function to calculate size of formatted int - objint: use size_t for arguments that measure bytes/sizes - compile: remove debugging code for compiler dispatch - lexer: remove unnecessary code, and unreachable code - vstr: combine vstr_new_size with vstr_new since they are rarely used - objdict: fix optimisation for allocating result in fromkeys - objdict: actually provide the key that failed in KeyError exception - use mp_raise_msg helper function where appropriate - add the module specified by -m to sys.modules as '__main__' extmod: - modussl_mbedtls: initial implementation of mbedTLS ussl module - uctypes: allow full 32-bit address range - modubinascii: fix crc32() function on 32-bit platforms - modussl_mbedtls: implement key= and cert= args to wrap_socket() - modussl_mbedtls: use 2-component include paths - machine_i2c: add clock stretching support - modussl_mbedtls: add server_hostname param for wrap_socket() - uzlib: add tinfgzip.c (gzip header parsing) from upstream - moduzlib: DecompIO: Add support for gzip-formatted streams - uzlib/: update uzlib to v2.0.3 - vfs_fat: add fat_vfs_statvfs(), reused from stmhal - machine_i2c: add support for the addrsize parameter in mem xfers - machine_spi: simplify SPI xfer function to only take one buf len - machine_spi: factor out software SPI code from esp8266 to extmod - machine_spi: use delay_half, not baudrate, for internal timing - machine_spi: add optional support for fast software SPI - vfs_fat: use mp_raise_OSError helper function - modlwip: use mp_raise_OSError helper function - use mp_raise_OSError helper function - vfs_fat_file: use MP_Exxx errno constants - uzlib: update to upstream v2.1 - machine_i2c: use writes not reads in i2c.scan() - vfs_fat: add file and directory checks for remove and rmdir - modujson: implement ujson.load() to load JSON from a stream - modujson: fix nanbox build - utime_mphal: factor out implementations in terms of mp_hal_* for reuse - utime_mphal: sleep_us/ms(): Don't wait on negative argument - modussl_mbedtls: add dummy setblocking() method lib: - interrupt_char: factor out typical Ctrl+C handling from esp8266 port - cmsis: move CMSIS headers to lib/ - cmsis: remove CMSIS-DSP headers, they are unused - cmsis: upgrade CMSIS-CORE to V4.30 - utils/pyexec: don't treat SystemExit as "forced exit" - utils/pyexec: allow behaviour of SystemExit to be configurable drivers: - dht: use mp_raise_OSError helper function tools: - update upip to 0.8, fixes IPv6 support - upgrade upip to 1.0, fully self-hosted release (without fallbacks), and uses stream gzip decompression (step towards support for limited-heap baremetal systems) - upgrade upip to 1.1.3, initial support for running on a baremetal, low-heap systems (like esp8266), using Python module interface tests: - pyb: add test for ExtInt when doing swint while disabled - pyb: update exp file for previously updated extint test - extmod/urandom: add urandom tests for error cases - basics: add errno1 test, to check basics of uerrno module - extmod: add test for machine.time_pulse_us() - struct1: test "l" specifier to improve coverage - array1: add tests for "l", "L" array types to improve coverage - get cmdline verbose tests running again - run-tests: add --via-mpy option to run test from precompiled code - uzlib_decompio_gz: test for DecompIO with gzip bitstream - basics: add test case for overflowing Py stack in try-finally - micropython: add tests for const names being replaced in parser - cmdline/cmd_showbc: fix test now that 1 value is stored on stack - extmod/vfs_fat_ramdisk: add test for VFS.statvfs() - float: add test for parsing a float from an empty string - basics: add test for set.difference_update with arg being itself - basics: add further tests for nonlocal scoping and closures - import: add test for compiling "import a.b as c" - basics: add test constructing a set from a non-trivial expression - basics: add test for printing OSError when errno is unknown - run-tests: disable cmdline/cmd_showbc test on Windows - extmod/btree1: checks for put, seq, string print and unsupported binary op - fix expected output of verbose cmdline test - extmod/uzlib: test adaptive huffman tree for tinflate coverage - improve coverage of struct with test for non-compliant behaviour - io/write_ext: add description comment - io/bytesio_ext: add test for readinto() - micropython: add test for micropython.opt_level() function - improve test coverage of py/compile.c - extmod/vfs_fat: test coverage for remove() and rmdir() - extmod: add test for ujson.load() - extmod/vfs_fat: replace asserts with prints and expected outputs - micropython: add tests for heap_lock, and emergency exceptions - cmdline: improve coverage test for printing bytecode - improve coverage of array, range, dict, slice, exc, unicode - add test to print full KeyError exc from failed dict lookup - run-tests: enable extmod/machine1.py on pyboard unix port: - fix build for when MICROPY_PY_SOCKET=0 - modjni: implement subscription for object arrays - modjni: add array() top-level function to create Java array - modjni: array(): Support creation of object arrays - enable btree module for coverage build - use mp_raise_OSError helper function - use common RAISE_ERRNO macro from mphalport.h windows port: - enable MICROPY_PY_UERRNO qemu-arm port: - enable lots of extmods and enable tests for them stmhal port: - lcd: de-assert chip select after completing SPI transmission - {accel,lcd}: use GPIO_{set,clear}_pin - extint: force 0 to 1 transition on swint() - boards: add pllvalues.py script to compute PLL values for sysclk - boards: for OLIMEX_E407, enable UART1 and fix I2C1 mapping - use attribute to avoid inlining - put common definitions from linker files to common.ld - remove STM32CubeF2 HAL files, they are unused/unsupported - modmachine: fix clearing of reset-cause flags - add virtual com port support for STM32L476DISC - remove CMSIS STM32F2xx files, they are unused/unsupported - spi: simplify spi_transfer function to take only one buf len arg - mphalport: implement mp_hal_pin_{input,output,write} - spi: make machine.SPI class conform to correct API - mphalport: fix mp_hal_pin_write to use correct pin_mask - spi: use software SPI if no periph id given, even if pins given - spi: enable use of fast software SPI - fix linker map for STM32L476 chips - usbdev: add OUT endpoint to HID interface - usb: add support to receive USB HID messages from host - usb: use correct ClassData structure for HID receive - usb: use real packet size (not maximum) in HID receive - fix ESPRUINO_PICO by adding ld scripts with correct flash size - mphalport: change pin obj type to const pointer, to avoid casts - moduos: implement total-number-of-blocks field in statvfs - disable network and usocket for ESPRUINO_PICO - enable machine.time_pulse_us() function - use mp_raise_OSError helper function - pybstdio: use size_t instead of mp_uint_t - modutime: refactor to use extmod/utime_mphal.c - implement machine.soft_reset() - enable str.center(), str.[r]partition() and builtin compile() cc3200 port: - add ssl_version argument to ssl.wrap_socket() esp8266 port: - Makefile: rename SCRIPTDIR to FROZEN_DIR for consistency with FROZEN_MPY_DIR - ets_alt_task: ets_post: Should return 0 on success, !0 - failure - esp_mphal: add tentative change to mp_hal_stdin_rx_chr() to wait IRQ - extend system microsecond counter to 64-bits; use in ticks_ms - add uos.statvfs() to get filesystem status - moduos: move stat/statvfs funcs to sit within #if VFS guard - modmachine: idle(): Return number of CPU cycles spent idling - main: put /lib before / in sys.path - modpybrtc.c: implement machine.RTC.alarm_left() - make PY_UHASHLIB_SHA1 config depend on PY_USSL and SSL_AXTLS - add FLASH_MODE,FLASH_SIZE options for make deploy target - use mp_raise_OSError helper function - make neopixel support configurable - mpconfigport: enable MICROPY_PY_BUILTINS_SLICE_ATTRS - enable sys.{stdin,stdout,stderr}.buffer for raw serial access - enable importing of precompiled .mpy files - enable micropython.alloc_emergency_exception_buf() zephyr port: - initial Zephyr RTOS port, MicroPython part - initial Zephyr RTOS port, Zephyr part - add zephyr_getchar module to handle console input - switch to microkernel, required for network to work in background - automatically derive ARCH - support extra make targets - Makefile: automatically derive target-specific CFLAGS - use recently added "make outputexports" Zephyr target - add README - enable stack checking and micropython.mem_info() - enable frozen modules support - main: execute main.py frozen module on boot, if available - zephyr_getchar: add support for Ctrl+C handling - add Ctrl+C handling - implement the help() function - add copyright blurbs README: - remove issue-stats badges, the service is no longer available - mention _thread module availability in select ports docs: - library/pyb.SPI: init(): describe "bits" argument - library/machine: update description of disable/enable IRQ funcs - uos: add uos.statvfs() documentation - wipy: correct deep sleep current figure - wipy: small doc fixes - reference: add constrained.rst doc travis: - abandon mingw32 in favour of mingw-w64 - run feature and coverage test for precompiled mpy files examples: - network/http_client*: use \r\n line-endings in request
Diffstat (limited to 'zephyr')
-rw-r--r--zephyr/Kbuild3
-rw-r--r--zephyr/Makefile86
-rw-r--r--zephyr/Makefile.zephyr27
-rw-r--r--zephyr/README.md45
-rw-r--r--zephyr/help.c56
-rw-r--r--zephyr/main.c128
-rw-r--r--zephyr/mpconfigport.h83
-rw-r--r--zephyr/mphalport.h1
-rw-r--r--zephyr/prj.conf4
-rw-r--r--zephyr/prj.mdef5
-rw-r--r--zephyr/src/Makefile17
-rw-r--r--zephyr/src/zephyr_getchar.c65
-rw-r--r--zephyr/src/zephyr_getchar.h20
-rw-r--r--zephyr/src/zephyr_start.c34
-rw-r--r--zephyr/uart_core.c47
-rw-r--r--zephyr/z_config.mk17
16 files changed, 638 insertions, 0 deletions
diff --git a/zephyr/Kbuild b/zephyr/Kbuild
new file mode 100644
index 0000000000..9e656d5f48
--- /dev/null
+++ b/zephyr/Kbuild
@@ -0,0 +1,3 @@
+#subdir-ccflags-y += -I$(SOURCE_DIR)/../mylib/include
+
+obj-y += src/
diff --git a/zephyr/Makefile b/zephyr/Makefile
new file mode 100644
index 0000000000..1db84cb320
--- /dev/null
+++ b/zephyr/Makefile
@@ -0,0 +1,86 @@
+#
+# 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.
+#
+
+BOARD ?= qemu_x86
+# Zephyr 1.5.0
+#OUTDIR_PREFIX =
+# Zephyr 1.6.0
+OUTDIR_PREFIX = $(BOARD)
+
+FROZEN_DIR = scripts
+
+# Zephyr (generated) config files - must be defined before include below
+Z_SYSGEN_H = outdir/$(OUTDIR_PREFIX)/misc/generated/sysgen/sysgen.h
+Z_EXPORTS = outdir/$(OUTDIR_PREFIX)/Makefile.export
+include $(Z_EXPORTS)
+
+include ../py/mkenv.mk
+include ../py/py.mk
+
+INC += -I.
+INC += -I..
+INC += -I$(BUILD)
+INC += -I$(ZEPHYR_BASE)/net/ip
+INC += -I$(ZEPHYR_BASE)/net/ip/contiki
+INC += -I$(ZEPHYR_BASE)/net/ip/contiki/os
+
+SRC_C = main.c \
+ help.c \
+ uart_core.c \
+ lib/utils/stdout_helpers.c \
+ lib/utils/printf.c \
+ lib/utils/pyexec.c \
+ lib/utils/interrupt_char.c \
+ lib/utils/pyhelp.c \
+ lib/mp-readline/readline.c \
+ $(BUILD)/frozen.c \
+ $(SRC_MOD)
+
+# List of sources for qstr extraction
+SRC_QSTR += $(SRC_C)
+
+OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
+
+CFLAGS = $(KBUILD_CFLAGS) $(NOSTDINC_FLAGS) $(ZEPHYRINCLUDE) \
+ -std=gnu99 -DNDEBUG $(INC)
+
+include ../py/mkrules.mk
+
+$(Z_EXPORTS):
+ $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) initconfig outputexports
+
+GENERIC_TARGETS = all zephyr qemu qemugdb flash debug
+KCONFIG_TARGETS = \
+ initconfig config nconfig menuconfig xconfig gconfig \
+ oldconfig silentoldconfig defconfig savedefconfig \
+ allnoconfig allyesconfig alldefconfig randconfig \
+ listnewconfig olddefconfig
+CLEAN_TARGETS = pristine mrproper
+
+$(GENERIC_TARGETS): $(LIBMICROPYTHON)
+$(CLEAN_TARGETS): clean
+
+$(GENERIC_TARGETS) $(KCONFIG_TARGETS) $(CLEAN_TARGETS):
+ $(RM) -f outdir/$(OUTDIR_PREFIX)/zephyr.lnk
+ $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) $@
+
+$(LIBMICROPYTHON): $(Z_SYSGEN_H)
+build/genhdr/qstr.i.last: $(Z_SYSGEN_H)
+
+$(Z_SYSGEN_H):
+ rm -f $(LIBMICROPYTHON)
+ -$(MAKE) -f Makefile.zephyr BOARD=$(BOARD)
+
+# Clean Zephyr things too
+clean: z_clean
+
+z_clean:
+ $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) clean
diff --git a/zephyr/Makefile.zephyr b/zephyr/Makefile.zephyr
new file mode 100644
index 0000000000..ad905a4dba
--- /dev/null
+++ b/zephyr/Makefile.zephyr
@@ -0,0 +1,27 @@
+#
+# Copyright (c) 2016 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+KERNEL_TYPE = micro
+# BOARD must be passed on command line from main Makefile
+#BOARD =
+CONF_FILE = prj.conf
+MDEF_FILE = prj.mdef
+
+#export SOURCE_DIR = $(ZEPHYR_BASE)/samples/static_lib/hello_world
+export LDFLAGS_zephyr += -L$(CURDIR)
+export ALL_LIBS += micropython
+
+include ${ZEPHYR_BASE}/Makefile.inc
diff --git a/zephyr/README.md b/zephyr/README.md
new file mode 100644
index 0000000000..63fb0e39f4
--- /dev/null
+++ b/zephyr/README.md
@@ -0,0 +1,45 @@
+MicroPython port to Zephyr RTOS
+===============================
+
+This is an initial port of MicroPython to Zephyr RTOS
+(http://zephyrproject.org).
+
+The port integrates well with Zephyr build system, using the latest
+features which will be available in 1.6.0, and thus requires Zephyr
+master to build against. All boards supported by Zephyr should be
+supported (but not all were tested).
+
+At this time, only basic interactive prompt (REPL) over UART connection
+is supported. Over time, bindings for various Zephyr subsystems may
+be added.
+
+
+Building
+--------
+
+Follow to Zephyr web site for Getting Started instruction of installing
+Zephyr SDK, getting Zephyr source code, and setting up development
+environment. (Direct link:
+https://www.zephyrproject.org/doc/getting_started/getting_started.html).
+You may want to build Zephyr's own sample applications to make sure your
+setup is correct.
+
+To build MicroPython port, in the port subdirectory (zephyr/), run:
+
+ make BOARD=<board>
+
+If you don't specify BOARD, the default is `qemu_x86` (x86 target running
+in QEMU emulator). Consult Zephyr documentation above for the list of
+supported boards.
+
+
+Running
+-------
+
+To run the resulting application in QEMU (for BOARDs like qemu_x86,
+qemu_cortex_m3):
+
+ make qemu
+
+For deploying/flashing the application on a real board, follow Zephyr
+documentation for a given board.
diff --git a/zephyr/help.c b/zephyr/help.c
new file mode 100644
index 0000000000..e574adf473
--- /dev/null
+++ b/zephyr/help.c
@@ -0,0 +1,56 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013-2016 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+
+#include "lib/utils/pyhelp.h"
+
+STATIC const char *help_text =
+"Welcome to MicroPython!\n"
+"\n"
+"Control commands:\n"
+" CTRL-A -- on a blank line, enter raw REPL mode\n"
+" CTRL-B -- on a blank line, enter normal REPL mode\n"
+" CTRL-C -- interrupt a running program\n"
+" CTRL-D -- on a blank line, do a soft reset of the board\n"
+" CTRL-E -- on a blank line, enter paste mode\n"
+"\n"
+"For further help on a specific object, type help(obj)\n"
+;
+
+STATIC mp_obj_t builtin_help(uint n_args, const mp_obj_t *args) {
+ if (n_args == 0) {
+ // print a general help message
+ printf("%s", help_text);
+
+ } else {
+ // try to print something sensible about the given object
+ pyhelp_print_obj(args[0]);
+ }
+
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, builtin_help);
diff --git a/zephyr/main.c b/zephyr/main.c
new file mode 100644
index 0000000000..8d319098b2
--- /dev/null
+++ b/zephyr/main.c
@@ -0,0 +1,128 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 Damien P. George
+ * Copyright (c) 2016 Linaro Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "py/nlr.h"
+#include "py/compile.h"
+#include "py/runtime.h"
+#include "py/repl.h"
+#include "py/gc.h"
+#include "py/stackctrl.h"
+#include "lib/utils/pyexec.h"
+#include "lib/mp-readline/readline.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;
+ mp_stack_set_top(stack_top);
+ // Should be set to stack size in prj.mdef minus fuzz factor
+ mp_stack_set_limit(3584);
+
+ #if MICROPY_ENABLE_GC
+ gc_init(heap, heap + sizeof(heap));
+ #endif
+ mp_init();
+ MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
+ pyexec_frozen_module("main.py");
+ #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..1654e79b8c
--- /dev/null
+++ b/zephyr/mpconfigport.h
@@ -0,0 +1,83 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Linaro Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include <alloca.h>
+
+// Include Zephyr's autoconf.h, which should be made first by Zephyr makefiles
+#include "autoconf.h"
+
+// 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_STACK_CHECK (1)
+#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_MICROPYTHON_MEM_INFO (1)
+#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"
+#define MICROPY_MODULE_FROZEN_STR (1)
+
+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 \
+ mp_obj_t mp_kbd_exception; \
+ const char *readline_hist[8];
+
+// extra built in names to add to the global namespace
+#define MICROPY_PORT_BUILTINS \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_help), (mp_obj_t)&mp_builtin_help_obj }, \
+
diff --git a/zephyr/mphalport.h b/zephyr/mphalport.h
new file mode 100644
index 0000000000..1bb64e0002
--- /dev/null
+++ b/zephyr/mphalport.h
@@ -0,0 +1 @@
+static inline mp_uint_t mp_hal_ticks_ms(void) { return 0; }
diff --git a/zephyr/prj.conf b/zephyr/prj.conf
new file mode 100644
index 0000000000..35cb036ded
--- /dev/null
+++ b/zephyr/prj.conf
@@ -0,0 +1,4 @@
+CONFIG_STDOUT_CONSOLE=y
+CONFIG_CONSOLE_HANDLER=y
+CONFIG_NEWLIB_LIBC=y
+CONFIG_FLOAT=y
diff --git a/zephyr/prj.mdef b/zephyr/prj.mdef
new file mode 100644
index 0000000000..5a7312f89f
--- /dev/null
+++ b/zephyr/prj.mdef
@@ -0,0 +1,5 @@
+% Application : MicroPython
+
+% TASK NAME PRIO ENTRY STACK GROUPS
+% ==================================
+ TASK MAIN 7 main 4096 [EXE]
diff --git a/zephyr/src/Makefile b/zephyr/src/Makefile
new file mode 100644
index 0000000000..36dd8c64ef
--- /dev/null
+++ b/zephyr/src/Makefile
@@ -0,0 +1,17 @@
+#
+# Copyright (c) 2016 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+obj-y += zephyr_start.o zephyr_getchar.o
diff --git a/zephyr/src/zephyr_getchar.c b/zephyr/src/zephyr_getchar.c
new file mode 100644
index 0000000000..89e3e0efbc
--- /dev/null
+++ b/zephyr/src/zephyr_getchar.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016 Linaro
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <zephyr.h>
+#include <uart.h>
+#include <drivers/console/uart_console.h>
+#include <misc/printk.h>
+#include "zephyr_getchar.h"
+
+extern int mp_interrupt_char;
+void mp_keyboard_interrupt(void);
+
+static struct nano_sem uart_sem;
+#define UART_BUFSIZE 256
+static uint8_t uart_ringbuf[UART_BUFSIZE];
+static uint8_t i_get, i_put;
+
+static int console_irq_input_hook(struct device *dev, uint8_t ch)
+{
+ int i_next = (i_put + 1) & (UART_BUFSIZE - 1);
+ if (i_next == i_get) {
+ printk("UART buffer overflow - char dropped\n");
+ return 1;
+ }
+ if (ch == mp_interrupt_char) {
+ mp_keyboard_interrupt();
+ return 1;
+ } else {
+ uart_ringbuf[i_put] = ch;
+ i_put = i_next;
+ }
+ //printk("%x\n", ch);
+ nano_isr_sem_give(&uart_sem);
+ return 1;
+}
+
+uint8_t zephyr_getchar(void) {
+ nano_task_sem_take(&uart_sem, TICKS_UNLIMITED);
+ unsigned int key = irq_lock();
+ uint8_t c = uart_ringbuf[i_get++];
+ i_get &= UART_BUFSIZE - 1;
+ irq_unlock(key);
+ return c;
+}
+
+void zephyr_getchar_init(void) {
+ nano_sem_init(&uart_sem);
+ struct device *uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
+ uart_irq_input_hook_set(uart_console_dev, console_irq_input_hook);
+ // All NULLs because we're interested only in the callback above
+ uart_register_input(NULL, NULL, NULL);
+}
diff --git a/zephyr/src/zephyr_getchar.h b/zephyr/src/zephyr_getchar.h
new file mode 100644
index 0000000000..fb5f19a7b4
--- /dev/null
+++ b/zephyr/src/zephyr_getchar.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2016 Linaro
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdint.h>
+
+void zephyr_getchar_init(void);
+uint8_t zephyr_getchar(void);
diff --git a/zephyr/src/zephyr_start.c b/zephyr/src/zephyr_start.c
new file mode 100644
index 0000000000..9e8a90bebd
--- /dev/null
+++ b/zephyr/src/zephyr_start.c
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Linaro Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include <zephyr.h>
+#include "zephyr_getchar.h"
+
+int real_main(void);
+
+void main(void) {
+ zephyr_getchar_init();
+ real_main();
+}
diff --git a/zephyr/uart_core.c b/zephyr/uart_core.c
new file mode 100644
index 0000000000..702c97d20a
--- /dev/null
+++ b/zephyr/uart_core.c
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Linaro Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include <unistd.h>
+#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++);
+ }
+}
diff --git a/zephyr/z_config.mk b/zephyr/z_config.mk
new file mode 100644
index 0000000000..28addd8f29
--- /dev/null
+++ b/zephyr/z_config.mk
@@ -0,0 +1,17 @@
+srctree = $(ZEPHYR_BASE)
+
+include $(Z_DOTCONFIG)
+override ARCH = $(subst $(DQUOTE),,$(CONFIG_ARCH))
+SOC_NAME = $(subst $(DQUOTE),,$(CONFIG_SOC))
+SOC_SERIES = $(subst $(DQUOTE),,$(CONFIG_SOC_SERIES))
+SOC_FAMILY = $(subst $(DQUOTE),,$(CONFIG_SOC_FAMILY))
+ifeq ($(SOC_SERIES),)
+SOC_PATH = $(SOC_NAME)
+else
+SOC_PATH = $(SOC_FAMILY)/$(SOC_SERIES)
+endif
+
+KBUILD_CFLAGS := -c
+include $(ZEPHYR_BASE)/scripts/Kbuild.include
+
+include $(ZEPHYR_BASE)/arch/$(ARCH)/Makefile