I'm about to close a long outstanding task: Make SD Cards work on a Hercules LaunchPad.I've done several half-baked attempts since November 2013. Thanks to persistence (not mine, martinvalencia's), it's finally working now.

This blog explains how to set up your Hercules peripherals, and it contains some insights and optimizations that we can do to TI's port of fatfs.

We'll check what libraries we need, how to configure the SPI module and the real time clock, and how to enable the fatfs string write functions.

Our end goal is to have a working SD Card program that can write data, read files and list directories.

I'm using the Hercules RM46 LaunchPad.

# Libraries and Dependencies

We need the fatfs library, and TI's port. These are available here: http://e2e.ti.com/cfs-file/__key/communityserver-discussions-components-files/312/RM48_5F00_HDK_5F00_SD_5F00_Card.zip.

Extract the files from RM48_HDK_SD_Card\Library to your location of choice.

I'm using location C:\Users\Jan\Documents\elektronica\Texas Instruments\Hercules\rm46\sdcard_libs\HDK SD Card\Library , but any location outside your CCS workspace is fine.

The fatfs folder contains the FatFS file system in the src folder, and TI's adaption to Hercules in the port folder.

The SDCard folder contains example code for the Hercules HDK. W"ll reuse and adapt that.

We'll update the TI port with fatfs' latest version (R0.11a at the time of writing): http://elm-chan.org/fsw/ff/00index_e.html

Extract the zip file somewhere temporary, and copy it's doc and src folders into the fatfs folder that you just created in the previous step. Overwrite when asked.

That's it. we now have our external sources available. All the rest we'll do step by step in the development tools.

# Create a Project

We start in Code Composer Studio. Create a new CCS project for your microcontroller

We'll generate a HALCoGen project too. We'll put it in a HALCoGen subfolder of our project.

#### Drivers

Our first task is to enable the drivers we'll be using.

We'll be talking to the SD Card via SPI. I'm going to use SPI1. But you can use any of the other SPIs that are available.

The RTI will deliver the 10 ms clock tick to our SD library. We use the SCI2 driver to talk serial over USB (check your LaunchPad's scghema to see what serial peripheral is linked to USB).

#### PINMUX

We're going to take care that our SCI and SPI PINS are set to the correct functions.

Take care that for SPI, you have selected MIBSPI1NCS_0, MIBSPI1CLK, MIBSPI1SOMI and MIBSPI1SIMO.

#### Clock Management

On the GCM tab, slow down the peripheral clock speed by setting the VCLK1 divider to 2.

This will allow us to get a reasonable SPI clock. If you have a proper PCB for your SD Card, you may reconsider putting this back to 1 and test if your design supports baud rates > 300kHz.

#### SPI

On the SPI1 tab, we make the settings to get a correct communication between Hercules and SD Card.

On SPI1 Global, leave all as it is.

On SPI1 Data Formats, adapt to this:

On SPI1 Port, carefully verify that the following pins are set to SPI - except CS[0]. That one is bitbanged by the lib and should be set to GIO.

#### The Real Time Clock

Our SD implementation needs a ping every 10 ms. The RTI will serve that ticker.Comparator 3 is 10 ms by default, so let's use that.

#### Interrupt handler for the ticker

We'll also enable the interrupt handler.

And finally, we'll configure the serial interface. That's the one we'll use to send commands to the Hercules, and read replies and messages. We'll use a terminal (puTTY, the CCS Terminal or whatever tool you prefer) to interact.

Yay, that's it. Generate the code (F5), Save the Project, and leave HalCoGen.

We'll now include the HALCoGen code in our project and try a first compilation.

This is an ideal point to check if our baseline is ok, and that we have a good point to start coding.

In CCS, right-click on the project, and select Properties from the context menu.

Confirm all.

Right Click on the project again, and select Build Project.

If you see this:

'Finished building target: RM46_sdcard_testbed.out'

' '

**** Build Finished ****

tap yourself on the shoulder.

# Add the SD Libraries to the Project

We'll now add all that code that we downloaded and extracted. The code will stay where it is, but in CCS, it will look as if it's part of the project.

#### fatFS

Right click on the project, and add a New Folder.

Browse to the place where you've dropped the fatfs sources

Confirm all.

#### SD Card

Do the same with the SDCard folder

Just like we did with the HALCoGen include folder, add the source folders of the SDCard and fatfs/src and fatfs/port to the include path.

"${workspace_loc:/${ProjName}/SDCard}"

"${workspace_loc:/${ProjName}/fatfs/src}"

"${workspace_loc:/${ProjName}/fatfs/port}"

#### Exclude unused sources from buld

Some files and folders should not be compiled. We exclude them by right-clicking and selecting Exclude from Build from the contect menu

Entries to exclude:

• fatfs/doc
• fatfs/src/option
• fatfs/src/diskio.c
• fatfs/port/mmc-hdk-hercules1.c (in part2, you'll see that we roll our own - SPI port independent - version of this file)
• fatfs/port/mmc-hdk-hercules2.c
• fatfs/port/sample-mmc.c

Replace SDCard/sdcard.c line 709 with this one (search for f_mount):

iFResult = f_mount(&g_sFatFs, "", 1);



The signature of this function has changed in version of the fatFS library that we downloaded. The TI example was using an older version (which we overwrote when extracting the latest fatFS archive).

Right Click on the project again, and select Build Project.

If you see this:

'Finished building target: RM46_sdcard_testbed.out'

' '

**** Build Finished ****

tap yourself on the shoulder - again.

We're now in a good position to start coding. Over to Part 2

Reference:

http://e2e.ti.com/support/microcontrollers/hercules/f/312/p/452304/1631747#pi239031350=1