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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
|
//*****************************************************************************
//
// usbdhid.c - USB HID device class driver.
//
// Copyright (c) 2008-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the Tiva USB Library.
//
//*****************************************************************************
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/usb.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "usblib/usblib.h"
#include "usblib/usblibpriv.h"
#include "usblib/usbhid.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdhid.h"
#include "usblib/usblibpriv.h"
//*****************************************************************************
//
//! \addtogroup hid_device_class_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// The subset of endpoint status flags that we consider to be reception
// errors. These are passed to the client via USB_EVENT_ERROR if seen.
//
//*****************************************************************************
#define USB_RX_ERROR_FLAGS (USBERR_DEV_RX_DATA_ERROR | \
USBERR_DEV_RX_OVERRUN | \
USBERR_DEV_RX_FIFO_FULL)
//*****************************************************************************
//
// Marker used to indicate that a given HID descriptor cannot be found in the
// client-supplied list.
//
//*****************************************************************************
#define HID_NOT_FOUND 0xFFFFFFFF
//*****************************************************************************
//
// Flags that may appear in ui16DeferredOpFlags to indicate some operation that
// has been requested but could not be processed at the time it was received.
// Each deferred operation is defined as the bit number that should be set in
// tHIDInstance->ui16DeferredOpFlags to indicate that the operation is pending.
//
//*****************************************************************************
#define HID_DO_PACKET_RX 5
#define HID_DO_SEND_IDLE_REPORT 6
//*****************************************************************************
//
// Endpoints to use for each of the required endpoints in the driver.
//
//*****************************************************************************
#define INT_IN_ENDPOINT USB_EP_3
#define INT_OUT_ENDPOINT USB_EP_3
//*****************************************************************************
//
// Device Descriptor. This is stored in RAM to allow several fields to be
// changed at runtime based on the client's requirements.
//
//*****************************************************************************
uint8_t g_pui8HIDDeviceDescriptor[] =
{
18, // Size of this structure.
USB_DTYPE_DEVICE, // Type of this structure.
USBShort(0x110), // USB version 1.1 (if we say 2.0, hosts assume
// high-speed - see USB 2.0 spec 9.2.6.6)
USB_CLASS_DEVICE, // USB Device Class
0, // USB Device Sub-class
USB_HID_PROTOCOL_NONE, // USB Device protocol
USBDHID_MAX_PACKET, // Maximum packet size for default pipe.
USBShort(0), // Vendor ID (VID).
USBShort(0), // Product ID (PID).
USBShort(0x100), // Device Version BCD.
1, // Manufacturer string identifier.
2, // Product string identifier.
3, // Product serial number.
1 // Number of configurations.
};
//*****************************************************************************
//
// Forward references for device handler callbacks
//
//*****************************************************************************
static void HandleGetDescriptor(void *pvHIDInstance, tUSBRequest *psUSBRequest);
static void HandleRequest(void *pvHIDInstance, tUSBRequest *psUSBRequest);
static void HandleConfigChange(void *pvHIDInstance, uint32_t ui32Info);
static void HandleEP0DataReceived(void *pvHIDInstance, uint32_t ui32Info);
static void HandleEP0DataSent(void *pvHIDInstance, uint32_t ui32Info);
static void HandleReset(void *pvHIDInstance);
static void HandleSuspend(void *pvHIDInstance);
static void HandleResume(void *pvHIDInstance);
static void HandleDisconnect(void *pvHIDInstance);
static void HandleEndpoints(void *pvHIDInstance, uint32_t ui32Status);
static void HandleDevice(void *pvHIDInstance, uint32_t ui32Request,
void *pvRequestData);
//*****************************************************************************
//
// The device information structure for the USB HID devices.
//
//*****************************************************************************
const tCustomHandlers g_sHIDHandlers =
{
//
// GetDescriptor
//
HandleGetDescriptor,
//
// RequestHandler
//
HandleRequest,
//
// InterfaceChange
//
0,
//
// ConfigChange
//
HandleConfigChange,
//
// DataReceived
//
HandleEP0DataReceived,
//
// DataSentCallback
//
HandleEP0DataSent,
//
// ResetHandler
//
HandleReset,
//
// SuspendHandler
//
HandleSuspend,
//
// ResumeHandler
//
HandleResume,
//
// DisconnectHandler
//
HandleDisconnect,
//
// EndpointHandler
//
HandleEndpoints,
//
// Device handler.
//
HandleDevice
};
//*****************************************************************************
//
// Set or clear deferred operation flags in an "atomic" manner.
//
// \param pui16DeferredOp points to the flags variable which is to be modified.
// \param ui16Bit indicates which bit number is to be set or cleared.
// \param bSet indicates the state that the flag must be set to. If \b true,
// the flag is set, if \b false, the flag is cleared.
//
// This function safely sets or clears a bit in a flag variable. The operation
// makes use of bitbanding to ensure that the operation is atomic (no read-
// modify-write is required).
//
// \return None.
//
//*****************************************************************************
static void
SetDeferredOpFlag(volatile uint16_t *pui16DeferredOp, uint16_t ui16Bit,
bool bSet)
{
//
// Set the flag bit to 1 or 0 using a bitband access.
//
HWREGBITH(pui16DeferredOp, ui16Bit) = bSet ? 1 : 0;
}
//*****************************************************************************
//
// This function is called to clear the counter used to keep track of the time
// elapsed since a given report was last sent.
//
// \param psHIDDevice points to the HID device structure whose report timer is
// to be cleared.
// \param ui8ReportID is the first byte of the report to be sent. If this
// device offers more than one input report, this value is used to find the
// relevant report timer structure in the psHIDDevice structure.
//
// \return None.
//
//*****************************************************************************
static void
ClearReportTimer(const tUSBDHIDDevice *psHIDDevice, uint8_t ui8ReportID)
{
uint32_t ui32Loop;
if(psHIDDevice->ui8NumInputReports > 1)
{
//
// We have more than 1 input report so the report must begin with a
// byte containing the report ID. Scan the table we were provided
// when the device was initialized to find the entry for this report.
//
for(ui32Loop = 0; ui32Loop < psHIDDevice->ui8NumInputReports;
ui32Loop++)
{
if(psHIDDevice->psReportIdle[ui32Loop].ui8ReportID == ui8ReportID)
{
break;
}
}
}
else
{
ui32Loop = 0;
}
//
// If we drop out of the loop with an index less than ui8NumInputReports,
// we found the relevant report so clear its timer.
//
if(ui32Loop < psHIDDevice->ui8NumInputReports)
{
psHIDDevice->psReportIdle[ui32Loop].ui32TimeSinceReportmS = 0;
}
}
//*****************************************************************************
//
// This function is called to clear the idle period timers for each input
// report supported by the device.
//
// \param psHIDDevice points to the HID device structure whose timers are to be
// cleared.
// \param ui32TimemS is the elapsed time in milliseconds since the last call
// to this function.
//
// \return None.
//
//*****************************************************************************
static void
ClearIdleTimers(const tUSBDHIDDevice *psHIDDevice)
{
uint32_t ui32Loop;
//
// Clear the "time till next report" counters for each input report.
//
for(ui32Loop = 0; ui32Loop < psHIDDevice->ui8NumInputReports; ui32Loop++)
{
psHIDDevice->psReportIdle[ui32Loop].ui16TimeTillNextmS =
psHIDDevice->psReportIdle[ui32Loop].ui8Duration4mS * 4;
}
}
//*****************************************************************************
//
// This function is called periodically to allow us to process the report idle
// timers.
//
// \param psHIDDevice points to the HID device structure whose timers are to be
// updated.
// \param ui32ElapsedmS indicates the number of milliseconds that have elapsed
// since the last call to this function.
//
// \return None.
//
//*****************************************************************************
static void
ProcessIdleTimers(const tUSBDHIDDevice *psHIDDevice, uint32_t ui32ElapsedmS)
{
uint32_t ui32Loop, ui32SizeReport;
void *pvReport;
tHIDInstance *psInst;
bool bDeferred;
//
// Get our instance data pointer
//
psInst = &((tUSBDHIDDevice *)psHIDDevice)->sPrivateData;
//
// We have not had to defer any report transmissions yet.
//
bDeferred = false;
//
// Look at each of the input report idle timers in turn.
//
for(ui32Loop = 0; ui32Loop < psHIDDevice->ui8NumInputReports; ui32Loop++)
{
//
// Update the time since the last report was sent.
//
psHIDDevice->psReportIdle[ui32Loop].ui32TimeSinceReportmS +=
ui32ElapsedmS;
//
// Is this timer running?
//
if(psHIDDevice->psReportIdle[ui32Loop].ui8Duration4mS)
{
//
// Yes - is it about to expire?
//
if(psHIDDevice->psReportIdle[ui32Loop].ui16TimeTillNextmS <=
ui32ElapsedmS)
{
//
// The timer is about to expire. Can we send a report right
// now?
//
if((psInst->iHIDTxState == eHIDStateIdle) &&
(psInst->bSendInProgress == false))
{
//
// We can send a report so send a message to the
// application to retrieve its latest report for
// transmission to the host.
//
ui32SizeReport = psHIDDevice->pfnRxCallback(
psHIDDevice->pvRxCBData,
USBD_HID_EVENT_IDLE_TIMEOUT,
psHIDDevice->psReportIdle[ui32Loop].ui8ReportID,
&pvReport);
//
// Schedule the report for transmission.
//
USBDHIDReportWrite((void *)psHIDDevice, pvReport,
ui32SizeReport, true);
//
// Reload the timer for the next period.
//
psHIDDevice->psReportIdle[ui32Loop].ui16TimeTillNextmS =
psHIDDevice->psReportIdle[ui32Loop].ui8Duration4mS * 4;
}
else
{
//
// We can't send the report straight away so flag it for
// transmission as soon as the previous transmission ends.
//
psHIDDevice->psReportIdle[ui32Loop].ui16TimeTillNextmS = 0;
bDeferred = true;
}
}
else
{
//
// The timer is not about to expire. Update the time till the
// next report transmission.
//
psHIDDevice->psReportIdle[ui32Loop].ui16TimeTillNextmS -=
ui32ElapsedmS;
}
}
}
//
// If we had to defer transmission of any report, remember this so that we
// will process it as soon as possible.
//
SetDeferredOpFlag(&psInst->ui16DeferredOpFlags, HID_DO_SEND_IDLE_REPORT,
bDeferred);
}
static void
SetIdleTimeout(const tUSBDHIDDevice *psHIDDevice, uint8_t ui8ReportID,
uint8_t ui8Timeout4mS)
{
uint32_t ui32Loop;
bool bReportNeeded;
tHIDReportIdle *psIdle;
//
// Remember that we have not found any report that needs to be sent
// immediately.
//
bReportNeeded = false;
//
// Search through all the input reports looking for ones that fit the
// requirements.
//
for(ui32Loop = 0; ui32Loop < psHIDDevice->ui8NumInputReports; ui32Loop++)
{
psIdle = &psHIDDevice->psReportIdle[ui32Loop];
//
// If the report ID passed matches the report ID in the idle timer
// control structure or we were passed a report ID of zero, which
// indicates that all timers are to be set...
//
if(!ui8ReportID || (ui8ReportID == psIdle->ui8ReportID))
{
//
// Save the new duration for the idle timer.
//
psIdle->ui8Duration4mS = ui8Timeout4mS;
//
// Are we enabling the idle timer? If so, fix up the time until it
// needs to fire.
//
if(ui8Timeout4mS)
{
//
// Determine what the timeout is for this report given the time
// since the last report of this type was sent.
//
if(psIdle->ui32TimeSinceReportmS >=
((uint32_t)ui8Timeout4mS * 4))
{
psIdle->ui16TimeTillNextmS = 0;
bReportNeeded = true;
}
else
{
psIdle->ui16TimeTillNextmS =
(((uint16_t)ui8Timeout4mS * 4) -
psIdle->ui32TimeSinceReportmS);
}
}
}
}
//
// If we get to here and bReportNeeded is true, this means we need to
// send back at least one of the input reports as soon as possible. Try
// to do this immediately.
//
if(bReportNeeded)
{
ProcessIdleTimers(psHIDDevice, 0);
}
}
//*****************************************************************************
//
// Find the idle timeout for a given HID input report.
//
// \param psHIDDevice points to the HID device whose report idle timeout is to
// be found.
// \param ui8ReportID identifies the report whose timeout is requested. If 0,
// the timeout for the first report is returns, regardless of its ID (or
// whether it has one).
//
// This function returns the current idle timeout for a given HID input report.
// The value returned is expressed in terms of 4mS intervals. Convert to
// milliseconds by multiplying by 4. If the return value is 0, this indicates
// that an infinite timeout is currently set and the device will not send the
// report unless a state change occurs.
//
// \return Returns the current idle timeout for the given report.
//
//*****************************************************************************
static uint32_t
GetIdleTimeout(const tUSBDHIDDevice *psHIDDevice, uint8_t ui8ReportID)
{
uint32_t ui32Loop;
tHIDReportIdle *psIdle;
//
// Search through all the input reports looking for ones that fit the
// requirements.
//
for(ui32Loop = 0; ui32Loop < psHIDDevice->ui8NumInputReports; ui32Loop++)
{
psIdle = &psHIDDevice->psReportIdle[ui32Loop];
//
// If the report ID passed matches the report ID in the idle timer
// control structure or we were passed a report ID of zero, which
// indicates that all timers are to be set...
//
if(!ui8ReportID || (ui8ReportID == psIdle->ui8ReportID))
{
//
// We found a report matching the required ID or we were not passed
// an ID and we are looking at the first report information.
//
return((uint32_t)psIdle->ui8Duration4mS);
}
}
//
// If we drop out, the report could not be found so we need to indicate
// an error.
//
return(HID_NOT_FOUND);
}
//*****************************************************************************
//
// Find the n-th HID class descriptor of a given type in the client-provided
// descriptor table.
//
// \param psHIDDevice points to the HID device which is to be searched for the
// required class descriptor.
// \param ui8Type is the type of class descriptor being requested. This will
// be either USB_HID_DTYPE_REPORT or USB_HID_DTYPE_PHYSICAL.
// \param ui32Index is the zero-based index of the descriptor that is being
// requested.
//
// This function parses the supplied HID descriptor to find the index into the
// sClassDescriptor array that corresponds to the requested descriptor. If
// a descriptor with the requested index does not exist, HID_NOT_FOUND will be
// returned unless the request is for a physical descriptor and at least one
// such descriptor exists. In this case, the index returned will be for the
// last physical descriptor (as required by the HID spec 7.1.1).
//
// \return Returns the index of the descriptor within the sClassDescriptor
// of the tHIDDevice structure if found or HID_NOT_FOUND otherwise.
//
//*****************************************************************************
static uint32_t
FindHIDDescriptor(const tUSBDHIDDevice *psHIDDevice, uint8_t ui8Type,
uint32_t ui32Index, uint32_t *pui32Len)
{
bool bFoundType;
uint32_t ui32Loop, ui32Count, ui32LastFound;
const tHIDClassDescriptorInfo *psDesc;
//
// Remember that we have not found any descriptor with a matching type yet.
//
bFoundType = false;
ui32Count = 0;
ui32LastFound = 0;
//
// Walk through all the class descriptors looking for the one which
// matches the requested index and type.
//
for(ui32Loop = 0; ui32Loop < psHIDDevice->psHIDDescriptor->bNumDescriptors;
ui32Loop++)
{
psDesc = &(psHIDDevice->psHIDDescriptor->sClassDescriptor[ui32Loop]);
if(psDesc->bDescriptorType == ui8Type)
{
//
// We found a descriptor of the correct type. Is this the
// correct index?
//
bFoundType = true;
//
// Is this the descriptor we are looking for?
//
if(ui32Count == ui32Index)
{
//
// Yes - we found it so return the index and size to the
// caller.
//
*pui32Len = (uint32_t)psDesc->wDescriptorLength;
return(ui32Loop);
}
else
{
//
// Update our count and keep looking. Remember where we were
// when we found this descriptor in case we need to return the
// last physical descriptor.
//
ui32Count++;
ui32LastFound = ui32Loop;
}
}
}
//
// If we drop out, we did not find the requested descriptor. Now handle
// the special case of a physical descriptor - if we found any physical
// descriptors, return the last one.
//
if((ui8Type == USB_HID_DTYPE_PHYSICAL) && bFoundType)
{
//
// Get the length of the last descriptor we found.
//
psDesc =
&(psHIDDevice->psHIDDescriptor->sClassDescriptor[ui32LastFound]);
*pui32Len = (uint32_t)psDesc->wDescriptorLength;
//
// Return the index to the caller.
//
return(ui32LastFound);
}
else
{
//
// We could not find the descriptor so return an appropriate error.
//
return(HID_NOT_FOUND);
}
}
//*****************************************************************************
//
// Schedule transmission of the next packet forming part of an input report.
//
// \param psHIDInst points to the HID device instance whose input report is to
// be sent.
//
// This function is called to transmit the next packet of an input report
// passed to the driver via a call to USBDHIDReportWrite. If any data remains
// to be sent, a USB packet is written to the FIFO and scheduled for
// transmission to the host. The function ensures that reports are sent as
// a sequence of full packets followed by either a single int16_t packet or a
// packet with no data to indicate the end of the transaction.
//
//*****************************************************************************
static int32_t
ScheduleReportTransmission(tHIDInstance *psHIDInst)
{
uint32_t ui32NumBytes;
uint8_t *pui8Data;
int32_t i32Retcode;
//
// Set the number of bytes to send this iteration.
//
ui32NumBytes = (uint32_t)(psHIDInst->ui16InReportSize -
psHIDInst->ui16InReportIndex);
//
// Limit individual transfers to the maximum packet size for the endpoint.
//
if(ui32NumBytes > USBDHID_MAX_PACKET)
{
ui32NumBytes = USBDHID_MAX_PACKET;
}
//
// Where are we sending this data from?
//
pui8Data = psHIDInst->pui8InReportData + psHIDInst->ui16InReportIndex;
//
// Put the data in the correct FIFO.
//
i32Retcode = MAP_USBEndpointDataPut(psHIDInst->ui32USBBase,
psHIDInst->ui8INEndpoint,
pui8Data, ui32NumBytes);
if(i32Retcode != -1)
{
//
// Update the count and index ready for the next time round.
//
psHIDInst->ui16InReportIndex += ui32NumBytes;
//
// Send out the current data.
//
i32Retcode = MAP_USBEndpointDataSend(psHIDInst->ui32USBBase,
psHIDInst->ui8INEndpoint,
USB_TRANS_IN);
}
//
// Tell the caller how we got on.
//
return(i32Retcode);
}
//*****************************************************************************
//
// Receives notifications related to data received from the host.
//
// \param psHIDDevice is the device instance whose endpoint is to be processed.
// \param ui32Status is the USB interrupt status that caused this function to
// be called.
//
// This function is called from HandleEndpoints for all interrupts signaling
// the arrival of data on the interrupt OUT endpoint (in other words, whenever
// the host has sent us a packet of data). We inform the client that a packet
// is available and, on return, check to see if the packet has been read. If
// not, we schedule another notification to the client for a later time.
//
// \return Returns \b true on success or \b false on failure.
//
//*****************************************************************************
static bool
ProcessDataFromHost(tUSBDHIDDevice *psHIDDevice, uint32_t ui32Status)
{
uint32_t ui32EPStatus, ui32Size;
tHIDInstance *psInst;
//
// Get a pointer to our instance data.
//
psInst = &psHIDDevice->sPrivateData;
//
// Get the endpoint status to see why we were called.
//
ui32EPStatus = MAP_USBEndpointStatus(USB0_BASE, psInst->ui8OUTEndpoint);
//
// Clear the status bits.
//
MAP_USBDevEndpointStatusClear(USB0_BASE, psInst->ui8OUTEndpoint,
ui32EPStatus);
//
// Has a packet been received?
//
if(ui32EPStatus & USB_DEV_RX_PKT_RDY)
{
//
// Set the flag we use to indicate that a packet read is pending. This
// will be cleared if the packet is read. If the client does not read
// the packet in the context of the USB_EVENT_RX_AVAILABLE callback,
// the event will be signaled later during tick processing.
//
SetDeferredOpFlag(&psInst->ui16DeferredOpFlags, HID_DO_PACKET_RX,
true);
//
// How big is the packet we have just been sent?
//
ui32Size = MAP_USBEndpointDataAvail(psInst->ui32USBBase,
psInst->ui8OUTEndpoint);
//
// The receive channel is not blocked so let the caller know
// that a packet is waiting. The parameters are set to indicate
// that the packet has not been read from the hardware FIFO yet.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USB_EVENT_RX_AVAILABLE, ui32Size,
(void *)0);
}
else
{
//
// No packet was received. Some error must have been reported. Check
// and pass this on to the client if necessary.
//
if(ui32EPStatus & USB_RX_ERROR_FLAGS)
{
//
// This is an error we report to the client so...
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USB_EVENT_ERROR,
(ui32EPStatus & USB_RX_ERROR_FLAGS),
(void *)0);
}
return(false);
}
return(true);
}
//*****************************************************************************
//
// Receives notifications related to data sent to the host.
//
// \param psHIDDevice is the device instance whose endpoint is to be processed.
// \param ui32Status is the USB interrupt status that caused this function to
// be called.
//
// This function is called from HandleEndpoints for all interrupts originating
// from the interrupt IN endpoint (in other words, whenever data has been
// transmitted to the USB host). We examine the cause of the interrupt and,
// if due to completion of a transmission, notify the client.
//
// \return Returns \b true on success or \b false on failure.
//
//*****************************************************************************
static bool
ProcessDataToHost(tUSBDHIDDevice *psHIDDevice, uint32_t ui32Status)
{
tHIDInstance *psInst;
uint32_t ui32EPStatus;
//
// Get a pointer to our instance data.
//
psInst = &psHIDDevice->sPrivateData;
//
// Get the endpoint status to see why we were called.
//
ui32EPStatus = MAP_USBEndpointStatus(psInst->ui32USBBase,
psInst->ui8INEndpoint);
//
// Clear the status bits.
//
MAP_USBDevEndpointStatusClear(psInst->ui32USBBase, psInst->ui8INEndpoint,
ui32EPStatus);
//
// Our last packet was transmitted successfully. Is there any more data to
// send or have we finished sending the whole report? We know we finished
// if the ui16InReportIndex has reached the ui16InReportSize value.
//
if(psInst->ui16InReportSize == psInst->ui16InReportIndex)
{
//
// We finished sending the last report so are idle once again.
//
psInst->iHIDTxState = eHIDStateIdle;
//
// Notify the client that the report transmission completed.
//
psHIDDevice->pfnTxCallback(psHIDDevice->pvTxCBData,
USB_EVENT_TX_COMPLETE,
psInst->ui16InReportSize, (void *)0);
//
// Do we have any reports to send as a result of idle timer timeouts?
//
if(psInst->ui16DeferredOpFlags & (1 << HID_DO_SEND_IDLE_REPORT))
{
//
// Yes - send reports for any timers that expired recently.
//
ProcessIdleTimers(psHIDDevice, 0);
}
}
else
{
//
// There must be more data or a zero length packet waiting to be sent
// so go ahead and do this.
//
ScheduleReportTransmission(psInst);
}
return(true);
}
//*****************************************************************************
//
// Called by the USB stack for any activity involving one of our endpoints
// other than EP0. This function is a fan out that merely directs the call to
// the correct handler depending upon the endpoint and transaction direction
// signaled in ui32Status.
//
//*****************************************************************************
static void
HandleEndpoints(void *pvHIDInstance, uint32_t ui32Status)
{
tUSBDHIDDevice *psHIDInst;
tHIDInstance *psInst;
ASSERT(pvHIDInstance != 0);
//
// Determine if the serial device is in single or composite mode because
// the meaning of ui32Index is different in both cases.
//
psHIDInst = (tUSBDHIDDevice *)pvHIDInstance;
psInst = &psHIDInst->sPrivateData;
//
// Handler for the interrupt OUT data endpoint.
//
if(ui32Status & (0x10000 << USBEPToIndex(psInst->ui8OUTEndpoint)))
{
//
// Data is being sent to us from the host.
//
ProcessDataFromHost(pvHIDInstance, ui32Status);
}
//
// Handler for the interrupt IN data endpoint.
//
if(ui32Status & (1 << USBEPToIndex(psInst->ui8INEndpoint)))
{
ProcessDataToHost(pvHIDInstance, ui32Status);
}
}
//*****************************************************************************
//
// Called by the USB stack whenever a configuration change occurs.
//
//*****************************************************************************
static void
HandleConfigChange(void *pvHIDInstance, uint32_t ui32Info)
{
tHIDInstance *psInst;
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Create the instance pointer.
//
psHIDDevice = pvHIDInstance;
//
// Get a pointer to our instance data.
//
psInst = &psHIDDevice->sPrivateData;
//
// Set all our endpoints to idle state.
//
psInst->iHIDRxState = eHIDStateIdle;
psInst->iHIDTxState = eHIDStateIdle;
//
// If we are not currently connected let the client know we are open for
// business.
//
if(!psInst->bConnected)
{
//
// Pass the connected event to the client.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USB_EVENT_CONNECTED, 0, (void *)0);
}
//
// Clear the idle timers for each input report.
//
ClearIdleTimers(psHIDDevice);
//
// Remember that we are connected.
//
psInst->bConnected = true;
}
//*****************************************************************************
//
// Device instance specific handler.
//
//*****************************************************************************
static void
HandleDevice(void *pvHIDInstance, uint32_t ui32Request, void *pvRequestData)
{
tHIDInstance *psInst;
uint8_t *pui8Data;
tUSBDHIDDevice *psHIDDevice;
psHIDDevice = (tUSBDHIDDevice *)pvHIDInstance;
//
// Create the serial instance data.
//
psInst = &((tUSBDHIDDevice *)pvHIDInstance)->sPrivateData;
//
// Create the int8_t array used by the events supported by the USB CDC
// serial class.
//
pui8Data = (uint8_t *)pvRequestData;
switch(ui32Request)
{
//
// This was an interface change event.
//
case USB_EVENT_COMP_IFACE_CHANGE:
{
psInst->ui8Interface = pui8Data[1];
break;
}
//
// This was an endpoint change event.
//
case USB_EVENT_COMP_EP_CHANGE:
{
//
// Determine if this is an IN or OUT endpoint that has changed.
//
if(pui8Data[0] & USB_EP_DESC_IN)
{
psInst->ui8INEndpoint = IndexToUSBEP((pui8Data[1] & 0x7f));
}
else
{
//
// Extract the new endpoint number.
//
psInst->ui8OUTEndpoint = IndexToUSBEP(pui8Data[1] & 0x7f);
}
break;
}
case USB_EVENT_LPM_RESUME:
{
if(psHIDDevice->pfnRxCallback)
{
//
// Pass the LPM resume event to the client.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USB_EVENT_LPM_RESUME, 0, (void *)0);
}
break;
}
case USB_EVENT_LPM_SLEEP:
{
if(psHIDDevice->pfnRxCallback)
{
//
// Pass the LPM sleep event to the client.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USB_EVENT_LPM_SLEEP, 0, (void *)0);
}
break;
}
case USB_EVENT_LPM_ERROR:
{
if(psHIDDevice->pfnRxCallback)
{
//
// Pass the LPM error event to the client.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USB_EVENT_LPM_ERROR, 0, (void *)0);
}
break;
}
default:
{
break;
}
}
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the device is
// disconnected from the host.
//
//*****************************************************************************
static void
HandleDisconnect(void *pvHIDInstance)
{
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Create the instance pointer.
//
psHIDDevice = (tUSBDHIDDevice *)pvHIDInstance;
//
// If we are not currently connected so let the client know we are open
// for business.
//
if(psHIDDevice->sPrivateData.bConnected)
{
//
// Pass the disconnected event to the client.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USB_EVENT_DISCONNECTED, 0, (void *)0);
}
//
// Remember that we are no longer connected.
//
psHIDDevice->sPrivateData.bConnected = false;
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever a request for a
// non-standard descriptor is received.
//
// \param pvHIDInstance is the instance data for this request.
// \param psUSBRequest points to the request received.
//
// This call parses the provided request structure and determines which
// descriptor is being requested. Assuming the descriptor can be found, it is
// scheduled for transmission via endpoint zero. If the descriptor cannot be
// found, the endpoint is stalled to indicate an error to the host.
//
//*****************************************************************************
static void
HandleGetDescriptor(void *pvHIDInstance, tUSBRequest *psUSBRequest)
{
uint32_t ui32Size, ui32Desc;
const tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Which device are we dealing with?
//
psHIDDevice = pvHIDInstance;
//
// Which type of class descriptor are we being asked for?
//
switch(psUSBRequest->wValue >> 8)
{
//
// This is a request for a HID report or physical descriptor.
//
case USB_HID_DTYPE_REPORT:
case USB_HID_DTYPE_PHYSICAL:
{
//
// Find the index to the descriptor that is being queried.
//
ui32Size = 0;
ui32Desc = FindHIDDescriptor(psHIDDevice,
psUSBRequest->wValue >> 8,
psUSBRequest->wValue & 0xFF,
&ui32Size);
//
// Did we find the descriptor?
//
if(ui32Desc == HID_NOT_FOUND)
{
//
// No - stall the endpoint and return.
//
USBDCDStallEP0(0);
return;
}
//
// If there is more data to send than the host requested then just
// send the requested amount of data.
//
if(ui32Size > psUSBRequest->wLength)
{
ui32Size = psUSBRequest->wLength;
}
//
// Send the data via endpoint 0.
//
USBDCDSendDataEP0(0,
(uint8_t *)psHIDDevice->ppui8ClassDescriptors[ui32Desc],
ui32Size);
break;
}
//
// This is a request for the HID descriptor (as found in the
// configuration descriptor following the relevant interface).
//
case USB_HID_DTYPE_HID:
{
//
// How big is the HID descriptor?
//
ui32Size = (uint32_t)psHIDDevice->psHIDDescriptor->bLength;
//
// If there is more data to send than the host requested then just
// send the requested amount of data.
//
if(ui32Size > psUSBRequest->wLength)
{
ui32Size = psUSBRequest->wLength;
}
//
// Send the data via endpoint 0.
//
USBDCDSendDataEP0(0, (uint8_t *)psHIDDevice->psHIDDescriptor,
ui32Size);
break;
}
//
// This was an unknown request so stall.
//
default:
{
USBDCDStallEP0(0);
break;
}
}
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever a non-standard
// request is received.
//
// \param pvHIDInstance is the instance data for this HID device.
// \param psUSBRequest points to the request received.
//
// This call parses the provided request structure. Assuming the request is
// understood, it is handled and any required response generated. If the
// request cannot be handled by this device class, endpoint zero is stalled to
// indicate an error to the host.
//
//*****************************************************************************
static void
HandleRequest(void *pvHIDInstance, tUSBRequest *psUSBRequest)
{
tHIDInstance *psInst;
uint8_t ui8Protocol;
uint32_t ui32Size, ui32Timeout;
uint8_t *pui8Report;
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Which device are we dealing with?
//
psHIDDevice = pvHIDInstance;
//
// Get a pointer to our instance data.
//
psInst = &psHIDDevice->sPrivateData;
//
// Make sure the request was for this interface.
//
if(psUSBRequest->wIndex != psInst->ui8Interface)
{
return;
}
//
// Determine the type of request.
//
switch(psUSBRequest->bRequest)
{
//
// A Set Report request is received from the host when it sends an
// Output report via endpoint 0.
//
case USBREQ_SET_REPORT:
{
//
// Ask the application for a buffer large enough to hold the
// report we are to be sent.
//
psInst->ui16OutReportSize = psUSBRequest->wLength;
psInst->pui8OutReportData =
(uint8_t *)psHIDDevice->pfnRxCallback(
psHIDDevice->pvRxCBData,
USBD_HID_EVENT_GET_REPORT_BUFFER,
psUSBRequest->wValue,
(void *)(uint32_t)(psUSBRequest->wLength));
//
// Did the client provide us a buffer?
//
if(!psInst->pui8OutReportData)
{
//
// The application could not provide us a buffer so stall the
// request.
//
USBDCDStallEP0(0);
}
else
{
//
// The client provided us a buffer to read the report into
// so request the data from the host.
//
//
// Set the state to indicate we are waiting for data.
//
psInst->iHIDRxState = eHIDStateWaitData;
//
// Now read the payload of the request. We handle the actual
// operation in the data callback once this data is received.
//
USBDCDRequestDataEP0(0, psInst->pui8OutReportData,
(uint32_t)psUSBRequest->wLength);
//
// Need to ACK the data on end point 0 in this case. Do this
// after requesting the data to prevent race conditions that
// occur if you acknowledge before setting up to receive the
// request data.
//
MAP_USBDevEndpointDataAck(psInst->ui32USBBase, USB_EP_0, false);
}
break;
}
//
// A Get Report request is used by the host to poll a device for its
// current state.
//
case USBREQ_GET_REPORT:
{
//
// Get the latest report from the application.
//
ui32Size = psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USBD_HID_EVENT_GET_REPORT,
psUSBRequest->wValue, &pui8Report);
//
// Need to ACK the data on end point 0 in this case.
//
MAP_USBDevEndpointDataAck(psInst->ui32USBBase, USB_EP_0, true);
//
// ..then send back the requested report.
//
psInst->bGetRequestPending = true;
USBDCDSendDataEP0(0, pui8Report, ui32Size);
break;
}
//
// A set IDLE request has been made. This indicates to us how often a
// given report should be sent back to the host in the absence of any
// change in state of the device.
//
case USBREQ_SET_IDLE:
{
//
// Set the idle timeout for the requested report(s).
//
SetIdleTimeout(psHIDDevice, psUSBRequest->wValue & 0xFF,
(psUSBRequest->wValue >> 8) & 0xFF);
//
// Need to ACK the data on end point 0 in this case.
//
MAP_USBDevEndpointDataAck(psInst->ui32USBBase, USB_EP_0, true);
break;
}
//
// A get IDLE request has been made. This request queries the current
// idle timeout for a given report.
//
case USBREQ_GET_IDLE:
{
//
// Determine the timeout for the requested report.
//
ui32Timeout = GetIdleTimeout(psHIDDevice, psUSBRequest->wValue);
if(ui32Timeout != HID_NOT_FOUND)
{
//
// Need to ACK the data on end point 0 in this case.
//
MAP_USBDevEndpointDataAck(psInst->ui32USBBase, USB_EP_0, true);
//
// Send our response to the host.
//
USBDCDSendDataEP0(0, (uint8_t *)&ui32Timeout, 1);
}
else
{
//
// The report ID was not found so stall the endpoint.
//
USBDCDStallEP0(0);
}
break;
}
//
// Set either boot or report protocol for reports sent from the device.
// This is only supported by devices in the boot subclass.
//
case USBREQ_SET_PROTOCOL:
{
if(psHIDDevice->ui8Subclass == USB_HID_SCLASS_BOOT)
{
//
// We need to ACK the data on end point 0 in this case.
//
MAP_USBDevEndpointDataAck(psInst->ui32USBBase, USB_EP_0, true);
//
// We are a boot subclass device so pass this on to the
// application.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USBD_HID_EVENT_SET_PROTOCOL,
psUSBRequest->wValue,
(void *)0);
}
else
{
//
// This is not a boot subclass device so stall the endpoint to
// show that we don't support this request.
//
USBDCDStallEP0(0);
}
break;
}
//
// Inform the host of the protocol, boot or report, that is currently
// in use. This is only supported by devices in the boot subclass.
//
case USBREQ_GET_PROTOCOL:
{
if(psHIDDevice->ui8Subclass == USB_HID_SCLASS_BOOT)
{
//
// We need to ACK the data on end point 0 in this case.
//
MAP_USBDevEndpointDataAck(psInst->ui32USBBase, USB_EP_0, true);
//
// We are a boot subclass device so pass this on to the
// application callback to get the answer.
//
ui8Protocol = (uint8_t)psHIDDevice->pfnRxCallback(
psHIDDevice->pvRxCBData, USBD_HID_EVENT_GET_PROTOCOL, 0,
(void *)0);
//
// Send our response to the host.
//
USBDCDSendDataEP0(0, (uint8_t *)&ui8Protocol, 1);
}
else
{
//
// This is not a boot subclass device so stall the endpoint to
// show that we don't support this request.
//
USBDCDStallEP0(0);
}
break;
}
//
// This request was not recognized so stall.
//
default:
{
USBDCDStallEP0(0);
break;
}
}
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the data requested
// on endpoint zero is received.
//
//*****************************************************************************
static void
HandleEP0DataReceived(void *pvHIDInstance, uint32_t ui32DataSize)
{
tHIDInstance *psInst;
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Which device are we dealing with?
//
psHIDDevice = pvHIDInstance;
//
// If we were not passed any data, just return.
//
if(ui32DataSize == 0)
{
return;
}
//
// Get our instance data pointer.
//
psInst = &psHIDDevice->sPrivateData;
//
// Make sure we are actually expecting something.
//
if(psInst->iHIDRxState != eHIDStateWaitData)
{
return;
}
//
// Change the endpoint state back to idle now that we have been passed
// the data we were waiting for.
//
psInst->iHIDRxState = eHIDStateIdle;
//
// The only things we ever request via endpoint zero are reports sent to
// us via a Set_Report request. Pass the newly received report on to
// the client.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USBD_HID_EVENT_SET_REPORT,
psInst->ui16OutReportSize,
psInst->pui8OutReportData);
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the data sent on
// endpoint zero is received and acknowledged by the host.
//
//*****************************************************************************
static void
HandleEP0DataSent(void *pvHIDInstance, uint32_t ui32Info)
{
tHIDInstance *psInst;
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Which device are we dealing with?
//
psHIDDevice = pvHIDInstance;
//
// Get our instance data pointer.
//
psInst = &psHIDDevice->sPrivateData;
//
// If we just sent a report in response to a Get_Report request, send an
// event to the application telling it that the transmission completed.
//
if(psInst->bGetRequestPending)
{
//
// Clear the flag now that we are sending the application callback.
//
psInst->bGetRequestPending = false;
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USBD_HID_EVENT_REPORT_SENT, 0, (void *)0);
}
return;
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the device is
// reset. If we are currently connected, send a disconnect event at this
// point.
//
//*****************************************************************************
static void
HandleReset(void *pvHIDInstance)
{
ASSERT(pvHIDInstance != 0);
//
// Merely call the disconnect handler. This causes a disconnect message to
// be sent to the client if we think we are currently connected.
//
HandleDisconnect(pvHIDInstance);
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the bus is put into
// suspend state.
//
//*****************************************************************************
static void
HandleSuspend(void *pvHIDInstance)
{
const tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Create the instance pointer.
//
psHIDDevice = (const tUSBDHIDDevice *)pvHIDInstance;
//
// Pass the event on to the client.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData, USB_EVENT_SUSPEND, 0,
(void *)0);
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the bus is taken
// out of suspend state.
//
//*****************************************************************************
static void
HandleResume(void *pvHIDInstance)
{
const tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Create the instance pointer.
//
psHIDDevice = (const tUSBDHIDDevice *)pvHIDInstance;
//
// Pass the event on to the client.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData, USB_EVENT_RESUME, 0,
(void *)0);
}
//*****************************************************************************
//
// This function is called periodically and provides us with a time reference
// and method of implementing delayed or time-dependent operations.
//
// \param pvHIDInstance is the instance data for this request.
// \param ui32TimemS is the elapsed time in milliseconds since the last call
// to this function.
//
// \return None.
//
//*****************************************************************************
static void
HIDTickHandler(void *pvHIDInstance, uint32_t ui32TimemS)
{
tHIDInstance *psInst;
uint32_t ui32Size;
tUSBDHIDDevice *psHIDDevice;
ASSERT(pvHIDInstance != 0);
//
// Create the instance pointer.
//
psHIDDevice = (tUSBDHIDDevice *)pvHIDInstance;
//
// Get our instance data pointer.
//
psInst = &psHIDDevice->sPrivateData;
//
// If we are connected, process our idle timers.
//
if(psInst->bConnected)
{
ProcessIdleTimers(psHIDDevice, ui32TimemS);
}
//
// Do we have a deferred receive waiting
//
if(psInst->ui16DeferredOpFlags & (1 << HID_DO_PACKET_RX))
{
//
// Yes - how big is the waiting packet?
//
ui32Size = MAP_USBEndpointDataAvail(USB0_BASE, psInst->ui8OUTEndpoint);
//
// Tell the client that there is a packet waiting for it.
//
psHIDDevice->pfnRxCallback(psHIDDevice->pvRxCBData,
USB_EVENT_RX_AVAILABLE, ui32Size,
(void *)0);
}
return;
}
//*****************************************************************************
//
//! Initializes HID device operation for a given USB controller.
//!
//! \param ui32Index is the index of the USB controller which is to be
//! initialized for HID device operation.
//! \param psHIDDevice points to a structure containing parameters customizing
//! the operation of the HID device.
//!
//! An application wishing to offer a USB HID interface to a host system
//! must call this function to initialize the USB controller and attach the
//! device to the USB bus. This function performs all required USB
//! initialization.
//!
//! On successful completion, this function will return the \e psHIDDevice
//! pointer passed to it. This must be passed on all future calls from the
//! application to the HID device class driver.
//!
//! The USB HID device class API offers the application a report-based transmit
//! interface for Input reports. Output reports may be received via the
//! control endpoint or via a dedicated Interrupt OUT endpoint. If using the
//! dedicated endpoint, report data is delivered to the application packet-by-
//! packet. If the application uses reports longer than \b USBDHID_MAX_PACKET
//! bytes and would rather receive full reports, it may use a USB buffer above
//! the receive channel to allow full reports to be read.
//!
//! Transmit Operation:
//!
//! Calls to USBDHIDReportWrite() pass complete reports to the driver for
//! transmission. These will be transmitted to the host using as many USB
//! packets as are necessary to complete the transmission.
//!
//! Once a full Input report has been acknowledged by the USB host, a
//! \b USB_EVENT_TX_COMPLETE event is sent to the application transmit callback
//! to inform it that another report may be transmitted.
//!
//! Receive Operation (when using a dedicated interrupt OUT endpoint):
//!
//! An incoming USB data packet will result in a call to the application
//! callback with event \b USB_EVENT_RX_AVAILABLE. The application must then
//! call USBDHIDPacketRead(), passing a buffer capable of holding the received
//! packet. The size of the packet may be determined by calling function
//! USBDHIDRxPacketAvailable() prior to reading the packet.
//!
//! Receive Operation (when not using a dedicated OUT endpoint):
//!
//! If no dedicated OUT endpoint is used, Output and Feature reports are sent
//! from the host using the control endpoint, endpoint zero. When such a
//! report is received, \b USBD_HID_EVENT_GET_REPORT_BUFFER is sent to the
//! application which must respond with a buffer large enough to hold the
//! report. The device class driver will then copy the received report into
//! the supplied buffer before sending \b USBD_HID_EVENT_SET_REPORT to indicate
//! that the report is now available.
//!
//! \note The application must not make any calls to the low level USB device
//! interface if interacting with USB via the USB HID device class API. Doing
//! so will cause unpredictable (though almost certainly unpleasant) behavior.
//!
//! \return Returns NULL on failure or the \e psHIDDevice pointer on success.
//
//*****************************************************************************
void *
USBDHIDInit(uint32_t ui32Index, tUSBDHIDDevice *psHIDDevice)
{
tDeviceDescriptor *pi16DevDesc;
//
// Check parameter validity.
//
ASSERT(ui32Index == 0);
ASSERT(psHIDDevice);
ASSERT(psHIDDevice->ppui8StringDescriptors);
ASSERT(psHIDDevice->pfnRxCallback);
ASSERT(psHIDDevice->pfnTxCallback);
ASSERT(psHIDDevice->ppui8ClassDescriptors);
ASSERT(psHIDDevice->psHIDDescriptor);
ASSERT((psHIDDevice->ui8NumInputReports == 0) || psHIDDevice->psReportIdle);
USBDHIDCompositeInit(ui32Index, psHIDDevice, 0);
//
// Fix up the device descriptor with the client-supplied values.
//
pi16DevDesc = (tDeviceDescriptor *)psHIDDevice->sPrivateData.sDevInfo.pui8DeviceDescriptor;
pi16DevDesc->idVendor = psHIDDevice->ui16VID;
pi16DevDesc->idProduct = psHIDDevice->ui16PID;
//
// All is well so now pass the descriptors to the lower layer and put
// the HID device on the bus.
//
USBDCDInit(ui32Index, &psHIDDevice->sPrivateData.sDevInfo,
(void *)psHIDDevice);
//
// Return the pointer to the instance indicating that everything went well.
//
return((void *)psHIDDevice);
}
//*****************************************************************************
//
//! Initializes HID device operation for a given USB controller.
//!
//! \param ui32Index is the index of the USB controller which is to be
//! initialized for HID device operation.
//! \param psHIDDevice points to a structure containing parameters customizing
//! the operation of the HID device.
//! \param psCompEntry is the composite device entry to initialize when
//! creating a composite device.
//!
//! USB HID device classes call this function to initialize the lower level
//! HID interface in the USB controller. If this HID device device is part of
//! a composite device, then the \e psCompEntry should point to the composite
//! device entry to initialize. This is part of the array that is passed to
//! the USBDCompositeInit() function.
//!
//! \return Returns zero on failure or a non-zero instance value that should be
//! used with the remaining USB HID APIs.
//
//*****************************************************************************
void *
USBDHIDCompositeInit(uint32_t ui32Index, tUSBDHIDDevice *psHIDDevice,
tCompositeEntry *psCompEntry)
{
tHIDInstance *psInst;
tEndpointDescriptor *psEndpoint;
//
// Check parameter validity.
//
ASSERT(ui32Index == 0);
ASSERT(psHIDDevice);
ASSERT(psHIDDevice->ppsConfigDescriptor);
ASSERT(psHIDDevice->ppui8StringDescriptors);
ASSERT(psHIDDevice->pfnRxCallback);
ASSERT(psHIDDevice->pfnTxCallback);
ASSERT(psHIDDevice->ppui8ClassDescriptors);
ASSERT(psHIDDevice->psHIDDescriptor);
ASSERT((psHIDDevice->ui8NumInputReports == 0) || psHIDDevice->psReportIdle);
//
// Initialize the workspace in the passed instance structure.
//
psInst = &psHIDDevice->sPrivateData;
//
// Initialize the device information structure.
//
psInst->sDevInfo.psCallbacks = &g_sHIDHandlers;
psInst->sDevInfo.pui8DeviceDescriptor = g_pui8HIDDeviceDescriptor;
psInst->sDevInfo.ppsConfigDescriptors = psHIDDevice->ppsConfigDescriptor;
psInst->sDevInfo.ppui8StringDescriptors =
psHIDDevice->ppui8StringDescriptors;
psInst->sDevInfo.ui32NumStringDescriptors =
psHIDDevice->ui32NumStringDescriptors;
//
// Default the endpoints zero before looking for them in the configuration
// descriptor.
//
psInst->ui8Interface = 0;
psInst->ui8INEndpoint = 0;
psInst->ui8OUTEndpoint = 0;
//
// Get the first endpoint descriptor on interface 0.
//
psEndpoint =
USBDCDConfigGetInterfaceEndpoint(psHIDDevice->ppsConfigDescriptor[0],
psInst->ui8Interface, 0, 0);
if(psEndpoint)
{
if(psEndpoint->bEndpointAddress & 0x80)
{
psInst->ui8INEndpoint = IndexToUSBEP(psEndpoint->bEndpointAddress);
}
else
{
psInst->ui8OUTEndpoint = IndexToUSBEP(psEndpoint->bEndpointAddress);
}
}
//
// Get the second endpoint descriptor on interface 0.
//
psEndpoint =
USBDCDConfigGetInterfaceEndpoint(psHIDDevice->ppsConfigDescriptor[0],
psInst->ui8Interface, 0, 1);
if(psEndpoint)
{
if(psEndpoint->bEndpointAddress & 0x80)
{
psInst->ui8INEndpoint = IndexToUSBEP(psEndpoint->bEndpointAddress);
}
else
{
psInst->ui8OUTEndpoint = IndexToUSBEP(psEndpoint->bEndpointAddress);
}
}
//
// Must have at least an IN endpoint.
//
if(psInst->ui8INEndpoint == 0)
{
return((void *)0);
}
//
// Initialize the composite entry that is used by the composite device
// class.
//
if(psCompEntry != 0)
{
psCompEntry->psDevInfo = &psInst->sDevInfo;
psCompEntry->pvInstance = (void *)psHIDDevice;
}
psInst->ui32USBBase = USB0_BASE;
psInst->iHIDRxState = eHIDStateUnconfigured;
psInst->iHIDTxState = eHIDStateUnconfigured;
psInst->ui16DeferredOpFlags = 0;
psInst->bConnected = false;
psInst->bGetRequestPending = false;
psInst->bSendInProgress = false;
psInst->ui16InReportIndex = 0;
psInst->ui16InReportSize = 0;
psInst->pui8InReportData = (uint8_t *)0;
psInst->ui16OutReportSize = 0;
psInst->pui8OutReportData = (uint8_t *)0;
//
// Initialize the device info structure for the HID device.
//
USBDCDDeviceInfoInit(0, &psInst->sDevInfo);
//
// Initialize the input report idle timers if any input reports exist.
//
ClearIdleTimers(psHIDDevice);
//
// Initialize the USB tick module, this will prevent it from being
// initialized later in the call to USBDCDInit();
//
InternalUSBTickInit();
//
// Register our tick handler (this must be done after USBDCDInit).
//
InternalUSBRegisterTickHandler(HIDTickHandler, (void *)psHIDDevice);
//
// Return the pointer to the instance indicating that everything went well.
//
return((void *)psHIDDevice);
}
//*****************************************************************************
//
//! Shuts down the HID device.
//!
//! \param pvHIDInstance is the pointer to the device instance structure as
//! returned by USBDHIDInit().
//!
//! This function terminates HID operation for the instance supplied and
//! removes the device from the USB bus. This function should not be called
//! if the HID device is part of a composite device and instead the
//! USBDCompositeTerm() function should be called for the full composite
//! device.
//!
//! Following this call, the \e pvHIDInstance instance should not me used in
//! any other calls.
//!
//! \return None.
//
//*****************************************************************************
void
USBDHIDTerm(void *pvHIDInstance)
{
tHIDInstance *psInst;
ASSERT(pvHIDInstance);
//
// Get a pointer to our instance data.
//
psInst = &((tUSBDHIDDevice *)pvHIDInstance)->sPrivateData;
//
// Terminate the requested instance.
//
USBDCDTerm(USBBaseToIndex(psInst->ui32USBBase));
psInst->ui32USBBase = 0;
}
//*****************************************************************************
//
//! Sets the client-specific pointer parameter for the receive channel
//! callback.
//!
//! \param pvHIDInstance is the pointer to the device instance structure as
//! returned by USBDHIDInit().
//! \param pvCBData is the pointer that client wishes to be provided on each
//! event sent to the receive channel callback function.
//!
//! The client uses this function to change the callback pointer passed in
//! the first parameter on all callbacks to the \e pfnRxCallback function
//! passed on USBDHIDInit().
//!
//! If a client wants to make runtime changes in the callback pointer, it must
//! ensure that the pvHIDInstance structure passed to USBDHIDInit() resides in
//! RAM. If this structure is in flash, callback data changes will not be
//! possible.
//!
//! \return Returns the previous callback pointer that was being used for
//! this instance's receive callback.
//
//*****************************************************************************
void *
USBDHIDSetRxCBData(void *pvHIDInstance, void *pvCBData)
{
void *pvOldValue;
ASSERT(pvHIDInstance);
//
// Set the callback data for the receive channel after remembering the
// previous value.
//
pvOldValue = ((tUSBDHIDDevice *)pvHIDInstance)->pvRxCBData;
((tUSBDHIDDevice *)pvHIDInstance)->pvRxCBData = pvCBData;
//
// Return the previous callback data value.
//
return(pvOldValue);
}
//*****************************************************************************
//
//! Sets the client-specific data pointer for the transmit callback.
//!
//! \param pvHIDInstance is the pointer to the device instance structure as
//! returned by USBDHIDInit().
//! \param pvCBData is the pointer that client wishes to be provided on each
//! event sent to the transmit channel callback function.
//!
//! The client uses this function to change the callback data pointer passed in
//! the first parameter on all callbacks to the \e pfnTxCallback function
//! passed on USBDHIDInit().
//!
//! If a client wants to make runtime changes in the callback data, it must
//! ensure that the pvHIDInstance structure passed to USBDHIDInit() resides in
//! RAM. If this structure is in flash, callback data changes will not be
//! possible.
//!
//! \return Returns the previous callback data pointer that was being used for
//! this instance's transmit callback.
//
//*****************************************************************************
void *
USBDHIDSetTxCBData(void *pvHIDInstance, void *pvCBData)
{
void *pvOldValue;
ASSERT(pvHIDInstance);
//
// Set the callback data for the transmit channel after remembering the
// previous value.
//
pvOldValue = ((tUSBDHIDDevice *)pvHIDInstance)->pvTxCBData;
((tUSBDHIDDevice *)pvHIDInstance)->pvTxCBData = pvCBData;
//
// Return the previous callback data value.
//
return(pvOldValue);
}
//*****************************************************************************
//
//! Transmits a HID device report to the USB host via the HID interrupt IN
//! endpoint.
//!
//! \param pvHIDInstance is the pointer to the device instance structure as
//! returned by USBDHIDInit().
//! \param pi8Data points to the first byte of data which is to be transmitted.
//! \param ui32Length is the number of bytes of data to transmit.
//! \param bLast is ignored in this implementation. This parameter is required
//! to ensure compatibility with other device class drivers and USB buffers.
//!
//! This function schedules the supplied data for transmission to the USB
//! host in a single USB transaction using as many packets as it takes to send
//! all the data in the report. If no transmission is currently ongoing,
//! the first packet of data is immediately copied to the relevant USB endpoint
//! FIFO for transmission. Whenever all the report data has been acknowledged
//! by the host, a \b USB_EVENT_TX_COMPLETE event will be sent to the
//! application transmit callback indicating that another report can now be
//! transmitted.
//!
//! The caller must ensure that the data pointed to by \e pui8Data remains
//! accessible and unaltered until the \b USB_EVENT_TX_COMPLETE is received.
//!
//! \return Returns the number of bytes actually scheduled for transmission.
//! At this level, this will either be the number of bytes passed or 0 to
//! indicate a failure.
//
//*****************************************************************************
uint32_t
USBDHIDReportWrite(void *pvHIDInstance, uint8_t *pi8Data, uint32_t ui32Length,
bool bLast)
{
tHIDInstance *psInst;
int32_t i32Retcode;
ASSERT(pvHIDInstance);
//
// Get our instance data pointer
//
psInst = &((tUSBDHIDDevice *)pvHIDInstance)->sPrivateData;
//
// Set a flag indicating that we are currently in the process of sending
// a packet.
//
psInst->bSendInProgress = true;
//
// Can we send the data provided?
//
if(psInst->iHIDTxState != eHIDStateIdle)
{
//
// We are in the middle of sending another report. Return 0 to
// indicate that we can't send this report until the previous one
// finishes.
//
psInst->bSendInProgress = false;
return(0);
}
//
// Clear the elapsed time since this report was last sent.
//
if(ui32Length)
{
ClearReportTimer(pvHIDInstance, *pi8Data);
}
//
// Keep track of the whereabouts of the report so that we can send it in
// multiple packets if necessary.
//
psInst->pui8InReportData = pi8Data;
psInst->ui16InReportIndex = 0;
psInst->ui16InReportSize = ui32Length;
//
// Schedule transmission of the first packet of the report.
//
psInst->iHIDTxState = eHIDStateWaitData;
i32Retcode = ScheduleReportTransmission(psInst);
//
// Clear the flag we use to indicate that we are in the midst of sending
// a packet.
//
psInst->bSendInProgress = false;
//
// Did an error occur while trying to send the data?
//
if(i32Retcode != -1)
{
//
// No - tell the caller we sent all the bytes provided.
//
return(ui32Length);
}
else
{
//
// Yes - tell the caller we could not send the data.
//
return(0);
}
}
//*****************************************************************************
//
//! Reads a packet of data received from the USB host via the interrupt OUT
//! endpoint (if in use).
//!
//! \param pvHIDInstance is the pointer to the device instance structure as
//! returned by USBDHIDInit().
//! \param pi8Data points to a buffer into which the received data will be
//! written.
//! \param ui32Length is the size of the buffer pointed to by pi8Data.
//! \param bLast indicates whether the client will make a further call to
//! read additional data from the packet.
//!
//! This function reads up to \e ui32Length bytes of data received from the USB
//! host into the supplied application buffer. If the driver detects that the
//! entire packet has been read, it is acknowledged to the host.
//!
//! The \e bLast parameter is ignored in this implementation since the end of
//! a packet can be determined without relying upon the client to provide
//! this information.
//!
//! \return Returns the number of bytes of data read.
//
//*****************************************************************************
uint32_t
USBDHIDPacketRead(void *pvHIDInstance, uint8_t *pi8Data, uint32_t ui32Length,
bool bLast)
{
uint32_t ui32EPStatus, ui32Count, ui32Pkt;
tHIDInstance *psInst;
int32_t i32Retcode;
ASSERT(pvHIDInstance);
//
// Get our instance data pointer
//
psInst = &((tUSBDHIDDevice *)pvHIDInstance)->sPrivateData;
//
// Does the relevant endpoint FIFO have a packet waiting for us?
//
ui32EPStatus = MAP_USBEndpointStatus(psInst->ui32USBBase,
psInst->ui8OUTEndpoint);
if(ui32EPStatus & USB_DEV_RX_PKT_RDY)
{
//
// How many bytes are available for us to receive?
//
ui32Pkt = MAP_USBEndpointDataAvail(psInst->ui32USBBase,
psInst->ui8OUTEndpoint);
//
// Get as much data as we can.
//
ui32Count = ui32Length;
i32Retcode = MAP_USBEndpointDataGet(psInst->ui32USBBase,
psInst->ui8OUTEndpoint,
pi8Data, &ui32Count);
//
// Did we read the last of the packet data?
//
if(ui32Count == ui32Pkt)
{
//
// Clear the endpoint status so that we know no packet is
// waiting.
//
MAP_USBDevEndpointStatusClear(psInst->ui32USBBase,
psInst->ui8OUTEndpoint,
ui32EPStatus);
//
// Acknowledge the data, thus freeing the host to send the
// next packet.
//
MAP_USBDevEndpointDataAck(psInst->ui32USBBase,
psInst->ui8OUTEndpoint, true);
//
// Clear the flag we set to indicate that a packet read is
// pending.
//
SetDeferredOpFlag(&psInst->ui16DeferredOpFlags,
HID_DO_PACKET_RX, false);
}
//
// If all went well, tell the caller how many bytes they got.
//
if(i32Retcode != -1)
{
return(ui32Count);
}
}
//
// No packet was available or an error occurred while reading so tell
// the caller no bytes were returned.
//
return(0);
}
//*****************************************************************************
//
//! Returns the number of free bytes in the transmit buffer.
//!
//! \param pvHIDInstance is the pointer to the device instance structure as
//! returned by USBDHIDInit().
//!
//! This function indicates to the caller whether or not it is safe to send a
//! new report using a call to USBDHIDReportWrite(). The value returned will
//! be the maximum USB packet size (\b USBDHID_MAX_PACKET) if no transmission
//! is currently outstanding or 0 if a transmission is in progress. Since the
//! function USBDHIDReportWrite() can accept full reports longer than a single
//! USB packet, the caller should be aware that the returned value from this
//! class driver, unlike others, does not indicate the maximum size of report
//! that can be written but is merely an indication that another report can be
//! written.
//!
//! \return Returns 0 if an outgoing report is still being transmitted or
//! \b USBDHID_MAX_PACKET if no transmission is currently in progress.
//
//*****************************************************************************
uint32_t
USBDHIDTxPacketAvailable(void *pvHIDInstance)
{
tHIDInstance *psInst;
ASSERT(pvHIDInstance);
//
// Get our instance data pointer.
//
psInst = &((tUSBDHIDDevice *)pvHIDInstance)->sPrivateData;
//
// Do we have a packet transmission currently ongoing?
//
if(psInst->iHIDTxState != eHIDStateIdle)
{
//
// We are not ready to receive a new packet so return 0.
//
return(0);
}
else
{
//
// We can receive a packet so return the max packet size for the
// relevant endpoint.
//
return(USBDHID_MAX_PACKET);
}
}
//*****************************************************************************
//
//! Determines whether a packet is available and, if so, the size of the
//! buffer required to read it.
//!
//! \param pvHIDInstance is the pointer to the device instance structure as
//! returned by USBDHIDInit().
//!
//! This function may be used to determine if a received packet remains to be
//! read and allows the application to determine the buffer size needed to
//! read the data.
//!
//! \return Returns 0 if no received packet remains unprocessed or the
//! size of the packet if a packet is waiting to be read.
//
//*****************************************************************************
uint32_t
USBDHIDRxPacketAvailable(void *pvHIDInstance)
{
uint32_t ui32EPStatus, ui32Size;
tHIDInstance *psInst;
ASSERT(pvHIDInstance);
//
// Get our instance data pointer
//
psInst = &((tUSBDHIDDevice *)pvHIDInstance)->sPrivateData;
//
// Does the relevant endpoint FIFO have a packet waiting for us?
//
ui32EPStatus = MAP_USBEndpointStatus(psInst->ui32USBBase,
psInst->ui8OUTEndpoint);
if(ui32EPStatus & USB_DEV_RX_PKT_RDY)
{
//
// Yes - a packet is waiting. How big is it?
//
ui32Size = MAP_USBEndpointDataAvail(psInst->ui32USBBase,
psInst->ui8OUTEndpoint);
return(ui32Size);
}
else
{
//
// There is no packet waiting to be received.
//
return(0);
}
}
#ifndef DEPRECATED
//*****************************************************************************
//
//! Reports the device power status (bus- or self-powered) to the USB library.
//!
//! \param pvHIDInstance is the pointer to the HID device instance structure.
//! \param ui8Power indicates the current power status, either
//! \b USB_STATUS_SELF_PWR or \b USB_STATUS_BUS_PWR.
//!
//! Applications which support switching between bus- or self-powered
//! operation should call this function whenever the power source changes
//! to indicate the current power status to the USB library. This information
//! is required by the USB library to allow correct responses to be provided
//! when the host requests status from the device.
//!
//! \return None.
//
//*****************************************************************************
void
USBDHIDPowerStatusSet(void *pvHIDInstance, uint8_t ui8Power)
{
ASSERT(pvHIDInstance);
//
// Pass the request through to the lower layer.
//
USBDCDPowerStatusSet(0, ui8Power);
}
#endif
//*****************************************************************************
//
//! Requests a remote wake up to resume communication when in suspended state.
//!
//! \param pvHIDInstance is the pointer to the HID device instance structure.
//!
//! When the bus is suspended, an application which supports remote wake up
//! (advertised to the host via the configuration descriptor) may call this
//! function to initiate remote wake up signaling to the host. If the remote
//! wake up feature has not been disabled by the host, this will cause the bus
//! to resume operation within 20mS. If the host has disabled remote wake up,
//! \b false will be returned to indicate that the wake up request was not
//! successful.
//!
//! \return Returns \b true if the remote wake up is not disabled and the
//! signaling was started or \b false if remote wake up is disabled or if
//! signaling is currently ongoing following a previous call to this function.
//
//*****************************************************************************
bool
USBDHIDRemoteWakeupRequest(void *pvHIDInstance)
{
ASSERT(pvHIDInstance);
//
// Pass the request through to the lower layer.
//
return(USBDCDRemoteWakeupRequest(0));
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
|