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 * The MIT License (MIT)
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * Copyright (c) 2016 Colin Hogben
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 7 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 8 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 10 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 11 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 12 *
pythontech 0:5868e8752d44 13 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 14 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 15 *
pythontech 0:5868e8752d44 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 22 * THE SOFTWARE.
pythontech 0:5868e8752d44 23 */
pythontech 0:5868e8752d44 24 // High-level mbed objects
pythontech 0:5868e8752d44 25
pythontech 0:5868e8752d44 26 #include "py/runtime.h"
pythontech 0:5868e8752d44 27
pythontech 0:5868e8752d44 28 // Only if selected in mpconfigport.h
pythontech 0:5868e8752d44 29 #if MICROPY_PY_MBED
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 #include "modmbed_i.h"
pythontech 0:5868e8752d44 32
Colin Hogben 7:379d46fd02c2 33 #if MICROPY_PY_BUILTINS_FLOAT
Colin Hogben 7:379d46fd02c2 34 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_wait_obj,
Colin Hogben 7:379d46fd02c2 35 mbed_wait);
Colin Hogben 7:379d46fd02c2 36 #endif
Colin Hogben 7:379d46fd02c2 37 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_wait_ms_obj,
Colin Hogben 7:379d46fd02c2 38 mbed_wait_ms);
Colin Hogben 7:379d46fd02c2 39 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_wait_us_obj,
Colin Hogben 7:379d46fd02c2 40 mbed_wait_us);
Colin Hogben 7:379d46fd02c2 41
pythontech 0:5868e8752d44 42 #if MICROPY_MBED_DIGITALOUT
pythontech 0:5868e8752d44 43 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 44 // DigitalOut class
pythontech 0:5868e8752d44 45 //-----------------------------------------------------------------------
pythontech 0:5868e8752d44 46 STATIC MP_DEFINE_CONST_FUN_OBJ_2(mbed_DigitalOut_write_obj,
Colin Hogben 7:379d46fd02c2 47 mbed_DigitalOut_write);
Colin Hogben 8:6c5fa976a1e3 48 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_DigitalOut_read_obj,
Colin Hogben 8:6c5fa976a1e3 49 mbed_DigitalOut_read);
Colin Hogben 8:6c5fa976a1e3 50 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_DigitalOut_is_connected_obj,
Colin Hogben 8:6c5fa976a1e3 51 mbed_DigitalOut_is_connected);
pythontech 0:5868e8752d44 52
pythontech 0:5868e8752d44 53 STATIC const mp_map_elem_t mbed_DigitalOut_locals_dict_table[] = {
Colin Hogben 7:379d46fd02c2 54 { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mbed_DigitalOut_write_obj },
Colin Hogben 8:6c5fa976a1e3 55 { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mbed_DigitalOut_read_obj },
Colin Hogben 8:6c5fa976a1e3 56 { MP_OBJ_NEW_QSTR(MP_QSTR_is_connected), (mp_obj_t)&mbed_DigitalOut_is_connected_obj },
pythontech 0:5868e8752d44 57 };
Colin Hogben 7:379d46fd02c2 58
pythontech 0:5868e8752d44 59 STATIC MP_DEFINE_CONST_DICT(mbed_DigitalOut_locals_dict,
Colin Hogben 7:379d46fd02c2 60 mbed_DigitalOut_locals_dict_table);
pythontech 0:5868e8752d44 61
Colin Hogben 6:3e98ebcedb4c 62 const mp_obj_type_t mbed_DigitalOut_type = {
Colin Hogben 7:379d46fd02c2 63 { &mp_type_type },
Colin Hogben 7:379d46fd02c2 64 .name = MP_QSTR_DigitalOut,
Colin Hogben 7:379d46fd02c2 65 // .print
Colin Hogben 7:379d46fd02c2 66 .make_new = mbed_DigitalOut_make_new,
Colin Hogben 7:379d46fd02c2 67 .locals_dict = (mp_obj_t)&mbed_DigitalOut_locals_dict,
pythontech 0:5868e8752d44 68 };
pythontech 0:5868e8752d44 69
pythontech 0:5868e8752d44 70 #endif // MICROPY_MBED_DIGITALOUT
pythontech 0:5868e8752d44 71
Colin Hogben 7:379d46fd02c2 72 #if MICROPY_MBED_DIGITALIN
Colin Hogben 7:379d46fd02c2 73 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 74 // DigitalIn class
Colin Hogben 7:379d46fd02c2 75 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 76 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_DigitalIn_read_obj,
Colin Hogben 7:379d46fd02c2 77 mbed_DigitalIn_read);
Colin Hogben 7:379d46fd02c2 78 STATIC MP_DEFINE_CONST_FUN_OBJ_2(mbed_DigitalIn_mode_obj,
Colin Hogben 7:379d46fd02c2 79 mbed_DigitalIn_mode);
Colin Hogben 7:379d46fd02c2 80 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_DigitalIn_is_connected_obj,
Colin Hogben 7:379d46fd02c2 81 mbed_DigitalIn_is_connected);
Colin Hogben 7:379d46fd02c2 82
Colin Hogben 7:379d46fd02c2 83 STATIC const mp_map_elem_t mbed_DigitalIn_locals_dict_table[] = {
Colin Hogben 7:379d46fd02c2 84 { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mbed_DigitalIn_read_obj },
Colin Hogben 8:6c5fa976a1e3 85 { MP_OBJ_NEW_QSTR(MP_QSTR_mode), (mp_obj_t)&mbed_DigitalIn_mode_obj },
Colin Hogben 7:379d46fd02c2 86 { MP_OBJ_NEW_QSTR(MP_QSTR_is_connected), (mp_obj_t)&mbed_DigitalIn_is_connected_obj },
Colin Hogben 7:379d46fd02c2 87 };
Colin Hogben 7:379d46fd02c2 88
Colin Hogben 7:379d46fd02c2 89 STATIC MP_DEFINE_CONST_DICT(mbed_DigitalIn_locals_dict,
Colin Hogben 7:379d46fd02c2 90 mbed_DigitalIn_locals_dict_table);
Colin Hogben 7:379d46fd02c2 91
Colin Hogben 7:379d46fd02c2 92 const mp_obj_type_t mbed_DigitalIn_type = {
Colin Hogben 7:379d46fd02c2 93 { &mp_type_type },
Colin Hogben 7:379d46fd02c2 94 .name = MP_QSTR_DigitalIn,
Colin Hogben 7:379d46fd02c2 95 // .print
Colin Hogben 7:379d46fd02c2 96 .make_new = mbed_DigitalIn_make_new,
Colin Hogben 7:379d46fd02c2 97 .locals_dict = (mp_obj_t)&mbed_DigitalIn_locals_dict,
Colin Hogben 7:379d46fd02c2 98 };
Colin Hogben 7:379d46fd02c2 99
Colin Hogben 7:379d46fd02c2 100 #endif // MICROPY_MBED_DIGITALIN
Colin Hogben 7:379d46fd02c2 101
Colin Hogben 3:d8dfbbbd0fc9 102 #if MICROPY_MBED_SERIAL
Colin Hogben 3:d8dfbbbd0fc9 103 /*-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 104 * Serial class
Colin Hogben 3:d8dfbbbd0fc9 105 *-----------------------------------------------------------------------*/
Colin Hogben 7:379d46fd02c2 106 STATIC MP_DEFINE_CONST_FUN_OBJ_2(mbed_Serial_baud_obj,
Colin Hogben 7:379d46fd02c2 107 mbed_Serial_baud);
Colin Hogben 7:379d46fd02c2 108 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mbed_Serial_format_obj, 1, 4,
Colin Hogben 7:379d46fd02c2 109 mbed_Serial_format);
Colin Hogben 3:d8dfbbbd0fc9 110 STATIC MP_DEFINE_CONST_FUN_OBJ_2(mbed_Serial_putc_obj,
Colin Hogben 7:379d46fd02c2 111 mbed_Serial_putc);
Colin Hogben 3:d8dfbbbd0fc9 112 STATIC MP_DEFINE_CONST_FUN_OBJ_2(mbed_Serial_puts_obj,
Colin Hogben 7:379d46fd02c2 113 mbed_Serial_puts);
Colin Hogben 3:d8dfbbbd0fc9 114 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_Serial_getc_obj,
Colin Hogben 7:379d46fd02c2 115 mbed_Serial_getc);
Colin Hogben 7:379d46fd02c2 116 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_Serial_readable_obj,
Colin Hogben 7:379d46fd02c2 117 mbed_Serial_readable);
Colin Hogben 7:379d46fd02c2 118 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_Serial_writeable_obj,
Colin Hogben 7:379d46fd02c2 119 mbed_Serial_writeable);
Colin Hogben 7:379d46fd02c2 120 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mbed_Serial_send_break_obj,
Colin Hogben 7:379d46fd02c2 121 mbed_Serial_send_break);
Colin Hogben 3:d8dfbbbd0fc9 122
Colin Hogben 3:d8dfbbbd0fc9 123 STATIC const mp_map_elem_t mbed_Serial_locals_dict_table[] = {
Colin Hogben 7:379d46fd02c2 124 { MP_OBJ_NEW_QSTR(MP_QSTR_baud), (mp_obj_t)&mbed_Serial_baud_obj },
Colin Hogben 7:379d46fd02c2 125 { MP_OBJ_NEW_QSTR(MP_QSTR_format), (mp_obj_t)&mbed_Serial_format_obj },
Colin Hogben 7:379d46fd02c2 126 { MP_OBJ_NEW_QSTR(MP_QSTR_putc), (mp_obj_t)&mbed_Serial_putc_obj },
Colin Hogben 7:379d46fd02c2 127 { MP_OBJ_NEW_QSTR(MP_QSTR_puts), (mp_obj_t)&mbed_Serial_puts_obj },
Colin Hogben 7:379d46fd02c2 128 { MP_OBJ_NEW_QSTR(MP_QSTR_getc), (mp_obj_t)&mbed_Serial_getc_obj },
Colin Hogben 7:379d46fd02c2 129 { MP_OBJ_NEW_QSTR(MP_QSTR_readable), (mp_obj_t)&mbed_Serial_readable_obj },
Colin Hogben 7:379d46fd02c2 130 { MP_OBJ_NEW_QSTR(MP_QSTR_writeable), (mp_obj_t)&mbed_Serial_writeable_obj },
Colin Hogben 7:379d46fd02c2 131 { MP_OBJ_NEW_QSTR(MP_QSTR_send_break), (mp_obj_t)&mbed_Serial_send_break_obj },
Colin Hogben 3:d8dfbbbd0fc9 132 };
Colin Hogben 7:379d46fd02c2 133
Colin Hogben 3:d8dfbbbd0fc9 134 STATIC MP_DEFINE_CONST_DICT(mbed_Serial_locals_dict,
Colin Hogben 7:379d46fd02c2 135 mbed_Serial_locals_dict_table);
Colin Hogben 3:d8dfbbbd0fc9 136
Colin Hogben 6:3e98ebcedb4c 137 const mp_obj_type_t mbed_Serial_type = {
Colin Hogben 7:379d46fd02c2 138 { &mp_type_type },
Colin Hogben 7:379d46fd02c2 139 .name = MP_QSTR_Serial,
Colin Hogben 7:379d46fd02c2 140 // .print
Colin Hogben 7:379d46fd02c2 141 .make_new = mbed_Serial_make_new,
Colin Hogben 7:379d46fd02c2 142 .locals_dict = (mp_obj_t)&mbed_Serial_locals_dict,
Colin Hogben 3:d8dfbbbd0fc9 143 };
Colin Hogben 3:d8dfbbbd0fc9 144
Colin Hogben 3:d8dfbbbd0fc9 145 #endif // MICROPY_MBED_SERIAL
Colin Hogben 3:d8dfbbbd0fc9 146
pythontech 0:5868e8752d44 147 // Module
Colin Hogben 7:379d46fd02c2 148 STATIC const mp_rom_map_elem_t mbed_module_globals_table[] = {
Colin Hogben 7:379d46fd02c2 149 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_mbed) },
Colin Hogben 7:379d46fd02c2 150 #if MICROPY_PY_BUILTINS_FLOAT
Colin Hogben 7:379d46fd02c2 151 { MP_ROM_QSTR(MP_QSTR_wait), MP_ROM_PTR(&mbed_wait_obj) },
Colin Hogben 7:379d46fd02c2 152 #endif
Colin Hogben 7:379d46fd02c2 153 { MP_ROM_QSTR(MP_QSTR_wait_ms), MP_ROM_PTR(&mbed_wait_ms_obj) },
Colin Hogben 7:379d46fd02c2 154 { MP_ROM_QSTR(MP_QSTR_wait_us), MP_ROM_PTR(&mbed_wait_us_obj) },
pythontech 0:5868e8752d44 155 #if MICROPY_MBED_DIGITALOUT
Colin Hogben 7:379d46fd02c2 156 { MP_ROM_QSTR(MP_QSTR_DigitalOut), MP_ROM_PTR(&mbed_DigitalOut_type) },
Colin Hogben 7:379d46fd02c2 157 #endif
Colin Hogben 7:379d46fd02c2 158 #if MICROPY_MBED_DIGITALIN
Colin Hogben 7:379d46fd02c2 159 { MP_ROM_QSTR(MP_QSTR_DigitalIn), MP_ROM_PTR(&mbed_DigitalIn_type) },
pythontech 0:5868e8752d44 160 #endif
Colin Hogben 3:d8dfbbbd0fc9 161 #if MICROPY_MBED_SERIAL
Colin Hogben 7:379d46fd02c2 162 { MP_ROM_QSTR(MP_QSTR_Serial), MP_ROM_PTR(&mbed_Serial_type) },
Colin Hogben 3:d8dfbbbd0fc9 163 #endif
pythontech 0:5868e8752d44 164 };
pythontech 0:5868e8752d44 165
Colin Hogben 7:379d46fd02c2 166 STATIC MP_DEFINE_CONST_DICT(mbed_module_globals,
Colin Hogben 7:379d46fd02c2 167 mbed_module_globals_table);
pythontech 0:5868e8752d44 168
pythontech 0:5868e8752d44 169 const mp_obj_module_t mp_module_mbed = {
Colin Hogben 7:379d46fd02c2 170 .base = { &mp_type_module },
Colin Hogben 7:379d46fd02c2 171 .name = MP_QSTR_mbed,
Colin Hogben 7:379d46fd02c2 172 .globals = (mp_obj_dict_t *)&mbed_module_globals,
pythontech 0:5868e8752d44 173 };
pythontech 0:5868e8752d44 174
pythontech 0:5868e8752d44 175 #endif