cancel
Showing results for 
Search instead for 
Did you mean: 

FATFS middleware: invert card detect logic level

CKugl.1
Senior II

I'm using STM32CubeMX 6.5.0, MCU STM32L4A6ZGTx on NUCLEO-L4A6ZG, and an Adafruit 4682 Micro SD SPI or SDIO Card Breakout Board.

On this board, the card detect line (DET) "is connected to GND internally when there's no card, but when one is inserted it is pulled up to 3V with a 4.7kΩ resistor. That means that when the pin's logic level is False there's no card and when it's True there is."

However, the middleware seems to assume the inverse.

FATFS\Target\fatfs_platform.c:

uint8_t	BSP_PlatformIsDetected(void) {
    uint8_t status = SD_PRESENT;
    /* Check SD card detect pin */
    if(HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != GPIO_PIN_RESET)
    {
        status = SD_NOT_PRESENT;
    }
    /* USER CODE BEGIN 1 */
    /* user code can be inserted here */
    /* USER CODE END 1 */
    return status;
}

What is the best way to solve this?

For now, I just did this:

uint8_t	BSP_PlatformIsDetected(void) {
    uint8_t status = SD_PRESENT;
    /* Check SD card detect pin */
    if(HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != GPIO_PIN_RESET)
    {
        status = SD_NOT_PRESENT;
    }
    /* USER CODE BEGIN 1 */
    // Adafruit 4682's logic level is False there's no card and when it's True there is
    if (status == SD_NOT_PRESENT)
    	status = SD_PRESENT;
    else
    	status = SD_NOT_PRESENT;
    /* USER CODE END 1 */
    return status;
}

2 REPLIES 2

>>However, the middleware seems to assume the inverse.

Most card-trap methods basically have the card make a switch, that most often goes to ground. The weak 50K pull-up on the STM32 indicating card absent with a HIGH state, card present with a LOW

>>What is the best way to solve this?

Change the logic of the test to reflect your hardware.

  if(HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != GPIO_PIN_SET)
  {
    status = SD_NOT_PRESENT;
  }
 
or
 
  if(HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == GPIO_PIN_RESET)
  {
    status = SD_NOT_PRESENT;
  }

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
CKugl.1
Senior II

I just generated a new project, and this time I found another way that is more like what I was looking for.

I noticed that in the file FATFS/Target/bsp_driver_sd.c, BSP_SD_IsDetected is declared as __weak:

/**
 * @brief  Detects if SD card is correctly plugged in the memory slot or not.
 * @param  None
 * @retval Returns if SD is detected or not
 */
__weak uint8_t BSP_SD_IsDetected(void)
{
  __IO uint8_t status = SD_PRESENT;
 
  if (BSP_PlatformIsDetected() == 0x0)
  {
    status = SD_NOT_PRESENT;
  }
 
  return status;
}

so I put my own version in main.c:

/**
 * @brief  Detects if SD card is correctly plugged in the memory slot or not.
 * @param  None
 * @retval Returns if SD is detected or not
 */
uint8_t BSP_SD_IsDetected(void){
  __IO uint8_t status = SD_PRESENT;
 
  if (BSP_PlatformIsDetected() != 0x0)
  {
    status = SD_NOT_PRESENT;
  }
 
  return status;
}

and that works.