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
|
/**
**************************************************************************
* @file at32f415_usb.c
* @version v2.0.5
* @date 2022-05-20
* @brief contains all the functions for the usb firmware library
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
#include "at32f415_conf.h"
/** @addtogroup AT32F415_periph_driver
* @{
*/
/** @defgroup USB
* @brief USB driver modules
* @{
*/
#ifdef USB_MODULE_ENABLED
/** @defgroup USB_private_functions
* @{
*/
#ifdef OTGFS_USB_GLOBAL
/**
* @brief usb global core soft reset
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval error status
*/
error_status usb_global_reset(otg_global_type *usbx)
{
uint32_t timeout = 0;
while(usbx->grstctl_bit.ahbidle == RESET)
{
if(timeout ++ > 200000)
{
break;
}
}
timeout = 0;
usbx->grstctl_bit.csftrst = TRUE;
while(usbx->grstctl_bit.csftrst == SET)
{
if(timeout ++ > 200000)
{
break;
}
}
return SUCCESS;
}
/**
* @brief usb global initialization
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_global_init(otg_global_type *usbx)
{
/* reset otg moudle */
usb_global_reset(usbx);
/* exit power down mode */
usbx->gccfg_bit.pwrdown = TRUE;
usbx->gccfg_bit.avalidsesen = TRUE;
usbx->gccfg_bit.bvalidsesen = TRUE;
}
/**
* @brief usb global select usb core (otg1 or otg2).
* @param usb_id: select otg1 or otg2
* this parameter can be one of the following values:
* - USB_OTG1_ID
* - USB_OTG2_ID
* @retval usb global register type pointer
*/
otg_global_type *usb_global_select_core(uint8_t usb_id)
{
/* use otg1 */
return OTG1_GLOBAL;
}
/**
* @brief flush tx fifo
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param fifo_num: tx fifo num,when fifo_num=16,flush all tx fifo
* parameter as following values: 0-16
* @retval none
*/
void usb_flush_tx_fifo(otg_global_type *usbx, uint32_t fifo_num)
{
uint32_t timeout = 0;
/* set flush fifo number */
usbx->grstctl_bit.txfnum = fifo_num;
/* start flush fifo */
usbx->grstctl_bit.txfflsh = TRUE;
while(usbx->grstctl_bit.txfflsh == TRUE)
{
if(timeout ++ > 200000)
{
break;
}
}
}
/**
* @brief flush rx fifo
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_flush_rx_fifo(otg_global_type *usbx)
{
uint32_t timeout = 0;
usbx->grstctl_bit.rxfflsh = TRUE;
while(usbx->grstctl_bit.rxfflsh == TRUE)
{
if(timeout ++ > 200000)
{
break;
}
}
}
/**
* @brief usb interrupt mask enable
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param interrupt:
* this parameter can be any combination of the following values:
* - USB_OTG_MODEMIS_INT
* - USB_OTG_OTGINT_INT
* - USB_OTG_SOF_INT
* - USB_OTG_RXFLVL_INT
* - USB_OTG_NPTXFEMP_INT
* - USB_OTG_GINNAKEFF_INT
* - USB_OTG_GOUTNAKEFF_INT
* - USB_OTG_ERLYSUSP_INT
* - USB_OTG_USBSUSP_INT
* - USB_OTG_USBRST_INT
* - USB_OTG_ENUMDONE_INT
* - USB_OTG_ISOOUTDROP_INT
* - USB_OTG_IEPT_INT
* - USB_OTG_OEPT_INT
* - USB_OTG_INCOMISOIN_INT
* - USB_OTG_INCOMPIP_INCOMPISOOUT_INT
* - USB_OTG_PRT_INT
* - USB_OTG_HCH_INT
* - USB_OTG_PTXFEMP_INT
* - USB_OTG_CONIDSCHG_INT
* - USB_OTG_DISCON_INT
* - USB_OTG_WKUP_INT
* @param new_state: TRUE or FALSE
* @retval none
*/
void usb_global_interrupt_enable(otg_global_type *usbx, uint16_t interrupt, confirm_state new_state)
{
if(new_state == TRUE)
{
usbx->gintmsk |= interrupt;
}
else
{
usbx->gintmsk &= ~interrupt;
}
}
/**
* @brief get all global core interrupt flag
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval intterupt flag
*/
uint32_t usb_global_get_all_interrupt(otg_global_type *usbx)
{
uint32_t intsts = usbx->gintsts;
return intsts & usbx->gintmsk;
}
/**
* @brief clear the global interrupt flag
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param flag: interrupt flag
* this parameter can be any combination of the following values:
* - USB_OTG_MODEMIS_FLAG
* - USB_OTG_OTGINT_FLAG
* - USB_OTG_SOF_FLAG
* - USB_OTG_RXFLVL_FLAG
* - USB_OTG_NPTXFEMP_FLAG
* - USB_OTG_GINNAKEFF_FLAG
* - USB_OTG_GOUTNAKEFF_FLAG
* - USB_OTG_ERLYSUSP_FLAG
* - USB_OTG_USBSUSP_FLAG
* - USB_OTG_USBRST_FLAG
* - USB_OTG_ENUMDONE_FLAG
* - USB_OTG_ISOOUTDROP_FLAG
* - USB_OTG_EOPF_FLAG
* - USB_OTG_IEPT_FLAG
* - USB_OTG_OEPT_FLAG
* - USB_OTG_INCOMISOIN_FLAG
* - USB_OTG_INCOMPIP_INCOMPISOOUT_FLAG
* - USB_OTG_PRT_FLAG
* - USB_OTG_HCH_FLAG
* - USB_OTG_PTXFEMP_FLAG
* - USB_OTG_CONIDSCHG_FLAG
* - USB_OTG_DISCON_FLAG
* - USB_OTG_WKUP_FLAG
* @retval none
*/
void usb_global_clear_interrupt(otg_global_type *usbx, uint32_t flag)
{
usbx->gintsts = flag;
}
/**
* @brief usb global interrupt enable
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* OTG1_GLOBAL , OTG2_GLOBAL
* @retval none
*/
void usb_interrupt_enable(otg_global_type *usbx)
{
usbx->gahbcfg_bit.glbintmsk = TRUE;
}
/**
* @brief usb global interrupt disable
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_interrupt_disable(otg_global_type *usbx)
{
usbx->gahbcfg_bit.glbintmsk = FALSE;
}
/**
* @brief usb set rx fifo size
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param size: rx fifo size
* @retval none
*/
void usb_set_rx_fifo(otg_global_type *usbx, uint16_t size)
{
usbx->grxfsiz = size;
}
/**
* @brief usb set tx fifo size
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param txfifo: the fifo number
* @param size: tx fifo size
* @retval none
*/
void usb_set_tx_fifo(otg_global_type *usbx, uint8_t txfifo, uint16_t size)
{
uint8_t i_index = 0;
uint32_t offset = 0;
offset = usbx->grxfsiz;
if(txfifo == 0)
{
usbx->gnptxfsiz_ept0tx = offset | (size << 16);
}
else
{
offset += usbx->gnptxfsiz_ept0tx_bit.nptxfdep;
for(i_index = 0; i_index < (txfifo - 1); i_index ++)
{
offset += usbx->dieptxfn_bit[i_index].ineptxfdep;
}
usbx->dieptxfn[txfifo - 1] = offset | (size << 16);
}
}
/**
* @brief set otg mode(device or host mode)
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param mode:
this parameter can be one of the following values:
* - OTG_DEVICE_MODE
* - OTG_HOST_MODE
* - OTG_DRD_MODE
* @retval none
*/
void usb_global_set_mode(otg_global_type *usbx, uint32_t mode)
{
/* set otg to device mode */
if(mode == OTG_DEVICE_MODE)
{
usbx->gusbcfg_bit.fhstmode = FALSE;
usbx->gusbcfg_bit.fdevmode = TRUE;
}
/* set otg to host mode */
if(mode == OTG_HOST_MODE)
{
usbx->gusbcfg_bit.fdevmode = FALSE;
usbx->gusbcfg_bit.fhstmode = TRUE;
}
/* set otg to default mode */
if(mode == OTG_DRD_MODE)
{
usbx->gusbcfg_bit.fdevmode = FALSE;
usbx->gusbcfg_bit.fhstmode = FALSE;
}
}
/**
* @brief disable the transceiver power down mode
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_global_power_on(otg_global_type *usbx)
{
/* core soft reset */
usbx->grstctl_bit.csftrst = TRUE;
while(usbx->grstctl_bit.csftrst);
/* disable power down mode */
usbx->gccfg_bit.pwrdown = TRUE;
}
/**
* @brief usb stop phy clock
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_stop_phy_clk(otg_global_type *usbx)
{
OTG_PCGCCTL(usbx)->pcgcctl_bit.stoppclk = TRUE;
}
/**
* @brief usb open phy clock
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_open_phy_clk(otg_global_type *usbx)
{
OTG_PCGCCTL(usbx)->pcgcctl_bit.stoppclk = FALSE;
}
/**
* @brief write data from user memory to usb buffer
* @param pusr_buf: point to user buffer
* @param offset_addr: endpoint tx offset address
* @param nbytes: number of bytes data write to usb buffer
* @retval none
*/
void usb_write_packet(otg_global_type *usbx, uint8_t *pusr_buf, uint16_t num, uint16_t nbytes)
{
uint32_t n_index;
uint32_t nhbytes = (nbytes + 3) / 4;
uint32_t *pbuf = (uint32_t *)pusr_buf;
for(n_index = 0; n_index < nhbytes; n_index ++)
{
#if defined (__ICCARM__) && (__VER__ < 7000000)
USB_FIFO(usbx, num) = *(__packed uint32_t *)pbuf;
#else
USB_FIFO(usbx, num) = __UNALIGNED_UINT32_READ(pbuf);
#endif
pbuf ++;
}
}
/**
* @brief read data from usb buffer to user buffer
* @param pusr_buf: point to user buffer
* @param offset_addr: endpoint rx offset address
* @param nbytes: number of bytes data write to usb buffer
* @retval none
*/
void usb_read_packet(otg_global_type *usbx, uint8_t *pusr_buf, uint16_t num, uint16_t nbytes)
{
uint32_t n_index;
uint32_t nhbytes = (nbytes + 3) / 4;
uint32_t *pbuf = (uint32_t *)pusr_buf;
for(n_index = 0; n_index < nhbytes; n_index ++)
{
#if defined (__ICCARM__) && (__VER__ < 7000000)
*(__packed uint32_t *)pbuf = USB_FIFO(usbx, 0);
#else
__UNALIGNED_UINT32_WRITE(pbuf, (USB_FIFO(usbx, 0)));
#endif
pbuf ++;
}
}
#endif
#ifdef OTGFS_USB_DEVICE
/**
* @brief open usb endpoint
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param ept_info: endpoint information structure
* @retval none
*/
void usb_ept_open(otg_global_type *usbx, usb_ept_info *ept_info)
{
uint8_t mps = USB_EPT0_MPS_64;
if(ept_info->eptn == USB_EPT0)
{
if(ept_info->maxpacket == 0x40)
{
mps = USB_EPT0_MPS_64;
}
else if(ept_info->maxpacket == 0x20)
{
mps = USB_EPT0_MPS_32;
}
else if(ept_info->maxpacket == 0x10)
{
mps = USB_EPT0_MPS_16;
}
else if(ept_info->maxpacket == 0x08)
{
mps = USB_EPT0_MPS_8;
}
}
/* endpoint direction is in */
if(ept_info->inout == EPT_DIR_IN)
{
OTG_DEVICE(usbx)->daintmsk |= 1 << ept_info->eptn;
if(ept_info->eptn == USB_EPT0)
{
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.mps = mps;
}
else
{
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.mps = ept_info->maxpacket;
}
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.eptype = ept_info->trans_type;
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.txfnum = ept_info->eptn;
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.setd0pid = TRUE;
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.usbacept = TRUE;
}
/* endpoint direction is out */
else
{
OTG_DEVICE(usbx)->daintmsk |= (1 << ept_info->eptn) << 16;
if(ept_info->eptn == USB_EPT0)
{
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.mps = mps;
}
else
{
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.mps = ept_info->maxpacket;
}
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.eptype = ept_info->trans_type;
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.setd0pid = TRUE;
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.usbacept = TRUE;
}
}
/**
* @brief close usb endpoint
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param ept_info: endpoint information structure
* @retval none
*/
void usb_ept_close(otg_global_type *usbx, usb_ept_info *ept_info)
{
if(ept_info->inout == EPT_DIR_IN)
{
OTG_DEVICE(usbx)->daintmsk &= ~(1 << ept_info->eptn);
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.usbacept = FALSE;
}
else
{
OTG_DEVICE(usbx)->daintmsk &= ~((1 << ept_info->eptn) << 16);
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.usbacept = FALSE;
}
}
/**
* @brief set endpoint tx or rx status to stall
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param ept_info: endpoint information structure
* @retval none
*/
void usb_ept_stall(otg_global_type *usbx, usb_ept_info *ept_info)
{
if(ept_info->inout == EPT_DIR_IN)
{
if(USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.eptena == RESET)
{
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.eptdis = FALSE;
}
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.stall = SET;
}
else
{
if(USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.eptena == RESET)
{
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.eptdis = FALSE;
}
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.stall = TRUE;
}
}
/**
* @brief clear endpoint tx or rx status to stall
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param ept_info: endpoint information structure
* @retval none
*/
void usb_ept_clear_stall(otg_global_type *usbx, usb_ept_info *ept_info)
{
if(ept_info->inout == EPT_DIR_IN)
{
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.stall = FALSE;
if(ept_info->trans_type == EPT_INT_TYPE || ept_info->trans_type == EPT_BULK_TYPE)
{
USB_INEPT(usbx, ept_info->eptn)->diepctl_bit.setd0pid = TRUE;
}
}
else
{
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.stall = FALSE;
if(ept_info->trans_type == EPT_INT_TYPE || ept_info->trans_type == EPT_BULK_TYPE)
{
USB_OUTEPT(usbx, ept_info->eptn)->doepctl_bit.setd0pid = TRUE;
}
}
}
/**
* @brief get all out endpoint interrupt bits
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval out endpoint interrupt bits
*/
uint32_t usb_get_all_out_interrupt(otg_global_type *usbx)
{
uint32_t intsts = OTG_DEVICE(usbx)->daint;
return ((intsts & (OTG_DEVICE(usbx)->daintmsk)) >> 16);
}
/**
* @brief get all in endpoint interrupt bits
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval in endpoint interrupt bits
*/
uint32_t usb_get_all_in_interrupt(otg_global_type *usbx)
{
uint32_t intsts = OTG_DEVICE(usbx)->daint;
return ((intsts & (OTG_DEVICE(usbx)->daintmsk)) & 0xFFFF);
}
/**
* @brief get out endpoint interrupt flag
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param eptn: endpoint number
* @retval out endpoint interrupt flags
*/
uint32_t usb_ept_out_interrupt(otg_global_type *usbx, uint32_t eptn)
{
uint32_t intsts = USB_OUTEPT(usbx, eptn)->doepint;
return (intsts & (OTG_DEVICE(usbx)->doepmsk));
}
/**
* @brief get in endpoint interrupt flag
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param eptn: endpoint number
* @retval in endpoint intterupt flags
*/
uint32_t usb_ept_in_interrupt(otg_global_type *usbx, uint32_t eptn)
{
uint32_t intsts, mask1, mask2;
mask1 = OTG_DEVICE(usbx)->diepmsk;
mask2 = OTG_DEVICE(usbx)->diepempmsk;
mask1 |= ((mask2 >> eptn) & 0x1) << 7;
intsts = USB_INEPT(usbx, eptn)->diepint & mask1;
return intsts;
}
/**
* @brief clear out endpoint interrupt flag
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param eptn: endpoint number
* @retval flag: interrupt flag
* this parameter can be any combination of the following values:
* - USB_OTG_DOEPINT_XFERC_FLAG
* - USB_OTG_DOEPINT_EPTDISD_FLAG
* - USB_OTG_DOEPINT_SETUP_FLAG
* - USB_OTG_DOEPINT_OTEPDIS_FLAG
* - USB_OTG_DOEPINT_B2BSTUP_FLAG
*/
void usb_ept_out_clear(otg_global_type *usbx, uint32_t eptn, uint32_t flag)
{
USB_OUTEPT(usbx, eptn)->doepint = flag;
}
/**
* @brief clear in endpoint interrupt flag
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param eptn: endpoint number
* @retval flag: interrupt flag
* this parameter can be any combination of the following values:
* - USB_OTG_DIEPINT_XFERC_FLAG
* - USB_OTG_DIEPINT_EPTDISD_FLAG
* - USB_OTG_DIEPINT_TMROC_FLAG
* - USB_OTG_DIEPINT_INTTXFE_FLAG
* - USB_OTG_DIEPINT_INEPNE_FLAG
* - USB_OTG_DIEPINT_TXFE_FLAG
*/
void usb_ept_in_clear(otg_global_type *usbx, uint32_t eptn, uint32_t flag)
{
USB_INEPT(usbx, eptn)->diepint = flag;
}
/**
* @brief set the host assignment address
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param address: host assignment address
* @retval none
*/
void usb_set_address(otg_global_type *usbx, uint8_t address)
{
OTG_DEVICE(usbx)->dcfg_bit.devaddr = address;
}
/**
* @brief enable endpoint 0 out
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_ept0_start(otg_global_type *usbx)
{
otg_eptout_type *usb_outept = USB_OUTEPT(usbx, 0);
usb_outept->doeptsiz = 0;
usb_outept->doeptsiz_bit.pktcnt = 1;
usb_outept->doeptsiz_bit.xfersize = 24;
usb_outept->doeptsiz_bit.rxdpid_setupcnt = 3;
}
/**
* @brief endpoint 0 start setup
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_ept0_setup(otg_global_type *usbx)
{
USB_INEPT(usbx, 0)->diepctl_bit.mps = 0;
OTG_DEVICE(usbx)->dctl_bit.cgnpinak = FALSE;
}
/**
* @brief connect usb device by enable pull-up
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_connect(otg_global_type *usbx)
{
/* D+ 1.5k pull-up enable */
OTG_DEVICE(usbx)->dctl_bit.sftdiscon = FALSE;
}
/**
* @brief disconnect usb device by disable pull-up
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_disconnect(otg_global_type *usbx)
{
/* D+ 1.5k pull-up disable */
OTG_DEVICE(usbx)->dctl_bit.sftdiscon = TRUE;
}
/**
* @brief usb remote wakeup set
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_remote_wkup_set(otg_global_type *usbx)
{
OTG_DEVICE(usbx)->dctl_bit.rwkupsig = TRUE;
}
/**
* @brief usb remote wakeup clear
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_remote_wkup_clear(otg_global_type *usbx)
{
OTG_DEVICE(usbx)->dctl_bit.rwkupsig = FALSE;
}
/**
* @brief usb suspend status get
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval usb suspend status
*/
uint8_t usb_suspend_status_get(otg_global_type *usbx)
{
return OTG_DEVICE(usbx)->dsts_bit.suspsts;
}
#endif
#ifdef OTGFS_USB_HOST
/**
* @brief usb port power on
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param state: state (TRUE or FALSE)
* @retval none
*/
void usb_port_power_on(otg_global_type *usbx, confirm_state state)
{
otg_host_type *usb_host = OTG_HOST(usbx);
uint32_t hprt_val = usb_host->hprt;
hprt_val &= ~(USB_OTG_HPRT_PRTENA | USB_OTG_HPRT_PRTENCHNG |
USB_OTG_HPRT_PRTOVRCACT | USB_OTG_HPRT_PRTCONDET);
if(state == TRUE)
{
usb_host->hprt = hprt_val | USB_OTG_HPRT_PRTPWR;
}
else
{
usb_host->hprt = hprt_val & (~USB_OTG_HPRT_PRTPWR);
}
}
/**
* @brief get current frame number
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
uint32_t usbh_get_frame(otg_global_type *usbx)
{
otg_host_type *usb_host = OTG_HOST(usbx);
return usb_host->hfnum & 0xFFFF;
}
/**
* @brief enable one host channel
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param chn: host channel number
* @param ept_num: devvice endpoint number
* @param dev_address: device address
* @param type: channel transfer type
* this parameter can be one of the following values:
* - EPT_CONTROL_TYPE
* - EPT_BULK_TYPE
* - EPT_INT_TYPE
* - EPT_ISO_TYPE
* @param maxpacket: support max packe size for this channel
* @param speed: device speed
* this parameter can be one of the following values:
* - USB_PRTSPD_FULL_SPEED
* - USB_PRTSPD_LOW_SPEED
* @retval none
*/
void usb_hc_enable(otg_global_type *usbx,
uint8_t chn,
uint8_t ept_num,
uint8_t dev_address,
uint8_t type,
uint16_t maxpacket,
uint8_t speed)
{
otg_hchannel_type *hch = USB_CHL(usbx, chn);
otg_host_type *usb_host = OTG_HOST(usbx);
switch(type)
{
case EPT_CONTROL_TYPE:
case EPT_BULK_TYPE:
hch->hcintmsk |= USB_OTG_HC_XFERCM_INT | USB_OTG_HC_STALLM_INT |
USB_OTG_HC_XACTERRM_INT | USB_OTG_HC_NAKM_INT |
USB_OTG_HC_DTGLERRM_INT;
if(ept_num & 0x80)
{
hch->hcintmsk_bit.bblerrmsk = TRUE;
}
break;
case EPT_INT_TYPE:
hch->hcintmsk |= USB_OTG_HC_XFERCM_INT | USB_OTG_HC_STALLM_INT |
USB_OTG_HC_XACTERRM_INT | USB_OTG_HC_NAKM_INT |
USB_OTG_HC_DTGLERRM_INT | USB_OTG_HC_FRMOVRRUN_INT;
break;
case EPT_ISO_TYPE:
hch->hcintmsk |= USB_OTG_HC_XFERCM_INT | USB_OTG_HC_ACKM_INT |
USB_OTG_HC_FRMOVRRUN_INT;
break;
}
usb_host->haintmsk |= 1 << chn;
usbx->gintmsk_bit.hchintmsk = TRUE;
hch->hcchar_bit.devaddr = dev_address;
hch->hcchar_bit.eptnum = ept_num & 0x7F;
hch->hcchar_bit.eptdir = (ept_num & 0x80)?1:0;
hch->hcchar_bit.lspddev = (speed == USB_PRTSPD_LOW_SPEED)?1:0;
hch->hcchar_bit.eptype = type;
hch->hcchar_bit.mps = maxpacket;
if(type == EPT_INT_TYPE)
{
hch->hcchar_bit.oddfrm = TRUE;
}
}
/**
* @brief host read channel interrupt
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval interrupt flag
*/
uint32_t usb_hch_read_interrupt(otg_global_type *usbx)
{
otg_host_type *usb_host = OTG_HOST(usbx);
return usb_host->haint & 0xFFFF;
}
/**
* @brief disable host
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @retval none
*/
void usb_host_disable(otg_global_type *usbx)
{
uint32_t i_index = 0, count = 0;
otg_hchannel_type *hch;
otg_host_type *usb_host = OTG_HOST(usbx);
usbx->gahbcfg_bit.glbintmsk = FALSE;
usb_flush_rx_fifo(usbx);
usb_flush_tx_fifo(usbx, 0x10);
for(i_index = 0; i_index < 16; i_index ++)
{
hch = USB_CHL(usbx, i_index);
hch->hcchar_bit.chdis = TRUE;
hch->hcchar_bit.chena = FALSE;
hch->hcchar_bit.eptdir = 0;
}
for(i_index = 0; i_index < 16; i_index ++)
{
hch = USB_CHL(usbx, i_index);
hch->hcchar_bit.chdis = TRUE;
hch->hcchar_bit.chena = TRUE;
hch->hcchar_bit.eptdir = 0;
do
{
if(count ++ > 1000)
break;
}while(hch->hcchar_bit.chena);
}
usb_host->haint = 0xFFFFFFFF;
usbx->gintsts = 0xFFFFFFFF;
usbx->gahbcfg_bit.glbintmsk = TRUE;
}
/**
* @brief halt channel
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param chn: channel number
* @retval none
*/
void usb_hch_halt(otg_global_type *usbx, uint8_t chn)
{
uint32_t count = 0;
otg_hchannel_type *usb_chh = USB_CHL(usbx, chn);
otg_host_type *usb_host = OTG_HOST(usbx);
/* endpoint type is control or bulk */
if(usb_chh->hcchar_bit.eptype == EPT_CONTROL_TYPE ||
usb_chh->hcchar_bit.eptype == EPT_BULK_TYPE)
{
usb_chh->hcchar_bit.chdis = TRUE;
if((usbx->gnptxsts & 0xFFFF) == 0)
{
usb_chh->hcchar_bit.chena = FALSE;
usb_chh->hcchar_bit.chena = TRUE;
usb_chh->hcchar_bit.eptdir = 0;
do
{
if(count ++ > 1000)
break;
}while(usb_chh->hcchar_bit.chena == SET);
}
else
{
usb_chh->hcchar_bit.chena = TRUE;
}
}
else
{
usb_chh->hcchar_bit.chdis = TRUE;
if((usb_host->hptxsts & 0xFFFF) == 0)
{
usb_chh->hcchar_bit.chena = FALSE;
usb_chh->hcchar_bit.chena = TRUE;
usb_chh->hcchar_bit.eptdir = 0;
do
{
if(count ++ > 1000)
break;
}while(usb_chh->hcchar_bit.chena == SET);
}
else
{
usb_chh->hcchar_bit.chena = TRUE;
}
}
}
/**
* @brief select full or low speed clock
* @param usbx: to select the otgfs peripheral.
* this parameter can be one of the following values:
* - OTG1_GLOBAL
* - OTG2_GLOBAL
* @param clk: clock frequency
* @retval none
*/
void usbh_fsls_clksel(otg_global_type *usbx, uint8_t clk)
{
otg_host_type *usb_host = OTG_HOST(usbx);
usb_host->hcfg_bit.fslspclksel = clk;
if(clk == USB_HCFG_CLK_6M)
{
usb_host->hfir = 6000;
}
else
{
usb_host->hfir = 48000;
}
}
#endif
/**
* @}
*/
#endif
/**
* @}
*/
/**
* @}
*/
|