USB interface is the most popular interface for communication with peripheral devices. Before USB (Universal Serial Bus) we used RS-232C and UART module in microcontrollers to exchange data with computers. Actually it is difficult to find computer which is equipped with serial RS-232C interface, but all computers are equipped with USB 2.0 or USB 3.0 interface. If we are constructing any device with microcontroller it is good way to equip that construction with interface. Companies which produces microcontrollers support end users with protocol stacks. Protocol stacks contains sample codes for various interfaces, USB is one them. After analysis of sample code for USB from protocol stack I decided to write USB service for chosen microcontroller on my own. Family of microcontrollers which I know the most are PIC microcontrollers. I chose PIC18LF2550 microcontroller. This device has USB 2.0 interface on board. Writing firmware which will allow microcontroller to communicate with computer via USB is not simple but I found in that a lot of satisfaction.
Before I have started I had read USB 2.0 specification which is possible to download from USB organization website. Obviously I spent some time during code creating. During programming device and clearing mistakes in my code I have collected plenty of data. Most of collected information is about events on USB bus during enumeration process. Enumeration procedure could vary for various operation systems. Microsoft has placed informations about USB devices enumeration process for developers in article How does USB stack enumerate a device. Article about enumeration is useful. Additional very useful information about enumeration process I have found at Jan Axelson’s Lakeview Research website. Currently I have tested my USB device only on computers with Windows 7 and Windows XP operation systems. Firmware which I have created allows microcontroller to work as USB device in HID class. My USB device supports two endpoints. Every USB device has to support bidirectional Endpoint 0. My USB device supports also bidirectional Endpoint 1 which is used for data exchange with applications on computer. I have recorded events during enumeration process of my USB device. In USB specification term frame is defined For Full Speed and Low Speed USB device frame has period of 1[ms]. Period of one millisecond is very short period of time for human (not for processor). Because it is impossible for human register online events on USB bus during enumeration process I have created a variable in RAM with two hundred fifty indexes. Every time when SETUP token was received from USB Host my microcontroller has been registering number of transfer and standard USB request’s data. After installation of device was finished I have triggered writing collected data to microcontroller’s EEPROM by specially prepared microcontroller’s input. Because writing EEPROM takes specified period on time it couldn’t be done during USB device enumeration and also because writing EEPROM requires switching off interrupts in microcontroller. Whole program code for microcontroller was written in C language under Microchip’s MPLABX environment and Microchip C18 compiler. During device testing I have used two very helpful tools. One of them is HID Device Exerciser which is possible to download as HidTest.zip. Second tool was HIDterminal which is a part of MikroElektronika MicroC PRO environment.