4747// MACRO TYPEDEF CONSTANT ENUM
4848//--------------------------------------------------------------------+
4949
50+ #ifdef DWC2_ENABLE_MEM_CACHE
51+
52+ #ifndef DWC2_MEM_CACHE_LINE_SIZE
53+ #warning "Cache line size not specified, use default(64b) instead"
54+ # define DWC2_MEM_CACHE_LINE_SIZE 0x40
55+ #endif // DWC2_MEM_CACHE_LINE_SIZE
56+
57+ CFG_TUD_MEM_SECTION struct {
58+ union {
59+ uint32_t data [2 ];
60+ uint8_t buffer [DWC2_MEM_CACHE_LINE_SIZE ];
61+ };
62+ } _cache_aligned_setup_packet ;
63+
64+ #define _setup_packet _cache_aligned_setup_packet.data
65+ #define _sizeof_setup_packet () DWC2_MEM_CACHE_LINE_SIZE
66+ #else
5067static CFG_TUD_MEM_SECTION TU_ATTR_ALIGNED (4 ) uint32_t _setup_packet [2 ];
68+ #define _sizeof_setup_packet ( ) sizeof (_setup_packet )
69+ #endif // DWC2_ENABLE_MEM_CACHE
70+
71+ // When DMA requires cache synchronization for memory
72+ // Data synchronization: cache to memory
73+ #ifndef dsync_c2m
74+ #define dsync_c2m (_addr , _size )
75+ #endif // dsync_c2m
76+
77+ // Data synchronization: memory to cache
78+ #ifndef dsync_m2c
79+ #define dsync_m2c (_addr , _size )
80+ #endif // dsync_m2c
5181
5282typedef struct {
5383 uint8_t * buffer ;
@@ -348,6 +378,11 @@ static void edpt_schedule_packets(uint8_t rhport, const uint8_t epnum, const uin
348378
349379 const bool is_dma = dma_device_enabled (dwc2 );
350380 if (is_dma ) {
381+ if (dir == TUSB_DIR_IN && total_bytes != 0 ) {
382+ // CACHE HINT
383+ // The xfer->buffer has new data for Host, move it to memory for DMA to transfer it
384+ dsync_c2m (xfer -> buffer , total_bytes );
385+ }
351386 dep -> diepdma = (uintptr_t ) xfer -> buffer ;
352387 }
353388
@@ -848,6 +883,11 @@ static void handle_epout_dma(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepi
848883
849884 if (doepint_bm .setup_phase_done ) {
850885 dma_setup_prepare (rhport );
886+ // CACHE HINT
887+ // When cache is enabled, _setup_packet must have cache line size alignment
888+ // and there should be no valuable data in memory after.
889+ // Thus, specific struct is used as a buffer for setup packet data
890+ dsync_m2c ((uint8_t * ) _setup_packet , _sizeof_setup_packet ());
851891 dcd_event_setup_received (rhport , (uint8_t * ) _setup_packet , true);
852892 return ;
853893 }
@@ -873,7 +913,9 @@ static void handle_epout_dma(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepi
873913 if (epnum == 0 && xfer -> total_len == 0 ) {
874914 dma_setup_prepare (rhport );
875915 }
876-
916+ // CACHE HINT
917+ // Some data has been received by DMA, fetch the data from memory to cache
918+ dsync_m2c (xfer -> buffer , xfer -> total_len );
877919 dcd_event_xfer_complete (rhport , epnum , xfer -> total_len , XFER_RESULT_SUCCESS , true);
878920 }
879921 }
0 commit comments