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_ASMX86_H__
pythontech 0:5868e8752d44 27 #define __MICROPY_INCLUDED_PY_ASMX86_H__
pythontech 0:5868e8752d44 28
pythontech 0:5868e8752d44 29 #include "py/mpconfig.h"
pythontech 0:5868e8752d44 30 #include "py/misc.h"
pythontech 0:5868e8752d44 31
pythontech 0:5868e8752d44 32 // x86 cdecl calling convention is:
pythontech 0:5868e8752d44 33 // - args passed on the stack in reverse order
pythontech 0:5868e8752d44 34 // - return value in EAX
pythontech 0:5868e8752d44 35 // - caller cleans up the stack after a call
pythontech 0:5868e8752d44 36 // - stack must be aligned to 16-byte boundary before all calls
pythontech 0:5868e8752d44 37 // - EAX, ECX, EDX are caller-save
pythontech 0:5868e8752d44 38 // - EBX, ESI, EDI, EBP, ESP, EIP are callee-save
pythontech 0:5868e8752d44 39
pythontech 0:5868e8752d44 40 // In the functions below, argument order follows x86 docs and generally
pythontech 0:5868e8752d44 41 // the destination is the first argument.
pythontech 0:5868e8752d44 42 // NOTE: this is a change from the old convention used in this file and
pythontech 0:5868e8752d44 43 // some functions still use the old (reverse) convention.
pythontech 0:5868e8752d44 44
pythontech 0:5868e8752d44 45 #define ASM_X86_PASS_COMPUTE (1)
pythontech 0:5868e8752d44 46 #define ASM_X86_PASS_EMIT (2)
pythontech 0:5868e8752d44 47
pythontech 0:5868e8752d44 48 #define ASM_X86_REG_EAX (0)
pythontech 0:5868e8752d44 49 #define ASM_X86_REG_ECX (1)
pythontech 0:5868e8752d44 50 #define ASM_X86_REG_EDX (2)
pythontech 0:5868e8752d44 51 #define ASM_X86_REG_EBX (3)
pythontech 0:5868e8752d44 52 #define ASM_X86_REG_ESP (4)
pythontech 0:5868e8752d44 53 #define ASM_X86_REG_EBP (5)
pythontech 0:5868e8752d44 54 #define ASM_X86_REG_ESI (6)
pythontech 0:5868e8752d44 55 #define ASM_X86_REG_EDI (7)
pythontech 0:5868e8752d44 56
pythontech 0:5868e8752d44 57 // x86 passes values on the stack, but the emitter is register based, so we need
pythontech 0:5868e8752d44 58 // to define registers that can temporarily hold the function arguments. They
pythontech 0:5868e8752d44 59 // need to be defined here so that asm_x86_call_ind can push them onto the stack
pythontech 0:5868e8752d44 60 // before the call.
pythontech 0:5868e8752d44 61 #define ASM_X86_REG_ARG_1 ASM_X86_REG_EAX
pythontech 0:5868e8752d44 62 #define ASM_X86_REG_ARG_2 ASM_X86_REG_ECX
pythontech 0:5868e8752d44 63 #define ASM_X86_REG_ARG_3 ASM_X86_REG_EDX
pythontech 0:5868e8752d44 64 #define ASM_X86_REG_ARG_4 ASM_X86_REG_EBX
pythontech 0:5868e8752d44 65 #define ASM_X86_REG_ARG_5 ASM_X86_REG_ESI
pythontech 0:5868e8752d44 66
pythontech 0:5868e8752d44 67 // condition codes, used for jcc and setcc (despite their j-name!)
pythontech 0:5868e8752d44 68 #define ASM_X86_CC_JB (0x2) // below, unsigned
pythontech 0:5868e8752d44 69 #define ASM_X86_CC_JZ (0x4)
pythontech 0:5868e8752d44 70 #define ASM_X86_CC_JE (0x4)
pythontech 0:5868e8752d44 71 #define ASM_X86_CC_JNZ (0x5)
pythontech 0:5868e8752d44 72 #define ASM_X86_CC_JNE (0x5)
pythontech 0:5868e8752d44 73 #define ASM_X86_CC_JL (0xc) // less, signed
pythontech 0:5868e8752d44 74 #define ASM_X86_CC_JGE (0xd) // greater or equal, signed
pythontech 0:5868e8752d44 75 #define ASM_X86_CC_JLE (0xe) // less or equal, signed
pythontech 0:5868e8752d44 76 #define ASM_X86_CC_JG (0xf) // greater, signed
pythontech 0:5868e8752d44 77
pythontech 0:5868e8752d44 78 typedef struct _asm_x86_t asm_x86_t;
pythontech 0:5868e8752d44 79
pythontech 0:5868e8752d44 80 asm_x86_t* asm_x86_new(mp_uint_t max_num_labels);
pythontech 0:5868e8752d44 81 void asm_x86_free(asm_x86_t* as, bool free_code);
pythontech 0:5868e8752d44 82 void asm_x86_start_pass(asm_x86_t *as, mp_uint_t pass);
pythontech 0:5868e8752d44 83 void asm_x86_end_pass(asm_x86_t *as);
pythontech 0:5868e8752d44 84 mp_uint_t asm_x86_get_code_pos(asm_x86_t *as);
pythontech 0:5868e8752d44 85 mp_uint_t asm_x86_get_code_size(asm_x86_t* as);
pythontech 0:5868e8752d44 86 void* asm_x86_get_code(asm_x86_t* as);
pythontech 0:5868e8752d44 87
pythontech 0:5868e8752d44 88 void asm_x86_align(asm_x86_t *as, mp_uint_t align);
pythontech 0:5868e8752d44 89 void asm_x86_data(asm_x86_t *as, mp_uint_t bytesize, mp_uint_t val);
pythontech 0:5868e8752d44 90
pythontech 0:5868e8752d44 91 void asm_x86_mov_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
pythontech 0:5868e8752d44 92 void asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32);
pythontech 0:5868e8752d44 93 void asm_x86_mov_i32_to_r32_aligned(asm_x86_t *as, int32_t src_i32, int dest_r32);
pythontech 0:5868e8752d44 94 void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
pythontech 0:5868e8752d44 95 void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
pythontech 0:5868e8752d44 96 void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
pythontech 0:5868e8752d44 97 void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
pythontech 0:5868e8752d44 98 void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
pythontech 0:5868e8752d44 99 void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
pythontech 0:5868e8752d44 100 void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
pythontech 0:5868e8752d44 101 void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
pythontech 0:5868e8752d44 102 void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
pythontech 0:5868e8752d44 103 void asm_x86_shl_r32_cl(asm_x86_t* as, int dest_r32);
pythontech 0:5868e8752d44 104 void asm_x86_sar_r32_cl(asm_x86_t* as, int dest_r32);
pythontech 0:5868e8752d44 105 void asm_x86_add_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
pythontech 0:5868e8752d44 106 void asm_x86_sub_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
pythontech 0:5868e8752d44 107 void asm_x86_mul_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
pythontech 0:5868e8752d44 108 void asm_x86_cmp_r32_with_r32(asm_x86_t* as, int src_r32_a, int src_r32_b);
pythontech 0:5868e8752d44 109 void asm_x86_test_r8_with_r8(asm_x86_t* as, int src_r32_a, int src_r32_b);
pythontech 0:5868e8752d44 110 void asm_x86_setcc_r8(asm_x86_t* as, mp_uint_t jcc_type, int dest_r8);
pythontech 0:5868e8752d44 111 void asm_x86_label_assign(asm_x86_t* as, mp_uint_t label);
pythontech 0:5868e8752d44 112 void asm_x86_jmp_label(asm_x86_t* as, mp_uint_t label);
pythontech 0:5868e8752d44 113 void asm_x86_jcc_label(asm_x86_t* as, mp_uint_t jcc_type, mp_uint_t label);
pythontech 0:5868e8752d44 114 void asm_x86_entry(asm_x86_t* as, mp_uint_t num_locals);
pythontech 0:5868e8752d44 115 void asm_x86_exit(asm_x86_t* as);
pythontech 0:5868e8752d44 116 void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32);
pythontech 0:5868e8752d44 117 void asm_x86_mov_local_to_r32(asm_x86_t* as, int src_local_num, int dest_r32);
pythontech 0:5868e8752d44 118 void asm_x86_mov_r32_to_local(asm_x86_t* as, int src_r32, int dest_local_num);
pythontech 0:5868e8752d44 119 void asm_x86_mov_local_addr_to_r32(asm_x86_t* as, int local_num, int dest_r32);
pythontech 0:5868e8752d44 120 void asm_x86_call_ind(asm_x86_t* as, void* ptr, mp_uint_t n_args, int temp_r32);
pythontech 0:5868e8752d44 121
pythontech 0:5868e8752d44 122 #endif // __MICROPY_INCLUDED_PY_ASMX86_H__