SD card library

Dependencies:   SDFileSystem mbed

Fork of 2545_SD_Card by Craig Evans

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* 2545_SD_Card Example
00002 
00003 Example of writing data to SD card.
00004 
00005 Based on FTF2014_lab4 Example
00006 
00007 https://developer.mbed.org/teams/Freescale/wiki/FTF2014_workshop
00008 
00009 Craig A. Evans, University of Leeds, Mar 2016
00010 
00011 */
00012 
00013 #include "mbed.h"
00014 #include "SDFileSystem.h"
00015 
00016 // Connections to SD card holder on K64F (SPI interface)
00017 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
00018 Serial serial(USBTX, USBRX);  // for PC debug
00019 
00020 void delete_file(char filename[]);
00021 /*
00022 int main()
00023 {
00024     serial.baud(115200);  // full-speed!
00025     serial.printf("#### SD Card Example #####\n");
00026     FILE *fp; // this is our file pointer
00027     wait(1);
00028 
00029     // Various examples below - can comment out ones you don't need
00030 
00031     /////////////////////// Deleting file example ////////////////////////
00032 
00033     // comment this line out if you don't want to delete the file!
00034     delete_file("/sd/test.txt");
00035 
00036     ////////////////////// Simple writing example //////////////////////////
00037 
00038     // open file for writing ('w') - creates file if it doesn't exist and overwrites
00039     // if it does. If you wish to add a score onto a list, then you can
00040     // append instead 'a'. This will open the file if it exists and start
00041     // writing at the end. It will create the file if it doesn't exist.
00042     fp = fopen("/sd/topscore.txt", "w");
00043     int top_score = 56;  // random example
00044 
00045     if (fp == NULL) {  // if it can't open the file then print error message
00046         serial.printf("Error! Unable to open file!\n");
00047     } else {  // opened file so can write
00048         serial.printf("Writing to file....");
00049         fprintf(fp, "%d",top_score); // ensure data type matches
00050         serial.printf("Done.\n");
00051         fclose(fp);  // ensure you close the file after writing
00052     }
00053 
00054     ////////////////////// Simple reading example //////////////////////////
00055 
00056     // now open file for reading
00057     fp = fopen("/sd/topscore.txt", "r");
00058     int stored_top_score = -1;  // -1 to demonstrate it has changed after reading
00059 
00060     if (fp == NULL) {  // if it can't open the file then print error message
00061         serial.printf("Error! Unable to open file!\n");
00062     } else {  // opened file so can write
00063         fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&)
00064         serial.printf("Read %d from file.\n",stored_top_score);
00065         fclose(fp);  // ensure you close the file after reading
00066     }
00067 
00068     ///////////////////// Writing list to file example //////////////////////
00069     
00070     // for this example, I'll create some numbers to write to file in a big list
00071     // a data logger for example will usually append to a file - at a reading
00072     // at the end rather than creating a new file
00073     fp = fopen("/sd/test.txt", "a");
00074 
00075     if (fp == NULL) {  // if it can't open the file then print error message
00076         serial.printf("Error! Unable to open file!\n");
00077     } else {  // opened file so can write
00078         serial.printf("Writing to file....");
00079         for(int i = 1; i <= 50; i++) {
00080             float dummy = 1000.0F/i;  // dummy variable
00081             fprintf(fp, "%d,%f\n",i,dummy);  // print formatted string to file (CSV)
00082         }
00083         serial.printf("Done.\n");
00084         fclose(fp);  // ensure you close the file after writing
00085     }
00086     
00087     // you can comment out the writing example to check that the writing has
00088     // worked - when you run it after commenting, it should still open the
00089     // file that exists on the SD card - assuming you didn't delete it!
00090 
00091     /////////////////////// Reading from file example ////////////////////////
00092 
00093     // now open file for reading...note the 'r'
00094     fp = fopen("/sd/test.txt", "r");
00095     if (fp == NULL) {  // if it can't open the file then print error message
00096         serial.printf("Error! Unable to open file!\n");
00097     } else {
00098         serial.printf("Reading file....\n");
00099         int i;    // create suitable variables to store the data in the file
00100         float value;
00101 
00102         // in this example, we keep reading (using fscanf) until we reach
00103         // the 'end of file'. Note we use the address operator & to write
00104         // to the variables. Also the format of the string must match what
00105         // is in the file
00106         while (fscanf(fp, "%d,%f", &i, &value) != EOF) {
00107             serial.printf("%d,%f\n",i,value);
00108         }
00109         serial.printf("Done.\n");
00110         fclose(fp);  // ensure you close the file after reading
00111     }
00112 
00113     ///////////////// Advanced Reading from file example ///////////////////
00114 
00115     // the previous example just read the values into variables and printed to
00116     // serial, we'll now read files into an array.
00117 
00118     // now open file for reading...note the 'r'
00119     fp = fopen("/sd/test.txt", "r");
00120 
00121     int n=0;  // going to store the number of lines in the file
00122     int *index_array;  // pointers to create dynamic arrays later
00123     float *value_array; // note memory will be in heap rather than on the stack
00124 
00125     if (fp == NULL) {  // if it can't open the file then print error message
00126         serial.printf("Error! Unable to open file!\n");
00127     } else {
00128         serial.printf("Counting lines in file....\n");
00129         //Since we may not know the
00130         // number of lines in the files ahead of time, we'll first count them
00131         // * means scan but don't save
00132         while (fscanf(fp, "%*d,%*f") != EOF) {
00133             n++;  // increment counter when read a line
00134         }
00135 
00136 
00137         serial.printf("Read %d lines\n",n);
00138         serial.printf("Creating dynamic arrays...\n");
00139         // calloc creates an array and initilises to 0
00140         // malloc returns unitialised array - diffrent syntax
00141         index_array = (int *)calloc(n, sizeof (int));
00142         value_array = (float *)calloc(n, sizeof (float));
00143 
00144         int i=0;
00145         rewind(fp); // 'scrolled' to end of file, so go back to beginning
00146         serial.printf("Reading into arrays...\n");
00147         while (fscanf(fp, "%d,%f",&index_array[i],&value_array[i]) != EOF) {
00148             i++;  // read data into array and increment index
00149         }
00150         serial.printf("Done.\n");
00151         fclose(fp);  // ensure you close the file after reading
00152     }
00153     
00154     // we should now have the data in the arrays, will print to serial to check
00155     for(int i=0; i<n ; i++) {
00156         serial.printf("[%d] %d,%f\n",i,index_array[i],value_array[i]);
00157     } 
00158 
00159     ///////////////////////////////////////////////////
00160     serial.printf("End of SD card example\n");
00161 }
00162 
00163 void delete_file(char filename[])
00164 {
00165     serial.printf("Deleting file '%s'...",filename);
00166     FILE *fp = fopen(filename, "r");  // try and open file
00167     if (fp != NULL) {  // if it does open...
00168         fclose(fp);    // close it
00169         remove(filename);  // and then delete
00170         serial.printf("Done!\n");
00171     }
00172     // if we can't open it, it doesn't exist and so we can't delete it
00173 }
00174 */