Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.

Dependents:   micropython-repl

This a port of MicroPython to the mbed Classic platform.

This provides an interpreter running on the board's USB serial connection.

Getting Started

Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:

  MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM
  Type "help()" for more information.
  >>>

Then you can start using micropython. For example:

  >>> from mbed import DigitalOut
  >>> from pins import LED1
  >>> led = DigitalOut(LED1)
  >>> led.write(1)

Requirements

You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.

Caveats

This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.

  • Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
  • Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
  • The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
  • Exceptions from the mbed classes are not yet handled.
  • Asynchronous processing (e.g. events on inputs) is not supported.

Credits

  • Damien P. George and other contributors who created micropython.
  • Colin Hogben, author of this port.
Committer:
Colin Hogben
Date:
Wed Apr 27 22:11:29 2016 +0100
Revision:
10:33521d742af1
Parent:
0:5868e8752d44
Update README and version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythontech 0:5868e8752d44 1 /*
pythontech 0:5868e8752d44 2 * This file is part of the Micro Python project, http://micropython.org/
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * The MIT License (MIT)
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Copyright (c) 2014 Damien P. George
pythontech 0:5868e8752d44 7 *
pythontech 0:5868e8752d44 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 9 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 10 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 12 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 13 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 14 *
pythontech 0:5868e8752d44 15 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 16 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 17 *
pythontech 0:5868e8752d44 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 24 * THE SOFTWARE.
pythontech 0:5868e8752d44 25 */
pythontech 0:5868e8752d44 26
pythontech 0:5868e8752d44 27 #include <stdio.h>
pythontech 0:5868e8752d44 28 #include <string.h>
pythontech 0:5868e8752d44 29 #include <assert.h>
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 #include "py/nlr.h"
pythontech 0:5868e8752d44 32 #include "py/runtime0.h"
pythontech 0:5868e8752d44 33 #include "py/runtime.h"
pythontech 0:5868e8752d44 34 #include "py/emitglue.h"
pythontech 0:5868e8752d44 35 #include "py/bc.h"
pythontech 0:5868e8752d44 36
pythontech 0:5868e8752d44 37 #if 0 // print debugging info
pythontech 0:5868e8752d44 38 #define DEBUG_printf DEBUG_printf
pythontech 0:5868e8752d44 39 #else // don't print debugging info
pythontech 0:5868e8752d44 40 #define DEBUG_printf(...) (void)0
pythontech 0:5868e8752d44 41 #endif
pythontech 0:5868e8752d44 42
pythontech 0:5868e8752d44 43 #if MICROPY_EMIT_NATIVE
pythontech 0:5868e8752d44 44
pythontech 0:5868e8752d44 45 // convert a Micro Python object to a valid native value based on type
pythontech 0:5868e8752d44 46 mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
pythontech 0:5868e8752d44 47 DEBUG_printf("mp_convert_obj_to_native(%p, " UINT_FMT ")\n", obj, type);
pythontech 0:5868e8752d44 48 switch (type & 0xf) {
pythontech 0:5868e8752d44 49 case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
pythontech 0:5868e8752d44 50 case MP_NATIVE_TYPE_BOOL:
pythontech 0:5868e8752d44 51 case MP_NATIVE_TYPE_INT:
pythontech 0:5868e8752d44 52 case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj);
pythontech 0:5868e8752d44 53 default: { // cast obj to a pointer
pythontech 0:5868e8752d44 54 mp_buffer_info_t bufinfo;
pythontech 0:5868e8752d44 55 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) {
pythontech 0:5868e8752d44 56 return (mp_uint_t)bufinfo.buf;
pythontech 0:5868e8752d44 57 } else {
pythontech 0:5868e8752d44 58 // assume obj is an integer that represents an address
pythontech 0:5868e8752d44 59 return mp_obj_get_int_truncated(obj);
pythontech 0:5868e8752d44 60 }
pythontech 0:5868e8752d44 61 }
pythontech 0:5868e8752d44 62 }
pythontech 0:5868e8752d44 63 }
pythontech 0:5868e8752d44 64
pythontech 0:5868e8752d44 65 #endif
pythontech 0:5868e8752d44 66
pythontech 0:5868e8752d44 67 #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
pythontech 0:5868e8752d44 68
pythontech 0:5868e8752d44 69 // convert a native value to a Micro Python object based on type
pythontech 0:5868e8752d44 70 mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) {
pythontech 0:5868e8752d44 71 DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
pythontech 0:5868e8752d44 72 switch (type & 0xf) {
pythontech 0:5868e8752d44 73 case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val;
pythontech 0:5868e8752d44 74 case MP_NATIVE_TYPE_BOOL: return mp_obj_new_bool(val);
pythontech 0:5868e8752d44 75 case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val);
pythontech 0:5868e8752d44 76 case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val);
pythontech 0:5868e8752d44 77 default: // a pointer
pythontech 0:5868e8752d44 78 // we return just the value of the pointer as an integer
pythontech 0:5868e8752d44 79 return mp_obj_new_int_from_uint(val);
pythontech 0:5868e8752d44 80 }
pythontech 0:5868e8752d44 81 }
pythontech 0:5868e8752d44 82
pythontech 0:5868e8752d44 83 #endif
pythontech 0:5868e8752d44 84
pythontech 0:5868e8752d44 85 #if MICROPY_EMIT_NATIVE
pythontech 0:5868e8752d44 86
pythontech 0:5868e8752d44 87 // wrapper that accepts n_args and n_kw in one argument
pythontech 0:5868e8752d44 88 // (native emitter can only pass at most 3 arguments to a function)
pythontech 0:5868e8752d44 89 mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args_kw, const mp_obj_t *args) {
pythontech 0:5868e8752d44 90 return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
pythontech 0:5868e8752d44 91 }
pythontech 0:5868e8752d44 92
pythontech 0:5868e8752d44 93 // wrapper that makes raise obj and raises it
pythontech 0:5868e8752d44 94 // END_FINALLY opcode requires that we don't raise if o==None
pythontech 0:5868e8752d44 95 void mp_native_raise(mp_obj_t o) {
pythontech 0:5868e8752d44 96 if (o != mp_const_none) {
pythontech 0:5868e8752d44 97 nlr_raise(mp_make_raise_obj(o));
pythontech 0:5868e8752d44 98 }
pythontech 0:5868e8752d44 99 }
pythontech 0:5868e8752d44 100
pythontech 0:5868e8752d44 101 // these must correspond to the respective enum in runtime0.h
pythontech 0:5868e8752d44 102 void *const mp_fun_table[MP_F_NUMBER_OF] = {
pythontech 0:5868e8752d44 103 mp_convert_obj_to_native,
pythontech 0:5868e8752d44 104 mp_convert_native_to_obj,
pythontech 0:5868e8752d44 105 mp_load_name,
pythontech 0:5868e8752d44 106 mp_load_global,
pythontech 0:5868e8752d44 107 mp_load_build_class,
pythontech 0:5868e8752d44 108 mp_load_attr,
pythontech 0:5868e8752d44 109 mp_load_method,
pythontech 0:5868e8752d44 110 mp_store_name,
pythontech 0:5868e8752d44 111 mp_store_global,
pythontech 0:5868e8752d44 112 mp_store_attr,
pythontech 0:5868e8752d44 113 mp_obj_subscr,
pythontech 0:5868e8752d44 114 mp_obj_is_true,
pythontech 0:5868e8752d44 115 mp_unary_op,
pythontech 0:5868e8752d44 116 mp_binary_op,
pythontech 0:5868e8752d44 117 mp_obj_new_tuple,
pythontech 0:5868e8752d44 118 mp_obj_new_list,
pythontech 0:5868e8752d44 119 mp_obj_list_append,
pythontech 0:5868e8752d44 120 mp_obj_new_dict,
pythontech 0:5868e8752d44 121 mp_obj_dict_store,
pythontech 0:5868e8752d44 122 #if MICROPY_PY_BUILTINS_SET
pythontech 0:5868e8752d44 123 mp_obj_new_set,
pythontech 0:5868e8752d44 124 mp_obj_set_store,
pythontech 0:5868e8752d44 125 #endif
pythontech 0:5868e8752d44 126 mp_make_function_from_raw_code,
pythontech 0:5868e8752d44 127 mp_native_call_function_n_kw,
pythontech 0:5868e8752d44 128 mp_call_method_n_kw,
pythontech 0:5868e8752d44 129 mp_call_method_n_kw_var,
pythontech 0:5868e8752d44 130 mp_getiter,
pythontech 0:5868e8752d44 131 mp_iternext,
pythontech 0:5868e8752d44 132 nlr_push,
pythontech 0:5868e8752d44 133 nlr_pop,
pythontech 0:5868e8752d44 134 mp_native_raise,
pythontech 0:5868e8752d44 135 mp_import_name,
pythontech 0:5868e8752d44 136 mp_import_from,
pythontech 0:5868e8752d44 137 mp_import_all,
pythontech 0:5868e8752d44 138 #if MICROPY_PY_BUILTINS_SLICE
pythontech 0:5868e8752d44 139 mp_obj_new_slice,
pythontech 0:5868e8752d44 140 #endif
pythontech 0:5868e8752d44 141 mp_unpack_sequence,
pythontech 0:5868e8752d44 142 mp_unpack_ex,
pythontech 0:5868e8752d44 143 mp_delete_name,
pythontech 0:5868e8752d44 144 mp_delete_global,
pythontech 0:5868e8752d44 145 mp_obj_new_cell,
pythontech 0:5868e8752d44 146 mp_make_closure_from_raw_code,
pythontech 0:5868e8752d44 147 mp_setup_code_state,
pythontech 0:5868e8752d44 148 };
pythontech 0:5868e8752d44 149
pythontech 0:5868e8752d44 150 /*
pythontech 0:5868e8752d44 151 void mp_f_vector(mp_fun_kind_t fun_kind) {
pythontech 0:5868e8752d44 152 (mp_f_table[fun_kind])();
pythontech 0:5868e8752d44 153 }
pythontech 0:5868e8752d44 154 */
pythontech 0:5868e8752d44 155
pythontech 0:5868e8752d44 156 #endif // MICROPY_EMIT_NATIVE