Interfacing with a Secure Digital (SD) card

From Mech
Jump to navigationJump to search

Original Assignment

Establish SPI communication between your PIC and a Secure Digital (SD) card for data logging. Demonstrate the ability to store data on the card and to read it back later.


Overview

Secure Digital Cards, or SD cards, are used to hold information in many common electronic devices from digital cameras to mobile phones and come in sizes as small as 4 MB and as large as 8 GB. In this lab, we will establish communication between a Microchip PIC 18F4520 and a 2GB SD card manufactured by Apacer.

SD cards can operate three different communication modes: One-bit SD mode, four-bit SD mode, and SPI mode. SPI is a more basic protocol and it is widely supported by many microcontrollers, including the PIC 18F4520. We'll be using SPI mode in this lab.


Hardware

Card Interface

An SD card has 9 pins. Only 7 of these pins are used to communicate with an SD card in SPI mode. SD cards require between 2 and 3.6 VDC. In this lab, we use a bench top power supply to provide 3.3 VDC to both the PIC and to the SD card. 50k pull-up resistors are essential, even for the pins that are not being used for SPI communications. Note that a pull-up resistor should not be used on the clock line.

Wiring diagram for an SD card (Foust, 2004)


SD Card Holder

SD card holders are typically surface-mount components that are designed to be placed on printed circuit boards by machines. As such, they can be very difficult to handle and connect. At first, we were given an HR8464CT-ND SD card holder. Attempts to solder solid-core to the pins of this device caused the pins to fall out. Instead, we used an HR845CT-ND which had pins that were more resistant to axial force and thus easier to solder.

For this lab, we cut a strip of square-pinned header to the proper length and simply soldered the pins of the holder to it. See the photograph below for more detail.

Completed Circuit

Assembled circuit

Note that the PIC is being powered from a bench top power supply and that it is hooked up to a PC via the RS-232 cable provided in lab.



Software

Driver

Note that this lab was completed by making slight modifications to an example application included in the CCS v4.081library. The low level driver code, included in the Drivers directory of the CCS installation, must be copied verbatim into the same directory as the application code.

Application

The original, unmodified application code was included in the newer version of CCS mentioned above. Note that as of Winter Quarter 2009, these drivers are not available in the Mechatronics lab but are available on the installation media provided in our lab kits. Modifications were made to to the code change the assignment of the SPI pins, modify the includes to use our PIC, and properly configure RS232 and the main clock. Additional modification was made to make the application display the contents of the SD card identification register. All modifications contain "MODIFIED" in their comments.


/////////////////////////////////////////////////////////////////////////
////                      ex_mmcsd.c                                 ////
////                                                                 ////
//// Similar to ex_extee.c, an example that demonstrates             ////
//// writing and reading to an MMC/SD card.                          ////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////
////        (C) Copyright 2007 Custom Computer Services              ////
//// This source code may only be used by licensed users of the CCS  ////
//// C compiler.  This source code may only be distributed to other  ////
//// licensed users of the CCS C compiler.  No other use,            ////
//// reproduction or distribution is permitted without written       ////
//// permission.  Derivative programs created using this software    ////
//// in object code form are not restricted in any way.              ////
/////////////////////////////////////////////////////////////////////////

 //These settings are for the CCS PICEEC development kit which contains
 //an MMC/SD connector.
 #include <18f4520.h>                // MODIFIED
 #fuses HS,NOLVP,NOWDT,NOPROTECT     // MODIFIED
 #use delay(clock=40000000)          // MODIFIED
 #use rs232(baud=19200, UART1)       // MODIFIED hardware UART uses  RC6/TX and RC7/RX

 #include <stdlib.h> // for atoi32

 //media library, a compatable media library is required for FAT.
 #use fast_io(c)
 #define MMCSD_PIN_SCL     PIN_B0 //o MODIFIED  THIS WAS A HUGE PROBLEM, C3 DIDN'T WORK
 #define MMCSD_PIN_SDI     PIN_C4 //i
 #define MMCSD_PIN_SDO     PIN_C5 //o
 #define MMCSD_PIN_SELECT  PIN_C2 //o
 #include <mmcsd.c>

 #include <input.c>

 void main(void)
 {
  BYTE value, cmd;
  int32 address;
  printf("\r\n\nex_mmcsd.c\r\n\n");
  printf("\r\n\n");

  if (mmcsd_init())
  {
     printf("Could not init the MMC/SD!!!!");
     while(TRUE);
  }
  mmcsd_print_cid();     // MODIFIED

  do {
     do {
        printf("\r\nRead or Write: ");
        cmd=getc();
        cmd=toupper(cmd);
        putc(cmd);
     } while ( (cmd!='R') && (cmd!='W') );
     printf("\n\rLocation: ");
     address = gethex();
     address = (address<<8)+gethex();
     if(cmd=='R')
     {
        mmcsd_read_byte(address, &value);
        printf("\r\nValue: %X\r\n", value);
     }
     if(cmd=='W') {
        printf("\r\nNew value: ");
        value = gethex();
        printf("\n\r");
        mmcsd_write_byte(address, value);
        mmcsd_flush_buffer();
     }
  } while (TRUE);
}

Operation

Interacting with the SD card is done via a PC connected by RS-232 to the PIC. Any terminal emulator can be used to connect to the correct COM port at 19200 baud. Once connected, powering up or resetting the system will cause the PIC to communicate with a card. If it cannot, the program will report an error message and quit. If it is successful in initializing communication with the card, it will immediately print the contents of the SD card's identification register, then prompt the user to read or write to a given memory address.

Operation of the card interface program

References and Further Reading

F. Foust (2004) -- Application Note: Secure Digital Card Interface for the MSP430