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 #ifndef __MICROPY_INCLUDED_PY_RUNTIME_H__
pythontech 0:5868e8752d44 27 #define __MICROPY_INCLUDED_PY_RUNTIME_H__
pythontech 0:5868e8752d44 28
pythontech 0:5868e8752d44 29 #include "py/mpstate.h"
pythontech 0:5868e8752d44 30 #include "py/obj.h"
pythontech 0:5868e8752d44 31
pythontech 0:5868e8752d44 32 typedef enum {
pythontech 0:5868e8752d44 33 MP_VM_RETURN_NORMAL,
pythontech 0:5868e8752d44 34 MP_VM_RETURN_YIELD,
pythontech 0:5868e8752d44 35 MP_VM_RETURN_EXCEPTION,
pythontech 0:5868e8752d44 36 } mp_vm_return_kind_t;
pythontech 0:5868e8752d44 37
pythontech 0:5868e8752d44 38 typedef enum {
pythontech 0:5868e8752d44 39 MP_ARG_BOOL = 0x001,
pythontech 0:5868e8752d44 40 MP_ARG_INT = 0x002,
pythontech 0:5868e8752d44 41 MP_ARG_OBJ = 0x003,
pythontech 0:5868e8752d44 42 MP_ARG_KIND_MASK = 0x0ff,
pythontech 0:5868e8752d44 43 MP_ARG_REQUIRED = 0x100,
pythontech 0:5868e8752d44 44 MP_ARG_KW_ONLY = 0x200,
pythontech 0:5868e8752d44 45 } mp_arg_flag_t;
pythontech 0:5868e8752d44 46
pythontech 0:5868e8752d44 47 typedef union _mp_arg_val_t {
pythontech 0:5868e8752d44 48 bool u_bool;
pythontech 0:5868e8752d44 49 mp_int_t u_int;
pythontech 0:5868e8752d44 50 mp_obj_t u_obj;
pythontech 0:5868e8752d44 51 mp_rom_obj_t u_rom_obj;
pythontech 0:5868e8752d44 52 } mp_arg_val_t;
pythontech 0:5868e8752d44 53
pythontech 0:5868e8752d44 54 typedef struct _mp_arg_t {
pythontech 0:5868e8752d44 55 qstr qst;
pythontech 0:5868e8752d44 56 mp_uint_t flags;
pythontech 0:5868e8752d44 57 mp_arg_val_t defval;
pythontech 0:5868e8752d44 58 } mp_arg_t;
pythontech 0:5868e8752d44 59
pythontech 0:5868e8752d44 60 // defined in objtype.c
pythontech 0:5868e8752d44 61 extern const qstr mp_unary_op_method_name[];
pythontech 0:5868e8752d44 62 extern const qstr mp_binary_op_method_name[];
pythontech 0:5868e8752d44 63
pythontech 0:5868e8752d44 64 void mp_init(void);
pythontech 0:5868e8752d44 65 void mp_deinit(void);
pythontech 0:5868e8752d44 66
pythontech 0:5868e8752d44 67 // extra printing method specifically for mp_obj_t's which are integral type
pythontech 0:5868e8752d44 68 int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec);
pythontech 0:5868e8752d44 69
pythontech 0:5868e8752d44 70 void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw);
pythontech 0:5868e8752d44 71 void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
pythontech 0:5868e8752d44 72 void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
pythontech 0:5868e8752d44 73 NORETURN void mp_arg_error_terse_mismatch(void);
pythontech 0:5868e8752d44 74 NORETURN void mp_arg_error_unimpl_kw(void);
pythontech 0:5868e8752d44 75
pythontech 0:5868e8752d44 76 static inline mp_obj_dict_t *mp_locals_get(void) { return MP_STATE_CTX(dict_locals); }
pythontech 0:5868e8752d44 77 static inline void mp_locals_set(mp_obj_dict_t *d) { MP_STATE_CTX(dict_locals) = d; }
pythontech 0:5868e8752d44 78 static inline mp_obj_dict_t *mp_globals_get(void) { return MP_STATE_CTX(dict_globals); }
pythontech 0:5868e8752d44 79 static inline void mp_globals_set(mp_obj_dict_t *d) { MP_STATE_CTX(dict_globals) = d; }
pythontech 0:5868e8752d44 80
pythontech 0:5868e8752d44 81 mp_obj_t mp_load_name(qstr qst);
pythontech 0:5868e8752d44 82 mp_obj_t mp_load_global(qstr qst);
pythontech 0:5868e8752d44 83 mp_obj_t mp_load_build_class(void);
pythontech 0:5868e8752d44 84 void mp_store_name(qstr qst, mp_obj_t obj);
pythontech 0:5868e8752d44 85 void mp_store_global(qstr qst, mp_obj_t obj);
pythontech 0:5868e8752d44 86 void mp_delete_name(qstr qst);
pythontech 0:5868e8752d44 87 void mp_delete_global(qstr qst);
pythontech 0:5868e8752d44 88
pythontech 0:5868e8752d44 89 mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg);
pythontech 0:5868e8752d44 90 mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs);
pythontech 0:5868e8752d44 91
pythontech 0:5868e8752d44 92 mp_obj_t mp_call_function_0(mp_obj_t fun);
pythontech 0:5868e8752d44 93 mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg);
pythontech 0:5868e8752d44 94 mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
pythontech 0:5868e8752d44 95 mp_obj_t mp_call_function_n_kw(mp_obj_t fun, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
pythontech 0:5868e8752d44 96 mp_obj_t mp_call_method_n_kw(mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
pythontech 0:5868e8752d44 97 mp_obj_t mp_call_method_n_kw_var(bool have_self, mp_uint_t n_args_n_kw, const mp_obj_t *args);
pythontech 0:5868e8752d44 98
pythontech 0:5868e8752d44 99 typedef struct _mp_call_args_t {
pythontech 0:5868e8752d44 100 mp_obj_t fun;
pythontech 0:5868e8752d44 101 mp_uint_t n_args, n_kw, n_alloc;
pythontech 0:5868e8752d44 102 mp_obj_t *args;
pythontech 0:5868e8752d44 103 } mp_call_args_t;
pythontech 0:5868e8752d44 104
pythontech 0:5868e8752d44 105 #if MICROPY_STACKLESS
pythontech 0:5868e8752d44 106 // Takes arguments which are the most general mix of Python arg types, and
pythontech 0:5868e8752d44 107 // prepares argument array suitable for passing to ->call() method of a
pythontech 0:5868e8752d44 108 // function object (and mp_call_function_n_kw()).
pythontech 0:5868e8752d44 109 // (Only needed in stackless mode.)
pythontech 0:5868e8752d44 110 void mp_call_prepare_args_n_kw_var(bool have_self, mp_uint_t n_args_n_kw, const mp_obj_t *args, mp_call_args_t *out_args);
pythontech 0:5868e8752d44 111 #endif
pythontech 0:5868e8752d44 112
pythontech 0:5868e8752d44 113 void mp_unpack_sequence(mp_obj_t seq, mp_uint_t num, mp_obj_t *items);
pythontech 0:5868e8752d44 114 void mp_unpack_ex(mp_obj_t seq, mp_uint_t num, mp_obj_t *items);
pythontech 0:5868e8752d44 115 mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value);
pythontech 0:5868e8752d44 116 mp_obj_t mp_load_attr(mp_obj_t base, qstr attr);
pythontech 0:5868e8752d44 117 void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest);
pythontech 0:5868e8752d44 118 void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
pythontech 0:5868e8752d44 119 void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
pythontech 0:5868e8752d44 120 void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
pythontech 0:5868e8752d44 121
pythontech 0:5868e8752d44 122 mp_obj_t mp_getiter(mp_obj_t o);
pythontech 0:5868e8752d44 123 mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration()
pythontech 0:5868e8752d44 124 mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...)
pythontech 0:5868e8752d44 125 mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val);
pythontech 0:5868e8752d44 126
pythontech 0:5868e8752d44 127 mp_obj_t mp_make_raise_obj(mp_obj_t o);
pythontech 0:5868e8752d44 128
pythontech 0:5868e8752d44 129 mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
pythontech 0:5868e8752d44 130 mp_obj_t mp_import_from(mp_obj_t module, qstr name);
pythontech 0:5868e8752d44 131 void mp_import_all(mp_obj_t module);
pythontech 0:5868e8752d44 132
pythontech 0:5868e8752d44 133 // Raise NotImplementedError with given message
pythontech 0:5868e8752d44 134 NORETURN void mp_not_implemented(const char *msg);
pythontech 0:5868e8752d44 135 NORETURN void mp_exc_recursion_depth(void);
pythontech 0:5868e8752d44 136
pythontech 0:5868e8752d44 137 // helper functions for native/viper code
pythontech 0:5868e8752d44 138 mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type);
pythontech 0:5868e8752d44 139 mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type);
pythontech 0:5868e8752d44 140 mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args_kw, const mp_obj_t *args);
pythontech 0:5868e8752d44 141 void mp_native_raise(mp_obj_t o);
pythontech 0:5868e8752d44 142
pythontech 0:5868e8752d44 143 #define mp_sys_path (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_path_obj)))
pythontech 0:5868e8752d44 144 #define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)))
pythontech 0:5868e8752d44 145
pythontech 0:5868e8752d44 146 #if MICROPY_WARNINGS
pythontech 0:5868e8752d44 147 void mp_warning(const char *msg, ...);
pythontech 0:5868e8752d44 148 #else
pythontech 0:5868e8752d44 149 #define mp_warning(msg, ...)
pythontech 0:5868e8752d44 150 #endif
pythontech 0:5868e8752d44 151
pythontech 0:5868e8752d44 152 #endif // __MICROPY_INCLUDED_PY_RUNTIME_H__