element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Achievement Levels
    • Benefits of Membership
    • Feedback and Support
    • Members Area
    • Personal Blogs
    • What's New on element14
  • Learn
    Learn
    • eBooks
    • Learning Center
    • Learning Groups
    • STEM Academy
    • Webinars, Training and Events
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Arduino Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Project Groups
    • Raspberry Pi Projects
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs TI Hercules LaunchPad: using an SD Card Part 3
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 8 Oct 2015 6:24 PM Date Created
  • Views 2165 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 6 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 6 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 6 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
  • Jan Cumps
    Jan Cumps over 6 years ago

    I'll have to recheck, because I'm getting the same error:

    Hercules SDCARD Demo
     Type 'help' for help.
    Catalog MCU, qjwang@ti.com 
    f_mount error: FR_NOT_READY

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

    Ah, I removed the card put it back and reset the Hercules:

    Hercules SDCARD Demo
     Type 'help' for help.
    Catalog MCU, qjwang@ti.com
    f_mount error: FR_NO_FILESYSTEM

     

    That's better.

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

    I formatted the card and it works.

    /> help
    help
    
    Available commands
    ------------------
    help  : Display list of commands
    h     : alias for help
    ?     : alias for help
    ls    : Display list of files
    chdir : Change directory
    cd    : alias for chdir
    pwd   : Show current working directory
    cat   : Show contents of a text file
    
    /> ls
    ls
    
    D-HS- 2017/09/30 12:15         0  SYSTEM~1
    ----A 2007/06/05 11:38        51  WSLOG.TXT
    
       1 File(s),        51 bytes total
       1 Dir(s),  615575916K bytes free
    
    /> cat wslog.txt
    cat wslog.txt
    temperature;humidity;uv
    00000001;00000002;00000003
    
    />

     

    Is there something specific you want me to look at or probe?

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

    I formatted the card and it works.

    /> help
    help
    
    Available commands
    ------------------
    help  : Display list of commands
    h     : alias for help
    ?     : alias for help
    ls    : Display list of files
    chdir : Change directory
    cd    : alias for chdir
    pwd   : Show current working directory
    cat   : Show contents of a text file
    
    /> ls
    ls
    
    D-HS- 2017/09/30 12:15         0  SYSTEM~1
    ----A 2007/06/05 11:38        51  WSLOG.TXT
    
       1 File(s),        51 bytes total
       1 Dir(s),  615575916K bytes free
    
    /> cat wslog.txt
    cat wslog.txt
    temperature;humidity;uv
    00000001;00000002;00000003
    
    />

     

    Is there something specific you want me to look at or probe?

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

    Here's the setup:

    image

     

    (also showing two other Herculeses for a CAN bus experiment)

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

    .... and this is how I wired the LaunchPad's pins to the SD Card module:

    image

     

    The black labels match with the silkscreen on the PCB (an LC Studio breakout. It was part of a set of components that I bought from someone who gave up the electronics hobby)

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

    I'll give the reset approach a try. Hopefully, it works (*fingers crossed*). Aside from that, here are some more questions/concerns regarding the project.

     

    QUESTIONS:

    1) What file system did you format your SD card with? (Mine is currently formatted for FAT32)

    2) Did you change anything on the project that you downloaded from the github?

    3) Regarding the send_cmd() function. Just to clarify, this function sends the CMD and arg parameters, and returns the received response... Correct?

    4) I don’t think I fully understand the logic that is happening when CMD41 is being sent. My question involves the logic in the comparison found in this line of code:

     

         if (send_cmd(CMD55, 0) <= 1 && send_cmd(CMD41, 1UL << 30) == 0) break; /* ACMD41 with HCS bit */

     

         To be more specific, my question is about the arg value of CMD41. What does 1UL << 30 represent/what is it doing? My understanding from what I have read is that it’s stuffing 30 bits at a location of 1UL... but I don't know what 1UL represents, so I'm pretty confused about this.

     

    POTENTIAL PROBLEM:

    I'm attempting to bit bang a different pin (CS[5] of MIBSPI3). If I understand this correctly, this is done in the SELECT() and DESELECT() functions where DSET and DCLR are set to 0x01 (which represents CS[0]). In order to bit bang the CS[5] pin of MIBSPI3, I changed this value to 0x20 (0010 0000) which should represent the CS[5] pin of MIBSPI3, assuming that each bit in this hex value represents a specific CS pin (i.e. 0x01 = CS[0], 0x02 = CS[1], 0x04 = CS[2], etc.0). However, I'm not entirely sure if I am interpreting the logic correctly. If you didn't change the github project that I linked you, then my understanding of the code _spiPORT->DSET and _spiPORT->DCLR are wrong.

     

    **EDIT: I found someone who was experiencing a similar issue while attempting to use an SDHC card. He stated that the command sequence "progressed properly until CMD55 (including experiencing a 0x01 R1 response from CMD8)". He apparently figured out the problem and fixed it saying that "You have to read the full R7 from CMD8, otherwise seemingly SDHC cards deadlock in some manner so they are no longer accessible at all (I accidentally omitted reading beyond the R1)." I am going to try this as well and see if it works. Here is a link to the referenced forum page:

     

    https://electronics.stackexchange.com/questions/321491/why-i-cant-issue-commands-after-cmd8-to-an-sdhc-card-in-spi-mode

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

    1) What file system did you format your SD card with? (Mine is currently formatted for FAT32)

     

    fat32

     

    2) Did you change anything on the project that you downloaded from the github?

     

    I haven't downloaded anything from github this time. I'm using the project that was the source for this blog series. Are you refering to my own github or another one?

     

    3) Regarding the send_cmd() function. Just to clarify, this function sends the CMD and arg parameters, and returns the received response... Correct?

    4) I don’t think I fully understand the logic that is happening when CMD41 is being sent. My question involves the logic in the comparison found in this line of code

     

    I'm not an expert on that subject.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jc2048
    jc2048 over 6 years ago in reply to caw4

    1UL << 30 means take 1 (as an unsigned long - probably 32 bits on your processor) and shift it left 30 times.

     

    So you're handing send_cmd a second parameter of 0x40000000. If you've got the code, you should be able to see what it does with it.

     

    CMD41 queries the OCR register in the card. (CMD55 is there to signal that there is an application specific command following.)

     

    Bit 30 of that register happens to be the Card Capacity Status bit. If it's 0 then it's SDSC. If it's 1 then it's SDHC or SDXC. So I would guess [you'd have to work through the code to be sure] it's all part of trying to determine the card type it's talking to and its capabilities.

    • Cancel
    • Vote Up 0 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 © 2023 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