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 <stdio.h>
pythontech 0:5868e8752d44 28 #include <stdlib.h>
pythontech 0:5868e8752d44 29 #include <string.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/mpstate.h"
pythontech 0:5868e8752d44 34
pythontech 0:5868e8752d44 35 #if 0 // print debugging info
pythontech 0:5868e8752d44 36 #define DEBUG_printf DEBUG_printf
pythontech 0:5868e8752d44 37 #else // don't print debugging info
pythontech 0:5868e8752d44 38 #define DEBUG_printf(...) (void)0
pythontech 0:5868e8752d44 39 #endif
pythontech 0:5868e8752d44 40
pythontech 0:5868e8752d44 41 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 42 #define UPDATE_PEAK() { if (MP_STATE_MEM(current_bytes_allocated) > MP_STATE_MEM(peak_bytes_allocated)) MP_STATE_MEM(peak_bytes_allocated) = MP_STATE_MEM(current_bytes_allocated); }
pythontech 0:5868e8752d44 43 #endif
pythontech 0:5868e8752d44 44
pythontech 0:5868e8752d44 45 #if MICROPY_ENABLE_GC
pythontech 0:5868e8752d44 46 #include "py/gc.h"
pythontech 0:5868e8752d44 47
pythontech 0:5868e8752d44 48 // We redirect standard alloc functions to GC heap - just for the rest of
pythontech 0:5868e8752d44 49 // this module. In the rest of micropython source, system malloc can be
pythontech 0:5868e8752d44 50 // freely accessed - for interfacing with system and 3rd-party libs for
pythontech 0:5868e8752d44 51 // example. On the other hand, some (e.g. bare-metal) ports may use GC
pythontech 0:5868e8752d44 52 // heap as system heap, so, to avoid warnings, we do undef's first.
pythontech 0:5868e8752d44 53 #undef malloc
pythontech 0:5868e8752d44 54 #undef free
pythontech 0:5868e8752d44 55 #undef realloc
pythontech 0:5868e8752d44 56 #define malloc(b) gc_alloc((b), false)
pythontech 0:5868e8752d44 57 #define malloc_with_finaliser(b) gc_alloc((b), true)
pythontech 0:5868e8752d44 58 #define free gc_free
pythontech 0:5868e8752d44 59 #define realloc(ptr, n) gc_realloc(ptr, n, true)
pythontech 0:5868e8752d44 60 #define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv)
pythontech 0:5868e8752d44 61 #else
pythontech 0:5868e8752d44 62 STATIC void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
pythontech 0:5868e8752d44 63 if (allow_move) {
pythontech 0:5868e8752d44 64 return realloc(ptr, n_bytes);
pythontech 0:5868e8752d44 65 } else {
pythontech 0:5868e8752d44 66 // We are asked to resize, but without moving the memory region pointed to
pythontech 0:5868e8752d44 67 // by ptr. Unless the underlying memory manager has special provision for
pythontech 0:5868e8752d44 68 // this behaviour there is nothing we can do except fail to resize.
pythontech 0:5868e8752d44 69 return NULL;
pythontech 0:5868e8752d44 70 }
pythontech 0:5868e8752d44 71 }
pythontech 0:5868e8752d44 72 #endif // MICROPY_ENABLE_GC
pythontech 0:5868e8752d44 73
pythontech 0:5868e8752d44 74 void *m_malloc(size_t num_bytes) {
pythontech 0:5868e8752d44 75 void *ptr = malloc(num_bytes);
pythontech 0:5868e8752d44 76 if (ptr == NULL && num_bytes != 0) {
pythontech 0:5868e8752d44 77 return m_malloc_fail(num_bytes);
pythontech 0:5868e8752d44 78 }
pythontech 0:5868e8752d44 79 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 80 MP_STATE_MEM(total_bytes_allocated) += num_bytes;
pythontech 0:5868e8752d44 81 MP_STATE_MEM(current_bytes_allocated) += num_bytes;
pythontech 0:5868e8752d44 82 UPDATE_PEAK();
pythontech 0:5868e8752d44 83 #endif
pythontech 0:5868e8752d44 84 DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
pythontech 0:5868e8752d44 85 return ptr;
pythontech 0:5868e8752d44 86 }
pythontech 0:5868e8752d44 87
pythontech 0:5868e8752d44 88 void *m_malloc_maybe(size_t num_bytes) {
pythontech 0:5868e8752d44 89 void *ptr = malloc(num_bytes);
pythontech 0:5868e8752d44 90 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 91 MP_STATE_MEM(total_bytes_allocated) += num_bytes;
pythontech 0:5868e8752d44 92 MP_STATE_MEM(current_bytes_allocated) += num_bytes;
pythontech 0:5868e8752d44 93 UPDATE_PEAK();
pythontech 0:5868e8752d44 94 #endif
pythontech 0:5868e8752d44 95 DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
pythontech 0:5868e8752d44 96 return ptr;
pythontech 0:5868e8752d44 97 }
pythontech 0:5868e8752d44 98
pythontech 0:5868e8752d44 99 #if MICROPY_ENABLE_FINALISER
pythontech 0:5868e8752d44 100 void *m_malloc_with_finaliser(size_t num_bytes) {
pythontech 0:5868e8752d44 101 void *ptr = malloc_with_finaliser(num_bytes);
pythontech 0:5868e8752d44 102 if (ptr == NULL && num_bytes != 0) {
pythontech 0:5868e8752d44 103 return m_malloc_fail(num_bytes);
pythontech 0:5868e8752d44 104 }
pythontech 0:5868e8752d44 105 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 106 MP_STATE_MEM(total_bytes_allocated) += num_bytes;
pythontech 0:5868e8752d44 107 MP_STATE_MEM(current_bytes_allocated) += num_bytes;
pythontech 0:5868e8752d44 108 UPDATE_PEAK();
pythontech 0:5868e8752d44 109 #endif
pythontech 0:5868e8752d44 110 DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
pythontech 0:5868e8752d44 111 return ptr;
pythontech 0:5868e8752d44 112 }
pythontech 0:5868e8752d44 113 #endif
pythontech 0:5868e8752d44 114
pythontech 0:5868e8752d44 115 void *m_malloc0(size_t num_bytes) {
pythontech 0:5868e8752d44 116 void *ptr = m_malloc(num_bytes);
pythontech 0:5868e8752d44 117 if (ptr == NULL && num_bytes != 0) {
pythontech 0:5868e8752d44 118 return m_malloc_fail(num_bytes);
pythontech 0:5868e8752d44 119 }
pythontech 0:5868e8752d44 120 memset(ptr, 0, num_bytes);
pythontech 0:5868e8752d44 121 return ptr;
pythontech 0:5868e8752d44 122 }
pythontech 0:5868e8752d44 123
pythontech 0:5868e8752d44 124 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
pythontech 0:5868e8752d44 125 void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
pythontech 0:5868e8752d44 126 #else
pythontech 0:5868e8752d44 127 void *m_realloc(void *ptr, size_t new_num_bytes) {
pythontech 0:5868e8752d44 128 #endif
pythontech 0:5868e8752d44 129 void *new_ptr = realloc(ptr, new_num_bytes);
pythontech 0:5868e8752d44 130 if (new_ptr == NULL && new_num_bytes != 0) {
pythontech 0:5868e8752d44 131 return m_malloc_fail(new_num_bytes);
pythontech 0:5868e8752d44 132 }
pythontech 0:5868e8752d44 133 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 134 // At first thought, "Total bytes allocated" should only grow,
pythontech 0:5868e8752d44 135 // after all, it's *total*. But consider for example 2K block
pythontech 0:5868e8752d44 136 // shrunk to 1K and then grown to 2K again. It's still 2K
pythontech 0:5868e8752d44 137 // allocated total. If we process only positive increments,
pythontech 0:5868e8752d44 138 // we'll count 3K.
pythontech 0:5868e8752d44 139 size_t diff = new_num_bytes - old_num_bytes;
pythontech 0:5868e8752d44 140 MP_STATE_MEM(total_bytes_allocated) += diff;
pythontech 0:5868e8752d44 141 MP_STATE_MEM(current_bytes_allocated) += diff;
pythontech 0:5868e8752d44 142 UPDATE_PEAK();
pythontech 0:5868e8752d44 143 #endif
pythontech 0:5868e8752d44 144 DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
pythontech 0:5868e8752d44 145 return new_ptr;
pythontech 0:5868e8752d44 146 }
pythontech 0:5868e8752d44 147
pythontech 0:5868e8752d44 148 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
pythontech 0:5868e8752d44 149 void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move) {
pythontech 0:5868e8752d44 150 #else
pythontech 0:5868e8752d44 151 void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move) {
pythontech 0:5868e8752d44 152 #endif
pythontech 0:5868e8752d44 153 void *new_ptr = realloc_ext(ptr, new_num_bytes, allow_move);
pythontech 0:5868e8752d44 154 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 155 // At first thought, "Total bytes allocated" should only grow,
pythontech 0:5868e8752d44 156 // after all, it's *total*. But consider for example 2K block
pythontech 0:5868e8752d44 157 // shrunk to 1K and then grown to 2K again. It's still 2K
pythontech 0:5868e8752d44 158 // allocated total. If we process only positive increments,
pythontech 0:5868e8752d44 159 // we'll count 3K.
pythontech 0:5868e8752d44 160 // Also, don't count failed reallocs.
pythontech 0:5868e8752d44 161 if (!(new_ptr == NULL && new_num_bytes != 0)) {
pythontech 0:5868e8752d44 162 size_t diff = new_num_bytes - old_num_bytes;
pythontech 0:5868e8752d44 163 MP_STATE_MEM(total_bytes_allocated) += diff;
pythontech 0:5868e8752d44 164 MP_STATE_MEM(current_bytes_allocated) += diff;
pythontech 0:5868e8752d44 165 UPDATE_PEAK();
pythontech 0:5868e8752d44 166 }
pythontech 0:5868e8752d44 167 #endif
pythontech 0:5868e8752d44 168 DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
pythontech 0:5868e8752d44 169 return new_ptr;
pythontech 0:5868e8752d44 170 }
pythontech 0:5868e8752d44 171
pythontech 0:5868e8752d44 172 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
pythontech 0:5868e8752d44 173 void m_free(void *ptr, size_t num_bytes) {
pythontech 0:5868e8752d44 174 #else
pythontech 0:5868e8752d44 175 void m_free(void *ptr) {
pythontech 0:5868e8752d44 176 #endif
pythontech 0:5868e8752d44 177 free(ptr);
pythontech 0:5868e8752d44 178 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 179 MP_STATE_MEM(current_bytes_allocated) -= num_bytes;
pythontech 0:5868e8752d44 180 #endif
pythontech 0:5868e8752d44 181 DEBUG_printf("free %p, %d\n", ptr, num_bytes);
pythontech 0:5868e8752d44 182 }
pythontech 0:5868e8752d44 183
pythontech 0:5868e8752d44 184 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 185 size_t m_get_total_bytes_allocated(void) {
pythontech 0:5868e8752d44 186 return MP_STATE_MEM(total_bytes_allocated);
pythontech 0:5868e8752d44 187 }
pythontech 0:5868e8752d44 188
pythontech 0:5868e8752d44 189 size_t m_get_current_bytes_allocated(void) {
pythontech 0:5868e8752d44 190 return MP_STATE_MEM(current_bytes_allocated);
pythontech 0:5868e8752d44 191 }
pythontech 0:5868e8752d44 192
pythontech 0:5868e8752d44 193 size_t m_get_peak_bytes_allocated(void) {
pythontech 0:5868e8752d44 194 return MP_STATE_MEM(peak_bytes_allocated);
pythontech 0:5868e8752d44 195 }
pythontech 0:5868e8752d44 196 #endif