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_MPZ_H__
pythontech 0:5868e8752d44 27 #define __MICROPY_INCLUDED_PY_MPZ_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
pythontech 0:5868e8752d44 34 // This mpz module implements arbitrary precision integers.
pythontech 0:5868e8752d44 35 //
pythontech 0:5868e8752d44 36 // The storage for each digit is defined by mpz_dig_t. The actual number of
pythontech 0:5868e8752d44 37 // bits in mpz_dig_t that are used is defined by MPZ_DIG_SIZE. The machine must
pythontech 0:5868e8752d44 38 // also provide a type that is twice as wide as mpz_dig_t, in both signed and
pythontech 0:5868e8752d44 39 // unsigned versions.
pythontech 0:5868e8752d44 40 //
pythontech 0:5868e8752d44 41 // MPZ_DIG_SIZE can be between 4 and 8*sizeof(mpz_dig_t), but it makes most
pythontech 0:5868e8752d44 42 // sense to have it as large as possible. If MPZ_DIG_SIZE is not already
pythontech 0:5868e8752d44 43 // defined then it is auto-detected below, depending on the machine. The types
pythontech 0:5868e8752d44 44 // are then set based on the value of MPZ_DIG_SIZE (although they can be freely
pythontech 0:5868e8752d44 45 // changed so long as the constraints mentioned above are met).
pythontech 0:5868e8752d44 46
pythontech 0:5868e8752d44 47 #ifndef MPZ_DIG_SIZE
pythontech 0:5868e8752d44 48 #if defined(__x86_64__) || defined(_WIN64)
pythontech 0:5868e8752d44 49 // 64-bit machine, using 32-bit storage for digits
pythontech 0:5868e8752d44 50 #define MPZ_DIG_SIZE (32)
pythontech 0:5868e8752d44 51 #else
pythontech 0:5868e8752d44 52 // default: 32-bit machine, using 16-bit storage for digits
pythontech 0:5868e8752d44 53 #define MPZ_DIG_SIZE (16)
pythontech 0:5868e8752d44 54 #endif
pythontech 0:5868e8752d44 55 #endif
pythontech 0:5868e8752d44 56
pythontech 0:5868e8752d44 57 #if MPZ_DIG_SIZE > 16
pythontech 0:5868e8752d44 58 typedef uint32_t mpz_dig_t;
pythontech 0:5868e8752d44 59 typedef uint64_t mpz_dbl_dig_t;
pythontech 0:5868e8752d44 60 typedef int64_t mpz_dbl_dig_signed_t;
pythontech 0:5868e8752d44 61 #elif MPZ_DIG_SIZE > 8
pythontech 0:5868e8752d44 62 typedef uint16_t mpz_dig_t;
pythontech 0:5868e8752d44 63 typedef uint32_t mpz_dbl_dig_t;
pythontech 0:5868e8752d44 64 typedef int32_t mpz_dbl_dig_signed_t;
pythontech 0:5868e8752d44 65 #elif MPZ_DIG_SIZE > 4
pythontech 0:5868e8752d44 66 typedef uint8_t mpz_dig_t;
pythontech 0:5868e8752d44 67 typedef uint16_t mpz_dbl_dig_t;
pythontech 0:5868e8752d44 68 typedef int16_t mpz_dbl_dig_signed_t;
pythontech 0:5868e8752d44 69 #else
pythontech 0:5868e8752d44 70 typedef uint8_t mpz_dig_t;
pythontech 0:5868e8752d44 71 typedef uint8_t mpz_dbl_dig_t;
pythontech 0:5868e8752d44 72 typedef int8_t mpz_dbl_dig_signed_t;
pythontech 0:5868e8752d44 73 #endif
pythontech 0:5868e8752d44 74
pythontech 0:5868e8752d44 75 #ifdef _WIN64
pythontech 0:5868e8752d44 76 #ifdef __MINGW32__
pythontech 0:5868e8752d44 77 #define MPZ_LONG_1 1LL
pythontech 0:5868e8752d44 78 #else
pythontech 0:5868e8752d44 79 #define MPZ_LONG_1 1i64
pythontech 0:5868e8752d44 80 #endif
pythontech 0:5868e8752d44 81 #else
pythontech 0:5868e8752d44 82 #define MPZ_LONG_1 1L
pythontech 0:5868e8752d44 83 #endif
pythontech 0:5868e8752d44 84
pythontech 0:5868e8752d44 85 // these define the maximum storage needed to hold an int or long long
pythontech 0:5868e8752d44 86 #define MPZ_NUM_DIG_FOR_INT ((sizeof(mp_int_t) * 8 + MPZ_DIG_SIZE - 1) / MPZ_DIG_SIZE)
pythontech 0:5868e8752d44 87 #define MPZ_NUM_DIG_FOR_LL ((sizeof(long long) * 8 + MPZ_DIG_SIZE - 1) / MPZ_DIG_SIZE)
pythontech 0:5868e8752d44 88
pythontech 0:5868e8752d44 89 typedef struct _mpz_t {
pythontech 0:5868e8752d44 90 mp_uint_t neg : 1;
pythontech 0:5868e8752d44 91 mp_uint_t fixed_dig : 1;
pythontech 0:5868e8752d44 92 mp_uint_t alloc : BITS_PER_WORD - 2;
pythontech 0:5868e8752d44 93 mp_uint_t len;
pythontech 0:5868e8752d44 94 mpz_dig_t *dig;
pythontech 0:5868e8752d44 95 } mpz_t;
pythontech 0:5868e8752d44 96
pythontech 0:5868e8752d44 97 // convenience macro to declare an mpz with a digit array from the stack, initialised by an integer
pythontech 0:5868e8752d44 98 #define MPZ_CONST_INT(z, val) mpz_t z; mpz_dig_t z ## _digits[MPZ_NUM_DIG_FOR_INT]; mpz_init_fixed_from_int(&z, z_digits, MPZ_NUM_DIG_FOR_INT, val);
pythontech 0:5868e8752d44 99
pythontech 0:5868e8752d44 100 void mpz_init_zero(mpz_t *z);
pythontech 0:5868e8752d44 101 void mpz_init_from_int(mpz_t *z, mp_int_t val);
pythontech 0:5868e8752d44 102 void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, mp_uint_t dig_alloc, mp_int_t val);
pythontech 0:5868e8752d44 103 void mpz_deinit(mpz_t *z);
pythontech 0:5868e8752d44 104
pythontech 0:5868e8752d44 105 void mpz_set(mpz_t *dest, const mpz_t *src);
pythontech 0:5868e8752d44 106 void mpz_set_from_int(mpz_t *z, mp_int_t src);
pythontech 0:5868e8752d44 107 void mpz_set_from_ll(mpz_t *z, long long i, bool is_signed);
pythontech 0:5868e8752d44 108 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 109 void mpz_set_from_float(mpz_t *z, mp_float_t src);
pythontech 0:5868e8752d44 110 #endif
pythontech 0:5868e8752d44 111 mp_uint_t mpz_set_from_str(mpz_t *z, const char *str, mp_uint_t len, bool neg, mp_uint_t base);
pythontech 0:5868e8752d44 112
pythontech 0:5868e8752d44 113 bool mpz_is_zero(const mpz_t *z);
pythontech 0:5868e8752d44 114 int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 115
pythontech 0:5868e8752d44 116 void mpz_abs_inpl(mpz_t *dest, const mpz_t *z);
pythontech 0:5868e8752d44 117 void mpz_neg_inpl(mpz_t *dest, const mpz_t *z);
pythontech 0:5868e8752d44 118 void mpz_not_inpl(mpz_t *dest, const mpz_t *z);
pythontech 0:5868e8752d44 119 void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs);
pythontech 0:5868e8752d44 120 void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs);
pythontech 0:5868e8752d44 121 void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 122 void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 123 void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 124 void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 125 void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 126 void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 127 void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 128 void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs);
pythontech 0:5868e8752d44 129
pythontech 0:5868e8752d44 130 mp_int_t mpz_hash(const mpz_t *z);
pythontech 0:5868e8752d44 131 bool mpz_as_int_checked(const mpz_t *z, mp_int_t *value);
pythontech 0:5868e8752d44 132 bool mpz_as_uint_checked(const mpz_t *z, mp_uint_t *value);
pythontech 0:5868e8752d44 133 void mpz_as_bytes(const mpz_t *z, bool big_endian, mp_uint_t len, byte *buf);
pythontech 0:5868e8752d44 134 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 135 mp_float_t mpz_as_float(const mpz_t *z);
pythontech 0:5868e8752d44 136 #endif
pythontech 0:5868e8752d44 137 mp_uint_t mpz_as_str_size(const mpz_t *i, mp_uint_t base, const char *prefix, char comma);
pythontech 0:5868e8752d44 138 mp_uint_t mpz_as_str_inpl(const mpz_t *z, mp_uint_t base, const char *prefix, char base_char, char comma, char *str);
pythontech 0:5868e8752d44 139
pythontech 0:5868e8752d44 140 #endif // __MICROPY_INCLUDED_PY_MPZ_H__