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 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.
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).
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.
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.
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
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 |
Top Comments