Electronics ru
Advanced Search +


USB Generic HID Open Source Framework for Atmel AVR and Windows. Part 2 - ATmega32U4 Firmware, Demonstration Firmware, Windows Applications

Part 1 - Reference Hardware

ATmega32U4 Firmware

The reference firmware is an AVR Studio 5 project which includes both the framework code and the required parts of the LUFA stack. There are two independent firmware types the 'reference firmware' which provides the ability to test and verify the framework and the 'demonstration firmware' which provides a simple demonstration of how to use the framework to read and set peripherals connected to the AVR.

Please note that the provided firmware is not optimised for minimising code foot-print. It is recommended that all unnecessary code (commands, debug, board definitions, etc.) is removed for target application use. It's perfectly possible to run both LUFA and the framework library on ATmega8U devices if required,
Reference Firmware

The first is the 'reference firmware' which provides 6 USB commands. 5 of the commands act as both a demonstration of the framework and also as a regression test tool to ensure the framework is functioning correctly. It performs the following 5 operations:

  • Receive 1 packet from host, verify the packet contents
  • Receive 1 packet from host, verify and then reply with 1 packet
  • Receive 1 packet from host, verify and then reply with 128 packets
  • Receive 128 packets from host, verify and then reply with 1 packet
  • Receive 128 packets from host, verify and then reply with 128 packets

Since the USB Generic HID stack is configured to use 64 byte packets this means, in the most extreme test, the firmware receives 8 Kbytes from the host and replies with 8 Kbytes. The USB Generic HID protocol is very simple and (in essence) provides a mechanism for sending 64 byte packets to and from the host. The framework overlays this with both a command/data structure as well as a bulk send and receive mechanism for transferring larger amounts of data.

The 6th command provides a live debug stream over USB allowing you to receive debug from the firmware (on the host) as the code executes. The firmware provides a function to which you can send debug strings, the strings are stored in a fixed-length circular buffer and then copied to the host when the send debug command is received.

Since the RAM on-board the AVR is limited, and you can only process one command at a time, the debug mechanism is not suitable for very 'chatty' debug. However it provides a useful way to read live variables and watch the flow of execution when developing. If the firmware's debug buffer is too full for a new string it simply discards the string meaning that debug messages are delivered in a FIFO manner without partial strings.

As the debug functions consume RAM on the AVR you can decide to include them (or exclude them) at compile time using the 'USB_DEBUG' compiler symbol. This leaves the debug function calls in the code but does not compile the debug code or the debug buffer.
Demonstration Firmware

The demonstration firmware is simpler than the reference firmware and provides 2 commands (plus the debug collection command). The first command reads the current state of the buttons, LEDs and potentiometer and passes this information back to the host. The 2nd command allows the host to set the status of the LEDs. For the buttons and LEDs the firmware relies on the built in board driver functions of the LUFA stack (which should make it very portable between different reference boards, of which LUFA supports many). In addition there is some simple ADC code which reads the value of the potentiometer.

Although the reference firmware has more USB capabilities, the demonstration firmware shows a more practical application of the framework.

Windows Applications

To match the available firmwares there are also two applications provided with the framework. Both are designed to be compiled in C# using Visual Studio 2010 (you can download the 'express' version of this free-of-charge from Microsoft).

Reference Application

The reference application provides a Windows front-end allowing the user to trigger all 5 of the available USB commands as well as monitor the debug output. The application also provides automatic device attach and detach handling. When the user selects a test the application passes patterned data over to the USB device and verifies the patterned data it receives in reply. This provides both an example application using the framework and a simple regression test to verify the underlying Generic HID communication library.

WFF GenericHID Test Application 4 0.PNG

Demonstration Application

The demonstration application provides a Windows front-end which allows the user to interact with the hardware in real-time. It is possible to toggle on and off the LEDs, monitor the status of the switches and see a graphical representation of the potentiometer position:

WFF GenericHID Demo Application 4 0.PNG

The demonstration application provides a useful example of how the framework can be used to create interactive hardware controlled via USB.

Demonstration Video


Visual Studio C# 2010 projects and AVR Studio 5 projects - download

Part 3 – USB Generic HID C# Communication Library

Slices ↓
Radiolocman facebook Radiolocman twitter Radiolocman google plus