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:
Tue Apr 26 22:50:06 2016 +0100
Revision:
7:379d46fd02c2
Add help, DigitalIn etc.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Colin Hogben 7:379d46fd02c2 1 /*
Colin Hogben 7:379d46fd02c2 2 * This file is part of the Micro Python project, http://micropython.org/
Colin Hogben 7:379d46fd02c2 3 *
Colin Hogben 7:379d46fd02c2 4 * The MIT License (MIT)
Colin Hogben 7:379d46fd02c2 5 *
Colin Hogben 7:379d46fd02c2 6 * Copyright (c) 2013, 2014 Damien P. George
Colin Hogben 7:379d46fd02c2 7 *
Colin Hogben 7:379d46fd02c2 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
Colin Hogben 7:379d46fd02c2 9 * of this software and associated documentation files (the "Software"), to deal
Colin Hogben 7:379d46fd02c2 10 * in the Software without restriction, including without limitation the rights
Colin Hogben 7:379d46fd02c2 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Colin Hogben 7:379d46fd02c2 12 * copies of the Software, and to permit persons to whom the Software is
Colin Hogben 7:379d46fd02c2 13 * furnished to do so, subject to the following conditions:
Colin Hogben 7:379d46fd02c2 14 *
Colin Hogben 7:379d46fd02c2 15 * The above copyright notice and this permission notice shall be included in
Colin Hogben 7:379d46fd02c2 16 * all copies or substantial portions of the Software.
Colin Hogben 7:379d46fd02c2 17 *
Colin Hogben 7:379d46fd02c2 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Colin Hogben 7:379d46fd02c2 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Colin Hogben 7:379d46fd02c2 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Colin Hogben 7:379d46fd02c2 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Colin Hogben 7:379d46fd02c2 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Colin Hogben 7:379d46fd02c2 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Colin Hogben 7:379d46fd02c2 24 * THE SOFTWARE.
Colin Hogben 7:379d46fd02c2 25 */
Colin Hogben 7:379d46fd02c2 26
Colin Hogben 7:379d46fd02c2 27 #include <stdint.h>
Colin Hogben 7:379d46fd02c2 28 #include <string.h>
Colin Hogben 7:379d46fd02c2 29 #include <stdarg.h>
Colin Hogben 7:379d46fd02c2 30
Colin Hogben 7:379d46fd02c2 31 #include "py/obj.h"
Colin Hogben 7:379d46fd02c2 32 #include "py/mphal.h"
Colin Hogben 7:379d46fd02c2 33
Colin Hogben 7:379d46fd02c2 34 #if MICROPY_PY_BUILTINS_FLOAT
Colin Hogben 7:379d46fd02c2 35 #include "py/formatfloat.h"
Colin Hogben 7:379d46fd02c2 36 #endif
Colin Hogben 7:379d46fd02c2 37
Colin Hogben 7:379d46fd02c2 38 #undef putchar // Some stdlibs have a #define for putchar
Colin Hogben 7:379d46fd02c2 39 int printf(const char *fmt, ...);
Colin Hogben 7:379d46fd02c2 40 int vprintf(const char *fmt, va_list ap);
Colin Hogben 7:379d46fd02c2 41 int putchar(int c);
Colin Hogben 7:379d46fd02c2 42 int puts(const char *s);
Colin Hogben 7:379d46fd02c2 43 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
Colin Hogben 7:379d46fd02c2 44 int snprintf(char *str, size_t size, const char *fmt, ...);
Colin Hogben 7:379d46fd02c2 45
Colin Hogben 7:379d46fd02c2 46 int printf(const char *fmt, ...) {
Colin Hogben 7:379d46fd02c2 47 va_list ap;
Colin Hogben 7:379d46fd02c2 48 va_start(ap, fmt);
Colin Hogben 7:379d46fd02c2 49 int ret = mp_vprintf(&mp_plat_print, fmt, ap);
Colin Hogben 7:379d46fd02c2 50 va_end(ap);
Colin Hogben 7:379d46fd02c2 51 return ret;
Colin Hogben 7:379d46fd02c2 52 }
Colin Hogben 7:379d46fd02c2 53
Colin Hogben 7:379d46fd02c2 54 int vprintf(const char *fmt, va_list ap) {
Colin Hogben 7:379d46fd02c2 55 return mp_vprintf(&mp_plat_print, fmt, ap);
Colin Hogben 7:379d46fd02c2 56 }
Colin Hogben 7:379d46fd02c2 57
Colin Hogben 7:379d46fd02c2 58 #if MICROPY_DEBUG_PRINTERS
Colin Hogben 7:379d46fd02c2 59 int DEBUG_printf(const char *fmt, ...) {
Colin Hogben 7:379d46fd02c2 60 va_list ap;
Colin Hogben 7:379d46fd02c2 61 va_start(ap, fmt);
Colin Hogben 7:379d46fd02c2 62 #ifndef MICROPY_DEBUG_PRINTER_DEST
Colin Hogben 7:379d46fd02c2 63 #define MICROPY_DEBUG_PRINTER_DEST mp_plat_print
Colin Hogben 7:379d46fd02c2 64 #endif
Colin Hogben 7:379d46fd02c2 65 extern const mp_print_t MICROPY_DEBUG_PRINTER_DEST;
Colin Hogben 7:379d46fd02c2 66 int ret = mp_vprintf(&MICROPY_DEBUG_PRINTER_DEST, fmt, ap);
Colin Hogben 7:379d46fd02c2 67 va_end(ap);
Colin Hogben 7:379d46fd02c2 68 return ret;
Colin Hogben 7:379d46fd02c2 69 }
Colin Hogben 7:379d46fd02c2 70 #endif
Colin Hogben 7:379d46fd02c2 71
Colin Hogben 7:379d46fd02c2 72 // need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a')
Colin Hogben 7:379d46fd02c2 73 int putchar(int c) {
Colin Hogben 7:379d46fd02c2 74 char chr = c;
Colin Hogben 7:379d46fd02c2 75 mp_hal_stdout_tx_strn_cooked(&chr, 1);
Colin Hogben 7:379d46fd02c2 76 return chr;
Colin Hogben 7:379d46fd02c2 77 }
Colin Hogben 7:379d46fd02c2 78
Colin Hogben 7:379d46fd02c2 79 // need this because gcc optimises printf("string\n") -> puts("string")
Colin Hogben 7:379d46fd02c2 80 int puts(const char *s) {
Colin Hogben 7:379d46fd02c2 81 mp_hal_stdout_tx_strn_cooked(s, strlen(s));
Colin Hogben 7:379d46fd02c2 82 char chr = '\n';
Colin Hogben 7:379d46fd02c2 83 mp_hal_stdout_tx_strn_cooked(&chr, 1);
Colin Hogben 7:379d46fd02c2 84 return 1;
Colin Hogben 7:379d46fd02c2 85 }
Colin Hogben 7:379d46fd02c2 86
Colin Hogben 7:379d46fd02c2 87 typedef struct _strn_print_env_t {
Colin Hogben 7:379d46fd02c2 88 char *cur;
Colin Hogben 7:379d46fd02c2 89 size_t remain;
Colin Hogben 7:379d46fd02c2 90 } strn_print_env_t;
Colin Hogben 7:379d46fd02c2 91
Colin Hogben 7:379d46fd02c2 92 STATIC void strn_print_strn(void *data, const char *str, size_t len) {
Colin Hogben 7:379d46fd02c2 93 strn_print_env_t *strn_print_env = data;
Colin Hogben 7:379d46fd02c2 94 if (len > strn_print_env->remain) {
Colin Hogben 7:379d46fd02c2 95 len = strn_print_env->remain;
Colin Hogben 7:379d46fd02c2 96 }
Colin Hogben 7:379d46fd02c2 97 memcpy(strn_print_env->cur, str, len);
Colin Hogben 7:379d46fd02c2 98 strn_print_env->cur += len;
Colin Hogben 7:379d46fd02c2 99 strn_print_env->remain -= len;
Colin Hogben 7:379d46fd02c2 100 }
Colin Hogben 7:379d46fd02c2 101
Colin Hogben 7:379d46fd02c2 102 #if defined(__GNUC__) && !defined(__clang__)
Colin Hogben 7:379d46fd02c2 103 // uClibc requires this alias to be defined, or there may be link errors
Colin Hogben 7:379d46fd02c2 104 // when linkings against it statically.
Colin Hogben 7:379d46fd02c2 105 int __GI_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) __attribute__((weak, alias ("vsnprintf")));
Colin Hogben 7:379d46fd02c2 106 #endif
Colin Hogben 7:379d46fd02c2 107
Colin Hogben 7:379d46fd02c2 108 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
Colin Hogben 7:379d46fd02c2 109 strn_print_env_t strn_print_env = {str, size};
Colin Hogben 7:379d46fd02c2 110 mp_print_t print = {&strn_print_env, strn_print_strn};
Colin Hogben 7:379d46fd02c2 111 int len = mp_vprintf(&print, fmt, ap);
Colin Hogben 7:379d46fd02c2 112 // add terminating null byte
Colin Hogben 7:379d46fd02c2 113 if (size > 0) {
Colin Hogben 7:379d46fd02c2 114 if (strn_print_env.remain == 0) {
Colin Hogben 7:379d46fd02c2 115 strn_print_env.cur[-1] = 0;
Colin Hogben 7:379d46fd02c2 116 } else {
Colin Hogben 7:379d46fd02c2 117 strn_print_env.cur[0] = 0;
Colin Hogben 7:379d46fd02c2 118 }
Colin Hogben 7:379d46fd02c2 119 }
Colin Hogben 7:379d46fd02c2 120 return len;
Colin Hogben 7:379d46fd02c2 121 }
Colin Hogben 7:379d46fd02c2 122
Colin Hogben 7:379d46fd02c2 123 int snprintf(char *str, size_t size, const char *fmt, ...) {
Colin Hogben 7:379d46fd02c2 124 va_list ap;
Colin Hogben 7:379d46fd02c2 125 va_start(ap, fmt);
Colin Hogben 7:379d46fd02c2 126 int ret = vsnprintf(str, size, fmt, ap);
Colin Hogben 7:379d46fd02c2 127 va_end(ap);
Colin Hogben 7:379d46fd02c2 128 return ret;
Colin Hogben 7:379d46fd02c2 129 }