SD card taking time to store data
Hi everyone,
I'm using stm32F407VG discovery board and STM32Cube_FW_F4_V1.24.2 fimware for my project. I've included freertos and FATFS in my project. I've created 3 tasks, higher priority task(running at 100Hz) which job is to do some calculation and store data in SD card and rest 2 tasks(50Hz, 25Hz) are running perfectly and finished in 600-700 microseconds.
I'm writing 182 bytes (in structure format) of data in SD card but some time while writing it is taking more time than usual, it get stuck in a function for longer time but storing data perfectly. I don't know the reason behind this.
I've found one issue in github which link is given below-
https://github.com/STMicroelectronics/STM32CubeF4/issues/2
Below is the code snippet as per the above issue in the sd_diskio.c file -
if(BSP_SD_WriteBlocks_DMA((uint32_t*)buff,
(uint32_t) (sector),
count) == MSD_OK)
{
/* Get the message from the queue */
event = osMessageGet(SDQueueID, SD_TIMEOUT);
if (event.status == osEventMessage)
{
if (event.value.v == WRITE_CPLT_MSG)
{
timer = osKernelSysTick() + SD_TIMEOUT;
/* block until SDIO IP is ready or a timeout occur */
while(timer > osKernelSysTick())
{
if (BSP_SD_GetCardState() == SD_TRANSFER_OK)
{
res = RES_OK;
break;
}
}
}
}
}
#if defined(ENABLE_SCRATCH_BUFFER)
} else
{
/* Slow path, fetch each sector a part and memcpy to destination buffer */
int i;
uint8_t ret;
#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)
/*
* invalidate the scratch buffer before the next write to get the actual data instead of the cached one
*/
SCB_InvalidateDCache_by_Addr((uint32_t*)scratch, BLOCKSIZE);
#endif
for (i = 0; i < count; i++) {
ret = BSP_SD_WriteBlocks_DMA((uint32_t*)scratch, (uint32_t)sector++, 1);
if (ret == MSD_OK) {
/* wait for a message from the queue or a timeout */
event = osMessageGet(SDQueueID, SD_TIMEOUT);
if (event.status == osEventMessage) {
if (event.value.v == WRITE_CPLT_MSG) {
memcpy((void *)buff, (void *)scratch, BLOCKSIZE);
buff += BLOCKSIZE;
}
}
}
else
{
break;
}
}They've found the bug inside the for loop but my code is not going inside that loop because it's inside else condition and my code is working inside if condition and inside while loop BSP_SD_GetCardState() taking time to give respond which results in sd card task to take long time to finish.
while(timer > osKernelSysTick())
{
if (BSP_SD_GetCardState() == SD_TRANSFER_OK)
{
res = RES_OK;
break;
}
}I've traced its function calling and timing using percepio tracelyzer tool. I've attached a excel sheet which tells the timing when SD card task is taking more time to write data on SD card and between of those timings it's working fine. if we see closely, then generally every 22 sec this is happening.
Can someone suggest why am I getting this random behaviour and how can I reduce this time taken by a SD card?
@Community member, @Community member - Please have a look at it. I think you can give some inputs in this.
