410 likes | 550 Views
Programming Microcontrollers USB – Universal Serial Bus Autumn term 2009. 64K or 96K Byte SRAM. Enter MAC. ARM966E. CORE. w/DSP. USB 2.0FS. 96 MHz. 256K or 512K Byte Burst Flash. PFQ BC. CAN 2.0B. DMA. INTR Cantle. CLK Cantle. 32K Byte Burst Flash. GPIO. OTP Memo.
E N D
Programming Microcontrollers USB – Universal Serial Bus Autumn term 2009 64K or 96K Byte SRAM Enter MAC ARM966E CORE w/DSP USB 2.0FS 96 MHz 256K or 512K Byte Burst Flash PFQ BC CAN 2.0B DMA INTR Cantle CLK Cantle 32K Byte Burst Flash GPIO OTP Memo LVD BOD EXT. Bus JTAG ETM9 PLL RTC TIM ADC SPI I2C UART USB - Universal Serial Bus STR912FAW44
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. History USB - Universal Serial Bus
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) Generality USB - Universal Serial Bus
USB Topology USB - Universal Serial Bus • 127 devices can be connected to the USB bus (incl. Hubs) • Each device receive a unique address from the Host (PC)
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 Power supply USB - Universal Serial Bus
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 Electrical Interface USB - Universal Serial Bus
USB connectors USB - Universal Serial Bus
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 Connection & speed investigation USB - Universal Serial Bus
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 Bus states (1) USB - Universal Serial Bus
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 Bus states (2) USB - Universal Serial Bus
State Diagram USB - Universal Serial Bus
The transmission begins with the LSB (Least Significant Bit) The transmitted bits are coded in to NRZI Non-Return-To-Zero-Inverted Data coding USB - Universal Serial Bus
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 protocol USB - Universal Serial Bus
USB Communication flow USB - Universal Serial Bus
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 Transfer type (1) USB - Universal Serial Bus
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 Transfer type (2) USB - Universal Serial Bus
The data are transmitted within packages Token-Packets SOP Setup IN & OUT Data-Packets Handshake-Packets Special-Packets Data Package USB - Universal Serial Bus
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 Structure of the packages USB - Universal Serial Bus
SOF (Token-Packet) USB - Universal Serial Bus • 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
Setup (Token-Packet) USB - Universal Serial Bus • The Host announces the sending of setup data with this Package • ADDR: 7-Bit Device-Address • ENDP: 4-Bit Endpoint Number
IN & OUT (Token-Packet) USB - Universal Serial Bus • 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
DATA0 / DATA1 (Data-Packet) USB - Universal Serial Bus • 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
Handshake-Packets USB - Universal Serial Bus • 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
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) Data transmission USB - Universal Serial Bus
Output transmission example (1) USB - Universal Serial Bus • Transmission without errors • Address: 0x123 • Endpoint: 1 • Data: 0x00, 0x11, 0x22, 0x33 OUT OUT IN
Output transmission example (2) USB - Universal Serial Bus • The FIFO is full • Address: 0x123 • Endpoint: 1 • Data: 0x00, 0x11, 0x22, 0x33 OUT OUT IN
Output transmission example (3) USB - Universal Serial Bus • The USB device is not activated • Address: 0x123 • Endpoint: 1 • Data: 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 OUT OUT IN
Input transmission example (1) USB - Universal Serial Bus • Transmission without errors • Address: 0x123 • Endpoint: 2 • Data: 0x00, 0x11, 0x22, 0x33 OUT IN IN
Input transmission example (2) USB - Universal Serial Bus • The FIFO is full • Address: 0x123 • Endpoint: 2 OUT IN
Input transmission example (3) USB - Universal Serial Bus • The USB device is not activated • Address: 0x123 • Endpoint: 2 OUT IN
USB Device Framework define how to communicate at the driver layer A firmware programmer will be interested basically at this layer USB Device Framework (OSI Layer 7) USB - Universal Serial Bus
The Host sends a “USB Device Request” to the Devices to start an action Endpoint 0 Structure of a Request USB Device Request USB - Universal Serial Bus
Standard Device Requests USB - Universal Serial Bus
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 Descriptors USB - Universal Serial Bus
Hierarchy of the Standard descriptors USB - Universal Serial Bus
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 Type of standard descriptors (1) USB - Universal Serial Bus
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 Type of standard descriptors (2) USB - Universal Serial Bus
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 Enumeration USB - Universal Serial Bus
Example for an application (1) USB - Universal Serial Bus Device Descriptor bLength 0x12 The Descriptor has a length of 18 bytes bDescriptorType 0x01 Device Descriptor bcdUSB 0x0200 USB Specification Number in BCD (2.00) bDeviceClass 0x00 Class is defined in the Interface-Descriptor bDeviceSubClass 0x00 Sub-Class is defined in the Interface-Descriptor bDeviceProtocol 0x00 Protocol is defined in the Interface-Descriptor bMaxPacketSize0 0x40 Size of Endpoint 0: 64 bytes idVendor 0x054C Vendor ID 0x054c (Sony Corporation) idProduct 0x0243 Product ID 0x0243 bcdDevice 0x0100 Device release Number in BCD (1.00) iManufacturer 0x01 iProduct 0x02 iSerialNumber 0x03 bNumConfigurations 0x01 1 configuration
Example for an application (2) USB - Universal Serial Bus Configuration Descriptor bLength 0x09The Descriptor has a length of 9 bytes bDescriptorType 0x02Configuration Descriptor wTotalLength 0x0020 Total size of the Configuration-, Interface-, Endpoint- Descriptors 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 0x09The Descriptor has a length of 9 bytes bDescriptorType 0x04Interface Descriptor bInterfaceNumber 0x00 this is interface 0 from configuration 1 bAlternateSetting 0x00 no alternate setting bNumEndpoints 0x02 2 Endpoints (1 Bulk-IN, 1 Bulk-OUT) bInterfaceClass 0x08 Mass Storage Device Class bInterfaceSubClass 0x06 SCSI transparent command set bInterfaceProtocol 0x50 Bulk-Only Transport iInterface 0x00 no string descriptor
Example for an application (3) USB - Universal Serial Bus Endpoint Descriptor 1 bLength 0x07The Desc. has a length of 7 bytes bDescriptorType 0x05Endpoint Descriptor bEndpointAddress 0x81 Address 0x81, IN-Endpoint Transfer Type 0x02 Bulk Endpoint wMaxPacketSize 0x0200 FIFO-Size 512 Bytes bInterval 0xFF not used for Bulk endpoints Endpoint Descriptor 2 bLength 0x07The Desc. has a length of 7 bytes bDescriptorType 0x05Endpoint Descriptor bEndpointAddress 0x02Address 0x02, OUT-Endpoint Transfer Type 0x02 Bulk Endpoint wMaxPacketSize 0x0200 FIFO-Size 512 Bytes bInterval 0xFF not used for Bulk endpoints