TMS570 SD Card

From Stanford SSI Wiki
Jump to navigation Jump to search

Contributors: Eldrick Millares, Orien Zeng, Jessica Romero

This project demonstrates the use of the SPI interface to communicate with SD Cards using the TMS570 board. SD cards may be useful on the satellite for data logging purposes. In addition, the code is a successful demonstration of the spi interface on the board.



Code download here:

The code has the following components:

1. The HalCoGen folder includes the necessary code to access the spi and gio devices on the tms570 device

2. Any files labelled fatfs contribute to an implementation of a file system on our sdcard ( This lets us talk about reading and writing files instead of reading/writing 'disk blocks' - in this case, disk blocks are sd card sectors. The fatfs code depends on a device interface for reading and writing sectors, which is implemented in item 3 below.

3. Any files labelled diskio or mmc-hdk-hercules are related to sending commands to the "disk" (sd card), with the key commands being readsector and writesector.

This code can be attributed almost completely to


Note: I've been having trouble getting the code to work on mac. The serial terminal seems to function differently, but anything outside of print and read from console should work fine. In particular, you can tell whether it works if the application creates the file WSLOG.txt on your sd card.

We'll be using the mibspi[1] and GIOA[0] pins on the tms570 ( and this breakout:, but any breakout should work. You can even put jumpers directly onto the sd card if you want, the interface is this: In this image, ignore the SD section, as we're using the SPI interface. The pinouts labelled D0, D1, D2, etc. also perform spi functions, such as chip select and slave out.

With that in mind, wire up the breakout to the board as follows:

1. cmd on the breakout goes to mibspi[1] SIMO (slave in master out) -- the master sends commands to the sd card

2. D3 (Chip Select) goes to GIOA[0] -- we bitbang chip select on the tms570 as there seems to be a pin conflict between MISO and CS in HalCoGen. Basically, we just pull down GIOA[0] for the duration that we use the card.

3. VCC goes to 3.3V power

4. GND goes to ground

5. D0 (Data Out) goes to SOMI.

With the card wired in, you should be able to run the program, which opens up a command line in the terminal. Try the commands

ls, cat myfile.txt, and append myfile.txt helloworld

and see what they do.


Usage (code)

in HalCoGen/sys_main.c, you will see the following commands:

sciInit(); gioInit(); rtiInit(); rtiEnableNotification(rtiNOTIFICATION_COMPARE3); _enable_IRQ(); rtiStartCounter(rtiCOUNTER_BLOCK1); // Initialize all the things

gioToggleBit(gioPORTA, 0U); // This toggles GIOA[0] high and low. We want this to be low when we're talking to the sdcard - recall that we've bitbanged chipselect to GIOA[0]

mmcSelectSpi(spiPORT1, spiREG1); // Initialize sdcard code to work on mibspi[1]

Read from file

FIL file;

iFResult = f_open(&file, full_file_path, FA_READ); // iFResult contains any error codes from the sdcard. If successful (iFResult == FR_OK), fsrc will be populated with a file object.

iFResult = f_read(&file, buffer, sizeof(buffer) - 1, (UINT *)&bytesRead); // Read one block of data from the file. bytesRead is populated with the number of bytes read.

Write to file

FIL file;

FRESULT iFResult = open_append(&file, full_file_path);

int res = f_printf(&file, "Hello");

res = f_close(&file); // If you don't do this the file might not save