2025-04-05 11:42 PM
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);
2025-04-06 12:57 AM
To check its no problem with cache, just switch off all caches, then test again. (+ MPU not used.)
2025-04-06 8:25 AM
The problem seems to be something else and not a cache problem. I have several buffers/structs defined in the external SDRAM, and during the bulk download, I notice that the values in another buffer change/corrupt! SCB_EnableDCache() is commented out, and SCB_CleanDCache_by_Addr() and SCB_InvalidateDCache_by_Addr() are not called.
When writing data to:
static FILE_SYS_BUFFER _bulkdwldBuff __attribute__((section(".buff_Section")));
Sometimes, change values in:
CAN_STRUCT CAN_Struct __attribute__((section(".buff_Section")));
2025-04-06 9:29 AM
For me just the " Sometimes " sounds suspicious : could be a hardware related problem, so sometimes wrong address on sdram ...?
Is it custom board ?
+ could you play with sdram timing , just to check, if its timing is a problem ?
2025-04-06 7:48 PM
I don't think it is an address on sdram because it fails at different points during the downloading of the 1.6MB file. Yes, it is a custom board, and it had some issues with the power supply stability. I was able to fix the power supply issue by using an inductor with a different value. I want to complete as much of the firmware as possible before designing another circuit board.
I don't think it is a board issue because I am using the same SDRAM for the display frame buffer, and I don't have any issues with it.