Getting Started with V-USB and Atmel Studio 7

Hello again,
As the title of this post says, I’m going to talk about messing about with VUSB and Atmel Studio 7. With regards to the former, I was looking for a cheap/easy way to build USB devices. Having used the excellent Teensy 2.0 from PJRC and the equally as excellent LUFA stack by Dean Camera, I have to say it was a really smooth experience, especially as LUFA got adopted by Atmel and is now a downloadable extension for their Atmel Studio IDEs. However, building more permanent stuff with surface-mount chips while being the way of the future, is relatively difficult for me. Simply put, I have trouble soldering such fine-pitched devices so its really a personal thing. Besides, USB-capable AVRs are fairly more expensive than their DIP counterparts and we all know what that means..
In any case, V-USB is a software-based stack for building Low-speed USB devices with AVR chips. Rather than using the…regular…means (1 or more interrupt/bulk/isochronous Endpoints) data is mostly transferred using Control Requests over the Control Endpoint, as defined in the USB spec (if this is French to you, you might want to brush up on your USB-fu before going on…BeyondLogic has a nice guide here), although you can actually specify interrupt or bulk endpoints if you want. So as not to sound like a broken record, head on over to Objective Development’s V-USB page. In a nutshell, if you want to build a USB gizmo using a regular old ATMega or ATTiny, right this way sir/madam..
Joonas Philajaamaa has an excellent set of tutorials explaining how to get started and do awesome with V-USB, so I’d strongly suggest grokking those first. Fabio Baltieri has some major cool gizmos built with V-USB which are totally worth a look as well. By this time you should be all fired up and ready to go!
Both F. Baltieri and J. Philajamaa prefer to use the commandline to manage and compile their program sources, as well as upload (or is it download) binaries to their chips. While I get that (the command line is an elegant, elegant tool), Windows is my primary OS (I make no apologies for that) and for that reason I use Atmel Studio to write code for AVRs. What this essentially means is that I do not write Makefiles by hand, in favor of using Atmel Studio’s own project management system. I recently upgraded to Atmel Studio 7 which is their latest and greatest release thus far, 7 being a magical number and all that… Its not much different from 6.x, but it does seem more polished thus far. Fingers crossed.
So, I was trying to get started with V-USB while using Atmel Studio 7, and it was slightly more difficult than I thought (mostly because I wasn’t paying attention) so I thought I’d talk about how I went about doing just that. Before we go on, I want to point out that I am using a similar hardware setup to Joonas’, but I’m using an Atmega328P instead and running it at 16MHz, which is actually out of the manufacturer specifications regarding supply voltage vs clock frequency…so proceed at your own risk, or use a lower clock like 12MHz, which is within spec.
You’ll want to create a new project (GCC Executable Project), give it a name that won’t make you cringe the next time you open the IDE and select the chip you’ll be using. You’ll eventually end up within a main.c file where you may then proceed to write your own damn code. Go ahead and obtain the latest V-USB release available and unzip/untar that to some location on your computer. Next, in your “Solution Explorer” pane in Atmel Studio, right-click on the Project node like so:
And select the “Add –> Existing Item” option from the resulting context menu. When the dialog comes up, navigate to the location where you extracted the V-USB release, and within that location there should be a subfolder called “usbdrv”. Open that and add all the files therein with the exception of text files and the usbdrvasm.asm file. Once that’s done, you’ll need to rename the file called “usbconfig-prototype.h” to “usbconfig.h” – this is done by right-clicking the file’s node in the Solution Explorer pane and selecting the Rename option from the resulting context menu. As per Joonas’ post, you’ll need to edit that file to reflect the particulars of your own USB device.
Here comes the clincher: trying to compile even the most basic example (from Joonas’ post) failed with some ridiculous error about CRC rates, so I had to manually edit the usbconfig.h file and completely remove/comment out the #define for USB_CFG_CHECK_CRC, rather than #define’ing it as 0. Once that was fixed, it still did not compile! The new error was about non-CRC rates! To fix that, I had to edit the usbconfig.h file again and explicitly specify USB_CLK_KHZ to 16000 rather than leaving it as F_CPU/1000. Obviously this seems counterintuitive, but it is what it is. Returning the commented-out #define USB_CFG_CHECK_CRC didn’t seem to have any ill-effects, so it turns out that simply #define’ing USB_CLK_KHZ explicitly fixed the issue. Hence, to get V-USB to compile in Atmel Studio 7, all you need do is to explicitly #define USB_CLK_KHZ to an actual figure (your clock frequency / 1000) in the usbconfig.h file.
Making that change allowed me compile the V-USB sample, and when I connected the circuit as described it actually enumerated! The resulting device was called “Template” but that’s on account of me not modifying the usbconfig.h file properly to reflect a more apt name or description so that one’s on me. Knowing what you know now, you can now go forth and build an army of VUSB-based gizmos to take over the world!
Or something.


Popular posts from this blog

Enabling SPI1 on the Raspberry Pi B+/Zero/2/3

Bitbanging SPI on the Raspberry Pi (via spi-gpio)

Getting Started with Logic Analyzers and Pulseview