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
|
/**
* \file
*
* \brief USB Device Driver for UOTGHS. Compliant with common UDD driver.
*
* Copyright (c) 2012-2015 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="https://www.atmel.com/design-support/">Atmel Support</a>
*/
#ifdef ARDUINO_ARCH_SAM
#include "compiler.h"
#include "uotghs_device_due.h"
#include "conf_usb.h"
#include "sysclk.h"
#include "udd.h"
#include "uotghs_otg.h"
#include <string.h>
#ifndef UDD_NO_SLEEP_MGR
# include "sleep.h"
# include "sleepmgr.h"
#endif
#if !(SAM3XA)
# error The current UOTGHS Device Driver supports only SAM3X and SAM3A.
#endif
#ifndef UDD_USB_INT_FUN
# define UDD_USB_INT_FUN UOTGHS_Handler
#endif
#ifndef UDD_USB_INT_LEVEL
# define UDD_USB_INT_LEVEL 5 // By default USB interrupt have low priority
#endif
#define UDD_EP_USED(ep) (USB_DEVICE_MAX_EP >= ep)
#if ( (UDD_EP_USED( 1) && Is_udd_endpoint_dma_supported( 1)) \
||(UDD_EP_USED( 2) && Is_udd_endpoint_dma_supported( 2)) \
||(UDD_EP_USED( 3) && Is_udd_endpoint_dma_supported( 3)) \
||(UDD_EP_USED( 4) && Is_udd_endpoint_dma_supported( 4)) \
||(UDD_EP_USED( 5) && Is_udd_endpoint_dma_supported( 5)) \
||(UDD_EP_USED( 6) && Is_udd_endpoint_dma_supported( 6)) \
||(UDD_EP_USED( 7) && Is_udd_endpoint_dma_supported( 7)) \
||(UDD_EP_USED( 8) && Is_udd_endpoint_dma_supported( 8)) \
||(UDD_EP_USED( 9) && Is_udd_endpoint_dma_supported( 9)) \
||(UDD_EP_USED(10) && Is_udd_endpoint_dma_supported(10)) \
||(UDD_EP_USED(11) && Is_udd_endpoint_dma_supported(11)) \
||(UDD_EP_USED(12) && Is_udd_endpoint_dma_supported(12)) \
||(UDD_EP_USED(13) && Is_udd_endpoint_dma_supported(13)) \
||(UDD_EP_USED(14) && Is_udd_endpoint_dma_supported(14)) \
||(UDD_EP_USED(15) && Is_udd_endpoint_dma_supported(15)) \
)
# define UDD_EP_DMA_SUPPORTED
#endif
#if ( (UDD_EP_USED( 1) && !Is_udd_endpoint_dma_supported( 1)) \
||(UDD_EP_USED( 2) && !Is_udd_endpoint_dma_supported( 2)) \
||(UDD_EP_USED( 3) && !Is_udd_endpoint_dma_supported( 3)) \
||(UDD_EP_USED( 4) && !Is_udd_endpoint_dma_supported( 4)) \
||(UDD_EP_USED( 5) && !Is_udd_endpoint_dma_supported( 5)) \
||(UDD_EP_USED( 6) && !Is_udd_endpoint_dma_supported( 6)) \
||(UDD_EP_USED( 7) && !Is_udd_endpoint_dma_supported( 7)) \
||(UDD_EP_USED( 8) && !Is_udd_endpoint_dma_supported( 8)) \
||(UDD_EP_USED( 9) && !Is_udd_endpoint_dma_supported( 9)) \
||(UDD_EP_USED(10) && !Is_udd_endpoint_dma_supported(10)) \
||(UDD_EP_USED(11) && !Is_udd_endpoint_dma_supported(11)) \
||(UDD_EP_USED(12) && !Is_udd_endpoint_dma_supported(12)) \
||(UDD_EP_USED(13) && !Is_udd_endpoint_dma_supported(13)) \
||(UDD_EP_USED(14) && !Is_udd_endpoint_dma_supported(14)) \
||(UDD_EP_USED(15) && !Is_udd_endpoint_dma_supported(15)) \
)
# define UDD_EP_FIFO_SUPPORTED
#endif
// for debug text
//#define dbg_print printf
#define dbg_print(...)
/**
* \ingroup udd_group
* \defgroup udd_udphs_group USB On-The-Go High-Speed Port for device mode (UOTGHS)
*
* \section UOTGHS_CONF UOTGHS Custom configuration
* The following UOTGHS driver configuration must be included in the conf_usb.h
* file of the application.
*
* UDD_USB_INT_LEVEL<br>
* Option to change the interrupt priority (0 to 15) by default 5 (recommended).
*
* UDD_USB_INT_FUN<br>
* Option to fit interrupt function to what defined in exception table.
*
* UDD_ISOCHRONOUS_NB_BANK(ep)<br>
* Feature to reduce or increase isochronous endpoints buffering (1 to 3).
* Default value 2.
*
* UDD_BULK_NB_BANK(ep)<br>
* Feature to reduce or increase bulk endpoints buffering (1 to 2).
* Default value 2.
*
* UDD_INTERRUPT_NB_BANK(ep)<br>
* Feature to reduce or increase interrupt endpoints buffering (1 to 2).
* Default value 1.
*
* \section Callbacks management
* The USB driver is fully managed by interrupt and does not request periodique
* task. Thereby, the USB events use callbacks to transfer the information.
* The callbacks are declared in static during compilation or in variable during
* code execution.
*
* Static declarations defined in conf_usb.h:
* - UDC_VBUS_EVENT(bool b_present)<br>
* To signal Vbus level change
* - UDC_SUSPEND_EVENT()<br>
* Called when USB bus enter in suspend mode
* - UDC_RESUME_EVENT()<br>
* Called when USB bus is wakeup
* - UDC_SOF_EVENT()<br>
* Called for each received SOF, Note: Each 1ms in HS/FS mode only.
*
* Dynamic callbacks, called "endpoint job" , are registered
* in udd_ep_job_t structure via the following functions:
* - udd_ep_run()<br>
* To call it when a transfer is finish
* - udd_ep_wait_stall_clear()<br>
* To call it when a endpoint halt is disabled
*
* \section Power mode management
* The Sleep modes authorized :
* - in USB IDLE state, the UOTGHS needs of USB clock and authorizes up to sleep mode WFI.
* - in USB SUSPEND state, the UOTGHS no needs USB clock and authorizes up to sleep mode WAIT.
* @{
*/
// Check USB Device configuration
#ifndef USB_DEVICE_EP_CTRL_SIZE
# error USB_DEVICE_EP_CTRL_SIZE not defined
#endif
#ifndef USB_DEVICE_MAX_EP
# error USB_DEVICE_MAX_EP not defined
#endif
// Note: USB_DEVICE_MAX_EP does not include control endpoint
#if USB_DEVICE_MAX_EP > (UDD_MAX_PEP_NB-1)
# error USB_DEVICE_MAX_EP is too high and not supported by this part
#endif
#define UDD_EP_ISO_NBANK_ERROR(ep) \
( (UDD_ISOCHRONOUS_NB_BANK(ep) < 1) \
|| (UDD_ISOCHRONOUS_NB_BANK(ep) > 3) )
#define UDD_EP_BULK_NBANK_ERROR(ep) \
( (UDD_BULK_NB_BANK(ep) < 1) || (UDD_BULK_NB_BANK(ep) > 2) )
#define UDD_EP_INT_NBANK_ERROR(ep) \
( (UDD_INTERRUPT_NB_BANK(ep) < 1) || (UDD_INTERRUPT_NB_BANK(ep) > 2) )
#define UDD_EP_ISO_NB_BANK_ERROR(ep) \
(UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
#define UDD_EP_BULK_NB_BANK_ERROR(ep) \
(UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
#define UDD_EP_INT_NB_BANK_ERROR(ep) \
(UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
#define UDD_EP_NB_BANK_ERROR(ep, type) \
(ATPASTE3(UDD_EP_, type, _NB_BANK_ERROR(ep)))
#define UDD_ISO_NB_BANK_ERROR \
( UDD_EP_NB_BANK_ERROR( 1, ISO) \
|| UDD_EP_NB_BANK_ERROR( 2, ISO) \
|| UDD_EP_NB_BANK_ERROR( 3, ISO) \
|| UDD_EP_NB_BANK_ERROR( 4, ISO) \
|| UDD_EP_NB_BANK_ERROR( 5, ISO) \
|| UDD_EP_NB_BANK_ERROR( 6, ISO) \
|| UDD_EP_NB_BANK_ERROR( 7, ISO) \
|| UDD_EP_NB_BANK_ERROR( 8, ISO) \
|| UDD_EP_NB_BANK_ERROR( 9, ISO) \
|| UDD_EP_NB_BANK_ERROR(10, ISO) \
|| UDD_EP_NB_BANK_ERROR(11, ISO) \
|| UDD_EP_NB_BANK_ERROR(12, ISO) \
|| UDD_EP_NB_BANK_ERROR(13, ISO) \
|| UDD_EP_NB_BANK_ERROR(14, ISO) \
|| UDD_EP_NB_BANK_ERROR(15, ISO) )
#define UDD_BULK_NB_BANK_ERROR \
( UDD_EP_NB_BANK_ERROR( 1, BULK) \
|| UDD_EP_NB_BANK_ERROR( 2, BULK) \
|| UDD_EP_NB_BANK_ERROR( 3, BULK) \
|| UDD_EP_NB_BANK_ERROR( 4, BULK) \
|| UDD_EP_NB_BANK_ERROR( 5, BULK) \
|| UDD_EP_NB_BANK_ERROR( 6, BULK) \
|| UDD_EP_NB_BANK_ERROR( 7, BULK) \
|| UDD_EP_NB_BANK_ERROR( 8, BULK) \
|| UDD_EP_NB_BANK_ERROR( 9, BULK) \
|| UDD_EP_NB_BANK_ERROR(10, BULK) \
|| UDD_EP_NB_BANK_ERROR(11, BULK) \
|| UDD_EP_NB_BANK_ERROR(12, BULK) \
|| UDD_EP_NB_BANK_ERROR(13, BULK) \
|| UDD_EP_NB_BANK_ERROR(14, BULK) \
|| UDD_EP_NB_BANK_ERROR(15, BULK) )
#define UDD_INTERRUPT_NB_BANK_ERROR \
( UDD_EP_NB_BANK_ERROR( 1, INT) \
|| UDD_EP_NB_BANK_ERROR( 2, INT) \
|| UDD_EP_NB_BANK_ERROR( 3, INT) \
|| UDD_EP_NB_BANK_ERROR( 4, INT) \
|| UDD_EP_NB_BANK_ERROR( 5, INT) \
|| UDD_EP_NB_BANK_ERROR( 6, INT) \
|| UDD_EP_NB_BANK_ERROR( 7, INT) \
|| UDD_EP_NB_BANK_ERROR( 8, INT) \
|| UDD_EP_NB_BANK_ERROR( 9, INT) \
|| UDD_EP_NB_BANK_ERROR(10, INT) \
|| UDD_EP_NB_BANK_ERROR(11, INT) \
|| UDD_EP_NB_BANK_ERROR(12, INT) \
|| UDD_EP_NB_BANK_ERROR(13, INT) \
|| UDD_EP_NB_BANK_ERROR(14, INT) \
|| UDD_EP_NB_BANK_ERROR(15, INT) )
#ifndef UDD_ISOCHRONOUS_NB_BANK
# define UDD_ISOCHRONOUS_NB_BANK(ep) 2
#else
# if UDD_ISO_NB_BANK_ERROR
# error UDD_ISOCHRONOUS_NB_BANK(ep) must be define within 1 to 3.
# endif
#endif
#ifndef UDD_BULK_NB_BANK
# define UDD_BULK_NB_BANK(ep) 2
#else
# if UDD_BULK_NB_BANK_ERROR
# error UDD_BULK_NB_BANK must be define with 1 or 2.
# endif
#endif
#ifndef UDD_INTERRUPT_NB_BANK
# define UDD_INTERRUPT_NB_BANK(ep) 1
#else
# if UDD_INTERRUPT_NB_BANK_ERROR
# error UDD_INTERRUPT_NB_BANK must be define with 1 or 2.
# endif
#endif
/**
* \name Power management routine.
*/
//@{
#ifndef UDD_NO_SLEEP_MGR
//! Definition of sleep levels
#define UOTGHS_SLEEP_MODE_USB_SUSPEND SLEEPMGR_WAIT_FAST
#define UOTGHS_SLEEP_MODE_USB_IDLE SLEEPMGR_SLEEP_WFI
//! State of USB line
static bool udd_b_idle;
//! State of sleep manager
static bool udd_b_sleep_initialized = false;
/*! \brief Authorize or not the CPU powerdown mode
*
* \param b_enable true to authorize idle mode
*/
static void udd_sleep_mode(bool b_idle)
{
if (!b_idle && udd_b_idle) {
dbg_print("_S ");
sleepmgr_unlock_mode(UOTGHS_SLEEP_MODE_USB_IDLE);
}
if (b_idle && !udd_b_idle) {
dbg_print("_W ");
sleepmgr_lock_mode(UOTGHS_SLEEP_MODE_USB_IDLE);
}
udd_b_idle = b_idle;
}
#else
static void udd_sleep_mode(bool b_idle)
{
b_idle = b_idle;
}
#endif // UDD_NO_SLEEP_MGR
//@}
/**
* \name Control endpoint low level management routine.
*
* This function performs control endpoint mangement.
* It handle the SETUP/DATA/HANDSHAKE phases of a control transaction.
*/
//@{
//! Global variable to give and record information about setup request management
COMPILER_WORD_ALIGNED udd_ctrl_request_t udd_g_ctrlreq;
//! Bit definitions about endpoint control state machine for udd_ep_control_state
typedef enum {
UDD_EPCTRL_SETUP = 0, //!< Wait a SETUP packet
UDD_EPCTRL_DATA_OUT = 1, //!< Wait a OUT data packet
UDD_EPCTRL_DATA_IN = 2, //!< Wait a IN data packet
UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP = 3, //!< Wait a IN ZLP packet
UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP = 4, //!< Wait a OUT ZLP packet
UDD_EPCTRL_STALL_REQ = 5, //!< STALL enabled on IN & OUT packet
} udd_ctrl_ep_state_t;
//! State of the endpoint control management
static udd_ctrl_ep_state_t udd_ep_control_state;
//! Total number of data received/sent during data packet phase with previous payload buffers
static uint16_t udd_ctrl_prev_payload_buf_cnt;
//! Number of data received/sent to/from udd_g_ctrlreq.payload buffer
static uint16_t udd_ctrl_payload_buf_cnt;
/**
* \brief Reset control endpoint
*
* Called after a USB line reset or when UDD is enabled
*/
static void udd_reset_ep_ctrl(void);
/**
* \brief Reset control endpoint management
*
* Called after a USB line reset or at the end of SETUP request (after ZLP)
*/
static void udd_ctrl_init(void);
//! \brief Managed reception of SETUP packet on control endpoint
static void udd_ctrl_setup_received(void);
//! \brief Managed reception of IN packet on control endpoint
static void udd_ctrl_in_sent(void);
//! \brief Managed reception of OUT packet on control endpoint
static void udd_ctrl_out_received(void);
//! \brief Managed underflow event of IN packet on control endpoint
static void udd_ctrl_underflow(void);
//! \brief Managed overflow event of OUT packet on control endpoint
static void udd_ctrl_overflow(void);
//! \brief Managed stall event of IN/OUT packet on control endpoint
static void udd_ctrl_stall_data(void);
//! \brief Send a ZLP IN on control endpoint
static void udd_ctrl_send_zlp_in(void);
//! \brief Send a ZLP OUT on control endpoint
static void udd_ctrl_send_zlp_out(void);
//! \brief Call callback associated to setup request
static void udd_ctrl_endofrequest(void);
/**
* \brief Main interrupt routine for control endpoint
*
* This switchs control endpoint events to correct sub function.
*
* \return \c 1 if an event about control endpoint is occured, otherwise \c 0.
*/
static bool udd_ctrl_interrupt(void);
//@}
/**
* \name Management of bulk/interrupt/isochronous endpoints
*
* The UDD manages the data transfer on endpoints:
* - Start data tranfer on endpoint with USB Device DMA
* - Send a ZLP packet if requested
* - Call callback registered to signal end of transfer
* The transfer abort and stall feature are supported.
*/
//@{
#if (0!=USB_DEVICE_MAX_EP)
//! Structure definition about job registered on an endpoint
typedef struct {
union {
//! Callback to call at the end of transfer
udd_callback_trans_t call_trans;
//! Callback to call when the endpoint halt is cleared
udd_callback_halt_cleared_t call_nohalt;
};
//! Buffer located in internal RAM to send or fill during job
uint8_t *buf;
//! Size of buffer to send or fill
iram_size_t buf_size;
//!< Size of data transfered
iram_size_t buf_cnt;
//!< Size of data loaded (or prepared for DMA) last time
iram_size_t buf_load;
//! A job is registered on this endpoint
uint8_t busy:1;
//! A short packet is requested for this job on endpoint IN
uint8_t b_shortpacket:1;
//! A stall has been requested but not executed
uint8_t stall_requested:1;
} udd_ep_job_t;
//! Array to register a job on bulk/interrupt/isochronous endpoint
static udd_ep_job_t udd_ep_job[USB_DEVICE_MAX_EP];
//! \brief Reset all job table
static void udd_ep_job_table_reset(void);
//! \brief Abort all endpoint jobs on going
static void udd_ep_job_table_kill(void);
#ifdef UDD_EP_FIFO_SUPPORTED
/**
* \brief Fill banks and send them
*
* \param ep endpoint number of job to abort
*/
static void udd_ep_in_sent(udd_ep_id_t ep);
/**
* \brief Store received banks
*
* \param ep endpoint number of job to abort
*/
static void udd_ep_out_received(udd_ep_id_t ep);
#endif
/**
* \brief Abort endpoint job on going
*
* \param ep endpoint number of job to abort
*/
static void udd_ep_abort_job(udd_ep_id_t ep);
/**
* \brief Call the callback associated to the job which is finished
*
* \param ptr_job job to complete
* \param b_abort if true then the job has been aborted
*/
static void udd_ep_finish_job(udd_ep_job_t * ptr_job, bool b_abort, uint8_t ep_num);
#ifdef UDD_EP_DMA_SUPPORTED
/**
* \brief Start the next transfer if necessary or complet the job associated.
*
* \param ep endpoint number without direction flag
*/
static void udd_ep_trans_done(udd_ep_id_t ep);
#endif
/**
* \brief Main interrupt routine for bulk/interrupt/isochronous endpoints
*
* This switchs endpoint events to correct sub function.
*
* \return \c 1 if an event about bulk/interrupt/isochronous endpoints has occured, otherwise \c 0.
*/
static bool udd_ep_interrupt(void);
#endif // (0!=USB_DEVICE_MAX_EP)
//@}
// ------------------------
//--- INTERNAL ROUTINES TO MANAGED GLOBAL EVENTS
/**
* \internal
* \brief Function called by UOTGHS interrupt to manage USB Device interrupts
*
* USB Device interrupt events are splited in three parts:
* - USB line events (SOF, reset, suspend, resume, wakeup)
* - control endpoint events (setup reception, end of data transfer, underflow, overflow, stall)
* - bulk/interrupt/isochronous endpoints events (end of data transfer)
*
* Note:
* Here, the global interrupt mask is not clear when an USB interrupt is enabled
* because this one can not be occured during the USB ISR (=during INTX is masked).
* See Technical reference $3.8.3 Masking interrupt requests in peripheral modules.
*/
#ifdef UHD_ENABLE
void udd_interrupt(void);
void udd_interrupt(void)
#else
ISR(UDD_USB_INT_FUN)
#endif
{
/* For fast wakeup clocks restore
* In WAIT mode, clocks are switched to FASTRC.
* After wakeup clocks should be restored, before that ISR should not
* be served.
*/
if (!pmc_is_wakeup_clocks_restored() && !Is_udd_suspend()) {
cpu_irq_disable();
return;
}
if (Is_udd_sof()) {
udd_ack_sof();
if (Is_udd_full_speed_mode()) {
udc_sof_notify();
}
#ifdef UDC_SOF_EVENT
UDC_SOF_EVENT();
#endif
goto udd_interrupt_sof_end;
}
if (Is_udd_msof()) {
udd_ack_msof();
udc_sof_notify();
goto udd_interrupt_sof_end;
}
dbg_print("%c ", udd_is_high_speed() ? 'H' : 'F');
if (udd_ctrl_interrupt()) {
goto udd_interrupt_end; // Interrupt acked by control endpoint managed
}
#if (0 != USB_DEVICE_MAX_EP)
if (udd_ep_interrupt()) {
goto udd_interrupt_end; // Interrupt acked by bulk/interrupt/isochronous endpoint managed
}
#endif
// USB bus reset detection
if (Is_udd_reset()) {
udd_ack_reset();
dbg_print("RST ");
// Abort all jobs on-going
#if (USB_DEVICE_MAX_EP != 0)
udd_ep_job_table_kill();
#endif
// Reset USB Device Stack Core
udc_reset();
// Reset endpoint control
udd_reset_ep_ctrl();
// Reset endpoint control management
udd_ctrl_init();
goto udd_interrupt_end;
}
if (Is_udd_suspend_interrupt_enabled() && Is_udd_suspend()) {
otg_unfreeze_clock();
// The suspend interrupt is automatic acked when a wakeup occur
udd_disable_suspend_interrupt();
udd_enable_wake_up_interrupt();
otg_freeze_clock(); // Mandatory to exit of sleep mode after a wakeup event
udd_sleep_mode(false); // Enter in SUSPEND mode
#ifdef UDC_SUSPEND_EVENT
UDC_SUSPEND_EVENT();
#endif
goto udd_interrupt_end;
}
if (Is_udd_wake_up_interrupt_enabled() && Is_udd_wake_up()) {
// Ack wakeup interrupt and enable suspend interrupt
otg_unfreeze_clock();
// Check USB clock ready after suspend and eventually sleep USB clock
while (!Is_otg_clock_usable()) {
if (Is_udd_suspend()) {
break; // In case of USB state change in HS
}
};
// The wakeup interrupt is automatic acked when a suspend occur
udd_disable_wake_up_interrupt();
udd_enable_suspend_interrupt();
udd_sleep_mode(true); // Enter in IDLE mode
#ifdef UDC_RESUME_EVENT
UDC_RESUME_EVENT();
#endif
goto udd_interrupt_end;
}
if (Is_otg_vbus_transition()) {
dbg_print("VBus ");
// Ack Vbus transition and send status to high level
otg_unfreeze_clock();
otg_ack_vbus_transition();
otg_freeze_clock();
#ifndef USB_DEVICE_ATTACH_AUTO_DISABLE
if (Is_otg_vbus_high()) {
udd_attach();
} else {
udd_detach();
}
#endif
#ifdef UDC_VBUS_EVENT
UDC_VBUS_EVENT(Is_otg_vbus_high());
#endif
goto udd_interrupt_end;
}
udd_interrupt_end:
dbg_print("\n\r");
udd_interrupt_sof_end:
return;
}
bool udd_include_vbus_monitoring(void)
{
return true;
}
void udd_enable(void)
{
irqflags_t flags;
flags = cpu_irq_save();
#ifdef UHD_ENABLE
// DUAL ROLE INITIALIZATION
if (otg_dual_enable()) {
// The current mode has been started by otg_dual_enable()
cpu_irq_restore(flags);
return;
}
#else
// SINGLE DEVICE MODE INITIALIZATION
pmc_enable_periph_clk(ID_UOTGHS);
sysclk_enable_usb();
// Here, only the device mode is possible, then link UOTGHS interrupt to UDD interrupt
NVIC_SetPriority((IRQn_Type) ID_UOTGHS, UDD_USB_INT_LEVEL);
NVIC_EnableIRQ((IRQn_Type) ID_UOTGHS);
// Always authorize asynchrone USB interrupts to exit of sleep mode
// For SAM USB wake up device except BACKUP mode
pmc_set_fast_startup_input(PMC_FSMR_USBAL);
#endif
#if (defined USB_ID_GPIO) && (defined UHD_ENABLE)
// Check that the device mode is selected by ID pin
if (!Is_otg_id_device()) {
cpu_irq_restore(flags);
return; // Device is not the current mode
}
#else
// ID pin not used then force device mode
otg_disable_id_pin();
otg_force_device_mode();
#endif
// Enable USB hardware
otg_enable_pad();
otg_enable();
// Set the USB speed requested by configuration file
#ifdef USB_DEVICE_LOW_SPEED
udd_low_speed_enable();
#else
udd_low_speed_disable();
# ifdef USB_DEVICE_HS_SUPPORT
udd_high_speed_enable();
# else
udd_high_speed_disable();
# endif
#endif // USB_DEVICE_LOW_SPEED
// Check USB clock
otg_unfreeze_clock();
while (!Is_otg_clock_usable());
// Reset internal variables
#if (0!=USB_DEVICE_MAX_EP)
udd_ep_job_table_reset();
#endif
otg_ack_vbus_transition();
// Force Vbus interrupt in case of Vbus always with a high level
// This is possible with a short timing between a Host mode stop/start.
if (Is_otg_vbus_high()) {
otg_raise_vbus_transition();
}
otg_enable_vbus_interrupt();
otg_freeze_clock();
#ifndef UDD_NO_SLEEP_MGR
if (!udd_b_sleep_initialized) {
udd_b_sleep_initialized = true;
// Initialize the sleep mode authorized for the USB suspend mode
udd_b_idle = false;
sleepmgr_lock_mode(UOTGHS_SLEEP_MODE_USB_SUSPEND);
} else {
udd_sleep_mode(false); // Enter idle mode
}
#endif
cpu_irq_restore(flags);
}
void udd_disable(void)
{
irqflags_t flags;
#ifdef UHD_ENABLE
# ifdef USB_ID_GPIO
if (Is_otg_id_host()) {
// Freeze clock to switch mode
otg_freeze_clock();
udd_detach();
otg_disable();
return; // Host mode running, ignore UDD disable
}
# else
if (Is_otg_host_mode_forced()) {
return; // Host mode running, ignore UDD disable
}
# endif
#endif
flags = cpu_irq_save();
otg_unfreeze_clock();
udd_detach();
#ifndef UDD_NO_SLEEP_MGR
if (udd_b_sleep_initialized) {
udd_b_sleep_initialized = false;
sleepmgr_unlock_mode(UOTGHS_SLEEP_MODE_USB_SUSPEND);
}
#endif
#ifndef UHD_ENABLE
otg_disable();
otg_disable_pad();
sysclk_disable_usb();
pmc_disable_periph_clk(ID_UOTGHS);
// Else the USB clock disable is done by UHC which manage USB dual role
#endif
cpu_irq_restore(flags);
}
void udd_attach(void)
{
irqflags_t flags;
flags = cpu_irq_save();
// At startup the USB bus state is unknown,
// therefore the state is considered IDLE to not miss any USB event
udd_sleep_mode(true);
otg_unfreeze_clock();
// This section of clock check can be improved with a chek of
// USB clock source via sysclk()
// Check USB clock because the source can be a PLL
while (!Is_otg_clock_usable());
// Authorize attach if Vbus is present
udd_attach_device();
// Enable USB line events
udd_enable_reset_interrupt();
udd_enable_suspend_interrupt();
udd_enable_wake_up_interrupt();
udd_enable_sof_interrupt();
#ifdef USB_DEVICE_HS_SUPPORT
udd_enable_msof_interrupt();
#endif
// Reset following interupts flag
udd_ack_reset();
udd_ack_sof();
udd_ack_msof();
// The first suspend interrupt must be forced
// The first suspend interrupt is not detected else raise it
udd_raise_suspend();
udd_ack_wake_up();
otg_freeze_clock();
cpu_irq_restore(flags);
}
void udd_detach(void)
{
otg_unfreeze_clock();
// Detach device from the bus
udd_detach_device();
otg_freeze_clock();
udd_sleep_mode(false);
}
bool udd_is_high_speed(void)
{
#ifdef USB_DEVICE_HS_SUPPORT
return !Is_udd_full_speed_mode();
#else
return false;
#endif
}
void udd_set_address(uint8_t address)
{
udd_disable_address();
udd_configure_address(address);
udd_enable_address();
}
uint8_t udd_getaddress(void)
{
return udd_get_configured_address();
}
uint16_t udd_get_frame_number(void)
{
return udd_frame_number();
}
uint16_t udd_get_micro_frame_number(void)
{
return udd_micro_frame_number();
}
void udd_send_remotewakeup(void)
{
#ifndef UDD_NO_SLEEP_MGR
if (!udd_b_idle)
#endif
{
udd_sleep_mode(true); // Enter in IDLE mode
otg_unfreeze_clock();
udd_initiate_remote_wake_up();
}
}
void udd_set_setup_payload(uint8_t *payload, uint16_t payload_size)
{
udd_g_ctrlreq.payload = payload;
udd_g_ctrlreq.payload_size = payload_size;
}
#if (0 != USB_DEVICE_MAX_EP)
bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes,
uint16_t MaxEndpointSize)
{
bool b_dir_in;
uint16_t ep_allocated;
uint8_t nb_bank, bank, i;
b_dir_in = ep & USB_EP_DIR_IN;
ep = ep & USB_EP_ADDR_MASK;
if (ep > USB_DEVICE_MAX_EP) {
return false;
}
if (Is_udd_endpoint_enabled(ep)) {
return false;
}
dbg_print("alloc(%x, %d) ", ep, MaxEndpointSize);
// Bank choise
switch (bmAttributes & USB_EP_TYPE_MASK) {
case USB_EP_TYPE_ISOCHRONOUS:
nb_bank = UDD_ISOCHRONOUS_NB_BANK(ep);
break;
case USB_EP_TYPE_INTERRUPT:
nb_bank = UDD_INTERRUPT_NB_BANK(ep);
break;
case USB_EP_TYPE_BULK:
nb_bank = UDD_BULK_NB_BANK(ep);
break;
default:
Assert(false);
return false;
}
switch (nb_bank) {
case 1:
bank = UOTGHS_DEVEPTCFG_EPBK_1_BANK >>
UOTGHS_DEVEPTCFG_EPBK_Pos;
break;
case 2:
bank = UOTGHS_DEVEPTCFG_EPBK_2_BANK >>
UOTGHS_DEVEPTCFG_EPBK_Pos;
break;
case 3:
bank = UOTGHS_DEVEPTCFG_EPBK_3_BANK >>
UOTGHS_DEVEPTCFG_EPBK_Pos;
break;
default:
Assert(false);
return false;
}
// Check if endpoint size is 8,16,32,64,128,256,512 or 1023
Assert(MaxEndpointSize < 1024);
Assert((MaxEndpointSize == 1023)
|| !(MaxEndpointSize & (MaxEndpointSize - 1)));
Assert(MaxEndpointSize >= 8);
// Set configuration of new endpoint
udd_configure_endpoint(ep, bmAttributes, (b_dir_in ? 1 : 0),
MaxEndpointSize, bank);
ep_allocated = 1 << ep;
// Unalloc endpoints superior
for (i = USB_DEVICE_MAX_EP; i > ep; i--) {
if (Is_udd_endpoint_enabled(i)) {
ep_allocated |= 1 << i;
udd_disable_endpoint(i);
udd_unallocate_memory(i);
}
}
// Realloc/Enable endpoints
for (i = ep; i <= USB_DEVICE_MAX_EP; i++) {
if (ep_allocated & (1 << i)) {
udd_ep_job_t *ptr_job = &udd_ep_job[i - 1];
bool b_restart = ptr_job->busy;
// Restart running job because
// memory window slides up and its data is lost
ptr_job->busy = false;
// Re-allocate memory
udd_allocate_memory(i);
udd_enable_endpoint(i);
if (!Is_udd_endpoint_configured(i)) {
dbg_print("ErrRealloc%d ", i);
if (NULL == ptr_job->call_trans) {
return false;
}
if (Is_udd_endpoint_in(i)) {
i |= USB_EP_DIR_IN;
}
ptr_job->call_trans(UDD_EP_TRANSFER_ABORT,
ptr_job->buf_cnt, i);
return false;
}
udd_enable_endpoint_bank_autoswitch(i);
if (b_restart) {
// Re-run the job remaining part
# ifdef UDD_EP_FIFO_SUPPORTED
if (!Is_udd_endpoint_dma_supported(i)
&& !Is_udd_endpoint_in(i)) {
ptr_job->buf_cnt -= ptr_job->buf_load;
}
# else
ptr_job->buf_cnt -= ptr_job->buf_load;
# endif
b_restart = udd_ep_run(Is_udd_endpoint_in(i) ?
(i | USB_EP_DIR_IN) : i,
ptr_job->b_shortpacket,
&ptr_job->buf[ptr_job->buf_cnt],
ptr_job->buf_size
- ptr_job->buf_cnt,
ptr_job->call_trans);
if (!b_restart) {
dbg_print("ErrReRun%d ", i);
return false;
}
}
}
}
return true;
}
void udd_ep_free(udd_ep_id_t ep)
{
uint8_t ep_index = ep & USB_EP_ADDR_MASK;
if (USB_DEVICE_MAX_EP < ep_index) {
return;
}
udd_disable_endpoint(ep_index);
udd_unallocate_memory(ep_index);
udd_ep_abort_job(ep);
udd_ep_job[ep_index - 1].stall_requested = false;
}
bool udd_ep_is_halted(udd_ep_id_t ep)
{
uint8_t ep_index = ep & USB_EP_ADDR_MASK;
return Is_udd_endpoint_stall_requested(ep_index);
}
bool udd_ep_set_halt(udd_ep_id_t ep)
{
uint8_t ep_index = ep & USB_EP_ADDR_MASK;
udd_ep_job_t *ptr_job = &udd_ep_job[ep_index - 1];
irqflags_t flags;
if (USB_DEVICE_MAX_EP < ep_index) {
return false;
}
if (Is_udd_endpoint_stall_requested(ep_index) // Endpoint stalled
|| ptr_job->stall_requested) { // Endpoint stall is requested
return true; // Already STALL
}
if (ptr_job->busy == true) {
return false; // Job on going, stall impossible
}
flags = cpu_irq_save();
if ((ep & USB_EP_DIR_IN) && (0 != udd_nb_busy_bank(ep_index))) {
// Delay the stall after the end of IN transfer on USB line
ptr_job->stall_requested = true;
#ifdef UDD_EP_FIFO_SUPPORTED
udd_disable_in_send_interrupt(ep_index);
udd_enable_endpoint_bank_autoswitch(ep_index);
#endif
udd_enable_bank_interrupt(ep_index);
udd_enable_endpoint_interrupt(ep_index);
cpu_irq_restore(flags);
return true;
}
// Stall endpoint immediately
udd_disable_endpoint_bank_autoswitch(ep_index);
udd_ack_stall(ep_index);
udd_enable_stall_handshake(ep_index);
cpu_irq_restore(flags);
return true;
}
bool udd_ep_clear_halt(udd_ep_id_t ep)
{
uint8_t ep_index = ep & USB_EP_ADDR_MASK;
udd_ep_job_t *ptr_job = &udd_ep_job[ep_index - 1];
bool b_stall_cleared = false;
if (USB_DEVICE_MAX_EP < ep_index)
return false;
if (ptr_job->stall_requested) {
// Endpoint stall has been requested but not done
// Remove stall request
ptr_job->stall_requested = false;
udd_disable_bank_interrupt(ep_index);
udd_disable_endpoint_interrupt(ep_index);
b_stall_cleared = true;
}
if (Is_udd_endpoint_stall_requested(ep_index)) {
if (Is_udd_stall(ep_index)) {
udd_ack_stall(ep_index);
// A packet has been stalled
// then reset datatoggle
udd_reset_data_toggle(ep_index);
}
// Disable stall
udd_disable_stall_handshake(ep_index);
udd_enable_endpoint_bank_autoswitch(ep_index);
b_stall_cleared = true;
}
if (b_stall_cleared) {
// If a job is register on clear halt action
// then execute callback
if (ptr_job->busy == true) {
ptr_job->busy = false;
ptr_job->call_nohalt();
}
}
return true;
}
bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket,
uint8_t * buf, iram_size_t buf_size,
udd_callback_trans_t callback)
{
#ifdef UDD_EP_FIFO_SUPPORTED
bool b_dir_in = Is_udd_endpoint_in(ep & USB_EP_ADDR_MASK);
#endif
udd_ep_job_t *ptr_job;
irqflags_t flags;
ep &= USB_EP_ADDR_MASK;
if (USB_DEVICE_MAX_EP < ep) {
return false;
}
// Get job about endpoint
ptr_job = &udd_ep_job[ep - 1];
if ((!Is_udd_endpoint_enabled(ep))
|| Is_udd_endpoint_stall_requested(ep)
|| ptr_job->stall_requested) {
return false; // Endpoint is halted
}
flags = cpu_irq_save();
if (ptr_job->busy == true) {
cpu_irq_restore(flags);
return false; // Job already on going
}
ptr_job->busy = true;
cpu_irq_restore(flags);
// No job running. Let's setup a new one.
ptr_job->buf = buf;
ptr_job->buf_size = buf_size;
ptr_job->buf_cnt = 0;
ptr_job->buf_load = 0;
ptr_job->call_trans = callback;
ptr_job->b_shortpacket = b_shortpacket || (buf_size == 0);
#ifdef UDD_EP_FIFO_SUPPORTED
// No DMA support
if (!Is_udd_endpoint_dma_supported(ep)) {
dbg_print("ex%x.%c%d\n\r", ep, b_dir_in ? 'i':'o', buf_size);
flags = cpu_irq_save();
udd_enable_endpoint_interrupt(ep);
if (b_dir_in) {
udd_disable_endpoint_bank_autoswitch(ep);
udd_enable_in_send_interrupt(ep);
} else {
udd_disable_endpoint_bank_autoswitch(ep);
udd_enable_out_received_interrupt(ep);
}
cpu_irq_restore(flags);
return true;
}
#endif // UDD_EP_FIFO_SUPPORTED
#ifdef UDD_EP_DMA_SUPPORTED
// Request first DMA transfer
dbg_print("(exDMA%x) ", ep);
udd_ep_trans_done(ep);
return true;
#endif
}
void udd_ep_abort(udd_ep_id_t ep)
{
uint8_t ep_index = ep & USB_EP_ADDR_MASK;
#ifdef UDD_EP_FIFO_SUPPORTED
if (!Is_udd_endpoint_dma_supported(ep_index)) {
// Disable interrupts
udd_disable_endpoint_interrupt(ep_index);
udd_disable_out_received_interrupt(ep_index);
udd_disable_in_send_interrupt(ep_index);
} else
#endif
{
// Stop DMA transfer
udd_disable_endpoint_dma_interrupt(ep_index);
udd_endpoint_dma_set_control(ep_index, 0);
}
udd_disable_endpoint_interrupt(ep_index);
// Kill IN banks
if (ep & USB_EP_DIR_IN) {
while(udd_nb_busy_bank(ep_index)) {
udd_kill_last_in_bank(ep_index);
while(Is_udd_kill_last(ep_index));
}
}
udd_ep_abort_job(ep);
}
bool udd_ep_wait_stall_clear(udd_ep_id_t ep,
udd_callback_halt_cleared_t callback)
{
udd_ep_job_t *ptr_job;
ep &= USB_EP_ADDR_MASK;
if (USB_DEVICE_MAX_EP < ep) {
return false;
}
ptr_job = &udd_ep_job[ep - 1];
if (!Is_udd_endpoint_enabled(ep)) {
return false; // Endpoint not enabled
}
// Wait clear halt endpoint
if (ptr_job->busy == true) {
return false; // Job already on going
}
if (Is_udd_endpoint_stall_requested(ep)
|| ptr_job->stall_requested) {
// Endpoint halted then registes the callback
ptr_job->busy = true;
ptr_job->call_nohalt = callback;
} else {
// endpoint not halted then call directly callback
callback();
}
return true;
}
#endif // (0 != USB_DEVICE_MAX_EP)
#ifdef USB_DEVICE_HS_SUPPORT
void udd_test_mode_j(void)
{
udd_enable_hs_test_mode();
udd_enable_hs_test_mode_j();
}
void udd_test_mode_k(void)
{
udd_enable_hs_test_mode();
udd_enable_hs_test_mode_k();
}
void udd_test_mode_se0_nak(void)
{
udd_enable_hs_test_mode();
}
void udd_test_mode_packet(void)
{
uint8_t i;
uint8_t *ptr_dest;
const uint8_t *ptr_src;
const uint8_t test_packet[] = {
// 00000000 * 9
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 01010101 * 8
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
// 01110111 * 8
0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
// 0, {111111S * 15}, 111111
0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF,
// S, 111111S, {0111111S * 7}
0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
// 00111111, {S0111111 * 9}, S0
0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
};
// Reconfigure control endpoint to bulk IN endpoint
udd_disable_endpoint(0);
udd_configure_endpoint(0, USB_EP_TYPE_BULK, 1,
64, UOTGHS_DEVEPTCFG_EPBK_1_BANK);
udd_allocate_memory(0);
udd_enable_endpoint(0);
udd_enable_hs_test_mode();
udd_enable_hs_test_mode_packet();
// Send packet on endpoint 0
ptr_dest = (uint8_t *) & udd_get_endpoint_fifo_access(0, 8);
ptr_src = test_packet;
for (i = 0; i < sizeof(test_packet); i++) {
*ptr_dest++ = *ptr_src++;
}
udd_ack_fifocon(0);
}
#endif // USB_DEVICE_HS_SUPPORT
// ------------------------
//--- INTERNAL ROUTINES TO MANAGED THE CONTROL ENDPOINT
static void udd_reset_ep_ctrl(void)
{
irqflags_t flags;
// Reset USB address to 0
udd_configure_address(0);
udd_enable_address();
// Alloc and configure control endpoint
udd_configure_endpoint(0,
USB_EP_TYPE_CONTROL,
0,
USB_DEVICE_EP_CTRL_SIZE,
UOTGHS_DEVEPTCFG_EPBK_1_BANK);
udd_allocate_memory(0);
udd_enable_endpoint(0);
flags = cpu_irq_save();
udd_enable_setup_received_interrupt(0);
udd_enable_out_received_interrupt(0);
udd_enable_endpoint_interrupt(0);
cpu_irq_restore(flags);
}
static void udd_ctrl_init(void)
{
irqflags_t flags;
flags = cpu_irq_save();
// In case of abort of IN Data Phase:
// No need to abort IN transfer (rise TXINI),
// because it is automatically done by hardware when a Setup packet is received.
// But the interrupt must be disabled to don't generate interrupt TXINI
// after SETUP reception.
udd_disable_in_send_interrupt(0);
cpu_irq_restore(flags);
// In case of OUT ZLP event is no processed before Setup event occurs
udd_ack_out_received(0);
udd_g_ctrlreq.callback = NULL;
udd_g_ctrlreq.over_under_run = NULL;
udd_g_ctrlreq.payload_size = 0;
udd_ep_control_state = UDD_EPCTRL_SETUP;
}
static void udd_ctrl_setup_received(void)
{
irqflags_t flags;
uint8_t i;
if (UDD_EPCTRL_SETUP != udd_ep_control_state) {
// May be a hidden DATA or ZLP phase or protocol abort
udd_ctrl_endofrequest();
// Reinitializes control endpoint management
udd_ctrl_init();
}
// Fill setup request structure
if (8 != udd_byte_count(0)) {
udd_ctrl_stall_data();
udd_ack_setup_received(0);
return; // Error data number doesn't correspond to SETUP packet
}
uint8_t *ptr = (uint8_t *) & udd_get_endpoint_fifo_access(0,8);
for (i = 0; i < 8; i++) {
((uint8_t*) &udd_g_ctrlreq.req)[i] = *ptr++;
}
// Manage LSB/MSB to fit with CPU usage
udd_g_ctrlreq.req.wValue = le16_to_cpu(udd_g_ctrlreq.req.wValue);
udd_g_ctrlreq.req.wIndex = le16_to_cpu(udd_g_ctrlreq.req.wIndex);
udd_g_ctrlreq.req.wLength = le16_to_cpu(udd_g_ctrlreq.req.wLength);
// Decode setup request
if (udc_process_setup() == false) {
// Setup request unknow then stall it
udd_ctrl_stall_data();
udd_ack_setup_received(0);
return;
}
udd_ack_setup_received(0);
if (Udd_setup_is_in()) {
// IN data phase requested
udd_ctrl_prev_payload_buf_cnt = 0;
udd_ctrl_payload_buf_cnt = 0;
udd_ep_control_state = UDD_EPCTRL_DATA_IN;
udd_ctrl_in_sent(); // Send first data transfer
} else {
if (0 == udd_g_ctrlreq.req.wLength) {
// No data phase requested
// Send IN ZLP to ACK setup request
udd_ctrl_send_zlp_in();
return;
}
// OUT data phase requested
udd_ctrl_prev_payload_buf_cnt = 0;
udd_ctrl_payload_buf_cnt = 0;
udd_ep_control_state = UDD_EPCTRL_DATA_OUT;
// To detect a protocol error, enable nak interrupt on data IN phase
udd_ack_nak_in(0);
flags = cpu_irq_save();
udd_enable_nak_in_interrupt(0);
cpu_irq_restore(flags);
}
}
static void udd_ctrl_in_sent(void)
{
static bool b_shortpacket = false;
uint16_t nb_remain;
uint8_t i;
uint8_t *ptr_dest, *ptr_src;
irqflags_t flags;
flags = cpu_irq_save();
udd_disable_in_send_interrupt(0);
cpu_irq_restore(flags);
if (UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP == udd_ep_control_state) {
// ZLP on IN is sent, then valid end of setup request
udd_ctrl_endofrequest();
// Reinitializes control endpoint management
udd_ctrl_init();
return;
}
Assert(udd_ep_control_state == UDD_EPCTRL_DATA_IN);
nb_remain = udd_g_ctrlreq.payload_size - udd_ctrl_payload_buf_cnt;
if (0 == nb_remain) {
// All content of current buffer payload are sent
// Update number of total data sending by previous playlaod buffer
udd_ctrl_prev_payload_buf_cnt += udd_ctrl_payload_buf_cnt;
if ((udd_g_ctrlreq.req.wLength == udd_ctrl_prev_payload_buf_cnt)
|| b_shortpacket) {
// All data requested are transfered or a short packet has been sent
// then it is the end of data phase.
// Generate an OUT ZLP for handshake phase.
udd_ctrl_send_zlp_out();
return;
}
// Need of new buffer because the data phase is not complete
if ((!udd_g_ctrlreq.over_under_run)
|| (!udd_g_ctrlreq.over_under_run())) {
// Underrun then send zlp on IN
// Here nb_remain=0 and allows to send a IN ZLP
} else {
// A new payload buffer is given
udd_ctrl_payload_buf_cnt = 0;
nb_remain = udd_g_ctrlreq.payload_size;
}
}
// Continue transfer and send next data
if (nb_remain >= USB_DEVICE_EP_CTRL_SIZE) {
nb_remain = USB_DEVICE_EP_CTRL_SIZE;
b_shortpacket = false;
} else {
b_shortpacket = true;
}
// Fill buffer of endpoint control
ptr_dest = (uint8_t *) & udd_get_endpoint_fifo_access(0, 8);
ptr_src = udd_g_ctrlreq.payload + udd_ctrl_payload_buf_cnt;
// Critical section
// Only in case of DATA IN phase abort without USB Reset signal after.
// The IN data don't must be written in endpoint 0 DPRAM during
// a next setup reception in same endpoint 0 DPRAM.
// Thereby, an OUT ZLP reception must check before IN data write
// and if no OUT ZLP is received the data must be written quickly (800µs)
// before an eventually ZLP OUT and SETUP reception
flags = cpu_irq_save();
if (Is_udd_out_received(0)) {
// IN DATA phase aborted by OUT ZLP
cpu_irq_restore(flags);
udd_ep_control_state = UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP;
return; // Exit of IN DATA phase
}
// Write quickly the IN data
for (i = 0; i < nb_remain; i++) {
*ptr_dest++ = *ptr_src++;
}
udd_ctrl_payload_buf_cnt += nb_remain;
// Validate and send the data available in the control endpoint buffer
udd_ack_in_send(0);
udd_enable_in_send_interrupt(0);
// In case of abort of DATA IN phase, no need to enable nak OUT interrupt
// because OUT endpoint is already free and ZLP OUT accepted.
cpu_irq_restore(flags);
}
static void udd_ctrl_out_received(void)
{
irqflags_t flags;
uint8_t i;
uint16_t nb_data;
if (UDD_EPCTRL_DATA_OUT != udd_ep_control_state) {
if ((UDD_EPCTRL_DATA_IN == udd_ep_control_state)
|| (UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP ==
udd_ep_control_state)) {
// End of SETUP request:
// - Data IN Phase aborted,
// - or last Data IN Phase hidden by ZLP OUT sending quiclky,
// - or ZLP OUT received normaly.
udd_ctrl_endofrequest();
} else {
// Protocol error during SETUP request
udd_ctrl_stall_data();
}
// Reinitializes control endpoint management
udd_ctrl_init();
return;
}
// Read data received during OUT phase
nb_data = udd_byte_count(0);
if (udd_g_ctrlreq.payload_size < (udd_ctrl_payload_buf_cnt + nb_data)) {
// Payload buffer too small
nb_data = udd_g_ctrlreq.payload_size - udd_ctrl_payload_buf_cnt;
}
uint8_t *ptr_src = (uint8_t *) & udd_get_endpoint_fifo_access(0, 8);
uint8_t *ptr_dest = udd_g_ctrlreq.payload + udd_ctrl_payload_buf_cnt;
for (i = 0; i < nb_data; i++) {
*ptr_dest++ = *ptr_src++;
}
udd_ctrl_payload_buf_cnt += nb_data;
if ((USB_DEVICE_EP_CTRL_SIZE != nb_data)
|| (udd_g_ctrlreq.req.wLength <=
(udd_ctrl_prev_payload_buf_cnt +
udd_ctrl_payload_buf_cnt))) {
// End of reception because it is a short packet
// Before send ZLP, call intermediat calback
// in case of data receiv generate a stall
udd_g_ctrlreq.payload_size = udd_ctrl_payload_buf_cnt;
if (NULL != udd_g_ctrlreq.over_under_run) {
if (!udd_g_ctrlreq.over_under_run()) {
// Stall ZLP
udd_ctrl_stall_data();
// Ack reception of OUT to replace NAK by a STALL
udd_ack_out_received(0);
return;
}
}
// Send IN ZLP to ACK setup request
udd_ack_out_received(0);
udd_ctrl_send_zlp_in();
return;
}
if (udd_g_ctrlreq.payload_size == udd_ctrl_payload_buf_cnt) {
// Overrun then request a new payload buffer
if (!udd_g_ctrlreq.over_under_run) {
// No callback availabled to request a new payload buffer
udd_ctrl_stall_data();
// Ack reception of OUT to replace NAK by a STALL
udd_ack_out_received(0);
return;
}
if (!udd_g_ctrlreq.over_under_run()) {
// No new payload buffer delivered
udd_ctrl_stall_data();
// Ack reception of OUT to replace NAK by a STALL
udd_ack_out_received(0);
return;
}
// New payload buffer available
// Update number of total data received
udd_ctrl_prev_payload_buf_cnt += udd_ctrl_payload_buf_cnt;
// Reinit reception on payload buffer
udd_ctrl_payload_buf_cnt = 0;
}
// Free buffer of control endpoint to authorize next reception
udd_ack_out_received(0);
// To detect a protocol error, enable nak interrupt on data IN phase
udd_ack_nak_in(0);
flags = cpu_irq_save();
udd_enable_nak_in_interrupt(0);
cpu_irq_restore(flags);
}
static void udd_ctrl_underflow(void)
{
if (Is_udd_out_received(0))
return; // Underflow ignored if OUT data is received
if (UDD_EPCTRL_DATA_OUT == udd_ep_control_state) {
// Host want to stop OUT transaction
// then stop to wait OUT data phase and wait IN ZLP handshake
udd_ctrl_send_zlp_in();
} else if (UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP == udd_ep_control_state) {
// A OUT handshake is waiting by device,
// but host want extra IN data then stall extra IN data
udd_enable_stall_handshake(0);
}
}
static void udd_ctrl_overflow(void)
{
if (Is_udd_in_send(0))
return; // Overflow ignored if IN data is received
// The case of UDD_EPCTRL_DATA_IN is not managed
// because the OUT endpoint is already free and OUT ZLP accepted
if (UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP == udd_ep_control_state) {
// A IN handshake is waiting by device,
// but host want extra OUT data then stall extra OUT data
udd_enable_stall_handshake(0);
}
}
static void udd_ctrl_stall_data(void)
{
// Stall all packets on IN & OUT control endpoint
udd_ep_control_state = UDD_EPCTRL_STALL_REQ;
udd_enable_stall_handshake(0);
}
static void udd_ctrl_send_zlp_in(void)
{
irqflags_t flags;
udd_ep_control_state = UDD_EPCTRL_HANDSHAKE_WAIT_IN_ZLP;
// Validate and send empty IN packet on control endpoint
flags = cpu_irq_save();
// Send ZLP on IN endpoint
udd_ack_in_send(0);
udd_enable_in_send_interrupt(0);
// To detect a protocol error, enable nak interrupt on data OUT phase
udd_ack_nak_out(0);
udd_enable_nak_out_interrupt(0);
cpu_irq_restore(flags);
}
static void udd_ctrl_send_zlp_out(void)
{
irqflags_t flags;
udd_ep_control_state = UDD_EPCTRL_HANDSHAKE_WAIT_OUT_ZLP;
// No action is necessary to accept OUT ZLP
// because the buffer of control endpoint is already free
// To detect a protocol error, enable nak interrupt on data IN phase
flags = cpu_irq_save();
udd_ack_nak_in(0);
udd_enable_nak_in_interrupt(0);
cpu_irq_restore(flags);
}
static void udd_ctrl_endofrequest(void)
{
// If a callback is registered then call it
if (udd_g_ctrlreq.callback) {
udd_g_ctrlreq.callback();
}
}
static bool udd_ctrl_interrupt(void)
{
if (!Is_udd_endpoint_interrupt(0)) {
return false; // No interrupt events on control endpoint
}
dbg_print("0: ");
// By default disable overflow and underflow interrupt
udd_disable_nak_in_interrupt(0);
udd_disable_nak_out_interrupt(0);
// Search event on control endpoint
if (Is_udd_setup_received(0)) {
dbg_print("stup ");
// SETUP packet received
udd_ctrl_setup_received();
return true;
}
if (Is_udd_in_send(0) && Is_udd_in_send_interrupt_enabled(0)) {
dbg_print("in ");
// IN packet sent
udd_ctrl_in_sent();
return true;
}
if (Is_udd_out_received(0)) {
dbg_print("out ");
// OUT packet received
udd_ctrl_out_received();
return true;
}
if (Is_udd_nak_out(0)) {
dbg_print("nako ");
// Overflow on OUT packet
udd_ack_nak_out(0);
udd_ctrl_overflow();
return true;
}
if (Is_udd_nak_in(0)) {
dbg_print("naki ");
// Underflow on IN packet
udd_ack_nak_in(0);
udd_ctrl_underflow();
return true;
}
dbg_print("n%x ", UOTGHS_ARRAY(UOTGHS_DEVEPTISR[0], 0));
return false;
}
// ------------------------
//--- INTERNAL ROUTINES TO MANAGED THE BULK/INTERRUPT/ISOCHRONOUS ENDPOINTS
#if (0 != USB_DEVICE_MAX_EP)
static void udd_ep_job_table_reset(void)
{
uint8_t i;
for (i = 0; i < USB_DEVICE_MAX_EP; i++) {
udd_ep_job[i].busy = false;
udd_ep_job[i].stall_requested = false;
}
}
static void udd_ep_job_table_kill(void)
{
uint8_t i;
// For each endpoint, kill job
for (i = 0; i < USB_DEVICE_MAX_EP; i++) {
udd_ep_finish_job(&udd_ep_job[i], true, i + 1);
}
}
static void udd_ep_abort_job(udd_ep_id_t ep)
{
ep &= USB_EP_ADDR_MASK;
// Abort job on endpoint
udd_ep_finish_job(&udd_ep_job[ep - 1], true, ep);
}
static void udd_ep_finish_job(udd_ep_job_t * ptr_job, bool b_abort, uint8_t ep_num)
{
if (ptr_job->busy == false) {
return; // No on-going job
}
dbg_print("(JobE%x:%d) ", (ptr_job-udd_ep_job)+1, b_abort);
ptr_job->busy = false;
if (NULL == ptr_job->call_trans) {
return; // No callback linked to job
}
if (Is_udd_endpoint_in(ep_num)) {
ep_num |= USB_EP_DIR_IN;
}
ptr_job->call_trans((b_abort) ? UDD_EP_TRANSFER_ABORT :
UDD_EP_TRANSFER_OK, ptr_job->buf_size, ep_num);
}
#ifdef UDD_EP_DMA_SUPPORTED
static void udd_ep_trans_done(udd_ep_id_t ep)
{
uint32_t udd_dma_ctrl = 0;
udd_ep_job_t *ptr_job;
iram_size_t next_trans;
irqflags_t flags;
// Get job corresponding at endpoint
ptr_job = &udd_ep_job[ep - 1];
if (!ptr_job->busy) {
return; // No job is running, then ignore it (system error)
}
if (ptr_job->buf_cnt != ptr_job->buf_size) {
// Need to send or receiv other data
next_trans = ptr_job->buf_size - ptr_job->buf_cnt;
if (UDD_ENDPOINT_MAX_TRANS < next_trans) {
// The USB hardware support a maximum
// transfer size of UDD_ENDPOINT_MAX_TRANS Bytes
next_trans = UDD_ENDPOINT_MAX_TRANS;
// Set 0 to tranfer the maximum
udd_dma_ctrl = UOTGHS_DEVDMACONTROL_BUFF_LENGTH(0);
} else {
udd_dma_ctrl = UOTGHS_DEVDMACONTROL_BUFF_LENGTH(next_trans);
}
if (Is_udd_endpoint_in(ep)) {
if (0 != (next_trans % udd_get_endpoint_size(ep))) {
// Enable short packet option
// else the DMA transfer is accepted
// and interrupt DMA valid but nothing is sent.
udd_dma_ctrl |= UOTGHS_DEVDMACONTROL_END_B_EN;
// No need to request another ZLP
ptr_job->b_shortpacket = false;
}
} else {
if ((USB_EP_TYPE_ISOCHRONOUS != udd_get_endpoint_type(ep))
|| (next_trans <= (iram_size_t) udd_get_endpoint_size(ep))) {
// Enable short packet reception
udd_dma_ctrl |= UOTGHS_DEVDMACONTROL_END_TR_IT
| UOTGHS_DEVDMACONTROL_END_TR_EN;
}
}
// Start USB DMA to fill or read fifo of the selected endpoint
udd_endpoint_dma_set_addr(ep, (uint32_t) & ptr_job->buf[ptr_job->buf_cnt]);
udd_dma_ctrl |= UOTGHS_DEVDMACONTROL_END_BUFFIT |
UOTGHS_DEVDMACONTROL_CHANN_ENB;
// Disable IRQs to have a short sequence
// between read of EOT_STA and DMA enable
flags = cpu_irq_save();
if (!(udd_endpoint_dma_get_status(ep)
& UOTGHS_DEVDMASTATUS_END_TR_ST)) {
dbg_print("dmaS%x ", ep);
udd_endpoint_dma_set_control(ep, udd_dma_ctrl);
ptr_job->buf_cnt += next_trans;
ptr_job->buf_load = next_trans;
udd_enable_endpoint_dma_interrupt(ep);
cpu_irq_restore(flags);
return;
}
cpu_irq_restore(flags);
// Here a ZLP has been recieved
// and the DMA transfer must be not started.
// It is the end of transfer
ptr_job->buf_size = ptr_job->buf_cnt;
}
if (Is_udd_endpoint_in(ep)) {
if (ptr_job->b_shortpacket) {
dbg_print("zlpS%x ", ep);
// Need to send a ZLP (No possible with USB DMA)
// enable interrupt to wait a free bank to sent ZLP
udd_ack_in_send(ep);
if (Is_udd_write_enabled(ep)) {
// Force interrupt in case of ep already free
udd_raise_in_send(ep);
}
udd_enable_in_send_interrupt(ep);
udd_enable_endpoint_interrupt(ep);
return;
}
}
dbg_print("dmaE ");
// Call callback to signal end of transfer
udd_ep_finish_job(ptr_job, false, ep);
}
#endif
#ifdef UDD_EP_FIFO_SUPPORTED
static void udd_ep_in_sent(udd_ep_id_t ep)
{
udd_ep_job_t *ptr_job = &udd_ep_job[ep - 1];
uint8_t *ptr_src = &ptr_job->buf[ptr_job->buf_cnt];
uint8_t *ptr_dst = (uint8_t *) & udd_get_endpoint_fifo_access(ep, 8);
uint32_t pkt_size = udd_get_endpoint_size(ep);
uint32_t nb_data = 0, i;
uint32_t nb_remain;
irqflags_t flags;
// All transfer done, including ZLP, Finish Job
if (ptr_job->buf_cnt >= ptr_job->buf_size && !ptr_job->b_shortpacket) {
flags = cpu_irq_save();
udd_disable_in_send_interrupt(ep);
udd_disable_endpoint_interrupt(ep);
cpu_irq_restore(flags);
ptr_job->buf_size = ptr_job->buf_cnt; // buf_size is passed to callback as XFR count
udd_ep_finish_job(ptr_job, false, ep);
return;
} else {
// ACK TXINI
udd_ack_in_send(ep);
// Fill FIFO
ptr_dst = (uint8_t *) & udd_get_endpoint_fifo_access(ep, 8);
ptr_src = &ptr_job->buf[ptr_job->buf_cnt];
nb_remain = ptr_job->buf_size - ptr_job->buf_cnt;
// Fill a bank even if no data (ZLP)
nb_data = min(nb_remain, pkt_size);
// Modify job information
ptr_job->buf_cnt += nb_data;
ptr_job->buf_load = nb_data;
// Copy buffer to FIFO
for (i = 0; i < nb_data; i++) {
*ptr_dst++ = *ptr_src++;
}
// Switch to next bank
udd_ack_fifocon(ep);
// ZLP?
if (nb_data < pkt_size) {
ptr_job->b_shortpacket = false;
}
}
}
static void udd_ep_out_received(udd_ep_id_t ep)
{
udd_ep_job_t *ptr_job = &udd_ep_job[ep - 1];
uint32_t nb_data = 0, i;
uint32_t nb_remain = ptr_job->buf_size - ptr_job->buf_cnt;
uint32_t pkt_size = udd_get_endpoint_size(ep);
uint8_t *ptr_src = (uint8_t *) & udd_get_endpoint_fifo_access(ep, 8);
uint8_t *ptr_dst = &ptr_job->buf[ptr_job->buf_cnt];
bool b_full = false, b_short = false;
// Clear RX OUT
udd_ack_out_received(ep);
// Read byte count
nb_data = udd_byte_count(ep);
if (nb_data < pkt_size) {
b_short = true;
}
//dbg_print("o%d ", ep);
//dbg_print("%d ", nb_data);
// Copy data if there is
if (nb_data > 0) {
if (nb_data >= nb_remain) {
nb_data = nb_remain;
b_full = true;
}
// Modify job information
ptr_job->buf_cnt += nb_data;
ptr_job->buf_load = nb_data;
// Copy FIFO to buffer
for (i = 0; i < nb_data; i++) {
*ptr_dst++ = *ptr_src++;
}
}
// Clear FIFO Status
udd_ack_fifocon(ep);
// Finish job on error or short packet
if (b_full || b_short) {
//dbg_print("EoO%d\n\r", ep);
udd_disable_out_received_interrupt(ep);
udd_disable_endpoint_interrupt(ep);
ptr_job->buf_size = ptr_job->buf_cnt; // buf_size is passed to callback as XFR count
udd_ep_finish_job(ptr_job, false, ep);
}
}
#endif // #ifdef UDD_EP_FIFO_SUPPORTED
static bool udd_ep_interrupt(void)
{
udd_ep_id_t ep;
udd_ep_job_t *ptr_job;
// For each endpoint different of control endpoint (0)
for (ep = 1; ep <= USB_DEVICE_MAX_EP; ep++) {
// Get job corresponding at endpoint
ptr_job = &udd_ep_job[ep - 1];
#ifdef UDD_EP_DMA_SUPPORTED
// Check DMA event
if (Is_udd_endpoint_dma_interrupt_enabled(ep)
&& Is_udd_endpoint_dma_interrupt(ep)) {
uint32_t nb_remaining;
if (udd_endpoint_dma_get_status(ep)
& UOTGHS_DEVDMASTATUS_CHANN_ENB) {
return true; // Ignore EOT_STA interrupt
}
dbg_print("dma%x: ", ep);
udd_disable_endpoint_dma_interrupt(ep);
// Save number of data no transfered
nb_remaining = (udd_endpoint_dma_get_status(ep) &
UOTGHS_DEVDMASTATUS_BUFF_COUNT_Msk)
>> UOTGHS_DEVDMASTATUS_BUFF_COUNT_Pos;
if (nb_remaining) {
// Transfer no complete (short packet or ZLP) then:
// Update number of data transfered
ptr_job->buf_cnt -= nb_remaining;
// Set transfer complete to stop the transfer
ptr_job->buf_size = ptr_job->buf_cnt;
}
udd_ep_trans_done(ep);
return true;
}
#endif
#ifdef UDD_EP_FIFO_SUPPORTED
// Check RXRDY and TXEMPTY event for none DMA endpoints
if (!Is_udd_endpoint_dma_supported(ep)
&& Is_udd_endpoint_interrupt_enabled(ep)) {
dbg_print("ep%x: ", ep);
// RXOUT: Full packet received
if (Is_udd_out_received(ep)
&& Is_udd_out_received_interrupt_enabled(ep)) {
dbg_print("Out ");
udd_ep_out_received(ep);
return true;
}
// TXIN: packet sent
if (Is_udd_in_send(ep)
&& Is_udd_in_send_interrupt_enabled(ep)) {
dbg_print("In ");
udd_ep_in_sent(ep);
return true;
}
// Errors: Abort?
if (Is_udd_overflow(ep)
|| Is_udd_underflow(ep)
|| Is_udd_crc_error(ep)) {
dbg_print("Err ");
udd_ep_abort(ep);
return true;
}
}
#endif // UDD_EP_FIFO_SUPPORTED
// Check empty bank interrupt event
if (Is_udd_endpoint_interrupt_enabled(ep)) {
dbg_print("bg%x: ", ep);
if (Is_udd_in_send_interrupt_enabled(ep)
&& Is_udd_in_send(ep)) {
dbg_print("I ");
udd_disable_in_send_interrupt(ep);
// One bank is free then send a ZLP
udd_ack_in_send(ep);
udd_ack_fifocon(ep);
udd_ep_finish_job(ptr_job, false, ep);
return true;
}
if (Is_udd_bank_interrupt_enabled(ep)
&& (0 == udd_nb_busy_bank(ep))) {
dbg_print("EoT ");
// End of background transfer on IN endpoint
udd_disable_bank_interrupt(ep);
udd_disable_endpoint_interrupt(ep);
Assert(ptr_job->stall_requested);
// A stall has been requested during backgound transfer
ptr_job->stall_requested = false;
udd_disable_endpoint_bank_autoswitch(ep);
udd_enable_stall_handshake(ep);
udd_reset_data_toggle(ep);
return true;
}
}
}
return false;
}
#endif // (0 != USB_DEVICE_MAX_EP)
//@}
#endif // ARDUINO_ARCH_SAM
|