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
pythontech 0:5868e8752d44 25 /**
pythontech 0:5868e8752d44 26 * Shims to isolate the pure C world of micropython from the C++ world
pythontech 0:5868e8752d44 27 * of mbed high-level objects. Maybe there's a way to write the module
pythontech 0:5868e8752d44 28 * directly in a compatible way but my C++-fu is not that strong.
pythontech 0:5868e8752d44 29 */
pythontech 0:5868e8752d44 30 extern "C" {
pythontech 0:5868e8752d44 31 #include "py/mpconfig.h"
pythontech 0:5868e8752d44 32 }
pythontech 0:5868e8752d44 33
pythontech 0:5868e8752d44 34 #if MICROPY_PY_MBED
pythontech 0:5868e8752d44 35
pythontech 0:5868e8752d44 36 #include "mbed.h"
pythontech 0:5868e8752d44 37 extern "C" {
Colin Hogben 7:379d46fd02c2 38 #include "py/runtime.h"
Colin Hogben 7:379d46fd02c2 39 #include "modmbed_i.h"
pythontech 0:5868e8752d44 40 }
pythontech 0:5868e8752d44 41
Colin Hogben 7:379d46fd02c2 42 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 43 // Functions
Colin Hogben 7:379d46fd02c2 44 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 45 #if MICROPY_PY_BUILTINS_FLOAT
Colin Hogben 7:379d46fd02c2 46 mp_obj_t mbed_wait(mp_obj_t s_in) {
Colin Hogben 7:379d46fd02c2 47 mp_float_t s = mp_obj_get_float(s_in);
Colin Hogben 7:379d46fd02c2 48 wait((float)s);
Colin Hogben 7:379d46fd02c2 49 return mp_const_none;
Colin Hogben 7:379d46fd02c2 50 }
Colin Hogben 7:379d46fd02c2 51 #endif
Colin Hogben 7:379d46fd02c2 52
Colin Hogben 7:379d46fd02c2 53 mp_obj_t mbed_wait_ms(mp_obj_t ms_in) {
Colin Hogben 7:379d46fd02c2 54 int ms = mp_obj_get_int(ms_in);
Colin Hogben 7:379d46fd02c2 55 wait_ms(ms);
Colin Hogben 7:379d46fd02c2 56 return mp_const_none;
Colin Hogben 7:379d46fd02c2 57 }
Colin Hogben 7:379d46fd02c2 58
Colin Hogben 7:379d46fd02c2 59 mp_obj_t mbed_wait_us(mp_obj_t us_in) {
Colin Hogben 7:379d46fd02c2 60 int us = mp_obj_get_int(us_in);
Colin Hogben 7:379d46fd02c2 61 wait_us(us);
Colin Hogben 7:379d46fd02c2 62 return mp_const_none;
Colin Hogben 7:379d46fd02c2 63 }
Colin Hogben 7:379d46fd02c2 64
Colin Hogben 7:379d46fd02c2 65 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 66 // DigitalOut
Colin Hogben 7:379d46fd02c2 67 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 68 struct mbed_DigitalOut_obj_t {
Colin Hogben 7:379d46fd02c2 69 mp_obj_base_t base;
Colin Hogben 7:379d46fd02c2 70 DigitalOut *dout;
Colin Hogben 7:379d46fd02c2 71 };
Colin Hogben 6:3e98ebcedb4c 72
Colin Hogben 6:3e98ebcedb4c 73 // constructor DigitalOut(pin)
Colin Hogben 6:3e98ebcedb4c 74 mp_obj_t mbed_DigitalOut_make_new(const mp_obj_type_t *type,
Colin Hogben 7:379d46fd02c2 75 mp_uint_t n_args, mp_uint_t n_kw,
Colin Hogben 7:379d46fd02c2 76 const mp_obj_t *args) {
Colin Hogben 7:379d46fd02c2 77 (void)type;
Colin Hogben 7:379d46fd02c2 78 mp_arg_check_num(n_args, n_kw, 1, 1, false);
Colin Hogben 7:379d46fd02c2 79 int pin = mp_obj_get_int(args[0]);
Colin Hogben 7:379d46fd02c2 80 mbed_DigitalOut_obj_t *o =
Colin Hogben 7:379d46fd02c2 81 m_new_obj_with_finaliser(mbed_DigitalOut_obj_t);
Colin Hogben 7:379d46fd02c2 82 o->base.type = &mbed_DigitalOut_type;
Colin Hogben 7:379d46fd02c2 83 o->dout = new DigitalOut((PinName)pin);
Colin Hogben 7:379d46fd02c2 84 return o;
Colin Hogben 7:379d46fd02c2 85 }
Colin Hogben 6:3e98ebcedb4c 86
Colin Hogben 7:379d46fd02c2 87 mp_obj_t mbed_DigitalOut_write(mp_obj_t self_in, mp_obj_t value_in) {
Colin Hogben 7:379d46fd02c2 88 mbed_DigitalOut_obj_t *self = (mbed_DigitalOut_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 89 int value = mp_obj_get_int(value_in);
Colin Hogben 7:379d46fd02c2 90 self->dout->write(value);
Colin Hogben 7:379d46fd02c2 91 return mp_const_none;
pythontech 0:5868e8752d44 92 }
pythontech 0:5868e8752d44 93
Colin Hogben 8:6c5fa976a1e3 94 mp_obj_t mbed_DigitalOut_read(mp_obj_t self_in) {
Colin Hogben 8:6c5fa976a1e3 95 mbed_DigitalOut_obj_t *self = (mbed_DigitalOut_obj_t *)self_in;
Colin Hogben 8:6c5fa976a1e3 96 int value = self->dout->read(); // 0 or 1
Colin Hogben 8:6c5fa976a1e3 97 return MP_OBJ_NEW_SMALL_INT(value);
Colin Hogben 8:6c5fa976a1e3 98 }
Colin Hogben 8:6c5fa976a1e3 99
Colin Hogben 8:6c5fa976a1e3 100 mp_obj_t mbed_DigitalOut_is_connected(mp_obj_t self_in) {
Colin Hogben 8:6c5fa976a1e3 101 mbed_DigitalOut_obj_t *self = (mbed_DigitalOut_obj_t *)self_in;
Colin Hogben 8:6c5fa976a1e3 102 int conn = self->dout->is_connected();
Colin Hogben 8:6c5fa976a1e3 103 return conn ? mp_const_true : mp_const_false;
Colin Hogben 8:6c5fa976a1e3 104 }
Colin Hogben 8:6c5fa976a1e3 105
Colin Hogben 7:379d46fd02c2 106 #if MICROPY_MBED_DIGITALIN
Colin Hogben 7:379d46fd02c2 107 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 108 // DigitalIn
Colin Hogben 7:379d46fd02c2 109 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 110 struct mbed_DigitalIn_obj_t {
Colin Hogben 7:379d46fd02c2 111 mp_obj_base_t base;
Colin Hogben 7:379d46fd02c2 112 DigitalIn *din;
Colin Hogben 7:379d46fd02c2 113 };
Colin Hogben 7:379d46fd02c2 114
Colin Hogben 7:379d46fd02c2 115 mp_obj_t mbed_DigitalIn_make_new(const mp_obj_type_t *type,
Colin Hogben 7:379d46fd02c2 116 size_t n_args, size_t n_kw,
Colin Hogben 7:379d46fd02c2 117 const mp_obj_t *args)
pythontech 0:5868e8752d44 118 {
Colin Hogben 7:379d46fd02c2 119 (void)type;
Colin Hogben 7:379d46fd02c2 120 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Colin Hogben 7:379d46fd02c2 121 int pin = mp_obj_get_int(args[0]);
Colin Hogben 7:379d46fd02c2 122 int mode = (n_args == 2) ? mp_obj_get_int(args[1]) : 0;
Colin Hogben 7:379d46fd02c2 123 mbed_DigitalIn_obj_t *o = m_new_obj_with_finaliser(mbed_DigitalIn_obj_t);
Colin Hogben 7:379d46fd02c2 124 o->base.type = &mbed_DigitalIn_type;
Colin Hogben 7:379d46fd02c2 125 if (n_args == 2) {
Colin Hogben 7:379d46fd02c2 126 o->din = new DigitalIn((PinName)pin, (PinMode)mode);
Colin Hogben 7:379d46fd02c2 127 } else {
Colin Hogben 7:379d46fd02c2 128 o->din = new DigitalIn((PinName)pin);
Colin Hogben 7:379d46fd02c2 129 }
Colin Hogben 7:379d46fd02c2 130 return o;
pythontech 0:5868e8752d44 131 }
pythontech 0:5868e8752d44 132
Colin Hogben 7:379d46fd02c2 133 mp_obj_t mbed_DigitalIn_read(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 134 mbed_DigitalIn_obj_t *self = (mbed_DigitalIn_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 135 int value = self->din->read(); // 0 or 1
Colin Hogben 7:379d46fd02c2 136 return MP_OBJ_NEW_SMALL_INT(value);
Colin Hogben 7:379d46fd02c2 137 }
Colin Hogben 7:379d46fd02c2 138
Colin Hogben 7:379d46fd02c2 139 mp_obj_t mbed_DigitalIn_mode(mp_obj_t self_in, mp_obj_t mode_in) {
Colin Hogben 7:379d46fd02c2 140 mbed_DigitalIn_obj_t *self = (mbed_DigitalIn_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 141 int mode = mp_obj_get_int(mode_in);
Colin Hogben 7:379d46fd02c2 142 self->din->mode((PinMode)mode);
Colin Hogben 7:379d46fd02c2 143 return mp_const_none;
Colin Hogben 7:379d46fd02c2 144 }
Colin Hogben 7:379d46fd02c2 145
Colin Hogben 7:379d46fd02c2 146 mp_obj_t mbed_DigitalIn_is_connected(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 147 mbed_DigitalIn_obj_t *self = (mbed_DigitalIn_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 148 int conn = self->din->is_connected();
Colin Hogben 7:379d46fd02c2 149 return conn ? mp_const_true : mp_const_false;
Colin Hogben 7:379d46fd02c2 150 }
Colin Hogben 7:379d46fd02c2 151
Colin Hogben 7:379d46fd02c2 152 #endif // MICROPY_MBED_DIGITALIN
Colin Hogben 7:379d46fd02c2 153
Colin Hogben 7:379d46fd02c2 154 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 155 // Serial
Colin Hogben 7:379d46fd02c2 156 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 157 struct mbed_Serial_obj_t {
Colin Hogben 7:379d46fd02c2 158 mp_obj_base_t base;
Colin Hogben 7:379d46fd02c2 159 Serial *serial;
Colin Hogben 7:379d46fd02c2 160 };
Colin Hogben 6:3e98ebcedb4c 161
Colin Hogben 6:3e98ebcedb4c 162 // constructor Serial(pin)
Colin Hogben 6:3e98ebcedb4c 163 mp_obj_t mbed_Serial_make_new(const mp_obj_type_t *type,
Colin Hogben 7:379d46fd02c2 164 mp_uint_t n_args, mp_uint_t n_kw,
Colin Hogben 7:379d46fd02c2 165 const mp_obj_t *args) {
Colin Hogben 7:379d46fd02c2 166 (void)type;
Colin Hogben 7:379d46fd02c2 167 mp_arg_check_num(n_args, n_kw, 2, 2, false);
Colin Hogben 7:379d46fd02c2 168 int tx = mp_obj_get_int(args[0]);
Colin Hogben 7:379d46fd02c2 169 int rx = mp_obj_get_int(args[1]);
Colin Hogben 7:379d46fd02c2 170 mbed_Serial_obj_t *o = m_new_obj_with_finaliser(mbed_Serial_obj_t);
Colin Hogben 7:379d46fd02c2 171 o->base.type = &mbed_Serial_type;
Colin Hogben 7:379d46fd02c2 172 o->serial = new Serial((PinName)tx, (PinName)rx);
Colin Hogben 7:379d46fd02c2 173 return o;
Colin Hogben 7:379d46fd02c2 174 }
Colin Hogben 6:3e98ebcedb4c 175
Colin Hogben 7:379d46fd02c2 176 mp_obj_t mbed_Serial_baud(mp_obj_t self_in, mp_obj_t baud_in) {
Colin Hogben 7:379d46fd02c2 177 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 178 int baud = mp_obj_get_int(baud_in);
Colin Hogben 7:379d46fd02c2 179 self->serial->baud(baud);
Colin Hogben 7:379d46fd02c2 180 return mp_const_none;
Colin Hogben 7:379d46fd02c2 181 }
Colin Hogben 6:3e98ebcedb4c 182
Colin Hogben 7:379d46fd02c2 183 mp_obj_t mbed_Serial_format(size_t n_args, const mp_obj_t *args) {
Colin Hogben 7:379d46fd02c2 184 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)args[0];
Colin Hogben 7:379d46fd02c2 185 int bits = (n_args > 1) ? mp_obj_get_int(args[1]) : 8;
Colin Hogben 7:379d46fd02c2 186 int parity = (n_args > 2) ? mp_obj_get_int(args[2]) : SerialBase::None;
Colin Hogben 7:379d46fd02c2 187 int stop_bits = (n_args > 3) ? mp_obj_get_int(args[3]) : 1;
Colin Hogben 7:379d46fd02c2 188 self->serial->format(bits, (SerialBase::Parity)parity, stop_bits);
Colin Hogben 7:379d46fd02c2 189 return mp_const_none;
Colin Hogben 3:d8dfbbbd0fc9 190 }
Colin Hogben 3:d8dfbbbd0fc9 191
Colin Hogben 7:379d46fd02c2 192 mp_obj_t mbed_Serial_readable(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 193 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 194 int value = self->serial->readable();
Colin Hogben 7:379d46fd02c2 195 return value ? mp_const_true : mp_const_false;
Colin Hogben 7:379d46fd02c2 196 }
Colin Hogben 6:3e98ebcedb4c 197
Colin Hogben 7:379d46fd02c2 198 mp_obj_t mbed_Serial_writeable(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 199 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 200 int value = self->serial->writeable();
Colin Hogben 7:379d46fd02c2 201 return value ? mp_const_true : mp_const_false;
Colin Hogben 3:d8dfbbbd0fc9 202 }
Colin Hogben 3:d8dfbbbd0fc9 203
Colin Hogben 7:379d46fd02c2 204 // TODO attach - interrupt handler
Colin Hogben 6:3e98ebcedb4c 205
Colin Hogben 7:379d46fd02c2 206 mp_obj_t mbed_Serial_send_break(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 207 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 208 self->serial->send_break();
Colin Hogben 7:379d46fd02c2 209 return mp_const_none;
Colin Hogben 3:d8dfbbbd0fc9 210 }
Colin Hogben 3:d8dfbbbd0fc9 211
Colin Hogben 7:379d46fd02c2 212 mp_obj_t mbed_Serial_putc(mp_obj_t self_in, mp_obj_t chr_in) {
Colin Hogben 7:379d46fd02c2 213 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 214 int chr = mp_obj_get_int(chr_in);
Colin Hogben 7:379d46fd02c2 215 self->serial->putc(chr);
Colin Hogben 7:379d46fd02c2 216 return mp_const_none;
Colin Hogben 7:379d46fd02c2 217 }
Colin Hogben 7:379d46fd02c2 218
Colin Hogben 7:379d46fd02c2 219 mp_obj_t mbed_Serial_puts(mp_obj_t self_in, mp_obj_t str_in) {
Colin Hogben 7:379d46fd02c2 220 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 221 const char *str = mp_obj_str_get_str(str_in);
Colin Hogben 7:379d46fd02c2 222 self->serial->puts(str);
Colin Hogben 7:379d46fd02c2 223 return mp_const_none;
Colin Hogben 7:379d46fd02c2 224 }
Colin Hogben 7:379d46fd02c2 225
Colin Hogben 7:379d46fd02c2 226 mp_obj_t mbed_Serial_getc(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 227 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 228 int chr = self->serial->getc();
Colin Hogben 7:379d46fd02c2 229 return MP_OBJ_NEW_SMALL_INT(chr);
Colin Hogben 3:d8dfbbbd0fc9 230 }
Colin Hogben 3:d8dfbbbd0fc9 231
pythontech 0:5868e8752d44 232 #endif