cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with STM32H743ZI2 using SDMMC and µSD card

Gcare.1
Associate II

I am trying to configure my STM32H743ZI2 to work with a microSD card. For data logging purposes.

schematic of the SD card adaptor:0693W00000AMlhCQAT.pngI configured the following in CUBEMX:

SDMMC1 as SD 4 bits Wide bus with SDMMC1 global interrupt enabled (all other settings are default)

FATFS mode SD Card with everything default.

Changed stack and heap size : 0x400 heap size, 0x800 stack size.

My clock config:0693W00000AMlmkQAD.pngWhen i run the following code :

void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
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[] = "STM32 FATFS works great!"; /* File write buffer */
  uint8_t rtext[_MAX_SS];/* File read buffer */
  /* USER CODE END 1 */
  /* USER CODE END 1 */
 
  /* Enable I-Cache---------------------------------------------------------*/
  SCB_EnableICache();
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ETH_Init();
  MX_USART3_UART_Init();
  MX_USB_OTG_FS_PCD_Init();
  MX_SDMMC1_SD_Init();
  MX_FATFS_Init();
  /* USER CODE BEGIN 2 */
  if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
  {
      Error_Handler();
  }
  else
  {
      if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext)) != FR_OK)
      {
          Error_Handler();
      }
      else
      {
          //Open file for writing (Create)
          if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
          {
              Error_Handler();
          }
          else
          {
 
 
 
              //Write to the text file
              res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
              if((byteswritten == 0) || (res != FR_OK))
              {
                  Error_Handler();
              }
              else
              {
 
 
 
                  f_close(&SDFile);
              }
          }
      }
  }
  f_mount(&SDFatFS, (TCHAR const*)NULL, 0);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

The following happens:

MX_SDMMC1_SD_Init(); and MX_FATFS_Init(); initialise without error.

(52) f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) also returns no error

But

f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext))

Gets me stuck in the following procedure:

part of : "static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)"

 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  {
    if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
    {
      for (count = 0U; count < 8U; count++)
      {
        *pData = SDMMC_ReadFIFO(hsd->Instance);
        pData++;
      }
    }
 
    if ((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
    {
      return HAL_SD_ERROR_TIMEOUT;
    }
  }

This procedure practically never ends since SDMMC datatimeout has a value of 0xFFFFFFFFU

When i test the signal on the oscilloscope i notice that:

From the start of the f_mkfs function the CLK is always 400KHz, even if i change the clockvalues

CD responds with short HIGH signal.

All data busses are completely empty, (no data)

I added a zip file of my fully configured SD test program.

5 REPLIES 5

Hello @Gcare.1​ ,

I think you need to link the FatFS driver. here Below the updated code. I have added FATFS_LinkDriver() and FATFS_UnLinkDriver() functions

  /*Link the micro SD disk I/O driver */
  if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
  {
    if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
    {
      Error_Handler();
    }
    else
    {
      if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext)) != FR_OK)
      {
        Error_Handler();
      }
      else
      {
        //Open file for writing (Create)
        if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
        {
          Error_Handler();
        }
        else
        {
          
          
          
          //Write to the text file
          res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
          if((byteswritten == 0) || (res != FR_OK))
          {
            Error_Handler();
          }
          else
          {
            
            
            
            f_close(&SDFile);
          }
        }
      }
    }
  }
  f_mount(&SDFatFS, (TCHAR const*)NULL, 0);
  /* Unlink the SD disk I/O driver */
  FATFS_UnLinkDriver(SDPath);
  /* USER CODE END 2 */

I hope this help you. Please let me know if it works now.

Walid.

Gcare.1
Associate II

Hi @Walid ZRELLI​ thank you for your adjusted code!

Although i do believe i need to link de FATFS driver, it unfortunately was not the solution to my problem.

When debugging your solution it only links (line2), mounts (line43) and unlinks(line45) the driver.

It never executes the 'f_mkfs' function.

When i place f_mkfs behind the f_mount function on line 43 i still get stuck in the same mentioned code:

while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
  {
    if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
    {
      for (count = 0U; count < 8U; count++)
      {
        *pData = SDMMC_ReadFIFO(hsd->Instance);
        pData++;
      }
    }
 
    if ((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
    {
      return HAL_SD_ERROR_TIMEOUT;
    }
  }

Do you need any additional information for addressing my problem?

Greetings and thank you for your help,

Gaël

EDIT:

Apparently the FATFS drivers get linked when the  MX_FATFS_Init(); function is called, meaning the drivers were already linked.

BCabr.1
Associate

I have the same issue, did you find the solution ?, Thanks

AScha.3
Chief II

1.

>(52) f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) also returns no error

ok always , because it does nothing. set option "1" , then you see, if it is mounting.

from source: BYTE opt /* Mode option 0:Do not mount (delayed mount), 1:Mount immediately 

2.

set port pin pullups and speed to medium ;

If you feel a post has answered your question, please click "Accept as Solution".

hi,

same issue with maine for controller-STM32H723VGT6.

After a deeply study ,i also found issue with sd card initialization as at the level of.....

 if (hsd->SdCard.CardVersion == CARD_V2_X)

 {

  /* SEND CMD55 APP_CMD with RCA as 0 */

  errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);

  if (errorstate != HAL_SD_ERROR_NONE)

  {

   return HAL_SD_ERROR_UNSUPPORTED_FEATURE;

  }

 }

please fix the prob.Already More then Two Cases are still pending----same problem

https://community.st.com/s/question/0D53W00000QvljESAR/4-bit-sdio-sd-card-sdmmc-issue-on-nucleoh723zg-stm32h723zg-stm32h7

https://community.st.com/s/question/0D53W000024YkeASAS/stm32h743xi-sdmmc1-sd-card-halsderrorunsupportedfeature