Not all of the projects that we build here at MCQN Ltd connect to the Internet, even if it feels that way at times. Occasionally it's useful to be able to connect some simple sensors or physical inputs up to a computer. Doing so with an Arduino and USB is fairly easy, but seems overkill at times, not to mention potentially expensive if you're going to build lots of devices and don't need the extra grunt. It's also nice not to need any extra drivers installed on the PC, which is possible with the Arduino UNO boards if you reflash the USB->Serial chip, but if you're going to do that we thought it was worth playing around with (something similar to) the USB->Serial chip on its own.

Farnell very generously offered to let us have one of their AT90USBKEY2 development boards to try that out, as long as we wrote up our experiences and gave them a mention.

The AT90USBKEY2 is a small PCB with an Atmel AT90U microcontroller on it, plus a few extras to make experimenting with it easier - there's a five-way joystick, a temperature sensor, and a button for inputs, plus some flash memory meaning it can show up as a USB flash drive as well as a standard keyboard, mouse or other USB device. We're wondering if that would make it a simple, self-contained module that comes with documentation or configuration information on the flash-drive side of things, while then allowing custom circuitry to masquerade as a keyboard or similar.

Anyway, that's for future experimentation - the first step is seeing if we can build some code to run on the board.

We eschewed the standard Atmel AVR development environment, as we wanted to try the open source LUFA USB stack. It's a well-written module which is used in projects like the Arduino, and there's a wealth of different example USB devices on offer - keyboards, mice, MIDI interfaces... all sorts.

Getting it up and running on Ubuntu 11.04 turned out to be pretty painless.

First off you need to install the tools required to compile things for AVR chips, and the programmer used to flash the devices:

$ sudo apt-get install gcc-avr binutils-avr avr-libc dfu-programmer

Then download the latest version of the LUFA code (there may well be a more recent one available by the time you read this - check this page to find the download link)

$ wget http://lufa-lib.googlecode.com/files/LUFA-120219.zip

Unzip the download and then change into the directory for one of the examples to get something easy to build

$ unzip LUFA-120219.zip
$ cd LUFA-120219/Demos/Device
$ make

That will go off and build all sorts of LUFA goodness. Once it's done, assuming there are no errors, you should have an assortment of demo firmwares to try out on your dev board. A quick find will flush them out:

$ find ./ -name *.hex

Which should give you output something like this:

./ClassDriver/DualVirtualSerial/DualVirtualSerial.hex
./ClassDriver/Joystick/Joystick.hex
./ClassDriver/VirtualSerial/VirtualSerial.hex
./ClassDriver/AudioOutput/AudioOutput.hex
./ClassDriver/MIDI/MIDI.hex
./ClassDriver/Keyboard/Keyboard.hex
./ClassDriver/Mouse/Mouse.hex
./ClassDriver/AudioInput/AudioInput.hex
./ClassDriver/GenericHID/GenericHID.hex
./ClassDriver/MassStorage/MassStorage.hex
./ClassDriver/MassStorageKeyboard/MassStorageKeyboard.hex
./ClassDriver/VirtualSerialMouse/VirtualSerialMouse.hex
./ClassDriver/KeyboardMouse/KeyboardMouse.hex
./ClassDriver/RNDISEthernet/RNDISEthernet.hex
./ClassDriver/VirtualSerialMassStorage/VirtualSerialMassStorage.hex
./ClassDriver/KeyboardMouseMultiReport/KeyboardMouseMultiReport.hex
./LowLevel/DualVirtualSerial/DualVirtualSerial.hex
./LowLevel/Joystick/Joystick.hex
./LowLevel/VirtualSerial/VirtualSerial.hex
./LowLevel/AudioOutput/AudioOutput.hex
./LowLevel/MIDI/MIDI.hex
./LowLevel/Keyboard/Keyboard.hex
./LowLevel/Mouse/Mouse.hex
./LowLevel/AudioInput/AudioInput.hex
./LowLevel/GenericHID/GenericHID.hex
./LowLevel/MassStorage/MassStorage.hex
./LowLevel/KeyboardMouse/KeyboardMouse.hex
./LowLevel/RNDISEthernet/RNDISEthernet.hex

The next step is getting one of the firmewares uploaded to the development board.

When you plug the development board in, it should show up in the list of USB devices when you run lsusb:

$ lsusb
Bus 002 Device 007: ID 03eb:2022 Atmel Corp. at90usbkey sample firmware (composite device)
...

To program the board you need to put it into DFU mode. That's a simple case of pressing both the "RST" and "HWB" push buttons, then releasing first the "RST" push button and then the "HWB" push button.

Next, use dfu-programmer to upload the new firmware. In the example below we'll program it with the demo keyboard code (note that you have to run dfu-programmer with sudo, otherwise it will complain that there's no device present):

$ sudo dfu-programmer at90usb1287 erase
$ sudo dfu-programmer at90usb1287 flash ClassDriver/Keyboard/Keyboard.hex
$ sudo dfu-programmer at90usb1287 reset

And finally hit the "RST" button on the board to reboot it into the new firmware. You should now be able to see it listed differently in the list of USB devices:

$ lsusb
Bus 002 Device 008: ID 03eb:2042 Atmel Corp. LUFA Keyboard Demo Application
...

(Note: If the device shows up as "Atmel Corp. at90usb AVR DFU bootloader" then you've forgotten to reset the board and it's still in DFU mode)

Now clicking the five-way joystick or pressing the "HWB" button should result in characters between "A" and "F" being typed into whichever window has focus, depending on which button or direction you pressed.

Uploading a different firmware just requires you to specify a different .hex file in the flash step of the DFU programming process. Now you're ready to go off and modify the LUFA code to build your cool new application.