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) 2013, 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 #ifndef __MICROPY_INCLUDED_PY_RUNTIME0_H__
pythontech 0:5868e8752d44 27 #define __MICROPY_INCLUDED_PY_RUNTIME0_H__
pythontech 0:5868e8752d44 28
pythontech 0:5868e8752d44 29 // These must fit in 8 bits; see scope.h
pythontech 0:5868e8752d44 30 #define MP_SCOPE_FLAG_VARARGS (0x01)
pythontech 0:5868e8752d44 31 #define MP_SCOPE_FLAG_VARKEYWORDS (0x02)
pythontech 0:5868e8752d44 32 #define MP_SCOPE_FLAG_GENERATOR (0x04)
pythontech 0:5868e8752d44 33 #define MP_SCOPE_FLAG_DEFKWARGS (0x08)
pythontech 0:5868e8752d44 34
pythontech 0:5868e8752d44 35 // types for native (viper) function signature
pythontech 0:5868e8752d44 36 #define MP_NATIVE_TYPE_OBJ (0x00)
pythontech 0:5868e8752d44 37 #define MP_NATIVE_TYPE_BOOL (0x01)
pythontech 0:5868e8752d44 38 #define MP_NATIVE_TYPE_INT (0x02)
pythontech 0:5868e8752d44 39 #define MP_NATIVE_TYPE_UINT (0x03)
pythontech 0:5868e8752d44 40 #define MP_NATIVE_TYPE_PTR (0x04)
pythontech 0:5868e8752d44 41 #define MP_NATIVE_TYPE_PTR8 (0x05)
pythontech 0:5868e8752d44 42 #define MP_NATIVE_TYPE_PTR16 (0x06)
pythontech 0:5868e8752d44 43 #define MP_NATIVE_TYPE_PTR32 (0x07)
pythontech 0:5868e8752d44 44
pythontech 0:5868e8752d44 45 typedef enum {
pythontech 0:5868e8752d44 46 MP_UNARY_OP_BOOL, // __bool__
pythontech 0:5868e8752d44 47 MP_UNARY_OP_LEN, // __len__
pythontech 0:5868e8752d44 48 MP_UNARY_OP_HASH, // __hash__; must return a small int
pythontech 0:5868e8752d44 49 MP_UNARY_OP_POSITIVE,
pythontech 0:5868e8752d44 50 MP_UNARY_OP_NEGATIVE,
pythontech 0:5868e8752d44 51 MP_UNARY_OP_INVERT,
pythontech 0:5868e8752d44 52 MP_UNARY_OP_NOT,
pythontech 0:5868e8752d44 53 } mp_unary_op_t;
pythontech 0:5868e8752d44 54
pythontech 0:5868e8752d44 55 typedef enum {
pythontech 0:5868e8752d44 56 MP_BINARY_OP_OR,
pythontech 0:5868e8752d44 57 MP_BINARY_OP_XOR,
pythontech 0:5868e8752d44 58 MP_BINARY_OP_AND,
pythontech 0:5868e8752d44 59 MP_BINARY_OP_LSHIFT,
pythontech 0:5868e8752d44 60 MP_BINARY_OP_RSHIFT,
pythontech 0:5868e8752d44 61
pythontech 0:5868e8752d44 62 MP_BINARY_OP_ADD,
pythontech 0:5868e8752d44 63 MP_BINARY_OP_SUBTRACT,
pythontech 0:5868e8752d44 64 MP_BINARY_OP_MULTIPLY,
pythontech 0:5868e8752d44 65 MP_BINARY_OP_FLOOR_DIVIDE,
pythontech 0:5868e8752d44 66 MP_BINARY_OP_TRUE_DIVIDE,
pythontech 0:5868e8752d44 67
pythontech 0:5868e8752d44 68 MP_BINARY_OP_MODULO,
pythontech 0:5868e8752d44 69 MP_BINARY_OP_POWER,
pythontech 0:5868e8752d44 70 MP_BINARY_OP_DIVMOD, // not emitted by the compiler but supported by the runtime
pythontech 0:5868e8752d44 71 MP_BINARY_OP_INPLACE_OR,
pythontech 0:5868e8752d44 72 MP_BINARY_OP_INPLACE_XOR,
pythontech 0:5868e8752d44 73
pythontech 0:5868e8752d44 74 MP_BINARY_OP_INPLACE_AND,
pythontech 0:5868e8752d44 75 MP_BINARY_OP_INPLACE_LSHIFT,
pythontech 0:5868e8752d44 76 MP_BINARY_OP_INPLACE_RSHIFT,
pythontech 0:5868e8752d44 77 MP_BINARY_OP_INPLACE_ADD,
pythontech 0:5868e8752d44 78 MP_BINARY_OP_INPLACE_SUBTRACT,
pythontech 0:5868e8752d44 79
pythontech 0:5868e8752d44 80 MP_BINARY_OP_INPLACE_MULTIPLY,
pythontech 0:5868e8752d44 81 MP_BINARY_OP_INPLACE_FLOOR_DIVIDE,
pythontech 0:5868e8752d44 82 MP_BINARY_OP_INPLACE_TRUE_DIVIDE,
pythontech 0:5868e8752d44 83 MP_BINARY_OP_INPLACE_MODULO,
pythontech 0:5868e8752d44 84 MP_BINARY_OP_INPLACE_POWER,
pythontech 0:5868e8752d44 85
pythontech 0:5868e8752d44 86 // these should return a bool
pythontech 0:5868e8752d44 87 MP_BINARY_OP_LESS,
pythontech 0:5868e8752d44 88 MP_BINARY_OP_MORE,
pythontech 0:5868e8752d44 89 MP_BINARY_OP_EQUAL,
pythontech 0:5868e8752d44 90 MP_BINARY_OP_LESS_EQUAL,
pythontech 0:5868e8752d44 91 MP_BINARY_OP_MORE_EQUAL,
pythontech 0:5868e8752d44 92
pythontech 0:5868e8752d44 93 MP_BINARY_OP_NOT_EQUAL,
pythontech 0:5868e8752d44 94 MP_BINARY_OP_IN,
pythontech 0:5868e8752d44 95 MP_BINARY_OP_IS,
pythontech 0:5868e8752d44 96 MP_BINARY_OP_EXCEPTION_MATCH,
pythontech 0:5868e8752d44 97 // these are not supported by the runtime and must be synthesised by the emitter
pythontech 0:5868e8752d44 98 MP_BINARY_OP_NOT_IN,
pythontech 0:5868e8752d44 99 MP_BINARY_OP_IS_NOT,
pythontech 0:5868e8752d44 100 } mp_binary_op_t;
pythontech 0:5868e8752d44 101
pythontech 0:5868e8752d44 102 typedef enum {
pythontech 0:5868e8752d44 103 MP_F_CONVERT_OBJ_TO_NATIVE = 0,
pythontech 0:5868e8752d44 104 MP_F_CONVERT_NATIVE_TO_OBJ,
pythontech 0:5868e8752d44 105 MP_F_LOAD_NAME,
pythontech 0:5868e8752d44 106 MP_F_LOAD_GLOBAL,
pythontech 0:5868e8752d44 107 MP_F_LOAD_BUILD_CLASS,
pythontech 0:5868e8752d44 108 MP_F_LOAD_ATTR,
pythontech 0:5868e8752d44 109 MP_F_LOAD_METHOD,
pythontech 0:5868e8752d44 110 MP_F_STORE_NAME,
pythontech 0:5868e8752d44 111 MP_F_STORE_GLOBAL,
pythontech 0:5868e8752d44 112 MP_F_STORE_ATTR,
pythontech 0:5868e8752d44 113 MP_F_OBJ_SUBSCR,
pythontech 0:5868e8752d44 114 MP_F_OBJ_IS_TRUE,
pythontech 0:5868e8752d44 115 MP_F_UNARY_OP,
pythontech 0:5868e8752d44 116 MP_F_BINARY_OP,
pythontech 0:5868e8752d44 117 MP_F_BUILD_TUPLE,
pythontech 0:5868e8752d44 118 MP_F_BUILD_LIST,
pythontech 0:5868e8752d44 119 MP_F_LIST_APPEND,
pythontech 0:5868e8752d44 120 MP_F_BUILD_MAP,
pythontech 0:5868e8752d44 121 MP_F_STORE_MAP,
pythontech 0:5868e8752d44 122 #if MICROPY_PY_BUILTINS_SET
pythontech 0:5868e8752d44 123 MP_F_BUILD_SET,
pythontech 0:5868e8752d44 124 MP_F_STORE_SET,
pythontech 0:5868e8752d44 125 #endif
pythontech 0:5868e8752d44 126 MP_F_MAKE_FUNCTION_FROM_RAW_CODE,
pythontech 0:5868e8752d44 127 MP_F_NATIVE_CALL_FUNCTION_N_KW,
pythontech 0:5868e8752d44 128 MP_F_CALL_METHOD_N_KW,
pythontech 0:5868e8752d44 129 MP_F_CALL_METHOD_N_KW_VAR,
pythontech 0:5868e8752d44 130 MP_F_GETITER,
pythontech 0:5868e8752d44 131 MP_F_ITERNEXT,
pythontech 0:5868e8752d44 132 MP_F_NLR_PUSH,
pythontech 0:5868e8752d44 133 MP_F_NLR_POP,
pythontech 0:5868e8752d44 134 MP_F_NATIVE_RAISE,
pythontech 0:5868e8752d44 135 MP_F_IMPORT_NAME,
pythontech 0:5868e8752d44 136 MP_F_IMPORT_FROM,
pythontech 0:5868e8752d44 137 MP_F_IMPORT_ALL,
pythontech 0:5868e8752d44 138 #if MICROPY_PY_BUILTINS_SLICE
pythontech 0:5868e8752d44 139 MP_F_NEW_SLICE,
pythontech 0:5868e8752d44 140 #endif
pythontech 0:5868e8752d44 141 MP_F_UNPACK_SEQUENCE,
pythontech 0:5868e8752d44 142 MP_F_UNPACK_EX,
pythontech 0:5868e8752d44 143 MP_F_DELETE_NAME,
pythontech 0:5868e8752d44 144 MP_F_DELETE_GLOBAL,
pythontech 0:5868e8752d44 145 MP_F_NEW_CELL,
pythontech 0:5868e8752d44 146 MP_F_MAKE_CLOSURE_FROM_RAW_CODE,
pythontech 0:5868e8752d44 147 MP_F_SETUP_CODE_STATE,
pythontech 0:5868e8752d44 148 MP_F_NUMBER_OF,
pythontech 0:5868e8752d44 149 } mp_fun_kind_t;
pythontech 0:5868e8752d44 150
pythontech 0:5868e8752d44 151 extern void *const mp_fun_table[MP_F_NUMBER_OF];
pythontech 0:5868e8752d44 152
pythontech 0:5868e8752d44 153 #endif // __MICROPY_INCLUDED_PY_RUNTIME0_H__