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 Generate header file with macros defining MicroPython version info.
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 sys
pythontech 0:5868e8752d44 10 import os
pythontech 0:5868e8752d44 11 import datetime
pythontech 0:5868e8752d44 12 import subprocess
pythontech 0:5868e8752d44 13
pythontech 0:5868e8752d44 14 def get_version_info_from_git():
pythontech 0:5868e8752d44 15 # Python 2.6 doesn't have check_output, so check for that
pythontech 0:5868e8752d44 16 try:
pythontech 0:5868e8752d44 17 subprocess.check_output
pythontech 0:5868e8752d44 18 subprocess.check_call
pythontech 0:5868e8752d44 19 except AttributeError:
pythontech 0:5868e8752d44 20 return None
pythontech 0:5868e8752d44 21
pythontech 0:5868e8752d44 22 # Note: git describe doesn't work if no tag is available
pythontech 0:5868e8752d44 23 try:
pythontech 0:5868e8752d44 24 git_tag = subprocess.check_output(["git", "describe", "--dirty", "--always"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
pythontech 0:5868e8752d44 25 except subprocess.CalledProcessError as er:
pythontech 0:5868e8752d44 26 if er.returncode == 128:
pythontech 0:5868e8752d44 27 # git exit code of 128 means no repository found
pythontech 0:5868e8752d44 28 return None
pythontech 0:5868e8752d44 29 git_tag = ""
pythontech 0:5868e8752d44 30 except OSError:
pythontech 0:5868e8752d44 31 return None
pythontech 0:5868e8752d44 32 try:
pythontech 0:5868e8752d44 33 git_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT, universal_newlines=True).strip()
pythontech 0:5868e8752d44 34 except subprocess.CalledProcessError:
pythontech 0:5868e8752d44 35 git_hash = "unknown"
pythontech 0:5868e8752d44 36 except OSError:
pythontech 0:5868e8752d44 37 return None
pythontech 0:5868e8752d44 38
pythontech 0:5868e8752d44 39 try:
pythontech 0:5868e8752d44 40 # Check if there are any modified files.
pythontech 0:5868e8752d44 41 subprocess.check_call(["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT)
pythontech 0:5868e8752d44 42 # Check if there are any staged files.
pythontech 0:5868e8752d44 43 subprocess.check_call(["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT)
pythontech 0:5868e8752d44 44 except subprocess.CalledProcessError:
pythontech 0:5868e8752d44 45 git_hash += "-dirty"
pythontech 0:5868e8752d44 46 except OSError:
pythontech 0:5868e8752d44 47 return None
pythontech 0:5868e8752d44 48
pythontech 0:5868e8752d44 49 # Try to extract MicroPython version from git tag
pythontech 0:5868e8752d44 50 if git_tag.startswith("v"):
pythontech 0:5868e8752d44 51 ver = git_tag[1:].split("-")[0].split(".")
pythontech 0:5868e8752d44 52 if len(ver) == 2:
pythontech 0:5868e8752d44 53 ver.append("0")
pythontech 0:5868e8752d44 54 else:
pythontech 0:5868e8752d44 55 ver = ["0", "0", "1"]
pythontech 0:5868e8752d44 56
pythontech 0:5868e8752d44 57 return git_tag, git_hash, ver
pythontech 0:5868e8752d44 58
pythontech 0:5868e8752d44 59 def get_version_info_from_docs_conf():
pythontech 0:5868e8752d44 60 with open(os.path.join(os.path.dirname(sys.argv[0]), "..", "docs", "conf.py")) as f:
pythontech 0:5868e8752d44 61 for line in f:
pythontech 0:5868e8752d44 62 if line.startswith("release = '"):
pythontech 0:5868e8752d44 63 ver = line.strip()[10:].strip("'")
pythontech 0:5868e8752d44 64 git_tag = "v" + ver
pythontech 0:5868e8752d44 65 ver = ver.split(".")
pythontech 0:5868e8752d44 66 if len(ver) == 2:
pythontech 0:5868e8752d44 67 ver.append("0")
pythontech 0:5868e8752d44 68 return git_tag, "<no hash>", ver
pythontech 0:5868e8752d44 69 return None
pythontech 0:5868e8752d44 70
pythontech 0:5868e8752d44 71 def make_version_header(filename):
pythontech 0:5868e8752d44 72 # Get version info using git, with fallback to docs/conf.py
pythontech 0:5868e8752d44 73 info = get_version_info_from_git()
pythontech 0:5868e8752d44 74 if info is None:
pythontech 0:5868e8752d44 75 info = get_version_info_from_docs_conf()
pythontech 0:5868e8752d44 76
pythontech 0:5868e8752d44 77 git_tag, git_hash, ver = info
pythontech 0:5868e8752d44 78
pythontech 0:5868e8752d44 79 # Generate the file with the git and version info
pythontech 0:5868e8752d44 80 file_data = """\
pythontech 0:5868e8752d44 81 // This file was generated by py/makeversionhdr.py
pythontech 0:5868e8752d44 82 #define MICROPY_GIT_TAG "%s"
pythontech 0:5868e8752d44 83 #define MICROPY_GIT_HASH "%s"
pythontech 0:5868e8752d44 84 #define MICROPY_BUILD_DATE "%s"
pythontech 0:5868e8752d44 85 #define MICROPY_VERSION_MAJOR (%s)
pythontech 0:5868e8752d44 86 #define MICROPY_VERSION_MINOR (%s)
pythontech 0:5868e8752d44 87 #define MICROPY_VERSION_MICRO (%s)
pythontech 0:5868e8752d44 88 #define MICROPY_VERSION_STRING "%s.%s.%s"
pythontech 0:5868e8752d44 89 """ % (git_tag, git_hash, datetime.date.today().strftime("%Y-%m-%d"),
pythontech 0:5868e8752d44 90 ver[0], ver[1], ver[2], ver[0], ver[1], ver[2])
pythontech 0:5868e8752d44 91
pythontech 0:5868e8752d44 92 # Check if the file contents changed from last time
pythontech 0:5868e8752d44 93 write_file = True
pythontech 0:5868e8752d44 94 if os.path.isfile(filename):
pythontech 0:5868e8752d44 95 with open(filename, 'r') as f:
pythontech 0:5868e8752d44 96 existing_data = f.read()
pythontech 0:5868e8752d44 97 if existing_data == file_data:
pythontech 0:5868e8752d44 98 write_file = False
pythontech 0:5868e8752d44 99
pythontech 0:5868e8752d44 100 # Only write the file if we need to
pythontech 0:5868e8752d44 101 if write_file:
pythontech 0:5868e8752d44 102 print("Generating %s" % filename)
pythontech 0:5868e8752d44 103 with open(filename, 'w') as f:
pythontech 0:5868e8752d44 104 f.write(file_data)
pythontech 0:5868e8752d44 105
pythontech 0:5868e8752d44 106 if __name__ == "__main__":
pythontech 0:5868e8752d44 107 make_version_header(sys.argv[1])