Go Down

Topic: How to program ATmega32U4 on Yun with a HEX file instead of source code? (Read 4517 times) previous topic - next topic

nickyang

Hi,

During the software development, I use Arduino IDE to compile and upload software to Atmel ATmega32U4 chip on Arduino Yun WiFi board. It works perfectly when I develop the software.

However, once the software development is done, I don't want to just give source code to other people to program the production board because the boards will be programmed outside my company. I am wondering if there is a way to program a HEX file to ATmega32U4 chip via its bootloader.

Thanks,

Nick


JosAH

On the Linux side of your Yun, there's a /usr/bin/run-avrdude script that can program your 32U4 given just an intel .hex file.

kind regards,

Jos

ShapeShifter

There are three ways to do it:

Using USB: When uploading a sketch with the Arduino IDE, it first compiles the sketch into a .hex file, and then runs avrdude to upload it to the Yun. Go into the FILE --> Preferences dialog of the IDE, and under "Show verbose output during" check both compilation and upload. Then build and upload a sketch. You will see a lot of extra information that explains what the IDE is doing. Looking near the end of the white text (the compiler output) just above the lines where it says how much memory is being used, there is the line where the hex file is being created: it will show you the full path to the final hex file (this path will change often, so you do need to look carefully and often.) Then, the last white line is the (rather long) command to run avrdude to do the actual upload (the orange text is the output of avrdude.) Once you have your hex file, you should be able to run that command manually to use avrdude to program your boards.

Using Network GUI: First, find your hex file as described above. Then, log into your Yun's web configuration pages, and at the bottom of the main page is a section to upload a sketch. Browse for the hex file, hit the upload button, and the sketch should be programmed.

Using Network Manually: (as JoshAH's mentions) First, find your hex file as described above, then get the file onto the Yun's Linux file system. You can use a program like SCP, or copy it to an SD card and then insert the SD card into the Yun, or any other method. Then, start an SSH session to get to the Linux command line, and enter these commands: (substitute the actual path to your hex file in place of "hex_file_name"
Code: [Select]
merge-sketch-with-bootloader.lua hex_file_name
run_avrdude hex_file_name

The first line will add the bootloader code to your compiled hex file - you want to do this once and only once for each hex file you copy to the Yun. Then, the second line will actually program the hex file into the '32U4 chip. You can run that last one as needed. If you skip the first command, the '32U4 will not have a bootloader, and you will no longer be able to program the '32U4 using the USB cable. If you run the first command multiple times, you will add multiple copies of the bootloader image, making the file larger and possibly causing to not run.

nickyang

Thanks for your kind replies, ShapeShifter and JosAH.

I am trying to use the first way (Using USB) to update the firmware. I got a problem.

I found where the HEX code is located and the avrdude command to program the flash. Please see the message in the code section below.

However, before the avrdude command, there is an extra step "Forcing reset using 1200bps open/close on port COM11". I know that my board connects to PC at COM11 (when my software runs, I open port 11 and I can see the message in the terminal window). However, it seems that COM12 is used when avrdude programs the flash.

If I use the same avrdude command (with COM12), it doesn't work (error message: avrdude: ser_open(): can't open device COM12).

Do you know how to get around this issue?


Another question:

Instead of installing the whole/big package of Arduino IDE to program flash with avrdude, I would like just copy avrdude.exe and avrdude.conf, my firmware and a batch file (the avrdude command)  into a package. Then I send this package to my customer and he just runs the batch file. Will it work?

Thank you very much,

Nick


Code: [Select]

... ...
Sketch uses 15,534 bytes (54%) of program storage space. Maximum is 28,672 bytes.
Global variables use 547 bytes (21%) of dynamic memory, leaving 2,013 bytes for local variables. Maximum is 2,560 bytes.

Forcing reset using 1200bps open/close on port COM11
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {COM11, } => {}
PORTS {COM11, } / {} => {}
PORTS {} / {COM12, } => {COM12, }
Found upload port: COM12

C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avrdude -CC:\Program Files (x86)\Arduino/hardware/tools/avr/etc/avrdude.conf -v -patmega32u4 -cavr109 -PCOM12 -b57600 -D -Uflash:w:C:\Users\Nick\AppData\Local\Temp\build1794757003189142693.tmp/FanControl.cpp.hex:i
... ...

ShapeShifter

However, before the avrdude command, there is an extra step "Forcing reset using 1200bps open/close on port COM11". I know that my board connects to PC at COM11 (when my software runs, I open port 11 and I can see the message in the terminal window). However, it seems that COM12 is used when avrdude programs the flash.

If I use the same avrdude command (with COM12), it doesn't work (error message: avrdude: ser_open(): can't open device COM12).

Do you know how to get around this issue?
The Yun has two modes: standard serial mode, and bootloader mode. They will show up as two different COMx port numbers, as they get different device drivers loaded for the two modes. The Yun is normally in serial port mode, which in your case is COM11. Then, just before running avrdude, the IDE does a real quick open of COM11 at 1200 baud, then closes it. This causes the Yun to reset into bootloader mode. COM11 goes away, and the bootloader starts up. On your system, the bootloader appears as COM12, and that is what avrdude uses.

You could do the same thing by using a terminal emulator to open COM11 at 1200 baud then close it, and then quickly start avrdude on COM12. Or, you may be able to do the same thing by pressing the Yun's 32U4 RST button, and then quickly trying to run avrdude.


Quote
Instead of installing the whole/big package of Arduino IDE to program flash with avrdude, I would like just copy avrdude.exe and avrdude.conf, my firmware and a batch file (the avrdude command)  into a package. Then I send this package to my customer and he just runs the batch file. Will it work?
It's worth a try. You should also be able to find avrdude as a download all by itself, independent of the Arduino IDE.


But before you spend a lot of time on it, consider the network option. If you customer is going to be able to get to the Yun's configuration web pages, then it's much easier to just use the upload hex file option on that first page you get to after you log in.

nickyang

Hi, ShapeShifter.

Thanks again for your reply. Your information definitely helps. Here are what I got:

1. I understand Web GUI is much easier. However, I am actually using Arduino Yun Mini board from arduino.org because it offers much smaller form factor than Arduino Yun board. I cannot find the section in Web GUI to update ATmega32U4 firmware. Maybe its web is different from Arduino Yun board's GUI (I don't have Yun board). Hence I cannot use Web GUI.

2. In order to put ATmega32U4 into bootloader mode, opening/closing the COM port at 1200bps does the trick. Just rebooting ATmega32U4 doesn't put it to bootloader mode. Once it's in bootloader mode, the avrdude command updates the firmware without problem.

3. I don't like 2 steps (open/close COM port at 1200bps and run avrdude command) to update the firmware. In the end, I wrote a PowerShell script to put 2 steps in 1 file and run only one command. It works.

4. I created a small package by copying avrdude.exe, avrdude.conf, libusb0.dll (it's needed), my powershell script, a batch file (command to run the script) and my firmware together. I copied the package into another PC without Arduino IDE installed. It works. For now, this solution works for me.

Thanks again for your kind help.

Nick

MattFL

@ShapeShifter, thank you very very much for your explanation of how the Yun bootloader works, I have been tearing my hair out trying to figure out why I couldn't program my Yun with avrdude, and why the Arduino IDE was using the next COM port up.  In case this helps someone else, on Windows you can do this (my Yun is on COM21):

mode COM21:1200,N,8,1,P
<wait a couple of seconds>
avrdude.exe -C "C:\path\to\avrdude.conf" -v -p m32u4 -c avr109 -P COM22 -U flash:w:"c:\path\to\myprog.hex":i

And it works!

If you want to put this all in one line to execute as one command then do the following. To put into a batch file, just separate the 3 onto separate lines: 

mode COM21:1200,N,8,1,P && waitfor /T 2 pause 2>nul || avrdude.exe -C "C:\path\to\avrdude.conf" -v -p m32u4 -c avr109 -P COM22 -U flash:w:"c:\path\to\myprog.hex":i

waitfor will wait 2 seconds, then return an error. Use 2>nul to dump the error text, and the || so execution continues even though there was an error.

Go Up