element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs TI Hercules LaunchPad: using an SD Card Part 3
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 8 Oct 2015 6:24 PM Date Created
  • Views 4139 views
  • Likes 4 likes
  • Comments 25 comments
  • hercules_launchpad
  • spreadsheet
  • spi
  • texas_instruments
  • sdcard
Related
Recommended

TI Hercules LaunchPad: using an SD Card Part 3

Jan Cumps
Jan Cumps
8 Oct 2015

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.

 

   image

 

 

This last part of the blog series shows how to enable write to the SD Card. We'll change our project from the previous blog and write some info to the disk.

 

image



Enable the String Write Functionality

 

When you look at the source of the default download of the FatFS library, you'll see that the string functions are grayed out (see screen capture below).

image

 

That's because the lib is configured that way. If you check the ffconf.h file, you'll find this line of code:

 

#define    _USE_STRFUNC    0
/* This option switches string functions, f_gets(), f_putc(), f_puts() and
/  f_printf().
/
/  0: Disable string functions.
/  1: Enable without LF-CRLF conversion.
/  2: Enable with LF-CRLF conversion. */

 

By changing this to:

 

#define    _USE_STRFUNC    1

 

we have access to the string functions.

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Writing Data

 

We'll add a little bit of code to the SDTest function that does the write.

A full version of the file is attached at the end of this blog. Use that, because it contains the open_append() function that's missing from the version you installed in Post 1.

 

This code will run at the startup of the controller. So you can read back the data from the command line of your terminal.

 

Look for this code in sd_card.c, at line

 

    // Enter an infinite loop for reading and processing commands from the

 

Paste this write function just before it:

 

#define TEST_FILENAME    "WSLOG.TXT"
    FRESULT res;                /* FatFs function common result code */


    // write some info
    FIL fsrc;                /* File objects */


      /* Open  the file for append */
      res = open_append(&fsrc, TEST_FILENAME);
      if (res != FR_OK) {
          /* Error. Cannot create the file */
          while(1);
      }


      // if file empty, write header
      if (! f_size(&fsrc)) {
          res = f_printf(&fsrc, "temperature;humidity;uv\n");
        if (res <0) {
            /* Error. Cannot write header */
            while(1);
        }
      }




      res = f_printf(&fsrc, "%08u;%08u;%08u\n", 1, 2, 3);
      if (res < FR_OK) {
          /* Error. Cannot log data */
          while(1);
      }


      /* Close the file */
      res = f_close(&fsrc);
      if (res != FR_OK)
      {
        /* Error. Cannot close the file */
        while(1);
      }

 

That's it. When you execure the project,

the code will first see if WSLOG.TXT exists in the root of your card.

If it doesn't, it will create it. Then it opens the file in append mode.

 

If the file is empty, we write a line of header information to it, else we skip that step.

 

Then we create one line of data, and we close the file.

 

image

 

You can see if this worked correctly by typing

 

ls

 

and

 

cat WSLOG.TXT

 

in the terminal.

 

Each time you execute the program from start, the file will grow with one line.

This is how it looks like on my LaunchPad:

 

/> cat WSLOG.TXT
temperature;humidity;uv
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003
00000001;00000002;00000003

 

You can import this file in your spreadsheet program, and it'll show nicely in 3 columns

image

 

Enjoy!

 

Related Blog
TI Hercules LaunchPad: using an SD Card Part 1
TI Hercules LaunchPad: using an SD Card Part 2
TI Hercules LaunchPad: using an SD Card Part 3
Attachments:
sd_card.c.zip
  • Sign in to reply

Top Comments

  • Jan Cumps
    Jan Cumps over 8 years ago in reply to tonofre +1
    The Hercules that I'm using here is not related to the TM4C family - the peripherals are different and work different. My code and explanation in this blog is specific for Hercules family of safety microcontrollers…
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to tonofre +1
    I'm afraid I have no more info for you, Thiago. What's in the blog is what I know. For sd_defs.h: have you downloaded the fatfs sourcecode? Don't use the links in my first post because - as indicated …
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to tonofre +1
    The sd_defs.h that I'm using is available (for RM48 Hercules Controller) in the first link of section: Libraries and Dependencies in the first blog of this series. The link point to a zip archive. The…
Parents
  • tonofre
    tonofre over 8 years ago

    Hi,

     

    First, I started using TI sample code and the program worked for opening files from the SDCard.

     

    Then, I downloaded your source.zip and it is missing some header files, such as #include "sd_defs.h"

     

    Please, could you help me?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to tonofre

    Did you follow the instructions from blog post 1 and 2?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • tonofre
    tonofre over 8 years ago in reply to Jan Cumps

    Hi Jan,

     

     

    I am using the TM4123G launchpad, I understand it is different, but technically, if I use Ti drivers for the SPI port, I would be able to follow your examples.

     

    As the TI sample code (sd card example) was working, I started on step 3. I was able to read a file, but I couldn't write yet.

     

    I downloaded your sample code, but in the zip file, it is missing the header files, for example, "sd_defs.h".

     

    Then, I decided to start from the beginning.

     

    1) Downloaded most recent version of fatfs from  http://elm-chan.org/fsw/ff/00index_e.html

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

    3) we now nothing is working, neither TI sample code...

     

    I want to be able to read and write on the SD card.

     

    What should I do?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to tonofre

    The Hercules that I'm using here is not related to the TM4C family - the peripherals are different and work different. My code and explanation in this blog is specific for Hercules family of safety microcontrollers.

    Can you check with someone who's fluent with TIVA / TM4C family?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to tonofre

    The Hercules that I'm using here is not related to the TM4C family - the peripherals are different and work different. My code and explanation in this blog is specific for Hercules family of safety microcontrollers.

    Can you check with someone who's fluent with TIVA / TM4C family?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • tonofre
    tonofre over 8 years ago in reply to Jan Cumps

    Hi Jan, good morning.

     

    The Hercules that I'm using here is not related to the TM4C family - the peripherals are different and work different. My code and explanation in this blog is specific for Hercules family of safety microcontrollers.

    In my understanding, your post part 1 and 2 are specific for the Hercules family. I agree with you. Therefore, your post part 3 is universal.

    Do you agree with me? If you have the  SPI port and the hardware abstraction layer working, the fatfs functions should be universal.

     

     

    In your zip file sd_card.c.zip line 32 there is this declaration:

    #include "sd_defs.h"

     

    You didn't mention it in the previous posts.  Please, could you make this file available?

     

    Can you check with someone who's fluent with TIVA / TM4C family?

    Unfortunately, besides Professor Jonathan Valvano,  I don't know anyone fluent with TIVA family (I have googled it several times).

     

     

    Tiva LaunchPad: Building an Event/Data Logger - Part 2 – Real Time Clock capability

    Jan, this code is for energia/arduino. I am working with C and ccs. Thanks anyways for sharing this extra reference.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to tonofre

    I'm afraid I have no more info for you, Thiago. What's in the blog is what I know.

    For sd_defs.h: have you downloaded the fatfs sourcecode?

    Don't use the links in my first post because - as indicated - they are a port specific for Hercules (in particular the Hercules RM48xxx safety microcontroller).

     

     

    this code is for energia/arduino. I am working with C and ccs.

     

    Converting from energia to CCS is possible. I've done that for two LCD libraries. Not easy, but doable.

     

    Have you tried an TIVA SD card example as a starting point?

    I have seen a right-out-of-the-box example  on the TI-RTOS for TivaC 2.16.00.08:

    FATFS example

     

    Example Summary

    ---------------

    Sample application to read and write data onto a SD Card (SPI interface)

     

    Board Overview

    --------------

    Board_LED0 Indicates that the board was initialized within main()

    Board_SDSPI0 Connection to SD card

     

    That should be a better starting point than my blog on an unrelated controller.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to tonofre

    The sd_defs.h that I'm using is available (for RM48 Hercules Controller) in the first link of section:

    Libraries and Dependencies

    in the first blog of this series.

     

    The link point to a zip archive. The header file is in the RM48_HDK_SD_Card\Library\SDCard folder.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube