Using the SD/MMC SPI Driver

To use the SD/MMC SPI driver, five files, in addition to the generic file system files, must be included in the build:

  • fs_dev_sd.c
  • fs_dev_sd.h
  • fs_dev_sd_spi.c
  • fs_dev_sd_spi.h
  • fs_dev_sd_spi_bsp.c

The file fs_dev_sd_spi.h must also be #included in any application or header files that directly reference the driver (for example, by registering the device driver). The following directories must be on the project include path:

  • \Micrium\Software\uC-FS\Dev\SD
  • \Micrium\Software\uC-FS\Dev\SD\SPI

A single SD/MMC volume is opened as shown in Using the SD/MMC SPI Driver#Listing - Opening a SD/MMC device volume (SPI mode). The file system initialization (FS_Init()) function must have previously been called.

ROM/RAM characteristics and performance benchmarks of the SD/MMC driver can be found in Driver Characterization. The SD/MMC driver also provides interface functions to get low-level card information and read the Card ID and Card-Specific Data registers (see FAT System Driver Functions).

Listing - Opening a SD/MMC device volume (SPI mode)
FS_ERR  App_FS_AddSD_SPI (void)
{
    FS_ERR       err;
        
    FS_DevDrvAdd((FS_DEV_API *)&FSDev_SD_SPI,           (1)                
                 (FS_ERR     *)&err);
    if ((err != FS_ERR_NONE) && (err != FS_ERR_DEV_DRV_ALREADY_ADDED)) {
        return (DEF_FAIL);
    }
                                                        (2)   
    FSDev_Open((CPU_CHAR *)"sd:0:",                        (a) 
               (void     *) 0,                             (b) 
               (FS_ERR   *)&err); 
    
   
    switch (err) {
        case FS_ERR_NONE:
             break;
        
        case FS_ERR_DEV:
        case FS_ERR_DEV_IO:
        case FS_ERR_DEV_TIMEOUT:
        case FS_ERR_DEV_NOT_PRESENT:
             return (DEF_FAIL);
        default:
             return (DEF_FAIL);
    }        
                                                       (3)        
    FSVol_Open((CPU_CHAR         *)"sd:0:",               (a) 
               (CPU_CHAR         *)"sd:0:",               (b)  
               (FS_PARTITION_NBR  ) 0,                    (c) 
               (FS_ERR           *)&err);    
    switch (err) {
        case FS_ERR_NONE:
             APP_TRACE_DBG(("    ...opened volume (mounted).\r\n"));
             break;
        case FS_ERR_DEV:
        case FS_ERR_DEV_IO:
        case FS_ERR_DEV_TIMEOUT:
        case FS_ERR_DEV_NOT_PRESENT:
        case FS_ERR_PARTITION_NOT_FOUND:            
             APP_TRACE_DBG(("    ...opened device (unmounted).\r\n"));             
             return (DEF_FAIL);
        default:                                      
             APP_TRACE_DBG(("    ...opening volume failed w/err = %d.\r\n\r\n", err));
             return (DEF_FAIL);
    }
    return (DEF_OK);
}

(1) Register the SD/MMC SPI device driver FSDev_SD_SPI.

(2) FSDev_Open() opens/initializes a file system device. The parameters are the device name (1a) and a pointer to a device driver-specific configuration structure (1b). The device name (1a) is composed of a device driver name (“sd”), a single colon, an ASCII-formatted integer (the unit number) and another colon. Since the SD/MMC SPI driver requires no configuration, the configuration structure (1b) should be passed a NULL pointer.

Since SD/MMC are often removable media, it is possible for the device to not be present when FSDev_Open() is called. The device will still be added to the file system and a volume opened on the (not yet present) device. When the volume is later accessed, the file system will attempt to refresh the device information and detect a file system (see Using Devices for more information).

(3) FSVol_Open() opens/mounts a volume. The parameters are the volume name (3a), the device name (3b) and the partition that will be opened (3c). There is no restriction on the volume name (3a); however, it is typical to give the volume the same name as the underlying device. If the default partition is to be opened, or if the device is not partition, then the partition number (3c) should be zero.

If the SD/MMC initialization succeeds, the file system will produce the trace output as shown in Using the SD/MMC SPI Driver#Figure - SD/MMC detection trace output (if a sufficiently high trace level is configured). See Trace Configuration about configuring the trace level.

Figure - SD/MMC detection trace output