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
|
/**
*********************************************************************************
*
* @file ald_usb.c
* @brief USB module driver.
*
* @version V1.0
* @date 25 Feb 2022
* @author AE Team
* @note
* Change Logs:
* Date Author Notes
* 25 Feb 2022 AE Team The first version
*
* Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************************
*/
#include "ald_conf.h"
#include "usb_lowlayer_api.h"
/** @addtogroup ES32VF2264_ALD
* @{
*/
/** @defgroup USB USB
* @brief USB module driver
* @{
*/
#ifdef ALD_USB
/**
* @defgroup USB_Public_Functions USB Public Function
* @{
*/
/** @defgroup USB_Public_Functions_Group1 Base functions
* @brief Base functions
* @{
*/
/**
* @brief Gets the number of current frame.
* @retval Number of the frame.
*/
uint32_t ald_usb_frame_number_get(void)
{
uint8_t framel = (uint8_t)USB->FRAME1;
uint8_t frameh = (uint8_t)USB->FRAME2;
return ((uint32_t)(framel & 0xFF) + ((uint32_t)(frameh & 0xFF) << 8));
}
/**
* @brief Request the session.
* @param start: true/false.
* @retval None
*/
void ald_usb_otg_session_request(bool start)
{
if (start)
USB->DEVCON |= (uint8_t)ALD_USB_DEVCON_SESSION;
else
USB->DEVCON &= (uint8_t)(~(ALD_USB_DEVCON_SESSION));
return;
}
/**
* @brief Gets the mode.
* @retval Mode
*/
uint32_t ald_usb_mode_get(void)
{
return (USB->DEVCON & ((uint8_t)(ALD_USB_DEVCON_HOST | ALD_USB_DEVCON_SESSION)));
}
/**
* @brief Start host require.
* @retval Mode
*/
uint32_t ald_usb_mode_host_req(void)
{
USB->DEVCON |= (uint8_t)ALD_USB_DEVCON_HOSTREQ;
return 0;
}
/**
* @brief Clear host require.
* @retval Mode
*/
uint32_t ald_usb_mode_host_req_clear(void)
{
USB->DEVCON &= (uint8_t)(~ALD_USB_DEVCON_HOSTREQ);
return 0;
}
/**
* @brief Enable/Disable the high mode.
* @param enable: ENABLE/DISABLE.
* @retval None
*/
void ald_usb_high_speed_enable(bool enable)
{
assert_param(enable == DISABLE);
return;
}
/**
* @brief Gets the speed of the device.
* @retval Type of the speed.
*/
uint32_t ald_usb_device_speed_get(void)
{
return ALD_USB_FULL_SPEED;
}
/**
* @brief Gets the number of the endpoint.
* @retval Number of the endpoint.
*/
uint32_t ald_usb_num_ep_get( void)
{
return ALD_NUM_USB_EP;
}
/**
* @brief Reset USB Control.
* @retval None
*/
void ald_usb_control_reset(void)
{
//ALD_RCU_USB_RESET_ENABLE();
return;
}
/**
* @brief Output USB clock, not support.
* @retval None
*/
void ald_usb_clock_output(void)
{
return;
}
/**
* @brief Starts eye diagram for high-speed host, not support.
* @param buf: Buffer for eye diagram.
* @param len: Length of the buffer.
* @retval Status, 0 means success, other values means failure.
*/
int ald_usb_eye_diagram_start(uint8_t *buf, uint16_t len)
{
return 0;
}
/**
* @brief Pull up or pull down USB dp line.
* @param pupd: USB_DPDM_FLOATING/USB_DPDM_PUSH_UP/USB_DPDM_PUSH_DOWN.
* @retval None
*/
void ald_usb_dppud_set(ald_dpdm_push_t pupd)
{
USB->DPDMCON &= (uint8_t)(~USB_DPDMCON_DPPUD_MSK);
USB->DPDMCON |= (uint8_t)(pupd << USB_DPDMCON_DPPUD_POSS);
return;
}
/**
* @brief Pull up or pull down USB dm line.
* @param pupd: USB_DPDM_FLOATING/USB_DPDM_PUSH_UP/USB_DPDM_PUSH_DOWN.
* @retval None
*/
void ald_usb_dmpud_set(ald_dpdm_push_t pupd)
{
USB->DPDMCON &= (uint8_t)(~USB_DPDMCON_DMPUD_MSK);
USB->DPDMCON |= (uint8_t)(pupd << USB_DPDMCON_DMPUD_POSS);
return;
}
/**
* @brief Switch usb mode by software.
* @param host: 0, indicates that software force to host
host: 1, indicates that software force to device.
* @retval None
*/
void ald_usb_swcid_host(uint8_t host)
{
if (host)
USB->SWCID |= (uint8_t)(USB_SWCID_HOST_MSK);
else
USB->SWCID &= (uint8_t)(~USB_SWCID_HOST_MSK);
return;
}
/**
* @brief Switch the control method of CID.
* @param cid: 0, indicates that use the hardware control
cid: 1, indicates that use the software control.
* @retval None
*/
void ald_usb_swcid_cidctrl(uint8_t cid)
{
if (cid)
USB->SWCID |= (uint8_t)(ALD_USB_SWCID_CIDCTRL);
else
USB->SWCID &= (uint8_t)(~ALD_USB_SWCID_CIDCTRL);
return;
}
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group2 Device functions
* @brief Device functions
* @{
*/
/**
* @brief Gets the address.
* @retval Address.
*/
uint8_t ald_usb_dev_get_addr(void)
{
return (USB->FADDR);
}
/**
* @brief Sets the address.
* @param addr: The address which will be set.
* @retval None
*/
void ald_usb_dev_set_addr(uint8_t addr)
{
USB->FADDR = addr;
return;
}
/**
* @brief Enable connection.
* @retval None
*/
void ald_usb_dev_connect(void)
{
USB->DPDMCON |= (uint8_t)(ALD_USB_DPDMCON_PHYPWREN);
return;
}
/**
* @brief Disable connection.
* @retval None
*/
void ald_usb_dev_disconnect(void)
{
USB->DPDMCON &= (uint8_t)(~(ALD_USB_DPDMCON_PHYPWREN));
return;
}
/**
* @brief Enable the devices suspend.
* @retval None
*/
void ald_usb_dev_suspend_enable(void)
{
USB->POWER |= (uint8_t)USB_POWER_SUSPENDEN_MSK;
return;
}
/**
* @brief Configure the endpoint in device mode.
* @param ep_idx: Index of the endpoint
* @param p_max: Size of the maximum package.
* @param flags: Flags of the endpoint.
* @retval None
*/
void ald_usb_dev_ep_config(uint32_t ep_idx, uint32_t p_max, uint32_t flags)
{
uint32_t tmp = 0U;
USB->INDEX = (uint8_t)ep_idx;
if (flags & ALD_USB_EP_DEV_IN) {
USB->TXMAXP = (uint8_t)(p_max);
if (flags & ALD_USB_EP_AUTO_SET)
tmp |= USB_TXCSRH_AUTOSET_MSK;
if ((flags & ALD_USB_EP_MODE_MASK) == ALD_USB_EP_MODE_ISOC)
tmp |= USB_TXCSRH_ISO_MSK;
USB->CSR0H_TXCSRH |= (uint8_t)tmp;
USB->CSR0L_TXCSRL |= (uint8_t)USB_TXCSRL_CLRDT_MSK;
}
else {
USB->RXMAXP = (uint8_t)(p_max);
if (flags & ALD_USB_EP_AUTO_CLEAR)
tmp |= USB_RXCSRH_AUTOCLR_MSK;
if ((flags & ALD_USB_EP_MODE_MASK) == ALD_USB_EP_MODE_ISOC)
tmp |= USB_TXCSRH_ISO_MSK;
USB->RXCSRH |= (uint8_t)tmp;
USB->RXCSRL |= (uint8_t)USB_RXCSRL_CLRDT_MSK;
}
return;
}
/**
* @brief Gets the parameters of the endpoint.
* @param ep_idx: Index of the endpoint
* @param p_max: Size of the maximum package.
* @param flags: Flags of the endpoint.
* @retval None
*/
void ald_usb_dev_ep_get_config(uint32_t ep_idx, uint32_t *p_max, uint32_t *flags)
{
uint32_t tmp;
USB->INDEX = (uint8_t)ep_idx;
if (*flags & ALD_USB_EP_DEV_IN) {
*flags = ALD_USB_EP_DEV_IN;
*p_max = (uint32_t)USB->TXMAXP;
tmp = (uint32_t)USB->CSR0H_TXCSRH;
if (tmp & USB_TXCSRH_AUTOSET_MSK)
*flags |= ALD_USB_EP_AUTO_SET;
if (tmp & USB_TXCSRH_ISO_MSK)
*flags |= ALD_USB_EP_MODE_ISOC;
else
*flags |= ALD_USB_EP_MODE_BULK;
}
else {
*flags = ALD_USB_EP_DEV_OUT;
*p_max = (uint32_t)USB->RXMAXP;
tmp = (uint32_t)USB->RXCSRH;
if (tmp & USB_RXCSRH_AUTOCLR_MSK)
*flags |= ALD_USB_EP_AUTO_CLEAR;
if (tmp & USB_RXCSRH_ISO_MSK)
*flags |= ALD_USB_EP_MODE_ISOC;
else
*flags |= ALD_USB_EP_MODE_BULK;
}
return;
}
extern void printf_e(const char *fmt, ...);
/**
* @brief Acknowledge the data from host.
* @param ep_idx: Index of the endpoint
* @param last: true/false
* @retval None
*/
void ald_usb_dev_ep_data_ack(uint32_t ep_idx, bool last)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0)
USB->CSR0L_TXCSRL |= (uint8_t)(USB_CSR0L_RXRDYC_MSK | (last ? USB_CSR0L_DATAEND_MSK : 0));
else
USB->RXCSRL &= (uint8_t)(~USB_RXCSRL_RXRDY_MSK);
return;
}
/**
* @brief Stall the endpoint.
* @param ep_idx: Index of the endpoint
* @param flags: Flags.
* @retval None
*/
void ald_usb_dev_ep_stall(uint32_t ep_idx, uint32_t flags)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0)
USB->CSR0L_TXCSRL |= (uint8_t)(USB_CSR0L_RXRDYC_MSK | USB_CSR0L_STALL_MSK);
else if (flags == ALD_USB_EP_DEV_IN)
USB->CSR0L_TXCSRL |= (uint8_t)USB_TXCSRL_STALL_MSK;
else
USB->RXCSRL |= (uint8_t)USB_RXCSRL_STALL_MSK;
return;
}
/**
* @brief Cancel the stall status.
* @param ep_idx: Index of the endpoint
* @param flags: Flags.
* @retval None
*/
void ald_usb_dev_ep_stall_clear(uint32_t ep_idx, uint32_t flags)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0)
USB->CSR0L_TXCSRL &= (uint8_t)(~USB_CSR0L_STALLED_MSK);
else if (flags == ALD_USB_EP_DEV_IN){
USB->CSR0L_TXCSRL &= (uint8_t)(~(USB_TXCSRL_STALL_MSK | USB_TXCSRL_STALLED_MSK));
USB->CSR0L_TXCSRL |= (uint8_t)USB_TXCSRL_CLRDT_MSK;
}
else{
USB->RXCSRL &= (uint8_t)(~(USB_RXCSRL_STALL_MSK | USB_RXCSRL_STALLED_MSK));
USB->RXCSRL |= (uint8_t)USB_RXCSRL_CLRDT_MSK;
}
return;
}
/**
* @brief Clear the status of the endpoint.
* @param ep_idx: Index of the endpoint
* @param flags: Flags.
* @retval None
*/
void ald_usb_dev_ep_status_clear(uint32_t ep_idx, uint32_t flags)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0) {
if (flags & ALD_USB_DEV_EP0_OUT_PKTRDY)
USB->CSR0L_TXCSRL |= (uint8_t)USB_CSR0L_RXRDYC_MSK;
if (flags & ALD_USB_DEV_EP0_SETUP_END)
USB->CSR0L_TXCSRL |= (uint8_t)USB_CSR0L_SETENDC_MSK;
if (flags & ALD_USB_DEV_EP0_SENT_STALL)
USB->CSR0L_TXCSRL &= (uint8_t)(~USB_CSR0L_STALLED_MSK);
}
else {
USB->CSR0L_TXCSRL &= (uint8_t)(~(flags & (ALD_USB_DEV_TX_SENT_STALL | ALD_USB_DEV_TX_UNDERRUN)));
USB->RXCSRL &= (uint8_t)(~((flags & (ALD_USB_DEV_RX_SENT_STALL | ALD_USB_DEV_RX_DATA_ERROR
| ALD_USB_DEV_RX_OVERRUN)) >> ALD_USB_RX_EPSTATUS_SHIFT));
}
return;
}
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group3 Host functions
* @brief Host functions
* @{
*/
/**
* @brief Gets the device's address.
* @param ep_idx: Index of the endpoint
* @param flags: Flags.
* @retval Address
*/
uint32_t ald_usb_host_addr_get(uint32_t ep_idx, uint32_t flags)
{
USB->INDEX = (uint8_t)ep_idx;
return (USB->FADDR);
}
/**
* @brief Sets the device's address.
* @param ep_idx: Index of the endpoint.
* @param addr: The device's address.
* @param flags: Flags.
* @retval None
*/
void ald_usb_host_addr_set(uint32_t ep_idx, uint32_t addr, uint32_t flags)
{
USB->INDEX = (uint8_t)ep_idx;
USB->FADDR = (uint8_t)addr;
return;
}
/**
* @brief Configure the endpoint in host mode.
* @param ep_idx: Index of the endpoint.
* @param p_max: Size of the maximum package.
* @param nak_val: Value of the nack.
* @param t_ep: Target endpoint.
* @param flags: Flags.
* @retval None
*/
void ald_usb_host_ep_config(uint32_t ep_idx, uint32_t p_max, uint32_t nak_val, uint32_t t_ep, uint32_t flags)
{
uint32_t tmp = 0U;
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0) {
USB->NAKLIMIT0_TXINTERVAL = (uint8_t)nak_val;
if (flags & ALD_USB_EP_SPEED_HIGH)
;
else if (flags & ALD_USB_EP_SPEED_FULL)
;
else
;
}
else {
tmp = t_ep;
if (flags & ALD_USB_EP_SPEED_HIGH)
;
else if (flags & ALD_USB_EP_SPEED_FULL)
;
else
;
switch (flags & ALD_USB_EP_MODE_MASK) {
case ALD_USB_EP_MODE_BULK:
tmp |= ALD_USB_TXTYPE1_PROTO_BULK;
break;
case ALD_USB_EP_MODE_ISOC:
tmp |= ALD_USB_TXTYPE1_PROTO_ISOC;
break;
case ALD_USB_EP_MODE_INT:
tmp |= ALD_USB_TXTYPE1_PROTO_INT;
break;
case ALD_USB_EP_MODE_CTRL:
tmp |= ALD_USB_TXTYPE1_PROTO_CTRL;
break;
}
if (flags & ALD_USB_EP_HOST_OUT) {
USB->TXTYPE = (uint8_t)tmp;
USB->NAKLIMIT0_TXINTERVAL = (uint8_t)nak_val;
USB->TXMAXP = (uint8_t)p_max;
tmp = 0;
if (flags & ALD_USB_EP_AUTO_SET)
tmp = (uint8_t)ALD_USB_TXCSRH_AUTOSET;
USB->CSR0H_TXCSRH |= (uint8_t)tmp;
}
else {
USB->RXTYPE = (uint8_t)tmp;
USB->RXINTERVAL = (uint8_t)nak_val;
USB->RXMAXP = (uint8_t)p_max;
tmp = 0;
if (flags & ALD_USB_EP_AUTO_CLEAR)
tmp |= (uint8_t)USB_RXCSRH_AUTOCLR_MSK;
if (flags & ALD_USB_EP_AUTO_REQUEST)
tmp |= (uint8_t)USB_RXCSRH_AUTOREQ_MSK;
USB->RXCSRH |= (uint8_t)tmp;
}
}
return;
}
/**
* @brief Acknowledge the data in host mode.
* @param ep_idx: Index of the endpoint.
* @retval None
*/
void ald_usb_host_ep_data_ack(uint32_t ep_idx)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0)
USB->CSR0L_TXCSRL &= (uint8_t)(~(USB_CSR0L_RXRDY_MSK));
else
USB->RXCSRL &= (uint8_t)(~(USB_RXCSRL_RXRDY_MSK));
return;
}
/**
* @brief Toggle the data in host mode.
* The function is used to force the state of the data toggle in host mode.
* If the value passed in the bDataToggle parameter is false, then the data
* toggle is set to the DATA0 state, and if it is true it is set to the DATA1
* state.
* @param ep_idx: Index of the endpoint.
* @param toggle: true/false.
* @param flags: can be USB_EP_HOST_IN or USB_EP_HOST_OUT.
* @retval None
*/
void ald_usb_host_ep_data_toggle(uint32_t ep_idx, bool toggle, uint32_t flags)
{
/* not supported by es32f0271. */
return;
}
/**
* @brief Clear the status of endpoint in host mode.
* @param ep_idx: Index of the endpoint.
* @param flags: Flags.
* @retval None
*/
void ald_usb_host_ep_status_clear(uint32_t ep_idx, uint32_t flags)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0) {
USB->CSR0L_TXCSRL &= (uint8_t)(~flags & 0xFF);
}
else {
USB->CSR0L_TXCSRL &= (uint8_t)(~flags & 0xFF);
USB->RXCSRL &= (uint8_t)(~(flags >> 16) & 0xFF);
}
return;
}
/**
* @brief Gets the HUB's address.
* @param ep_idx: Index of the endpoint.
* @param flags: Flags.
* @retval Address
*/
uint32_t ald_usb_host_hub_addr_get(uint32_t ep_idx, uint32_t flags)
{
/* not supported by es32f0271. */
return 0;
}
/**
* @brief Sets the HUB's address.
* @param ep_idx: Index of the endpoint.
* @param addr: HUB's address which will be set.
* @param flags: Flags.
* @retval Address
*/
void ald_usb_host_hub_addr_set(uint32_t ep_idx, uint32_t addr, uint32_t flags)
{
/* not supported by es32f0271. */
return;
}
/**
* @brief Disable power.
* @retval None
*/
void ald_usb_host_pwr_disable(void)
{
USB->DPDMCON &= (uint8_t)(~(ALD_USB_DPDMCON_PHYPWREN));
return;
}
/**
* @brief Enable power.
* @retval None
*/
void ald_usb_host_pwr_enable(void)
{
USB->DPDMCON |= (uint8_t)(ALD_USB_DPDMCON_PHYPWREN);
return;
}
/**
* @brief Configure power in host mode.
* @param flags: Flags
* @retval None
*/
void ald_usb_host_pwr_config(uint32_t flags)
{
return;
}
/**
* @brief Disable the fault parameters of the power.
* @retval None
*/
void ald_usb_host_pwr_fault_disable(void)
{
return;
}
/**
* @brief Enable the fault parameters of the power.
* @retval None
*/
void ald_usb_host_pwr_fault_enable(void)
{
return;
}
/**
* @brief Request data IN(from device to host)
* @param ep_idx: Index of the endpoint.
* @retval None
*/
void ald_usb_host_request_in(uint32_t ep_idx)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0)
USB->CSR0L_TXCSRL |= (uint8_t)USB_CSR0L_REQPKT_MSK;
else
USB->RXCSRL |= (uint8_t)USB_RXCSRL_REQPKT_MSK;
return;
}
/**
* @brief Clear the status of request IN.
* @param ep_idx: Index of the endpoint.
* @retval None
*/
void ald_usb_host_request_in_clear(uint32_t ep_idx)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0)
USB->CSR0L_TXCSRL &= (uint8_t)(~USB_CSR0L_REQPKT_MSK);
else
USB->RXCSRL &= (uint8_t)(~USB_RXCSRL_REQPKT_MSK);
return;
}
/**
* @brief Set the request for a status IN transaction.
* @retval None
*/
void ald_usb_host_request_status(void)
{
USB->CSR0L_TXCSRL |= (uint8_t)(ALD_USB_CSR0L_REQPKT | ALD_USB_CSR0L_STATUSPKT);
return;
}
/**
* @brief Reset the USB's bus.
* @param start: true/false.
* @retval None
*/
void ald_usb_host_reset(bool start)
{
if (start)
USB->POWER |= ALD_USB_POWER_RESET;
else
USB->POWER &= ~(ALD_USB_POWER_RESET);
return;
}
/**
* @brief Resume the devices.
* @param start: true/false.
* @retval None
*/
void ald_usb_host_resume(bool start)
{
if (start)
USB->POWER |= (uint8_t)USB_POWER_RESUME_MSK;
else
USB->POWER &= (uint8_t)(~(ALD_USB_POWER_RESUME));
return;
}
/**
* @brief Suspend the devices.
* @retval None
*/
void ald_usb_host_suspend(void)
{
USB->POWER |= (uint8_t)USB_POWER_SUSPEND_MSK;
return;
}
/**
* @brief Gets the device's speed.
* @retval Type of the speed.
*/
uint32_t ald_usb_host_speed_get(void)
{
if (USB->DEVCON & ALD_USB_DEVCON_FSDEV)
return ALD_USB_FULL_SPEED;
if (USB->DEVCON & ALD_USB_DEVCON_LSDEV)
return ALD_USB_LOW_SPEED;
return ALD_USB_UNDEF_SPEED;
}
/**
* @brief Sets the endpoint speed.
* @param ep_idx: Index of the endpoint.
* @param flags: Type of the speed.
* @retval None
*/
void ald_usb_host_ep_speed_set(uint32_t ep_idx, uint32_t flags)
{
/* not support in F0271 */
return;
}
/**
* @brief Ping the endpoint.
* @param ep_idx: Index of the endpoint.
* @param enable: ENABLE/DISABLE.
* @retval None
*/
void ald_usb_host_ep_ping(uint32_t ep_idx, bool enable)
{
/* not support in F0271 */
return;
}
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group4 Endpoint functions
* @brief Endpoint functions
* @{
*/
/**
* @brief Gets the size of the available data.
* @param ep_idx: Index of the endpoint
* @retval Size in bytes.
*/
uint32_t ald_usb_ep_data_avail(uint32_t ep_idx)
{
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0) {
if ((USB->CSR0L_TXCSRL & ALD_USB_CSR0L_RXRDY) == 0){
return 0;
}
return USB->COUNT0_RX1;
}
else {
if ((USB->RXCSRL & ALD_USB_CSR0L_RXRDY) == 0)
{
return 0;
}
return (USB->COUNT0_RX1 + (((uint32_t)USB->RXCOUNT2) << 8));
}
}
/**
* @brief Gets the data from FIFO.
* @param ep_idx: Index of the endpoint
* @param data: Pointer to the buffer.
* @param size: Size of the data.
* @retval Status.
*/
int32_t ald_usb_ep_data_get(uint32_t ep_idx, uint8_t *data, uint32_t *size)
{
uint32_t i, rx_fifo_addr;
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0) {
if ((USB->CSR0L_TXCSRL & ALD_USB_CSR0L_RXRDY) == 0) {
*size = 0;
return -1;
}
i = USB->COUNT0_RX1;
}
else {
if ((USB->RXCSRL & ALD_USB_CSR0L_RXRDY) == 0) {
*size = 0;
return -1;
}
i = USB->COUNT0_RX1 + (((uint32_t)USB->RXCOUNT2) << 8);
}
i = (i < *size) ? i : *size;
*size = i;
rx_fifo_addr = (uint32_t)(&USB->EP0FIFO) + 4 * ep_idx;
for (; i > 0; i--)
*data++ = *(volatile uint8_t *)(rx_fifo_addr);
return 0;
}
/**
* @brief Puts data to the FIFO.
* @param ep_idx: Index of the endpoint
* @param data: Pointer to the data.
* @param size: Size of the data.
* @retval Status.
*/
int32_t ald_usb_ep_data_put(uint32_t ep_idx, uint8_t *data, uint32_t size)
{
uint32_t tx_fifo_addr;
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0) {
if (USB->CSR0L_TXCSRL & ALD_USB_CSR0L_TXRDY)
return -1;
}
else {
if (USB->CSR0L_TXCSRL & ALD_USB_TXCSRL_TXRDY)
return -1;
}
tx_fifo_addr = (uint32_t)(&USB->EP0FIFO) + 4 * ep_idx;
for (; size > 0; size--)
*(volatile uint8_t *)tx_fifo_addr = *data++;
return 0;
}
/**
* @brief Send data.
* @param ep_idx: Index of the endpoint
* @param tx_type: Type.
* @retval Status.
*/
int32_t ald_usb_ep_data_send(uint32_t ep_idx, uint32_t tx_type)
{
uint32_t tmp;
USB->INDEX = (uint8_t)ep_idx;
if (ep_idx == ALD_USB_EP_0) {
if (USB->CSR0L_TXCSRL & ALD_USB_CSR0L_TXRDY)
return -1;
tmp = tx_type & 0xFF;
}
else {
if (USB->CSR0L_TXCSRL & ALD_USB_TXCSRL_TXRDY)
return -1;
tmp = (tx_type >> 8) & 0xff;
}
USB->CSR0L_TXCSRL = tmp;
return 0;
}
/**
* @brief Clear the status of the toggle.
* @param ep_idx: Index of the endpoint
* @param flags: Flags.
* @retval None
*/
void ald_usb_ep_data_toggle_clear(uint32_t ep_idx, uint32_t flags)
{
USB->INDEX = (uint8_t)ep_idx;
if (flags & (ALD_USB_EP_HOST_OUT | ALD_USB_EP_DEV_IN))
USB->CSR0L_TXCSRL |= (uint8_t)ALD_USB_TXCSRL_CLRDT;
else
USB->RXCSRL |= (uint8_t)ALD_USB_RXCSRL_CLRDT;
return;
}
/**
* @brief Sets the size of request data IN
* @param ep_idx: Index of the endpoint
* @param count: Size of request data IN.
* @retval None
*/
void ald_usb_ep_req_packet_count(uint32_t ep_idx, uint32_t count)
{
/* not support in f0271 */
return;
}
/**
* @brief Gets the status of the endpoint.
* @param ep_idx: Index of the endpoint
* @retval Status.
*/
uint32_t ald_usb_ep_status(uint32_t ep_idx)
{
uint32_t status;
USB->INDEX = (uint8_t)ep_idx;
status = (ep_idx == ALD_USB_EP_0) ? (USB->CSR0L_TXCSRL): (USB->CSR0L_TXCSRL | (USB->RXCSRL << 16));
return status;
}
/**
* @brief Configure the endpoint in DMA mode.
* @param ep_idx: Index of the endpoint
* @param flag: Flags.
* @param en: ENABLE/DISABLE.
* @retval None
*/
void ald_usb_ep_dma_config(uint32_t ep_idx, uint32_t flag, type_func_t en)
{
/* Not supported in F0271 */
return;
}
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group5 FIFO functions
* @brief FIFO functions
* @{
*/
/**
* @brief Gets the address of the FIFO.
* @param ep_idx: Index of the endpoint
* @retval Address
*/
uint32_t ald_usb_fifo_addr_get(uint32_t ep_idx)
{
/* Not supported in F0271 */
return 0;
}
/**
* @brief Gets the parameters of the FIFO.
* @param ep_idx: Index of the endpoint
* @param addr: Address.
* @param size: Size of FIFO.
* @param flags: Flags.
* @retval None
*/
void ald_usb_fifo_config_get(uint32_t ep_idx, uint32_t *addr, uint32_t *size, uint32_t flags)
{
USB->INDEX = ep_idx;
if (flags & (ALD_USB_EP_HOST_OUT | ALD_USB_EP_DEV_IN)) {
*addr = ((uint32_t)USB->TXFIFO1 | ((uint32_t)USB->TXFIFO2 & 0x03)) << 3;
*size = ((USB->TXFIFO2 & ALD_USB_TXFIFO2_DPB) == 0x00) ? (USB->TXFIFO2 & ALD_USB_TXFIFO2_MAXPKTSIZE_1024) : (2 * (USB->TXFIFO2 & ALD_USB_TXFIFO2_MAXPKTSIZE_1024));
}
else {
*addr = ((uint32_t)USB->RXFIFO1 | ((uint32_t)USB->RXFIFO2 & 0x03)) << 3;
*size = ((USB->RXFIFO2 & ALD_USB_RXFIFO2_DPB) == 0x00) ? (USB->RXFIFO2 & ALD_USB_RXFIFO2_MAXPKTSIZE_1024) : (2 * (USB->RXFIFO2 & ALD_USB_RXFIFO2_MAXPKTSIZE_1024));
}
return;
}
/**
* @brief Sets the parameters of the FIFO.
* @param ep_idx: Index of the endpoint
* @param addr: Address.
* @param size: Size of FIFO, valid parameter has defined in usb_lowlayer_api.h.
* @param flags: Flags.
* @retval None
*/
void ald_usb_fifo_config_set(uint32_t ep_idx, uint32_t addr, uint32_t size, uint32_t flags)
{
USB->INDEX = ep_idx;
if (flags & (ALD_USB_EP_HOST_OUT | ALD_USB_EP_DEV_IN)) {
USB->TXFIFO1 = (uint8_t)((addr >> 3) & 0xFF);
USB->TXFIFO2 |= (uint8_t)(((addr >> 3) >> 8) & 0x0F);
USB->TXFIFO2 |= (uint8_t)(size << USB_TXFIFO2_MAXPKTSIZE_POSS);
USB->CSR0L_TXCSRL |= (uint8_t)ALD_USB_TXCSRL_FLUSH;
}
else {
USB->RXFIFO1 = (uint8_t)((addr >> 3) & 0xFF);
USB->RXFIFO2 |= (uint8_t)(((addr >> 3) >> 8) & 0x0F);
USB->RXFIFO2 |= (uint8_t)(size << USB_RXFIFO2_MAXPKTSIZE_POSS);
USB->RXCSRL |= (uint8_t)ALD_USB_RXCSRL_FLUSH;
}
return;
}
/**
* @brief Flush the FIFO
* @param ep_idx: Index of the endpoint
* @param flags: Flags.
* @retval None
*/
void ald_usb_fifo_flush(uint32_t ep_idx, uint32_t flags)
{
USB->INDEX = ep_idx;
if (ep_idx == ALD_USB_EP_0) {
if ((USB->CSR0L_TXCSRL & (ALD_USB_CSR0L_RXRDY | ALD_USB_CSR0L_TXRDY)) != 0)
USB->CSR0H_TXCSRH |= ALD_USB_CSR0H_FLUSH;
}
else {
if (flags & (ALD_USB_EP_HOST_OUT | ALD_USB_EP_DEV_IN)) {
if (USB->CSR0L_TXCSRL & ALD_USB_TXCSRL_TXRDY)
USB->CSR0L_TXCSRL |= ALD_USB_TXCSRL_FLUSH;
}
else {
if (USB->RXCSRL & ALD_USB_RXCSRL_RXRDY)
USB->RXCSRL |= ALD_USB_RXCSRL_FLUSH;
}
}
return;
}
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group6 Interrupt functions
* @brief Interrupt functions
* @{
*/
/**
* @brief Disable interrupt.
* @param flags: Type of the interrupt.
* @retval None
*/
void ald_usb_int_disable(uint32_t flags)
{
if (flags & ALD_USB_IDR_STATUS)
USB->IDR |= (uint8_t)(flags & ALD_USB_IDR_STATUS);
return;
}
/**
* @brief Enable interrupt.
* @param flags: Type of the interrupt.
* @retval None
*/
void ald_usb_int_enable(uint32_t flags)
{
if (flags & ALD_USB_IER_STATUS)
USB->IER |= (uint8_t)(flags & ALD_USB_IER_STATUS);
return;
}
/**
* @brief Gets the status of the interrupt.
* @retval Status.
*/
uint32_t ald_usb_int_status_get(void)
{
uint32_t Status;
Status = USB->IFM & 0x7F;
USB->ICR |= Status;
return Status;
}
/**
* @brief Disable interrupt of the endpoint.
* @param flags: Type of the interrupt.
* @retval None
*/
void ald_usb_int_disable_ep(uint32_t flags)
{
USB->TXIDR |= (uint8_t)(flags & (ALD_USB_INTEP_HOST_OUT | ALD_USB_INTEP_DEV_IN | ALD_USB_INTEP_0));
USB->RXIDR |= (uint8_t)((flags & (ALD_USB_INTEP_HOST_IN | ALD_USB_INTEP_DEV_OUT)) >> ALD_USB_INTEP_RX_SHIFT);
return;
}
/**
* @brief Enable interrupt of the endpoint.
* @param flags: Type of the interrupt.
* @retval None
*/
void ald_usb_int_enable_ep(uint32_t flags)
{
USB->TXIER |= (uint8_t)(flags & (ALD_USB_INTEP_HOST_OUT | ALD_USB_INTEP_DEV_IN | ALD_USB_INTEP_0));
USB->RXIER |= (uint8_t)((flags & (ALD_USB_INTEP_HOST_IN | ALD_USB_INTEP_DEV_OUT)) >> ALD_USB_INTEP_RX_SHIFT);
return;
}
/**
* @brief Gets the ststus of the endpoint interrupt.
* @retval Status.
*/
uint32_t ald_usb_int_status_ep_get(void)
{
uint32_t status;
status = USB->TXIFM;
status |= (USB->RXIFM << ALD_USB_INTEP_RX_SHIFT);
USB->TXICR |= (uint8_t)(status & 0xFF);
USB->RXICR |= (uint8_t)((status >> ALD_USB_INTEP_RX_SHIFT) & 0xFF);
return status;
}
/**
* @brief Register USB's interrupt.
* @retval None
*/
void ald_usb_int_register(void)
{
//ald_mcu_irq_config(USB_IRQn, 2, ENABLE);
csi_vic_set_prio(USB_IRQn, 3);
csi_vic_enable_sirq(USB_IRQn);
return;
}
/**
* @brief Unregister USB's interrupt.
* @retval None
*/
void ald_usb_int_unregister(void)
{
//ald_mcu_irq_config(USB_IRQn, 2, DISABLE);
csi_vic_disable_sirq(USB_IRQn);
return;
}
/**
* @brief Get USB's interrupt number.
* @retval None
*/
uint32_t ald_usb_int_num_get(void)
{
return USB_IRQn;
}
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group7 DMA functions
* @brief DMA functions
* @{
*/
#if defined(__ALD_MDA_H__)
/**
* @brief Configure DMA's channel.
* @param ch: Channel.
* @param addr: Address.
* @param count: Size of the data to be moved.
* @param ctrl: Parameters of the DMA's controler
* @retval None
*/
void ald_usb_dma_channel_config(uint8_t ch, uint32_t addr, uint32_t count, uint32_t ctrl)
{
return;
}
/**
* @brief Start multiple receive.
* @param ep_idx: Index of the endpoint
* @retval None
*/
void ald_usb_dma_mult_recv_start(uint32_t ep_idx)
{
return;
}
/**
* @brief Start DMA's machine.
* @param ch: Channel.
* @retval None
*/
void ald_usb_dma_channel_start(uint8_t ch)
{
return;
}
/**
* @brief Stop DMA's machine.
* @param ch: Channel.
* @retval None
*/
void ald_usb_dma_channel_stop(uint8_t ch)
{
return;
}
/**
* @brief Gets flags of the interrupt.
* @retval Flags of the interrupt.
*/
uint32_t ald_usb_dma_get_interrupt_flag(void)
{
return;
}
/**
* @brief Gets the status of the error.
* @param ch: Channel.
* @retval Status.
*/
uint32_t ald_usb_dma_get_channel_error(uint8_t ch)
{
return 0;
}
/**
* @brief Clear the status of the error.
* @param ch: Channel.
* @retval None
*/
void ald_usb_dma_clear_channel_error(uint8_t ch)
{
return;
}
#endif
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group8 LPM functions
* @brief LPM functions
* @{
*/
/**
* @brief Gets status of remote wakeup.
* @retval Status.
*/
uint32_t ald_usb_lpm_remote_wake_is_enable(void)
{
return 1;
}
/**
* @brief Gets the link status
* @retval Status
*/
uint32_t ald_usb_lpm_link_status_get(void)
{
return 0;
}
/**
* @brief Gets the index of the endpoint.
* @retval Index of the endpoint.
*/
uint32_t ald_usb_lpm_ep_get(void)
{
return 0;
}
/**
* @brief Gets the status of the interrupt.
* @retval Status.
*/
uint32_t ald_usb_lpm_int_status_get(void)
{
return 0;
}
/**
* @brief Disable the LPM interrupt.
* @retval None
*/
void ald_usb_lpm_int_disable(uint32_t ints)
{
return;
}
/**
* @brief Enable the LPM interrupt.
* @retval None
*/
void ald_usb_lpm_int_enable(uint32_t ints)
{
return;
}
/**
* @brief Transmit a LPM transaction in host mode.
* @param addr: Address.
* @param ep_idx: Index of the endpoint.
* @retval None
*/
void ald_usb_host_lpm_send(uint32_t addr, uint32_t ep_idx)
{
return;
}
/**
* @brief Configure the LPM parameters in host mode.
* @param resume_time: Resume time.
* @param config: Parameters
* @retval None
*/
void ald_usb_host_lpm_config(uint32_t resume_time, uint32_t config)
{
return;
}
/**
* @brief Initiate a RESUME from the L1 state in host mode.
* @retval None
*/
void ald_usb_host_lpm_resume(void)
{
return;
}
/**
* @brief Enable remote wakeup in device mode.
* @retval None
*/
void ald_usb_dev_lpm_remote_wake(void)
{
return;
}
/**
* @brief Enable remote wakeup in device mode.
* @retval None
*/
void ald_usb_dev_lpm_config(uint32_t config)
{
return;
}
/**
* @brief Enable LPM in device mode.
* @retval None
*/
void ald_usb_dev_lpm_enable(void)
{
return;
}
/**
* @brief Disable LPM in device mode.
* @retval None
*/
void ald_usb_dev_lpm_disable(void)
{
return;
}
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group10 LPM functions
* @brief USB SWVBUS control
* @{
*/
/**
* @brief Selet the control mode of VBUS.
* @param sigctl: 0, indicates that use the hardware control
* sigctl: 1, indicates that use the software control.
* @retval Status.
*/
void ald_usb_swvbus_sigctl_set(uint8_t sigctl)
{
if (sigctl == 1)
{
USB->SWVBUS |= 0x01 << 0;
}
else
{
USB->SWVBUS &= ~(0x01 << 0);
}
return;
}
/**
* @brief Selet the control mode of VBUS.
* @param None.
* @retval Status: 0, indicates that use the hardware control
* Status: 1, indicates that use the software control.
*/
uint8_t ald_usb_swvbus_sigctl_get(void)
{
return (USB->SWVBUS & (0x01 << 0));
}
/**
* @brief Set session end threshold.
* @param thd: 0, indicates that lower than the session end threshold
* thd: 1, indicates that higher than the session end threshold.
* @retval Status.
*/
void ald_usb_swvbus_sesendth_set(uint8_t thd)
{
if (thd == 1)
{
USB->SWVBUS |= 0x01 << 1;
}
else
{
USB->SWVBUS &= ~(0x01 << 1);
}
return;
}
/**
* @brief Set session end threshold.
* @param None.
* @retval Status: 0, indicates that lower than the session end threshold
* Status: 1, indicates that higher than the session end threshold.
*/
uint8_t ald_usb_swvbus_sesendth_get(void)
{
return (USB->SWVBUS & (0x01 << 1));
}
/**
* @brief Set session valid threshold.
* @param thd: 0, indicates that lower than the session valid threshold
thd: 1, indicates that higher than the session valid threshold.
* @retval Status.
*/
void ald_usb_swvbus_sesvalth_set(uint8_t thd)
{
if (thd == 1)
{
USB->SWVBUS |= 0x01 << 2;
}
else
{
USB->SWVBUS &= ~(0x01 << 2);
}
return;
}
/**
* @brief Set session valid threshold.
* @param None.
* @retval Status: 0, indicates that lower than the session valid threshold
Status: 1, indicates that higher than the session valid threshold.
*/
uint8_t ald_usb_swvbus_sesvalth_get(void)
{
return (USB->SWVBUS & (0x01 << 2));
}
/**
* @brief Set VBUS valid threshold.
* @param thd: 0, indicates that lower than the vbus valid threshold
thd: 1, indicates that higher than the vbus valid threshold.
* @retval Status.
*/
void ald_usb_swvbus_valth_set(uint8_t thd)
{
if (thd == 1)
{
USB->SWVBUS |= 0x01 << 3;
}
else
{
USB->SWVBUS &= ~(0x01 << 3);
}
return;
}
/**
* @brief Set VBUS valid threshold.
* @param None.
* @retval Status: 0, indicates that lower than the vbus valid threshold
Status:thd: 1, indicates that higher than the vbus valid threshold.
*/
uint8_t ald_usb_swvbus_valth_get(void)
{
return (USB->SWVBUS & (0x01 << 3));
}
/**
* @}
*/
/** @defgroup USB_Public_Functions_Group11 components initialization functions
* @brief USB components initialization
* @{
*/
/**
* @brief Initialize usb host components.
* @retval None
*/
void ald_usb_host_components_init(void)
{
WRITE_REG(SYSCFG->PROT, 0x55AA6996U);
RMU->AHB1RSTR |= 0x40;
RMU->AHB1RSTR &= 0xFFFFFFBF;
WRITE_REG(SYSCFG->PROT, 0x0);
/* Config EP0 */
ald_usb_host_ep_config(ALD_USB_EP_0, 64, 0, 0, (ALD_USB_EP_MODE_CTRL | ALD_USB_EP_SPEED_FULL | ALD_USB_EP_HOST_OUT));
/* Clear interrupts */
/* Enable PHY power */
ald_usb_host_pwr_enable();
/* clear hnp session */
ald_usb_otg_session_request(false);
/* set vbus control mode and threshold value */
ald_usb_swvbus_sigctl_set(1);
ald_usb_swvbus_sesendth_set(1);
ald_usb_swvbus_sesvalth_set(1);
ald_usb_swvbus_valth_set(1);
/* Pull down DP and DM */
ald_usb_dppud_set(ALD_USB_DPDM_PUSH_DOWN);
ald_usb_dmpud_set(ALD_USB_DPDM_PUSH_DOWN);
/* software control CID */
ald_usb_swcid_cidctrl(1);
/* force to host mode */
ald_usb_swcid_host(0);
/* start host request */
ald_usb_mode_host_req();
/* Start hnp */
ald_usb_otg_session_request(true);
/* Clear interrupts */
ald_usb_int_status_get();
/* Init interrupts */
ald_usb_int_enable(ALD_USB_INTCTRL_SESSION | ALD_USB_INTCTRL_DISCONNECT | ALD_USB_INTCTRL_CONNECT | ALD_USB_INTCTRL_SOF |
ALD_USB_INTCTRL_BABBLE | ALD_USB_INTCTRL_RESUME);
ald_usb_int_enable_ep(ALD_USB_INTEP_ALL);
ald_usb_int_register();
return;
}
/**
* @brief Initialize usb device components.
* @retval None
*/
void ald_usb_device_components_init(void)
{
ald_usb_otg_session_request(true);
#if (defined(ES32VF2264))
ald_usb_dppud_set(ALD_USB_DPDM_FLOATING);/*测试板的USBDP(PA12),上拉到3.3V。具体情况请参考ReadMe.txt*/
#else
ald_usb_dppud_set(USB_DPDM_PUSH_UP);
#endif /* ES32VF2264 */
/* software control CID */
ald_usb_swcid_cidctrl(1);
/* force to dev mode */
ald_usb_swcid_host(1);
ald_usb_dev_suspend_enable();
return;
}
/**
* @}
*/
/**
* @}
*/
#endif /* ALD_USB */
/**
* @}
*/
/**
* @}
*/
|