AnsweredAssumed Answered

SDIO not mounting/opening file using HAL on STM32F4-Discovery

Question asked by deschaux.zachary on Jun 3, 2015
Latest reply on Jun 3, 2015 by deschaux.zachary
I have been working on the STM32F4-Discovery board with the STM32DIS-BB to connect to an SD card and write to it. I have been able to do so using the SPL with no issues, but I have yet to be able to using the HAL library and the code generated by STM32CubeMX. I would go forward with SPL, but unfortunately our development is dependent upon using HAL drivers. Also, if this is an actual HAL bug, it should be brought to ST's attention.


I have used a scope to see if there was a signal being output to the SDIO pins, and there was no issue there. The issue seems to lie within how FatFs and HAL interract; whether I have an SD card in or not, and whether the SD card is Kingston or SanDisk (all HC Class 10), the program claims FR_INVALID_DRIVE. If I try to bypass the driver linking, it will mount it but will fail upon attempt to write a file because it can't open an invalid drive or claim FR_DISK_ERR. I have tried using DMA and non-DMA writing methods, but to no avail. 


I have attached a copy of my project, and the main portion of my main.c source code (the code and setup was borrowed from a different demo called FatFs_uSD for the STM324xG_EVAL).


Any help would be greatly appreciated. I'm not sure if this is something I can fix or if it actually is a HAL bug. I've been at this problem for over a month now and have yet to make progress. Please ask for any clarifications if needed.


NEW_HAL_SD.rar


/* USER CODE BEGIN PV */
FATFS SDFatFs;  /* File system object for SD card logical drive */
FIL MyFile;     /* File object */
char SDPath[4]; /* SD card logical drive path */
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

/* USER CODE BEGIN PFP */
static void Error_Handler(void);
/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)
{
  /* USER CODE BEGIN 1 */
     FRESULT res;                                          /* FatFs function common result code */
     uint32_t byteswritten, bytesread;                     /* File write/read counts */
     uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
     uint8_t rtext[100];                                   /* File read buffer */
     
     HAL_NVIC_DisableIRQ(SDIO_IRQn);
     HAL_NVIC_DisableIRQ(DMA2_Stream6_IRQn);
     HAL_NVIC_DisableIRQ(DMA2_Stream3_IRQn);
  /* 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_DMA_Init();
  MX_SDIO_SD_Init();
  MX_FATFS_Init();

  /* USER CODE BEGIN 2 */
     /*##-1- Link the micro SD disk I/O driver ##################################*/
  if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
  {
    /*##-2- Register the file system object to the FatFs module ##############*/
    if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
    {
      /* FatFs Initialization Error */
      Error_Handler();
    }
    else
    {
      /*##-3- Create a FAT file system (format) on the logical drive #########*/
      /* WARNING: Formatting the uSD card will delete all content on the device */
      if(f_mkfs((TCHAR const*)SDPath, 0, 0) != FR_OK)
      {
        /* FatFs Format Error */
        Error_Handler();
      }
      else
      {       
        /*##-4- Create and Open a new text file object with write access #####*/
        if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
        {
          /* 'STM32.TXT' file Open for write Error */
          Error_Handler();
        }
        else
        {
          /*##-5- Write data to the text file ################################*/
          res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
          
          if((byteswritten == 0) || (res != FR_OK))
          {
            /* 'STM32.TXT' file Write or EOF Error */
            Error_Handler();
          }
          else
          {
            /*##-6- Close the open text file #################################*/
            f_close(&MyFile);
            
            /*##-7- Open the text file object with read access ###############*/
            if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
            {
              /* 'STM32.TXT' file Open for read Error */
              Error_Handler();
            }
            else
            {
              /*##-8- Read data from the text file ###########################*/
              res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);
              
              if((bytesread == 0) || (res != FR_OK))
              {
                /* 'STM32.TXT' file Read or EOF Error */
                Error_Handler();
              }
              else
              {
                /*##-9- Close the open text file #############################*/
                f_close(&MyFile);
                
                /*##-10- 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 */
                  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
                }
              }
            }
          }
        }
      }
    }
  }
  
  /*##-11- Unlink the RAM disk I/O driver ####################################*/
  FATFS_UnLinkDriver(SDPath);
  
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}

Outcomes