Tips: SD Card Detection on NuttX

Today I got SDCard over SPI working on NuttX and already submitted the patch to mainline: https://bitbucket.org/nuttx/nuttx/commits/adb32e31a0123a16095f53de0c2b493b7db11281

Now you also can test SDCard using the STM32F103-Minimum board.

This post is to explain how NuttX’s SDCard driver does to ignore the Card Detect pin on modules that doesn’t have it. Yes, my module doesn’t have CD pin, see:

Initially I was getting this warning message:

WARNING: No card present

And when I tried to mount the SDCard it reported error -19. Well, error -19 is “ENODEV”, that means: “No such device”.

Then analyzing the source code of the driver (at nuttx/drivers/mmcsd/mmcsd_spi.c) I found this:

  /* Check if there is a card present in the slot.  This is normally a matter is
   * of GPIO sensing and does not really involve SPI, but by putting this
   * functionality in the SPI interface, we encapsulate the SPI MMC/SD
   * interface
   */

  if ((SPI_STATUS(spi, SPIDEV_MMCSD) & SPI_STATUS_PRESENT) == 0)
    {
      fwarn("WARNING: No card present\n");
      slot->state |= MMCSD_SLOTSTATUS_NODISK;
      return -ENODEV;
    }

Hmm, interesting! NuttX can use the return of spi_status() function to inform that card is present. Interesting, I was going to mess with the card detection thread/callback, saved by the bell!

Then all I did was to include:

#ifdef CONFIG_MMCSD_SPI
  if (devid == SPIDEV_MMCSD)
    {
       status |= SPI_STATUS_PRESENT;
    }
#endif

inside the stm32_spi1status() at nuttx/configs/stm32f103-minimum/src/stm32_spi.c!

This is a simple and clever solution, like everything else in the NuttX!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s