USB to AVR. Introduction
We will talk about the popular USB interface, namely how to plug this USB into simple AVR microcontrollers. I plan to write several topics with code examples and explanations both from the microcontroller and from the computer. Of course, the Internet is full of all sorts of examples on this topic, but at best it’s a source in which the devil’s leg will break, and a short one, on the page, its description.
To begin with, the previously widespread interfaces like COM, LPT, MIDI are currently outdated, but are still often present on modern computers and are used in industrial and highly specialized equipment. So for the communication of some kind of own piece of iron with a computer, it is high time to master something else. Alternatively, you can still use all sorts of converters / adapters / emulators, but they do not always work as the original interface, causing a lot of problems.
All is enough empty talk, let's get down to business. How to use USB in own devices?
- You can take a microcontroller that has hardware support for a USB interface (for example, AT90USB *). Next, you need to know how to work with it and write a special firmware for it. Well, lastly, you also need to write a driver for the computer, if your device is not a standard USB class.
- Use a universal USB converter to a "different" interface. The “other” can be RS232, I2C, ... In this situation, we do not need to know how USB works, we do not need to write special firmware and a driver for the computer. The converter does all the work for us, and as a rule the driver has already been written by the converter manufacturer.
- Take a conventional microcontroller without hardware USB support and programmatically emulate a USB interface. This raises a problem in the performance of our microcontroller. USB speed is very high: LowSpeed - 1.5Mbit / s, FullSpeed - 12Mbit / s, HighSpeed - 480Mbit / s. I'm generally silent for USB 3.0. Therefore, at home on the knee you get only LowSpeed USB, and then with some difficulties. True, in most cases, for self-made devices this is more than enough.
We are real Jedi, so we will follow the path of emulation. At the moment, there are already three ready-made projects for software emulation of USB microcontrollers AVR:
The project from Igor Češko was the first, written entirely in assembler, and it served as a kind of inspiration for V-USB. Based on it, a noteworthy universal IR receiver for a computer is made , as well as many other projects. V-USB, in turn, is written in C, although using assembler code in places critical to performance and accuracy of emulation. USBtiny is a derivative of an earlier version of V-USB, has fewer features and is theoretically easier to understand.

I settled on the implementation of V-USB, I think these are the main advantages:
- V-USB is published on the principles of the GNU General Public License Version 2, there is also a commercial license
- full emulation of USB 1.1 low-speed devices excluding communication error handling and electrical specifications
- It runs on almost all AVRs, you need a minimum of 2 kilobytes of flash, 128 bytes of RAM and a frequency of 12, 15, 16, 16.5 or 20 MHz.
- V-USB provides a free pair of identifiers (Vendor-ID and Product-ID)
- well-documented C code, easier to understand

In my experiments with USB, I wrote firmware for MK using C language on AVR-Studio 4 + WinAVR, I developed a PC program using Borland C ++ Builder 6.0 as the fastest and easiest option. Accordingly, all future examples will be. In general, the choice of development tools is a very crucial step, but you don’t need to arrange a holivar about what is better than C or Assembler. I will say simply: these are just tools in our hands. You need to own everything and use the one that is more convenient and correct for your goals. Naturally, in capable hands, any tool is effective.
You also need to say separately about the VID and PID. These are 16-bit numbers by which the operating system detects devices and loads the correct driver. In order to get a Vendor-ID you need to pay usb.org 2000 $. Interesting thoughts about the legality of using VID / PID can be found on the BSVi embedder page . The fact that V-USB provides a free VID / PID pair (legally purchased from usb.org) really warms the soul. But what to do when you need to simultaneously connect multiple USB devices with the same VID / PID? It's okay, besides these VID / PID, each USB device has VENDOR_NAME and DEVICE_NAME identifiers, then in the examples I will show how to use it.
A few links on a subject:
- www.usb.org/developers/docs - official USB documentation. Not easy to read, but the most complete.
- www.beyondlogic.org/usbnutshell/usb1.htm - A good overview of the important parts of the USB specification. It’s easier to smoke than an official ...
- www.lvr.com/usb.htm - many good USB related links
In conclusion, I’ll write that all the conditions for creating USB 1.1 devices are available to everyone:
- cheap microcontrollers AVR
- USB software emulation, choose V-USB
- free pair of Vendor-ID and Product-ID (together with V-USB)
- many examples for both the microcontroller and the computer
There is only one thing left - the desire to understand! And then work miracles on penny microcontrollers to all the majorities for envy.
I plan to write another topic, with the practical implementation of a ticker that receives data via USB.