Fun and Easy USB – How the USB Protocol Works


Hey Guys,so in this lecture we’re gonna be talking about USB. Now, USB is an Asynchronous Serial communication protocol and it stands for “Universal Serial Bus”. So, before USB there were just too many different port types. There was serial port, parallel port, PS2, etc. And the use imposed limitation such as not being hot plugable. This means that you have to shutdown your PC first, connect a device and then boot it up in order to use it. Quite monotonous, right? And then also, it does not allow for automatic configuration, So it means that you have to manually install the drivers. So they have very limited implementation, due to the requirements, in terms of non-sharable IRQ lines. – IRQ stands for Interrupt ReQuest lines, as well as IO (or Input/Output) address space -. So connectors like the parallel port had way too many connectors and took up significant port space on the motherboard. This made things really expensive, if you wanted to implement multiple peripherals. So, in order to solve this problem, all the tech and software giants came together to create a magical standard called “USB” and this happened around in 1994. So as you can see, we have Compaq, DEC, IBM Nortel, NEC, intel and Microsoft. and thus USB was born. So the main reason that USB (or Universal Serial Bus) was implemented, was to provide a replacement for those legacy ports on your computer, as well as to make the addition of peripheral devices quick and easy for the end user. You can connect literally any type of device, from printers, to mass storage, to webcams, to MP3 players, cell phones, keyboard and mice. And if that is not magical, I don’t know what else is So let’s take a look at USB speed. So USB 1.0 can throttle between low speed and full speed, which means it can throttle between 1.5 Mbps to around 12 Mbps. Then if you go a step higher, to USB 2.0, which allows high speed of 480 Mbps. Now if you go to 3.0, you are allowed 5Gbps; and USB 3.1 can go up to a whopping 10Gbps This is called Super Speed Plus (SS+) Let’s take a look at the USB architecture. Now, USB is an Asynchronous Serial protocol which means that there is no dedicated Clock line. And, if we look closer at USB: So the USB system is made up of host and a multiple number of USB ports. And multiple peripheral devices connected to a tiered star topology. To expand the number of USB ports, the USB hubs can be included in the tiers, allowing for branching into tree structures with up to five tier levels! So the tiered star topology has some benefits. Firstly, the power of each device can be monitored, and even switched off if an over current condition occurs. And all this happens without disrupting other USB devices. So both high, full and low speed devices can be supported with the hub filtering out the high speed and full speed transactions. This is so low speed devices do not receive them. USB is actually and addressable bus system with up to 7-bit address code. This means it gives support to up to 127 devices, on nodes, at once. The all zero code (0b0000000) is not a valid address. However, it can have only one host: the PC itself. So PC with peripherals connected via the USB forms a star local area network. If you look at the advantages of USB… so USB is hot pluggable. As mentioned before, you can plug in your USB to your computer without having to shutdown or restart your computer. USB is also low cost and allows for low cost devices. For most of the devices, you don’t need a power supply; and the current that’s supplied from USB is around 100mA to 500mA. There is also robust error detection. So if you look at the USB host and hub, they manage the power by enabling and disabling power to individual devices to electrically remove ill-behaved peripherals from the system. Further, they can instruct devices to enter a suspend state which reduces the maximum power consumption to 500uA. So these are all advantages of USB Now, if you look at the disadvantages of USB: So USB is a very complex protocol to implement compared to UART, i2c and SPI. Also, if you implement your own device, fees may apply. There was also hardware problems in Legacy devices. So the USB flash used to stop working if you pulled it out, without safely removing the device. but all this has been solved with current devices. So let’s take a look at USB signaling: So, we got our USB standard A, that you’re all familiar with As well as the USB’s B standard. So, this consists of our Vcc or power… we have our ground – which is the minus – and then we have our data lines, which are differential signals, which are called D+ and D- Notice that the power lines are longer and, thus get connected to the device first, before the data lines. So connecting a USB device to a computer is very simple, as you all know. You find the USB connector on the back or front of your machine and plug in the USB connector into it. If it is a new device, the Operating System autodetects it and asks for the driver disk. If the device has already been installed, the computer activates it and starts talking to it. So the USB standard, specifies 2 kinds of cables and connectors: The USB cable will usually have an A connector on the one end and a B connector on the other. That means: the USB devices will have an A connection on it, if not the device has a socket on it that accepts a USB B connector. Now you may be asking yourself in confusion: Why not just have one connector? Well, this was to avoid confusion between the host and the device. So A connector head means upstream (toward the computer) and B connector means downstream (towards the individual devices). If we look at the USB pull up resistors… so, USB devices pulls one of the data lines High, with a 1.5kohm resistor. This overpowers one of the pull-down resistors in the host, and leaves the data lines in an idle state called J. The choice of data lines indicates a device speed support, so full speed devices pull D+ high while low speed devices pull D- high as you can see over here. So in fact the data is transmitted by toggling the data lines between the J state and the opposite
case state. If we go back to USB signaling… As we mentioned, we got the J and the K state So serial data is set along the USB in differential or pushable mode. But the opposite polarity is on the two signal lines. This improves the signal to noise ratio by doubling the effective signal’s amplitude and allowing for cancellation of any common mode noise in use in the cable. The data’s sent in a Non-Return-To-Zero (or NRTZ) format. This is to ensure that a minimum density of signal transitions. USB also uses bit stuffing, which means you add an extra 0 which is inserted into the data stream after any appearance after six consecutive 1-bits. Seven consecutive 1-bits is always considered as an error. Now, if we have to look at the USB 4 main data transfer types: So We’ve got the Control, Isochronous, Bulk and Interrupt. Let’s take a look at each of these… So, Control transfers exchange configuration, set-up and command information between the device and the host. The host can also send command or query parameters with Control packets. In Isochronous transfers, this is used by time-critical streaming devices – such as speakers and video-cameras. It is time-sensitive information. So, within limitations, it is guaranteed access to the USB bus. Data streams between the device and the host in real time. So there won’t be any error crushing. And then We’ve got Bulk transfer. So, Bulk transfer is used by devices such as printers and scanners which recieves data in one big packet. Here, the time in delivery is not crucial. High above, Bulk transfers are fillers. So they claim unused USB bandwidth when nothing more important is going on. And then We have air crushing on top of this which protects these packets. And then the fat one which is Interrupt transfers. And this is used by peripherals sharing a small ammount of data that needs immediate attention. It is used by devices to request servicing from the PC host. So devices like the mouse or keyboard come in this scale degree. Air rejecting also validates the data. So before I move on, if you’d like any of the devices that I’m showing in the slides, you can check the links down below for the link to their pages. Ok so moving on… Let’s take a look at the USB protocol. Now, like I mentioned before, the USB protocol is extremely complex. So as you can see, here We have one standard protocol. Over here we have multiple packets that we can send or multiple “Packet types”. We’ve got the Handshake packets, Token, Data packets PRE packets, as well as Start of frame. These are just some of the packets that form part of the USB protocol. So if you look at the handshake packets… So handshake packets consist of a PID byte (which stands for “Product ID” – or “Product IDentification”), and they’re generally sent in response to the data packets. The 3 basic types of Handshake packets are: The “ACK”, which stands for “Acknowledge” – And this stands for that the data was successfully received- Number two, which is the “NACK” (Or “Not-Acknowledged”): Indicating that the data cannot be received at this time, and should be retried. Number three is the “Stall” And this indicates that the device has an error, and it will never be able to successfully transfer data until the correction of the error is performed. So USB 2.X has two additional handshake packets, which are called “NYET” – And this indicates that a split transaction is not yet complete -. And then, Number two which is the “ERR Handshake” And this is to indicate a split transaction has failed. The only handshake packet the USB host may generate is the “ACK”, or “Acknowledge”. If it is not traited to receive data, it should not instruct any device to send any. So, We move on to the Token Packets. So, Token Packets consist of a PID byte – as We mentioned before was “Product ID” – And this is followed by an eleven-bit of “Address” As well as a 5-bit CRC – which stands for “Cyclic Redundancy Check” -. (That is an error-checking method) So Tokens are only sent by the host and NOT a device. There are 3 types of Token Packets: So you’ve got the “IN token”, which informs the USB device that the host wishes to READ information. And then you get Number two, which is the “OUT token” This informs the USB device that the host wishes to SEND information. And then you get Number three, which is the “SETUP token” This is used to begin control transfers. IN and OUT tokens contain a 7-bit Device Number, and a 4-bit Function Number. And this is used for multifunction devices; And they command the device to transfer data packets, or receive the following data packets, respectively. And so, we move on to the “Data Packets” So there are 2 basic data packets. You’ve got “DATA0” and “DATA1”. So, both consist of the “Data PID” field, with 0 to 1023 bytes of data payload along with a 16-bit CRC – Or “Cyclic Redundancy Check” -. They must always be preceded by an Address Token And are always successfully followed by a Handshake Token from the receiver back to the transmitter. So Number one, if you look at it: The maximum data payload size of slow-speed devices is 8 bytes. This is very low compared to the maximum data payload for full-speed devices, which 1-0-2-3, which is 1023 bytes. For high-speed it’s 1024 bytes And also, data must be sent in multiples of bytes. If you look at USB 2, it added also “DATA2” and “MDATA”. “MDATA” stands for “Metadata Packets” They are only used by high-speed devices doing high-bandwidth asynchronous transfers. which need to transfer more than 1024 bytes per 125 μs. This allows for a transfer of 8192 kb/s Ok so hopefully you’re still with me. We are almost there and almost finish with this lecture. So if you look at PRE Packets: So the Low-speed devices are supported with a special PID value, and are called “PRE” This marks the beginning of a low-speed packet, and is used by HUBs, which normally do not send full-speed packets to low-speed devices. Since all PID bytes include four 0 bits, they leave the bus in a full-speed K-state, which is the same as the low-speed J-state. It is followed by the brief pause, during which the HUB enables the low-speed upwards. Already idling in the J-state and then a low-speed packet follows. This is the beginning of a “Sync” sequence and a PID byte. All this ending with a brief period of SE0 (Single Ended Zero). So full-speed devices other than HUBs can simply ignore the PRE Packets and its low-speed contents until the final SE0 indicates that a new packet follows. Ok so finally, the Start of Frame Packets: So every 1 mili-seconds, the USB host transmits a special SOF (or “Start of Frame”) token. This contains an 11-bit incrementing-frame-number, in place of a device address. This is used to synchronize asynchronous data flows. So, high-speed USB 2 devices receive a 7 additional duplicate SOF token (or frame) each introducing 125 micro-seconds. Ok, so that is it for this lecture. I hope you enjoyed it If you have any questions, please let me know down below. If you liked any of the devices I’ve show earlier, please check the link down below as well in the description. And please don’t forget to like, subscribe and share. And I’ll see you in next lecture. Thank you for watching.

About the author

Comments

  1. It would be great if your speech was a little more clearer. You lost me from USB Data Packet types. Great video though!

  2. Great demonstration,

    i have q question her about the USB License how it work? what type it is ? what type of data transfer ?
    to limit my question, as ex the USB license Key for the drive testing Tools (like TEMS, XCAL) please i am very interest to get more information about their functionality

    Thank you SIR

  3. Wonderful explanation from a embedded standpoint. I highly appreciate your effort on this video.

  4. Thank you so much for such a great lecture.
    But only one problem : please provide captions as some of the words are not clear.
    Otherwise awesome lectures. Thanks again :-).

  5. I just came across your channel 🙂 I love it so much! Lot's of valuable stuff is here. Thank you for your commitment! Big thumb up!

  6. Thanks for this upload. Sub'd for more like this.
    I appreciate your efforts. I have a hard time learning things like USB protocol from just the theory without some practical application. As a suggestion, perhaps you could look into a project like the USBasp Open Source Hardware serial programmer (no affiliation) and explain some of the grassroots aspects of USB on a more practical level using the open source code. I know that device is using an ATmega8 MCU to fake a USB connection. I imagine that is probably the simplest possible example of USB protocol.
    I haven't played with the ATmega32 myself but I believe that microcontroller has an on board USB port. That controller is supported on the Arduino IDE as the MCU on the Arduino Leonardo board so hobbyists and beginners like myself should be able to understand a practical example on these platforms more easily.
    I appreciate your efforts here. Every time I look into USB protocol I get overwhelmed and don't make much progress, but I'm barely able to copy and paste code so….
    I see a few comments about people that don't understand your English. I am a native English speaker from the USA. I have a hard time understanding many Asian to English accents. In my opinion you seem to put a lot of effort into slowing down your enunciation and altering your natural speaking rythem. I appreciate that, and I was able to understand you and subbscribed because of it.
    Have a great day,
    -Jake

  7. thanks for explaining USB protocol
    I m working on some project and I need to configure my USB port on zybo board as composite device.
    can you tell me how to do that ?
    it will be a great help.

  8. ya its great explanation, but i want to know about when we plug usb device in pc then what does the initial function work to communicate pc to usb & usb to pc

  9. Hi, this video is really great! I have a question, how does the signal works/looks like when multiple device connects to a usb hub, especially for the park where the usb hub connects to the computer part?

  10. I'm sure for someone who already knows a bunch about this stuff this was a good video, but I still have no clue what's going on. The title says "fun and easy," and in my experience, this was neither of those, but very informative though.

  11. can we manipulate it's frames using some softwares ??? so we can play with the algo being used to gain some speed with different large size of data?

  12. Very informative video. I appreciate the information on how the USB came to be and the progression in speed. I couldn't understand how it transfers data, since I don't know programmer/EE lingo, but will look elsewhere for a ELI5 explanation. Thumbs up! thanks

Leave a Reply

Your email address will not be published. Required fields are marked *