0% found this document useful (0 votes)
12 views26 pages

21 - Usb

The document provides detailed information on Universal Serial Bus (USB) device enumeration, data flow, descriptors, and protocol. It outlines the ten steps involved in the enumeration process when a new USB device is connected, including device detection, address assignment, and configuration. Additionally, it describes USB transactions, packet types, and the structure of various USB descriptors essential for device communication.

Uploaded by

Thomas Shi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views26 pages

21 - Usb

The document provides detailed information on Universal Serial Bus (USB) device enumeration, data flow, descriptors, and protocol. It outlines the ten steps involved in the enumeration process when a new USB device is connected, including device detection, address assignment, and configuration. Additionally, it describes USB transactions, packet types, and the structure of various USB descriptors essential for device communication.

Uploaded by

Thomas Shi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

ECE 4240

MICROPROCESSOR INTERFACING
Universal Serial Bus (USB)

Photo by Yogesh Phuyal on Unsplash


COPYRIGHT NOTICE

These course materials are copyright ©2023 The University of Manitoba.


Some elements are the property of other rights holders and are included
under the Fair Dealing provision of the Canadian Copyright Act.
These course notes are provided to students of ECE 4240 for self study
purposes only. They may not be uploaded to any online service or portal, or
redistributed in any manner, electronic or otherwise, without the express
written permission of the instructor and the other relevant rights holders.
USB-C CABLES

lumafield.com Chindi.ap, Wikimedia Commons, Creative Commons Attribution-Share Alike 4.0 International
USB DEVICE ENUMERATION

There are 10 steps in the enumeration process associated with the connection of a
new device.
1. A device is physically connected.
This may be the result of the user plugging in a new device, or;
The system could start-up with a device already physically connected.
2. The device is detected.
Either the root hub or a downstream hub recognizes the presence of a device.
Detection is made possible by way of pull-up resistors attached to either the D+
or D– data lines within the device. An attached device will pull one of these
signal lines to a logic high value from their default logic low state.
USB DEVICE ENUMERATION

3. The host is informed of a new device.


The hub to which the new device is attached informs the host that there
has been a change in the state of attached devices.
The host then interrogates the hub to determine the nature of the change.
The hub responds by indicating a new attachment.
USB DEVICE ENUMERATION

4. Device speed is determined.


The speed of the attached device must next be determined.
This is based on which of the two differential signal lines has been pulled-up
by the device’s internal resistor.
D+ pulled high indicates a full-speed device is attached;
D– pulled high indicates a low-speed device is attached.
High-speed devices initially connect in full-speed mode and later switch to
high-speed under host control.
USB DEVICE ENUMERATION

5. Device reset.
The host initiates a reset on the newly attached device.
This is achieved by having the hub force both of the differential signal lines low
simultaneously and holds them in that state for >10ms.
6. Full-speed or High-speed.
The hub then checks to see if the attached device is capable of high-speed
operation by waiting for the device to initiate a differential signalling on the D+/D–
lines.
The hub responds with an alternating signal which confirms the state change.
When switching to high-speed mode, the device disconnects the pull-up resistor
from the D+ line.
USB DEVICE ENUMERATION

7. Device address is assigned.


Once the device completes its reset and high-speed test, the host will then configure
the device with its own unique device address.
If the set-address message is properly acknowledged by the device, all further
communication switches to that address.
8. Reading of configuration information.
The host interrogates the device through its control pipe to endpoint 0 in order to
determine device characteristics, including number of configurations supported by the
device. This information is contained in the device descriptor.
Descriptors are data structures which contain information about the entire device or
specific portions of it, such as configurations, interfaces, and endpoint properties.
USB DEVICE ENUMERATION

9. Device driver assignment.


Based on the descriptor data returned by the device, the host associates a suitable
device driver to handle actual communication with the devices’ function.
Composite devices may have multiple device drivers assigned to their own individual
interfaces within the device.
10. Configuration assignment.
A configuration, as selected by the device driver, is then assigned to the device.
The device is now in a fully configured state and can assume normal operation within the
system.
If the device is bus powered and requires more than the default power level, it may now
draw up to its requested maximum power from the VBUS supply.
USB DATA FLOW

Conceptually, the host is


communicating through a logical
pipe to each endpoint (EP) within a
USB device.
(Addr, Endpoint, Direction)

Fig 7.36, Jonathan Valvano, Real-time Interfacing to ARM® Cortex -M Microcontrollers, Vol. 2, 2016.
USB DESCRIPTORS

There are three required descriptors which are common to all USB devices.
Device
This includes attributes such as device class (ex. HID), vendor ID, product ID, product
description, max packet size, and configuration count.
Configuration
Each device has at least one configuration descriptor.
It contains information such as the number of interfaces, whether the device is self-
powered or bus-powered, and the maximum bus power required.
Interface
This includes details about the number of endpoints forming the interface.
USB DESCRIPTORS

Endpoint
This descriptor is optional. If there are endpoints in the device other than
endpoint 0 then this descriptor is used.
Most devices will have one or more endpoint descriptors.
It defines attributes such as the transfer type, the maximum packet size
supported, and the sampling interval required for isochronous transfers.
USB DESCRIPTOR HIERARCHY FOR HID DEVICES

Device Descriptor String Descriptor

Configuration
Descriptor

Interface Descriptor

Endpoint Descriptor HID Descriptor

Report Descriptor Physical Descriptor

Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
DEVICE DESCRIPTOR
Size (by Sample Va
Name Description
tes) lue

bLength 1 Length in bytes of this descriptor. 0x12


bDescriptorType 1 Descriptor type. (DEVICE = 0x01) 0x01
bcdUSB 2 Specification release version (i.e. 2.0) 0x0200
bDeviceClass 1 Class identifier code for the device. 0x00
bDeviceSubClass 1 Subclass identifier code. 0x00
bDeviceProtocol 1 Class specific protocol code. 0x00
bMaxPacketSize0 1 Max. packet size accepted by Endpoint 0 (2 bMaxPacketSize0) 0x08
idVendor 2 Manufacturer specific vendor ID number. 0xFFFF
idProduct 2 Product ID number. (Assigned by manufacturer.) 0x0001
bcdDevice 2 Device release version. (Assigned by manufacturer.) 0x0100
iManufacturer 1 Index of manufacturer string in String Descriptor 0x04
iProduct 1 Index of product description in String Descriptor. 0x0E
iSerialNumber 1 Index of device serial # string in String Descriptor. 0x30
bNumConfigurations 1 Number of supported configurations 0x01

Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
STRING DESCRIPTOR

Size (by
Name Description Sample Value
tes)

bLength 1 Length in bytes of this descriptor. 0x04


bDescriptorType 1 Descriptor type. (STRING = 0x03) 0x03
bString 2 Language ID code. (ENGLISH = 0x0009) 0x0009
bLength 1 Length of String Descriptor 0x0A
bDescriptorType 1 Descriptor type. (STRING = 0x03) 0x03
bString 8 String (ex. Manufacturer) ‘ACME’
bLength 1 Length of String Descriptor 0x22
bDescriptorType 1 Descriptor type. (STRING = 0x03) 0x03

bString 32 String (ex. Product Name) ‘Locator Keyboard’

bLength 1 Length of String Descriptor 0x0E


bDescriptorType 1 Descriptor type. (STRING = 0x03) 0x03
bString 12 String (ex. Serial Number) ‘ABC123’

Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
CONFIGURATION DESCRIPTOR

Size (by
Name Description Sample Value
tes)

bLength 1 Length in bytes of this descriptor. 0x09

bDescriptorType 1 Descriptor type. (CONFIGURATION = 0x02) 0x02

wTotalLength 2 Total length of all descriptors in the configuration. 0x003B

bNumInterfaces 1 Number of interfaces forming this configuration. 0x02

bConfigurationValue 1 Configuration number for this device. 0x01

iConfiguration 1 Index of configuration string in String Descriptor. 0x00

Configuration characteristics.
Bit 7 = RESERVED (set to 1)
bmAttributes 1 Bit 6 = Self powered 10100000B
Bit 5 = Remote Wake-up
Bits 4–0 are Reserved (set to 0)

Maximum power consumption when operational.


MaxPower 1 (Low/Full/High-speed: Max power = 2mA x value) 0x32
(SuperSpeed: Max power = 8mA x value)

Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
INTERFACE DESCRIPTOR
Size (by
Name Description Sample Value
tes)

bLength 1 Length in bytes of this descriptor. 0x09

bDescriptorType 1 Descriptor type. (INTERFACE = 0x04) 0x04

bInterfaceNumber 1 Unique interface identifier for this device. 0x00

bAlternateSetting 1 Alternate interface identifier. 0x00

bNumEndpoints 1 Number of Endpoints forming this interface. 0x01

bInterfaceClass 1 Class identifier for the interface. (HID = 0x03) 0x03

Subcode within the above class. (NONE = 0x00)


bInterfaceSubClass 1 0x01
(BOOT = 0x01)

Protocol code.
bInterfaceProtocol 1 0x01
(KEYBOARD = 0x01) (MOUSE = 0x02)

iInterface 1 Index to interface string in String Descriptor 0x00

Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
ENDPOINT DESCRIPTOR

Size (by
Name Description Sample Value
tes)

bLength 1 Length in bytes of this descriptor. 0x07

bDescriptorType 1 Descriptor type. (ENDPOINT = 0x05) 0x05

Unique endpoint address within the device.


Bits 0–3 = Endpoint number;
bEndpointAddress 1 10000001B
Bits 4–6 = Reserved (set to 0);
Bit 7 = Direction (OUT = 0, IN = 1)

Description of the endpoint’s attributes. Bits 0–1 = Transfer


type. (CONTROL = 00, ISOCHRONOS = 01, BULK = 10,
bmAttributes 1 00000011B
INTERRUPT = 11)
Bits 2–7 = Reserved. (set to 0)

wMaxPacketSize 2 Maximum packet size able to be sent/received. 0x0008

bInterval 1 Device polling interval in milliseconds. (2bInterval-1) 0x0A

Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
USB PROTOCOL

Data exchange in USB is broken down into a series of frames.


For low-speed and full-speed devices, the frame duration is 1ms. For high-
speed devices, the frame is only 125µs in duration and is called a microframe.
Frames are composed of a sequence of several transactions between the host
and device endpoints. Transactions within the same frame can be sent to any
device endpoint in the system.
Frame

Transaction

Token Data HSK


USB PROTOCOL

An actual, high-level, transfer of data from the client to a function will require
one or more transactions, which may be scheduled into the same frame or
spread across multiple frames.
Each transaction is composed of a series of packets, beginning with a token
packet. In most cases the token packet is followed by a data and handshake
packet.
Frame

Transaction

Token Data HSK


USB PROTOCOL

All packets begin with a packet identifier (PID) field which indicates the type
of packet being sent.
There are 16 different packet types represented by a 4-bit packet identifier
(PID). PID Type PID Name PID PID Type PID Name PID
OUT 0001 ACK 0010
When transmitted, the PID is IN 1001 NAK 1010
given as an 8-bit value where Token
SOF 0101
Handshake
STALL 1110

the high nibble is the bitwise SETUP 1101 NYET 0110


DATA 0 0011 ERR 1100
complement of the 4-bit code DATA 1 1011 SPLIT 1000
Data Special
shown (low nibble). DATA 2 0111 PING 0100

(ex. SOF = 1010 0101 = 0xA5) MDATA 1111 Reserved 0000


USB TRANSACTIONS

Token Packet
A token packet consists of the PID, followed by the USB device address and
endpoint number, and finally a CRC field.
The address is a 7-bit value (128 unique addresses).
The endpoint is a 4-bit value, allowing for up to 15 endpoints in the same
USB device (in addition to the control endpoint).
A 5-bit CRC is performed on the address and endpoint value.
PID Address Endpoint CRC
USB TRANSACTIONS

Data Packet
A data packet beings with the PID that identifies it as data.
The PID is followed by the actual data payload, whose length may vary.
For low-speed devices the data payload is limited to 8 bytes;
For full-speed devices the data payload is limited to 1023 bytes, and;
For high-speed devices the data payload is limited to 1024 bytes.
The data payload is followed by a 16-bit CRC field calculated on the data
payload alone. PID Data Data Data CRC
USB TRANSACTIONS

Handshake Packet
The handshake packet is made up of only the handshake PID.
It is only sent to complete transactions in which data was exchanged.
USB TRANSACTIONS

There are four possible PIDs which may be sent in a handshake packet.
ACK: Indicates that the data packet was received without error;
NAK: Indicates that the receiver can not accept the data, or there is no data to
be sent. It indicates a temporary situation, not a permanent failure. This
handshake packet can only be generated by the USB function, never by the
host.
STALL: Indicates that the receiver is unable to accept data or send data. The
function sending the STALL is in an unknown state from there on. This
handshake packet can only be generated by the USB function, never by the
host.
NYET: This is a high-speed handshake packet type.
USB TRANSACTIONS

Start-of-Frame (SOF) Packet


In addition to beginning a transaction, the token packet also provides the
ability to announce the start of each USB frame.
The SOF token is used to maintain synchrony between the devices
communicating over the bus. It is sent every 1ms in the case of low or full-
speed devices, or 125µs in the case of high-speed devices.
The PID is followed by an 11 bit frame number.
The packet terminates with a 5-bit CRC computed on the frame number.
PID Frame Number CRC

You might also like