Question
Cummulative data loss in SD write
Posted on March 30, 2016 at 05:30
Hello all,
I am facing some data loss while SD card writing. My application needs to save timer count and accerelometer data in a 8KB buffer in every 10ms (Tim_9 interrupt) and when the buffer is full, it'll write to SD card. Tim_9 interrupt has higher priority (0) than SDIO (1). It seems to loose 10~12 samples in every ~3s; What might be the reason? Is it missing data when processor executes SD write command ? I've tried double buffer: to store samples in another buffer when SD write executes. No improvement. Is my approach wrong? I request your comment to fix it. Thank you. int WriteIndex= 0; char BufferSting[2][8192]; int main(void) { ....... memset(&fs32, 0, sizeof(FATFS)); // SD card initialization res = f_mount(0, &fs32); memset(&fil, 0, sizeof(FIL)); res=f_open(&fil, filename, FA_OPEN_ALWAYS | FA_WRITE); ..... while(1) { PWR_EnterSleepMode(PWR_Regulator_ON, PWR_SLEEPEntry_WFI); if(sample_flag == 1) // 10ms interrupt from Timer 9 { Acc_GetXYZ_Data16(&x,&y,&z); // Accerelometer data. if(WriteIndex >= 8192) // Buffer full { f_lseek(&fil, fil.fsize); f_write(&fil, BufferSting[bf], strlen(BufferSting[bf]), &BytesWritten); // SD card writing f_sync(&fil); WriteIndex=0; if (bf==0) bf=1; // alternative buffer selection else bf=0; WriteIndex += sprintf(&BufferSting[bf][WriteIndex],''%d\t%d\t%d\t%d\r\n'',CNT32,x,y,z); } else { WriteIndex += sprintf(&BufferSting[bf][WriteIndex],''%d\t%d\t%d\t%d\r\n'',CNT32,x,y,z); } sample_flag = 0; } } void TIM9_IRQHandler(void) { if (TIM_GetITStatus(TIM9, TIM_IT_Update) != RESET) { CNT32= TIM5->CNT; // 32-bit count TIM5->CNT = 0; TIM_ClearITPendingBit(TIM9, TIM_IT_Update); sample_flag = 1; } }