4747// MACRO TYPEDEF CONSTANT ENUM
4848//--------------------------------------------------------------------+
4949
50+ #ifdef DWC2_MEM_CACHE_LINE_SIZE
51+ CFG_TUD_MEM_SECTION struct {
52+ union {
53+ uint32_t data [2 ];
54+ uint8_t buffer [DWC2_MEM_CACHE_LINE_SIZE ];
55+ };
56+ } _cache_aligned_setup_packet ;
57+
58+ #define _setup_packet _cache_aligned_setup_packet.data
59+ #define _sizeof_setup_packet () DWC2_MEM_CACHE_LINE_SIZE
60+ #else
5061static CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED (4 ) uint32_t _setup_packet [2 ];
62+ #define _sizeof_setup_packet ( ) sizeof (_setup_packet )
63+ #endif // DWC2_MEM_CACHE_LINE_SIZE
5164
5265typedef struct {
5366 uint8_t * buffer ;
@@ -348,6 +361,11 @@ static void edpt_schedule_packets(uint8_t rhport, const uint8_t epnum, const uin
348361
349362 const bool is_dma = dma_device_enabled (dwc2 );
350363 if (is_dma ) {
364+ if (dir == TUSB_DIR_IN && total_bytes != 0 ) {
365+ // CACHE HINT
366+ // The xfer->buffer has new data for Host, move it to memory for DMA to transfer it
367+ dcd_dcache_clean (xfer -> buffer , total_bytes );
368+ }
351369 dep -> diepdma = (uintptr_t ) xfer -> buffer ;
352370 }
353371
@@ -848,6 +866,11 @@ static void handle_epout_dma(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepi
848866
849867 if (doepint_bm .setup_phase_done ) {
850868 dma_setup_prepare (rhport );
869+ // CACHE HINT
870+ // When cache is enabled, _setup_packet must have cache line size alignment
871+ // and there should be no valuable data in memory after.
872+ // Thus, specific struct is used as a buffer for setup packet data
873+ dcd_dcache_invalidate ((uint8_t * ) _setup_packet , _sizeof_setup_packet ());
851874 dcd_event_setup_received (rhport , (uint8_t * ) _setup_packet , true);
852875 return ;
853876 }
@@ -873,7 +896,9 @@ static void handle_epout_dma(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepi
873896 if (epnum == 0 && xfer -> total_len == 0 ) {
874897 dma_setup_prepare (rhport );
875898 }
876-
899+ // CACHE HINT
900+ // Some data has been received by DMA, fetch the data from memory to cache
901+ dcd_dcache_invalidate (xfer -> buffer , xfer -> total_len );
877902 dcd_event_xfer_complete (rhport , epnum , xfer -> total_len , XFER_RESULT_SUCCESS , true);
878903 }
879904 }
0 commit comments