TMS570 SD Card

From Stanford SSI Wiki
Revision as of 03:56, 13 January 2017 by Ozeng (talk | contribs)
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.

SDCard.jpg

Contents

Code download here: https://github.com/stanford-ssi/TMS570SDCard

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 (http://elm-chan.org/fsw/ff/00index_e.html). 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 https://www.element14.com/community/people/jancumps/blog/2015/10/03/ti-hercules-launchpad-using-an-sd-card

Usage

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 (https://e2e.ti.com/support/microcontrollers/hercules/f/312/p/518134/1882443) and this breakout: https://www.sparkfun.com/products/12941, but any breakout should work. You can even put jumpers directly onto the sd card if you want, the interface is this: http://i.stack.imgur.com/izQKT.png. 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