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 MicroPython 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-2016 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 <stdio.h>
Colin Hogben 7:379d46fd02c2 28
Colin Hogben 7:379d46fd02c2 29 #include "lib/utils/pyhelp.h"
Colin Hogben 7:379d46fd02c2 30
Colin Hogben 7:379d46fd02c2 31 STATIC void pyhelp_print_info_about_object(mp_obj_t name_o, mp_obj_t value) {
Colin Hogben 7:379d46fd02c2 32 printf(" ");
Colin Hogben 7:379d46fd02c2 33 mp_obj_print(name_o, PRINT_STR);
Colin Hogben 7:379d46fd02c2 34 printf(" -- ");
Colin Hogben 7:379d46fd02c2 35 mp_obj_print(value, PRINT_STR);
Colin Hogben 7:379d46fd02c2 36 printf("\n");
Colin Hogben 7:379d46fd02c2 37 }
Colin Hogben 7:379d46fd02c2 38
Colin Hogben 7:379d46fd02c2 39 // Helper for 1-argument form of builtin help
Colin Hogben 7:379d46fd02c2 40 //
Colin Hogben 7:379d46fd02c2 41 // Typically a port will define a help function thus:
Colin Hogben 7:379d46fd02c2 42 //
Colin Hogben 7:379d46fd02c2 43 // STATIC const char *const myport_help_text =
Colin Hogben 7:379d46fd02c2 44 // "Welcome to MicroPython!\n"
Colin Hogben 7:379d46fd02c2 45 // "\n"
Colin Hogben 7:379d46fd02c2 46 // "...information specific to this port e.g. modules available...";
Colin Hogben 7:379d46fd02c2 47 //
Colin Hogben 7:379d46fd02c2 48 // STATIC mp_obj_t myport_help(mp_uint_t n_args, const mp_obj_t *args) {
Colin Hogben 7:379d46fd02c2 49 // if (n_args == 0) {
Colin Hogben 7:379d46fd02c2 50 // printf("%s", myport_help_text);
Colin Hogben 7:379d46fd02c2 51 // } else {
Colin Hogben 7:379d46fd02c2 52 // pyhelp_print_obj(args[0]);
Colin Hogben 7:379d46fd02c2 53 // }
Colin Hogben 7:379d46fd02c2 54 // return mp_const_none;
Colin Hogben 7:379d46fd02c2 55 // }
Colin Hogben 7:379d46fd02c2 56 // MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, myport_help);
Colin Hogben 7:379d46fd02c2 57 //
Colin Hogben 7:379d46fd02c2 58 void pyhelp_print_obj(const mp_obj_t obj) {
Colin Hogben 7:379d46fd02c2 59 // try to print something sensible about the given object
Colin Hogben 7:379d46fd02c2 60 printf("object ");
Colin Hogben 7:379d46fd02c2 61 mp_obj_print(obj, PRINT_STR);
Colin Hogben 7:379d46fd02c2 62 printf(" is of type %s\n", mp_obj_get_type_str(obj));
Colin Hogben 7:379d46fd02c2 63
Colin Hogben 7:379d46fd02c2 64 mp_map_t *map = NULL;
Colin Hogben 7:379d46fd02c2 65 if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) {
Colin Hogben 7:379d46fd02c2 66 map = mp_obj_dict_get_map(mp_obj_module_get_globals(obj));
Colin Hogben 7:379d46fd02c2 67 } else {
Colin Hogben 7:379d46fd02c2 68 mp_obj_type_t *type;
Colin Hogben 7:379d46fd02c2 69 if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) {
Colin Hogben 7:379d46fd02c2 70 type = obj;
Colin Hogben 7:379d46fd02c2 71 } else {
Colin Hogben 7:379d46fd02c2 72 type = mp_obj_get_type(obj);
Colin Hogben 7:379d46fd02c2 73 }
Colin Hogben 7:379d46fd02c2 74 if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
Colin Hogben 7:379d46fd02c2 75 map = mp_obj_dict_get_map(type->locals_dict);
Colin Hogben 7:379d46fd02c2 76 }
Colin Hogben 7:379d46fd02c2 77 }
Colin Hogben 7:379d46fd02c2 78 if (map != NULL) {
Colin Hogben 7:379d46fd02c2 79 for (uint i = 0; i < map->alloc; i++) {
Colin Hogben 7:379d46fd02c2 80 if (map->table[i].key != MP_OBJ_NULL) {
Colin Hogben 7:379d46fd02c2 81 pyhelp_print_info_about_object(map->table[i].key, map->table[i].value);
Colin Hogben 7:379d46fd02c2 82 }
Colin Hogben 7:379d46fd02c2 83 }
Colin Hogben 7:379d46fd02c2 84 }
Colin Hogben 7:379d46fd02c2 85 }