@@ -78,8 +78,25 @@ TU_ATTR_ALWAYS_INLINE static inline dwc2_regs_t* DWC2_REG(uint8_t rhport) {
7878//--------------------------------------------------------------------+
7979// MACRO TYPEDEF CONSTANT ENUM
8080//--------------------------------------------------------------------+
81-
81+ #if DWC2_ENABLE_MEM_CACHE
82+ #ifndef DWC2_MEM_CACHE_LINE_SIZE
83+ #warning "Cache line size not specified, use default(64b) instead"
84+ # define DWC2_MEM_CACHE_LINE_SIZE 0x40
85+ #endif // DWC2_MEM_CACHE_LINE_SIZE
86+
87+ CFG_TUD_MEM_SECTION struct {
88+ union {
89+ uint32_t data [2 ];
90+ uint8_t buffer [DWC2_MEM_CACHE_LINE_SIZE ];
91+ };
92+ } _cache_aligned_setup_packet ;
93+
94+ #define _setup_packet _cache_aligned_setup_packet.data
95+ #define _sizeof_setup_packet () DWC2_MEM_CACHE_LINE_SIZE
96+ #else
8297static CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED (4 ) uint32_t _setup_packet [2 ];
98+ #define _sizeof_setup_packet ( ) sizeof (_setup_packet )
99+ #endif // DWC2_ENABLE_MEM_CACHE
83100
84101typedef struct {
85102 uint8_t * buffer ;
@@ -477,6 +494,8 @@ static void edpt_schedule_packets(uint8_t rhport, uint8_t const epnum, uint8_t c
477494
478495 if (dma_enabled (dwc2 )) {
479496 dep -> diepdma = (uintptr_t )xfer -> buffer ;
497+ // CACHE HINT
498+ // The xfer->buffer has new data, move it to memory for DMA transfer it
480499 dsync_c2m (xfer -> buffer , total_bytes );
481500 // For ISO endpoint set correct odd/even bit for next frame.
482501 if ((dep -> diepctl & DIEPCTL_EPTYP ) == DIEPCTL_EPTYP_0 && (XFER_CTL_BASE (epnum , dir ))-> interval == 1 ) {
@@ -1057,7 +1076,11 @@ static void handle_epout_irq(uint8_t rhport) {
10571076
10581077 if (dma_enabled (dwc2 )) {
10591078 dma_setup_prepare (rhport );
1060- dsync_m2c ((uint8_t * ) _setup_packet , sizeof (_setup_packet ));
1079+ // CACHE HINT
1080+ // When cache is enabled, _setup_packet must have cache line size alignment
1081+ // and there should be no valuable data in memory after.
1082+ // Thus, specific struct is used as a buffer for setup packet data
1083+ dsync_m2c ((uint8_t * ) _setup_packet , _sizeof_setup_packet ());
10611084 }
10621085
10631086 dcd_event_setup_received (rhport , (uint8_t * ) _setup_packet , true);
@@ -1084,6 +1107,8 @@ static void handle_epout_irq(uint8_t rhport) {
10841107 if (epnum == 0 && xfer -> total_len == 0 ) {
10851108 dma_setup_prepare (rhport );
10861109 }
1110+ // CACHE HINT
1111+ // Some data has been received by DMA, fetch the data from memory to cache
10871112 dsync_m2c (xfer -> buffer , xfer -> total_len );
10881113 dcd_event_xfer_complete (rhport , epnum , xfer -> total_len , XFER_RESULT_SUCCESS , true);
10891114 }
0 commit comments