Thursday, June 13, 2013

Arduino, where is my hex?

As many Arduino users with a serious electronics background have found, arduino compiler hides things from us. One of them is the hex file output (=final code firmware file to be written in FLASH memory of target AVR microcontroller). It doesn't appear on the project folder but instead is hidden in a place far far away, in a windows folder where windows put temporary application data garbage. The path for this folder can be found if you enable the verbose output of the compiler and you check the last line of this output after compilation. But be warned, if you close the compiler, it will disappear.

When the arduino compiler starts compiling a sketch, it creates a folder named: buildXXXXXX.tmp in C:\Users\username\AppData\Local\Temp\ where "username" is your windows user login name.
In the buildXXXXXX.tmp folder it keeps all temporary files needed for the compiling. XXXXXX seems to be a random number.
So, the whole path for the hex files is something like this: C:\Users\username\AppData\Local\Temp\buildXXXXXX.tmp

 As an engineer with experience with normal compilers, I always keep regular backups of the source code and the compiled firmware in rar archives for whatever I build. This eliminates the future problems, when you switched to another computer, or your compiler license has expired, or the new version of the compiler creates different hex outputs that just behave differently if not working at all.

So, how do you dig out the hex file without the hassle of copy-pasting paths for every project? I'm happy you're asking... My solution is this batch file script:

CLS
C:
REM Locate the latest HEX files created by Arduino
REM and bring them here, in the current folder.
TIMEOUT /T 2
cd %UserProfile%\AppData\Local\Temp
dir build*.tmp /O-D /S /B > %~dp0loc.txt
set /p texte=< %~dp0loc.txt
copy %texte%\*.hex %~dp0*.*
del %~dp0loc.txt
TIMEOUT /T 30

What does this do? Well, you keep it in your sketch folder as a xxx.bat file and you run it (manually, sorry...) after compiling. It seeks for the latest compiled hex file in the depths of your computer and brings it to your sketch folder.
It works fine in my Win7 64bit machine. I have named mine as GiveMeMyHEX.bat
Normally you should see your hex in your sketch folder, named sketchname.cpp.hex

I normally would keep the eep file also, but another pitfall with arduino is that you cannot write stuff to AVR's EEPROM memory file, so it's pointless. You have to write code that stores actual eeprom data during first boot if you want to have any initial programmable values in your application. Of course your code must prevent altering of those initial data on second boot and on any other boot, unless you want to.

I guess people would like to know what every line does, huh?
Ok, here it is:

CLS //clears screen
C: //prompt goes to C drive
REM // just displays comments
TIMEOUT /T 2 //waits for 2 seconds, or skip it
cd %UserProfile%\AppData\Local\Temp //prompt goes to this path where %UserProfile% is an enviromental variable that is replaced by your win login name
dir build*.tmp /O-D /S /B > %~dp0loc.txt //creates a loc.txt file in the same folder where the batch file is (%~dp0 variable is the current path) with a list of all build*.tmp folder/files(* is a wild char), sorted by date(/O-D =more recent)
set /p texte=< %~dp0loc.txt //gets the first line of the text file which is the path for the most recent folder and puts it in a "texte" named variable as a string
copy %texte%\*.hex %~dp0*.* //copies the hex found under that path, into our current sketch folder
del %~dp0loc.txt //deletes the loc.txt file, we don't need it more
TIMEOUT /T 30 //waits for 30seconds for you to admire the marvels of ancient computing, or press any key to exit

Now, if only we could get it run automatically, just after the end of the compiling...

P.S.
If you want to learn more about batch files commands, open DOS prompt, command line tool (START->RUN->cmd.exe) and type help, following by your command.
e.g.
c:\> help dir