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 # where py object files go (they have a name prefix to prevent filename clashes)
pythontech 0:5868e8752d44 2 PY_BUILD = $(BUILD)/py
pythontech 0:5868e8752d44 3
pythontech 0:5868e8752d44 4 # where autogenerated header files go
pythontech 0:5868e8752d44 5 HEADER_BUILD = $(BUILD)/genhdr
pythontech 0:5868e8752d44 6
pythontech 0:5868e8752d44 7 # file containing qstr defs for the core Python bit
pythontech 0:5868e8752d44 8 PY_QSTR_DEFS = $(PY_SRC)/qstrdefs.h
pythontech 0:5868e8752d44 9
pythontech 0:5868e8752d44 10 # some code is performance bottleneck and compiled with other optimization options
pythontech 0:5868e8752d44 11 CSUPEROPT = -O3
pythontech 0:5868e8752d44 12
pythontech 0:5868e8752d44 13 INC += -I../lib/netutils
pythontech 0:5868e8752d44 14
pythontech 0:5868e8752d44 15 ifeq ($(MICROPY_PY_USSL),1)
pythontech 0:5868e8752d44 16 CFLAGS_MOD += -DMICROPY_PY_USSL=1 -I../lib/axtls/ssl -I../lib/axtls/crypto -I../lib/axtls/config
pythontech 0:5868e8752d44 17 LDFLAGS_MOD += -L../lib/axtls/_stage -laxtls
pythontech 0:5868e8752d44 18 endif
pythontech 0:5868e8752d44 19
pythontech 0:5868e8752d44 20 #ifeq ($(MICROPY_PY_LWIP),1)
pythontech 0:5868e8752d44 21 #CFLAGS_MOD += -DMICROPY_PY_LWIP=1 -I../lib/lwip/src/include -I../lib/lwip/src/include/ipv4 -I../extmod/lwip-include
pythontech 0:5868e8752d44 22 #endif
pythontech 0:5868e8752d44 23
pythontech 0:5868e8752d44 24 ifeq ($(MICROPY_PY_LWIP),1)
pythontech 0:5868e8752d44 25 LWIP_DIR = lib/lwip/src
pythontech 0:5868e8752d44 26 INC += -I../lib/lwip/src/include -I../lib/lwip/src/include/ipv4 -I../extmod/lwip-include
pythontech 0:5868e8752d44 27 CFLAGS_MOD += -DMICROPY_PY_LWIP=1
pythontech 0:5868e8752d44 28 SRC_MOD += extmod/modlwip.c lib/netutils/netutils.c
pythontech 0:5868e8752d44 29 SRC_MOD += $(addprefix $(LWIP_DIR)/,\
pythontech 0:5868e8752d44 30 core/def.c \
pythontech 0:5868e8752d44 31 core/dns.c \
pythontech 0:5868e8752d44 32 core/init.c \
pythontech 0:5868e8752d44 33 core/mem.c \
pythontech 0:5868e8752d44 34 core/memp.c \
pythontech 0:5868e8752d44 35 core/netif.c \
pythontech 0:5868e8752d44 36 core/pbuf.c \
pythontech 0:5868e8752d44 37 core/raw.c \
pythontech 0:5868e8752d44 38 core/stats.c \
pythontech 0:5868e8752d44 39 core/sys.c \
pythontech 0:5868e8752d44 40 core/tcp.c \
pythontech 0:5868e8752d44 41 core/tcp_in.c \
pythontech 0:5868e8752d44 42 core/tcp_out.c \
pythontech 0:5868e8752d44 43 core/timers.c \
pythontech 0:5868e8752d44 44 core/udp.c \
pythontech 0:5868e8752d44 45 core/ipv4/autoip.c \
pythontech 0:5868e8752d44 46 core/ipv4/icmp.c \
pythontech 0:5868e8752d44 47 core/ipv4/igmp.c \
pythontech 0:5868e8752d44 48 core/ipv4/inet.c \
pythontech 0:5868e8752d44 49 core/ipv4/inet_chksum.c \
pythontech 0:5868e8752d44 50 core/ipv4/ip_addr.c \
pythontech 0:5868e8752d44 51 core/ipv4/ip.c \
pythontech 0:5868e8752d44 52 core/ipv4/ip_frag.c \
pythontech 0:5868e8752d44 53 )
pythontech 0:5868e8752d44 54 ifeq ($(MICROPY_PY_LWIP_SLIP),1)
pythontech 0:5868e8752d44 55 CFLAGS_MOD += -DMICROPY_PY_LWIP_SLIP=1
pythontech 0:5868e8752d44 56 SRC_MOD += $(LWIP_DIR)/netif/slipif.c
pythontech 0:5868e8752d44 57 endif
pythontech 0:5868e8752d44 58 endif
pythontech 0:5868e8752d44 59
pythontech 0:5868e8752d44 60 # py object files
pythontech 0:5868e8752d44 61 PY_O_BASENAME = \
pythontech 0:5868e8752d44 62 mpstate.o \
pythontech 0:5868e8752d44 63 nlrx86.o \
pythontech 0:5868e8752d44 64 nlrx64.o \
pythontech 0:5868e8752d44 65 nlrthumb.o \
pythontech 0:5868e8752d44 66 nlrxtensa.o \
pythontech 0:5868e8752d44 67 nlrsetjmp.o \
pythontech 0:5868e8752d44 68 malloc.o \
pythontech 0:5868e8752d44 69 gc.o \
pythontech 0:5868e8752d44 70 qstr.o \
pythontech 0:5868e8752d44 71 vstr.o \
pythontech 0:5868e8752d44 72 mpprint.o \
pythontech 0:5868e8752d44 73 unicode.o \
pythontech 0:5868e8752d44 74 mpz.o \
pythontech 0:5868e8752d44 75 lexer.o \
pythontech 0:5868e8752d44 76 lexerstr.o \
pythontech 0:5868e8752d44 77 lexerunix.o \
pythontech 0:5868e8752d44 78 parse.o \
pythontech 0:5868e8752d44 79 scope.o \
pythontech 0:5868e8752d44 80 compile.o \
pythontech 0:5868e8752d44 81 emitcommon.o \
pythontech 0:5868e8752d44 82 emitbc.o \
pythontech 0:5868e8752d44 83 asmx64.o \
pythontech 0:5868e8752d44 84 emitnx64.o \
pythontech 0:5868e8752d44 85 asmx86.o \
pythontech 0:5868e8752d44 86 emitnx86.o \
pythontech 0:5868e8752d44 87 asmthumb.o \
pythontech 0:5868e8752d44 88 emitnthumb.o \
pythontech 0:5868e8752d44 89 emitinlinethumb.o \
pythontech 0:5868e8752d44 90 asmarm.o \
pythontech 0:5868e8752d44 91 emitnarm.o \
pythontech 0:5868e8752d44 92 formatfloat.o \
pythontech 0:5868e8752d44 93 parsenumbase.o \
pythontech 0:5868e8752d44 94 parsenum.o \
pythontech 0:5868e8752d44 95 emitglue.o \
pythontech 0:5868e8752d44 96 runtime.o \
pythontech 0:5868e8752d44 97 nativeglue.o \
pythontech 0:5868e8752d44 98 stackctrl.o \
pythontech 0:5868e8752d44 99 argcheck.o \
pythontech 0:5868e8752d44 100 warning.o \
pythontech 0:5868e8752d44 101 map.o \
pythontech 0:5868e8752d44 102 obj.o \
pythontech 0:5868e8752d44 103 objarray.o \
pythontech 0:5868e8752d44 104 objattrtuple.o \
pythontech 0:5868e8752d44 105 objbool.o \
pythontech 0:5868e8752d44 106 objboundmeth.o \
pythontech 0:5868e8752d44 107 objcell.o \
pythontech 0:5868e8752d44 108 objclosure.o \
pythontech 0:5868e8752d44 109 objcomplex.o \
pythontech 0:5868e8752d44 110 objdict.o \
pythontech 0:5868e8752d44 111 objenumerate.o \
pythontech 0:5868e8752d44 112 objexcept.o \
pythontech 0:5868e8752d44 113 objfilter.o \
pythontech 0:5868e8752d44 114 objfloat.o \
pythontech 0:5868e8752d44 115 objfun.o \
pythontech 0:5868e8752d44 116 objgenerator.o \
pythontech 0:5868e8752d44 117 objgetitemiter.o \
pythontech 0:5868e8752d44 118 objint.o \
pythontech 0:5868e8752d44 119 objint_longlong.o \
pythontech 0:5868e8752d44 120 objint_mpz.o \
pythontech 0:5868e8752d44 121 objlist.o \
pythontech 0:5868e8752d44 122 objmap.o \
pythontech 0:5868e8752d44 123 objmodule.o \
pythontech 0:5868e8752d44 124 objobject.o \
pythontech 0:5868e8752d44 125 objpolyiter.o \
pythontech 0:5868e8752d44 126 objproperty.o \
pythontech 0:5868e8752d44 127 objnone.o \
pythontech 0:5868e8752d44 128 objnamedtuple.o \
pythontech 0:5868e8752d44 129 objrange.o \
pythontech 0:5868e8752d44 130 objreversed.o \
pythontech 0:5868e8752d44 131 objset.o \
pythontech 0:5868e8752d44 132 objsingleton.o \
pythontech 0:5868e8752d44 133 objslice.o \
pythontech 0:5868e8752d44 134 objstr.o \
pythontech 0:5868e8752d44 135 objstrunicode.o \
pythontech 0:5868e8752d44 136 objstringio.o \
pythontech 0:5868e8752d44 137 objtuple.o \
pythontech 0:5868e8752d44 138 objtype.o \
pythontech 0:5868e8752d44 139 objzip.o \
pythontech 0:5868e8752d44 140 opmethods.o \
pythontech 0:5868e8752d44 141 sequence.o \
pythontech 0:5868e8752d44 142 stream.o \
pythontech 0:5868e8752d44 143 binary.o \
pythontech 0:5868e8752d44 144 builtinimport.o \
pythontech 0:5868e8752d44 145 builtinevex.o \
pythontech 0:5868e8752d44 146 modarray.o \
pythontech 0:5868e8752d44 147 modbuiltins.o \
pythontech 0:5868e8752d44 148 modcollections.o \
pythontech 0:5868e8752d44 149 modgc.o \
pythontech 0:5868e8752d44 150 modio.o \
pythontech 0:5868e8752d44 151 modmath.o \
pythontech 0:5868e8752d44 152 modcmath.o \
pythontech 0:5868e8752d44 153 modmicropython.o \
pythontech 0:5868e8752d44 154 modstruct.o \
pythontech 0:5868e8752d44 155 modsys.o \
pythontech 0:5868e8752d44 156 vm.o \
pythontech 0:5868e8752d44 157 bc.o \
pythontech 0:5868e8752d44 158 showbc.o \
pythontech 0:5868e8752d44 159 repl.o \
pythontech 0:5868e8752d44 160 smallint.o \
pythontech 0:5868e8752d44 161 frozenmod.o \
pythontech 0:5868e8752d44 162 ../extmod/moductypes.o \
pythontech 0:5868e8752d44 163 ../extmod/modujson.o \
pythontech 0:5868e8752d44 164 ../extmod/modure.o \
pythontech 0:5868e8752d44 165 ../extmod/moduzlib.o \
pythontech 0:5868e8752d44 166 ../extmod/moduheapq.o \
pythontech 0:5868e8752d44 167 ../extmod/moduhashlib.o \
pythontech 0:5868e8752d44 168 ../extmod/modubinascii.o \
pythontech 0:5868e8752d44 169 ../extmod/machine_mem.o \
pythontech 0:5868e8752d44 170 ../extmod/modussl.o \
pythontech 0:5868e8752d44 171 ../extmod/modurandom.o \
pythontech 0:5868e8752d44 172 ../extmod/modwebsocket.o \
pythontech 0:5868e8752d44 173 ../extmod/fsusermount.o \
pythontech 0:5868e8752d44 174 ../extmod/vfs_fat.o \
pythontech 0:5868e8752d44 175 ../extmod/vfs_fat_ffconf.o \
pythontech 0:5868e8752d44 176 ../extmod/vfs_fat_diskio.o \
pythontech 0:5868e8752d44 177 ../extmod/vfs_fat_file.o \
pythontech 0:5868e8752d44 178 ../extmod/vfs_fat_lexer.o \
pythontech 0:5868e8752d44 179 ../extmod/vfs_fat_misc.o \
pythontech 0:5868e8752d44 180 ../extmod/moduos_dupterm.o \
pythontech 0:5868e8752d44 181
pythontech 0:5868e8752d44 182 # prepend the build destination prefix to the py object files
pythontech 0:5868e8752d44 183 PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))
pythontech 0:5868e8752d44 184
pythontech 0:5868e8752d44 185 # Anything that depends on FORCE will be considered out-of-date
pythontech 0:5868e8752d44 186 FORCE:
pythontech 0:5868e8752d44 187 .PHONY: FORCE
pythontech 0:5868e8752d44 188
pythontech 0:5868e8752d44 189 $(HEADER_BUILD)/mpversion.h: FORCE | $(HEADER_BUILD)
pythontech 0:5868e8752d44 190 $(Q)$(PYTHON) $(PY_SRC)/makeversionhdr.py $@
pythontech 0:5868e8752d44 191
pythontech 0:5868e8752d44 192 # mpconfigport.mk is optional, but changes to it may drastically change
pythontech 0:5868e8752d44 193 # overall config, so they need to be caught
pythontech 0:5868e8752d44 194 MPCONFIGPORT_MK = $(wildcard mpconfigport.mk)
pythontech 0:5868e8752d44 195
pythontech 0:5868e8752d44 196 # qstr data
pythontech 0:5868e8752d44 197
pythontech 0:5868e8752d44 198 # Adding an order only dependency on $(HEADER_BUILD) causes $(HEADER_BUILD) to get
pythontech 0:5868e8752d44 199 # created before we run the script to generate the .h
pythontech 0:5868e8752d44 200 # Note: we need to protect the qstr names from the preprocessor, so we wrap
pythontech 0:5868e8752d44 201 # the lines in "" and then unwrap after the preprocessor is finished.
pythontech 0:5868e8752d44 202 $(HEADER_BUILD)/qstrdefs.generated.h: $(PY_QSTR_DEFS) $(QSTR_DEFS) $(PY_SRC)/makeqstrdata.py mpconfigport.h $(MPCONFIGPORT_MK) $(PY_SRC)/mpconfig.h | $(HEADER_BUILD)
pythontech 0:5868e8752d44 203 $(ECHO) "GEN $@"
pythontech 0:5868e8752d44 204 $(Q)cat $(PY_QSTR_DEFS) $(QSTR_DEFS) | $(SED) 's/^Q(.*)/"&"/' | $(CPP) $(CFLAGS) - | sed 's/^"\(Q(.*)\)"/\1/' > $(HEADER_BUILD)/qstrdefs.preprocessed.h
pythontech 0:5868e8752d44 205 $(Q)$(PYTHON) $(PY_SRC)/makeqstrdata.py $(HEADER_BUILD)/qstrdefs.preprocessed.h > $@
pythontech 0:5868e8752d44 206
pythontech 0:5868e8752d44 207 # emitters
pythontech 0:5868e8752d44 208
pythontech 0:5868e8752d44 209 $(PY_BUILD)/emitnx64.o: CFLAGS += -DN_X64
pythontech 0:5868e8752d44 210 $(PY_BUILD)/emitnx64.o: py/emitnative.c
pythontech 0:5868e8752d44 211 $(call compile_c)
pythontech 0:5868e8752d44 212
pythontech 0:5868e8752d44 213 $(PY_BUILD)/emitnx86.o: CFLAGS += -DN_X86
pythontech 0:5868e8752d44 214 $(PY_BUILD)/emitnx86.o: py/emitnative.c
pythontech 0:5868e8752d44 215 $(call compile_c)
pythontech 0:5868e8752d44 216
pythontech 0:5868e8752d44 217 $(PY_BUILD)/emitnthumb.o: CFLAGS += -DN_THUMB
pythontech 0:5868e8752d44 218 $(PY_BUILD)/emitnthumb.o: py/emitnative.c
pythontech 0:5868e8752d44 219 $(call compile_c)
pythontech 0:5868e8752d44 220
pythontech 0:5868e8752d44 221 $(PY_BUILD)/emitnarm.o: CFLAGS += -DN_ARM
pythontech 0:5868e8752d44 222 $(PY_BUILD)/emitnarm.o: py/emitnative.c
pythontech 0:5868e8752d44 223 $(call compile_c)
pythontech 0:5868e8752d44 224
pythontech 0:5868e8752d44 225 # optimising gc for speed; 5ms down to 4ms on pybv2
pythontech 0:5868e8752d44 226 $(PY_BUILD)/gc.o: CFLAGS += $(CSUPEROPT)
pythontech 0:5868e8752d44 227
pythontech 0:5868e8752d44 228 # optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
pythontech 0:5868e8752d44 229 $(PY_BUILD)/vm.o: CFLAGS += $(CSUPEROPT)
pythontech 0:5868e8752d44 230 # Optimizing vm.o for modern deeply pipelined CPUs with branch predictors
pythontech 0:5868e8752d44 231 # may require disabling tail jump optimization. This will make sure that
pythontech 0:5868e8752d44 232 # each opcode has its own dispatching jump which will improve branch
pythontech 0:5868e8752d44 233 # branch predictor efficiency.
pythontech 0:5868e8752d44 234 # http://article.gmane.org/gmane.comp.lang.lua.general/75426
pythontech 0:5868e8752d44 235 # http://hg.python.org/cpython/file/b127046831e2/Python/ceval.c#l828
pythontech 0:5868e8752d44 236 # http://www.emulators.com/docs/nx25_nostradamus.htm
pythontech 0:5868e8752d44 237 #-fno-crossjumping