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:
8:6c5fa976a1e3
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 * Copyright (c) 2016 Colin Hogben
pythontech 0:5868e8752d44 8 *
pythontech 0:5868e8752d44 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 10 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 11 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 13 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 14 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 15 *
pythontech 0:5868e8752d44 16 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 17 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 18 *
pythontech 0:5868e8752d44 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 25 * THE SOFTWARE.
pythontech 0:5868e8752d44 26 */
pythontech 0:5868e8752d44 27 #include <stdint.h>
pythontech 0:5868e8752d44 28
Colin Hogben 8:6c5fa976a1e3 29 // mbed's printf outputs to USB serial but does not do LF->CRLF conversion.
Colin Hogben 8:6c5fa976a1e3 30 // Ensure lib/utils/printf.c implementation is used instead.
Colin Hogben 8:6c5fa976a1e3 31 // But not for C++ where cstdio would get upset.
Colin Hogben 8:6c5fa976a1e3 32 #ifndef __cplusplus
Colin Hogben 8:6c5fa976a1e3 33 extern int mp__printf(const char *, ...);
Colin Hogben 8:6c5fa976a1e3 34 #define printf mp__printf
Colin Hogben 8:6c5fa976a1e3 35 #endif
Colin Hogben 8:6c5fa976a1e3 36
pythontech 0:5868e8752d44 37 // options to control how Micro Python is built
pythontech 0:5868e8752d44 38
pythontech 0:5868e8752d44 39 // The mbed online compiler uses different assembler syntax, so avoid
Colin Hogben 7:379d46fd02c2 40 #define MICROPY_NLR_SETJMP (1)
pythontech 0:5868e8752d44 41
pythontech 0:5868e8752d44 42 #define MICROPY_ALLOC_PATH_MAX (256)
pythontech 0:5868e8752d44 43 #define MICROPY_ALLOC_PARSE_CHUNK_INIT (16)
pythontech 0:5868e8752d44 44 #define MICROPY_EMIT_X64 (0)
pythontech 0:5868e8752d44 45 #define MICROPY_EMIT_THUMB (0)
pythontech 0:5868e8752d44 46 #define MICROPY_EMIT_INLINE_THUMB (0)
pythontech 0:5868e8752d44 47 #define MICROPY_COMP_MODULE_CONST (0)
pythontech 0:5868e8752d44 48 #define MICROPY_COMP_CONST (0)
pythontech 0:5868e8752d44 49 #define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0)
pythontech 0:5868e8752d44 50 #define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (0)
pythontech 0:5868e8752d44 51 #define MICROPY_MEM_STATS (0)
pythontech 0:5868e8752d44 52 #define MICROPY_DEBUG_PRINTERS (0)
pythontech 0:5868e8752d44 53 #define MICROPY_ENABLE_GC (0)
pythontech 0:5868e8752d44 54 #define MICROPY_REPL_EVENT_DRIVEN (0)
pythontech 0:5868e8752d44 55 #define MICROPY_HELPER_REPL (1)
pythontech 0:5868e8752d44 56 #define MICROPY_HELPER_LEXER_UNIX (0)
pythontech 0:5868e8752d44 57 #define MICROPY_ENABLE_SOURCE_LINE (0)
pythontech 0:5868e8752d44 58 #define MICROPY_ENABLE_DOC_STRING (0)
pythontech 0:5868e8752d44 59 #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
pythontech 0:5868e8752d44 60 #define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0)
pythontech 0:5868e8752d44 61 #define MICROPY_PY_BUILTINS_BYTEARRAY (0)
pythontech 0:5868e8752d44 62 #define MICROPY_PY_BUILTINS_MEMORYVIEW (0)
pythontech 0:5868e8752d44 63 #define MICROPY_PY_BUILTINS_ENUMERATE (0)
pythontech 0:5868e8752d44 64 #define MICROPY_PY_BUILTINS_FILTER (0)
pythontech 0:5868e8752d44 65 #define MICROPY_PY_BUILTINS_FROZENSET (0)
pythontech 0:5868e8752d44 66 #define MICROPY_PY_BUILTINS_REVERSED (0)
pythontech 0:5868e8752d44 67 #define MICROPY_PY_BUILTINS_SET (0)
pythontech 0:5868e8752d44 68 #define MICROPY_PY_BUILTINS_SLICE (0)
pythontech 0:5868e8752d44 69 #define MICROPY_PY_BUILTINS_PROPERTY (0)
pythontech 0:5868e8752d44 70 #define MICROPY_PY_BUILTINS_MIN_MAX (0)
pythontech 0:5868e8752d44 71 #define MICROPY_PY___FILE__ (0)
pythontech 0:5868e8752d44 72 #define MICROPY_PY_GC (0)
pythontech 0:5868e8752d44 73 #define MICROPY_PY_ARRAY (0)
pythontech 0:5868e8752d44 74 #define MICROPY_PY_ATTRTUPLE (0)
pythontech 0:5868e8752d44 75 #define MICROPY_PY_COLLECTIONS (0)
pythontech 0:5868e8752d44 76 #define MICROPY_PY_MATH (0)
pythontech 0:5868e8752d44 77 #define MICROPY_PY_CMATH (0)
pythontech 0:5868e8752d44 78 #define MICROPY_PY_IO (0)
pythontech 0:5868e8752d44 79 #define MICROPY_PY_STRUCT (1)
pythontech 0:5868e8752d44 80 #define MICROPY_PY_SYS (1)
pythontech 0:5868e8752d44 81 // Build "machine" module (port-specific)
Colin Hogben 7:379d46fd02c2 82 #define MICROPY_PY_MACHINE (1)
pythontech 0:5868e8752d44 83 // Build "mbed" module
Colin Hogben 7:379d46fd02c2 84 #define MICROPY_PY_MBED (1)
pythontech 0:5868e8752d44 85 // Select which mbed features wanted
Colin Hogben 7:379d46fd02c2 86 #define MICROPY_MBED_DIGITALOUT (1)
Colin Hogben 7:379d46fd02c2 87 #define MICROPY_MBED_DIGITALIN (1)
Colin Hogben 7:379d46fd02c2 88 #define MICROPY_MBED_SERIAL (1)
Colin Hogben 7:379d46fd02c2 89 #define MICROPY_PY_PINS (1)
pythontech 0:5868e8752d44 90
pythontech 0:5868e8752d44 91 #define MICROPY_MODULE_FROZEN (0)
pythontech 0:5868e8752d44 92 #define MICROPY_CPYTHON_COMPAT (0)
pythontech 0:5868e8752d44 93 #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
pythontech 0:5868e8752d44 94 #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
pythontech 0:5868e8752d44 95
pythontech 0:5868e8752d44 96 // type definitions for the specific machine
pythontech 0:5868e8752d44 97
pythontech 0:5868e8752d44 98 #define BYTES_PER_WORD (4)
pythontech 0:5868e8752d44 99 #define MP_ENDIANNESS_LITTLE 1
pythontech 0:5868e8752d44 100
pythontech 0:5868e8752d44 101 #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1))
pythontech 0:5868e8752d44 102
pythontech 0:5868e8752d44 103 // This port is intended to be 32-bit, but unfortunately, int32_t for
pythontech 0:5868e8752d44 104 // different targets may be defined in different ways - either as int
pythontech 0:5868e8752d44 105 // or as long. This requires different printf formatting specifiers
pythontech 0:5868e8752d44 106 // to print such value. So, we avoid int32_t and use int directly.
pythontech 0:5868e8752d44 107 #define UINT_FMT "%u"
pythontech 0:5868e8752d44 108 #define INT_FMT "%d"
pythontech 0:5868e8752d44 109 typedef int mp_int_t; // must be pointer size
pythontech 0:5868e8752d44 110 typedef unsigned mp_uint_t; // must be pointer size
pythontech 0:5868e8752d44 111
pythontech 0:5868e8752d44 112 typedef void *machine_ptr_t; // must be of pointer size
pythontech 0:5868e8752d44 113 typedef const void *machine_const_ptr_t; // must be of pointer size
pythontech 0:5868e8752d44 114 typedef long mp_off_t;
pythontech 0:5868e8752d44 115
pythontech 0:5868e8752d44 116 #define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)
pythontech 0:5868e8752d44 117
pythontech 0:5868e8752d44 118 // extra built in names to add to the global namespace
Colin Hogben 7:379d46fd02c2 119 extern const struct _mp_obj_fun_builtin_t mp_builtin_help_obj;
pythontech 0:5868e8752d44 120 #define MICROPY_PORT_BUILTINS \
Colin Hogben 7:379d46fd02c2 121 { MP_OBJ_NEW_QSTR(MP_QSTR_help), (mp_obj_t)&mp_builtin_help_obj },
pythontech 0:5868e8752d44 122
pythontech 0:5868e8752d44 123 // We need to provide a declaration/definition of alloca()
pythontech 0:5868e8752d44 124 #include <alloca.h>
pythontech 0:5868e8752d44 125
Colin Hogben 7:379d46fd02c2 126 #define MICROPY_PY_SYS_PLATFORM "mbed"
pythontech 0:5868e8752d44 127 #define MICROPY_HW_BOARD_NAME "mbed"
pythontech 0:5868e8752d44 128 #define MICROPY_HW_MCU_NAME "arm"
pythontech 0:5868e8752d44 129
pythontech 0:5868e8752d44 130 #define MP_STATE_PORT MP_STATE_VM
pythontech 0:5868e8752d44 131
pythontech 0:5868e8752d44 132 // Extra modules to build in
pythontech 0:5868e8752d44 133 extern const struct _mp_obj_module_t mp_module_mbed;
Colin Hogben 5:1d19b8110e11 134 extern const struct _mp_obj_module_t mp_module_pins;
pythontech 0:5868e8752d44 135 #define MICROPY_PORT_BUILTIN_MODULES \
Colin Hogben 7:379d46fd02c2 136 { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) }, \
Colin Hogben 7:379d46fd02c2 137 { MP_ROM_QSTR(MP_QSTR_mbed), MP_ROM_PTR(&mp_module_mbed) }, \
Colin Hogben 7:379d46fd02c2 138 { MP_ROM_QSTR(MP_QSTR_pins), MP_ROM_PTR(&mp_module_pins) }, \
pythontech 0:5868e8752d44 139
pythontech 0:5868e8752d44 140 // Use by readline.c
pythontech 0:5868e8752d44 141 #define MICROPY_PORT_ROOT_POINTERS \
pythontech 0:5868e8752d44 142 const char *readline_hist[8];