Skip to content
This repository has been archived by the owner on Aug 27, 2021. It is now read-only.

Add build & upload support for Arduino Teensy #163

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

jpdoyle
Copy link

@jpdoyle jpdoyle commented Apr 25, 2014

My commit messages give more technical details, but in essence, this patch allows ino to build and upload projects for Arduino Teensy boards. I avoided changes that would obviously break other boards, adding exceptions specifically for the Teensy boards.

There are two small changes to argument handling in ino build: the build chain option defaults are now overridden by settings in boards.txt (eg. teensy3.build.command.make), and the --menu option allows command-line selection of build options that would typically be presented in drop-downs in the Arduino IDE. --menu's argument is a comma-delimited list of colon-separated key-value pairs (eg. "k0:v0,k1:v1,...") which select options for menus found in boards.txt (eg. "usb:serial" selects teensy3.menu.usb.serial), and the contents of these are merged into the board dictionary, making them invisible to the program after argument processing.

ino upload has been changed to support post_compile_script and avrdude_wrapper options specifically for teensy, which are defined in boards.txt. This lets ino support any board that works with Teensyduino, including the teensy3.1 (thanks to theJenix for his work on this).

Best wishes,
-Joe Doyle (Ginto8)

jpdoyle added 2 commits April 25, 2014 00:41
The Arduino Teensy has a different processor architecture, and
therefore a different build chain, than standard Arduinos. Building
and uploading are still possible through the Arduino IDE by patching
it and installing the teensy build chain. This patch adds parsing and
handling of extra options in the teensy's boards.txt, which should
allow it to read all necessary configuration to build on all Teensy
versions.

With this patch, the following command will build for teensy3 with a
96 MHz clock, an en-US keyboard format, and serial USB mode:

 $ ino build -m teensy3 --menu='keys:en-us,speed:96,usb:serial'

** This patch supports building only. Uploading will come later.

*** For some reason, this patch does not work with teensy1. I don't
know why right now, but I will investigate.
This was easier than I expected; it just overrides avrdude and uses
teensy-loader-cli. This should work just fine:
 $ ino upload -m teensy3
@wilywampa
Copy link

Hi, I'm trying to use your fork to build/upload to a Teensy 3.1 and I'm running into some problems. First, when I initialize a project I get a syntax error in build.py, but it's just a missing colon on line 182. After I add that colon, init works fine.

However, I get stuck when I try to build. Here's the command and the resulting error:

ino build -d $ARDUINO_DIR -m teensy31 --menu f_cpu:96000000

Traceback (most recent call last):
  File "/usr/local/bin/ino", line 6, in <module>
    main()
  File "/usr/local/lib/python2.7/site-packages/ino/runner.py", line 76, in main
    args.func(args)
  File "/usr/local/lib/python2.7/site-packages/ino/commands/build.py", line 413, in run
    self.discover(args)
  File "/usr/local/lib/python2.7/site-packages/ino/commands/build.py", line 178, in discover
    menu = self._parseMenu(args)
TypeError: _parseMenu() takes exactly 3 arguments (2 given)

Then I commented out the _parseMenu that takes 3 arguments and got this error:

Traceback (most recent call last):
  File "/usr/local/bin/ino", line 6, in <module>
    main()
  File "/usr/local/lib/python2.7/site-packages/ino/runner.py", line 76, in main
    args.func(args)
  File "/usr/local/lib/python2.7/site-packages/ino/commands/build.py", line 413, in run
    self.discover(args)
  File "/usr/local/lib/python2.7/site-packages/ino/commands/build.py", line 180, in discover
    _mergeDicts(self.e['menu'],menu)
NameError: global name '_mergeDicts' is not defined

So I hard-coded F_CPU to be 96000000 in the Python script. Now I'm getting this error and I think I'm stuck:

src/sketch.cpp
In file included from /Applications/Arduino-1.0.5.app/Contents/Resources/Java/hardware/teensy/cores/teensy3/usb_keyboard.h:34:0,
                 from /Applications/Arduino-1.0.5.app/Contents/Resources/Java/hardware/teensy/cores/teensy3/WProgram.h:25,
                 from /Applications/Arduino-1.0.5.app/Contents/Resources/Java/hardware/teensy/cores/teensy3/Arduino.h:1,
                 from .build/teensy31-5d575bc3/src/sketch.cpp:1:
/Applications/Arduino-1.0.5.app/Contents/Resources/Java/hardware/teensy/cores/teensy3/keylayouts.h:5414:14: error: 'KEYCODE_TYPE' does not name a type
/Applications/Arduino-1.0.5.app/Contents/Resources/Java/hardware/teensy/cores/teensy3/keylayouts.h:5415:14: error: 'KEYCODE_TYPE' does not name a type
make: *** [.build/teensy31-5d575bc3/src/sketch.o] Error 1
Make failed with code 2

Any idea why I'm running into all these problems? I'm running OS X 10.9.3, Python 2.7.6, and Arduino 1.0.5.

@jpdoyle
Copy link
Author

jpdoyle commented Jun 15, 2014

I was working on improving some stuff, and it seems I broke it quite a bit in the process. I didn't have a separate "develop" branch, so the non-working commit ended up on master. I have created a develop branch and reverted master to the working version. Re-clone and it should work now.

@wilywampa
Copy link

Thanks for the quick response! It made a whole lot more progress. Unfortunately it's still getting stuck. I got this error:

Linking firmware.elf
/Users/jniehus/Downloads/teensy/tools/mac/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/bin/ld: cannot find -larm_cortexM4l_math
collect2: error: ld returned 1 exit status
make: *** [.build/teensy31-5d575bc3/firmware.elf] Error 1
Make failed with code 2

I do have a file /Applications/Arduino-1.0.5.app/Contents/Resources/Java/hardware/tools/arm-none-eabi/arm-none-eabi/lib/libarm_cortexM4l_math.a so I change the command line to:

ino build -d $ARDUINO_DIR -m teensy31 --menu f_cpu:96000000 --ldflags="-L/Applications/Arduino-1.0.5.app/Contents/Resources/Java/hardware/tools/arm-none-eabi/arm-none-eabi/lib"

but that resulted in this error:

Linking firmware.elf
/Users/jniehus/Downloads/teensy/tools/mac/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/lib/thumb2/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text+0x12): undefined reference to `_sbrk'
collect2: error: ld returned 1 exit status
make: *** [.build/teensy31-5d575bc3/firmware.elf] Error 1
Make failed with code 2

@jpdoyle
Copy link
Author

jpdoyle commented Jun 15, 2014

It looks like you might have a weird libc.a in the arduino build system. My copy has an MD5 sum of 6cd4cd9e0c4a3f6c883085c7d46d9b81, so you can check it against yours. It also seems a little odd that your path has thumb2 in it. It's been a bit since I was really dug into the build system, but I remember having to disable something to do with thumb2. I'll see what I can figure out.

Edit: I was misremembering the thumb part, which makes things weirder. It's hard to debug a problem like this remotely, but I'll see if I can find anything that could narrow things down.

Edit 2.0: I found where _sbrk is defined. For me it's in /usr/share/arduino/hardware/teensy/cores/teensy3/mk20dx128.c -- do you have the teensy hardware files installed on your machine? My patch only works if you already have the teensy-specific build tools and libraries installed. Most of them are available at http://pjrc.com/teensy/teensyduino.html

@wilywampa
Copy link

I think I had the wrong arm-none-eabi-gcc in my path, so it seems to build successfully now. Now it's looking for teensy-loader-cli. The official page for the Teensy loader command line tool says that loading a Teensy 3.0 only works in Linux. Is there a different version of that tool that can be used on OS X?

Again, I tried some hackish things to make it work. I compiled this branch of teensi-loader-cli: stefanklug/teensy-loader-cli@0a2281f

Neither the OS X nor Linux branches support the mk20dx256 (Teensy 3.1), but I tried adding in support and I also tried uploading to a Teensy 3.0. Both of those attempts result in this message with no response when I press the button:

ino upload -d $ARDUINO_DIR -m teensy3
teensy3
Guessing serial port ... /dev/tty.usbmodem16771
Ready to upload... Press reboot button on teensy to continue

I was able to build and upload to the Teensy 3.1 using this project so I'll probably stick to that, although maybe I will use ino to compile and do my own thing for uploading the hex file.

@wilywampa
Copy link

Unfortunately even when I load the hex file manually using the GUI Teensy loader, it never gets into the compiled code (i.e. the light never turns on and it doesn't become visible over USB until after I press the button).

@jpdoyle
Copy link
Author

jpdoyle commented Jun 16, 2014

Thanks for linking me to that Teensy template project! It looks like it manually does a few things I trusted to teensy-loader-cli. I'll try during this week to incorporate that and make my patch more cross-platform. Does it get into the compiled code when you use the Teensy template, or does it fail like the gui loader? If not, I'll definitely try to mimic the Teensy template.

@wilywampa
Copy link

As far as I can tell, only the template is making a working hex file and I can only upload it sucessfully using the template's method (teensy_post_compile and teensy_reboot).

@wilywampa
Copy link

So I noticed the hex file I was getting from the template was 30-something kilobytes while the version from ino was ~1200 bytes. The template version was compiling all the stuff in the cores/teensy3 directory. It took awhile to track down but but I found that if I put just mk20dx128.c in with the .ino file, it compiles correctly. So I guess that file needs to be compiled for a Teensy sketch to work. I don't know how that fits in with your work.

@corny
Copy link

corny commented Jun 29, 2014

Any news on this issue? ino build works at my machine if use commit 60e2c4a and append the absolute path of arduino/hardware/tools/arm-none-eabi/bin to $PATH:

PATH=$PATH:$ARDUINO_DIR/hardware/tools/arm-none-eabi/bin ino build -d $ARDUINO_DIR -m teensy31 -v

@jpdoyle
Copy link
Author

jpdoyle commented Jun 29, 2014

Wilywampa, could you post your boards.txt for teensy? For me it's in /usr/share/arduino/hardware/teensy/. I've been looking through the template's upload procedure, but if your build requires manual intervention like that, you might also have a faulty build configuration file.

Corny, having to add things like that to PATH is expected behavior. Ino checks /usr/local/share/arduino/, /usr/share/arduino/, and (on OSX) /Applications/Arduino.app/Contents/Resources/Java/ -- if your arduino files are not in any of those, it assumes they're in PATH

@corny
Copy link

corny commented Jun 29, 2014

@Ginto8 After installing the Teensyduino package I expect that everything works out of the box. I have just modified the search path: corny@19d40b9

@wilywampa
Copy link

My teensy boards.txt is here: https://gist.github.com/wilywampa/0cce8b96f50a08feec57

@mgcrea
Copy link

mgcrea commented Jun 29, 2014

Using this current PR branch and cherry-picker corny/ino@19d40b9,

$ PATH=$PATH:$ARDUINO_DIR/hardware/tools/arm-none-eabi/bin ino build -d $ARDUINO_DIR -m teensy31 -v

Errors on teensy31 is not a valid board model.

I'm using stock Arduino.app 1.0.5 with TeensyDuino installed.

Any ideas?

@corny
Copy link

corny commented Jun 29, 2014

Does the output of ino list-models -d $ARDUINO_DIR include any tennsy models?
And did you install Ino with [sudo] make install?

@ivankravets
Copy link

Hey,

I can add support for Teensy to PlatformIO. Can anyone help me with testing?

What is interesting, PlatformIO Library Registry contains a few Teensy libs and you can install their with platformio lib install command and build your source code with it.

theJenix and others added 3 commits November 3, 2014 21:49
teensy_reboot (bundled with Teensyduino), as defined in the boards.txt.
Otherwise arm-none-eabi-gcc can not be found for Teensy models.
@theJenix
Copy link

theJenix commented Nov 4, 2014

Hi, I stumbled across this pull request; I use Ino extensively, and am in the middle of switching from Arduino to Teensy microcontrollers. I wanted to use this without installing teensy-upload-cli; Teensy 3.1 doesn't appear to be supported, and the Teensyduino package includes other, more general install scripts. I've implemented support in upload.py for these scripts in my fork: https://github.com/theJenix/ino/tree/master (specifically, it uses the post_compile_script and avrdude_wrapper values for the selected board, as loaded from the boards.txt file). This should extend this commit, and should work with all versions of the Teensy supported by Teensyduino.

What else needs to be done for this PR to be pulled in? I'd like to help wherever I can.

@jpdoyle
Copy link
Author

jpdoyle commented Nov 5, 2014

theJenix, your commits have been merged into the PR. Thanks for the contribution!

@ivankravets
Copy link

Dear friends,

I'm glad to inform you that latest version of PlatformIO has support for Teensy (2 & 3). You don't need to install any IDEs, tool chains or etc. PlatformIO works on the all popular OS, including card-size PC (Raspberry Pi, BeagleBone, CubieBoard and etc.).

See here Teensy example.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants