Skip to main content
Visitor II
April 6, 2025
Question

SDRAM + SD-Card intermittent write failure.

  • April 6, 2025
  • 2 replies
  • 837 views

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);

 

 

 

 

    This topic has been closed for replies.

    2 replies

    Super User
    April 6, 2025

    To check its no problem with cache, just switch off all caches, then test again. (+ MPU not used.)

     

     

    cleonb322Author
    Visitor II
    April 6, 2025

    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")));

    Super User
    April 6, 2025

    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 ?

    Graduate II
    October 22, 2025