MicroPython for Teensy 3.1

Status
Not open for further replies.
Hello Dave,

Thank you for all your hard work with the MicroPython port. I am really looking forward to getting this working on my teensy brained bots.

I am having a few problems getting started though.
I can checkout and build the MicroPython STM platform OK, but the teensy side has a number of build errors.

In main.c there is an include to "mpqstr.h" which I can't seem to located or find an automated python generation script.
Just to get a bit further I have included "qstr.h" from the STM platform.

The next errors relate to mp_parser having insufficient parameters.
In teensy/main.c line 312 there is...
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);

Whereas in py/parse.h mp_parse is declared as:
mp_parse_node_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind, qstr *exc_id_out, const char **exc_msg_out);

I am getting more build errors, but I suspect I am doing something dumb or have missed something.

Any advice to point me in the right direction would be most appreciated.

Keep up the great work!

Andy
 
Hi Andy,

Yeah - the main source is in a fairly fast state of flux and its been a few days since I updated the port.

You can grab a copy that does compile by doing:

git clone https://github.com/dhylands/micropython.git
git checkout fix-2014-01-14

It looks like we're going to try and use the mbed hal layer (which has been open sourced)., so I'm currently in the process of adding the python interfaces for the mbed hal, and then need to flesh out the hal for the STM32F4 and the teensy board.

There are already partial hal implementations for the STM32F4xx:
https://github.com/mbedmicro/mbed/tree/master/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F4XX (which the micropython board will be using) and
https://github.com/mbedmicro/mbed/t...ed/targets/hal/TARGET_Freescale/TARGET_K20D5M (which should work - perhaps with changes - for the teensy)

I'm also actively looking to factor as much stuff as possible out of the stm port so that it can be shared more easily (right now the teensy is really a copy/paste).

Once that's done I'll get back to putting the teensy port in shape on the official micropython repo. Until then the fix-2014-01-14 branch should work (for hacking). Jon and I are both using that branch for our robots for right now.
 
Hi Andy,
Yeah - the main source is in a fairly fast state of flux and its been a few days since I updated the port.

Ah... I thought so....

Code:
About execute /boot.py
Executing boot.py
Done executing /boot.py
About execute '/src/main.py'
Executing main.py
Done executing '/src/main.py'
Micro Python for Teensy 3.1
Type "help()" for more information.
>>> print("Thank's Dave...you're awesome!")
Thank's Dave...you're awesome!
>>>

:)

let the hacking commence!
 
Which tool chain are you using? I haven't been able to build the Teensy version from yours, or the official micropython repo.

8-Dale
 
I've only ever built micropython under linux.

When building for the Teensy 3.1, it expects that you'll have TeensyDuino 1.18rc2 installed
http://forum.pjrc.com/threads/24796-Teensyduino-1-18-Release-Candidate-2-Available

and that the ARDUINO environment variable points to the top of the arduino tree. It then uses the toolchain contained in hardware/tools/arm-none-eabi/bin/

It's unlikely that teensy will build from the official micropython rep. You need to use the fix-2014-01-14 branch from repo.

Code:
git clone [url]https://github.com/dhylands/micropython.git[/url]
cd micropython
git checkout fix-2014-01-14
cd teensy
ARDUINO=/home/username/arduino make
(adjust ARDUINO appropriately)
 
I've only ever built micropython under linux.
I don't have anything that runs anything but Linux here. :)

I have the latest stuff from PJRC installed here, as suggested.

It's unlikely that teensy will build from the official micropython rep. You need to use the fix-2014-01-14 branch from repo.
OK, I will have to wait until my home internet is fixed then, when I can get the proper setup created.

Thanks!

8-Dale
 
If it helps, I have compiled the latest micropython branch to work with the teensy.

I'm not sure if main.c is working, (I wasn't too bothered with that). My main aim was to create a static library for use in C++ projects.

The micropython_vs repository is a Visual Studio makefile project that creates a static library (libmpython.a)
NOTE: it uses MingW make ... (I gave up on nmake ... too painful).

I have uploaded a fork of micropython....
https://github.com/10bulls/micropython
(use branch 10bulls)

https://github.com/10bulls/micropython_vs
This refers to the source files in my modified micropython repo.
Some of the build folders may need creating manually to get make working (sorry...haven't got round to fixing that build rule yet)

Here is the current state of my teensy brained robot which can now read micropython scripts from an SD card.
You can also upload scripts/files using xmodem and download using zmodem via a bluetooth link.

https://github.com/10bulls/zumo1
(use branch micropython)

I am using Visual Micro as my teensy/arduino environment.
The zumo1 project contains a 'libraries' folder with the files needed to make this happen:
From this...
Copy 'mpython' folder to the arduino libaries folder. (these are just header files from micropython with a couple of changes to get working with c++).

Add the contents of 'boards.txt' to the existing 'arduino/hardware/teensy/boards.txt'
(DO NOT REPLACE THE EXISTING ONE!)

Copy 'mk20dx256py.ld' to 'arduino/hardware/teensy/cores/teensy3'

NEEDS
Teensyduino 1.18 RC # 2 or later for programs > 128K
http://forum.pjrc.com/threads/24796-Teensyduino-1-18-Release-Candidate-2-Available

Now in a Visual Micro project you can just select 'Teensy 3.1 + python' from the target board drop down, cross your fingers and there is a possibility it might all work! ;)

The bulk of the python stuff is in zumo1\pysystem.c (similar to the main.c module in the micropython source).

Sorry it's all very messy and clunky, I'll try and tidy it all up sometime and maybe push some changes back to micropython.

Thanks again Dave for getting me up an running!
 
Last edited:
I just added libmpython.a to the zumo1\libraries project so if anyone wants to add python to an arduino c++ sketch project, you could give that a go.
(so no need to build micropython)
 
I just added libmpython.a to the zumo1\libraries project so if anyone wants to add python to an arduino c++ sketch project, you could give that a go.
(so no need to build micropython)

Take me by the hand here, I am missing something. I have done the things called for in "readme.txt", I see the new board type, what next? I guess what I am asking is how to get to an interactive prompt? How to save and load scripts? Micropython backer BTW
 
Take me by the hand here, I am missing something. I have done the things called for in "readme.txt", I see the new board type, what next? I guess what I am asking is how to get to an interactive prompt? How to save and load scripts? Micropython backer BTW
I have just uploaded a new git project...
https://github.com/10bulls/teensypy

This is a real cut down version that should be easier to get into.

It demonstrates
. running a python script stored on an SD card
. running the python (REPL) command line.
. finding python objects and calling their methods (from c++)

Dave added some neat memory zip code (which is in the main micropython repository). This basically appends a zip archive of your scripts to the .hex file which
you can then refer to. Handy if you don't a flash memory.

pymain.c is very ugly and needs a good tidy.
It contains some standard library functions that are needed as we are building using -nodefaultlibs.
These should really be separated out and added to libmpython.a
void python_setup(void)
is the function of most interest in pymain.c as this is where user defined C functions and objects are mapped into the python runtime.

I hope this helps!
 
Is it too soon, in the maturation of the Teensy mPython, for me to try this...
Python 2.7 or 2.6 code for Raspberry Pi, talking SPI to an RFM22 radio. Get that working.
Then take the same python code to the Teensy 3, tweak the SPI interface code for the T3.
Get the T3 and the RPi talking via the RF22 libraries' code that does star and mesh radio topology. Rather than using XBees on the UARTs. I know XBees well, but want sub-GHz range and cheaper radios.

The library for the RF22 is in C, so there'd have to be some API wrapper for python to call. No clue as to what that entails.

I suppose I could do this in C, but for drill, try Python. I did a large Python project for XBees with an embedded small ARM - and it worked better than I expected in terms of throughput. I avoided over-pythonic obtuse code.
 
Last edited:
Just a quick update on my dabblings...

https://github.com/10bulls/libmpython
...is a new project that just builds a static library from the main micropython repository code.
This library is intended to work with Arduino build environments like Visual Micro.
The boards.txt insert now works with the Arduino IDE as well (I think).

https://github.com/10bulls/teensypy
...has been updated to use libmpython.
The libraries folder contains a libmpython.a binary and some instructions on setting up the Arduino environment.
If you just want to have a play with micropython in an Arduino sketch without needing to build micropython this may help.
 
Is it too soon, in the maturation of the Teensy mPython, for me to try this...
Depends really. If it is for anything serious then I'd say give it another few months at least... (the main repository is changing by the hour).

It also depends on what level of python you want to use (avoid making use of python libraries for the time being).

If it's for experimentation or fun then I'd say give it a go. The python -> C API is pretty straight forward.
Doing most of the complicated stuff in C and using python as an easily changeable scripting glue seems a good combination.
 
I suppose I observe and let others trail-blaze.
I'd like to study-up. The Python for embedded that I did last year, many thousands of lines of Python 2.4 code (could not do 2.6+) - had a lot of external libraries stored in flash. On that project I could not use C, for a dumb vendor-proprietary reason. The key to this was compiling to bytecode on a PC, then building a zip file which was pushed into the flash file system on the embedded system. Then Python's ability to "mount" the zip file as a file system made it easy. No source code in the field. Just one zip file to distribute the entire large app.

I haven't looked at a Python bytecode interpreter's C code. I imagine it to be a complicated weave due to the multiple architectures that a common interpreter code base has to support.
 
I've only ever built micropython under linux.

When building for the Teensy 3.1, it expects that you'll have TeensyDuino 1.18rc2 installed
http://forum.pjrc.com/threads/24796-Teensyduino-1-18-Release-Candidate-2-Available

and that the ARDUINO environment variable points to the top of the arduino tree. It then uses the toolchain contained in hardware/tools/arm-none-eabi/bin/

It's unlikely that teensy will build from the official micropython rep. You need to use the fix-2014-01-14 branch from repo.

Code:
git clone [url]https://github.com/dhylands/micropython.git[/url]
cd micropython
git checkout fix-2014-01-14
cd teensy
ARDUINO=/home/username/arduino make
(adjust ARDUINO appropriately)

I followed your directions and it worked nicely. :D

Of course it would be wonderful to have support for more of the Teensy's features.
 
The latest master on micropython builds now, but it has a bit less functionality than the earlier build.
I've started working on the teensy port again and I'm slowly implementing things.

I'll be updating things that work on this wiki page:
https://github.com/micropython/micropython/wiki/Board-Teensy3.1

I thought I would give the latest Master a try. I'm getting an error.


danp@positron:~/fork/micropython/teensy$ ARDUINO=/home/danp/arduino/arduino-1.0.5 make
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
mkdir -p build/genhdr
Create build/pins_qstr.h
CPP ../py/qstrdefs.h
make: arm-none-eabi-gcc: Command not found
make: *** [build/genhdr/qstrdefs.generated.h] Error 127

Correction and edit
I went back your early version (fix-2014-01-14 branch) and built that again just to make sure I had not forgotten how I was doning that. It still works. Not to meantion that in the mean time I had installed "realpath" so now it is running main.py without complaint too.

2. Tried ecb5792f887392d53f98a84e1c56a807cc93d2ea
I tried checking out ecb5792f887392d53f98a84e1c56a807cc93d2ea on the master repository. The result was the same error.

3. difference in Makefile
I see in the fix makefile:

TOOLS_PATH = $(ARDUINO)/hardware/tools
COMPILER_PATH = $(TOOLS_PATH)/arm-none-eabi/bin
CORE_PATH = $(ARDUINO)/hardware/teensy/cores/teensy3

CROSS_COMPILE = $(COMPILER_PATH)/arm-none-eabi-

and in the master makefile

CROSS_COMPILE = arm-none-eabi-

which I ame guessing leads to the error. I have no real prior experience with Makefiles however.

4. adding compiler path -- will compile but not link
I tried adding the compiler path as shown in 3 and ensured there was no space at the end of arm-none-eabi- . Once this was done it would compile all the files but it would not link.

[snip]...core/yield.o build/pins_gen.o -L ./ -lm -L ./ -lc -L ./ -lgcc
/home/danp/arduino/arduino-1.0.5/hardware/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/bin/ld: cannot find -lm
/home/danp/arduino/arduino-1.0.5/hardware/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/bin/ld: cannot find -lc
/home/danp/arduino/arduino-1.0.5/hardware/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status
make: *** [build/micropython.elf] Error 1
 
Last edited:
Right. Somewhere along the way I changed things so that teensyduino is no longer required to build micropython. It now uses the same toolchain as is used for the pyboard. I use the toolchain mentioned in the README.md file at the root of the git tree. This has the advantage that the teensy builds are now integrated with Travis and every checkin to the tree verifies that the teensy build is still working.

I should probably make it fall back to using the toolchain from teensyduino if that's setup and no other toolchain is available.
 
Last edited:
Edit 3.
Okay. I got it to build on my debian computer.!
I made use of the directions at http://eliaselectronics.com/stm32f4-tutorials/setting-up-the-stm32f4-arm-development-toolchain/
They filled in the gaps I did not know.

My this really is amazing. I was so surprised by the number of features added already.

Thank you so much Dave.

Executing main.py
Micro Python v1.2-59-gecb5792 on 2014-08-11; Teensy-3.1 with MK20DX256
Type "help()" for more information.
>>>
>>> help()
Welcome to Micro Python!

For online help please visit http://micropython.org/help/.

Quick overview of commands for the board:
pyb.info() -- print some general information
pyb.gc() -- run the garbage collector
pyb.delay(n) -- wait for n milliseconds
pyb.Switch() -- create a switch object
Switch methods: (), callback(f)
pyb.LED(n) -- create an LED object for LED n (n=1,2,3,4)
LED methods: on(), off(), toggle(), intensity(<n>)
pyb.Pin(pin) -- get a pin, eg pyb.Pin('X1')
pyb.Pin(pin, m, [p]) -- get a pin and configure it for IO mode m, pull mode p
Pin methods: init(..), value([v]), high(), low()
pyb.ExtInt(pin, m, p, callback) -- create an external interrupt object
pyb.ADC(pin) -- make an analog object from a pin
ADC methods: read(), read_timed(buf, freq)
pyb.DAC(port) -- make a DAC object
DAC methods: triangle(freq), write(n), write_timed(buf, freq)
pyb.RTC() -- make an RTC object; methods: datetime([val])
...[snip]
 
Last edited:
Actually, it looks like that's the help for the regular pyboard. The teensy doesn't have alll of that implemented yet.
 
Actually, it looks like that's the help for the regular pyboard. The teensy doesn't have alll of that implemented yet.

I did notice a discrepancy between the features meantioned in the github micro python teensy wiki and those shown by the help. By the time I got it to build I did not have time to try anything else but type help().

I also think I remember noticing when programming (it was at 2 am) that memory was about 80% full and I had remembered it was a lot less full for the fix-2014-01-14 version. I'm heading off to work now so I won't be able to look agian until tonight.
 
I did notice a discrepancy between the features meantioned in the github micro python teensy wiki and those shown by the help. By the time I got it to build I did not have time to try anything else but type help().

I also think I remember noticing when programming (it was at 2 am) that memory was about 80% full and I had remembered it was a lot less full for the fix-2014-01-14 version. I'm heading off to work now so I won't be able to look agian until tonight.

I think that's mostly from the core part of micropython supporting more features.
 
This is awesome! I am learning Python and started a small group of kids who want to learn a "real" programming language; we have an informal google group if anyone else is interested: PythonForKids and it would be great to have some hands-on with this. I will check out the micropython project and pyboard too. We are using Python on BeagleBone Black so nice to extend it to Teensy as well. Thanks for doing this! Will try to contribute!
 
@bboyes I've signed up for the google group and sent a note expressing my interest in helping.
steve
 
Hey folks,

I've got a teensy 3.1 and I'm trying to compile and flash micropython from the git... with no success. I'm missing a few steps probably.

I cloned the git repo, and installed all the pre-requisite software.

When I compile using make in the teensy directory, and then attempt to flash using the teensy loader app I get this message:

11:55:57 AM: File "micropython.hex". 201176 bytes, 312% used
11:55:57 AM: File contains data beyond chip size, yikes!
11:56:03 AM: Verbose Info event

Can anyone post a bit more detail about the exact steps I should take? I've followed everything available but there are no details about actually building in the instructions anywhere.

Thanks!
 
Status
Not open for further replies.
Back
Top