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:
Tue Apr 26 22:50:06 2016 +0100
Revision:
7:379d46fd02c2
Parent:
6:3e98ebcedb4c
Child:
8:6c5fa976a1e3
Add help, DigitalIn etc.

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 7:379d46fd02c2 94 #if MICROPY_MBED_DIGITALIN
Colin Hogben 7:379d46fd02c2 95 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 96 // DigitalIn
Colin Hogben 7:379d46fd02c2 97 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 98 struct mbed_DigitalIn_obj_t {
Colin Hogben 7:379d46fd02c2 99 mp_obj_base_t base;
Colin Hogben 7:379d46fd02c2 100 DigitalIn *din;
Colin Hogben 7:379d46fd02c2 101 };
Colin Hogben 7:379d46fd02c2 102
Colin Hogben 7:379d46fd02c2 103 mp_obj_t mbed_DigitalIn_make_new(const mp_obj_type_t *type,
Colin Hogben 7:379d46fd02c2 104 size_t n_args, size_t n_kw,
Colin Hogben 7:379d46fd02c2 105 const mp_obj_t *args)
pythontech 0:5868e8752d44 106 {
Colin Hogben 7:379d46fd02c2 107 (void)type;
Colin Hogben 7:379d46fd02c2 108 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Colin Hogben 7:379d46fd02c2 109 int pin = mp_obj_get_int(args[0]);
Colin Hogben 7:379d46fd02c2 110 int mode = (n_args == 2) ? mp_obj_get_int(args[1]) : 0;
Colin Hogben 7:379d46fd02c2 111 mbed_DigitalIn_obj_t *o = m_new_obj_with_finaliser(mbed_DigitalIn_obj_t);
Colin Hogben 7:379d46fd02c2 112 o->base.type = &mbed_DigitalIn_type;
Colin Hogben 7:379d46fd02c2 113 if (n_args == 2) {
Colin Hogben 7:379d46fd02c2 114 o->din = new DigitalIn((PinName)pin, (PinMode)mode);
Colin Hogben 7:379d46fd02c2 115 } else {
Colin Hogben 7:379d46fd02c2 116 o->din = new DigitalIn((PinName)pin);
Colin Hogben 7:379d46fd02c2 117 }
Colin Hogben 7:379d46fd02c2 118 return o;
pythontech 0:5868e8752d44 119 }
pythontech 0:5868e8752d44 120
Colin Hogben 7:379d46fd02c2 121 mp_obj_t mbed_DigitalIn_read(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 122 mbed_DigitalIn_obj_t *self = (mbed_DigitalIn_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 123 int value = self->din->read(); // 0 or 1
Colin Hogben 7:379d46fd02c2 124 return MP_OBJ_NEW_SMALL_INT(value);
Colin Hogben 7:379d46fd02c2 125 }
Colin Hogben 7:379d46fd02c2 126
Colin Hogben 7:379d46fd02c2 127 mp_obj_t mbed_DigitalIn_mode(mp_obj_t self_in, mp_obj_t mode_in) {
Colin Hogben 7:379d46fd02c2 128 mbed_DigitalIn_obj_t *self = (mbed_DigitalIn_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 129 int mode = mp_obj_get_int(mode_in);
Colin Hogben 7:379d46fd02c2 130 self->din->mode((PinMode)mode);
Colin Hogben 7:379d46fd02c2 131 return mp_const_none;
Colin Hogben 7:379d46fd02c2 132 }
Colin Hogben 7:379d46fd02c2 133
Colin Hogben 7:379d46fd02c2 134 mp_obj_t mbed_DigitalIn_is_connected(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 135 mbed_DigitalIn_obj_t *self = (mbed_DigitalIn_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 136 int conn = self->din->is_connected();
Colin Hogben 7:379d46fd02c2 137 return conn ? mp_const_true : mp_const_false;
Colin Hogben 7:379d46fd02c2 138 }
Colin Hogben 7:379d46fd02c2 139
Colin Hogben 7:379d46fd02c2 140 #endif // MICROPY_MBED_DIGITALIN
Colin Hogben 7:379d46fd02c2 141
Colin Hogben 7:379d46fd02c2 142 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 143 // Serial
Colin Hogben 7:379d46fd02c2 144 //-----------------------------------------------------------------------
Colin Hogben 7:379d46fd02c2 145 struct mbed_Serial_obj_t {
Colin Hogben 7:379d46fd02c2 146 mp_obj_base_t base;
Colin Hogben 7:379d46fd02c2 147 Serial *serial;
Colin Hogben 7:379d46fd02c2 148 };
Colin Hogben 6:3e98ebcedb4c 149
Colin Hogben 6:3e98ebcedb4c 150 // constructor Serial(pin)
Colin Hogben 6:3e98ebcedb4c 151 mp_obj_t mbed_Serial_make_new(const mp_obj_type_t *type,
Colin Hogben 7:379d46fd02c2 152 mp_uint_t n_args, mp_uint_t n_kw,
Colin Hogben 7:379d46fd02c2 153 const mp_obj_t *args) {
Colin Hogben 7:379d46fd02c2 154 (void)type;
Colin Hogben 7:379d46fd02c2 155 mp_arg_check_num(n_args, n_kw, 2, 2, false);
Colin Hogben 7:379d46fd02c2 156 int tx = mp_obj_get_int(args[0]);
Colin Hogben 7:379d46fd02c2 157 int rx = mp_obj_get_int(args[1]);
Colin Hogben 7:379d46fd02c2 158 mbed_Serial_obj_t *o = m_new_obj_with_finaliser(mbed_Serial_obj_t);
Colin Hogben 7:379d46fd02c2 159 o->base.type = &mbed_Serial_type;
Colin Hogben 7:379d46fd02c2 160 o->serial = new Serial((PinName)tx, (PinName)rx);
Colin Hogben 7:379d46fd02c2 161 return o;
Colin Hogben 7:379d46fd02c2 162 }
Colin Hogben 6:3e98ebcedb4c 163
Colin Hogben 7:379d46fd02c2 164 mp_obj_t mbed_Serial_baud(mp_obj_t self_in, mp_obj_t baud_in) {
Colin Hogben 7:379d46fd02c2 165 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 166 int baud = mp_obj_get_int(baud_in);
Colin Hogben 7:379d46fd02c2 167 self->serial->baud(baud);
Colin Hogben 7:379d46fd02c2 168 return mp_const_none;
Colin Hogben 7:379d46fd02c2 169 }
Colin Hogben 6:3e98ebcedb4c 170
Colin Hogben 7:379d46fd02c2 171 mp_obj_t mbed_Serial_format(size_t n_args, const mp_obj_t *args) {
Colin Hogben 7:379d46fd02c2 172 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)args[0];
Colin Hogben 7:379d46fd02c2 173 int bits = (n_args > 1) ? mp_obj_get_int(args[1]) : 8;
Colin Hogben 7:379d46fd02c2 174 int parity = (n_args > 2) ? mp_obj_get_int(args[2]) : SerialBase::None;
Colin Hogben 7:379d46fd02c2 175 int stop_bits = (n_args > 3) ? mp_obj_get_int(args[3]) : 1;
Colin Hogben 7:379d46fd02c2 176 self->serial->format(bits, (SerialBase::Parity)parity, stop_bits);
Colin Hogben 7:379d46fd02c2 177 return mp_const_none;
Colin Hogben 3:d8dfbbbd0fc9 178 }
Colin Hogben 3:d8dfbbbd0fc9 179
Colin Hogben 7:379d46fd02c2 180 mp_obj_t mbed_Serial_readable(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 181 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 182 int value = self->serial->readable();
Colin Hogben 7:379d46fd02c2 183 return value ? mp_const_true : mp_const_false;
Colin Hogben 7:379d46fd02c2 184 }
Colin Hogben 6:3e98ebcedb4c 185
Colin Hogben 7:379d46fd02c2 186 mp_obj_t mbed_Serial_writeable(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 187 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 188 int value = self->serial->writeable();
Colin Hogben 7:379d46fd02c2 189 return value ? mp_const_true : mp_const_false;
Colin Hogben 3:d8dfbbbd0fc9 190 }
Colin Hogben 3:d8dfbbbd0fc9 191
Colin Hogben 7:379d46fd02c2 192 // TODO attach - interrupt handler
Colin Hogben 6:3e98ebcedb4c 193
Colin Hogben 7:379d46fd02c2 194 mp_obj_t mbed_Serial_send_break(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 195 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 196 self->serial->send_break();
Colin Hogben 7:379d46fd02c2 197 return mp_const_none;
Colin Hogben 3:d8dfbbbd0fc9 198 }
Colin Hogben 3:d8dfbbbd0fc9 199
Colin Hogben 7:379d46fd02c2 200 mp_obj_t mbed_Serial_putc(mp_obj_t self_in, mp_obj_t chr_in) {
Colin Hogben 7:379d46fd02c2 201 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 202 int chr = mp_obj_get_int(chr_in);
Colin Hogben 7:379d46fd02c2 203 self->serial->putc(chr);
Colin Hogben 7:379d46fd02c2 204 return mp_const_none;
Colin Hogben 7:379d46fd02c2 205 }
Colin Hogben 7:379d46fd02c2 206
Colin Hogben 7:379d46fd02c2 207 mp_obj_t mbed_Serial_puts(mp_obj_t self_in, mp_obj_t str_in) {
Colin Hogben 7:379d46fd02c2 208 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 209 const char *str = mp_obj_str_get_str(str_in);
Colin Hogben 7:379d46fd02c2 210 self->serial->puts(str);
Colin Hogben 7:379d46fd02c2 211 return mp_const_none;
Colin Hogben 7:379d46fd02c2 212 }
Colin Hogben 7:379d46fd02c2 213
Colin Hogben 7:379d46fd02c2 214 mp_obj_t mbed_Serial_getc(mp_obj_t self_in) {
Colin Hogben 7:379d46fd02c2 215 mbed_Serial_obj_t *self = (mbed_Serial_obj_t *)self_in;
Colin Hogben 7:379d46fd02c2 216 int chr = self->serial->getc();
Colin Hogben 7:379d46fd02c2 217 return MP_OBJ_NEW_SMALL_INT(chr);
Colin Hogben 3:d8dfbbbd0fc9 218 }
Colin Hogben 3:d8dfbbbd0fc9 219
pythontech 0:5868e8752d44 220 #endif