-
Notifications
You must be signed in to change notification settings - Fork 0
/
USBDOcPrepared.tex
1151 lines (1042 loc) · 57.4 KB
/
USBDOcPrepared.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{article}
\usepackage[a4paper, left=0.5in, right=0.4in, top=0.5in, bottom=0.5in]{geometry}
\usepackage{xparse}
\usepackage{float}
\usepackage{hyperref}
\usepackage{cite}
\usepackage{graphicx}
\usepackage{adjustbox}
\usepackage{enumitem}
\usepackage{amsmath, amssymb}
\usepackage{tikz}
\usepackage{tikz-timing}[2014/10/29]
\usepackage{bytefield}
\usepackage{multirow}
\usepackage{xcolor}
\usepackage{rotating}
\usetikztiminglibrary[rising arrows]{clockarrows}
\NewDocumentCommand{\busref}{som}{\texttt{%
#3%
\IfValueTF{#2}{[#2]}{}%
\IfBooleanTF{#1}{\#}{}%
}}
\newcommand{\bitlabel}[2]{%
\bitbox[]{#1}{%
\raisebox{0pt}[2ex][0pt]{%
\turnbox{45}{\fontsize{7}{7}\selectfont#2}%
}%
}%
}
\title{USB - A Study}
\author{Narendiran S}
\date{03-07-2021}
\begin{document}
\Large
\maketitle
\section{Source: Dogan Ibrahim - Advanced PIC Microcontroller Projects in C From USB to RTOS with the PIC 18F Series (2008, Newnes)}
\begin{itemize}
\item By USB Implementation Forum Inc. (Compaq, Microsfot, Intel, NECT, HP, Lucent, Philips, etc.)
\item High-sepped Serial interface
\item 127 devices (7-bit address filed -- Address 0 is reserverd)
\item Thirty meters max
\item Maximum tiers (hubs) can be 6
\item USB 1.1 -- 11 Mbps \& USB 2.0 -- 480 Mbps
\begin{itemize}
\item Low Speed - 1.5 Mbps
\item Full Speed - 12 Mbps
\item High Speed - 480 Mbps
\end{itemize}
\item White $--$ Data- \& Green $--$ Data+ carry differential data signals and signle-ended data states.
\item USB signals are bi-phase and uses NRZI (Non-Return to Zero Inverted) data encoding.
\item{NRZI}
\begin{itemize}
\item Uses clock.
\item If the data bit is 1, the NRZI transitions at clock boudaries.
\item If the data bit is 0, no transitions.
\item NRZI may have long series of 0s or 1s, resulting in clock recovery difficulties.
\end{itemize}
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{CLK} & 2{l} 26{c} \\
\busref{DATA} & D{0}; D{1}; D{1}; D{1}; D{1}; D{0}; D{0}; D{1}; D{0}; D{0}; D{0}; D{0}; D{1};D{0};\\
\busref{NRZI} & h h; L; H; L; H; H; H; L; L; L; L; L; H; H;\\
\extracode
\begin{pgfonlayer}{background}
\begin{scope}[semitransparent,semithick]
\vertlines[red]{1,2,...,7}
\end{scope}
\end{pgfonlayer}
\end{tikztimingtable}
\end{center}
\item NRZI may have long series of 0s or 1s, resulting in clock recovery difficulties.
\item Hence, a concept of \verb|bit stuffing| is used where after every six consecutive ones, a 0 bit is stuffed.
\item Packet of data transmitted by host is sent to every device connected to the bus.
\item All the devices receive the signal, but only the addressed one accepts the data.
\item Only one device can transmit data to the host.
\item Device class - enables same device driver to be used for several devices having similar functionalities.
\item{Device Class}
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|}
\hline
\textbf{Device Class} & \textbf{Description} & \textbf{Example Devices} \\
\hline
0x00 & Reserved & - \\
\hline
0x01 & USB Audio Device & Sound Card \\
\hline
0x02 & USB Communications Device & Modem, Fax \\
\hline
0x03 & USB Human Interface Device & Keyboard, Mouse \\
\hline
0x07 & USB Printer Device & Printer \\
\hline
0x08 & USB Mass Storage Device & Memory Card, Flash Drive \\
\hline
0x09 & USB Hub Device & Hubs \\
\hline
0x0B & USB Smart Card Reader Device & Card Reader \\
\hline
0x0E & USB Video Device & Webcam, Scanner \\
\hline
0xE0 & USB Wireless Device & Bluetooth \\
\hline
\end{tabular}
\end{table}
\item Commom USB Terms
\begin{itemize}
\item Endpoint - Either a source or a sink of data. A single USB device can have sixteen IN endpoints and sixteen OUT endpoints.
\item Trasactions - transfer of data on the bus.
\item Pipe - Logic data connection between the host and an endpoint.
\end{itemize}
\end{itemize}
\subsection{Speed and Device plugged Identification on Bus}
\label{sec: deviIden}
\begin{itemize}
\item At the device end (client side or endpoint) of the bus, a 1.5K pull-up resistor is connected from D+ or D- to 3.3V.
\item For full-speed bus, D+ line is pulled to 3.3V through pull-up resistor.
\item For Low-speed bus, D- line is pulled to 3.3V through pull-up resistor.
\item When no device is plugged, the host sees a low on the data lines.
\item When a device is plugged in, either of the D+ or D- is pulled high, and the host knows that a devie is plugged into the bus.
\item The speed is determined by with line is pulled high.
\end{itemize}
\subsection{USB States}
\begin{table}[H]
\begin{center}
\begin{tabular}{|c|p{5cm}|p{10cm}|}
\hline
\textbf{USB States} & \textbf{Line Values (D+ and D-)} & \textbf{Description} \\
\hline
\hline
\textbf{IDLE} & Bus in idle state, one line is high and other line is low (which line is high and which line is low depends on the speed mode(See Section \ref{sec: deviIden})) & This is the state of lines before and after a packet transmission. Also, the device is plugged in. \\
\hline
\textbf{DETACHED} & Both lines are low. & No devices is plugged/connected. \\
\hline
\textbf{ATTACHED} & Either of the line is logic high. & Devices is plugged/connected. \\
\hline
\textbf{J State} & Same as idle & Same as idle \\
\hline
\textbf{K State} & Opposite of J State & Opposite of J State \\
\hline
\textbf{SE0} & Single Ended Zero State, both lines are pulled low & - \\
\hline
\textbf{SE1} & Single Ended One State, both lines are pulled high & Illegal condition, should never happen \\
\hline
\textbf{RESET} & pulling both the lines low (SE0 State) for at least 10ms. & When host wants to communicate with device on bus, it first sends a reset condition. \\
\hline
\textbf{EOP} & - & End of Packet state - SE0 state for 2 bit times, followed by J State for 1 bit time. \\
\hline
\textbf{KEEP ALIVE} & - & Keep Alive State is achieved after EOP State. Keep Alive is sent at least once every millisecond to keep the device from suspending. \\
\hline
\textbf{SUSPEND} & - & Used to Save Power, Suspend is done by not sendingy anything to a device for 3ms. Suspended device draws less than 0.5mA and must recognie reset and resume signal. \\
\hline
\textbf{RESUME} & Reversing the polarity of the singal on the data lines for at leaset 20ms followed by a low-speed EOP signal & Suspended device is woken using resume by sending resersed polrity signal for 20 ms and then low-speed EOP \\
\hline
\end{tabular}
\end{center}
\end{table}
\subsection{USB Bus Communication}
\begin{itemize}
\item USB is host-centric connectivity system, so host dictates the use of USB Bus.
\item Each device on the bus is assigned a unique USB address.
\item No slave device can assert signal on the bus, until the host asks for it.
\end{itemize}
\subsubsection{Initial Operation}
\begin{enumerate}[label=\roman*)]
\item A new device is plugged in is identified by either of D+ or D- line going high (Generally both lines are low).
\item When new device is blugged into bus, the USB host uses the address 0 to ask basic information from the device.
\item Then, host assigns unique USB address.
\item Then, host ask for and gets infomations such as name of manufacturer, device capacilities, product ID, etc.
\item Then two-way transactions on the bus can begin.
\end{enumerate}
\subsection{Packets}
\begin{itemize}
\item Data is transmitted on a USB bus using Packets.
\item A packet starts with a 8-bit sync pattern to allow receiver clock to synchronize with the data.
\item Followed by the \textbf{Packet Identifier (PID)} byte information.
\begin{itemize}
\item PID byte is a 8-bit long.
\item PID is 4bit + 4-bit coplemented PID.
\item Based on the which PID, four packes are available : \textit{Token, Data, Handshake, Special}
\end{itemize}
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|p{9cm}|}
\hline
\textbf{PID type - Packet} & \textbf{Bits} & \textbf{PID name} & \textbf{Description} \\
\hline
\multirow{4}{*}{Data Packet} & 11\textcolor{magenta}{00}\_0011 & DATA0 & Data Packet PID even \\
& 01\textcolor{magenta}{00}\_1011 & DATA1 & Data Packet PID odd \\
& 10\textcolor{magenta}{00}\_0111 & DATA2 & Data Packet PID high speed \\
& 00\textcolor{magenta}{00}\_1111 & MDATA & Data Packet PID high speed \\
\hline
\multirow{4}{*}{Handshake Packet} & 11\textcolor{magenta}{01}\_0010 & ACK & Receiver accepts packet (Receiver acknowledges that it has received) \\
& 01\textcolor{magenta}{01}\_1010 & NAK & Receiver did not accepts packet (Receiver cannot accept the packet) \\
& 10\textcolor{magenta}{01}\_0110 & NYET & No response from receiver \\
& 00\textcolor{magenta}{01}\_1110 & STALL & Stalled (Indicates endpoint is halted) \\
\hline
\multirow{4}{*}{Token Packet} & 11\textcolor{magenta}{10}\_0001 & OUT & Host to device transaction \\
& 01\textcolor{magenta}{10}\_1001 & IN & Device to host transaction \\
& 10\textcolor{magenta}{10}\_0101 & SOF & Start of frame \\
& 00\textcolor{magenta}{10}\_1101 & SETUP & Setup Command \\
\hline
\multirow{4}{*}{Special Packet} & 11\textcolor{magenta}{11}\_0000 & Reserved & Reserved \\
& 01\textcolor{magenta}{11}\_1000 & SPLIT & High-speed split transaction \\
& 10\textcolor{magenta}{11}\_0100 & PING & High-speed flow control \\
& 00\textcolor{magenta}{11}\_1100 & ERR & Split transaction error \\
& 00\textcolor{magenta}{11}\_1100 & PRE & Host preample \\
\hline
\end{tabular}
\end{table}
\item Followed by
\begin{itemize}
\item 0-1024 bytes of data, 2-byte CRC checksum in case of \textit{Data Packet}.
\item 7-bit address, a 4-bit ENDP (Endpoint Number) and a 5-bit CRC checksum in case of \textit{Token Packet}.
\item Nothing in case of \textit{Handshake Packet}.
\end{itemize}
\item Finally, ends with the end of packet (EOP) signal.
\end{itemize}
\begin{table}[H]
\centering
\caption{Data Packet}
\begin{tabular}{|c|c|c|c|c|}
\hline
\textbf{SYNC} & \textbf{PID} & \textbf{DATA} & \textbf{CRC} & \textbf{EOP} \\
\hline
8-bits & 4+4 bits & 0-1024 bytes & 16-bits & - \\
\hline
\end{tabular}
\end{table}
\begin{table}[H]
\centering
\caption{Token Packet}
\begin{tabular}{|c|c|c|c|c|c|}
\hline
\textbf{SYNC} & \textbf{PID} & \textbf{ADDR} & \textbf{ENDP} & \textbf{CRC} & \textbf{EOP} \\
\hline
8-bits & 4+4 bits & 7-bits & 4-bits & 5-bits & - \\
\hline
\end{tabular}
\end{table}
\begin{table}[H]
\centering
\caption{Handshake Packet}
\begin{tabular}{|c|c|c|}
\hline
\textbf{SYNC} & \textbf{PID} & \textbf{EOP} \\
\hline
8-bits & 4+4 bits & - \\
\hline
\end{tabular}
\end{table}
\subsection{Data Flow Types}
Data is transferred in a USB using
\begin{enumerate}
\item Bulk transfer
\item Interrupt transfer
\item Isochronous transfer
\item Control transfer
\end{enumerate}
\subsubsection{Bulk Transfer}
\begin{itemize}
\item Transfer lare amounts of data with no errors
\item No guarantee of bandwidth
\item If an endpoint is defined as OUT and uses bulk tranfer.
\begin{itemize}
\item Then host transfer data from host to endpoint.
\end{itemize}
\item If an endpoint is defined as IN and uses bulk tranfer.
\begin{itemize}
\item Then host transfer data from endpoint to host.
\end{itemize}
\item Used when slow data rate is not an issue.
\item No bulk transfer at low speed
\item 8-64 packets at full speed
\item 512 packets at high speed
\end{itemize}
\subsubsection{Interrupt Transfer}
\begin{itemize}
\item Transfer small amount of data with high bandwidth.
\item 1-8 bytes at low speed
\item 1-64 byutes packets at full speed
\item upto 1024 bytes at high speed
\end{itemize}
\subsubsection{Isochronous Transfer}
\begin{itemize}
\item Guaranteed bandwidth with no guaranteed error-free
\item Audio data (where speed is important but loss of data is not importnat)
\item No Isochronous transfer at low speed
\item 1023 bytes at full speed
\item 1024 bytes at high speed
\end{itemize}
\subsubsection{Control Transfer}
\begin{itemize}
\item Bidirectional Data transferred
\item using both IN and OUT endpoints.
\item for initial configuation of device by host.
\item 8-bytes at low Speed
\item 8-64 bytes full Speed
\item 64-bytes at high speed.
\end{itemize}
\subsection{Enumeration}
When a device is plugged into a USB bus, it is known to the host through \textbf{Enumeration}.
The initial communication between the host and the device is carried out using the \emph{control transfer} type of data flow.
\begin{itemize}
\item When a device is plugged in, the host becomes aware of it because one of thedata lines (D+ or D-) becomes logic high.
\item The host sends a USB reset signal to the device to place the device in a known state.
\item The reset device responds to address 0.
\item The host sends a request on address 0 to the device to find out its maximum packet size using a \emph{Get Descriptor} command.
\item The device responds by sending a small portion of the device descriptor.
\item The host sends a USB reset again.
\item The host assigns a unique address to the device and sends a \emph{Set Address} request to the device.
\item After the request is completed, the device assumes the new address. At this point the host is free to reset any other newly plugged-indevices on the bus.
\item The host sends a \emph{Get Device Descriptor} request to retrieve the complete device descriptor, gathering information such as manufacturer, type of device, and maximum control packet size.
\item The host sends a \emph{Get Configuration Descriptors} request to receive the device’s configuration data, such as power requirements and the types and number of interfaces supported.
\item The host may request any additional descriptors from the device.
\end{itemize}
Initially, the device is addressed, but it is in an unconfigured state.
After the host gathers enough information about the device, it loads a suitable device driver which configures the device by sending it a \emph{Set Configuration} request.
At this point the device has been configured, and it is ready to respond to device-specific requests.
\subsection{Descriptors}
All USB devices have a hierarchy of descriptors that describe various features of the device: the manufacturer ID, the version of the device, the version of USB it supports, what the device is, its power requirements, the number and type of endpoints, and so forth.
The commonly used descriptors are
\begin{itemize}
\item Device descriptors
\item Configuration descriptors
\item Interface descriptors
\item HID descriptors
\item Endpoint descriptors
\end{itemize}
The hierarchy can be seen below
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/DescriptorHierarchy.png}
\end{center}
\end{figure}
The HID descriptor comes in same level as Interface descriptor when interface belogs to HID class.
The first byte of all descriptor is the \textit{bLength} - length of descriptor.
The second byte of all descriptor is the \textit{bDescriptorType} - type of descriptor.
\subsubsection{Device Descriptor}
\begin{itemize}
\item Top-level information read from device.
\item First item, the host retrieves
\item USB device has only one Device Descriptor
\item Represents entier device
\item information such as manufacturer, serial number, product number, the class of the device, and the number of configurations
\end{itemize}
\begin{table}[H]
\centering
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{|c|c|c|c|p{6cm}|}
\hline
\textbf{Offset (in Bytes)} & \textbf{Field} & \textbf{Size (in Bytes)} & \textbf{Meaning} & \textbf{Description} \\
\hline
0 & bLength & 1 & Descriptor Size in bytes & Provides the length of this device descriptor in bytes. \\
\hline
1 & bDescriptorType & 1 & Device Descritor type & Value is 0x01 since, the type is device descriptor. \\
\hline
2 & bcdUSB & 2 & Highest version of USB supported & Provides the highest usb version supported by the device in BCD format, 0xJJMN (JJ - major version, M - minor version, N - subminor version) \\
\hline
4 & bDeviceClass & 1 & Class Code & \multirow{3}{6cm}{assigned by the USB organization and used by system to find a class driver for device.} \\
\cline{1-4}
5 & bDeviceSubClass & 1 & Subclass code & \\
\cline{1-4}
6 & bDeviceProtocol & 1 & Protocol code & \\
\hline
7 & bMaxPacketSize0 & 1 & Maximum Packet size & For Endpoint 0, maximum input and output packet size \\
\hline
8 & idVendor & 2 & Vendor ID & Vendor's ID assigned by USB organization \\
\hline
10 & idProduct & 2 & Product ID & Product's ID assigned by manufacturer \\
\hline
12 & bcdDevice & 2 & Device release number & Deivce release number - same format as bcdUSB \\
\hline
14 & iManufacturer & 1 & Manufacturer string descripto & \multirow{3}{6cm}{details about the manufacturer andthe product. These fields have no requirement and can be set to zero.} \\
\cline{1-4}
15 & iProduct & 1 & Index of product string descriptor & \\
\cline{1-4}
16 & iSerialNumber & 1 & Index of serial number descriptor & \\
\hline
17 & bNumConfigurations & 1 & Number of possible configurations & Number of configuation the device supports \\
\hline
\end{tabular}
\end{adjustbox}
\end{table}
\subsubsection{Configuration Descriptors}
\begin{itemize}
\item information about power requirements of the device, how many different interfaces it support
\item may have more than one configuration for a device
\item When the configuration descriptor is read by the host, it returns the entire configuration information, which includes all interface and endpoint descriptors.
\end{itemize}
\begin{table}[H]
\centering
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{|c|c|c|c|p{6cm}|}
\hline
\textbf{Offset (in Bytes)} & \textbf{Field} & \textbf{Size (in Bytes)} & \textbf{Meaning} & \textbf{Description} \\
\hline
0 & bLength & 1 & Descriptor Size in bytes & Provides the length of this configuation descriptor in bytes. \\
\hline
1 & bDescriptorType & 1 & Device Descritor type & Value is 0x02 since, the type is configuation descriptor. \\
\hline
2 & wTotalLength & 2 & Total bytes returned & total combined size of this set of descriptors (configuration descriptor + interface descriptor + HID descriptor + endpointdescriptor) \\
\hline
4 & bNumInterfaces & 1 & Number of interfaces & number of interfaces present for this configuration \\
\hline
5 & bConfigurationValue & 1 & Value used to select configuration & used by the host (in command SetConfiguration) to select the configuration. \\
\hline
6 & iConfiguration & 1 & Index describing configuration string & index to a string descriptor describing the configuration inreadable format. \\
\hline
7 & bmAttributes & 1 & Power supply attributes & D7==1 -- Bus-powered, D6==1 -- Self Powered, D5==1 -- Remote wakeup \\
\hline
8 & bMaxPower & 2 & Max power consumption in 2mA & maximum power the device will draw from the bus in 2mAunits. \\
\hline
\end{tabular}
\end{adjustbox}
\end{table}
\subsubsection{Interface Descriptors}
\begin{itemize}
\item Specify the class of interface and the Number of endpoint it uses
\item May be more then one interface.
\end{itemize}
\begin{table}[H]
\centering
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{|c|c|c|c|p{6cm}|}
\hline
\textbf{Offset (in Bytes)} & \textbf{Field} & \textbf{Size (in Bytes)} & \textbf{Meaning} & \textbf{Description} \\
\hline
0 & bLength & 1 & Descriptor Size in bytes & Provides the length of this interface descriptor in bytes. \\
\hline
1 & bDescriptorType & 1 & Device Descritor type & Value is 0x04 since, the type of this interface descriptor. \\
\hline
2 & bInterfaceNumber & 1 & Number of interface & index of this interface descriptor \\
\hline
3 & bAlternateSetting & 1 & Value to select alternate setting & pecify alternate interfaces that can be selected by the host using command Set Interface. \\
\hline
4 & bNumEndpoints & 1 & Number of endpoints & number of endpoints used by this interface. \\
\hline
5 & bInterfaceClass & 1 & Class code & \multirow{3}{6cm}{assigned by USB organization} \\
\cline{1-4}
6 & bInterfaceSubClass & 1 & subclass code & \\
\cline{1-4}
7 & bInterfaceProtocol & 1 & protocol code & \\
\hline
8 & iInterface & 1 & Index of string descriptor to interface & index to a string descriptor of the interface. \\
\hline
\end{tabular}
\end{adjustbox}
\end{table}
\subsubsection{HID Interface}
An HID descriptor always follows an interface descriptor when the interface belongs tothe HID class.
\begin{table}[H]
\centering
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{|c|c|c|c|p{6cm}|}
\hline
\textbf{Offset (in Bytes)} & \textbf{Field} & \textbf{Size (in Bytes)} & \textbf{Meaning} & \textbf{Description} \\
\hline
0 & bLength & 1 & Descriptor Size in bytes & Provides the length of this HID descriptor in bytes. \\
\hline
1 & bDescriptorType & 1 & Device Descritor type & Value is 0x21 since, the type of this HID descriptor. \\
\hline
2 & bcdHID & 2 & HID class & HID class specification \\
\hline
4 & bCountryCode & 1 & Special country dependent code & specifies any special local changes \\
\hline
5 & bNumDescriptors & 1 & Number of additional descriptors & specifes if there are any additional descriptors associated with thisclass. \\
\hline
6 & bDescriptorType & 1 & Type of additional descriptor & type of the additional descriptor specified inbNumDescriptors. \\
\hline
7 & wDescriptorLength & 2 & Length of additional descriptor & ength of the additional descriptor in bytes. \\
\hline
\end{tabular}
\end{adjustbox}
\end{table}
\subsubsection{Endpoint Descriptors}
\begin{table}[H]
\centering
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{|c|c|c|c|p{6cm}|}
\hline
\textbf{Offset (in Bytes)} & \textbf{Field} & \textbf{Size (in Bytes)} & \textbf{Meaning} & \textbf{Description} \\
\hline
0 & bLength & 1 & Descriptor Size in bytes & Provides the length of this Endpoint descriptor in bytes. \\
\hline
1 & bDescriptorType & 1 & Device Descritor type & Value is 0x21 since, the type of this Endpoint descriptor. \\
\hline
2 & bEndpointAddress & 1 & Endpoint address & address of the endpoint. \\
\hline
3 & bmAttributes & 1 & Type of endpoint & specifies what type of endpoint it is. \\
\hline
4 & wMaxPacketSize & 2 & Max packet size & maximum packet size \\
\hline
6 & bInterval & 1 & Polling interval & how often the endpoint should be polled (in ms) \\
\hline
\end{tabular}
\end{adjustbox}
\end{table}
\section{Source : \href{https://www.usbmadesimple.co.uk}{USB MADE SIMPLE}}
\subsection{Introduction}
\begin{itemize}
\item Support hot-pluggable, Specific Interupts or DMA
\item Has OTG (On-the-Go), extension of USB spec to allow devices to become limited role host.
\item Tiered Star topology - single host controller and upto 127 slave devices.
\item Maximum tier is 6.
\item Length of cable is 5 meters.
\end{itemize}
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/usb_physical_topology.png}
\end{center}
\end{figure}
\begin{itemize}
\item Three types of USB Host controller.
\item OHCI (Open Host Controller Interface)
\begin{itemize}
\item Compaq, Microsoft and National Semiconductors cooperated to produce this standard host controller specification for USB 1.0 and USB 1.1.
\item Hardware Oriented
\item Low speed and full Speed
\end{itemize}
\item UHCI (Universal Host Controller Interface)
\begin{itemize}
\item Intel's software-orientd of controller for USB 1.0 and 1.1
\item Requieres license from Intel
\item Low speed and full Speed
\end{itemize}
\item EHCI (Extended Host Controller Interface)
\begin{itemize}
\item USB 2.0 with high speed for a single host controller specs.
\item EHCI handless high speed and hands off low and full speed to either OHCI or UHCI.
\end{itemize}
\item Four Wiere - VBUS [Red] (5V) , GND [Black] (reference), D+ [Green], D- [White]
\item Carry differential data signal or single-ended signal states.
\item The D+ or D- resistor should, strictly speaking, be pulled up to a 3.3V supply derived from Vbus, or controlled by Vbus in such a way that the resistor never sources current to the data line when Vbus is switched off.
\end{itemize}
\subsection{SPeed Identification}
\begin{itemize}
\item At device end, a 1.5 kohm resistor pulls one of the lines to 3.3V supply derived from VBUS.
\item Low speed : D- has the pull up resistor
\item Full speed : D+ has the pull up resistor
\item High speed : D+ has the pull up resistor
\item host determine the speed by observering the lines.
\end{itemize}
\subsection{Transceivers}
\begin{itemize}
\item Transceivers differe mainly by resistors.
\item Upstream is the host end, where a 15k pull down resistor pulls the line low.
\item Maximum high-leve is 3.3V
\end{itemize}
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/TransceiversUp.jpg}
\caption{Upstream Host Transceiver}
\end{center}
\end{figure}
\begin{itemize}
\item Downstream is at device end, where eithere there is a pull up resistor of 1.5kOhm at D+ or D- to make then high.
\item Can be able to detect states (such as SE0 - both lines are low)
\end{itemize}
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/TransceiversDOwn.jpg}
\caption{Downstream device Transceiver (Full Speed)}
\end{center}
\end{figure}
\subsection{Line States}
Based the 2 data lines, different line states are obtained.
\subsubsection{Detached}
When no device is plugged in, the host will see line low due to 15kohm pulling the lines low.
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; 12L;\\
\busref{D-} & X{}; 12L;\\
\end{tikztimingtable}
\end{center}
\subsubsection{Attached}
Either of D+ or D- goes to high level to indicated the device is plugged in.
\begin{figure}[H]
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; L; 11H;\\
\busref{D-} & X{}; 12L;\\
\end{tikztimingtable}
\end{center}
\caption{Full SPeed}
\end{figure}
\begin{figure}[H]
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; 12L;\\
\busref{D-} & X{}; L; 11H;\\
\end{tikztimingtable}
\end{center}
\caption{Low SPeed}
\end{figure}
\subsubsection{Idle}
When the pulled up line is high and other line is low.
State of lines before and after packets are sent.
\begin{figure}[H]
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; 12H;\\
\busref{D-} & X{}; 12L;\\
\end{tikztimingtable}
\end{center}
\caption{Full SPeed}
\end{figure}
\begin{figure}[H]
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; 12L;\\
\busref{D-} & X{}; 12H;\\
\end{tikztimingtable}
\end{center}
\caption{Low SPeed}
\end{figure}
\subsubsection{J and K State}
J state is same as Idle state, but the lines are driven by eigher host or device.
Opposite to J state.
J and K are used for full and low speed links as they are Opposite polarity.
\begin{figure}[H]
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; 12H;\\
\busref{D-} & X{}; 12L;\\
\end{tikztimingtable}
\end{center}
\caption{J state}
\end{figure}
\begin{figure}[H]
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; 12L;\\
\busref{D-} & X{}; 12H;\\
\end{tikztimingtable}
\end{center}
\caption{K state}
\end{figure}
\subsubsection{SE0 state (Single Ended Zero)}
Both lines are pulled low.
Driven low.
\begin{figure}[H]
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; 12L;\\
\busref{D-} & X{}; 12L;\\
\end{tikztimingtable}
\end{center}
\end{figure}
\subsubsection{SE1 state (Single Ended One)}
Both lines are high.
Illegal, not happen.
\begin{figure}[H]
\begin{center}
\begin{tikztimingtable}[%
timing/dslope=0.1,
timing/.style={x=5ex,y=2ex},
x=5ex,
timing/rowdist=3ex,
timing/c/rising arrows,
timing/name/.style={font=\sffamily\scriptsize}
]
\busref{D+} & X{}; 12H;\\
\busref{D-} & X{}; 12H;\\
\end{tikztimingtable}
\end{center}
\end{figure}
\subsubsection{Reset}
When host wants to communicate it will start by applying Reset condition, which will set device to default config.
\begin{itemize}
\item Pulling down lines to low for 10 ms (SE0 state)
\item Device may recognie reset after 2.5 $\mu$s
\end{itemize}
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/resetSingal.png}
\end{center}
\end{figure}
\subsubsection{EOP - ENd of Pack}
EOP is SE0 for 2 bit time, and J state for 1 bit time.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/EOPSignal.jpg}
\end{center}
\end{figure}
\subsubsection{Suspend}
Not sending any signal for 3 ms. (IDLE state)
Normally a SOF packet (at full speed) or a Keep Alive signal (at low speed) is sent by the host every 1 ms, and this is what keeps the device awake.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/SuspendSIgnal.jpg}
\end{center}
\end{figure}
\subsubsection{Resume}
Wake up device which is in suspend.
Reverse the polarity of the lines for atlease 20ms.
Followed by low speed EOP signal.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/resumesignal.jpg}
\end{center}
\end{figure}
\subsubsection{Keep Alive signal}
Low speed EOP.
Atleaset every 1ms on low speed link to keep the device from suspending.
\begin{figure}[H]
\begin{center}
\includegraphics[scale=0.5]{DocuResources/KEEPAlige.jpg}
\end{center}
\end{figure}
\subsection{Packets and Transmission}
\begin{itemize}
\item Packets - smallest element of data transmission.
\item Before and after Packet, the bus is idle.
\item \textcolor{red}{\textbf{Serial Interface Engine (SIE)} takes care of SYNCs, Bit Stuffing, and EOP conditoions}.
\item USB uses LSB first. (also in a 2-byte data, LSB byte is transmitted first)
\item Packets starts with a SYNC pattern to synchronize with the receiver bit clock.
\item Followed by data packets and ends with EOP signal.
\item NRZI encoding
\item to ensure sufficent frequent transitions - Bit Stuffing (zero after 6 sucessive 1's) is done.
\end{itemize}
\subsection{Definitions}
\subsubsection{EndPoints}
\begin{itemize}
\item Each USB device has a number of endpoints.
\item Each endpoint is a source or a sink of data.
\item A device can have upto 16 OUT and 16 IN endpoints
\item OUT means from host to device.
\item IN means device to host.
\item Endpoint 0 is combination of endpoint0 OUT and endpoint0 IN and is used for controlling device.
\end{itemize}
\subsubsection{Pipes}
\begin{itemize}
\item A logical connection between host and a particular endpoint.
\end{itemize}
\subsubsection{Transactions}
\begin{itemize}
\item Simple transfer of data called Transactions built up using packets.
\end{itemize}
\subsection{Packet FOrmat}
\begin{itemize}
\item The first byte of every packet is the Packet Identifier (PID).
\item To make it easy for the SIE, there is no CRC check for PID.
\item USB uses two CRCs - 5-bit CRC (CRC5) and 16-bit CRC (CRC16)
\item First two bits of PID determines the groups in which they fall into.
\item Even Here LSB's are transmitted first, hence, PID0 is transmitted first, followed by PID1, PID2, PID3, $\overline{PID0}$, $\overline{PID1}$, $\overline{PID2}$, $\overline{PID3}$
\end{itemize}
\begin{bytefield}[bitformatting={\large\bfseries},
endianness=little,bitwidth=0.1\linewidth]{8}
\bitlabel{1}{LSB}
\bitlabel{13}{MSB}\\
\bitbox{1}{\small PID0}
\bitbox{1}{\small PID1}
\bitbox{1}{\small PID2}
\bitbox{1}{\small PID3}
\bitbox{1}{\small $\overline{PID0}$}
\bitbox{1}{\small $\overline{PID1}$}
\bitbox{1}{\small $\overline{PID2}$}
\bitbox{1}{\small $\overline{PID3}$}\\
\end{bytefield}
\subsubsection{Token Packet}
\begin{bytefield}[bitformatting={\large\bfseries},
endianness=little,bitwidth=0.1\linewidth]{6}
\bitlabel{1}{SYNC}
\bitlabel{1}{PID}
\bitlabel{1}{ADDR}
\bitlabel{1}{ENDP}
\bitlabel{1}{CRC5}
\bitlabel{1}{EOP}\\
\bitbox{1}{}
\bitbox{1}{\small 8 bits}
\bitbox{1}{\small 7 bits}
\bitbox{1}{\small 4 bits}
\bitbox{1}{\small 5 bits}
\bitbox{1}{}\\
\end{bytefield}
\begin{itemize}
\item User for SETUP, OUT, IN packets.
\item Transmitted first in a transaction - identify the endpoint and purpose of transacting.
\item Address is 7-bit can so can address upto 127 adddressed using USB (Address 0 is reserved).
\item Endpoint can be upto 16-possible values in each directions(IN or OUT).
\item The direction (IN or OUT) is said by PID.
\begin{itemize}
\item OUT and SETUP PID will refer OUT endpoint.
\item IN PID will refere IN endpoint.
\end{itemize}
\end{itemize}
\subsubsection{Data Packets}
\begin{bytefield}[bitformatting={\large\bfseries},
endianness=little,bitwidth=0.12\linewidth]{5}
\bitlabel{1}{SYNC}
\bitlabel{1}{PID}
\bitlabel{1}{DATA}
\bitlabel{1}{CRC16}
\bitlabel{1}{EOP}\\
\bitbox{1}{}
\bitbox{1}{\small 8 bits}
\bitbox{1}{\small (0-1024) $\times$ 8 bits}
\bitbox{1}{\small 16 bits}
\bitbox{1}{}\\
\end{bytefield}
\begin{itemize}
\item Used for DATA0, DATA1, DATA2, MDATA packet.
\item For data stage inside a transaction.
\item DIfferent DATA for error-checkng system.
\item DATA0 and DATA1 is used in Low and FUll speed links.
\begin{itemize}
\item DATA0 and DATA1 packets comes alternatively when sending data.
\item This helps endpoint to know which one to expect.
\end{itemize}
\item DATA2 and MDATA are used for high speed links.
\end{itemize}
\subsubsection{Handshake Packet}
\begin{bytefield}[bitformatting={\large\bfseries},
endianness=little,bitwidth=0.12\linewidth]{3}
\bitlabel{1}{SYNC}
\bitlabel{1}{PID}
\bitlabel{1}{EOP}\\
\bitbox{1}{}
\bitbox{1}{\small 8 bits}
\bitbox{1}{}\\
\end{bytefield}
\begin{itemize}
\item USed for ACK, NAK, STALL, NYET.
\item status stage of a transcation.
\item ACK - reciever acknowledges error free packet.
\item NACK - receiver cannot accept data or tranmitting device cannot send data.
\item STALL - endpoint is halted
\item NYET - No response yet from receiver (high speed only)
\end{itemize}
\subsubsection{SOF Packet}
\begin{bytefield}[bitformatting={\large\bfseries},
endianness=little,bitwidth=0.1\linewidth]{5}
\bitlabel{1}{SYNC}
\bitlabel{1}{PID}
\bitlabel{1}{Frame No.}
\bitlabel{1}{CRC5}
\bitlabel{1}{EOP}\\
\bitbox{1}{}
\bitbox{1}{\small 8 bits}
\bitbox{1}{\small 11 bits}
\bitbox{1}{\small 5 bits}
\bitbox{1}{}\\
\end{bytefield}
\begin{itemize}
\item Only on full speed links.
\end{itemize}
\subsection{Transactions}
\begin{itemize}