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) 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_MPSTATE_H__
pythontech 0:5868e8752d44 27 #define __MICROPY_INCLUDED_PY_MPSTATE_H__
pythontech 0:5868e8752d44 28
pythontech 0:5868e8752d44 29 #include <stdint.h>
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 #include "py/mpconfig.h"
pythontech 0:5868e8752d44 32 #include "py/misc.h"
pythontech 0:5868e8752d44 33 #include "py/nlr.h"
pythontech 0:5868e8752d44 34 #include "py/obj.h"
pythontech 0:5868e8752d44 35 #include "py/objlist.h"
pythontech 0:5868e8752d44 36 #include "py/objexcept.h"
pythontech 0:5868e8752d44 37
pythontech 0:5868e8752d44 38 // This file contains structures defining the state of the Micro Python
pythontech 0:5868e8752d44 39 // memory system, runtime and virtual machine. The state is a global
pythontech 0:5868e8752d44 40 // variable, but in the future it is hoped that the state can become local.
pythontech 0:5868e8752d44 41
pythontech 0:5868e8752d44 42 // This structure contains dynamic configuration for the compiler.
pythontech 0:5868e8752d44 43 #if MICROPY_DYNAMIC_COMPILER
pythontech 0:5868e8752d44 44 typedef struct mp_dynamic_compiler_t {
pythontech 0:5868e8752d44 45 uint8_t small_int_bits; // must be <= host small_int_bits
pythontech 0:5868e8752d44 46 bool opt_cache_map_lookup_in_bytecode;
pythontech 0:5868e8752d44 47 bool py_builtins_str_unicode;
pythontech 0:5868e8752d44 48 } mp_dynamic_compiler_t;
pythontech 0:5868e8752d44 49 extern mp_dynamic_compiler_t mp_dynamic_compiler;
pythontech 0:5868e8752d44 50 #endif
pythontech 0:5868e8752d44 51
pythontech 0:5868e8752d44 52 // This structure hold information about the memory allocation system.
pythontech 0:5868e8752d44 53 typedef struct _mp_state_mem_t {
pythontech 0:5868e8752d44 54 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 55 size_t total_bytes_allocated;
pythontech 0:5868e8752d44 56 size_t current_bytes_allocated;
pythontech 0:5868e8752d44 57 size_t peak_bytes_allocated;
pythontech 0:5868e8752d44 58 #endif
pythontech 0:5868e8752d44 59
pythontech 0:5868e8752d44 60 byte *gc_alloc_table_start;
pythontech 0:5868e8752d44 61 size_t gc_alloc_table_byte_len;
pythontech 0:5868e8752d44 62 #if MICROPY_ENABLE_FINALISER
pythontech 0:5868e8752d44 63 byte *gc_finaliser_table_start;
pythontech 0:5868e8752d44 64 #endif
pythontech 0:5868e8752d44 65 byte *gc_pool_start;
pythontech 0:5868e8752d44 66 byte *gc_pool_end;
pythontech 0:5868e8752d44 67
pythontech 0:5868e8752d44 68 int gc_stack_overflow;
pythontech 0:5868e8752d44 69 size_t gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
pythontech 0:5868e8752d44 70 size_t *gc_sp;
pythontech 0:5868e8752d44 71 uint16_t gc_lock_depth;
pythontech 0:5868e8752d44 72
pythontech 0:5868e8752d44 73 // This variable controls auto garbage collection. If set to 0 then the
pythontech 0:5868e8752d44 74 // GC won't automatically run when gc_alloc can't find enough blocks. But
pythontech 0:5868e8752d44 75 // you can still allocate/free memory and also explicitly call gc_collect.
pythontech 0:5868e8752d44 76 uint16_t gc_auto_collect_enabled;
pythontech 0:5868e8752d44 77
pythontech 0:5868e8752d44 78 size_t gc_last_free_atb_index;
pythontech 0:5868e8752d44 79
pythontech 0:5868e8752d44 80 #if MICROPY_PY_GC_COLLECT_RETVAL
pythontech 0:5868e8752d44 81 size_t gc_collected;
pythontech 0:5868e8752d44 82 #endif
pythontech 0:5868e8752d44 83 } mp_state_mem_t;
pythontech 0:5868e8752d44 84
pythontech 0:5868e8752d44 85 // This structure hold runtime and VM information. It includes a section
pythontech 0:5868e8752d44 86 // which contains root pointers that must be scanned by the GC.
pythontech 0:5868e8752d44 87 typedef struct _mp_state_vm_t {
pythontech 0:5868e8752d44 88 ////////////////////////////////////////////////////////////
pythontech 0:5868e8752d44 89 // START ROOT POINTER SECTION
pythontech 0:5868e8752d44 90 // everything that needs GC scanning must go here
pythontech 0:5868e8752d44 91 // this must start at the start of this structure
pythontech 0:5868e8752d44 92 //
pythontech 0:5868e8752d44 93
pythontech 0:5868e8752d44 94 // Note: nlr asm code has the offset of this hard-coded
pythontech 0:5868e8752d44 95 nlr_buf_t *nlr_top;
pythontech 0:5868e8752d44 96
pythontech 0:5868e8752d44 97 qstr_pool_t *last_pool;
pythontech 0:5868e8752d44 98
pythontech 0:5868e8752d44 99 // non-heap memory for creating an exception if we can't allocate RAM
pythontech 0:5868e8752d44 100 mp_obj_exception_t mp_emergency_exception_obj;
pythontech 0:5868e8752d44 101
pythontech 0:5868e8752d44 102 // memory for exception arguments if we can't allocate RAM
pythontech 0:5868e8752d44 103 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
pythontech 0:5868e8752d44 104 #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
pythontech 0:5868e8752d44 105 // statically allocated buf
pythontech 0:5868e8752d44 106 byte mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE];
pythontech 0:5868e8752d44 107 #else
pythontech 0:5868e8752d44 108 // dynamically allocated buf
pythontech 0:5868e8752d44 109 byte *mp_emergency_exception_buf;
pythontech 0:5868e8752d44 110 #endif
pythontech 0:5868e8752d44 111 #endif
pythontech 0:5868e8752d44 112
pythontech 0:5868e8752d44 113 // dictionary with loaded modules (may be exposed as sys.modules)
pythontech 0:5868e8752d44 114 mp_obj_dict_t mp_loaded_modules_dict;
pythontech 0:5868e8752d44 115
pythontech 0:5868e8752d44 116 // pending exception object (MP_OBJ_NULL if not pending)
pythontech 0:5868e8752d44 117 volatile mp_obj_t mp_pending_exception;
pythontech 0:5868e8752d44 118
pythontech 0:5868e8752d44 119 // current exception being handled, for sys.exc_info()
pythontech 0:5868e8752d44 120 #if MICROPY_PY_SYS_EXC_INFO
pythontech 0:5868e8752d44 121 mp_obj_base_t *cur_exception;
pythontech 0:5868e8752d44 122 #endif
pythontech 0:5868e8752d44 123
pythontech 0:5868e8752d44 124 // dictionary for the __main__ module
pythontech 0:5868e8752d44 125 mp_obj_dict_t dict_main;
pythontech 0:5868e8752d44 126
pythontech 0:5868e8752d44 127 // these two lists must be initialised per port, after the call to mp_init
pythontech 0:5868e8752d44 128 mp_obj_list_t mp_sys_path_obj;
pythontech 0:5868e8752d44 129 mp_obj_list_t mp_sys_argv_obj;
pythontech 0:5868e8752d44 130
pythontech 0:5868e8752d44 131 // dictionary for overridden builtins
pythontech 0:5868e8752d44 132 #if MICROPY_CAN_OVERRIDE_BUILTINS
pythontech 0:5868e8752d44 133 mp_obj_dict_t *mp_module_builtins_override_dict;
pythontech 0:5868e8752d44 134 #endif
pythontech 0:5868e8752d44 135
pythontech 0:5868e8752d44 136 // include any root pointers defined by a port
pythontech 0:5868e8752d44 137 MICROPY_PORT_ROOT_POINTERS
pythontech 0:5868e8752d44 138
pythontech 0:5868e8752d44 139 // root pointers for extmod
pythontech 0:5868e8752d44 140
pythontech 0:5868e8752d44 141 #if MICROPY_PY_OS_DUPTERM
pythontech 0:5868e8752d44 142 mp_obj_t term_obj;
pythontech 0:5868e8752d44 143 #endif
pythontech 0:5868e8752d44 144
pythontech 0:5868e8752d44 145 #if MICROPY_PY_LWIP_SLIP
pythontech 0:5868e8752d44 146 mp_obj_t lwip_slip_stream;
pythontech 0:5868e8752d44 147 #endif
pythontech 0:5868e8752d44 148
pythontech 0:5868e8752d44 149 #if MICROPY_FSUSERMOUNT
pythontech 0:5868e8752d44 150 // for user-mountable block device (max fixed at compile time)
pythontech 0:5868e8752d44 151 struct _fs_user_mount_t *fs_user_mount[MICROPY_FATFS_VOLUMES];
pythontech 0:5868e8752d44 152 #endif
pythontech 0:5868e8752d44 153
pythontech 0:5868e8752d44 154 //
pythontech 0:5868e8752d44 155 // END ROOT POINTER SECTION
pythontech 0:5868e8752d44 156 ////////////////////////////////////////////////////////////
pythontech 0:5868e8752d44 157
pythontech 0:5868e8752d44 158 // pointer and sizes to store interned string data
pythontech 0:5868e8752d44 159 // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
pythontech 0:5868e8752d44 160 byte *qstr_last_chunk;
pythontech 0:5868e8752d44 161 size_t qstr_last_alloc;
pythontech 0:5868e8752d44 162 size_t qstr_last_used;
pythontech 0:5868e8752d44 163
pythontech 0:5868e8752d44 164 // Stack top at the start of program
pythontech 0:5868e8752d44 165 // Note: this entry is used to locate the end of the root pointer section.
pythontech 0:5868e8752d44 166 char *stack_top;
pythontech 0:5868e8752d44 167
pythontech 0:5868e8752d44 168 #if MICROPY_STACK_CHECK
pythontech 0:5868e8752d44 169 mp_uint_t stack_limit;
pythontech 0:5868e8752d44 170 #endif
pythontech 0:5868e8752d44 171
pythontech 0:5868e8752d44 172 mp_uint_t mp_optimise_value;
pythontech 0:5868e8752d44 173
pythontech 0:5868e8752d44 174 // size of the emergency exception buf, if it's dynamically allocated
pythontech 0:5868e8752d44 175 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
pythontech 0:5868e8752d44 176 mp_int_t mp_emergency_exception_buf_size;
pythontech 0:5868e8752d44 177 #endif
pythontech 0:5868e8752d44 178 } mp_state_vm_t;
pythontech 0:5868e8752d44 179
pythontech 0:5868e8752d44 180 // This structure combines the above 2 structures, and adds the local
pythontech 0:5868e8752d44 181 // and global dicts.
pythontech 0:5868e8752d44 182 // Note: if this structure changes then revisit all nlr asm code since they
pythontech 0:5868e8752d44 183 // have the offset of nlr_top hard-coded.
pythontech 0:5868e8752d44 184 typedef struct _mp_state_ctx_t {
pythontech 0:5868e8752d44 185 // these must come first for root pointer scanning in GC to work
pythontech 0:5868e8752d44 186 mp_obj_dict_t *dict_locals;
pythontech 0:5868e8752d44 187 mp_obj_dict_t *dict_globals;
pythontech 0:5868e8752d44 188 // this must come next for root pointer scanning in GC to work
pythontech 0:5868e8752d44 189 mp_state_vm_t vm;
pythontech 0:5868e8752d44 190 mp_state_mem_t mem;
pythontech 0:5868e8752d44 191 } mp_state_ctx_t;
pythontech 0:5868e8752d44 192
pythontech 0:5868e8752d44 193 extern mp_state_ctx_t mp_state_ctx;
pythontech 0:5868e8752d44 194
pythontech 0:5868e8752d44 195 #define MP_STATE_CTX(x) (mp_state_ctx.x)
pythontech 0:5868e8752d44 196 #define MP_STATE_VM(x) (mp_state_ctx.vm.x)
pythontech 0:5868e8752d44 197 #define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
pythontech 0:5868e8752d44 198
pythontech 0:5868e8752d44 199 #endif // __MICROPY_INCLUDED_PY_MPSTATE_H__