1

I'm trying to write to a MicroSD-Card using STM32F405 chip.
The pins are connected correctly and each pin on the MicroSD-Card slot can be written to by using HAL_GPIO_WritePin. (Messured with ossciloscope) I'm using CubeMX to generate the init code for TrueStudio, so hopefully everything is ok there as well. But when I run the following code, f_mount returns FR_DISK_ERR. The MicroSD-Card can be written to and read from. If I use a different Device number, i.e. "1:", I get FR_INVALID_DRIVE

So my question is: what could cause FR_DISK_ERR except a faulty MicroSD-Card?

Here is my code so far:

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

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

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SDIO_SD_Init();
  MX_FATFS_Init();

  /* USER CODE BEGIN WHILE */
  FATFS fileSystem;
  FIL testFile;
  uint8_t testBuffer[16] = "SD write success";
  UINT testBytes;
  FRESULT res;

  while((res = f_mount(&fileSystem, SD_MOUNT_PATH, 1)) != FR_OK){
      printf("%d", res); //used to debug res, only for TrueStudio Debugger
  }


    uint8_t path[13] = "testfile.txt";
    path[12] = '\0';

    res = f_open(&testFile, (char*)path, FA_WRITE | FA_CREATE_ALWAYS);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
    res = f_write(&testFile, testBuffer, 16, &testBytes);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);

    res = f_close(&testFile);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);


}

in MX_FATFS_Init() FATFS_LinkDriver(&SD_Driver, SD_Path) is called and returns 0.

Poehli
  • 307
  • 4
  • 16

1 Answers1

0

Without the SD_MOUNT_PATH macro, does the f_mount call look like this: f_mount(&fileSystem, "0:", 1) ?'

Are you saying that f_open, f_write return FR_OK even if f_mount fails?!

FR_DISK_ERR usually means disk_read() or disk_write() failed. Try giving 100ms or 1 sec delay before using f_mount() and between between fatfs function calls.

  • Yes, `SD_MOUNT_PATH` is a macro for `"0:"` Since `f_mount` fails, the while loop will never exit, so no, `f_open`, `f_write` will not return `FR_OK` either. I should have said, that the GDB Debugger shows me, that it fails during `disk_read`. It seems, that `disk_write` is never executed during `f_mount` – Poehli May 10 '17 at 01:14