Skip to content

Commit d073592

Browse files
committed
feature(dcd_dwc2): Added cache synchronization
1 parent ead08bd commit d073592

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/portable/synopsys/dwc2/dcd_dwc2.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,20 @@
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
5061
static 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

5265
typedef 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
}

src/portable/synopsys/dwc2/dwc2_esp32.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
#include "soc/periph_defs.h"
4040
#include "soc/usb_wrap_struct.h"
4141

42+
#if (CFG_TUD_DWC2_DMA_ENABLE && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE)
43+
#include "sdkconfig.h"
44+
#include "esp_cache.h"
45+
#include "esp_log.h"
46+
47+
#define DWC2_MEM_CACHE_LINE_SIZE CONFIG_CACHE_L1_CACHE_LINE_SIZE
48+
#endif // SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
49+
4250
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
4351
#define DWC2_FS_REG_BASE 0x60080000UL
4452
#define DWC2_EP_MAX 7
@@ -111,6 +119,25 @@ TU_ATTR_ALWAYS_INLINE static inline void dwc2_phy_update(dwc2_regs_t* dwc2, uint
111119
// maybe usb_utmi_hal_disable()
112120
}
113121

122+
#if (CFG_TUD_DWC2_DMA_ENABLE && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE)
123+
void dcd_dcache_clean(void const* addr, uint32_t data_size) {
124+
int flags = ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED;
125+
if (addr != NULL && data_size) {
126+
esp_err_t ret = esp_cache_msync((void *) addr, data_size, flags);
127+
assert(ret == ESP_OK);
128+
}
129+
}
130+
131+
void dcd_dcache_invalidate(void const* addr, uint32_t data_size) {
132+
int flags = ESP_CACHE_MSYNC_FLAG_DIR_M2C;
133+
if (addr != NULL && data_size) {
134+
data_size = (data_size < DWC2_MEM_CACHE_LINE_SIZE)? DWC2_MEM_CACHE_LINE_SIZE : data_size;
135+
esp_err_t ret = esp_cache_msync((void *) addr, data_size, flags);
136+
assert(ret == ESP_OK);
137+
}
138+
}
139+
#endif // CFG_TUD_DWC2_DMA_ENABLE && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
140+
114141
#ifdef __cplusplus
115142
}
116143
#endif

0 commit comments

Comments
 (0)