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
pythontech 0:5868e8752d44 27 #include <assert.h>
pythontech 0:5868e8752d44 28 #include <string.h>
pythontech 0:5868e8752d44 29
pythontech 0:5868e8752d44 30 #include "py/runtime.h"
pythontech 0:5868e8752d44 31 #include "py/builtin.h"
pythontech 0:5868e8752d44 32 #include "py/stream.h"
pythontech 0:5868e8752d44 33
pythontech 0:5868e8752d44 34 #if MICROPY_PY_IO
pythontech 0:5868e8752d44 35
pythontech 0:5868e8752d44 36 extern const mp_obj_type_t mp_type_fileio;
pythontech 0:5868e8752d44 37 extern const mp_obj_type_t mp_type_textio;
pythontech 0:5868e8752d44 38
pythontech 0:5868e8752d44 39 #if MICROPY_PY_IO_BUFFEREDWRITER
pythontech 0:5868e8752d44 40 typedef struct _mp_obj_bufwriter_t {
pythontech 0:5868e8752d44 41 mp_obj_base_t base;
pythontech 0:5868e8752d44 42 mp_obj_t stream;
pythontech 0:5868e8752d44 43 size_t alloc;
pythontech 0:5868e8752d44 44 size_t len;
pythontech 0:5868e8752d44 45 byte buf[0];
pythontech 0:5868e8752d44 46 } mp_obj_bufwriter_t;
pythontech 0:5868e8752d44 47
pythontech 0:5868e8752d44 48 STATIC mp_obj_t bufwriter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
pythontech 0:5868e8752d44 49 mp_arg_check_num(n_args, n_kw, 2, 2, false);
pythontech 0:5868e8752d44 50 size_t alloc = mp_obj_get_int(args[1]);
pythontech 0:5868e8752d44 51 mp_obj_bufwriter_t *o = m_new_obj_var(mp_obj_bufwriter_t, byte, alloc);
pythontech 0:5868e8752d44 52 o->base.type = type;
pythontech 0:5868e8752d44 53 o->stream = args[0];
pythontech 0:5868e8752d44 54 o->alloc = alloc;
pythontech 0:5868e8752d44 55 o->len = 0;
pythontech 0:5868e8752d44 56 return o;
pythontech 0:5868e8752d44 57 }
pythontech 0:5868e8752d44 58
pythontech 0:5868e8752d44 59 STATIC mp_uint_t bufwriter_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
pythontech 0:5868e8752d44 60 mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 61
pythontech 0:5868e8752d44 62 mp_uint_t org_size = size;
pythontech 0:5868e8752d44 63
pythontech 0:5868e8752d44 64 while (size > 0) {
pythontech 0:5868e8752d44 65 mp_uint_t rem = self->alloc - self->len;
pythontech 0:5868e8752d44 66 if (size < rem) {
pythontech 0:5868e8752d44 67 memcpy(self->buf + self->len, buf, size);
pythontech 0:5868e8752d44 68 self->len += size;
pythontech 0:5868e8752d44 69 return org_size;
pythontech 0:5868e8752d44 70 }
pythontech 0:5868e8752d44 71
pythontech 0:5868e8752d44 72 // Buffer flushing policy here is to flush entire buffer all the time.
pythontech 0:5868e8752d44 73 // This allows e.g. to have a block device as backing storage and write
pythontech 0:5868e8752d44 74 // entire block to it. memcpy below is not ideal and could be optimized
pythontech 0:5868e8752d44 75 // in some cases. But the way it is now it at least ensures that buffer
pythontech 0:5868e8752d44 76 // is word-aligned, to guard against obscure cases when it matters, e.g.
pythontech 0:5868e8752d44 77 // https://github.com/micropython/micropython/issues/1863
pythontech 0:5868e8752d44 78 memcpy(self->buf + self->len, buf, rem);
pythontech 0:5868e8752d44 79 buf = (byte*)buf + rem;
pythontech 0:5868e8752d44 80 size -= rem;
pythontech 0:5868e8752d44 81 mp_uint_t out_sz = mp_stream_writeall(self->stream, self->buf, self->alloc, errcode);
pythontech 0:5868e8752d44 82 if (out_sz == MP_STREAM_ERROR) {
pythontech 0:5868e8752d44 83 return MP_STREAM_ERROR;
pythontech 0:5868e8752d44 84 }
pythontech 0:5868e8752d44 85 self->len = 0;
pythontech 0:5868e8752d44 86 }
pythontech 0:5868e8752d44 87
pythontech 0:5868e8752d44 88 return org_size;
pythontech 0:5868e8752d44 89 }
pythontech 0:5868e8752d44 90
pythontech 0:5868e8752d44 91 STATIC mp_obj_t bufwriter_flush(mp_obj_t self_in) {
pythontech 0:5868e8752d44 92 mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 93
pythontech 0:5868e8752d44 94 if (self->len != 0) {
pythontech 0:5868e8752d44 95 int err;
pythontech 0:5868e8752d44 96 mp_uint_t out_sz = mp_stream_writeall(self->stream, self->buf, self->len, &err);
pythontech 0:5868e8752d44 97 self->len = 0;
pythontech 0:5868e8752d44 98 if (out_sz == MP_STREAM_ERROR) {
pythontech 0:5868e8752d44 99 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(err)));
pythontech 0:5868e8752d44 100 }
pythontech 0:5868e8752d44 101 }
pythontech 0:5868e8752d44 102
pythontech 0:5868e8752d44 103 return mp_const_none;
pythontech 0:5868e8752d44 104 }
pythontech 0:5868e8752d44 105 STATIC MP_DEFINE_CONST_FUN_OBJ_1(bufwriter_flush_obj, bufwriter_flush);
pythontech 0:5868e8752d44 106
pythontech 0:5868e8752d44 107 STATIC const mp_map_elem_t bufwriter_locals_dict_table[] = {
pythontech 0:5868e8752d44 108 { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
pythontech 0:5868e8752d44 109 { MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&bufwriter_flush_obj },
pythontech 0:5868e8752d44 110 };
pythontech 0:5868e8752d44 111 STATIC MP_DEFINE_CONST_DICT(bufwriter_locals_dict, bufwriter_locals_dict_table);
pythontech 0:5868e8752d44 112
pythontech 0:5868e8752d44 113 STATIC const mp_stream_p_t bufwriter_stream_p = {
pythontech 0:5868e8752d44 114 .write = bufwriter_write,
pythontech 0:5868e8752d44 115 };
pythontech 0:5868e8752d44 116
pythontech 0:5868e8752d44 117 STATIC const mp_obj_type_t bufwriter_type = {
pythontech 0:5868e8752d44 118 { &mp_type_type },
pythontech 0:5868e8752d44 119 .name = MP_QSTR_BufferedWriter,
pythontech 0:5868e8752d44 120 .make_new = bufwriter_make_new,
pythontech 0:5868e8752d44 121 .stream_p = &bufwriter_stream_p,
pythontech 0:5868e8752d44 122 .locals_dict = (mp_obj_t)&bufwriter_locals_dict,
pythontech 0:5868e8752d44 123 };
pythontech 0:5868e8752d44 124 #endif // MICROPY_PY_IO_BUFFEREDWRITER
pythontech 0:5868e8752d44 125
pythontech 0:5868e8752d44 126 STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
pythontech 0:5868e8752d44 127 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__io) },
pythontech 0:5868e8752d44 128 // Note: mp_builtin_open_obj should be defined by port, it's not
pythontech 0:5868e8752d44 129 // part of the core.
pythontech 0:5868e8752d44 130 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
pythontech 0:5868e8752d44 131 #if MICROPY_PY_IO_FILEIO
pythontech 0:5868e8752d44 132 { MP_ROM_QSTR(MP_QSTR_FileIO), MP_ROM_PTR(&mp_type_fileio) },
pythontech 0:5868e8752d44 133 #if MICROPY_CPYTHON_COMPAT
pythontech 0:5868e8752d44 134 { MP_ROM_QSTR(MP_QSTR_TextIOWrapper), MP_ROM_PTR(&mp_type_textio) },
pythontech 0:5868e8752d44 135 #endif
pythontech 0:5868e8752d44 136 #endif
pythontech 0:5868e8752d44 137 { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) },
pythontech 0:5868e8752d44 138 #if MICROPY_PY_IO_BYTESIO
pythontech 0:5868e8752d44 139 { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) },
pythontech 0:5868e8752d44 140 #endif
pythontech 0:5868e8752d44 141 #if MICROPY_PY_IO_BUFFEREDWRITER
pythontech 0:5868e8752d44 142 { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&bufwriter_type) },
pythontech 0:5868e8752d44 143 #endif
pythontech 0:5868e8752d44 144 };
pythontech 0:5868e8752d44 145
pythontech 0:5868e8752d44 146 STATIC MP_DEFINE_CONST_DICT(mp_module_io_globals, mp_module_io_globals_table);
pythontech 0:5868e8752d44 147
pythontech 0:5868e8752d44 148 const mp_obj_module_t mp_module_io = {
pythontech 0:5868e8752d44 149 .base = { &mp_type_module },
pythontech 0:5868e8752d44 150 .name = MP_QSTR__io,
pythontech 0:5868e8752d44 151 .globals = (mp_obj_dict_t*)&mp_module_io_globals,
pythontech 0:5868e8752d44 152 };
pythontech 0:5868e8752d44 153
pythontech 0:5868e8752d44 154 #endif