21 - Usb
21 - Usb
MICROPROCESSOR INTERFACING
Universal Serial Bus (USB)
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
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
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
Configuration
Descriptor
Interface Descriptor
Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
DEVICE DESCRIPTOR
Size (by Sample Va
Name Description
tes) lue
Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
STRING DESCRIPTOR
Size (by
Name Description Sample Value
tes)
Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
CONFIGURATION DESCRIPTOR
Size (by
Name Description Sample Value
tes)
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)
Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
INTERFACE DESCRIPTOR
Size (by
Name Description Sample Value
tes)
Protocol code.
bInterfaceProtocol 1 0x01
(KEYBOARD = 0x01) (MOUSE = 0x02)
Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
ENDPOINT DESCRIPTOR
Size (by
Name Description Sample Value
tes)
Device Class Definition for Human Interface Devices, USB Implementers Forum, 2001
USB PROTOCOL
Transaction
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
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
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