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 Process raw qstr file and output qstr data with length, hash and data bytes.
pythontech 0:5868e8752d44 3
pythontech 0:5868e8752d44 4 This script works with Python 2.6, 2.7, 3.3 and 3.4.
pythontech 0:5868e8752d44 5 """
pythontech 0:5868e8752d44 6
pythontech 0:5868e8752d44 7 from __future__ import print_function
pythontech 0:5868e8752d44 8
pythontech 0:5868e8752d44 9 import re
pythontech 0:5868e8752d44 10 import sys
pythontech 0:5868e8752d44 11
pythontech 0:5868e8752d44 12 # codepoint2name is different in Python 2 to Python 3
pythontech 0:5868e8752d44 13 import platform
pythontech 0:5868e8752d44 14 if platform.python_version_tuple()[0] == '2':
pythontech 0:5868e8752d44 15 from htmlentitydefs import codepoint2name
pythontech 0:5868e8752d44 16 elif platform.python_version_tuple()[0] == '3':
pythontech 0:5868e8752d44 17 from html.entities import codepoint2name
pythontech 0:5868e8752d44 18 codepoint2name[ord('-')] = 'hyphen';
pythontech 0:5868e8752d44 19
pythontech 0:5868e8752d44 20 # add some custom names to map characters that aren't in HTML
pythontech 0:5868e8752d44 21 codepoint2name[ord(' ')] = 'space'
pythontech 0:5868e8752d44 22 codepoint2name[ord('\'')] = 'squot'
pythontech 0:5868e8752d44 23 codepoint2name[ord(',')] = 'comma'
pythontech 0:5868e8752d44 24 codepoint2name[ord('.')] = 'dot'
pythontech 0:5868e8752d44 25 codepoint2name[ord(':')] = 'colon'
pythontech 0:5868e8752d44 26 codepoint2name[ord('/')] = 'slash'
pythontech 0:5868e8752d44 27 codepoint2name[ord('%')] = 'percent'
pythontech 0:5868e8752d44 28 codepoint2name[ord('#')] = 'hash'
pythontech 0:5868e8752d44 29 codepoint2name[ord('(')] = 'paren_open'
pythontech 0:5868e8752d44 30 codepoint2name[ord(')')] = 'paren_close'
pythontech 0:5868e8752d44 31 codepoint2name[ord('[')] = 'bracket_open'
pythontech 0:5868e8752d44 32 codepoint2name[ord(']')] = 'bracket_close'
pythontech 0:5868e8752d44 33 codepoint2name[ord('{')] = 'brace_open'
pythontech 0:5868e8752d44 34 codepoint2name[ord('}')] = 'brace_close'
pythontech 0:5868e8752d44 35 codepoint2name[ord('*')] = 'star'
pythontech 0:5868e8752d44 36 codepoint2name[ord('!')] = 'bang'
pythontech 0:5868e8752d44 37 codepoint2name[ord('\\')] = 'backslash'
pythontech 0:5868e8752d44 38 codepoint2name[ord('+')] = 'plus'
pythontech 0:5868e8752d44 39
pythontech 0:5868e8752d44 40 # this must match the equivalent function in qstr.c
pythontech 0:5868e8752d44 41 def compute_hash(qstr, bytes_hash):
pythontech 0:5868e8752d44 42 hash = 5381
pythontech 0:5868e8752d44 43 for char in qstr:
pythontech 0:5868e8752d44 44 hash = (hash * 33) ^ ord(char)
pythontech 0:5868e8752d44 45 # Make sure that valid hash is never zero, zero means "hash not computed"
pythontech 0:5868e8752d44 46 return (hash & ((1 << (8 * bytes_hash)) - 1)) or 1
pythontech 0:5868e8752d44 47
pythontech 0:5868e8752d44 48 def do_work(infiles):
pythontech 0:5868e8752d44 49 # read the qstrs in from the input files
pythontech 0:5868e8752d44 50 qcfgs = {}
pythontech 0:5868e8752d44 51 qstrs = {}
pythontech 0:5868e8752d44 52 for infile in infiles:
pythontech 0:5868e8752d44 53 with open(infile, 'rt') as f:
pythontech 0:5868e8752d44 54 for line in f:
pythontech 0:5868e8752d44 55 line = line.strip()
pythontech 0:5868e8752d44 56
pythontech 0:5868e8752d44 57 # is this a config line?
pythontech 0:5868e8752d44 58 match = re.match(r'^QCFG\((.+), (.+)\)', line)
pythontech 0:5868e8752d44 59 if match:
pythontech 0:5868e8752d44 60 value = match.group(2)
pythontech 0:5868e8752d44 61 if value[0] == '(' and value[-1] == ')':
pythontech 0:5868e8752d44 62 # strip parenthesis from config value
pythontech 0:5868e8752d44 63 value = value[1:-1]
pythontech 0:5868e8752d44 64 qcfgs[match.group(1)] = value
pythontech 0:5868e8752d44 65 continue
pythontech 0:5868e8752d44 66
pythontech 0:5868e8752d44 67 # is this a QSTR line?
pythontech 0:5868e8752d44 68 match = re.match(r'^Q\((.*)\)$', line)
pythontech 0:5868e8752d44 69 if not match:
pythontech 0:5868e8752d44 70 continue
pythontech 0:5868e8752d44 71
pythontech 0:5868e8752d44 72 # get the qstr value
pythontech 0:5868e8752d44 73 qstr = match.group(1)
pythontech 0:5868e8752d44 74 ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr)
pythontech 0:5868e8752d44 75
pythontech 0:5868e8752d44 76 # don't add duplicates
pythontech 0:5868e8752d44 77 if ident in qstrs:
pythontech 0:5868e8752d44 78 continue
pythontech 0:5868e8752d44 79
pythontech 0:5868e8752d44 80 # add the qstr to the list, with order number to retain original order in file
pythontech 0:5868e8752d44 81 qstrs[ident] = (len(qstrs), ident, qstr)
pythontech 0:5868e8752d44 82
pythontech 0:5868e8752d44 83 if not qcfgs:
pythontech 0:5868e8752d44 84 sys.stderr.write("ERROR: Empty preprocessor output - check for errors above\n")
pythontech 0:5868e8752d44 85 sys.exit(1)
pythontech 0:5868e8752d44 86
pythontech 0:5868e8752d44 87 # get config variables
pythontech 0:5868e8752d44 88 cfg_bytes_len = int(qcfgs['BYTES_IN_LEN'])
pythontech 0:5868e8752d44 89 cfg_bytes_hash = int(qcfgs['BYTES_IN_HASH'])
pythontech 0:5868e8752d44 90 cfg_max_len = 1 << (8 * cfg_bytes_len)
pythontech 0:5868e8752d44 91
pythontech 0:5868e8752d44 92 # print out the starter of the generated C header file
pythontech 0:5868e8752d44 93 print('// This file was automatically generated by makeqstrdata.py')
pythontech 0:5868e8752d44 94 print('')
pythontech 0:5868e8752d44 95
pythontech 0:5868e8752d44 96 # add NULL qstr with no hash or data
pythontech 0:5868e8752d44 97 print('QDEF(MP_QSTR_NULL, (const byte*)"%s%s" "")' % ('\\x00' * cfg_bytes_hash, '\\x00' * cfg_bytes_len))
pythontech 0:5868e8752d44 98
pythontech 0:5868e8752d44 99 # go through each qstr and print it out
pythontech 0:5868e8752d44 100 for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
pythontech 0:5868e8752d44 101 qhash = compute_hash(qstr, cfg_bytes_hash)
pythontech 0:5868e8752d44 102 # Calculate len of str, taking escapes into account
pythontech 0:5868e8752d44 103 qlen = len(qstr.replace("\\\\", "-").replace("\\", ""))
pythontech 0:5868e8752d44 104 qdata = qstr.replace('"', '\\"')
pythontech 0:5868e8752d44 105 if qlen >= cfg_max_len:
pythontech 0:5868e8752d44 106 print('qstr is too long:', qstr)
pythontech 0:5868e8752d44 107 assert False
pythontech 0:5868e8752d44 108 qlen_str = ('\\x%02x' * cfg_bytes_len) % tuple(((qlen >> (8 * i)) & 0xff) for i in range(cfg_bytes_len))
pythontech 0:5868e8752d44 109 qhash_str = ('\\x%02x' * cfg_bytes_hash) % tuple(((qhash >> (8 * i)) & 0xff) for i in range(cfg_bytes_hash))
pythontech 0:5868e8752d44 110 print('QDEF(MP_QSTR_%s, (const byte*)"%s%s" "%s")' % (ident, qhash_str, qlen_str, qdata))
pythontech 0:5868e8752d44 111
pythontech 0:5868e8752d44 112 if __name__ == "__main__":
pythontech 0:5868e8752d44 113 do_work(sys.argv[1:])