Building Arduino 1.0.1 Bootloader for Leostick

A tiny breakout board for the ATmega32U4 MCU, pre-loaded with a pre-release version of the Arduino Leonardo bootloader. This one is for the hackers! [Product info]

Building Arduino 1.0.1 Bootloader for Leostick

Postby adr1an » Wed May 23, 2012 11:42 am

EDIT 1/6/12: I've updated the leostick.zip file with my newer USBCore.cpp files etc from the other thread.

I've been playing around with using the new Caterina LUFA based bootloader from Arduino 1.0.1 . As noted in the other thread at viewtopic.php?f=27&t=660 it doesn't solve all the issues (Tone etc) but it makes a few things easier. Side effect is though that the Bootloader changed from "arduino" to "avr109", and will require the native Leonardo drivers as it resets the VID/PID to use the official Leonardo ones. Not a huge issue, but technically speaking, only official products from Arduino are allowed to run the Arduino VID/PID, so as a learning experience I set about re mapping it all back to be Freetronics. This meant recompiling the Bootloader and updating the Board profile.

EDIT: On reflection, I guess this means the best path forward maybe to stop confusion for end-users etc might be that similar to what I think Arduino are doing is using a new PID for the new bootloader and a new PID for the sketches - that way "old" bootloaders could work with old .inf and "new" bootloaders can automatically be detected using the newer .inf (among other things). This is a last minute thought before bed.. anyway..

The biggest thing that changes is that it now uses the two serial port setup like the official Arduino (a bootloader comport and a sketch comport) but the release drivers use two different PIDs for this. The original Freetronics driver from what I can tell uses just one PID? So taking lead from the Arduino team, dropped the leading digit from the PID used by the Bootloader makefile (making it 0x0151 ?) and have updated the Freetronics driver to use the new Arduino Leonardo .inf format and new VID/PIDs . I also noticed that the release Leonardo Board Profile uses lfuses of ff, where as the Leostick has de . This is to do with the bootloader delay I think? Anyway, I wanted to have the full Leonardo experience, but rename it back to Leostick, so went with ff.

I am posting this info for anyone else curious about how to do this. I did it and it works fine and would love some feedback on if there is anything I've done wrong/stupid - particularly the made up PID.

I'm attaching my arduino-sketches\hardware\LeoStick directory which I believe you can replace with your existing one and the below should all be done except for the Driver update (include as a separate attachment). But NONE of this is anything but my own hackish attempts - those unfamiliar should probably wait for guidance from Jon ! :) But for an interesting experience learning how this USB stuff works you can also do it from scratch yourself. Ensure your restart the Arduino-IDE if you just intend to use the files.

Remember the second comport will only appear once you load a sketch - reloading the compiled bootloader will leave it sitting forever at the Bootloader waiting for upload as there is no sketch loaded (The shipped 'bootloader' includes a sketch, hence its larger size)
leostick.zip
(104.17 KiB) Downloaded 362 times

LeoStick_Bootloader_driver_v2.zip
(1.21 KiB) Downloaded 413 times


To build the bootloader yourself and get the Freetronics Leostick names/drivers back you need to do the following under Win7:

1) download LUFA-111009 and unpack it into the same directory as your Arduino IDE (e.g, if in c:\arduino-1.0.1, place the LUFA-111009 directory in c:\LUFA-111009 as well). You need the previous release, not the latest: http://lufa-lib.googlecode.com/files/LUFA-111009.zip

1a) OPTIONAL - edit to \arduino-1.0.1\hardware\arduino\bootloaders\caterina\Descriptors.c - which sets the 'unknown' driver details which appear when plugging in to a new computer. The default Unknown for non Arduino boards is a little ugly personally, so I added an entry for Freetronics. Starting Line 196 through 215:
Code: Select all
const USB_Descriptor_String_t ProductString =
{
   #if DEVICE_PID == 0x0036
   .Header                 = {.Size = USB_STRING_LEN(16), .Type = DTYPE_String},
   .UnicodeString          = L"Arduino Leonardo"
   #elif DEVICE_PID == 0x0151
   .Header                 = {.Size = USB_STRING_LEN(20), .Type = DTYPE_String},
   .UnicodeString          = L"Freetronics LeoStick"
   #else
   .Header                 = {.Size = USB_STRING_LEN(12), .Type = DTYPE_String},
   .UnicodeString         = L"USB IO board"
   #endif
};

const USB_Descriptor_String_t ManufNameString =
{
   .Header               = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
   
   #if DEVICE_VID == 0x2341
   .UnicodeString         = L"Arduino LLC"
   #elif DEVICE_VID == 0x20A0
   .UnicodeString          = L"Freetronics"
   #else
   .UnicodeString         = L"Unknown    "
   #endif
};


2) Open a command shell, and run the following after making sure BASEDIR is set correctly. You need to compile from inside the \arduino-1.0.1\hardware\arduino\bootloaders\caterina folder or it wont work due to the way the Makefile is setup for LUFA. Im trying to keep this as compatible with Arduino-IDE/Leonardo as possible hence not altering the Makefile.
Code: Select all
set BASEDIR=c:\arduino-1.0.1\hardware
set DIRAVRUTIL=%BASEDIR%\tools\avr\utils\bin
set DIRAVRBIN=%BASEDIR%\tools\avr\bin
set DIRAVRAVR=%BASEDIR%\tools\avr\avr\bin
set DIRLIBEXEC=%BASEDIR%\tools\avr\libexec\gcc\avr\4.3.2
set OLDPATH=%PATH%
@path %DIRAVRUTIL%;%DIRAVRBIN%;%DIRAVRAVR%;%DIRLIBEXEC%;%PATH%
cd %BASEDIR%\arduino\bootloaders\caterina
%DIRAVRUTIL%\make.exe clean
%DIRAVRUTIL%\make.exe all VID=0x20A0 PID=0x0151
copy Caterina.hex Caterina-Leostick.hex
%DIRAVRUTIL%\make.exe clean
@path %OLDPATH%


3) Now inside your \arduino-sketches\hardware\LeoStick directory, create a new directory called bootloaders and copy the entire \arduino-1.0.1\bootloaders\caterina folder into it, so you have \arduino-sketches\hardware\LeoStick\bootloaders\caterina .
I got errors initially about pins_arduino.h, so I re copied the \arduino-1.0.1\hardware\arduino\cores to \arduino-sketches\hardware\leostick\cores .

4) Remove the old LeoStick_Bootloader_Driver and prepare a new one. To use the new avr109 based drivers/ports etc, I initially had problems. I ended up uninstalling and choosing delete the driver file as well? when removing the existing Freetronics Bootloader driver (remember to press reset to get to bot com ports if you already have the two comports installed) and then using a modified one from the \arduino-1.0.1\drivers\Arduino_Leonardo.inf file . All I did was update the Vendor details to be the Freetronics VID/PID and Details, and added the 0151 PID for the bootloader. My version is also attached for anyone brave (_v2.inf) .

The modified section from LeoStick_Bootloader_Driver_v1.inf is line 88 through 94 which I altered to:
Code: Select all
[DeviceList]
%DESCRIPTION%=DriverInstall, USB\VID_20A0&PID_0151
%DESCRIPTION%=DriverInstall, USB\VID_20A0&PID_4151&MI_00

[DeviceList.NTamd64]
%DESCRIPTION%=DriverInstall, USB\VID_20A0&PID_0151
%DESCRIPTION%=DriverInstall, USB\VID_20A0&PID_4151&MI_00


5) Modify the \arduino-sketches\hardware\LeoStick\boards.txt file. There was a few changes from the original boards.txt, primarily the inclusion of the VID/PID used by Sketches when compiling (so the USB Serial PID, 4151, Matches up) , the change to avr109 protocol, changed upload speed (now 57600), added upload.disable_flushing and I also have opted so far to go with the Leonardo lfuse of 0xff. Heres my updated boards.txt file
Code: Select all
################################
leostick.name=Freetronics LeoStick
leostick.upload.protocol=avr109
leostick.upload.maximum_size=28672
leostick.upload.speed=57600
leostick.upload.disable_flushing=true
leostick.bootloader.low_fuses=0xff
leostick.bootloader.high_fuses=0xd8
leostick.bootloader.extended_fuses=0xcb
leostick.bootloader.path=caterina
leostick.bootloader.file=Caterina-Leostick.hex
leostick.bootloader.unlock_bits=0x3F
leostick.bootloader.lock_bits=0x2F
leostick.build.mcu=atmega32u4
leostick.build.f_cpu=16000000L
leostick.build.vid=0x20A0
leostick.build.pid=0x4151
leostick.build.core=arduino
leostick.build.variant=leostick


6) Now you can burn the Bootloader via the Arduino-IDE or alternatively use the command line which I did. If you copy this bit, you'll need to change lots of things like the programmer, the directories and the COM ports. I am including purely for completeness! :) (I used a Buspirate to load it BTW)
Code: Select all
C:\arduino-1.0.1\hardware/tools/avr/bin/avrdude -CC:\arduino-1.0.1\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega32u4 -cbuspirate -P\\.\COM2 -e -Ulock:w:0x3F:m -Uefuse:w:0xcb:m -Uhfuse:w:0xd8:m -Ulfuse:w:0xff:m
C:\arduino-1.0.1\hardware/tools/avr/bin/avrdude -CC:\arduino-1.0.1\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega32u4 -cbuspirate -P\\.\COM2 -Uflash:w:C:\arduino-sketches\hardware\LeoStick\bootloaders\caterina\Caterina-Leostick.hex:i -Ulock:w:0x2F:m


7) Set the Arduino IDE to use the Freetronics Leostick Profile as before and done! You should now have a setup for the Leostick and still support a Leonardo simultaneously or any other reason you might want to have 1.0.1 bootloader, but Leostick VID/PIDs (almost).

8) Have Fun with your 1.0.1 Leonardo/LeoStick ! :) I tested this with the Leonardo example sketches and the Basic Fade/Blink sketches. All seemed to work ok.

Can someone please review the above and give me any comments/advice. Love to know if my blind attempts at doing this are remotely correct? (USB, WinDriver.inf newbie!)
Last edited by adr1an on Fri Jun 01, 2012 5:50 am, edited 18 times in total.
adr1an
 
Posts: 21
Joined: Tue Nov 01, 2011 10:04 am

Re: My Experience Building Arduino 1.0.1 Bootloader for Leos

Postby Fractal » Wed May 23, 2012 12:53 pm

That's pretty awesome!
I gave it a shot, on mac os x, by downloading the first file, and just uploading the bootloader.
It works fine, though sadly without the specific inf file, it's still "USB IO board "
Code: Select all
  Product ID:   0x4151
  Vendor ID:   0x20a0

is what is listed with that bootloader - the existing makefile does seem to assume that it should be 0x0151, so there's a little bug there.

Anyway, all seems to work otherwise, and I don't really have the skills or time to compile my own bootloader yet.
Thanks!

EDIT: I've had a quick once over the descriptors.c - at least one typo, presumably:

Code: Select all
   #if DEVICE_VID == 0x2341
   .UnicodeString         = L"Arduino LLC"
   #elif DEVICE_PID == 0x20A0  //This should be DEVICE_VID
   .UnicodeString          = L"Freetronics"


Maybe at some point I'll have a shot at correcting this, it is strange that the PID is incorrect...

EDIT#2: Yep, that did the trick. Turns out make is pretty easy to use (type "make all", wait), provided nothing goes wrong. Also, it's much easier for doing this quick hack, to change the makefile so you know where lufa is.
You'll want to change Desccriptors.c so this happens :
Code: Select all
const USB_Descriptor_String_t ProductString =
{
   #if DEVICE_PID == 0x0036
   .Header                 = {.Size = USB_STRING_LEN(16), .Type = DTYPE_String},
   .UnicodeString          = L"Arduino Leonardo"
   #elif DEVICE_PID == 0x0151
   .Header                 = {.Size = USB_STRING_LEN(20), .Type = DTYPE_String},
   .UnicodeString          = L"Freetronics LeoStick"
   #else
   .Header                 = {.Size = USB_STRING_LEN(12), .Type = DTYPE_String},
   .UnicodeString         = L"USB IO board"
   #endif
};

Otherwise you get a "Freetronics LeoS"
So, thanks for the super handy tutorial!
Last edited by Fractal on Wed May 23, 2012 1:19 pm, edited 1 time in total.
Fractal
 
Posts: 12
Joined: Mon May 21, 2012 10:45 pm

Re: My Experience Building Arduino 1.0.1 Bootloader for Leos

Postby adr1an » Wed May 23, 2012 1:09 pm

Cheers - Just fixed and have re uploaded the files again! Not sure where that PIDs gone astray though ?
adr1an
 
Posts: 21
Joined: Tue Nov 01, 2011 10:04 am

Re: My Experience Building Arduino 1.0.1 Bootloader for Leos

Postby Fractal » Wed May 23, 2012 1:21 pm

Ah, I'm a muppet. The wrong PID was for when the board had left the bootloader, and had started running sketches! (I still don't know how that gets assigned, but nevermind!)
Fractal
 
Posts: 12
Joined: Mon May 21, 2012 10:45 pm

Re: My Experience Building Arduino 1.0.1 Bootloader for Leos

Postby adr1an » Wed May 23, 2012 1:31 pm

Fractal wrote:Ah, I'm a muppet. The wrong PID was for when the board had left the bootloader, and had started running sketches! (I still don't know how that gets assigned, but nevermind!)


It comes out of the board.txt entry for the leostick under 'build.vid' - i added it to the new board.txt file. Basically - 0151 is used for the bootloader com port - and when no sketch is loaded it sits at the bootloader. 4151 is used for sketches, and is specified at build time via the board.txt file and used by the Arduino IDE (took me a while! at first I had LeoStick Bootloader but Arduino Leonardo Sketches! :) )

Also reuploaded the leostick.zip file which should fix the short charcter problems - thanx for the pick up! and reattached here

Btw - "make all" works, but if you haven't uncommented the PID/VID in the Makefile then make sure you do "make all VID=0x20A0 PID=0x0151" . You can skip it though if you uncomment the Makefile, but doing it on the command line preserves Leonardo compatibility as well
Attachments
leostick.zip
(102.35 KiB) Downloaded 174 times
Last edited by adr1an on Wed May 23, 2012 1:54 pm, edited 1 time in total.
adr1an
 
Posts: 21
Joined: Tue Nov 01, 2011 10:04 am

Re: My Experience Building Arduino 1.0.1 Bootloader for Leos

Postby Fractal » Wed May 23, 2012 1:46 pm

Hah, yes luckily the makefile had the new PIDs/VIDs uncommmented.

Next problem is the power - It's all fine running directly off a 500ma port, but plugging the stick into a 100ma max unpowered hub (my keyboard) leads to an error- the stick wants 500ma, but refuses to negotiate down, leading to an annoying OSX error message. This only applies when running the sketch.
If you have any insight, it would be useful, otherwise, I may just wait until someone solves it for when this comes up with the official Leonardo boards.

Glad to have been of some assistance!
Fractal
 
Posts: 12
Joined: Mon May 21, 2012 10:45 pm

Re: My Experience Building Arduino 1.0.1 Bootloader for Leos

Postby adr1an » Wed May 23, 2012 1:49 pm

ahead of me on that one! Will have a poke around and see - still coming to grips with LUFA! :)
adr1an
 
Posts: 21
Joined: Tue Nov 01, 2011 10:04 am

Re: My Experience Building Arduino 1.0.1 Bootloader for Leos

Postby Fractal » Wed May 23, 2012 2:10 pm

Hrm, it seems to be tucked away in USBcore.h, in the cores directory. I don't think smart negotiation will be trivial, shame. Nevertheless, it is a simple matter of changing a 500 to a 100, to solve this particular problem. No idea how to re-enumerate in a sketch though!
Fractal
 
Posts: 12
Joined: Mon May 21, 2012 10:45 pm

Re: My Experience Building Arduino 1.0.1 Bootloader for Leos

Postby adr1an » Wed May 23, 2012 2:16 pm

Fractal wrote:Hrm, it seems to be tucked away in USBcore.h, in the cores directory. I don't think smart negotiation will be trivial, shame. Nevertheless, it is a simple matter of changing a 500 to a 100, to solve this particular problem. No idea how to re-enumerate in a sketch though!


nice find - I'm distracted with another issue for a bit - but will have a look. You have me intrigued now :)
adr1an
 
Posts: 21
Joined: Tue Nov 01, 2011 10:04 am

Re: Building Arduino 1.0.1 Bootloader for Leostick

Postby adr1an » Wed May 30, 2012 5:14 am

Best solution I have so far is two different Board definitions.

EDIT: Check viewtopic.php?f=27&t=679 for a solution
Last edited by adr1an on Fri Jun 01, 2012 5:19 am, edited 1 time in total.
adr1an
 
Posts: 21
Joined: Tue Nov 01, 2011 10:04 am

Next

Return to LeoStick

Who is online

Users browsing this forum: No registered users and 3 guests