Fast data logging with STM32F4 using USB_OTG_FS and FatFS
Hi everyone,
I'm trying to log data from different sensor into a USB flash drive with a 500Hz sampling frequency.
I set up a timer to define the sampling frequency for data logging and everything seems to work fine.
The problems comes when i try to write the data to a log file (I've tryed txt, csv and bin, but it doesn't seems to make a difference for FatFs) beacuse the writing instruction introduces a irregoular delay.
So far i've tried:
- buffering data in different way to match sector size
- formatting usb disk in different way (FAT32, FAT16, exFAT)
- cleaning code as much as possible to just do the writing operation while logging
Does anyone know a method to speed up writing operation?
Thank you
Here's the main:
if(eff==1){
if(fs==1){
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, RESET);
myData = LIS3DSH_GetDataScaled();
x=(int)(myData.x*9806.65);
acc_raw[k]=x;
k++;
y=(int)(myData.y*9806.65);
acc_raw[k]=y;
k++;
z=(int)(myData.z*9806.65);
acc_raw[k]=z;
k++;
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, SET);
fs=0;
}
if(k==99){
s++;
for(int g=0; g<98; g=g+3){
f_printf(&myFilea, "%d : %d, %d, %d\n", s, acc_raw[g], acc_raw[g+1], acc_raw[g+2] );
}
k=0;
bufclear1();
}
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET && write_var==0){
i=0;
write_var=1;
while(deb<250){
}
eff=1;
}
if(one_sec==500 && write_var==1){
for(int g=0; g<98; g=g+3){
f_printf(&myFilea, "%d : %d, %d, %d\n", s+1, acc_raw[g], acc_raw[g+1], acc_raw[g+2] );
//g=g+2;
}
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET);
f_close(&myFilea);
f_close(&myFilet);
write_var=0;
s=0;
deb=0;
eff=0;
open=0;
one_sec=0;
acqu_count++;
HAL_Delay(500);
}
if(Appli_state == APPLICATION_DISCONNECT){
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
open=0;
}
if(open==0){
switch(Appli_state){
case APPLICATION_IDLE:
break;
case APPLICATION_START:
res=f_mount(&myUsbFatFS, (TCHAR const*)USBHPath, 0);
if( res != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
//HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
}
break;
case APPLICATION_READY:
sprintf (buffer, "A%d.bin",acqu_count);
res=f_open(&myFilea, buffer, FA_OPEN_APPEND | FA_WRITE );
bufclear();
open=1;
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET );
//HAL_Delay(500);
//}
break;
case APPLICATION_DISCONNECT:
//tUTRN GREEN ON
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
break;
}
}
}
Here's the Timer interrupt handler:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* Prevent unused argument(s) compilation warning */
HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
if(eff==1){
one_sec++;
}
if(write_var==1){
deb++;
fs=1;
if(temp_counter <250) temp_counter++;
else {
fs_temp=1;
temp_counter=0;
}
//i=1-i;
}
}