AnsweredAssumed Answered

Problem of writing/reading microsd card using the code generated by stm32f4cubemx

Question asked by mao on May 30, 2015
Latest reply on Jul 3, 2015 by mao
This is is very simple project, using stm32f4 discovery and wired microsd socket to read/write data to microsd card.
the code is generated by the stm32f4cubemx.
However error occurs at f_open, tracking down it shows FS_DISK_ERR in ff.c

Please help..


The code for reading/write is:
  /* Register the file system object to the FatFs module */
  if(f_mount(&SDDISKFatFs, (TCHAR const*)SD_Path, 0) != FR_OK)
  {
          
    /* FatFs Initialization Error */
    Error_Handler();
  }
  else
  {
          LED_ON(ORANGE,1);
      /* Create and Open a new text file object with write access */
          if(f_open(&MySdFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) 
      {
                    LED_ON(RED,1);
                    HAL_Delay(2000);
                    LED_ON(RED,0);HAL_Delay(1000);
        /* 'STM32.TXT' file Open for write Error */
        Error_Handler();
      }
      else
      {
                    LED_ON(BLUE,1);
        /* Write data to the text file */
        res = f_write(&MySdFile, wtext, sizeof(wtext), (void *)&byteswritten);
        
        if((byteswritten == 0) || (res != FR_OK))
        {
          /* 'STM32.TXT' file Write or EOF Error */
          Error_Handler();
        }
        else
        {
                         LED_ON(GREEN,1);
          /* Close the open text file */
          f_close(&MySdFile);
          
        /* Open the text file object with read access */
        if(f_open(&MySdFile, "STM32.TXT", FA_READ) != FR_OK)
        {
          /* 'STM32.TXT' file Open for read Error */
          Error_Handler();
        }
        else
        {
          /* Read data from the text file */
          res = f_read(&MySdFile, rtext, sizeof(rtext), (void *)&bytesread);
          
          if((bytesread == 0) || (res != FR_OK))
          {
            /* 'STM32.TXT' file Read or EOF Error */
            Error_Handler();
          }
          else
          {
            /* Close the open text file */
            f_close(&MySdFile);
            
            /* Compare read data with the expected data */
            if((bytesread != byteswritten))
            {                
              /* Read data is different from the expected data */
              Error_Handler();
            }
            else
            {
          /* Success of the demo: no error occurrence */
//              BSP_LED_On(LED4);
            }
          }
        }
      }
    }
  }

Outcomes