7

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?

  • Welcome to Arduino SE! You're really vague here. USB devices are a really complex topic, and most likely, you won't be able to make your own files for USB without a bit of skill. You can find HEX files for the USB chip to mimic other devices pre-compiled. – Anonymous Penguin Jun 5 '14 at 2:27
  • @AnnonomusPenguin Hi! I don't think any hex to imitate a DataLocker or IronKey device exist yet, so I'd like to try and do it. xD Any advice on where to start? – trusktr Jun 5 '14 at 16:27
  • I don't really have any idea :D I'd say maybe look at some open source hex files that incorporate USB mass storage (I've seen a couple of open source projects with at least thirty types of USB devices for Arduino) and modify it so it doesn't show files until there is a text file with the password on the root of the device. The IRC would be a good route to get help with this. So you don't make people mad, try to learn how to do it, not just copy & paste. Also, if you do, make sure to share your code with others. – Anonymous Penguin Jun 5 '14 at 17:12
  • The very tutorial you are following contains links to a gzipped tar archive of the source code and makefile used to produce Darran's keyboard hex file which you loaded: hunt.net.nz/users/darran/weblog/b3029/attachments/bd341/… – Chris Stratton Jun 5 '14 at 18:55
2

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.

2

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.

1

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)

1

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:

  1. Go to File > Load HEX File... and select the file.
  2. Choose your microcontroller by selecting the first icon on the bar.
  3. 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.

  • Based on your answer, you are generating the hex files only through the use of Arduino IDE, right? Can you use Arduino IDE to, for example, write USB Human Interface Device (HID) code? – trusktr Jun 12 '14 at 18:26
  • That's right, nothing other than the IDE is required. Assuming your HID is compatible with the Arduino IDE, you can. If it requires an add-on (like a Crumbuino or a Teensy, for example), it will still do the same thing. That's because it uploads the .hex file the same way. The only thing that add-on does is define custom features. You just have to find the .hex file among the temporary files. If the HID isn't compatible, you can try writing the code on Atmel Studio (only for AVR and ARM) with an appropriate compiler that can build the .hex file. – LIGHThouse Jun 13 '14 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.