ms_uc / fue1 / v10 11- 1 usb - universal serial bus programming microcontrollers usb – universal...
Post on 20-Dec-2015
218 views
TRANSCRIPT
11- 1
USB - Universal Serial Bus
MS_uC / fue1 / V10
Programming MicrocontrollersUSB – Universal Serial Bus
Autumn term 2009
STR912FAW44
32K Byte 32K Byte Burst FlashBurst Flash
64K or 96K 64K or 96K Byte SRAM Byte SRAM
256K or 512K 256K or 512K Byte Burst Byte Burst
FlashFlash
OTP OTP MemMem
ooUARTUARTI2CI2CSPISPITIMTIMRTCRTC
EXT. EXT. Bus Bus
GPIOGPIO
USB USB 2.0FS 2.0FS
CAN CAN 2.0B 2.0B
EnteEnter r
MAC MAC
PFPFQ Q
BC BC
DMADMA
INTINTR R
CantCantlele
ARM96ARM966E 6E CORE CORE
w/DSPw/DSP96 MHz 96 MHz
CLK CLK CanCantletle
ADCADC
LVD LVD BODBOD
PLLPLL JTAJTAGG
ETMETM99
11- 2
USB - Universal Serial Bus
MS_uC / fue1 / V10
History
The USB Implements Forum (USB IF) has been founded in 1995 The first standard (USB 1.0) has been specified a year later
USB was planned to connect different peripheral devices to a universal interface Mouse, keyboard, printer etc.
The USB 2.0 standard follows in 2000 Speed has been enhanced to 480 MBit/s
This enables to connect also hard discs, video cameras etc.
11- 3
USB - Universal Serial Bus
MS_uC / fue1 / V10
Generality
USB is a asymmetrical bus We distinguish between Host (PC) and Device (also called function)
The type of transmission is asynchronous serial bit
The transmitted speed are 1.5MBit/s (Low-Speed) 12MBit/s (Full-Speed) 480MBit/s (High-Speed, only for USB 2.0)
Since USB 2.0, it is also possible to connect the devices between them OTG (On-The-Go)
11- 4
USB - Universal Serial Bus
MS_uC / fue1 / V10
USB Topology
127 devices can be connected to the USB bus (incl. Hubs) Each device receive a unique address from the Host (PC)
Host
Root-Hub
Hub
Hub
Device 1
Device 2
Device 3 Device 4
1 2
3 4
5 6
11- 5
USB - Universal Serial Bus
MS_uC / fue1 / V10
USB Power supply
Bus-Powered Hubs Supply voltage is furnished by the host/hub The total power consumption of the connected devices must be <
500 mA
Self-Powered-Hubs Supply voltage is furnished separately Each device can consume 500 mA
11- 6
USB - Universal Serial Bus
MS_uC / fue1 / V10
Electrical Interface
The USB cable contains 4 lines VCC (+5V) GND D+ and D- (0 and 3.3 V), which work in a differential mode
Difference bigger than +200 mV represents a logic 1 Difference smaller than -200 mV represents a logic 0
11- 7
USB - Universal Serial Bus
MS_uC / fue1 / V10
USB connectors
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 connected
11- 8
USB - Universal Serial Bus
MS_uC / fue1 / V10 Connection & speed investigation
Full-Speed and High speed Device
Low-Speed Device
The Host/Hub must recognize the devices that are connected to its outputs The Host/Hub begins therefore with the speed investigation
Pull up or pull down resistors
11- 9
USB - Universal Serial Bus
MS_uC / fue1 / V10
Bus states (1)
Low-Speed Full-/High-Speed
StateBus-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(Single End Zero)
Active Low low NA low low NA
SOP (Start-of-Packet) The transition from the “idle” in to the “K” state This change is the first bit of the SYNC field
EOP (End-of-Packet) The “SE0” state is kept during 2 cycles followed by the “Idle” state
11- 10
USB - Universal Serial Bus
MS_uC / fue1 / V10
Bus states (2)
Bus Reset SE0 is activated during 2.5 ms
Suspend A USB device must enter in to a sleep mode to save energy
If there is no activity on the USB bus during 3 ms
The High-Speed devices go in to the Full-Speed modes after 3 ms After 100 to 875 µs they go in to the suspend state
Resume A suspended device must be wake up with a resume sequence
Data lines must be set in to the “K” state during 20 ms followed by a SOF sequence
11- 11
USB - Universal Serial Bus
MS_uC / fue1 / V10
State Diagram
11- 12
USB - Universal Serial Bus
MS_uC / fue1 / V10
Data coding
The transmission begins with the LSB (Least Significant Bit)
The transmitted bits are coded in to NRZI Non-Return-To-Zero-Inverted
11- 13
USB - Universal Serial Bus
MS_uC / fue1 / V10
USB protocol
The USB is a polled bus (nobody speaks if not asked. No interrupts)
The Host controller initiates all data transfers Transaction type and direction USB address Endpoint (token package)
11- 14
USB - Universal Serial Bus
MS_uC / fue1 / V10
USB Communication flow
11- 15
USB - Universal Serial Bus
MS_uC / fue1 / V10
Transfer type (1)
Control Transfer The Control transfer enables to configure the Device The Host sends the requests to the Device 10% of the band width is used for this kind of transfer
Interrupt Transfer Transfer of a few amount of data, which must be send periodically
Mouse, keyboards etc. The Host polls the Endpoints
Period is defined in the Endpoint 90% of the Bandwidth is reserved for Interrupt and Isochronous
Transfers
11- 16
USB - Universal Serial Bus
MS_uC / fue1 / V10
Transfer type (2)
Bulk Transfer Transfer of great amounts of data, which is not time critical
Printers, scanner and memory sticks Transfer are realized with the remaining Bandwidth
Isochronous Transfer 90% of the Bandwidth is reserved for Interrupt and Isochronous
Transfers The data are transferred one time per frame of 1 ms
11- 17
USB - Universal Serial Bus
MS_uC / fue1 / V10
Data Package
The data are transmitted within packages Token-Packets
SOP Setup IN & OUT
Data-Packets
Handshake-Packets
Special-Packets
11- 18
USB - Universal Serial Bus
MS_uC / fue1 / V10
Structure of the packages
SYNC Each package begins with the SYNC field Synchronization of the Device PLL with the Host one
PID The second field is the PID (Packet-Identifier) one The 4 first bits are the complement of the 4 last ones
The next fields depend on the package types
The CRC (Cyclic Redundancy Check) is often the next-to-last field
The EOP is the last field
11- 19
USB - Universal Serial Bus
MS_uC / fue1 / V10
SOF (Token-Packet)
Host / Hub sends a SOF every ms (Start-of-Frame) To avoid that the Device enter into the Suspend mode
Low-Speed
Full-Speed Frame # : 11-Bit frame number, which is systematically
incremented
EOP
EOP
SYNC PID Frame # CRC 5 EOP
0x01 0xA5 frame number 0xXX EOP
11- 20
USB - Universal Serial Bus
MS_uC / fue1 / V10
Setup (Token-Packet)
The Host announces the sending of setup data with this Package
ADDR: 7-Bit Device-Address ENDP: 4-Bit Endpoint Number
SYNC PID ADDR ENDP CRC 5 EOP
0x01 0x2D Device address Endpoint number
0xXX EOP
11- 21
USB - Universal Serial Bus
MS_uC / fue1 / V10
IN & OUT (Token-Packet)
IN: Le Host ask the Device to send him Data The Device will then send a Data to the Host.
OUT: The Host tells to the Device, that he will send Data to him The Host will then send a Data to the Device
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
11- 22
USB - Universal Serial Bus
MS_uC / fue1 / V10
DATA0 / DATA1 (Data-Packet)
The package DATA0 and DATA1 contain the data to transmit
DATA0 and DATA1 are send alternatively for synchronization purposes 0..8 Bytes for Low Speed 0..1023 Bytes for Full Speed 0..1024 Bytes for High Speed
DATA0
DATA1
SYNC PID DATA CRC 16 EOP
0x01 0xC3 Data bytes 0xXXXX EOP
SYNC PID DATA CRC 16 EOP
0x01 0x4B Data bytes 0xXXXX EOP
11- 23
USB - Universal Serial Bus
MS_uC / fue1 / V10
Handshake-Packets
ACK Confirmation that the package Data has been received without
errors
NAK is only send by the device The package Data could not be treated (OUT) There are no data to send (IN)
STALL is only send by the device The device is not activated
SYNC PID EOP
0x01 0xD2 EOP
SYNC PID EOP
0x01 0x1E EOP
SYNC PID EOP
0x01 0x5A EOP
11- 24
USB - Universal Serial Bus
MS_uC / fue1 / V10Data transmission
The Data transmission is realized during 3 phases The host sends a token package to the device (phase 1) Either the host or the device sends the data (phase 2) The successful reception is confirmed either by the Host or by the
Device with a handshake (phase 3)
11- 25
USB - Universal Serial Bus
MS_uC / fue1 / V10
Output transmission example (1)
Transmission without errors 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
11- 26
USB - Universal Serial Bus
MS_uC / fue1 / V10
Output transmission example (2)
The FIFO 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
11- 27
USB - Universal Serial Bus
MS_uC / fue1 / V10
Output transmission example (3)
The USB device is not activated Address: 0x123 Endpoint: 1 Data: 0x00, 0x11, 0x22, 0x33, 0x44, 0x55
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
11- 28
USB - Universal Serial Bus
MS_uC / fue1 / V10
Input transmission example (1)
Transmission without errors Address: 0x123 Endpoint: 2 Data: 0x00, 0x11, 0x22, 0x33
SYNC
PID ADDR ENDP CRC 5 EOP
0x01 0x69(IN) 0x123 0x02 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
11- 29
USB - Universal Serial Bus
MS_uC / fue1 / V10
Input transmission example (2)
The FIFO is full Address: 0x123 Endpoint: 2
SYNC
PID ADDR ENDP CRC 5 EOP
0x01 0x69(IN) 0x123 0x02 0xXX EOP
SYNC
PID EOP
0x01 0x5A(NAK) EOP
OUT
IN
11- 30
USB - Universal Serial Bus
MS_uC / fue1 / V10
Input transmission example (3)
The USB device is not activated Address: 0x123 Endpoint: 2
SYNC
PID ADDR ENDP CRC 5 EOP
0x01 0x69(IN) 0x123 0x02 0xXX EOP
SYNC
PID EOP
0x01 0x1E(STALL)
EOP
OUT
IN
11- 31
USB - Universal Serial Bus
MS_uC / fue1 / V10
USB Device Framework (OSI Layer 7)
USB Device Framework define how to communicate at the driver layer
A firmware programmer will be interested basically at this layer
11- 32
USB - Universal Serial Bus
MS_uC / fue1 / V10
USB Device Request
The Host sends a “USB Device Request” to the Devices to start an action Endpoint 0
Structure of a Request
Offset Field Size(Bytes)
Description
0 bmRequestType 1 Characteristics of request
1 bRequest 1 Specific request
2 wValue 2 Word-sized field that varies according torequest
4 wIndex 2 Word-sized field that varies according torequest; typically used to pass an index oroffset
6 wLength 2 Number of Bytes to transfer if there is aData stage
11- 33
USB - Universal Serial Bus
MS_uC / fue1 / V10
Standard Device Requests
bRequest Description
CLEAR_FEATURE Clear or activate a features
GET_CONFIGURATION Ask the Device to send its actual configuration number
GET_DESCRIPTOR Ask the Device to send its descriptor, which has been specified with wValue
GET_INTERFACE Ask the Device to send its actual “alternate setting“
GET_STATUS Ask the Device to send its actual state
SET_ADDRESS Change the Device address
SET_CONFIGURATION Set the Device in to a new configuration
SET_DESCRIPTOR (Optional)
SET_FEATURE Activate a new Feature in a Interface or an Endpoint
SET_INTERFACE Set the “alternate setting“ for the Interface
SYNCH_FRAME Can be used to synchronize the Isochronous Endpoints
11- 34
USB - Universal Serial Bus
MS_uC / fue1 / V10
Descriptors
Descriptors are predefined structures, which enable to exchange attributes between the host and the device
Type of descriptors Standard descriptors Specific descriptors
Class or Vendor
The descriptors are asked from the Host through the Standard Device Request “GET_DESCRIPTOR” The first Byte contains the size of the descriptor The second the type of the descriptor
The standard descriptors describes the standard function and the properties of the USB Device
11- 35
USB - Universal Serial Bus
MS_uC / fue1 / V10
Hierarchy of the Standard descriptors
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)
11- 36
USB - Universal Serial Bus
MS_uC / fue1 / V10
Type of standard descriptors (1)
Device Descriptor Class of the Device The supported standard (USB 1.1 or 2.0) The manufacture A list of the Device Configurations
Configuration Descriptor Class of the Device A list of the Configuration Interfaces A list of the Endpoints
Interface Descriptor Class of the Device A list of the Endpoints
11- 37
USB - Universal Serial Bus
MS_uC / fue1 / V10
Type of standard descriptors (2)
Endpoint Descriptor Endpoint properties Endpoint number Endpoint direction (IN or OUT) Transfer type
Bulk, Interrupt or Isochronous
String Descriptor Text description of the Device
Class of Vendor-Specific Descriptors Descriptor that are specific to the Device classes
11- 38
USB - Universal Serial Bus
MS_uC / fue1 / V10
Enumeration
The OS must install the drivers for the new connected Device
This task is called Enumeration and it is executed in Windows as follow Host tells Windows that a new Device has been connected Windows calls the Device descriptor of the Device with the
temporary address 0 A new address is attributed to the Device The Device descriptor is called for a second time The Configuration descriptor is called without the Interface &
Endpoint descriptors The complete Configuration descriptor is called Windows search the corresponding drivers and install them Window configure the Device
11- 39
USB - Universal Serial Bus
MS_uC / fue1 / V10
Example for an application (1)
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
11- 40
USB - Universal Serial Bus
MS_uC / fue1 / V10
Example for an application (2)
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
11- 41
USB - Universal Serial Bus
MS_uC / fue1 / V10
Example for an application (3)
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