SDRAM + SD-Card intermittent write failure.
I am having issues when writing a buffer that is declared in the external buffer to an SD card. When I tried to download a bulk file (1.5MB), write failure (f_write returns FR_DISK_ERR) would happen at least once during the download process, but if I declared the buffer in the internal RAM, I didn't have the problem.
Example of my linker:
/* Memories definition */
MEMORY
{
RAM (rw) : ORIGIN = 0x20000000, LENGTH = 499K /*512K*/
FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 2048K - 0x8000
SDRAM (rw) : ORIGIN = 0xC0000000, LENGTH = 16M
Memory_B1 (rw) : ORIGIN = 0x2007C000, LENGTH = 0xA0
Memory_B2 (rw) : ORIGIN = 0x2007C0A0, LENGTH = 0xA0
QUADSPI (rw) : ORIGIN = 0x90000000, LENGTH = 16M
}
....
.buff_sec (NOLOAD) :
{
. = ABSOLUTE(0xC0144100);
*(.buff_Section)
} > SDRAM
//code example - buffer definitions
__attribute__((aligned(4))) static FILE_SYS_BUFFER _bulkdwldBuff __attribute__((section(".buff_Section")));
If I remove "__attribute__((section(".buff_Section")))" from the declaration, it works 100% of the time. i tried using "SCB_CleanDCache_by_Addr((uint32_t *)aligned_addr, cache_size);" before f_write but it doesn't help the problem.
I even tried setting up MPU regions for the SDRAM:
MPU_InitStruct.Number = MPU_REGION_NUMBER5;
MPU_InitStruct.BaseAddress = 0xC0000000; // Start of SDRAM
MPU_InitStruct.Size = MPU_REGION_SIZE_16MB; // Adjust to your SDRAM size (ensure correct size)
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.IsCacheable = 0; // Key! Make non-cacheable
MPU_InitStruct.IsBufferable = 0; // Key! Make non-bufferable
MPU_InitStruct.IsShareable = 1; // Optional based on usage
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.SubRegionDisable = 0x00;
MPU_InitStruct.DisableExec = 1; // Disable execution in SDRAM region
HAL_MPU_ConfigRegion(&MPU_InitStruct);
