MS_uC / fue1 / V01 6- 1 TIMx - Timer Programming Microcontroller TIMx - Timer Autumn term 2012.
MS_uC / fue1 / V01 1 USB Programming Microcontroller Universal Synchronous Bus (USB) Autumn 2012.
-
Upload
cuthbert-charles -
Category
Documents
-
view
226 -
download
1
Transcript of MS_uC / fue1 / V01 1 USB Programming Microcontroller Universal Synchronous Bus (USB) Autumn 2012.
MS_uC / fue1 / V01
1
USB
Programming MicrocontrollerUniversal Synchronous Bus (USB)
Autumn 2012
MS_uC / fue1 / V01 Overview (story)USB
2
1995: Foundation of the “USB Implementers Forum” (USB IF) http://www.usb.org
1996: First USB specification issue (USB 1.0)
Connection of various peripheral devices to a single interface Mouse, keyboard, printer …
2000: Specification of USB 2.0 Extension of the speed at 480 M bit/s Possibility to connect hard drives or video devices
MS_uC / fue1 / V01 Overview (generality)
USB is a asymmetrical bus Distinction between USB host (PC) & device
Data is transmitted in a serial asynchronous format
Baud rate 1.5 M bit/s (Low-Speed) 12 M bit/s (Full-Speed) 480 M bit/s (High-Speed, only for USB 2.0)
USB
3
MS_uC / fue1 / V01
4
USB bus topology (network)
127 devices can be connected to the bus (incl. hubs) Each USB device receives a specific address from the
host (PC) Enumeration Host
Root-Hub
Hub
Hub
Device 1
Device 2
Device 3 Device 4
1 2
3 4
5 6
USB
4
MS_uC / fue1 / V01 USB bus topology (power supply)
Bus powered hub The total current is obtained from the host (PC) 500 mA are available for the all the connected devices
Self powered hub Additional connector for the power supply Each device can consume a maximum of 500 mA
USB
5
MS_uC / fue1 / V01 USB cable
USB cable contains 4 wires VCC (+5V) GND Differential data lines D+ and D- (0 and 3.3 V)
Signal rise and fall times www.usb.org
USB
6
MS_uC / fue1 / V01
7
USB plugs
Function
Host Pin-Nr(Type A)
Device Pin-Nr(Type B)
Device Pin-Nr
(Mini USB)
Color
VCC (5V) 1 1 1 red
D - 2 2 2 white
D+ 3 3 3 green
GND 4 4 5 black
ID - - 4 Not connecte
d
USB
7
MS_uC / fue1 / V01 USB speed detections (1/2)
Full & high speed Devices Low speed Device
Host / hub must recognize the connected USB device
Host / hub begins with the speed detection Pull-up or pull-down resistors
8
MS_uC / fue1 / V01 USB speed detections (2/2)
Distinction between full & fast speed devices Full speed devices generate a chirp signal (fast toggling)
during reset Host recognize this chirp and switch into high speed Pull-up resistor is disconnected during this transmission mode
4- 9
UART - Serial communic.
MS_uC / fue1 / V01 USB bus states
Low-Speed Full-/High-Speed
State Bus-Driver D+ D- D+ D-
Idle-State Passive Low high differential 0 high Low differential 1
J-State Active Low high differential 0 high low differential 1
K-State Active High low differential 1 low high differential 0
SE0-State Active Low low NA low low NA
SOP (Start-of-Packet) Transition from the “Idle-State” into the “K-State” First bit of the SYNC-field
EOP (End-of-Packet) Data lines are set to low (SE0)
USB
10
MS_uC / fue1 / V01 USB bus states
Bus Reset SE0 is driven more than 2.5 ms
Suspend (Energy saving mode) USB devices fall asleep when the bus is inactive for 3 ms
After 3 ms high-speed devices switch into full-speed mode 100-875 ms after they go into suspend mode
Resume Resume sequences wake up the fallen asleep devices Data lines are driven into the K-State during 20 ms
USB
11
MS_uC / fue1 / V01 Data encoding
Data transmission begins with LSB (Least Significant Bit)
Data encoding is NRZI (Non-Return-To-Zero-Inverted)
USB
12
MS_uC / fue1 / V01
13
Bit Stuffing
Problem Synchronization loss when several high bits must be send
Solution Insertion of an additional low bit after the 6th high bit
USB
13
MS_uC / fue1 / V01 Transfer types (1/2)
Control transfer Configuration of the USB device Host sends requests to the device 10% of the bandwidth is reserved for control transfer
Interrupt Transfer Transfer of a few amount of data with guaranteed bandwidth
Mouse, keyboards etc. Host asks the interrupt endpoints periodically
Endpoint specifies the period 90% of the bandwidth is reserved for interrupt & isochronous
transfers Interrupt transfers are subject to the USB error detection
USB
14
MS_uC / fue1 / V01 Transfer types (2/2)
Bulk transfer (full- & high speed device) Transmission of large amount of data, which are not time
critical Printers, scanner & memory-Sticks
Transmission in the remaining available bandwidth
Isochronous transfer 90% of the bandwidth is reserved for interrupt & isochronous
transfers Data are sent every 1 ms frame Devices that require a constant data stream
Audio- und video capture devices
USB
15
MS_uC / fue1 / V01Protocol Layer (OSI Layer 2)
Data are transferred though USB data pipes Each pipe goes from the host and terminates in an endpoint
OUT1
C0 IN1 IN2 IN3 OUT1 OUT2 OUT3
Device 3
Address 12
C0 IN1 IN2 OUT1 OUT2
Device 1
Address 20
C0 IN1 OUT1
Device 2
Address 11
C0 IN1 OUT1 OUT2 OUT3 OUT4 OUT1
Root-Hub
Host
C0 IN1 OUT2 OUT3 OUT4 OUT5
HUB
Address 10
16
MS_uC / fue1 / V01 Pipes and endpoints
Sub address in a USB device with its own FIFO
USB
17
MS_uC / fue1 / V01
18
Type of endpoints
Each USB device has at least one control endpoint (endpoint 0) Configuration of the USB device
Each endpoint has a predefined FIFO size
All endpoint except endpoint 0 are unidirectional Each endpoint has a number (address)
IN- endpoints: Address 0x81 until 0x8F OUT- endpoints: Address 0x01 until 0x0F
Endpoint size in bytes
Bus-Driver Low-Speed Full-Speed High-Speed
Control-Transfer 8 8,16,32,64 64
Interrupt-Transfer 8 1 until 64 1 until 3072
Bulk-Transfer N/A 8,16,32,64 512
Isochronous-Transfer NA 1 until 1023 1 until 3072
18
MS_uC / fue1 / V01 Detection & treatment of errors
USB specifies various types of errors CRC check sum (Cyclic Redundancy Code)
CRC5: G(X) = X5 + X2 + 1 CRC16 : G(X) = X15 + X2 + 1
PID check 4 PID bits PID are transmitted twice
• Original and inverted forms
Bit stuff error Reception of more than six bits of same polarity in a row (logic
high)
Timeout error Confirmation of the received data outside a certain period
USB
19
MS_uC / fue1 / V01 Packet (1/2)
The USB data are transmitted by packets
Each packet starts with the following fields SYNC
Synchronization of the receiver PLL
PID (Packet-IDentifier) The upper 4 bits are the inverse of the lower 4 bits
USB
20
MS_uC / fue1 / V01 Packet (2/2)
Packets are classed in 4 groups Token-Packets
SOF Setup IN & OUT
Data-Packets Handshake-Packets Special-Packets
USB
21
MS_uC / fue1 / V01 SOF (token packet)
Host send a SOF packet (Start-Of-Frame) every milliseconds Prevent USB device from going into suspend mode
Low-Speed
Full-Speed 11-bit frame number is incremented systematically
EOP
EOP
SYNC PID Frame # CRC 5 EOP
0x01 0xA5 frame number 0xXX EOP
USB
22
MS_uC / fue1 / V01 Setup (token packet)
Host announces the sending of setup data to the devices
ADDR: 7 bits device address ENDP: 4 Bits endpoint number
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0x2D Device address Endpoint number 0xXX EOP
USB
23
MS_uC / fue1 / V01 IN & OUT (token packet)
IN: Host requests the USB device to send data to it USB device then sends one packet DATAx
OUT: Host announces that it will send data to the USB device Host then sends one packet DATAx
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0x69 Device address Endpoint number 0xXX EOP
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0xE1 Device address Endpoint number 0xXX EOP
USB
24
MS_uC / fue1 / V01
25
DATA0 / DATA1 packets
The DATA0 & DATA1 packets are sent alternately
DATA0
DATA1
Number of data bytes 0..8 Bytes for Low Speed 0..1023 Bytes for Full Speed 0..1024 Bytes for High Speed
SYNC PID DATA CRC 16 EOP
0x01 0xC3 Data bytes 0xXXXX EOP
SYNC PID DATA CRC 16 EOP
0x01 0x4B Data bytes 0xXXXX EOP
USB
25
MS_uC / fue1 / V01 Handshake packets ACK
Confirmation of the correct reception of a packet DATAx
NAK is only sent from the device OUT: Received data packet could not be processed IN: No data are available for sending
STALL is only sent from the device OUT: Received data packet could not be processed IN: No data are available for sending
SYNC PID EOP
0x01 0xD2 EOP
SYNC PID EOP
0x01 0x1E EOP
SYNC PID EOP
0x01 0x5A EOP
USB
26
MS_uC / fue1 / V01 Data transfer through USB
Host can only initiate a data transfer
Data transfer consists of 3 phases Phase 1 Host sends a token packet to the USB device Phase 2 Host or device sends the data Phase 3 Host or device acknowledges receipt
USB
27
MS_uC / fue1 / V01 Example of output data transfer (1/3)
Data transfer without error Address: 0x123 Endpoint: 1 Data: 0x00, 0x11, 0x22, 0x33
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0xE1(OUT) 0x123 0x01 0xXX EOP
SYNC PID DATA CRC 16 EOP
0x01 0xC3(DATA0) 0x00, 0x11, 0x22, 0x33 0xXXXX EOP
SYNC PID EOP
0x01 0xD2(ACK) EOP
OUT
OUT
IN
USB
28
MS_uC / fue1 / V01 Example of output data transfer (2/3)
FIFO of the USB device is full Address: 0x123 Endpoint: 1 Data: 0x00, 0x11, 0x22, 0x33
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0xE1(OUT) 0x123 0x01 0xXX EOP
SYNC PID DATA CRC 16 EOP
0x01 0xC3(DATA0) 0x00, 0x11, 0x22, 0x33 0xXXXX EOP
SYNC PID EOP
0x01 0x5A(NAK) EOP
OUT
OUT
IN
USB
29
MS_uC / fue1 / V01 Example of output data transfer (3/3)
USB device is disabled Address: 0x123 Endpoint: 1 Data: 0x00, 0x11, 0x22, 0x33
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0xE1(OUT) 0x123 0x01 0xXX EOP
SYNC PID DATA CRC 16 EOP
0x01 0xC3(DATA0) 0x00, 0x11, 0x22, 0x33 0xXXXX EOP
SYNC PID EOP
0x01 0x1E(STALL) EOP
OUT
OUT
IN
USB
30
MS_uC / fue1 / V01 Example of input data transfer (1/4)
Data transfer without error Address: 0x123 Endpoint: 2 Data: 0x00, 0x11, 0x22, 0x33
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0x69(IN) 0x123 0x82 0xXX EOP
SYNC PID DATA CRC 16 EOP
0x01 0xC3(DATA0) 0x00, 0x11, 0x22, 0x33 0xXXXX EOP
SYNC PID EOP
0x01 0xD2(ACK) EOP
OUT
IN
IN
USB
31
MS_uC / fue1 / V01 Example of input data transfer (2/4)
FIFO of the USB device is empty Address: 0x123 Endpoint: 2
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0x69(IN) 0x123 0x82 0xXX EOP
SYNC PID EOP
0x01 0x5A(NAK) EOP
OUT
IN
USB
32
MS_uC / fue1 / V01 Example of input data transfer (3/4)
USB device is disabled Address: 0x123 Endpoint: 2
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0x69(IN) 0x123 0x82 0xXX EOP
SYNC PID EOP
0x01 0x1E(STALL) EOP
OUT
IN
USB
33
MS_uC / fue1 / V01 Example of input data transfer (4/4)USB
34
MS_uC / fue1 / V01 Enumeration
Operating system must load the drivers for the USB devices Collect information about the device (descriptors) Assign a new address to the device
This process is called enumeration The enumeration proceeds as follows in Windows
Host tells Windows that a device has been connected Windows request from the device through address 0 its descriptor A new address is assigned to the device The device descriptor is requested again The configuration descriptor is requested The complete configuration descriptor (including interface and
endpoint descriptors) is requested Windows will search for the appropriate drivers and loads them Windows sets the device into the configured state
USB
35
MS_uC / fue1 / V01 USB Device Framework (OSI Layer 7)
Communication basis on driver & application level Collect information about the USB device (descriptors) Load the drivers for the USB device,
USB
36
MS_uC / fue1 / V01 USB Device Request
Host sends a “USB device request” to the USB device (endpoint0) Triggering the attribute exchanges (descriptors) of the USB
device
USB
37
MS_uC / fue1 / V01 Setup stage
Data transfer of the setup stage consists of the following 3 phases
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0x2D (Setup) 0x00 0x00 0xXX EOP
SYNC PID DATA CRC 16 EOP
0x01 0xC3(DATA0) USB Device Request 0xXXXX EOP
SYNC PID EOP
0x01 0xD2(ACK) EOP
OUT
OUT
IN
USB
38
MS_uC / fue1 / V01
Structure of the “USB Device Request“ (USB 2 Specification)
Offset Field Size(Bytes)
Description
0 bmRequestType 1 Characteristics of request: D7: Data transfer direction D6…5: Type D4…0: Recipient
1 bRequest 1 Specific request
2 wValue 2 Word-sized field that varies according to request
4 wIndex 2 Word-sized field that varies according to request; typically used to pass an index or Offset
6 wLength 2 Number of Bytes to transfer if there is a Data stage
USB
39
MS_uC / fue1 / V01 Specific requests
bRequest Description
CLEAR_FEATURE Delete or disable a feature
GET_CONFIGURATION Request the current configuration of the USB device
GET_DESCRIPTOR Request the descriptor specified in wValue
GET_INTERFACE Request the current interface settings of the USB device
GET_STATUS Request the current status of the USB device
SET_ADDRESS Change the address of the USB device
SET_CONFIGURATION Set the current configuration of the USB device
SET_DESCRIPTOR (Optional)
SET_FEATURE Enabled a feature in an interface or endpoint
SET_INTERFACE Sets the “alternative setting” for an interface
SYNCH_FRAME Used for synchronization (Isochronous Endpoints)
USB
40
MS_uC / fue1 / V01
Beispiel für USB Device Request19.04.23
41
MS_uC / fue1 / V01
42
SET_ADDRESS
SET_CONFIGURATION
GET_DESCRIPTOR
USB Example of USB device requests
42
MS_uC / fue1 / V01 USB device descriptors
Descriptors are data structures with a predefined format To exchange USB attributes between host and device
Type of descriptors Standard descriptors Class or vendor specific descriptors
Descriptors are asked with the request “GET_DESCRIPTOR” 1st byte of the descriptor contains its size 2nd byte of the descriptor informs about its type
USB
43
MS_uC / fue1 / V01 19.04.23
44
Standard descriptors (1/3)
Device Descriptor
Configuration Descriptor 1
Interface Descriptor 1
Endpoint Descriptor 1
Endpoint Descriptor n3
Interface Descriptor 2
Interface Descriptor n2
Endpoint Descriptor n3
Configuration Descriptor 2
Configuration Descriptor n1
String DescriptorID 1
String DescriptorID 2
String DescriptorID 3
iManufacturer
iProduct
iSerialNumber
String DescriptorID 4
(optional)
bNumConfigurations
bNumInterfaces
bNumEndpoints
iConfiguration
String DescriptorID 5
(optional)
iInterface
(optional)
MS_uC / fue1 / V01 Standard descriptors (2/3)
Describe the basic features and properties of the device
Device Descriptor USB-IF device class Supported standards (USB 1.1 or 2.0) Manufacturer Number of configurations
Configuration descriptor Size of the interface descriptors Size of the endpoint descriptors Power consumption of the various configurations
USB
45
MS_uC / fue1 / V01 Standard descriptors (3/3)
Interface Descriptor USB-IF device class Number of endpoints
Endpoint Descriptor Endpoint number (address) Transmission way (input or output) Transfer type
Bulk, interrupt or isochronous
String Descriptor Description of the USB device in text form
Class or vendor specific descriptors Specific to the USB-IF Device Classes
USB
46
MS_uC / fue1 / V01 Descriptor example (1/3)
Device DescriptorbLength 0x12 The Descriptor has a length of 18 bytesbDescriptorType 0x01 Device DescriptorbcdUSB 0x0200 USB Specification Number in BCD (2.00) bDeviceClass 0x00 Class is defined in the Interface-DescriptorbDeviceSubClass 0x00 Sub-Class is defined in the Interface-DescriptorbDeviceProtocol 0x00 Protocol is defined in the Interface-DescriptorbMaxPacketSize0 0x40 Size of Endpoint 0: 64 bytesidVendor 0x054C Vendor ID 0x054c (Sony Corporation)idProduct 0x0243 Product ID 0x0243bcdDevice 0x0100 Device release Number in BCD (1.00)iManufacturer 0x01iProduct 0x02iSerialNumber 0x03bNumConfigurations 0x01 1 configuration
USB
47
MS_uC / fue1 / V01 19.04.23
48
Descriptor example (2/3)
Configuration DescriptorbLength 0x09 The Descriptor has a length of 9 bytesbDescriptorType 0x02 Configuration DescriptorwTotalLength 0x0020 Total size of the Configuration-, Interface-, Endpoint-
DescriptorsbNumInterfaces 0x01 contains 1 interfacebConfigurationValue 0x01 this is configuration 1iConfiguration 0x00 no string descriptorbmAttributes 0x80 Bus PoweredbMaxPower 0x32 50 x 2 mA = 100 mA
Interface DescriptorbLength 0x09 The Descriptor has a length of 9 bytesbDescriptorType 0x04 Interface DescriptorbInterfaceNumber 0x00 this is interface 0 from configuration 1bAlternateSetting 0x00 no alternate settingbNumEndpoints 0x02 2 Endpoints (1 Bulk-IN, 1 Bulk-OUT)bInterfaceClass 0x08 Mass Storage Device ClassbInterfaceSubClass 0x06 SCSI transparent command setbInterfaceProtocol 0x50 Bulk-Only TransportiInterface 0x00 no string descriptor
MS_uC / fue1 / V01
49
Endpoint Descriptor 1bLength 0x07 The Desc. has a length of 7 bytesbDescriptorType 0x05 Endpoint DescriptorbEndpointAddress 0x81 Address 0x81, IN-EndpointTransfer Type 0x02 Bulk EndpointwMaxPacketSize 0x0200 FIFO-Size 512 BytesbInterval 0xFF not used for Bulk endpoints
Endpoint Descriptor 2bLength 0x07 The Desc. has a length of 7 bytesbDescriptorType 0x05 Endpoint DescriptorbEndpointAddress 0x02 Address 0x02, OUT-EndpointTransfer Type 0x02 Bulk EndpointwMaxPacketSize 0x0200 FIFO-Size 512 BytesbInterval 0xFF not used for Bulk endpoints
USB Descriptor example (3/3)
MS_uC / fue1 / V01USB-IF device classes
There are standard drivers for USB devices New USB devices can be developed without specific drivers
Mouse, keyboards ...
Standardization is achieved with the help of device classes bInterfaceClass bInterfaceSubClass bInterfaceProtocol
USB
50
MS_uC / fue1 / V01
Geräteklassen des USB Implementers ForumsClass (back) Base Class ID Descriptor Usage
Typical Devices
Audio Class 0x01 Interface Sound cards
Communication Device Class
0x02, 0x0A Interface USB to serial adapter,modem, …
Human Interface Device (HID)
0x03 Device, Interface Mouse, Keyboards, joystick …
Still Image Capture Device
0x06 Interface
Printer Class 0x07 Interface Printer
Mass Storage Device 0x08 Interface Memory stick, hard disk, floppy …
Hub 0x09 Device Hubs
Smart Card 0x0B Interface
Content Security 0x0D Interface
Video Class 0x0F Interface Video capture devices
Diagnostic Device 0xDC Device, Interface
Wireless Controller 0xE0 Interface Bluetooth, wireless USB
Miscellaneous 0xEF Device, Interface Palm, USB wireless
IrDA Bridge Device Class: 0xFESub class: 0x02
Interface IrDA devices
Test and measurements Class: 0xFESub class: 0x03
Interface Measure instruments
Vendor Specific 0xFF Device, Interface Some MP3 Player,CARME JTAG debugger
MS_uC / fue1 / V01 USB Keil library
USBD_Reset()
USBD_Suspend()
USBD_Resume()
USBD_P_SOF_Event()
USBD_EndPointx()
Enumeration
Reset Suspend
Resume
Out_Packet SOF
IN_Packet USBD_EndPointx()
USBD_Init()
OTG_FS_IRQHandler()
USB
52