summaryrefslogtreecommitdiffstatshomepage
path: root/zephyr
diff options
context:
space:
mode:
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