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:
pythontech
Date:
Sat Apr 16 17:11:56 2016 +0000
Revision:
0:5868e8752d44
Split off library from repl

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) 2014 Paul Sokolovsky
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
pythontech 0:5868e8752d44 28 #include <stdio.h>
pythontech 0:5868e8752d44 29 #include <string.h>
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 #include "py/nlr.h"
pythontech 0:5868e8752d44 32 #include "py/objstr.h"
pythontech 0:5868e8752d44 33 #include "py/runtime.h"
pythontech 0:5868e8752d44 34 #include "py/stream.h"
pythontech 0:5868e8752d44 35
pythontech 0:5868e8752d44 36 #if MICROPY_PY_IO
pythontech 0:5868e8752d44 37
pythontech 0:5868e8752d44 38 typedef struct _mp_obj_stringio_t {
pythontech 0:5868e8752d44 39 mp_obj_base_t base;
pythontech 0:5868e8752d44 40 vstr_t *vstr;
pythontech 0:5868e8752d44 41 // StringIO has single pointer used for both reading and writing
pythontech 0:5868e8752d44 42 mp_uint_t pos;
pythontech 0:5868e8752d44 43 } mp_obj_stringio_t;
pythontech 0:5868e8752d44 44
pythontech 0:5868e8752d44 45 #if MICROPY_CPYTHON_COMPAT
pythontech 0:5868e8752d44 46 STATIC void check_stringio_is_open(const mp_obj_stringio_t *o) {
pythontech 0:5868e8752d44 47 if (o->vstr == NULL) {
pythontech 0:5868e8752d44 48 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "I/O operation on closed file"));
pythontech 0:5868e8752d44 49 }
pythontech 0:5868e8752d44 50 }
pythontech 0:5868e8752d44 51 #else
pythontech 0:5868e8752d44 52 #define check_stringio_is_open(o)
pythontech 0:5868e8752d44 53 #endif
pythontech 0:5868e8752d44 54
pythontech 0:5868e8752d44 55 STATIC void stringio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pythontech 0:5868e8752d44 56 (void)kind;
pythontech 0:5868e8752d44 57 mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 58 mp_printf(print, self->base.type == &mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>", self);
pythontech 0:5868e8752d44 59 }
pythontech 0:5868e8752d44 60
pythontech 0:5868e8752d44 61 STATIC mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
pythontech 0:5868e8752d44 62 (void)errcode;
pythontech 0:5868e8752d44 63 mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
pythontech 0:5868e8752d44 64 check_stringio_is_open(o);
pythontech 0:5868e8752d44 65 mp_uint_t remaining = o->vstr->len - o->pos;
pythontech 0:5868e8752d44 66 if (size > remaining) {
pythontech 0:5868e8752d44 67 size = remaining;
pythontech 0:5868e8752d44 68 }
pythontech 0:5868e8752d44 69 memcpy(buf, o->vstr->buf + o->pos, size);
pythontech 0:5868e8752d44 70 o->pos += size;
pythontech 0:5868e8752d44 71 return size;
pythontech 0:5868e8752d44 72 }
pythontech 0:5868e8752d44 73
pythontech 0:5868e8752d44 74 STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
pythontech 0:5868e8752d44 75 (void)errcode;
pythontech 0:5868e8752d44 76 mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
pythontech 0:5868e8752d44 77 check_stringio_is_open(o);
pythontech 0:5868e8752d44 78 mp_uint_t remaining = o->vstr->alloc - o->pos;
pythontech 0:5868e8752d44 79 if (size > remaining) {
pythontech 0:5868e8752d44 80 // Take all what's already allocated...
pythontech 0:5868e8752d44 81 o->vstr->len = o->vstr->alloc;
pythontech 0:5868e8752d44 82 // ... and add more
pythontech 0:5868e8752d44 83 vstr_add_len(o->vstr, size - remaining);
pythontech 0:5868e8752d44 84 }
pythontech 0:5868e8752d44 85 memcpy(o->vstr->buf + o->pos, buf, size);
pythontech 0:5868e8752d44 86 o->pos += size;
pythontech 0:5868e8752d44 87 if (o->pos > o->vstr->len) {
pythontech 0:5868e8752d44 88 o->vstr->len = o->pos;
pythontech 0:5868e8752d44 89 }
pythontech 0:5868e8752d44 90 return size;
pythontech 0:5868e8752d44 91 }
pythontech 0:5868e8752d44 92
pythontech 0:5868e8752d44 93 #define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
pythontech 0:5868e8752d44 94
pythontech 0:5868e8752d44 95 STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
pythontech 0:5868e8752d44 96 mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 97 check_stringio_is_open(self);
pythontech 0:5868e8752d44 98 return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len);
pythontech 0:5868e8752d44 99 }
pythontech 0:5868e8752d44 100 STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
pythontech 0:5868e8752d44 101
pythontech 0:5868e8752d44 102 STATIC mp_obj_t stringio_close(mp_obj_t self_in) {
pythontech 0:5868e8752d44 103 mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 104 #if MICROPY_CPYTHON_COMPAT
pythontech 0:5868e8752d44 105 vstr_free(self->vstr);
pythontech 0:5868e8752d44 106 self->vstr = NULL;
pythontech 0:5868e8752d44 107 #else
pythontech 0:5868e8752d44 108 vstr_clear(self->vstr);
pythontech 0:5868e8752d44 109 self->vstr->alloc = 0;
pythontech 0:5868e8752d44 110 self->vstr->len = 0;
pythontech 0:5868e8752d44 111 self->pos = 0;
pythontech 0:5868e8752d44 112 #endif
pythontech 0:5868e8752d44 113 return mp_const_none;
pythontech 0:5868e8752d44 114 }
pythontech 0:5868e8752d44 115 STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close);
pythontech 0:5868e8752d44 116
pythontech 0:5868e8752d44 117 STATIC mp_obj_t stringio___exit__(size_t n_args, const mp_obj_t *args) {
pythontech 0:5868e8752d44 118 (void)n_args;
pythontech 0:5868e8752d44 119 return stringio_close(args[0]);
pythontech 0:5868e8752d44 120 }
pythontech 0:5868e8752d44 121 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
pythontech 0:5868e8752d44 122
pythontech 0:5868e8752d44 123 STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
pythontech 0:5868e8752d44 124 mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
pythontech 0:5868e8752d44 125 o->base.type = type;
pythontech 0:5868e8752d44 126 o->vstr = vstr_new();
pythontech 0:5868e8752d44 127 o->pos = 0;
pythontech 0:5868e8752d44 128 return o;
pythontech 0:5868e8752d44 129 }
pythontech 0:5868e8752d44 130
pythontech 0:5868e8752d44 131 STATIC mp_obj_t stringio_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
pythontech 0:5868e8752d44 132 (void)n_kw; // TODO check n_kw==0
pythontech 0:5868e8752d44 133 mp_obj_stringio_t *o = stringio_new(type_in);
pythontech 0:5868e8752d44 134
pythontech 0:5868e8752d44 135 if (n_args > 0) {
pythontech 0:5868e8752d44 136 mp_buffer_info_t bufinfo;
pythontech 0:5868e8752d44 137 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
pythontech 0:5868e8752d44 138 stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL);
pythontech 0:5868e8752d44 139 // Cur ptr is always at the beginning of buffer at the construction
pythontech 0:5868e8752d44 140 o->pos = 0;
pythontech 0:5868e8752d44 141 }
pythontech 0:5868e8752d44 142 return MP_OBJ_FROM_PTR(o);
pythontech 0:5868e8752d44 143 }
pythontech 0:5868e8752d44 144
pythontech 0:5868e8752d44 145 STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
pythontech 0:5868e8752d44 146 { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
pythontech 0:5868e8752d44 147 { MP_ROM_QSTR(MP_QSTR_readall), MP_ROM_PTR(&mp_stream_readall_obj) },
pythontech 0:5868e8752d44 148 { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
pythontech 0:5868e8752d44 149 { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
pythontech 0:5868e8752d44 150 { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&stringio_close_obj) },
pythontech 0:5868e8752d44 151 { MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
pythontech 0:5868e8752d44 152 { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
pythontech 0:5868e8752d44 153 { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) },
pythontech 0:5868e8752d44 154 };
pythontech 0:5868e8752d44 155
pythontech 0:5868e8752d44 156 STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
pythontech 0:5868e8752d44 157
pythontech 0:5868e8752d44 158 STATIC const mp_stream_p_t stringio_stream_p = {
pythontech 0:5868e8752d44 159 .read = stringio_read,
pythontech 0:5868e8752d44 160 .write = stringio_write,
pythontech 0:5868e8752d44 161 .is_text = true,
pythontech 0:5868e8752d44 162 };
pythontech 0:5868e8752d44 163
pythontech 0:5868e8752d44 164 STATIC const mp_stream_p_t bytesio_stream_p = {
pythontech 0:5868e8752d44 165 .read = stringio_read,
pythontech 0:5868e8752d44 166 .write = stringio_write,
pythontech 0:5868e8752d44 167 };
pythontech 0:5868e8752d44 168
pythontech 0:5868e8752d44 169 const mp_obj_type_t mp_type_stringio = {
pythontech 0:5868e8752d44 170 { &mp_type_type },
pythontech 0:5868e8752d44 171 .name = MP_QSTR_StringIO,
pythontech 0:5868e8752d44 172 .print = stringio_print,
pythontech 0:5868e8752d44 173 .make_new = stringio_make_new,
pythontech 0:5868e8752d44 174 .getiter = mp_identity,
pythontech 0:5868e8752d44 175 .iternext = mp_stream_unbuffered_iter,
pythontech 0:5868e8752d44 176 .stream_p = &stringio_stream_p,
pythontech 0:5868e8752d44 177 .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
pythontech 0:5868e8752d44 178 };
pythontech 0:5868e8752d44 179
pythontech 0:5868e8752d44 180 #if MICROPY_PY_IO_BYTESIO
pythontech 0:5868e8752d44 181 const mp_obj_type_t mp_type_bytesio = {
pythontech 0:5868e8752d44 182 { &mp_type_type },
pythontech 0:5868e8752d44 183 .name = MP_QSTR_BytesIO,
pythontech 0:5868e8752d44 184 .print = stringio_print,
pythontech 0:5868e8752d44 185 .make_new = stringio_make_new,
pythontech 0:5868e8752d44 186 .getiter = mp_identity,
pythontech 0:5868e8752d44 187 .iternext = mp_stream_unbuffered_iter,
pythontech 0:5868e8752d44 188 .stream_p = &bytesio_stream_p,
pythontech 0:5868e8752d44 189 .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
pythontech 0:5868e8752d44 190 };
pythontech 0:5868e8752d44 191 #endif
pythontech 0:5868e8752d44 192
pythontech 0:5868e8752d44 193 #endif