Build a USB 2.0 device from scratch Friday 15, July 2016 Philmon - - PowerPoint PPT Presentation

build a usb 2 0 device from scratch
SMART_READER_LITE
LIVE PREVIEW

Build a USB 2.0 device from scratch Friday 15, July 2016 Philmon - - PowerPoint PPT Presentation

Build a USB 2.0 device from scratch Friday 15, July 2016 Philmon `PhilGekni` Gardet <phil@lse.epita.fr> 1 / 34 2 / 34 Plan The Overview The Protocol layers Implementation considerations 3 / 34 The Overview 4 / 34 Host


slide-1
SLIDE 1

Build a USB 2.0 device from scratch

Friday 15, July 2016 Philémon `PhilGekni` Gardet <phil@lse.epita.fr>

1 / 34

slide-2
SLIDE 2

2 / 34

slide-3
SLIDE 3

Plan

  • Implementation considerations
  • The Protocol layers
  • The Overview

3 / 34

slide-4
SLIDE 4

The Overview

4 / 34

slide-5
SLIDE 5

Topology

Host

RootHub Hub Hub Hub Hub Hub Hub Hub Dev Dev Dev Dev Dev Dev Dev Dev

7 levels Host

  • Manages exchanges

Hubs

  • Plug / Unplug

notifications

  • Power management

Devices

  • Offer functions

5 / 34

slide-6
SLIDE 6

Endpoints and pipes device Basic types

  • Input
  • Output
  • Control

Full-speed

  • 1 Control endpoint 0
  • 15 in endpoints max
  • 15 out endpoints

min

Device #? Control endpoint 0 Interface 1 In 1 Out 1 Interface 2 In 2 Out 2

Access by device + endpoint address

6 / 34

slide-7
SLIDE 7

Endpoints/pipes types

Interrupt transfer (≤ 64 Bytes)

Regular queries

Bulk transfer (≤ 1023 Bytes)

Large packet

Isochronous transfer (≤ 64 Bytes)

Constant bandwidth

Control transfers (≤ 64 Bytes)

Device setup / Hubs management / Status Full-speed bandwidth

Isochronous up to 69%

Interrupt 5% Bulk 5% Control 7%

7 / 34

slide-8
SLIDE 8

Device Class

  • Bass Class
  • Subclass
  • Protocol

Class Base ID Descriptor Usage Description 00h Device Refer to Interfaces 01h Interface Audio 02h Both Communication and CDC 09h Device Hub EFh Both Miscellaneous FEh Interface Application Specific FFh Both Vendor specific

8 / 34

slide-9
SLIDE 9

Vendor ID / Product ID Device signature

vid:pid

Vendor ID

Delivered by the USB-IF

Product ID

Chosen by vendor

www.linux-usb.org/usb.ids

9 / 34

slide-10
SLIDE 10

The Protocol layers

10 / 34

slide-11
SLIDE 11

Signals - The connector

Name Voltage Domain

Vcc 5 V D+ 3.3/0 V D- 3.3/0 V GND Ground

11 / 34

slide-12
SLIDE 12

Signals - The link between a hub and a device

Hub

Full-speed link (D+ pull-up)

D SE0 OE D D+ D-

Device

D SE0 OE D D+ D-

D+ D-

15KΩ 3.3V 15KΩ 1.5KΩ 12 / 34

Half-Duplex

slide-13
SLIDE 13

Signals - Decoding them - Differential level

13 / 34

slide-14
SLIDE 14

Signals - Decoding them - NRZI encoding

Data NRZI

0 1 1 0 1 0 0 0 1 1 1 0 1 1

14 / 34

slide-15
SLIDE 15

Signals - Decoding them - Bit stuffing

Stuffed Data NRZI Data

0 0 0 1 1 1 1 1 1 1 1 0 1 1

15 / 34

slide-16
SLIDE 16

Signals - Patterns

Sync / SOP

Start of packet

EOP

End of packet

Reset

Full-speed: J=1 and K=0

D+ D-

Idle K J K J K J K K

2 bits 10-20 mS

16 / 34

slide-17
SLIDE 17

Packets - A classical exchange

Token package Data package Handshake package

Upstream IN : Upstream OUT : Downstream IN : Downstream OUT : Upstream

Timeout : 6.5 - 7.5 bit times

(total window : 16 bit times)

17 / 34

slide-18
SLIDE 18

Packets - PID/Types

Token

OUT IN SETUP

Data

DATA0 DATA1

Handshake

ACK NAK STALL

PID code Check code

PID

4 bits 4 bits 1’ complement

18 / 34

slide-19
SLIDE 19

Packets - The formats

Data CRC16 PID SYNC EOF PID SYNC EOF Dev addr Endpt addr CRC5 PID SYNC EOF

Token packet Data packet Ack packet

8 bits 8 bits 7 bits 4 bits 5 bits 2 bits 16 bits

19 / 34

slide-20
SLIDE 20

Configuration - Descriptors

Device

descriptor

Configuration

descriptor

Configuration

descriptor

Interface

descriptor

Interface

descriptor

Interface

descriptor

Endpoint

descriptor

Endpoint

descriptor 20 / 34

slide-21
SLIDE 21

Configuration - Descriptors data

Device

descriptor

Configuration

descriptor

Interface

descriptor

Endpoint

descriptor

Class - Subclass - Protocol - VID - PID Max packet size endpoint 0 Power attributes - Max power Class - Subclass - Protocol Alternative setting Endpoint address - Endpoint type

21 / 34

slide-22
SLIDE 22

Configuration

  • Device status
  • Attached
  • Powered
  • Default
  • Address
  • Configured

22 / 34

slide-23
SLIDE 23

Configuration

  • Hub configured

23

Internal hub registers updating

23 / 34

slide-24
SLIDE 24

Configuration

  • Reset

24

  • Only one device
  • Adresse set to 0

24 / 34

slide-25
SLIDE 25

Configuration

  • Set address

25

  • Only one device

with 0 address

  • Host -> Endpoint 0

25 / 34

slide-26
SLIDE 26

Configuration

  • Configure device
  • Gets

Descriptors

  • Choses

descriptors

26 / 34

slide-27
SLIDE 27

Implementation considerations

27 / 34

slide-28
SLIDE 28

Hardware role

  • Differential

level

  • NRZI
  • Bit stuffing
  • Bit stuffing

error detection

Payload SYNC EOF

28 / 34

slide-29
SLIDE 29

Internals

NRZI & Bit stuffing NRZI & Bit stuffing Shift register Shift register Buffer Buffer Hold Rx Tx RRDY RACK TMT TRDY ERR Rx Tx Hold

29 / 34

slide-30
SLIDE 30

Internals

Status Manager PLL NRZI & Bit stuffing NRZI & Bit stuffing Shift register Shift register Buffer Buffer Hold Hold Rx Tx OE SE0 D+ D- PLL_E D- D+ TOE ERR Rx Tx RRDY RACK TMT TRDY D+ D-

30 / 34

RESET

slide-31
SLIDE 31

PLL and resynchronization D+ D-

  • ALTPLL
  • ALTPLL_RECONFIG

“ Dynamic adjustment of the

charge-pump current and loop- filter components to facilitate dynamic reconfiguration of the PLL bandwidth. This feature is available only in Arria GX, HardCopy II, Stratix II, Stratix II GX, Stratix III, and Stratix IV devices. “

Sync pattern

31 / 34

slide-32
SLIDE 32

Deviation : PHY USB

Microchip USB3320

32 / 34

slide-33
SLIDE 33

Conclusion

33 / 34

slide-34
SLIDE 34

Bibliography & links

  • Universal Serial Bus Revision 2.0 specification
  • Universal Serial Bus System Architecture

ANDERSON Don, DZATKO Dave. Addison-Wesley, 2001

  • USB in a NutShell

http://www.beyondlogic.org/usbnutshell

  • USB class specifications

http://www.usb.org/developers/docs/devclass_docs

  • Open Sources PID/VID

http://pid.codes/

34 / 34