Skip to content

Commit 163d2d0

Browse files
pennamfacchinm
authored andcommitted
whd: opta: fix dcache maintenance
1 parent 90bc47e commit 163d2d0

File tree

1 file changed

+39
-16
lines changed
  • targets/TARGET_STM/TARGET_STM32H7/TARGET_STM32H747xI/TARGET_OPTA/COMPONENT_WHD/port

1 file changed

+39
-16
lines changed

targets/TARGET_STM/TARGET_STM32H7/TARGET_STM32H747xI/TARGET_OPTA/COMPONENT_WHD/port/cyhal_sdio.c

+39-16
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@
6363
#define LINK_MTU 1024
6464
#define MAX(a,b) (a>b)?a:b
6565

66+
/* D-cache maintenance for DMA buffers */
67+
#if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
68+
#define _CYHAL_DCACHE_MAINTENANCE
69+
#define _CYHAL_DMA_BUFFER_ALIGN_BYTES (32u)
70+
#else
71+
#define _CYHAL_DMA_BUFFER_ALIGN_BYTES (4u)
72+
#endif /* defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) */
73+
74+
/* Macro to ALIGN */
75+
#if defined (__ARMCC_VERSION) /* ARM Compiler */
76+
#define ALIGN_HAL_COMMON(buf, x) __align(x) buf
77+
#elif defined (__GNUC__) /* GNU Compiler */
78+
#define ALIGN_HAL_COMMON(buf, x) buf __attribute__ ((aligned (x)))
79+
#elif defined (__ICCARM__) /* IAR Compiler */
80+
#define ALIGN_HAL_COMMON(buf, x) __ALIGNED(x) buf
81+
#endif
82+
83+
/* Macro to get variable aligned for cache maintenance purpose */
84+
#define CYHAL_ALIGN_DMA_BUFFER(arg) ALIGN_HAL_COMMON(arg, _CYHAL_DMA_BUFFER_ALIGN_BYTES)
85+
86+
6687
extern pinconfig_t PinConfig[];
6788
extern SD_HandleTypeDef hsd;
6889

@@ -74,7 +95,7 @@ static uint32_t dctrl;
7495
static whd_driver_t whd_handler;
7596
static cyhal_sdio_irq_handler_t sdio_irq_handler;
7697

77-
static uint8_t temp_dma_buffer[2048] __attribute__((aligned(8)));
98+
CYHAL_ALIGN_DMA_BUFFER(static uint8_t temp_dma_buffer[2048]);
7899
static uint8_t *user_data;
79100
static uint32_t user_data_size;
80101
static uint8_t *dma_data_source;
@@ -150,22 +171,24 @@ static void sdio_prepare_data_transfer(cyhal_transfer_t direction, uint32_t bloc
150171
dma_transfer_size = (uint32_t)(((data_size + (uint16_t) block_size - 1) / (uint16_t) block_size) * (uint16_t) block_size);
151172

152173
if (direction == CYHAL_WRITE) {
153-
154-
#if !(defined(DUAL_CORE) && defined(CORE_CM4))
155-
SCB_CleanDCache_by_Addr((uint32_t *)dma_data_source, data_size + 32);
156-
#endif
157174
memcpy(temp_dma_buffer, data, data_size);
158175
dma_data_source = temp_dma_buffer;
159176
} else {
160177
dma_data_source = (uint8_t *)temp_dma_buffer;
161-
//VIKR
162-
//memset(dma_data_source,0x12,data_size);
178+
}
163179

164-
#if !(defined(DUAL_CORE) && defined(CORE_CM4))
180+
#ifdef _CYHAL_DCACHE_MAINTENANCE
181+
if (direction == CYHAL_WRITE)
182+
{
183+
SCB_CleanDCache_by_Addr((uint32_t*)dma_data_source, block_size * dma_transfer_size);
184+
}
185+
else
186+
{
165187
/* Cache-Invalidate the output from DMA */
166-
SCB_CleanDCache_by_Addr((uint32_t *)dma_data_source, data_size + 32);
167-
#endif
188+
SCB_InvalidateDCache_by_Addr((uint32_t*)dma_data_source,
189+
data_size + __SCB_DCACHE_LINE_SIZE);
168190
}
191+
#endif
169192

170193
SDIO->DTIMER = (uint32_t) 0xFFFFFFFF;
171194
SDIO->DLEN = dma_transfer_size;
@@ -252,12 +275,6 @@ cy_rslt_t cyhal_sdio_init(cyhal_sdio_t *obj, cyhal_gpio_t cmd, cyhal_gpio_t clk,
252275
/* Enable the SDIO Clock */
253276
__HAL_RCC_SDMMC1_CLK_ENABLE();
254277

255-
#if !(defined(DUAL_CORE) && defined(CORE_CM4))
256-
/* Disable DCache for STM32H7 family */
257-
SCB_CleanDCache();
258-
SCB_DisableDCache();
259-
#endif
260-
261278
WPRINT_WHD_DEBUG(("in init: %p\n", sdio_transfer_finished_semaphore));
262279

263280
// Lower speed configuration
@@ -437,6 +454,12 @@ cy_rslt_t cyhal_sdio_bulk_transfer(cyhal_sdio_t *obj, cyhal_transfer_t direction
437454
}
438455

439456
if (direction == CYHAL_READ) {
457+
#ifdef _CYHAL_DCACHE_MAINTENANCE
458+
SCB_CleanInvalidateDCache_by_Addr(
459+
(uint32_t*)((uint32_t)dma_data_source & ~(__SCB_DCACHE_LINE_SIZE - 1U)),
460+
user_data_size + __SCB_DCACHE_LINE_SIZE);
461+
#endif /* if defined(_CYHAL_DCACHE_MAINTENANCE) */
462+
440463
memcpy(user_data, dma_data_source, (size_t) user_data_size);
441464
}
442465

0 commit comments

Comments
 (0)