Electronic – Read big file from sd card with fatfs

cembeddedfatsdstm32f4-discovery

I'm using the STM32F4DISCOVERY board and I am trying to read a file from a micro SD card, and treat the data. I'm using the following functions

int main(void)
{

  int i = 0;
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_SDIO_SD_Init();
  MX_TIM10_Init();
  MX_FATFS_Init();
  MX_USART2_UART_Init();
  create_filter_bank();

  ret = f_mount(&filesystem, buff_mount, 1);
  ret = f_open(&file, WANTED, FA_READ);
  ret = f_read(&file, header, 78, &br);
  ret = acquire_voice_from_file(&file);
  f_sync(&file);
  f_close(&file);
  // Some other code 
}

FRESULT acquire_voice_from_file(FIL* file){

  file_pointer = HEADER_WAV_SIZE;  
  acquired_frames = 0;
  FRESULT read_res;
  while(acquired_frames < NUM_ACQUIRE_FRAMES){
    file_pointer += FFT_SIGNAL_BYTES/2; // Need a correlation between frames
    read_res = read_frame_from_file(file);
    acquired_frames++;
    // Some Other Code 
  }
  return read_res;
}
FRESULT read_frame_from_file(FIL* file){

  int i = 0;
  FRESULT res;
  res = f_lseek(file, file_pointer);
  res = f_read(file, bytes, FFT_SIGNAL_BYTES, &br);
  if(res != FR_OK){
    return res;
  }
  for(i = 0; i < FFT_SIGNAL_LENGTH; i++){
    curr_frame[i] = (bytes[2*i+1] << 8) + bytes[2*i];  
    // Multiplying by Hamming window 
    curr_frame[i] *= (0.54 - 0.46*cos(2*PI*i/((float)FFT_SIGNAL_LENGTH)));
  }
  return res;
}

All the constant values such as WANTED, FFT_SIGNAL_BYTES, and so on, are declared on the header file correctly. In the main function, f_mount, f_open, and f_read, return the result FR_OK and the data is correctly read. But then in the function "acquire_voice_from_file", one or two frames are successfully read, and then I get the value FR_DISK_ERR from the function f_read in the function "read_frame_from_file". I assume that my hardware configuration is alright since the first bytes are read correctly, but I don't manage to solve the issue and read correctly the next frames. The size of the frame I'm trying to read is 2048 bytes (FFT_SIGNAL_BYTES = 2048). Is there an issue to read big files with FATFS ? And if so, is there a solution ? Many thanks for your help.

Best Answer

Problem solved, I followed the answer on this link and it did the trick. I read the data by blocks of 512 bytes and I successfully read frame after frame

Related Topic