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 <stdlib.h>
pythontech 0:5868e8752d44 28 #include <stdio.h>
pythontech 0:5868e8752d44 29 #include <string.h>
pythontech 0:5868e8752d44 30 #include <assert.h>
pythontech 0:5868e8752d44 31
pythontech 0:5868e8752d44 32 #include "py/nlr.h"
pythontech 0:5868e8752d44 33 #include "py/parsenum.h"
pythontech 0:5868e8752d44 34 #include "py/runtime0.h"
pythontech 0:5868e8752d44 35 #include "py/runtime.h"
pythontech 0:5868e8752d44 36
pythontech 0:5868e8752d44 37 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 38
pythontech 0:5868e8752d44 39 #include <math.h>
pythontech 0:5868e8752d44 40 #include "py/formatfloat.h"
pythontech 0:5868e8752d44 41
pythontech 0:5868e8752d44 42 #if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D
pythontech 0:5868e8752d44 43
pythontech 0:5868e8752d44 44 typedef struct _mp_obj_float_t {
pythontech 0:5868e8752d44 45 mp_obj_base_t base;
pythontech 0:5868e8752d44 46 mp_float_t value;
pythontech 0:5868e8752d44 47 } mp_obj_float_t;
pythontech 0:5868e8752d44 48
pythontech 0:5868e8752d44 49 const mp_obj_float_t mp_const_float_e_obj = {{&mp_type_float}, M_E};
pythontech 0:5868e8752d44 50 const mp_obj_float_t mp_const_float_pi_obj = {{&mp_type_float}, M_PI};
pythontech 0:5868e8752d44 51
pythontech 0:5868e8752d44 52 #endif
pythontech 0:5868e8752d44 53
pythontech 0:5868e8752d44 54 STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
pythontech 0:5868e8752d44 55 (void)kind;
pythontech 0:5868e8752d44 56 mp_float_t o_val = mp_obj_float_get(o_in);
pythontech 0:5868e8752d44 57 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
pythontech 0:5868e8752d44 58 char buf[16];
pythontech 0:5868e8752d44 59 const int precision = 7;
pythontech 0:5868e8752d44 60 #else
pythontech 0:5868e8752d44 61 char buf[32];
pythontech 0:5868e8752d44 62 const int precision = 16;
pythontech 0:5868e8752d44 63 #endif
pythontech 0:5868e8752d44 64 mp_format_float(o_val, buf, sizeof(buf), 'g', precision, '\0');
pythontech 0:5868e8752d44 65 mp_print_str(print, buf);
pythontech 0:5868e8752d44 66 if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
pythontech 0:5868e8752d44 67 // Python floats always have decimal point (unless inf or nan)
pythontech 0:5868e8752d44 68 mp_print_str(print, ".0");
pythontech 0:5868e8752d44 69 }
pythontech 0:5868e8752d44 70 }
pythontech 0:5868e8752d44 71
pythontech 0:5868e8752d44 72 STATIC mp_obj_t float_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 73 (void)type_in;
pythontech 0:5868e8752d44 74 mp_arg_check_num(n_args, n_kw, 0, 1, false);
pythontech 0:5868e8752d44 75
pythontech 0:5868e8752d44 76 switch (n_args) {
pythontech 0:5868e8752d44 77 case 0:
pythontech 0:5868e8752d44 78 return mp_obj_new_float(0);
pythontech 0:5868e8752d44 79
pythontech 0:5868e8752d44 80 case 1:
pythontech 0:5868e8752d44 81 default:
pythontech 0:5868e8752d44 82 if (MP_OBJ_IS_STR(args[0])) {
pythontech 0:5868e8752d44 83 // a string, parse it
pythontech 0:5868e8752d44 84 mp_uint_t l;
pythontech 0:5868e8752d44 85 const char *s = mp_obj_str_get_data(args[0], &l);
pythontech 0:5868e8752d44 86 return mp_parse_num_decimal(s, l, false, false, NULL);
pythontech 0:5868e8752d44 87 } else if (mp_obj_is_float(args[0])) {
pythontech 0:5868e8752d44 88 // a float, just return it
pythontech 0:5868e8752d44 89 return args[0];
pythontech 0:5868e8752d44 90 } else {
pythontech 0:5868e8752d44 91 // something else, try to cast it to a float
pythontech 0:5868e8752d44 92 return mp_obj_new_float(mp_obj_get_float(args[0]));
pythontech 0:5868e8752d44 93 }
pythontech 0:5868e8752d44 94 }
pythontech 0:5868e8752d44 95 }
pythontech 0:5868e8752d44 96
pythontech 0:5868e8752d44 97 STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) {
pythontech 0:5868e8752d44 98 mp_float_t val = mp_obj_float_get(o_in);
pythontech 0:5868e8752d44 99 switch (op) {
pythontech 0:5868e8752d44 100 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(val != 0);
pythontech 0:5868e8752d44 101 case MP_UNARY_OP_POSITIVE: return o_in;
pythontech 0:5868e8752d44 102 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val);
pythontech 0:5868e8752d44 103 default: return MP_OBJ_NULL; // op not supported
pythontech 0:5868e8752d44 104 }
pythontech 0:5868e8752d44 105 }
pythontech 0:5868e8752d44 106
pythontech 0:5868e8752d44 107 STATIC mp_obj_t float_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
pythontech 0:5868e8752d44 108 mp_float_t lhs_val = mp_obj_float_get(lhs_in);
pythontech 0:5868e8752d44 109 #if MICROPY_PY_BUILTINS_COMPLEX
pythontech 0:5868e8752d44 110 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
pythontech 0:5868e8752d44 111 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs_in);
pythontech 0:5868e8752d44 112 } else
pythontech 0:5868e8752d44 113 #endif
pythontech 0:5868e8752d44 114 {
pythontech 0:5868e8752d44 115 return mp_obj_float_binary_op(op, lhs_val, rhs_in);
pythontech 0:5868e8752d44 116 }
pythontech 0:5868e8752d44 117 }
pythontech 0:5868e8752d44 118
pythontech 0:5868e8752d44 119 const mp_obj_type_t mp_type_float = {
pythontech 0:5868e8752d44 120 { &mp_type_type },
pythontech 0:5868e8752d44 121 .name = MP_QSTR_float,
pythontech 0:5868e8752d44 122 .print = float_print,
pythontech 0:5868e8752d44 123 .make_new = float_make_new,
pythontech 0:5868e8752d44 124 .unary_op = float_unary_op,
pythontech 0:5868e8752d44 125 .binary_op = float_binary_op,
pythontech 0:5868e8752d44 126 };
pythontech 0:5868e8752d44 127
pythontech 0:5868e8752d44 128 #if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D
pythontech 0:5868e8752d44 129
pythontech 0:5868e8752d44 130 mp_obj_t mp_obj_new_float(mp_float_t value) {
pythontech 0:5868e8752d44 131 mp_obj_float_t *o = m_new(mp_obj_float_t, 1);
pythontech 0:5868e8752d44 132 o->base.type = &mp_type_float;
pythontech 0:5868e8752d44 133 o->value = value;
pythontech 0:5868e8752d44 134 return MP_OBJ_FROM_PTR(o);
pythontech 0:5868e8752d44 135 }
pythontech 0:5868e8752d44 136
pythontech 0:5868e8752d44 137 mp_float_t mp_obj_float_get(mp_obj_t self_in) {
pythontech 0:5868e8752d44 138 assert(mp_obj_is_float(self_in));
pythontech 0:5868e8752d44 139 mp_obj_float_t *self = MP_OBJ_TO_PTR(self_in);
pythontech 0:5868e8752d44 140 return self->value;
pythontech 0:5868e8752d44 141 }
pythontech 0:5868e8752d44 142
pythontech 0:5868e8752d44 143 #endif
pythontech 0:5868e8752d44 144
pythontech 0:5868e8752d44 145 STATIC void mp_obj_float_divmod(mp_float_t *x, mp_float_t *y) {
pythontech 0:5868e8752d44 146 // logic here follows that of CPython
pythontech 0:5868e8752d44 147 // https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
pythontech 0:5868e8752d44 148 // x == (x//y)*y + (x%y)
pythontech 0:5868e8752d44 149 // divmod(x, y) == (x//y, x%y)
pythontech 0:5868e8752d44 150 mp_float_t mod = MICROPY_FLOAT_C_FUN(fmod)(*x, *y);
pythontech 0:5868e8752d44 151 mp_float_t div = (*x - mod) / *y;
pythontech 0:5868e8752d44 152
pythontech 0:5868e8752d44 153 // Python specs require that mod has same sign as second operand
pythontech 0:5868e8752d44 154 if (mod == 0.0) {
pythontech 0:5868e8752d44 155 mod = MICROPY_FLOAT_C_FUN(copysign)(0.0, *y);
pythontech 0:5868e8752d44 156 } else {
pythontech 0:5868e8752d44 157 if ((mod < 0.0) != (*y < 0.0)) {
pythontech 0:5868e8752d44 158 mod += *y;
pythontech 0:5868e8752d44 159 div -= 1.0;
pythontech 0:5868e8752d44 160 }
pythontech 0:5868e8752d44 161 }
pythontech 0:5868e8752d44 162
pythontech 0:5868e8752d44 163 mp_float_t floordiv;
pythontech 0:5868e8752d44 164 if (div == 0.0) {
pythontech 0:5868e8752d44 165 // if division is zero, take the correct sign of zero
pythontech 0:5868e8752d44 166 floordiv = MICROPY_FLOAT_C_FUN(copysign)(0.0, *x / *y);
pythontech 0:5868e8752d44 167 } else {
pythontech 0:5868e8752d44 168 // Python specs require that x == (x//y)*y + (x%y)
pythontech 0:5868e8752d44 169 floordiv = MICROPY_FLOAT_C_FUN(floor)(div);
pythontech 0:5868e8752d44 170 if (div - floordiv > 0.5) {
pythontech 0:5868e8752d44 171 floordiv += 1.0;
pythontech 0:5868e8752d44 172 }
pythontech 0:5868e8752d44 173 }
pythontech 0:5868e8752d44 174
pythontech 0:5868e8752d44 175 // return results
pythontech 0:5868e8752d44 176 *x = floordiv;
pythontech 0:5868e8752d44 177 *y = mod;
pythontech 0:5868e8752d44 178 }
pythontech 0:5868e8752d44 179
pythontech 0:5868e8752d44 180 mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs_in) {
pythontech 0:5868e8752d44 181 mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
pythontech 0:5868e8752d44 182 switch (op) {
pythontech 0:5868e8752d44 183 case MP_BINARY_OP_ADD:
pythontech 0:5868e8752d44 184 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
pythontech 0:5868e8752d44 185 case MP_BINARY_OP_SUBTRACT:
pythontech 0:5868e8752d44 186 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
pythontech 0:5868e8752d44 187 case MP_BINARY_OP_MULTIPLY:
pythontech 0:5868e8752d44 188 case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
pythontech 0:5868e8752d44 189 case MP_BINARY_OP_FLOOR_DIVIDE:
pythontech 0:5868e8752d44 190 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
pythontech 0:5868e8752d44 191 if (rhs_val == 0) {
pythontech 0:5868e8752d44 192 zero_division_error:
pythontech 0:5868e8752d44 193 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
pythontech 0:5868e8752d44 194 }
pythontech 0:5868e8752d44 195 // Python specs require that x == (x//y)*y + (x%y) so we must
pythontech 0:5868e8752d44 196 // call divmod to compute the correct floor division, which
pythontech 0:5868e8752d44 197 // returns the floor divide in lhs_val.
pythontech 0:5868e8752d44 198 mp_obj_float_divmod(&lhs_val, &rhs_val);
pythontech 0:5868e8752d44 199 break;
pythontech 0:5868e8752d44 200 case MP_BINARY_OP_TRUE_DIVIDE:
pythontech 0:5868e8752d44 201 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
pythontech 0:5868e8752d44 202 if (rhs_val == 0) {
pythontech 0:5868e8752d44 203 goto zero_division_error;
pythontech 0:5868e8752d44 204 }
pythontech 0:5868e8752d44 205 lhs_val /= rhs_val;
pythontech 0:5868e8752d44 206 break;
pythontech 0:5868e8752d44 207 case MP_BINARY_OP_MODULO:
pythontech 0:5868e8752d44 208 case MP_BINARY_OP_INPLACE_MODULO:
pythontech 0:5868e8752d44 209 if (rhs_val == 0) {
pythontech 0:5868e8752d44 210 goto zero_division_error;
pythontech 0:5868e8752d44 211 }
pythontech 0:5868e8752d44 212 lhs_val = MICROPY_FLOAT_C_FUN(fmod)(lhs_val, rhs_val);
pythontech 0:5868e8752d44 213 // Python specs require that mod has same sign as second operand
pythontech 0:5868e8752d44 214 if (lhs_val == 0.0) {
pythontech 0:5868e8752d44 215 lhs_val = MICROPY_FLOAT_C_FUN(copysign)(0.0, rhs_val);
pythontech 0:5868e8752d44 216 } else {
pythontech 0:5868e8752d44 217 if ((lhs_val < 0.0) != (rhs_val < 0.0)) {
pythontech 0:5868e8752d44 218 lhs_val += rhs_val;
pythontech 0:5868e8752d44 219 }
pythontech 0:5868e8752d44 220 }
pythontech 0:5868e8752d44 221 break;
pythontech 0:5868e8752d44 222 case MP_BINARY_OP_POWER:
pythontech 0:5868e8752d44 223 case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break;
pythontech 0:5868e8752d44 224 case MP_BINARY_OP_DIVMOD: {
pythontech 0:5868e8752d44 225 if (rhs_val == 0) {
pythontech 0:5868e8752d44 226 goto zero_division_error;
pythontech 0:5868e8752d44 227 }
pythontech 0:5868e8752d44 228 mp_obj_float_divmod(&lhs_val, &rhs_val);
pythontech 0:5868e8752d44 229 mp_obj_t tuple[2] = {
pythontech 0:5868e8752d44 230 mp_obj_new_float(lhs_val),
pythontech 0:5868e8752d44 231 mp_obj_new_float(rhs_val),
pythontech 0:5868e8752d44 232 };
pythontech 0:5868e8752d44 233 return mp_obj_new_tuple(2, tuple);
pythontech 0:5868e8752d44 234 }
pythontech 0:5868e8752d44 235 case MP_BINARY_OP_LESS: return mp_obj_new_bool(lhs_val < rhs_val);
pythontech 0:5868e8752d44 236 case MP_BINARY_OP_MORE: return mp_obj_new_bool(lhs_val > rhs_val);
pythontech 0:5868e8752d44 237 case MP_BINARY_OP_EQUAL: return mp_obj_new_bool(lhs_val == rhs_val);
pythontech 0:5868e8752d44 238 case MP_BINARY_OP_LESS_EQUAL: return mp_obj_new_bool(lhs_val <= rhs_val);
pythontech 0:5868e8752d44 239 case MP_BINARY_OP_MORE_EQUAL: return mp_obj_new_bool(lhs_val >= rhs_val);
pythontech 0:5868e8752d44 240
pythontech 0:5868e8752d44 241 default:
pythontech 0:5868e8752d44 242 return MP_OBJ_NULL; // op not supported
pythontech 0:5868e8752d44 243 }
pythontech 0:5868e8752d44 244 return mp_obj_new_float(lhs_val);
pythontech 0:5868e8752d44 245 }
pythontech 0:5868e8752d44 246
pythontech 0:5868e8752d44 247 #endif // MICROPY_PY_BUILTINS_FLOAT