Example of using the SDFileSystem library on a K64F to write data to files and also read data into dynamically created arrays.
main.cpp@1:caceb9d9d17a, 2020-02-03 (annotated)
- Committer:
- eencae
- Date:
- Mon Feb 03 12:06:21 2020 +0000
- Revision:
- 1:caceb9d9d17a
- Parent:
- 0:5448330e1a33
Updated Mbed library. Removed serial. Confirmed working Jan 2020.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:5448330e1a33 | 1 | /* 2545_SD_Card Example |
eencae | 0:5448330e1a33 | 2 | |
eencae | 0:5448330e1a33 | 3 | Example of writing data to SD card. |
eencae | 0:5448330e1a33 | 4 | |
eencae | 0:5448330e1a33 | 5 | Based on FTF2014_lab4 Example |
eencae | 0:5448330e1a33 | 6 | |
eencae | 0:5448330e1a33 | 7 | https://developer.mbed.org/teams/Freescale/wiki/FTF2014_workshop |
eencae | 0:5448330e1a33 | 8 | |
eencae | 0:5448330e1a33 | 9 | Craig A. Evans, University of Leeds, Mar 2016 |
eencae | 0:5448330e1a33 | 10 | |
eencae | 0:5448330e1a33 | 11 | */ |
eencae | 0:5448330e1a33 | 12 | |
eencae | 0:5448330e1a33 | 13 | #include "mbed.h" |
eencae | 0:5448330e1a33 | 14 | #include "SDFileSystem.h" |
eencae | 0:5448330e1a33 | 15 | |
eencae | 0:5448330e1a33 | 16 | // Connections to SD card holder on K64F (SPI interface) |
eencae | 0:5448330e1a33 | 17 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
eencae | 0:5448330e1a33 | 18 | |
eencae | 0:5448330e1a33 | 19 | void delete_file(char filename[]); |
eencae | 0:5448330e1a33 | 20 | |
eencae | 0:5448330e1a33 | 21 | int main() |
eencae | 0:5448330e1a33 | 22 | { |
eencae | 1:caceb9d9d17a | 23 | printf("#### SD Card Example #####\n"); |
eencae | 0:5448330e1a33 | 24 | FILE *fp; // this is our file pointer |
eencae | 0:5448330e1a33 | 25 | wait(1); |
eencae | 0:5448330e1a33 | 26 | |
eencae | 0:5448330e1a33 | 27 | // Various examples below - can comment out ones you don't need |
eencae | 0:5448330e1a33 | 28 | |
eencae | 0:5448330e1a33 | 29 | /////////////////////// Deleting file example //////////////////////// |
eencae | 0:5448330e1a33 | 30 | |
eencae | 0:5448330e1a33 | 31 | // comment this line out if you don't want to delete the file! |
eencae | 0:5448330e1a33 | 32 | delete_file("/sd/test.txt"); |
eencae | 0:5448330e1a33 | 33 | |
eencae | 0:5448330e1a33 | 34 | ////////////////////// Simple writing example ////////////////////////// |
eencae | 0:5448330e1a33 | 35 | |
eencae | 0:5448330e1a33 | 36 | // open file for writing ('w') - creates file if it doesn't exist and overwrites |
eencae | 0:5448330e1a33 | 37 | // if it does. If you wish to add a score onto a list, then you can |
eencae | 0:5448330e1a33 | 38 | // append instead 'a'. This will open the file if it exists and start |
eencae | 0:5448330e1a33 | 39 | // writing at the end. It will create the file if it doesn't exist. |
eencae | 0:5448330e1a33 | 40 | fp = fopen("/sd/topscore.txt", "w"); |
eencae | 0:5448330e1a33 | 41 | int top_score = 56; // random example |
eencae | 0:5448330e1a33 | 42 | |
eencae | 0:5448330e1a33 | 43 | if (fp == NULL) { // if it can't open the file then print error message |
eencae | 1:caceb9d9d17a | 44 | printf("Error! Unable to open file!\n"); |
eencae | 0:5448330e1a33 | 45 | } else { // opened file so can write |
eencae | 1:caceb9d9d17a | 46 | printf("Writing to file...."); |
eencae | 0:5448330e1a33 | 47 | fprintf(fp, "%d",top_score); // ensure data type matches |
eencae | 1:caceb9d9d17a | 48 | printf("Done.\n"); |
eencae | 0:5448330e1a33 | 49 | fclose(fp); // ensure you close the file after writing |
eencae | 0:5448330e1a33 | 50 | } |
eencae | 0:5448330e1a33 | 51 | |
eencae | 0:5448330e1a33 | 52 | ////////////////////// Simple reading example ////////////////////////// |
eencae | 0:5448330e1a33 | 53 | |
eencae | 0:5448330e1a33 | 54 | // now open file for reading |
eencae | 0:5448330e1a33 | 55 | fp = fopen("/sd/topscore.txt", "r"); |
eencae | 0:5448330e1a33 | 56 | int stored_top_score = -1; // -1 to demonstrate it has changed after reading |
eencae | 0:5448330e1a33 | 57 | |
eencae | 0:5448330e1a33 | 58 | if (fp == NULL) { // if it can't open the file then print error message |
eencae | 1:caceb9d9d17a | 59 | printf("Error! Unable to open file!\n"); |
eencae | 0:5448330e1a33 | 60 | } else { // opened file so can write |
eencae | 0:5448330e1a33 | 61 | fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) |
eencae | 1:caceb9d9d17a | 62 | printf("Read %d from file.\n",stored_top_score); |
eencae | 0:5448330e1a33 | 63 | fclose(fp); // ensure you close the file after reading |
eencae | 0:5448330e1a33 | 64 | } |
eencae | 0:5448330e1a33 | 65 | |
eencae | 0:5448330e1a33 | 66 | ///////////////////// Writing list to file example ////////////////////// |
eencae | 0:5448330e1a33 | 67 | |
eencae | 0:5448330e1a33 | 68 | // for this example, I'll create some numbers to write to file in a big list |
eencae | 0:5448330e1a33 | 69 | // a data logger for example will usually append to a file - at a reading |
eencae | 0:5448330e1a33 | 70 | // at the end rather than creating a new file |
eencae | 0:5448330e1a33 | 71 | fp = fopen("/sd/test.txt", "a"); |
eencae | 0:5448330e1a33 | 72 | |
eencae | 0:5448330e1a33 | 73 | if (fp == NULL) { // if it can't open the file then print error message |
eencae | 1:caceb9d9d17a | 74 | printf("Error! Unable to open file!\n"); |
eencae | 0:5448330e1a33 | 75 | } else { // opened file so can write |
eencae | 1:caceb9d9d17a | 76 | printf("Writing to file...."); |
eencae | 0:5448330e1a33 | 77 | for(int i = 1; i <= 50; i++) { |
eencae | 0:5448330e1a33 | 78 | float dummy = 1000.0F/i; // dummy variable |
eencae | 0:5448330e1a33 | 79 | fprintf(fp, "%d,%f\n",i,dummy); // print formatted string to file (CSV) |
eencae | 0:5448330e1a33 | 80 | } |
eencae | 1:caceb9d9d17a | 81 | printf("Done.\n"); |
eencae | 0:5448330e1a33 | 82 | fclose(fp); // ensure you close the file after writing |
eencae | 0:5448330e1a33 | 83 | } |
eencae | 0:5448330e1a33 | 84 | |
eencae | 0:5448330e1a33 | 85 | // you can comment out the writing example to check that the writing has |
eencae | 0:5448330e1a33 | 86 | // worked - when you run it after commenting, it should still open the |
eencae | 0:5448330e1a33 | 87 | // file that exists on the SD card - assuming you didn't delete it! |
eencae | 0:5448330e1a33 | 88 | |
eencae | 0:5448330e1a33 | 89 | /////////////////////// Reading from file example //////////////////////// |
eencae | 0:5448330e1a33 | 90 | |
eencae | 0:5448330e1a33 | 91 | // now open file for reading...note the 'r' |
eencae | 0:5448330e1a33 | 92 | fp = fopen("/sd/test.txt", "r"); |
eencae | 0:5448330e1a33 | 93 | if (fp == NULL) { // if it can't open the file then print error message |
eencae | 1:caceb9d9d17a | 94 | printf("Error! Unable to open file!\n"); |
eencae | 0:5448330e1a33 | 95 | } else { |
eencae | 1:caceb9d9d17a | 96 | printf("Reading file....\n"); |
eencae | 0:5448330e1a33 | 97 | int i; // create suitable variables to store the data in the file |
eencae | 0:5448330e1a33 | 98 | float value; |
eencae | 0:5448330e1a33 | 99 | |
eencae | 0:5448330e1a33 | 100 | // in this example, we keep reading (using fscanf) until we reach |
eencae | 0:5448330e1a33 | 101 | // the 'end of file'. Note we use the address operator & to write |
eencae | 0:5448330e1a33 | 102 | // to the variables. Also the format of the string must match what |
eencae | 0:5448330e1a33 | 103 | // is in the file |
eencae | 0:5448330e1a33 | 104 | while (fscanf(fp, "%d,%f", &i, &value) != EOF) { |
eencae | 1:caceb9d9d17a | 105 | printf("%d,%f\n",i,value); |
eencae | 0:5448330e1a33 | 106 | } |
eencae | 1:caceb9d9d17a | 107 | printf("Done.\n"); |
eencae | 0:5448330e1a33 | 108 | fclose(fp); // ensure you close the file after reading |
eencae | 0:5448330e1a33 | 109 | } |
eencae | 0:5448330e1a33 | 110 | |
eencae | 0:5448330e1a33 | 111 | ///////////////// Advanced Reading from file example /////////////////// |
eencae | 0:5448330e1a33 | 112 | |
eencae | 0:5448330e1a33 | 113 | // the previous example just read the values into variables and printed to |
eencae | 0:5448330e1a33 | 114 | // serial, we'll now read files into an array. |
eencae | 0:5448330e1a33 | 115 | |
eencae | 0:5448330e1a33 | 116 | // now open file for reading...note the 'r' |
eencae | 0:5448330e1a33 | 117 | fp = fopen("/sd/test.txt", "r"); |
eencae | 0:5448330e1a33 | 118 | |
eencae | 0:5448330e1a33 | 119 | int n=0; // going to store the number of lines in the file |
eencae | 0:5448330e1a33 | 120 | int *index_array; // pointers to create dynamic arrays later |
eencae | 0:5448330e1a33 | 121 | float *value_array; // note memory will be in heap rather than on the stack |
eencae | 0:5448330e1a33 | 122 | |
eencae | 0:5448330e1a33 | 123 | if (fp == NULL) { // if it can't open the file then print error message |
eencae | 1:caceb9d9d17a | 124 | printf("Error! Unable to open file!\n"); |
eencae | 0:5448330e1a33 | 125 | } else { |
eencae | 1:caceb9d9d17a | 126 | printf("Counting lines in file....\n"); |
eencae | 0:5448330e1a33 | 127 | //Since we may not know the |
eencae | 0:5448330e1a33 | 128 | // number of lines in the files ahead of time, we'll first count them |
eencae | 0:5448330e1a33 | 129 | // * means scan but don't save |
eencae | 0:5448330e1a33 | 130 | while (fscanf(fp, "%*d,%*f") != EOF) { |
eencae | 0:5448330e1a33 | 131 | n++; // increment counter when read a line |
eencae | 0:5448330e1a33 | 132 | } |
eencae | 0:5448330e1a33 | 133 | |
eencae | 0:5448330e1a33 | 134 | |
eencae | 1:caceb9d9d17a | 135 | printf("Read %d lines\n",n); |
eencae | 1:caceb9d9d17a | 136 | printf("Creating dynamic arrays...\n"); |
eencae | 0:5448330e1a33 | 137 | // calloc creates an array and initilises to 0 |
eencae | 0:5448330e1a33 | 138 | // malloc returns unitialised array - diffrent syntax |
eencae | 0:5448330e1a33 | 139 | index_array = (int *)calloc(n, sizeof (int)); |
eencae | 0:5448330e1a33 | 140 | value_array = (float *)calloc(n, sizeof (float)); |
eencae | 0:5448330e1a33 | 141 | |
eencae | 0:5448330e1a33 | 142 | int i=0; |
eencae | 0:5448330e1a33 | 143 | rewind(fp); // 'scrolled' to end of file, so go back to beginning |
eencae | 1:caceb9d9d17a | 144 | printf("Reading into arrays...\n"); |
eencae | 0:5448330e1a33 | 145 | while (fscanf(fp, "%d,%f",&index_array[i],&value_array[i]) != EOF) { |
eencae | 0:5448330e1a33 | 146 | i++; // read data into array and increment index |
eencae | 0:5448330e1a33 | 147 | } |
eencae | 1:caceb9d9d17a | 148 | printf("Done.\n"); |
eencae | 0:5448330e1a33 | 149 | fclose(fp); // ensure you close the file after reading |
eencae | 0:5448330e1a33 | 150 | } |
eencae | 0:5448330e1a33 | 151 | |
eencae | 0:5448330e1a33 | 152 | // we should now have the data in the arrays, will print to serial to check |
eencae | 0:5448330e1a33 | 153 | for(int i=0; i<n ; i++) { |
eencae | 1:caceb9d9d17a | 154 | printf("[%d] %d,%f\n",i,index_array[i],value_array[i]); |
eencae | 0:5448330e1a33 | 155 | } |
eencae | 0:5448330e1a33 | 156 | |
eencae | 0:5448330e1a33 | 157 | /////////////////////////////////////////////////// |
eencae | 1:caceb9d9d17a | 158 | printf("End of SD card example\n"); |
eencae | 0:5448330e1a33 | 159 | } |
eencae | 0:5448330e1a33 | 160 | |
eencae | 0:5448330e1a33 | 161 | void delete_file(char filename[]) |
eencae | 0:5448330e1a33 | 162 | { |
eencae | 1:caceb9d9d17a | 163 | printf("Deleting file '%s'...",filename); |
eencae | 0:5448330e1a33 | 164 | FILE *fp = fopen(filename, "r"); // try and open file |
eencae | 0:5448330e1a33 | 165 | if (fp != NULL) { // if it does open... |
eencae | 0:5448330e1a33 | 166 | fclose(fp); // close it |
eencae | 0:5448330e1a33 | 167 | remove(filename); // and then delete |
eencae | 1:caceb9d9d17a | 168 | printf("Done!\n"); |
eencae | 0:5448330e1a33 | 169 | } |
eencae | 0:5448330e1a33 | 170 | // if we can't open it, it doesn't exist and so we can't delete it |
eencae | 0:5448330e1a33 | 171 | } |