I have followed this tutorial on flashing my Arduino R3 to turn it into a "USB keyboard", but the article doesn't explain how the hex works, it's just for granted. Where can I learn about how to make my own hex file so that I can learn how to make my Arduino mimic any USB device?
4 Answers
The HEX file is simply a translated version of the AVR executable, which is in turn a translated version of AVR source code.
There are many tools available for turning (compiling) AVR source code into an AVR executable, including but not limited to the Arduino IDE, Ino, AVR-GCC, and Atmel Studio. Note that all four tools will compile the code the same way, since AVR-GCC is used under the hood by the other three.
avr-g++ -mmcu=atmega328 source.cc -o executable
Once you've compiled the code, you can use avr-objcopy to extract the appropriate sections from the executable into HEX files for programming into the flash and EEPROM.
avr-objcopy -j .text -j .data -O ihex executable executable.hex
You can then use AVRDUDE to upload the various HEX files to the target device.
avrdude -c arduino -P /dev/ttyUSB0 -b 19200 -p m328 -U flash:w:executable.hex:i
Note that the USB libraries in Arduino/Ino and AVR LibC are limited; you will need to use Atmel Studio if you want full USB capabilities.
- 17,733
- 1
- 28
- 32
There are plenty of sites out there that document how to make/build such.
Example: http://hunt.net.nz/users/darran/
or https://github.com/harlequin-tech/arduino-usb
GOOGLE: build usb keyboard 16u2
Please note that when attempting this on the Uno R3, the DFU is updating the code on the 16U2 and not the 328p.
I would suggest for novices to use the Leonardo. Where there are stock examples in the IDE for using the application directly as USB HID. Avoiding any alterations to either the boot loader or the USB to Serial co-processor chip (the 16u2). Making it much simpler.
- 2,523
- 14
- 13
It is possible, but it's kind of hard(especially if you want to mod both the 8u2 AND the 16u2. I'd recommend the Teensy(https://www.pjrc.com/teensy/index.html) I recently posted a question about a question similar to yours and after hearing about all the pitfall I decided to go with the Teensy. I has much richer USB/HID support by default (You can start on your project right away rather than trying to mod the UNO). Teensys are also relatively cheap ranging from $18 to $24.
By comparison, UNOs are $27.18 and are out of stock. To be fair both are probably cheaper used. This is probably moot since it seems you already have one. For more info, see my question (Feasability of flashing Arduino UNO r3 with teensy firmware for HID (keyboard) emulation purposes)
- 23
- 4
You don't need to do something magical. Here's how you can use the Arduino IDE's affiliates.
Once you compile or upload a sketch, the .hex file is automatically created and stored in your computer's drive that has the OS installed (I've found this on Windows). You can access it by navigating to the directory containing temporary files.
Typically, the path will be something like this: C: > Users > Admin > AppData > Local > Temp
Now you'll see (a lot of) directories with names that go like "build"... followed by a big number. One of these directories contains compiler-generated files of the code you're looking for. From this location, you can search (using Windows Explorer, for example) and identify the .hex file you're looking for by the date and time of last modification.
Once you've got your .hex file, you can upload it to your Arduino using Atmel FLIP after setting your Arduino's microcontroller to the Device Firmware Upgrade (DFU) mode. Atmel FLIP, an acronym for FLexible In-system Programmer, can be used very easily, thanks to its minimal UI.
Here's how to use FLIP:
- Go to File > Load HEX File... and select the file.
- Choose your microcontroller by selecting the first icon on the bar.
- Choose your programming interface from the small list.
I've used this with ATXmega that supports USB but it's not the same for all.
- 41
- 2