USB - Universal Serial Bus MS_uC / fue1 / V10 11- 1 Programming Microcontrollers USB – Universal Serial Bus Autumn term 2009 ARM966 CORE E w/DSP 96 MHz DMA PFQ BC 64K or 96K Byte SRAM 256K or 512K Byte Burst Flash Enter MAC USB 2.0FS CAN 2.0B INTR CLK 32K Byte GPIO Cantl Cantl Burst Flash e e LVD PLL JTAG ETM9 OTP EXT. Memo Bus BOD ADC RTC TIM SPI STR912FAW44 I2C UART USB - Universal Serial Bus MS_uC / fue1 / V10 History 11- 2 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. USB - Universal Serial Bus MS_uC / fue1 / V10 Generality 11- 3 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) USB Topology USB - Universal Serial Bus MS_uC / fue1 / V10 11- 4 127 devices can be connected to the USB bus (incl. Hubs) Each device receive a unique address from the Host (PC) Host Root-Hub 1 2 Hub 3 Device 1 4 Device 2 Hub 5 Device 3 6 Device 4 USB - Universal Serial Bus MS_uC / fue1 / V10 USB Power supply 11- 5 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Electrical Interface 11- 6 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 USB connectors USB - Universal Serial Bus MS_uC / fue1 / V10 11- 7 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 USB - Universal Serial Bus MS_uC / fue1 / V10 11- 8 Connection & speed investigation 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 Full-Speed and High speed Device Low-Speed Device Bus states (1) USB - Universal Serial Bus MS_uC / fue1 / V10 11- 9 Low-Speed State BusDriver D+ D- Idle-State passive Low high J-State Active Low K-State Active SE0-State Active (Single End Zero) Full-/High-Speed D+ D- differential 0 high Low differential 1 high differential 0 high low differential 1 High low differential 1 low high differential 0 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Bus states (2) 11- 10 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 USB - Universal Serial Bus MS_uC / fue1 / V10 11- 11 State Diagram USB - Universal Serial Bus MS_uC / fue1 / V10 Data coding 11- 12 The transmission begins with the LSB (Least Significant Bit) The transmitted bits are coded in to NRZI Non-Return-To-Zero-Inverted USB - Universal Serial Bus MS_uC / fue1 / V10 USB protocol 11- 13 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) USB - Universal Serial Bus MS_uC / fue1 / V10 11- 14 USB Communication flow USB - Universal Serial Bus MS_uC / fue1 / V10 Transfer type (1) 11- 15 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Transfer type (2) 11- 16 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Data Package 11- 17 The data are transmitted within packages Token-Packets SOP Setup IN & OUT Data-Packets Handshake-Packets Special-Packets USB - Universal Serial Bus MS_uC / fue1 / V10 Structure of the packages 11- 18 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 SOF (Token-Packet) USB - Universal Serial Bus MS_uC / fue1 / V10 11- 19 Host / Hub sends a SOF every ms (Start-of-Frame) To avoid that the Device enter into the Suspend mode Low-Speed EOP EOP Full-Speed Frame # : 11-Bit frame number, which is systematically incremented SYNC PID Frame # CRC 5 EOP 0x01 0xA5 frame number 0xXX EOP USB - Universal Serial Bus MS_uC / fue1 / V10 Setup (Token-Packet) 11- 20 The Host announces the sending of setup data with this Package SYNC PID ADDR ENDP CRC 5 EOP 0x01 0x2D Device address Endpoint number 0xXX EOP ADDR: 7-Bit Device-Address ENDP: 4-Bit Endpoint Number USB - Universal Serial Bus MS_uC / fue1 / V10 IN & OUT (Token-Packet) 11- 21 IN: Le Host ask the Device to send him Data The Device will then send a Data to the Host. SYNC PID ADDR ENDP CRC 5 EOP 0x01 0x69 Device address Endpoint number 0xXX EOP 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 0xE1 Device address Endpoint number 0xXX EOP USB - Universal Serial Bus MS_uC / fue1 / V10 DATA0 / DATA1 (Data-Packet) 11- 22 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 SYNC PID DATA CRC 16 EOP 0x01 0xC3 Data bytes 0xXXXX EOP SYNC PID DATA CRC 16 EOP 0x01 0x4B Data bytes 0xXXXX EOP DATA1 USB - Universal Serial Bus MS_uC / fue1 / V10 Handshake-Packets 11- 23 ACK Confirmation that the package Data has been received without errors SYNC PID EOP 0x01 0xD2 EOP NAK is only send by the device The package Data could not be treated (OUT) There are no data to send (IN) SYNC PID EOP 0x01 0x5A EOP STALL is only send by the device The device is not activated SYNC PID EOP 0x01 0x1E EOP USB - Universal Serial Bus MS_uC / fue1 / V10 Data transmission 11- 24 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) Output transmission example (1) USB - Universal Serial Bus MS_uC / fue1 / V10 11- 25 Transmission without errors Address: 0x123 Endpoint: 1 Data: 0x00, 0x11, 0x22, 0x33 OUT OUT IN 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Output transmission example (2) 11- 26 The FIFO is full Address: 0x123 Endpoint: 1 Data: 0x00, 0x11, 0x22, 0x33 OUT OUT IN 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Output transmission example (3) 11- 27 The USB device is not activated Address: 0x123 Endpoint: 1 Data: 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 OUT OUT IN 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 Input transmission example (1) USB - Universal Serial Bus MS_uC / fue1 / V10 11- 28 Transmission without errors Address: 0x123 Endpoint: 2 Data: 0x00, 0x11, 0x22, 0x33 OUT IN IN 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 Input transmission example (2) USB - Universal Serial Bus MS_uC / fue1 / V10 11- 29 The FIFO is full Address: 0x123 Endpoint: 2 OUT IN SYNC PID ADDR ENDP CRC 5 EOP 0x01 0x69(IN) 0x123 0x02 0xXX EOP SYNC PID EOP 0x01 0x5A(NAK) EOP Input transmission example (3) USB - Universal Serial Bus MS_uC / fue1 / V10 11- 30 The USB device is not activated Address: 0x123 Endpoint: 2 OUT IN SYNC PID ADDR ENDP CRC 5 EOP 0x01 0x69(IN) 0x123 0x02 0xXX EOP SYNC PID EOP 0x01 0x1E(STALL) EOP USB - Universal Serial Bus MS_uC / fue1 / V10 11- 31 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 USB - Universal Serial Bus MS_uC / fue1 / V10 USB Device Request 11- 32 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 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 - Universal Serial Bus MS_uC / fue1 / V10 Standard Device Requests 11- 33 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Descriptors 11- 34 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 Hierarchy of the Standard descriptors USB - Universal Serial Bus MS_uC / fue1 / V10 String Descriptor ID 1 11- 35 iManufacturer String Descriptor ID 2 iProduct String Descriptor ID 3 iSerialNumber String Descriptor ID 4 (optional) Device Descriptor bNumConfigurations Configuration Descriptor 1 iConfiguration (optional) Configuration Descriptor 2 Configuration Descriptor n1 bNumInterfaces String Descriptor ID 5 iInterface (optional) Interface Descriptor 1 Interface Descriptor 2 Interface Descriptor n2 bNumEndpoints Endpoint Descriptor 1 Endpoint Descriptor n3 Endpoint Descriptor n3 USB - Universal Serial Bus MS_uC / fue1 / V10 Type of standard descriptors (1) 11- 36 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Type of standard descriptors (2) 11- 37 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Enumeration 11- 38 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 USB - Universal Serial Bus MS_uC / fue1 / V10 Example for an application (1) 11- 39 Device Descriptor bLength bDescriptorType bcdUSB bDeviceClass bDeviceSubClass bDeviceProtocol bMaxPacketSize0 idVendor idProduct bcdDevice iManufacturer iProduct iSerialNumber bNumConfigurations 0x12 0x01 0x0200 0x00 0x00 0x00 0x40 0x054C 0x0243 0x0100 0x01 0x02 0x03 0x01 The Descriptor has a length of 18 bytes Device Descriptor USB Specification Number in BCD (2.00) Class is defined in the Interface-Descriptor Sub-Class is defined in the Interface-Descriptor Protocol is defined in the Interface-Descriptor Size of Endpoint 0: 64 bytes Vendor ID 0x054c (Sony Corporation) Product ID 0x0243 Device release Number in BCD (1.00) 1 configuration USB - Universal Serial Bus MS_uC / fue1 / V10 Example for an application (2) 11- 40 Configuration Descriptor bLength bDescriptorType wTotalLength 0x09 The Descriptor has a length of 9 bytes 0x02 Configuration Descriptor 0x0020 Total size of the Configuration-, Interface-, EndpointDescriptors bNumInterfaces 0x01 contains 1 interface bConfigurationValue 0x01 this is configuration 1 iConfiguration 0x00 no string descriptor bmAttributes 0x80 Bus Powered bMaxPower 0x32 50 x 2 mA = 100 mA Interface Descriptor bLength 0x09 bDescriptorType 0x04 bInterfaceNumber 0x00 bAlternateSetting 0x00 bNumEndpoints 0x02 bInterfaceClass 0x08 bInterfaceSubClass 0x06 bInterfaceProtocol 0x50 iInterface 0x00 The Descriptor has a length of 9 bytes Interface Descriptor this is interface 0 from configuration 1 no alternate setting 2 Endpoints (1 Bulk-IN, 1 Bulk-OUT) Mass Storage Device Class SCSI transparent command set Bulk-Only Transport no string descriptor USB - Universal Serial Bus MS_uC / fue1 / V10 Example for an application (3) 11- 41 Endpoint Descriptor 1 bLength bDescriptorType bEndpointAddress Transfer Type wMaxPacketSize bInterval 0x07 0x05 0x81 0x02 0x0200 0xFF The Desc. has a length of 7 bytes Endpoint Descriptor Address 0x81, IN-Endpoint Bulk Endpoint FIFO-Size 512 Bytes not used for Bulk endpoints Endpoint Descriptor 2 bLength bDescriptorType bEndpointAddress Transfer Type wMaxPacketSize bInterval 0x07 0x05 0x02 0x02 0x0200 0xFF The Desc. has a length of 7 bytes Endpoint Descriptor Address 0x02, OUT-Endpoint Bulk Endpoint FIFO-Size 512 Bytes not used for Bulk endpoints