We will talk about the popular USB interface, namely how to use this very USB on simple AVR microcontrollers. I plan to write a few topics with code examples and explanations from both the microcontroller and the computer. Of course, the Internet is full of all sorts of examples on this topic, but at best, this is the source code in which the devil himself breaks a leg, but a short, per page, its description.
To begin with, previously widely used interfaces like COM, LPT, MIDI are now obsolete, but still often present on modern computers and used in industrial and highly specialized equipment. So, to connect some sort of your own piece of hardware with a computer, it’s already time to master something else. As an option, you can still use any converters / adapters / emulators, but they do not always work as the original interface, causing a lot of problems.
Everything is enough empty chatter, get down to business. How to use USB in your own devices?
- You can take a microcontroller that has hardware support for the USB interface (for example, AT90USB *). Next you need to know how to work with it and write a special firmware for it. And 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 in the "other" interface. RS232, I2C, can be as “other” ... In this situation, we don’t need to know how USB works, we don’t need to write a special firmware and driver for the computer. For us, all the work is done by the converter, and as a rule, the driver is already written by the manufacturer of the converter.
- Take the usual microcontroller without hardware support for USB and programmatically emulate the 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 generally keep quiet for USB 3.0. Therefore, at home on the knee, only LowSpeed ​​USB will turn out, and then with some difficulties. True, in most cases for homemade devices this is more than enough.
We are real Jedi, so let's take the emulation path. At the moment, there are already three ready-made projects for USB software emulation on AVR microcontrollers:
The project from Igor Češko was the first to be written entirely in assembler and it served as a kind of inspiration for V-USB. On its basis, a noteworthy
universal IR receiver for a computer was made , as well as many other projects. V-USB, in turn, is written in C, albeit using assembly code in places critical to performance and accuracy of emulation. USBtiny is derived from an earlier version of V-USB, it has fewer features, which makes it theoretically easier to understand.

I stopped at the implementation of the V-USB, I think these are the main advantages:
- V-USB is published on the principles of GNU General Public License Version 2, there is also a commercial license
- full USB 1.1 emulation of low-speed devices with the exception of communication error handling and electrical characteristics
- runs on almost all AVRs, you need at least 2 Kbytes 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 firmware for MK, I wrote C in AVR-Studio 4 + WinAVR, I developed a program for PC using Borland C ++ Builder 6.0 as the fastest and simplest option. Accordingly, these will be all future examples. In general, the choice of development tools is a very crucial step, but it is not necessary to organize a holivar about what is better than C or Assembler. I will say simply: these are only tools in our hands. You need to own all and use the one that is more convenient and correct for the goals set for yourself. Naturally in skillful hands any tool is effective.
You also need to separately say about the VID and PID. These are 16-bit numbers, with the help of which the operating system determines the devices and loads the necessary 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 pair of VID / PID (legally purchased at usb.org) is very warm for the soul. But what to do when you need to simultaneously connect multiple USB devices with the same VID / PID? It's okay, in addition to these VID / PID, each USB device has VENDOR_NAME and DEVICE_NAME identifiers, then in the examples I will show how to use it.
Some links on a subject:
In conclusion, I will write that all the conditions for creating USB 1.1 devices are available to everyone:
- cheap avr microcontrollers
- USB software emulation, select V-USB
- Free Vendor-ID and Product-ID (with V-USB)
- many examples for both the microcontroller and computer
Only one thing remains - the desire to understand! And then work wonders on penny microcontrollers for all the majors for envy.
')
I plan to write another topic, with the practical implementation of a running line that receives data via USB.