journal of recreational computer science

home

Using an SD Card with a Texas Instruments Tiva C TM4C129

28 Aug 2015

I’ve been trying to get this to work for a few hours so I hope this writeup will save someone at least that much time.

Tiva SD1

First things first - the evaluation board can use two “booster packs” (prefabricated modules that extend the functionality of the eval board with various things). Each booster pack has an SPI configuration assigned to it, so when you want to use SPI for booster pack 1, you need to tell the SPI library to select a different set of pins, and if you use the other, you need to select a different one.

The library in question is the SD_TM4C.

You can tell the library which SPI interface to use by using an additional third parameter when calling init:

card.init(CS_PIN, SPI_HALF_SPEED, SPI_MODULE_NUMBER);

Useful values of SPI_MODULE_NUMBER are 0 through 4 for the TM4C1294NCPDT board - each corresponding to a different SPI pin port. Consult the source for details. Any unused pin can be configured as the CS pin for the SD card.

The SD Cards needs to be wired as follows:

SPI Micro SD SD Card SPI 0 SPI 1 SPI 2 SPI 3 SPI 4
    9          
CS 2 DAT3/CD 1 DAT3/CD PA3 PB4 PD2 PF2 PQ1
MOSI 3 CMD 2 CMD PA4 PE4 PD1 PF1 PQ2
GND 6 VSS/GND 3 VSS/GND GND GND GND GND GND
VDD 4 VDD/3.3v 4 VDD/3.3v +3.3v +3.3v +3.3v +3.3v +3.3v
SCLK 5 CLK 5 CLK PA2 PB5 PD3 PF3 PQ0
    6 VSS/GND          
MISO 7 DAT0 7 DAT0 PA5 PE5 PD0 PF0 PQ3
    8          

The example in the SD card library for Energia is wrong as of 28 August 2015. Someone has switched parameters around when initializing the card and it took me some digging to discover that.

Below is a code snippet for the CardInfo example that works:

/*
  SD card test

  created  28 Mar 2011
  by Limor Fried
  modified 9 Apr 2012
  by Tom Igoe
  modified 28 Aug 2015
  by Jan Szumiec
 
*/
// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

const int chipSelect = PD_2; //cs PIN

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.print("\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);     // change this to 53 on a mega


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(chipSelect, SPI_HALF_SPEED, 2)) { // 2 is the SPI module number here.
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch (card.type()) {
  case SD_CARD_TYPE_SD1:
    Serial.println("SD1");
    break;
  case SD_CARD_TYPE_SD2:
    Serial.println("SD2");
    break;
  case SD_CARD_TYPE_SDHC:
    Serial.println("SDHC");
    break;
  default:
    Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);


  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {

}
comments powered by Disqus