FATFS f_open fails when data cache is enabled
When enabled Data cache for example in main:
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();I think I am supposed to uncomment L63 in sd_diskio.c to activate Cache maintenance:
/* USER CODE BEGIN enableSDDmaCacheMaintenance */
#define ENABLE_SD_DMA_CACHE_MAINTENANCE 1
/* USER CODE END enableSDDmaCacheMaintenance */That way the following code is activated after reading from the SD card via DMA:
#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)
/*
the SCB_InvalidateDCache_by_Addr() requires a 32-Byte aligned address,
adjust the address and the D-Cache size to invalidate accordingly.
*/
alignedAddr = (uint32_t)buff & ~0x1F;
SCB_InvalidateDCache_by_Addr((uint32_t*)alignedAddr, count*BLOCKSIZE + ((uint32_t)buff - alignedAddr));I found that through the FATFS my 32 Byte aligned buffer is not anymore aligned when it reaches that function in sd_diskio.c. Some Bytes will be probably used by the FATFS. Those leading bytes are overwritten by the Cache invalidate function.
Therefore f_open call is failing.
I could fix this behavior by adding the two lines:
alignedAddr = (uint32_t)buff & ~0x1F;
SCB_CleanDCache_by_Addr((uint32_t*)alignedAddr, ((uint32_t)buff - alignedAddr));right before the DMA setup function in sd_diskio.c around L216:
if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,
(uint32_t) (sector),
count) == MSD_OK)I did not check yet, maybe it needs to be added elsewhere.
Side note: I lately discoverd another problem with the same function see "Read From SD Card gets stuck in a While-Loop"
