cancel
Showing results for 
Search instead for 
Did you mean: 

Possible errors in the implementation of "hal_sd" and "fatfs"

Mick P. F.
Senior

Hello,

I use the current tools: STM32CubeIDE 1.8.0, STM32CubeMX 6.4.0 and STM32Cube_FW_H7_V1.9.1.

I have a couple of issues with current implementation of the connection between SDMMC and FATFS:

The first problem in the platform driver "fatfs_platform.c, function" BSP_PlatformIsDetected ":

This code queries

        if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != GPIO_PIN_RESET)

but my card reader (direct connection between the card slot and the default pins of the SDMMC1 interface and 3.3V supply) returns "GPIO_PIN_SET" for this pin if the card is in the slot.

I don't know if it is a specific fault of my card reader (Adafruit 4682 - “MicroSD SPI or SDIO Card Breakout Board�?), so I changed the code and don't want to investigate it any further.

The second problem is much more serious. During the initialization the "SD_SendSDStatus" procedure is called (see attachment) and here it doesn't leave the status query loop ("stm32h7xx_hal_sd.c", lines 3258ff):

  /* Get status data */
  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;
    }
  }

The code seems to be illogical to me: it should wait until these 4 bits are cleared, but instead it waits until one of these bits is set (one of these errors occurs)! Or am I reading this code wrong? This loop runs with a timeout of about one year ... Really, I don't want to wait so long.

In addition, according to some sources (also "AN5200 - Getting Started with STM32H7 Series SDMMC host controller") and examples from the Internet, some parameters for DMA and MDMA must be set/changed in STM32CubeMX, but I cannot enter or change anything there. I can only activate the interrupts for SDMMC1 interface in NVIC settings.

Can somebody help me to get it running?

Thanks in advance,

Michael

2 REPLIES 2
TDK
Guru

The BSP_PlatformIsDetected function is going to be board-specific and will depend on what hardware you're running. I don't see this function in the H7 CubeMX library.

https://github.com/STMicroelectronics/STM32CubeH7/search?q=BSP_PlatformIsDetected

> The code seems to be illogical to me: it should wait until these 4 bits are cleared, but instead it waits until one of these bits is set (one of these errors occurs)!

Only the first three are error flags. The last flag (DATAEND) indicates the end of the transfer and is the normal way of exiting that loop.

Agreed that the default timeout seems silly.

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

About the first "problem": I think, the STM32CubeMX generates the source file "fatfs_platform.c", when you link SDMMC with FATFS. It appears in the subdirectory "FATFS/Target" of the project after executing the step "Generate code".

About the second problem: Great! Now I know that I have correctly understood the content of Chapter 55.9.11 in the manual "RM0433" about the status register.

But it doesn't solve the problem. Since there were no transfers before the interface was initialized, "DATAEND" (bit 8) cannot be set. This "faulty" state is not caused by my hardware or my code. Or am I still getting it wrong?