Skip to content

Commit 1799d4a

Browse files
committed
feature(dcd_dwc2): Added macro to cover the cache operation if they are not implemented
1 parent 3644459 commit 1799d4a

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

src/portable/synopsys/dwc2/dcd_dwc2.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ static bool _sof_en;
105105
//--------------------------------------------------------------------
106106
// DMA
107107
//--------------------------------------------------------------------
108+
// When DMA requires cache synchronization for memory
109+
// Data synchronization: cache to memory
110+
#ifndef dsync_c2m
111+
#define dsync_c2m(_addr, _size)
112+
#endif // dsync_c2m
113+
114+
// Data synchronization: memory to cache
115+
#ifndef dsync_m2c
116+
#define dsync_m2c(_addr, _size)
117+
#endif // dsync_m2c
108118

109119
TU_ATTR_ALWAYS_INLINE static inline bool dma_enabled(const dwc2_regs_t* dwc2) {
110120
#if !CFG_TUD_DWC2_DMA
@@ -467,9 +477,7 @@ static void edpt_schedule_packets(uint8_t rhport, uint8_t const epnum, uint8_t c
467477

468478
if(dma_enabled(dwc2)) {
469479
dep->diepdma = (uintptr_t)xfer->buffer;
470-
if (total_bytes != 0) {
471-
dwc2_dcd_sync_cache_to_memory(xfer->buffer, total_bytes);
472-
}
480+
dsync_c2m(xfer->buffer, total_bytes);
473481
// For ISO endpoint set correct odd/even bit for next frame.
474482
if ((dep->diepctl & DIEPCTL_EPTYP) == DIEPCTL_EPTYP_0 && (XFER_CTL_BASE(epnum, dir))->interval == 1) {
475483
// Take odd/even bit from frame counter.
@@ -507,9 +515,7 @@ static void edpt_schedule_packets(uint8_t rhport, uint8_t const epnum, uint8_t c
507515

508516
if(dma_enabled(dwc2)) {
509517
dep->doepdma = (uintptr_t)xfer->buffer;
510-
if (total_bytes != 0) {
511-
dwc2_dcd_sync_cache_to_memory(xfer->buffer, total_bytes);
512-
}
518+
dsync_c2m(xfer->buffer, total_bytes);
513519
}
514520

515521
dep->doepctl |= DOEPCTL_EPENA | DOEPCTL_CNAK;
@@ -1055,7 +1061,7 @@ static void handle_epout_irq(uint8_t rhport) {
10551061

10561062
if(dma_enabled(dwc2)) {
10571063
dma_setup_prepare(rhport);
1058-
dwc2_dcd_sync_memory_to_cache((uint8_t*) _setup_packet, sizeof(_setup_packet));
1064+
dsync_m2c((uint8_t*) _setup_packet, sizeof(_setup_packet));
10591065
}
10601066

10611067
dcd_event_setup_received(rhport, (uint8_t*) _setup_packet, true);
@@ -1082,9 +1088,7 @@ static void handle_epout_irq(uint8_t rhport) {
10821088
if(epnum == 0 && xfer->total_len == 0) {
10831089
dma_setup_prepare(rhport);
10841090
}
1085-
if (xfer->total_len) {
1086-
dwc2_dcd_sync_memory_to_cache(xfer->buffer, xfer->total_len);
1087-
}
1091+
dsync_m2c(xfer->buffer, xfer->total_len);
10881092
dcd_event_xfer_complete(rhport, epnum, xfer->total_len, XFER_RESULT_SUCCESS, true);
10891093
}
10901094
} else {

src/portable/synopsys/dwc2/dwc2_esp32.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,37 @@ TU_ATTR_ALWAYS_INLINE static inline void dwc2_dcd_sync_cache_to_memory(void *add
109109
#if DWC2_ENABLE_MEM_CACHE
110110
ESP_EARLY_LOGV("dwc2_esp32", "cache to mem sync, addr 0x%"PRIx32", size %d", (uintptr_t)addr, size);
111111
int flags = ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED;
112-
esp_err_t ret = esp_cache_msync(addr, size, flags);
113-
assert(ret == ESP_OK);
112+
if (addr != NULL && size) {
113+
esp_err_t ret = esp_cache_msync(addr, size, flags);
114+
assert(ret == ESP_OK);
115+
}
114116
#else
115-
(void)addr;
116-
(void)size;
117+
(void) addr;
118+
(void) size;
117119
// nothing to do
118120
#endif // DWC2_ENABLE_MEM_CACHE
119121
}
120122

121123
TU_ATTR_ALWAYS_INLINE static inline void dwc2_dcd_sync_memory_to_cache(void *addr, size_t size) {
122124
#if DWC2_ENABLE_MEM_CACHE
123-
int flags = ESP_CACHE_MSYNC_FLAG_DIR_M2C;
124125
ESP_EARLY_LOGV("dwc2", "mem to cache sync, addr 0x%"PRIx32", size %d", (uintptr_t)addr, size);
125-
// TODO: size should be multiply of CONFIG_CACHE_L1_CACHE_LINE_SIZE?
126-
size = (size < CONFIG_CACHE_L1_CACHE_LINE_SIZE)? CONFIG_CACHE_L1_CACHE_LINE_SIZE : size;
127-
esp_err_t ret = esp_cache_msync(addr, size, flags);
128-
assert(ret == ESP_OK);
126+
int flags = ESP_CACHE_MSYNC_FLAG_DIR_M2C;
127+
if (addr != NULL && size) {
128+
// TODO: size should be multiply of CONFIG_CACHE_L1_CACHE_LINE_SIZE?
129+
size = (size < CONFIG_CACHE_L1_CACHE_LINE_SIZE)? CONFIG_CACHE_L1_CACHE_LINE_SIZE : size;
130+
esp_err_t ret = esp_cache_msync(addr, size, flags);
131+
assert(ret == ESP_OK);
132+
}
129133
#else
130-
(void)addr;
131-
(void)size;
134+
(void) addr;
135+
(void) size;
132136
// nothing to do
133137
#endif // DWC2_ENABLE_MEM_CACHE
134138
}
135139

140+
#define dsync_c2m(_addr, _size) dwc2_dcd_sync_cache_to_memory((_addr), (_size))
141+
#define dsync_m2c(_addr, _size) dwc2_dcd_sync_memory_to_cache((_addr), (_size))
142+
136143
#ifdef __cplusplus
137144
}
138145
#endif

0 commit comments

Comments
 (0)