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 <stdlib.h>
pythontech 0:5868e8752d44 29 #include <assert.h>
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 #include "py/nlr.h"
pythontech 0:5868e8752d44 32 #include "py/obj.h"
pythontech 0:5868e8752d44 33 #include "py/runtime.h"
pythontech 0:5868e8752d44 34 #include "py/bc.h"
pythontech 0:5868e8752d44 35 #include "py/objgenerator.h"
pythontech 0:5868e8752d44 36 #include "py/objfun.h"
pythontech 0:5868e8752d44 37
pythontech 0:5868e8752d44 38 /******************************************************************************/
pythontech 0:5868e8752d44 39 /* generator wrapper */
pythontech 0:5868e8752d44 40
pythontech 0:5868e8752d44 41 typedef struct _mp_obj_gen_wrap_t {
pythontech 0:5868e8752d44 42 mp_obj_base_t base;
pythontech 0:5868e8752d44 43 mp_obj_t *fun;
pythontech 0:5868e8752d44 44 } mp_obj_gen_wrap_t;
pythontech 0:5868e8752d44 45
pythontech 0:5868e8752d44 46 typedef struct _mp_obj_gen_instance_t {
pythontech 0:5868e8752d44 47 mp_obj_base_t base;
pythontech 0:5868e8752d44 48 mp_obj_dict_t *globals;
pythontech 0:5868e8752d44 49 mp_code_state code_state;
pythontech 0:5868e8752d44 50 } mp_obj_gen_instance_t;
pythontech 0:5868e8752d44 51
pythontech 0:5868e8752d44 52 STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
pythontech 0:5868e8752d44 53 mp_obj_gen_wrap_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 54 mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
pythontech 0:5868e8752d44 55 assert(self_fun->base.type == &mp_type_fun_bc);
pythontech 0:5868e8752d44 56
pythontech 0:5868e8752d44 57 // get start of bytecode
pythontech 0:5868e8752d44 58 const byte *ip = self_fun->bytecode;
pythontech 0:5868e8752d44 59
pythontech 0:5868e8752d44 60 // bytecode prelude: get state size and exception stack size
pythontech 0:5868e8752d44 61 mp_uint_t n_state = mp_decode_uint(&ip);
pythontech 0:5868e8752d44 62 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
pythontech 0:5868e8752d44 63
pythontech 0:5868e8752d44 64 // allocate the generator object, with room for local stack and exception stack
pythontech 0:5868e8752d44 65 mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte,
pythontech 0:5868e8752d44 66 n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
pythontech 0:5868e8752d44 67 o->base.type = &mp_type_gen_instance;
pythontech 0:5868e8752d44 68
pythontech 0:5868e8752d44 69 o->globals = self_fun->globals;
pythontech 0:5868e8752d44 70 o->code_state.n_state = n_state;
pythontech 0:5868e8752d44 71 o->code_state.ip = (byte*)(ip - self_fun->bytecode); // offset to prelude
pythontech 0:5868e8752d44 72 mp_setup_code_state(&o->code_state, self_fun, n_args, n_kw, args);
pythontech 0:5868e8752d44 73 return MP_OBJ_FROM_PTR(o);
pythontech 0:5868e8752d44 74 }
pythontech 0:5868e8752d44 75
pythontech 0:5868e8752d44 76 const mp_obj_type_t mp_type_gen_wrap = {
pythontech 0:5868e8752d44 77 { &mp_type_type },
pythontech 0:5868e8752d44 78 .name = MP_QSTR_generator,
pythontech 0:5868e8752d44 79 .call = gen_wrap_call,
pythontech 0:5868e8752d44 80 };
pythontech 0:5868e8752d44 81
pythontech 0:5868e8752d44 82 mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
pythontech 0:5868e8752d44 83 mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
pythontech 0:5868e8752d44 84 o->base.type = &mp_type_gen_wrap;
pythontech 0:5868e8752d44 85 o->fun = MP_OBJ_TO_PTR(fun);
pythontech 0:5868e8752d44 86 return MP_OBJ_FROM_PTR(o);
pythontech 0:5868e8752d44 87 }
pythontech 0:5868e8752d44 88
pythontech 0:5868e8752d44 89 /******************************************************************************/
pythontech 0:5868e8752d44 90 /* generator instance */
pythontech 0:5868e8752d44 91
pythontech 0:5868e8752d44 92 STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pythontech 0:5868e8752d44 93 (void)kind;
pythontech 0:5868e8752d44 94 mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 95 mp_printf(print, "<generator object '%q' at %p>", mp_obj_code_get_name(self->code_state.code_info), self);
pythontech 0:5868e8752d44 96 }
pythontech 0:5868e8752d44 97
pythontech 0:5868e8752d44 98 mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
pythontech 0:5868e8752d44 99 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
pythontech 0:5868e8752d44 100 mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 101 if (self->code_state.ip == 0) {
pythontech 0:5868e8752d44 102 *ret_val = MP_OBJ_STOP_ITERATION;
pythontech 0:5868e8752d44 103 return MP_VM_RETURN_NORMAL;
pythontech 0:5868e8752d44 104 }
pythontech 0:5868e8752d44 105 if (self->code_state.sp == self->code_state.state - 1) {
pythontech 0:5868e8752d44 106 if (send_value != mp_const_none) {
pythontech 0:5868e8752d44 107 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
pythontech 0:5868e8752d44 108 }
pythontech 0:5868e8752d44 109 } else {
pythontech 0:5868e8752d44 110 *self->code_state.sp = send_value;
pythontech 0:5868e8752d44 111 }
pythontech 0:5868e8752d44 112 mp_obj_dict_t *old_globals = mp_globals_get();
pythontech 0:5868e8752d44 113 mp_globals_set(self->globals);
pythontech 0:5868e8752d44 114 mp_vm_return_kind_t ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
pythontech 0:5868e8752d44 115 mp_globals_set(old_globals);
pythontech 0:5868e8752d44 116
pythontech 0:5868e8752d44 117 switch (ret_kind) {
pythontech 0:5868e8752d44 118 case MP_VM_RETURN_NORMAL:
pythontech 0:5868e8752d44 119 default:
pythontech 0:5868e8752d44 120 // Explicitly mark generator as completed. If we don't do this,
pythontech 0:5868e8752d44 121 // subsequent next() may re-execute statements after last yield
pythontech 0:5868e8752d44 122 // again and again, leading to side effects.
pythontech 0:5868e8752d44 123 // TODO: check how return with value behaves under such conditions
pythontech 0:5868e8752d44 124 // in CPython.
pythontech 0:5868e8752d44 125 self->code_state.ip = 0;
pythontech 0:5868e8752d44 126 *ret_val = *self->code_state.sp;
pythontech 0:5868e8752d44 127 break;
pythontech 0:5868e8752d44 128
pythontech 0:5868e8752d44 129 case MP_VM_RETURN_YIELD:
pythontech 0:5868e8752d44 130 *ret_val = *self->code_state.sp;
pythontech 0:5868e8752d44 131 if (*ret_val == MP_OBJ_STOP_ITERATION) {
pythontech 0:5868e8752d44 132 self->code_state.ip = 0;
pythontech 0:5868e8752d44 133 }
pythontech 0:5868e8752d44 134 break;
pythontech 0:5868e8752d44 135
pythontech 0:5868e8752d44 136 case MP_VM_RETURN_EXCEPTION:
pythontech 0:5868e8752d44 137 self->code_state.ip = 0;
pythontech 0:5868e8752d44 138 *ret_val = self->code_state.state[self->code_state.n_state - 1];
pythontech 0:5868e8752d44 139 break;
pythontech 0:5868e8752d44 140 }
pythontech 0:5868e8752d44 141
pythontech 0:5868e8752d44 142 return ret_kind;
pythontech 0:5868e8752d44 143 }
pythontech 0:5868e8752d44 144
pythontech 0:5868e8752d44 145 STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
pythontech 0:5868e8752d44 146 mp_obj_t ret;
pythontech 0:5868e8752d44 147 switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
pythontech 0:5868e8752d44 148 case MP_VM_RETURN_NORMAL:
pythontech 0:5868e8752d44 149 default:
pythontech 0:5868e8752d44 150 // Optimize return w/o value in case generator is used in for loop
pythontech 0:5868e8752d44 151 if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
pythontech 0:5868e8752d44 152 return MP_OBJ_STOP_ITERATION;
pythontech 0:5868e8752d44 153 } else {
pythontech 0:5868e8752d44 154 nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
pythontech 0:5868e8752d44 155 }
pythontech 0:5868e8752d44 156
pythontech 0:5868e8752d44 157 case MP_VM_RETURN_YIELD:
pythontech 0:5868e8752d44 158 if (throw_value != MP_OBJ_NULL && mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(throw_value)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
pythontech 0:5868e8752d44 159 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
pythontech 0:5868e8752d44 160 }
pythontech 0:5868e8752d44 161 return ret;
pythontech 0:5868e8752d44 162
pythontech 0:5868e8752d44 163 case MP_VM_RETURN_EXCEPTION:
pythontech 0:5868e8752d44 164 // TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
pythontech 0:5868e8752d44 165 // of mp_iternext() protocol, but this function is called by other methods
pythontech 0:5868e8752d44 166 // too, which may not handled MP_OBJ_STOP_ITERATION.
pythontech 0:5868e8752d44 167 if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
pythontech 0:5868e8752d44 168 mp_obj_t val = mp_obj_exception_get_value(ret);
pythontech 0:5868e8752d44 169 if (val == mp_const_none) {
pythontech 0:5868e8752d44 170 return MP_OBJ_STOP_ITERATION;
pythontech 0:5868e8752d44 171 }
pythontech 0:5868e8752d44 172 }
pythontech 0:5868e8752d44 173 nlr_raise(ret);
pythontech 0:5868e8752d44 174 }
pythontech 0:5868e8752d44 175 }
pythontech 0:5868e8752d44 176
pythontech 0:5868e8752d44 177 STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
pythontech 0:5868e8752d44 178 return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
pythontech 0:5868e8752d44 179 }
pythontech 0:5868e8752d44 180
pythontech 0:5868e8752d44 181 STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
pythontech 0:5868e8752d44 182 mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
pythontech 0:5868e8752d44 183 if (ret == MP_OBJ_STOP_ITERATION) {
pythontech 0:5868e8752d44 184 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
pythontech 0:5868e8752d44 185 } else {
pythontech 0:5868e8752d44 186 return ret;
pythontech 0:5868e8752d44 187 }
pythontech 0:5868e8752d44 188 }
pythontech 0:5868e8752d44 189
pythontech 0:5868e8752d44 190 STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
pythontech 0:5868e8752d44 191
pythontech 0:5868e8752d44 192 STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
pythontech 0:5868e8752d44 193 STATIC mp_obj_t gen_instance_throw(size_t n_args, const mp_obj_t *args) {
pythontech 0:5868e8752d44 194 mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
pythontech 0:5868e8752d44 195 exc = mp_make_raise_obj(exc);
pythontech 0:5868e8752d44 196
pythontech 0:5868e8752d44 197 mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
pythontech 0:5868e8752d44 198 if (ret == MP_OBJ_STOP_ITERATION) {
pythontech 0:5868e8752d44 199 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
pythontech 0:5868e8752d44 200 } else {
pythontech 0:5868e8752d44 201 return ret;
pythontech 0:5868e8752d44 202 }
pythontech 0:5868e8752d44 203 }
pythontech 0:5868e8752d44 204
pythontech 0:5868e8752d44 205 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
pythontech 0:5868e8752d44 206
pythontech 0:5868e8752d44 207 STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
pythontech 0:5868e8752d44 208 mp_obj_t ret;
pythontech 0:5868e8752d44 209 switch (mp_obj_gen_resume(self_in, mp_const_none, MP_OBJ_FROM_PTR(&mp_const_GeneratorExit_obj), &ret)) {
pythontech 0:5868e8752d44 210 case MP_VM_RETURN_YIELD:
pythontech 0:5868e8752d44 211 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
pythontech 0:5868e8752d44 212
pythontech 0:5868e8752d44 213 // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
pythontech 0:5868e8752d44 214 case MP_VM_RETURN_EXCEPTION:
pythontech 0:5868e8752d44 215 // ret should always be an instance of an exception class
pythontech 0:5868e8752d44 216 if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit)) ||
pythontech 0:5868e8752d44 217 mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
pythontech 0:5868e8752d44 218 return mp_const_none;
pythontech 0:5868e8752d44 219 }
pythontech 0:5868e8752d44 220 nlr_raise(ret);
pythontech 0:5868e8752d44 221
pythontech 0:5868e8752d44 222 default:
pythontech 0:5868e8752d44 223 // The only choice left is MP_VM_RETURN_NORMAL which is successful close
pythontech 0:5868e8752d44 224 return mp_const_none;
pythontech 0:5868e8752d44 225 }
pythontech 0:5868e8752d44 226 }
pythontech 0:5868e8752d44 227
pythontech 0:5868e8752d44 228 STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
pythontech 0:5868e8752d44 229
pythontech 0:5868e8752d44 230 STATIC const mp_rom_map_elem_t gen_instance_locals_dict_table[] = {
pythontech 0:5868e8752d44 231 { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&gen_instance_close_obj) },
pythontech 0:5868e8752d44 232 { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&gen_instance_send_obj) },
pythontech 0:5868e8752d44 233 { MP_ROM_QSTR(MP_QSTR_throw), MP_ROM_PTR(&gen_instance_throw_obj) },
pythontech 0:5868e8752d44 234 };
pythontech 0:5868e8752d44 235
pythontech 0:5868e8752d44 236 STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
pythontech 0:5868e8752d44 237
pythontech 0:5868e8752d44 238 const mp_obj_type_t mp_type_gen_instance = {
pythontech 0:5868e8752d44 239 { &mp_type_type },
pythontech 0:5868e8752d44 240 .name = MP_QSTR_generator,
pythontech 0:5868e8752d44 241 .print = gen_instance_print,
pythontech 0:5868e8752d44 242 .getiter = mp_identity,
pythontech 0:5868e8752d44 243 .iternext = gen_instance_iternext,
pythontech 0:5868e8752d44 244 .locals_dict = (mp_obj_dict_t*)&gen_instance_locals_dict,
pythontech 0:5868e8752d44 245 };