SD card with the file system.
Dependencies: SDFileSystem mbed
Fork of FTF2014_lab4 by
main.cpp@2:41dd6f412728, 2014-04-07 (annotated)
- Committer:
- Kojto
- Date:
- Mon Apr 07 19:36:26 2014 +0000
- Revision:
- 2:41dd6f412728
- Parent:
- 0:a83db87be46c
mbed-src replaced by mbed lib v82
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 0:a83db87be46c | 1 | #include "mbed.h" |
Kojto | 0:a83db87be46c | 2 | #include "SDFileSystem.h" |
Kojto | 0:a83db87be46c | 3 | |
Kojto | 0:a83db87be46c | 4 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
Kojto | 0:a83db87be46c | 5 | Serial pc(USBTX, USBRX); |
Kojto | 0:a83db87be46c | 6 | FILE *fp; |
Kojto | 0:a83db87be46c | 7 | char buffer[1024]; |
Kojto | 0:a83db87be46c | 8 | |
Kojto | 0:a83db87be46c | 9 | int main() { |
Kojto | 0:a83db87be46c | 10 | pc.printf("Initializing \n"); |
Kojto | 0:a83db87be46c | 11 | wait(2); |
Kojto | 0:a83db87be46c | 12 | |
Kojto | 0:a83db87be46c | 13 | fp = fopen("/sd/hello.txt", "r"); |
Kojto | 0:a83db87be46c | 14 | if (fp != NULL) { |
Kojto | 0:a83db87be46c | 15 | fclose(fp); |
Kojto | 0:a83db87be46c | 16 | remove("/sd/hello.txt"); |
Kojto | 0:a83db87be46c | 17 | pc.printf("Remove an existing file with the same name \n"); |
Kojto | 0:a83db87be46c | 18 | } |
Kojto | 0:a83db87be46c | 19 | |
Kojto | 0:a83db87be46c | 20 | printf("\nWriting data to the sd card \n"); |
Kojto | 0:a83db87be46c | 21 | fp = fopen("/sd/hello.txt", "w"); |
Kojto | 0:a83db87be46c | 22 | if (fp == NULL) { |
Kojto | 0:a83db87be46c | 23 | pc.printf("Unable to write the file \n"); |
Kojto | 0:a83db87be46c | 24 | } else { |
Kojto | 0:a83db87be46c | 25 | fprintf(fp, "mbed SDCard application!"); |
Kojto | 0:a83db87be46c | 26 | fclose(fp); |
Kojto | 0:a83db87be46c | 27 | pc.printf("File successfully written! \n"); |
Kojto | 0:a83db87be46c | 28 | } |
Kojto | 0:a83db87be46c | 29 | |
Kojto | 0:a83db87be46c | 30 | printf("\nReading data from the SD card. \n"); |
Kojto | 0:a83db87be46c | 31 | fp = fopen("/sd/hello.txt", "r"); |
Kojto | 0:a83db87be46c | 32 | if (fp != NULL) { |
Kojto | 0:a83db87be46c | 33 | int size = fread(buffer, sizeof(char), 1024, fp); |
Kojto | 0:a83db87be46c | 34 | printf("Number of data read: %d, text from hello.txt file: %s \n", size, buffer); |
Kojto | 0:a83db87be46c | 35 | fclose(fp); |
Kojto | 0:a83db87be46c | 36 | } |
Kojto | 0:a83db87be46c | 37 | printf("End of Lab 4. \n"); |
Kojto | 0:a83db87be46c | 38 | } |