cancel
Showing results for 
Search instead for 
Did you mean: 

How to continuously store data into SD card?

Niyathi Shenoy
Associate II

I have SD card interfaced with stm32f407 through 4bit SDIO. I am writing data from I2S _DMA_receive buffer into the sd card. This write should happen continuously,but it happens only once.Here is the piece of the code:

FATFS myFATAFS;

FATFS *myFATAFS1;

FIL myFILE;

UINT testByte;

FRESULT res;

uint16_t i2s_rcvbf[2];

uint32_t i2s_final;

while(1)

{

HAL_I2S_Receive_DMA(&hi2s2,i2s_rcvbf,2);

i2s_final= (i2s_rcvbf[0]<<16)+ i2s_rcvbf[1];

HAL_Delay(500);

if(f_mount(&myFATAFS,SD_Path,1)==FR_OK)

{

char myPath[]="0:rec.wav"; 

if(f_open(&myFILE,myPath,FA_WRITE | FA_OPEN_ALWAYS)== FR_OK)

{

HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_14);

if(f_write(&myFILE,(void*)&i2s_final,sizeof(i2s_final),&testByte)==FR_OK)

{

HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_15);

}

f_close(&myFILE);

i2s_final=0x00000000;

HAL_Delay(1000);

}

}

}

2 REPLIES 2

Writing 4 byte into a file you keep creating doesn't seem like a winning strategy.

Would also imagine the I2C is choking with an overflow at every iteration.

Move the mount and file creation out side the loop. Write much more data in each iteration. Try at least 8KB. You might also need to use two buffers, allowing the I2C to fill one while writing the other.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Niyathi Shenoy
Associate II

Hi Clive,

Thank you for your response.

I have moved the mount and file creation outside the loop. How to write more data in each iteration? And could you please explain using of two buffers and how to do that?

My code now is:

FATFS myFATAFS;

FATFS *myFATAFS1;

FIL myFILE;

UINT testByte;

FRESULT res;

uint16_t i2s_rcvbf[2];

uint32_t i2s_final;

if(f_mount(&myFATAFS,SD_Path,1)==FR_OK)

{

char myPath[]="0:rec.wav"; 

if(f_open(&myFILE,myPath,FA_WRITE | FA_OPEN_ALWAYS)== FR_OK)

{

HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_14);

 }

}

while(1)

{

HAL_I2S_Receive_DMA(&hi2s2,i2s_rcvbf,2);

i2s_final= (i2s_rcvbf[0]<<16)+ i2s_rcvbf[1];

HAL_Delay(500);

if(f_write(&myFILE,(void*)&i2s_final,sizeof(i2s_final),&testByte)==FR_OK)

{

HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_15);

}

f_close(&myFILE);

i2s_final=0x00000000;

HAL_Delay(1000);

}