3
\$\begingroup\$

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.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ You seem to be using a whole load of global variables that aren't then included in the code above. How about including all of the relevant code and links to the libraries used. \$\endgroup\$
    – Andrew
    Commented Mar 29, 2017 at 8:44
  • \$\begingroup\$ Try a different card. \$\endgroup\$
    – Turbo J
    Commented Mar 29, 2017 at 9:02
  • 1
    \$\begingroup\$ When you say "big", I find myself asking "how big, exactly"? \$\endgroup\$
    – Finbarr
    Commented Mar 29, 2017 at 10:40
  • \$\begingroup\$ @Finbarr the size is 43067 KB \$\endgroup\$ Commented Mar 29, 2017 at 11:00
  • \$\begingroup\$ @Andrew which variables and functions do you refer to ? If you need I can add the FATFS library but it is built in and I don't think the problem come from there \$\endgroup\$ Commented Mar 29, 2017 at 11:05

2 Answers 2

1
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

If you're using an SD card most probably the __MAX_SS and __MIN_SS values in the ffconf.h will be set to 512. And that means your buffer created for read-write operations will be of that size. This is why you were getting an FR disk error, (a lower level error, something wrong on the driver side). Maybe you were passing a bigger value into your buffer. Anyway, you got it solved. But hope this would help someone else.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.