USB based communication using microcontrollers

Updated on 2017/06/19 23:40

Syllabus

Concept of USB based communication using microcontrollers

Features

USB 1.0Low speed1.5 Mbps
USB 1.5Full speed12 Mbps
USB 2.0High speed480 Mbps
USB 3.0Super speed5 Gbps
USB 3.1Super speed10 Gbps

USB Pinout

USB-pinout.png

Advantages of USB

  • Ease of Use
  • Single Interface for multiple devices
  • Auto-configuration
  • Easy to expand
  • Compact Size
  • Speed
  • Reliability
  • Low cost
  • Low power consumption

Limitations

  • Speed: With the introduction of USB 3.0, it is possible to achieve 5Gbits/sec. But it is still lower than Gigabit Ethernet. The FireWire 800(IEEE-1394b) is also a competitor for USB.
  • Peer to Peer Communication: As per the USB standard, the communication takes place between the host and the peripheral. Two hosts cannot communicate directly to each other. Same is the case for a peripheral. On the other hand, interfaces like FireWire supports peripheral to peripheral communication. For overcoming this limitation, the USB introduced the concept of OTG (On The Go). The OTG device normally functions as peripheral, but it can also function as host with some limited capability when required.
  • Distance: According to USB standards, the connecting cable can be as long as 5 meters, beyond which, USB hubs need to be used for expanding the connectivity.
  • Broadcasting: Universal Serial Bus does not provide the broadcasting feature, only individual messages can be communicated between host and peripheral.

USB Packet Types

USB has four different packet types. Token packets indicate the type of transaction to follow, data packets contain the payload, handshake packets are used for acknowledging data or reporting errors and start of frame packets indicate the start of a new frame.

1) Token Packets

There are three types of token packets:

  • In - Informs the USB device that the host wishes to read information.
  • Out - Informs the USB device that the host wishes to send information.
  • Setup - Used to begin control transfers.

Token Packets must conform to the following format:

Sync

PID

ADDR

ENDP

CRC5

EOP

2) ​Data Packets

There are two types of data packets each capable of transmitting up to 1024 bytes of data.

  • Data1
  • Data0

High Speed mode defines another two data PIDs, DATA2 and MDATA.

Data packets have the following format:

Sync

PID

Data

CRC16

EOP

  • Maximum data payload size for low-speed devices is 8 bytes.
  • Data must be sent in multiples of bytes.
  • Maximum data payload size for high-speed devices is 1024 bytes.
  • Maximum data payload size for full-speed devices is 1023 bytes.

3) Handshake Packets

There are three type of handshake packets which consist simply of the PID

  • ACK - Acknowledgment that the packet has been successfully received.
  • STALL - The device finds its in a state that it requires intervention from the host.
  • NAK - Reports that the device temporary cannot send or received data. Also used during interrupt transactions to inform the host there is no data to send.

Handshake Packets have the following format:

Sync

PID

EOP

4) Start of Frame Packets

The SOF packet consisting of an 11-bit frame number is sent by the host every 1ms  500ns on a full speed bus or every 125 µs  0.0625 µs on a high speed bus.

Sync

PID

Frame Number

CRC5

EOP

Common USB Packet Fields

Data on the USBus is transmitted LSBit first. USB packets consist of the following fields:

Sync: All packets must start with a sync field. The sync field is 8 bits long at low and full speed or 32 bits long for high speed and is used to synchronise the clock of the receiver with that of the transmitter. The last two bits indicate where the PID fields starts.

PID: PID stands for Packet ID. This field is used to identify the type of packet that is being sent. The following table shows the possible values.

There are 4 bits to the PID, however to insure it is received correctly, the 4 bits are complemented and repeated, making an 8 bit PID in total. The resulting format is shown below.

PID0

PID1

PID2

PID3

nPID0

nPID1

nPID2

nPID3

ADDR: The address field specifies which device the packet is designated for. Being 7 bits in length allows for 127 devices to be supported. Address 0 is not valid, as any device which is not yet assigned an address must respond to packets sent to address zero.

ENDP: The endpoint field is made up of 4 bits, allowing 16 possible endpoints. Low speed devices, however can only have 2 additional endpoints on top of the default pipe. (4 endpoints max)

CRC: Cyclic Redundancy Checks are performed on the data within the packet payload. All token packets have a 5 bit CRC while data packets have a 16 bit CRC.

EOP: End of packet. Signalled by a Single Ended Zero (SE0) for approximately 2 bit times followed by a J for 1 bit time.

Endpoints

Endpoints can be described as sources or sinks of data.

Pipes

A pipe is a logical connection between the host and endpoint(s).

USB Data Transfer Types

The USB device (function) communicates with the host by transferring data through a pipe between a memory buffer on the host and an endpoint on the device. USB supports four different transfer types. A type is selected for a specific endpoint according to the requirements of the device and the software. The transfer type of a specific endpoint is determined in the endpoint descriptor.

The USB specification provides for the following data transfer types:

1) Control Transfer

  • Control Transfer is mainly intended to support configuration, command and status operations between the software on the host and the device.
  • This transfer type is used for low-, full- and high-speed devices.
  • Each USB device has at least one control pipe (default pipe), which provides access to the configuration, status and control information.
  • Control transfer is bursty, non-periodic communication.
  • The control pipe is bi-directional – i.e., data can flow in both directions.
  • Control transfer has a robust error detection, recovery and retransmission mechanism and retries are made without the involvement of the driver.
  • The maximum packet size for control endpoints can be only 8 bytes for low-speed devices; 8, 16, 32, or 64 bytes for full-speed devices; and only 64 bytes for high-speed devices.

USB-communication-sequence.png

This is initiated by the host via an appropriately coded token packet with information regarding the control command being issued. The recipient either replies with an ACK handshake, or ignores the token totally. This type of transfer also contains an optional unidirectional data transfer between the host and the device. Lastly, a status is returned to whatever end of the pipe was last to transmit.

e.g. Each and very USB device connected to PC is part of control transfer.

2) Isochronous Transfer

  • Isochronous Transfer is most commonly used for time-dependent information, such as multimedia streams and telephony.
  • This transfer type can be used by full-speed and high-speed devices, but not by low-speed devices.
  • Isochronous transfer is periodic and continuous.
  • The isochronous pipe is unidirectional, i.e., a certain endpoint can either transmit or receive information. Bi-directional isochronous communication requires two isochronous pipes, one in each direction.
  • USB guarantees the isochronous transfer access to the USB bandwidth (i.e., it reserves the required amount of bytes of the USB frame) with bounded latency, and guarantees the data transfer rate through the pipe, unless there is less data transmitted.
  • Since timeliness is more important than correctness in this type of transfer, no retries are made in case of error in the data transfer. However, the data receiver can determine that an error occurred on the bus.

USB-isochronous-transactions.png

These are also begun with an appropriate token packet being sent to the function, and then data is transmitted as required. However, due to the assumed error-tolerance of isochronous data, no handshake packets are used.

e.g. Speakers,Microphone,Telephone etc

3) Interrupt Transfer

  • Interrupt Transfer is intended for devices that send and receive small amounts of data infrequently or in an asynchronous time frame.
  • This transfer type can be used for low-, full- and high-speed devices.
  • Interrupt transfer type guarantees a maximum service period and that delivery will be re-attempted in the next period if there is an error on the bus.
  • The interrupt pipe, like the isochronous pipe, is unidirectional and periodical.
  • The maximum packet size for interrupt endpoints can be 8 bytes or less for low-speed devices; 64 bytes or less for full-speed devices; and 1,024 bytes or less for high-speed devices.

USB-interrupt-transactions.png

The function is queried by the host via a token packet, and returns data if it has any interrupt related information to transmit, or a NAK handshake if it doesn't.

e.g. Mouse, Keypad, Joystick

4) Bulk Transfer

  • Bulk Transfer is typically used for devices that transfer large amounts of non-time sensitive data, and that can use any available bandwidth, such as printers and scanners.
  • This transfer type can be used by full-speed and high-speed devices, but not by low-speed devices.
  • Bulk transfer is non-periodic, large packet, bursty communication.
  • Bulk transfer allows access to the bus on an "as-available" basis, guarantees the data transfer but not the latency, and provides an error check mechanism with retries attempts. If part of the USB bandwidth is not being used for other transfers, the system will use it for bulk transfer.
  • Like the other stream pipes (isochronous and interrupt), the bulk pipe is also unidirectional, so bi-directional transfers require two endpoints.

The maximum packet size for bulk endpoints can be 8, 16, 32, or 64 bytes for full-speed devices, and 512 bytes for high-speed devices.

USB-io-bulk-transactions.png

When the host wants to initiate a bulk transfer from a device, it sends a Token Packet specifying whether input or output is desired. If the host wants input, the function in turn sends either a packet of data or a non-ACK handshake. If the host is sending a request to transmit, it will immediately send the data packet and wait for an appropriate handshake to be returned by the recipient

Ex. Print Jobs sent to printer from PC, Image generated from scanner

USB Controller (Architecture and Operation)

The Universal Serial Bus (USB) is a four-wire bus that supports communication between a host and one or more (up to 127) peripherals. The host controller allocates the USB bandwidth to attached devices through a token-based protocol. The bus supports hot plugging and dynamic configuration of the devices. All transactions are initiated by the host controller.

Features

  • Fully compliant with the USB 2.0 specification (full speed).
  • Supports 32 physical (16 logical) endpoints.
  • Supports Control, Bulk, Interrupt and Isochronous endpoints.
  • Scalable realization of endpoints at run time.
  • Endpoint maximum packet size selection by software at run time.
  • Supports SoftConnect and GoodLink features.
  • Supports DMA transfers on all non-control endpoints.
  • Allows dynamic switching between CPU controlled and DMA modes.
  • Double buffer implementation for Bulk and Isochronous endpoints.

Architecture

USB-device-controller-block-diagram.png

  • Analog transceiver: The USB Device Controller has a built-in analog transceiver (ATX). The USB ATX sends/receives the bi-directional D+ and D- signals of the USB bus.

  • Serial Interface Engine (SIE): It handles transfer of data between the endpoint buffers in EP_RAM and the USB bus. The functions of this block include: synchronization pattern recognition, parallel/serial conversion, bit stuffing/de-stuffing, CRC checking/generation, PID verification/generation, address recognition, and handshake evaluation/generation.

  • Endpoint RAM (EP_RAM): Each endpoint buffer is implemented as an SRAM based FIFO. The SRAM dedicated for this purpose is called the EP_RAM.

  • EP_RAM access control: The EP_RAM Access Control logic handles transfer of data from/to the EP_RAM and the three sources that can access it: the CPU (via the Register Interface), the SIE, and the DMA Engine.

  • DMA engine and bus master interface: When enabled for an endpoint, the DMA Engine transfers data between RAM on the AHB bus and the endpoint’s buffer in EP_RAM.

  • Register interface: The Register Interface allows the CPU to control the operation of the USB Device Controller. It also provides a way to write transmit data to the controller and read receive data from the controller.

  • SoftConnect: The connection to the USB is accomplished by bringing D+ (for a full-speed device) HIGH through a 1.5 kOhm pull-up resistor. The SoftConnect feature can be used to allow software to finish its initialization sequence before deciding to establish connection to the USB. Re-initialization of the USB bus connection can also be performed without having to unplug the cable.

  • GoodLink: Good USB connection indication is provided through GoodLink technology. When the device is successfully enumerated and configured, the LED indicator will be permanently ON. During suspend, the LED will be OFF. .

Operational overview

  • Transactions on the USB bus transfer data between device endpoints and the host. The direction of a transaction is defined with respect to the host.
  • OUT transactions transfer data from the host to the device.
  • IN transactions transfer data from the device to the host.
  • All transactions are initiated by the host controller.

For an OUT transaction, the USB ATX receives the bi-directional D+ and D- signals of the USB bus. The Serial Interface Engine (SIE) receives the serial data from the ATX and converts it into a parallel data stream. The parallel data is written to the corresponding endpoint buffer in the EP_RAM.

For IN transactions, the SIE reads the parallel data from the endpoint buffer in EP_RAM, converts it into serial data, and transmits it onto the USB bus using the USB ATX.

References

Prof. Sujit Wagh

Tags:
Created by Admin on 2017/04/07 00:51