4.2 SPI for SD Card Interfacing

Control of multimedia and SD cards without a native host interface is possible by using the card's SPI mode. The communication protocol is relatively simple, as described in detail here (http://elm-chan.org/docs/mmc/mmc_e.html) by ELM-Chan. It is possible to use an AVR SPI peripheral for this with ease. The necessary configuration for SD card initialization is as follows:
  • The desired pin locations may be checked, and the alternate location selected, if necessary, in the PORTMUX peripheral
  • The MOSI, CS, SCK pin directions may be set as output
  • A pull-up may be enabled on the MISO pin (the SD card DO pin - this is very important specifically for SD cards; without it, they will fail to initialize)
  • Master SPI mode may be enabled
  • An SPI prescaler may be selected to result in a frequency between 100 and 400 kHz (this is also very important for SD card initialization). This will vary depending on the peripheral clock frequency. More information can be found in the device data sheet CLKCTRL chapter.
  • Buffer mode may be enabled to make flags available for transmit and receive complete
  • Wait for Receive may be enabled to allow data to go directly to the shift register
  • The slave select line may be disabled (this may be controlled by software)
  • SPI Mode 0 (default setting, recommended) or SPI Mode 3 (will work in most cases) may be selected
  • Finally, the SPI may be enabled

After initialization, the SPI clock prescaler and 2X setting can be changed to result in a faster SPI clock.