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 *
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 <stdint.h>
pythontech 0:5868e8752d44 28
pythontech 0:5868e8752d44 29 #include "py/nlr.h"
pythontech 0:5868e8752d44 30 #include "py/objfun.h"
pythontech 0:5868e8752d44 31 #include "py/compile.h"
pythontech 0:5868e8752d44 32 #include "py/runtime.h"
pythontech 0:5868e8752d44 33 #include "py/builtin.h"
pythontech 0:5868e8752d44 34
pythontech 0:5868e8752d44 35 #if MICROPY_PY_BUILTINS_COMPILE
pythontech 0:5868e8752d44 36
pythontech 0:5868e8752d44 37 typedef struct _mp_obj_code_t {
pythontech 0:5868e8752d44 38 mp_obj_base_t base;
pythontech 0:5868e8752d44 39 mp_obj_t module_fun;
pythontech 0:5868e8752d44 40 } mp_obj_code_t;
pythontech 0:5868e8752d44 41
pythontech 0:5868e8752d44 42 STATIC const mp_obj_type_t mp_type_code = {
pythontech 0:5868e8752d44 43 { &mp_type_type },
pythontech 0:5868e8752d44 44 .name = MP_QSTR_code,
pythontech 0:5868e8752d44 45 };
pythontech 0:5868e8752d44 46
pythontech 0:5868e8752d44 47 STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
pythontech 0:5868e8752d44 48 // save context and set new context
pythontech 0:5868e8752d44 49 mp_obj_dict_t *old_globals = mp_globals_get();
pythontech 0:5868e8752d44 50 mp_obj_dict_t *old_locals = mp_locals_get();
pythontech 0:5868e8752d44 51 mp_globals_set(globals);
pythontech 0:5868e8752d44 52 mp_locals_set(locals);
pythontech 0:5868e8752d44 53
pythontech 0:5868e8752d44 54 // a bit of a hack: fun_bc will re-set globals, so need to make sure it's
pythontech 0:5868e8752d44 55 // the correct one
pythontech 0:5868e8752d44 56 if (MP_OBJ_IS_TYPE(self->module_fun, &mp_type_fun_bc)) {
pythontech 0:5868e8752d44 57 mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
pythontech 0:5868e8752d44 58 fun_bc->globals = globals;
pythontech 0:5868e8752d44 59 }
pythontech 0:5868e8752d44 60
pythontech 0:5868e8752d44 61 // execute code
pythontech 0:5868e8752d44 62 nlr_buf_t nlr;
pythontech 0:5868e8752d44 63 if (nlr_push(&nlr) == 0) {
pythontech 0:5868e8752d44 64 mp_obj_t ret = mp_call_function_0(self->module_fun);
pythontech 0:5868e8752d44 65 nlr_pop();
pythontech 0:5868e8752d44 66 mp_globals_set(old_globals);
pythontech 0:5868e8752d44 67 mp_locals_set(old_locals);
pythontech 0:5868e8752d44 68 return ret;
pythontech 0:5868e8752d44 69 } else {
pythontech 0:5868e8752d44 70 // exception; restore context and re-raise same exception
pythontech 0:5868e8752d44 71 mp_globals_set(old_globals);
pythontech 0:5868e8752d44 72 mp_locals_set(old_locals);
pythontech 0:5868e8752d44 73 nlr_jump(nlr.ret_val);
pythontech 0:5868e8752d44 74 }
pythontech 0:5868e8752d44 75 }
pythontech 0:5868e8752d44 76
pythontech 0:5868e8752d44 77 STATIC mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) {
pythontech 0:5868e8752d44 78 (void)n_args;
pythontech 0:5868e8752d44 79
pythontech 0:5868e8752d44 80 // get the source
pythontech 0:5868e8752d44 81 mp_uint_t str_len;
pythontech 0:5868e8752d44 82 const char *str = mp_obj_str_get_data(args[0], &str_len);
pythontech 0:5868e8752d44 83
pythontech 0:5868e8752d44 84 // get the filename
pythontech 0:5868e8752d44 85 qstr filename = mp_obj_str_get_qstr(args[1]);
pythontech 0:5868e8752d44 86
pythontech 0:5868e8752d44 87 // create the lexer
pythontech 0:5868e8752d44 88 mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0);
pythontech 0:5868e8752d44 89
pythontech 0:5868e8752d44 90 // get the compile mode
pythontech 0:5868e8752d44 91 qstr mode = mp_obj_str_get_qstr(args[2]);
pythontech 0:5868e8752d44 92 mp_parse_input_kind_t parse_input_kind;
pythontech 0:5868e8752d44 93 switch (mode) {
pythontech 0:5868e8752d44 94 case MP_QSTR_single: parse_input_kind = MP_PARSE_SINGLE_INPUT; break;
pythontech 0:5868e8752d44 95 case MP_QSTR_exec: parse_input_kind = MP_PARSE_FILE_INPUT; break;
pythontech 0:5868e8752d44 96 case MP_QSTR_eval: parse_input_kind = MP_PARSE_EVAL_INPUT; break;
pythontech 0:5868e8752d44 97 default:
pythontech 0:5868e8752d44 98 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad compile mode"));
pythontech 0:5868e8752d44 99 }
pythontech 0:5868e8752d44 100
pythontech 0:5868e8752d44 101 mp_obj_code_t *code = m_new_obj(mp_obj_code_t);
pythontech 0:5868e8752d44 102 code->base.type = &mp_type_code;
pythontech 0:5868e8752d44 103 code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
pythontech 0:5868e8752d44 104 return MP_OBJ_FROM_PTR(code);
pythontech 0:5868e8752d44 105 }
pythontech 0:5868e8752d44 106 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
pythontech 0:5868e8752d44 107
pythontech 0:5868e8752d44 108 #endif // MICROPY_PY_BUILTINS_COMPILE
pythontech 0:5868e8752d44 109
pythontech 0:5868e8752d44 110 #if MICROPY_PY_BUILTINS_EVAL_EXEC
pythontech 0:5868e8752d44 111
pythontech 0:5868e8752d44 112 STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
pythontech 0:5868e8752d44 113 // work out the context
pythontech 0:5868e8752d44 114 mp_obj_dict_t *globals = mp_globals_get();
pythontech 0:5868e8752d44 115 mp_obj_dict_t *locals = mp_locals_get();
pythontech 0:5868e8752d44 116 if (n_args > 1) {
pythontech 0:5868e8752d44 117 globals = MP_OBJ_TO_PTR(args[1]);
pythontech 0:5868e8752d44 118 if (n_args > 2) {
pythontech 0:5868e8752d44 119 locals = MP_OBJ_TO_PTR(args[2]);
pythontech 0:5868e8752d44 120 } else {
pythontech 0:5868e8752d44 121 locals = globals;
pythontech 0:5868e8752d44 122 }
pythontech 0:5868e8752d44 123 }
pythontech 0:5868e8752d44 124
pythontech 0:5868e8752d44 125 #if MICROPY_PY_BUILTINS_COMPILE
pythontech 0:5868e8752d44 126 if (MP_OBJ_IS_TYPE(args[0], &mp_type_code)) {
pythontech 0:5868e8752d44 127 return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
pythontech 0:5868e8752d44 128 }
pythontech 0:5868e8752d44 129 #endif
pythontech 0:5868e8752d44 130
pythontech 0:5868e8752d44 131 mp_uint_t str_len;
pythontech 0:5868e8752d44 132 const char *str = mp_obj_str_get_data(args[0], &str_len);
pythontech 0:5868e8752d44 133
pythontech 0:5868e8752d44 134 // create the lexer
pythontech 0:5868e8752d44 135 // MP_PARSE_SINGLE_INPUT is used to indicate a file input
pythontech 0:5868e8752d44 136 mp_lexer_t *lex;
pythontech 0:5868e8752d44 137 if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
pythontech 0:5868e8752d44 138 lex = mp_lexer_new_from_file(str);
pythontech 0:5868e8752d44 139 if (lex == NULL) {
pythontech 0:5868e8752d44 140 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "could not open file '%s'", str));
pythontech 0:5868e8752d44 141 }
pythontech 0:5868e8752d44 142 parse_input_kind = MP_PARSE_FILE_INPUT;
pythontech 0:5868e8752d44 143 } else {
pythontech 0:5868e8752d44 144 lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
pythontech 0:5868e8752d44 145 }
pythontech 0:5868e8752d44 146
pythontech 0:5868e8752d44 147 return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
pythontech 0:5868e8752d44 148 }
pythontech 0:5868e8752d44 149
pythontech 0:5868e8752d44 150 STATIC mp_obj_t mp_builtin_eval(size_t n_args, const mp_obj_t *args) {
pythontech 0:5868e8752d44 151 return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
pythontech 0:5868e8752d44 152 }
pythontech 0:5868e8752d44 153 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
pythontech 0:5868e8752d44 154
pythontech 0:5868e8752d44 155 STATIC mp_obj_t mp_builtin_exec(size_t n_args, const mp_obj_t *args) {
pythontech 0:5868e8752d44 156 return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
pythontech 0:5868e8752d44 157 }
pythontech 0:5868e8752d44 158 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
pythontech 0:5868e8752d44 159
pythontech 0:5868e8752d44 160 #endif // MICROPY_PY_BUILTINS_EVAL_EXEC
pythontech 0:5868e8752d44 161
pythontech 0:5868e8752d44 162 #if MICROPY_PY_BUILTINS_EXECFILE
pythontech 0:5868e8752d44 163 STATIC mp_obj_t mp_builtin_execfile(size_t n_args, const mp_obj_t *args) {
pythontech 0:5868e8752d44 164 // MP_PARSE_SINGLE_INPUT is used to indicate a file input
pythontech 0:5868e8752d44 165 return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
pythontech 0:5868e8752d44 166 }
pythontech 0:5868e8752d44 167 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
pythontech 0:5868e8752d44 168 #endif