3

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.

  • 2
    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. – Andrew Mar 29 '17 at 08:44
  • Try a different card. – Turbo J Mar 29 '17 at 09:02
  • 1
    When you say "big", I find myself asking "how big, exactly"? – Finbarr Mar 29 '17 at 10:40
  • @Finbarr the size is 43067 KB – Armand Chocron Mar 29 '17 at 11:00
  • @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 – Armand Chocron Mar 29 '17 at 11:05
  • @ArmandChocron at a glance ret, filer_pointer and br are undefined in the code you posted. Almost certainly not the cause of the issue in this case if reading 512 byte blocks clears the problem but people aren't going to spend time trying to guess the problem with incomplete information. – Andrew Mar 29 '17 at 16:08

2 Answers2

1

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

0

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.

clamentjohn
  • 386
  • 3
  • 15