Skip to content

Commit cdaa19a

Browse files
committed
feature(dcd_dwc2): Covered _setup_packet with alignment, added cache hints comments
1 parent ff4cf1a commit cdaa19a

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/portable/synopsys/dwc2/dcd_dwc2.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8297
static 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

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

src/portable/synopsys/dwc2/dwc2_esp32.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#if TU_CHECK_MCU(OPT_MCU_ESP32P4)
4343
#if (CFG_TUD_DWC2_DMA && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE)
44+
#include "sdkconfig.h"
4445
#include "hal/cache_hal.h"
4546
#include "esp_cache.h"
4647
#include "esp_log.h"
@@ -121,41 +122,36 @@ TU_ATTR_ALWAYS_INLINE static inline void dwc2_phy_update(dwc2_regs_t* dwc2, uint
121122
// maybe usb_utmi_hal_disable()
122123
}
123124

125+
#if DWC2_ENABLE_MEM_CACHE
124126
// MCU specific cache synchronization call
125127
TU_ATTR_ALWAYS_INLINE static inline void dwc2_dcd_sync_cache_to_memory(void *addr, size_t size) {
126-
#if DWC2_ENABLE_MEM_CACHE
127-
ESP_EARLY_LOGV("dwc2_esp32", "cache to mem sync, addr 0x%"PRIx32", size %d", (uintptr_t)addr, size);
128128
int flags = ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED;
129129
if (addr != NULL && size) {
130+
ESP_EARLY_LOGV("dwc2_esp32", "cache to mem sync, addr 0x%"PRIx32", size %d", (uintptr_t)addr, size);
130131
esp_err_t ret = esp_cache_msync(addr, size, flags);
131132
assert(ret == ESP_OK);
132133
}
133-
#else
134-
(void) addr;
135-
(void) size;
136-
// nothing to do
137-
#endif // DWC2_ENABLE_MEM_CACHE
138134
}
139135

140136
TU_ATTR_ALWAYS_INLINE static inline void dwc2_dcd_sync_memory_to_cache(void *addr, size_t size) {
141-
#if DWC2_ENABLE_MEM_CACHE
142-
ESP_EARLY_LOGV("dwc2", "mem to cache sync, addr 0x%"PRIx32", size %d", (uintptr_t)addr, size);
143137
int flags = ESP_CACHE_MSYNC_FLAG_DIR_M2C;
144138
if (addr != NULL && size) {
139+
ESP_EARLY_LOGV("dwc2", "mem to cache sync, addr 0x%"PRIx32", size %d (%s)",
140+
(uintptr_t)addr,
141+
size,
142+
(size % CONFIG_CACHE_L1_CACHE_LINE_SIZE)? "not aligned" : "aligned");
145143
// TODO: size should be multiply of CONFIG_CACHE_L1_CACHE_LINE_SIZE?
146144
size = (size < CONFIG_CACHE_L1_CACHE_LINE_SIZE)? CONFIG_CACHE_L1_CACHE_LINE_SIZE : size;
147145
esp_err_t ret = esp_cache_msync(addr, size, flags);
148146
assert(ret == ESP_OK);
149147
}
150-
#else
151-
(void) addr;
152-
(void) size;
153-
// nothing to do
154-
#endif // DWC2_ENABLE_MEM_CACHE
155148
}
156149

157150
#define dsync_c2m(_addr, _size) dwc2_dcd_sync_cache_to_memory((_addr), (_size))
158151
#define dsync_m2c(_addr, _size) dwc2_dcd_sync_memory_to_cache((_addr), (_size))
152+
#define DWC2_MEM_CACHE_LINE_SIZE CONFIG_CACHE_L1_CACHE_LINE_SIZE
153+
154+
#endif // DWC2_ENABLE_MEM_CACHE
159155

160156
#ifdef __cplusplus
161157
}

0 commit comments

Comments
 (0)