41
41
#include "device/dcd.h"
42
42
#include "dwc2_common.h"
43
43
44
- #if TU_CHECK_MCU (OPT_MCU_GD32VF103 )
45
- #define DWC2_EP_COUNT (_dwc2 ) DWC2_EP_MAX
46
- #else
47
- #define DWC2_EP_COUNT (_dwc2 ) ((_dwc2)->ghwcfg2_bm.num_dev_ep + 1)
48
- #endif
49
-
50
44
//--------------------------------------------------------------------+
51
45
// MACRO TYPEDEF CONSTANT ENUM
52
46
//--------------------------------------------------------------------+
@@ -79,6 +73,16 @@ CFG_TUD_MEM_SECTION static struct {
79
73
TUD_EPBUF_DEF (setup_packet , 8 );
80
74
} _dcd_usbbuf ;
81
75
76
+ TU_ATTR_ALWAYS_INLINE static inline uint8_t dwc2_ep_count (const dwc2_regs_t * dwc2 ) {
77
+ #if TU_CHECK_MCU (OPT_MCU_GD32VF103 )
78
+ return DWC2_EP_MAX ;
79
+ #else
80
+ const dwc2_ghwcfg2_t ghwcfg2 = {.value = dwc2 -> ghwcfg2 };
81
+ return ghwcfg2 .num_dev_ep + 1 ;
82
+ #endif
83
+ }
84
+
85
+
82
86
//--------------------------------------------------------------------
83
87
// DMA
84
88
//--------------------------------------------------------------------
@@ -102,7 +106,8 @@ bool dcd_dcache_clean_invalidate(const void* addr, uint32_t data_size) {
102
106
TU_ATTR_ALWAYS_INLINE static inline bool dma_device_enabled (const dwc2_regs_t * dwc2 ) {
103
107
(void ) dwc2 ;
104
108
// Internal DMA only
105
- return CFG_TUD_DWC2_DMA_ENABLE && dwc2 -> ghwcfg2_bm .arch == GHWCFG2_ARCH_INTERNAL_DMA ;
109
+ const dwc2_ghwcfg2_t ghwcfg2 = {.value = dwc2 -> ghwcfg2 };
110
+ return CFG_TUD_DWC2_DMA_ENABLE && ghwcfg2 .arch == GHWCFG2_ARCH_INTERNAL_DMA ;
106
111
}
107
112
108
113
static void dma_setup_prepare (uint8_t rhport ) {
@@ -261,20 +266,15 @@ static void edpt_activate(uint8_t rhport, const tusb_desc_endpoint_t* p_endpoint
261
266
xfer -> interval = p_endpoint_desc -> bInterval ;
262
267
263
268
// Endpoint control
264
- union {
265
- uint32_t value ;
266
- dwc2_depctl_t bm ;
267
- } depctl ;
268
- depctl .value = 0 ;
269
-
270
- depctl .bm .mps = xfer -> max_size ;
271
- depctl .bm .active = 1 ;
272
- depctl .bm .type = p_endpoint_desc -> bmAttributes .xfer ;
269
+ dwc2_depctl_t depctl = {.value = 0 };
270
+ depctl .mps = xfer -> max_size ;
271
+ depctl .active = 1 ;
272
+ depctl .type = p_endpoint_desc -> bmAttributes .xfer ;
273
273
if (p_endpoint_desc -> bmAttributes .xfer != TUSB_XFER_ISOCHRONOUS ) {
274
- depctl .bm . set_data0_iso_even = 1 ;
274
+ depctl .set_data0_iso_even = 1 ;
275
275
}
276
276
if (dir == TUSB_DIR_IN ) {
277
- //depctl.bm. tx_fifo_num = epnum;
277
+ //depctl.tx_fifo_num = epnum;
278
278
uint8_t fifo_num = epnum ;
279
279
#if TU_CHECK_MCU (OPT_MCU_ESP32S2 , OPT_MCU_ESP32S3 )
280
280
// Special Case for EP5, which is used by CDC but not actually called by the driver
@@ -285,7 +285,7 @@ static void edpt_activate(uint8_t rhport, const tusb_desc_endpoint_t* p_endpoint
285
285
fifo_num = get_free_fifo ();
286
286
}
287
287
#endif
288
- depctl .bm . tx_fifo_num = fifo_num ;
288
+ depctl .tx_fifo_num = fifo_num ;
289
289
}
290
290
291
291
dwc2_dep_t * dep = & dwc2 -> ep [dir == TUSB_DIR_IN ? 0 : 1 ][epnum ];
@@ -365,31 +365,22 @@ static void edpt_schedule_packets(uint8_t rhport, const uint8_t epnum, const uin
365
365
}
366
366
367
367
// transfer size: A full OUT transfer (multiple packets, possibly) triggers XFRC.
368
- union {
369
- uint32_t value ;
370
- dwc2_ep_tsize_t bm ;
371
- } deptsiz ;
372
- deptsiz .value = 0 ;
373
- deptsiz .bm .xfer_size = total_bytes ;
374
- deptsiz .bm .packet_count = num_packets ;
375
-
368
+ dwc2_ep_tsize_t deptsiz = {.value = 0 };
369
+ deptsiz .xfer_size = total_bytes ;
370
+ deptsiz .packet_count = num_packets ;
376
371
dep -> tsiz = deptsiz .value ;
377
372
378
373
// control
379
- union {
380
- dwc2_depctl_t bm ;
381
- uint32_t value ;
382
- } depctl ;
383
- depctl .value = dep -> ctl ;
384
-
385
- depctl .bm .clear_nak = 1 ;
386
- depctl .bm .enable = 1 ;
387
- if (depctl .bm .type == DEPCTL_EPTYPE_ISOCHRONOUS && xfer -> interval == 1 ) {
388
- const uint32_t odd_now = (dwc2 -> dsts_bm .frame_number & 1u );
374
+ dwc2_depctl_t depctl = {.value = dep -> ctl };
375
+ depctl .clear_nak = 1 ;
376
+ depctl .enable = 1 ;
377
+ if (depctl .type == DEPCTL_EPTYPE_ISOCHRONOUS && xfer -> interval == 1 ) {
378
+ const dwc2_dsts_t dsts = {.value = dwc2 -> dsts };
379
+ const uint32_t odd_now = dsts .frame_number & 1u ;
389
380
if (odd_now ) {
390
- depctl .bm . set_data0_iso_even = 1 ;
381
+ depctl .set_data0_iso_even = 1 ;
391
382
} else {
392
- depctl .bm . set_data1_iso_odd = 1 ;
383
+ depctl .set_data1_iso_odd = 1 ;
393
384
}
394
385
}
395
386
@@ -432,7 +423,8 @@ bool dcd_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
432
423
433
424
// XCVRDLY: transceiver delay between xcvr_sel and txvalid during device chirp is required
434
425
// when using with some PHYs such as USB334x (USB3341, USB3343, USB3346, USB3347)
435
- if (dwc2 -> ghwcfg2_bm .hs_phy_type == GHWCFG2_HSPHY_ULPI ) {
426
+ const dwc2_ghwcfg2_t ghwcfg2 = {.value = dwc2 -> ghwcfg2 };
427
+ if (ghwcfg2 .hs_phy_type == GHWCFG2_HSPHY_ULPI ) {
436
428
dcfg |= DCFG_XCVRDLY ;
437
429
}
438
430
} else {
@@ -667,7 +659,7 @@ void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) {
667
659
// 7.4.1 Initialization on USB Reset
668
660
static void handle_bus_reset (uint8_t rhport ) {
669
661
dwc2_regs_t * dwc2 = DWC2_REG (rhport );
670
- const uint8_t ep_count = DWC2_EP_COUNT (dwc2 );
662
+ const uint8_t ep_count = dwc2_ep_count (dwc2 );
671
663
672
664
tu_memclr (xfer_status , sizeof (xfer_status ));
673
665
@@ -697,7 +689,9 @@ static void handle_bus_reset(uint8_t rhport) {
697
689
dfifo_device_init (rhport );
698
690
699
691
// 5. Reset device address
700
- dwc2 -> dcfg_bm .address = 0 ;
692
+ dwc2_dcfg_t dcfg = {.value = dwc2 -> dcfg };
693
+ dcfg .address = 0 ;
694
+ dwc2 -> dcfg = dcfg .value ;
701
695
702
696
// Fixed both control EP0 size to 64 bytes
703
697
dwc2 -> epin [0 ].ctl &= ~(0x03 << DIEPCTL_MPSIZ_Pos );
@@ -717,8 +711,9 @@ static void handle_bus_reset(uint8_t rhport) {
717
711
718
712
static void handle_enum_done (uint8_t rhport ) {
719
713
dwc2_regs_t * dwc2 = DWC2_REG (rhport );
714
+ const dwc2_dsts_t dsts = {.value = dwc2 -> dsts };
720
715
tusb_speed_t speed ;
721
- switch (dwc2 -> dsts_bm .enum_speed ) {
716
+ switch (dsts .enum_speed ) {
722
717
case DCFG_SPEED_HIGH :
723
718
speed = TUSB_SPEED_HIGH ;
724
719
break ;
@@ -763,12 +758,12 @@ static void handle_rxflvl_irq(uint8_t rhport) {
763
758
const volatile uint32_t * rx_fifo = dwc2 -> fifo [0 ];
764
759
765
760
// Pop control word off FIFO
766
- const dwc2_grxstsp_t grxstsp_bm = dwc2 -> grxstsp_bm ;
767
- const uint8_t epnum = grxstsp_bm .ep_ch_num ;
761
+ const dwc2_grxstsp_t grxstsp = {. value = dwc2 -> grxstsp } ;
762
+ const uint8_t epnum = grxstsp .ep_ch_num ;
768
763
769
764
dwc2_dep_t * epout = & dwc2 -> epout [epnum ];
770
765
771
- switch (grxstsp_bm .packet_status ) {
766
+ switch (grxstsp .packet_status ) {
772
767
case GRXSTS_PKTSTS_GLOBAL_OUT_NAK :
773
768
// Global OUT NAK: do nothing
774
769
break ;
@@ -790,7 +785,7 @@ static void handle_rxflvl_irq(uint8_t rhport) {
790
785
791
786
case GRXSTS_PKTSTS_RX_DATA : {
792
787
// Out packet received
793
- const uint16_t byte_count = grxstsp_bm .byte_count ;
788
+ const uint16_t byte_count = grxstsp .byte_count ;
794
789
xfer_ctl_t * xfer = XFER_CTL_BASE (epnum , TUSB_DIR_OUT );
795
790
796
791
if (byte_count ) {
@@ -804,7 +799,8 @@ static void handle_rxflvl_irq(uint8_t rhport) {
804
799
805
800
// short packet, minus remaining bytes (xfer_size)
806
801
if (byte_count < xfer -> max_size ) {
807
- xfer -> total_len -= epout -> tsiz_bm .xfer_size ;
802
+ const dwc2_ep_tsize_t tsiz = {.value = epout -> tsiz };
803
+ xfer -> total_len -= tsiz .xfer_size ;
808
804
if (epnum == 0 ) {
809
805
xfer -> total_len -= _dcd_data .ep0_pending [TUSB_DIR_OUT ];
810
806
_dcd_data .ep0_pending [TUSB_DIR_OUT ] = 0 ;
@@ -866,11 +862,13 @@ static void handle_epin_slave(uint8_t rhport, uint8_t epnum, dwc2_diepint_t diep
866
862
// - 64 bytes or
867
863
// - Half/Empty of TX FIFO size (configured by GAHBCFG.TXFELVL)
868
864
if (diepint_bm .txfifo_empty && (dwc2 -> diepempmsk & (1 << epnum ))) {
869
- const uint16_t remain_packets = epin -> tsiz_bm .packet_count ;
865
+ dwc2_ep_tsize_t tsiz = {.value = epin -> tsiz };
866
+ const uint16_t remain_packets = tsiz .packet_count ;
870
867
871
868
// Process every single packet (only whole packets can be written to fifo)
872
869
for (uint16_t i = 0 ; i < remain_packets ; i ++ ) {
873
- const uint16_t remain_bytes = (uint16_t ) epin -> tsiz_bm .xfer_size ;
870
+ tsiz .value = epin -> tsiz ;
871
+ const uint16_t remain_bytes = (uint16_t ) tsiz .xfer_size ;
874
872
const uint16_t xact_bytes = tu_min16 (remain_bytes , xfer -> max_size );
875
873
876
874
// Check if dtxfsts has enough space available
@@ -889,7 +887,8 @@ static void handle_epin_slave(uint8_t rhport, uint8_t epnum, dwc2_diepint_t diep
889
887
}
890
888
891
889
// Turn off TXFE if all bytes are written.
892
- if (epin -> tsiz_bm .xfer_size == 0 ) {
890
+ tsiz .value = epin -> tsiz ;
891
+ if (tsiz .xfer_size == 0 ) {
893
892
dwc2 -> diepempmsk &= ~(1 << epnum );
894
893
}
895
894
}
@@ -920,7 +919,8 @@ static void handle_epout_dma(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepi
920
919
xfer_ctl_t * xfer = XFER_CTL_BASE (epnum , TUSB_DIR_OUT );
921
920
922
921
// determine actual received bytes
923
- const uint16_t remain = epout -> tsiz_bm .xfer_size ;
922
+ const dwc2_ep_tsize_t tsiz = {.value = epout -> tsiz };
923
+ const uint16_t remain = tsiz .xfer_size ;
924
924
xfer -> total_len -= remain ;
925
925
926
926
// this is ZLP, so prepare EP0 for next setup
@@ -956,7 +956,7 @@ static void handle_epin_dma(uint8_t rhport, uint8_t epnum, dwc2_diepint_t diepin
956
956
static void handle_ep_irq (uint8_t rhport , uint8_t dir ) {
957
957
dwc2_regs_t * dwc2 = DWC2_REG (rhport );
958
958
const bool is_dma = dma_device_enabled (dwc2 );
959
- const uint8_t ep_count = DWC2_EP_COUNT (dwc2 );
959
+ const uint8_t ep_count = dwc2_ep_count (dwc2 );
960
960
const uint8_t daint_offset = (dir == TUSB_DIR_IN ) ? DAINT_IEPINT_Pos : DAINT_OEPINT_Pos ;
961
961
dwc2_dep_t * ep_base = & dwc2 -> ep [dir == TUSB_DIR_IN ? 0 : 1 ][0 ];
962
962
0 commit comments