Programming USB Peripheral in Microcontrollers:
Introduction:
USB stands for "Universal Serial Bus", as the name suggests its a communication bus that can be used for the tranfer any data between different embedded systems like PCs, Cell Phones, Cameras etc.If we talk about history we would see that the USB Protocol is a revolutionised version of different communication protocols like Serial Transmission(UART-USART) or Parallel transmission using parallel port etc. All of these communication protocols had some drawbacks and limitations e.g Speed, ability to host a device, supporting multiple devices at the same time..., so a univesal protocol had to be developed that can handle all this limits or conflicts and that universal protcol is called USB.
Architecure:
USb 2.0 ports come indifferent configurations as shown in fig.1, all of which have 4 pins but the size is different you can choose any one of them depending on the need of your embedded system.![]() |
Figure 1 |
Standard USB 2.0 port has 4 lines, the outer two are for power V+(5v) and V-(GND) respectively and the central two are for the transmission of data as shown in figure 2. Usb 2.0 can also be used to draw 500mA Max at 5v to power some device, making the USB devices self-powered.
![]() |
Figure 2 |
Technical Details:
Now lets talk about some technical aspects of the USB 2.0 Protocol. Like any other digital communication USB uses digital signals for its communication .i.e 1's and 0's. In the sipmplest digital communication 1 is represented as some higher voltage level and 0 is represented by a ground (unipolar) or some negative voltage (bipolar). But USB uses a different bit encoding scheme called uniploar NRZI.![]() |
Figure 3 |
In NRZI(Non Return to Zero Inverted) signal is not toggled between higher level to lower level with 1's and 0's but but signal is inverted only when a 1 is encounterd, if there is a contineous stream of ones the NRZI signal will be constantly toggling, in case when a 0 is encounterd the signal is not inverted it is kept the same, just look at the figure.3 on right side it will make things clear.
In USB the NRZI scheme is implemented by connecting the pull-up resistors to both pins.
Note: data is sent in usb D+ and D- lines both differentialy .i.e if there is a 1 on D+ there will be a 0 on D-.
So a differential 1 is sent by transmitting >2.8v on D+ and <0.3v on D- line with 15k pull-up resistors, similarly a differential 0 is sent by transmitting >2.8 v on D- and <0.3v on D+ with 15k pull-up resistors.
The USB is driven into suspend mode when there is no activity on the data lines for greater then ~3ms to limit current. The USB Protocol then waits 7ms more if still there is no activity it shuts down the device.
USB Protocols:
In USB the data is tranfered in terms of packets. A Packet is a set of predefined no. bits that are allowed to travel all togathe one by one.The USB Protocols are quite complex if we study them from the roots, since this tutorial is focused on developing USB Systems on microcontrolers which have a built in USB peripheral so we will discuss the the upper layer protools as lower level protocols are handled by microcintrollers itself.
Now lets see, each USB data transaction consits of certain packets for successfull trasnsaction, they are as follows:
- Token Packet: is sent by the host to device to tell the device what host expects to follow, it also tells the device about the last transaction. It has furthur sub-types like IN,OUT etc.
- Data Packet (optional): contains the data or payload that has to be send, it can be optional depending on the availaibilty or tranfer type discussed later.
- Status Packet: is used to acknowledge transaction and to provide a means of error corretion.
- Hand-Shake Packet: is used to indicate the succefull tranfer of bit stream.
Now that we have briefly discussed the typed of Packets. As each of these packets have different purpose but the field architecure of every packet is same. A packet consists of total 6 fields as follows:
- Sync Field: is 8-bits wide for low and full speed USB modes while for High speed USB mode its's 32-bits wide. It is used to synchronize the clock between the transmitter and reciever.
- PID (Packet ID) : is used to identify the type of packet as discussed earlier. e.g. 0001=OUT Token and 1001=IN Token. Actually thee are 4 bits of PID but are sent as 8 rest of the 4 being the complement of the other.
- ADDR : is Device Addressfield it's 7-bits wide. So clearly it can support 127 devices (Note: 0 is not a valid address).
- ENDP : or End Point Packet is 4-bits wide can support a total of 16 endpoints.
- CRC : this field is 5-bits wide and is used for Cyclic Redundancy Check for error correction.
- EOP : or End Of Packet, is indicated by a single ended 0.
END Points :
In simple words End points are source or sink of data. The Device or the Host both read and write data through these endpoints. Both have a set of both IN and OUT Enspoints. Let's say Host wants to send some data to the device at a specific address, it will send the packet to the End point-OUT and will wait untill the line is free, as soon as the line gets free it sends data to the device where it gets stored into the End point-IN of the device. So that the device can read at any time and vice versa.Pipes: are actually the logical paths or connections between the host and the End points or the device and its End points. They have two furhur types:
- Stream Pipe
- Message Pipe
Descriptors :
All the things that we covered before comes in somewhat low level protocols that are handeld by Microcontrollers itself e.g PIC18F4550. Now we will talk about the most important topic for designing USB devices on microcontrollers DESCRIPTORS.Descriptors as the name suggests describe something in this case the device. There are a large no. of Desriptors that are used to describe every thing about the device to the Host.
- Device Descriptor: has furthur 4 subfields, it has a total size of 18 byte.
- Configuration Descriptor : has 8 subfields in a total of 9 bytes.
- Interface Descriptor : has 8 subfields in total of 9 bytes. It is used to provide information about a function or feature that device implements.
- End Point Descriptors : is 7 bytes wide is used to provide inormation about the Devies's End points to the Host.
For Furthur dtails on these decrioptors or there sub-fields visit the book:
good tutorial
ReplyDeleteI’m really impressed with your article, such great & usefull knowledge you mentioned here
ReplyDeleteKik account
AwardSpace is a blazing fast, secure and feature rich web hosting provider. They have all the tools and plugins available on their console which you need to build a website. Hence, it is a complete web hosting package which you can buy during awardspace black friday sale
ReplyDeleteNice information, this is good blog... if you are searching a avira antivirus technical support number then contacts us:-
ReplyDeleteavira antivirus support
avira customer support
https://technical-and-technological.blogspot.com/2010/10/ibms-storwize-v7000-is-disk-array-and.html?showComment=1606984279145#c6245598107776245209
ReplyDelete
ReplyDeleteSatta Matka may function as the top-notch taste for-you personally from unless you might receive yourself several Kalyan matka bajar.