![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LED.H
Dependencies: ELEC350-Practicals-FZ429
Fork of ELEC350-SD_Card_Demo by
Diff: main.cpp
- Revision:
- 2:f1b3a82f7738
- Parent:
- 0:1ce5a958aaf8
- Child:
- 3:528e42a15d44
diff -r d3ed3e830dd1 -r f1b3a82f7738 main.cpp --- a/main.cpp Fri Oct 20 11:30:34 2017 +0000 +++ b/main.cpp Mon Dec 04 16:33:51 2017 +0000 @@ -1,41 +1,82 @@ +// NickO 2017 /* Access an SD Card using SPI */ #include "mbed.h" #include "SDBlockDevice.h" #include "FATFileSystem.h" + #include "sample_hardware.hpp" + //SD Card Object SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs uint8_t block[512] = "Hello World!\n"; int main() { - printf("Initialise and write to a file\n"); + //POWER ON SELF TEST + post(); + + printf("Initialise\n"); //FileSystemLike(*sd); - // call the SDBlockDevice instance initialisation method. - if ( 0 != sd.init()) { + if ( sd.init() != 0) { printf("Init failed \n"); - return -1; - } - //FileSystemLike(*sd); + errorCode(FATAL); + } + //Create a filing system for SD Card FATFileSystem fs("sd", &sd); - - FILE *fp = fopen("/sd/test.txt","w"); - if(fp == NULL) { + // ************* + // Open to WRITE + // ************* + printf("Write to a file\n"); + FILE* fp = fopen("/sd/test.txt","w"); + //Check file handle (stream) + if (fp == NULL) { error("Could not open file for write\n"); + errorCode(FATAL); } //Put some text in the file... - fprintf(fp, "Martin Says Hi! One for the good guys\n"); + fprintf(fp, "Welcome to ELEC350\n"); + + //Close the file + fclose(fp); - //Tidy up here + // ************ + // Open to READ + // ************ + printf("Read a file\n"); + fp = fopen("/sd/test.txt","r"); + if (fp == NULL) { + error("Could not open file for read\n"); + errorCode(FATAL); + } + + //Read in three strings + char s1[64], s2[64], s3[64]; + fscanf(fp, "%s %s %s", s1,s2,s3); + //To read a whole line, use: fgets(s1, sizeof(s1), fp); + printf("READ BACK: %s %s %s\n", s1, s2, s3); + + //Close File fclose(fp); + + //Close down sd.deinit(); printf("All done...\n"); + errorCode(OK); + //Flash to indicate goodness + while(true) { + greenLED = 1; + wait(0.5); + greenLED = 0; + wait(0.1); + } +} + /* printf("sd size: %llu\n", sd.size()); printf("sd read size: %llu\n", sd.get_read_size()); @@ -63,5 +104,4 @@ // call the SDBlockDevice instance de-initialisation method. sd.deinit(); - */ -} \ No newline at end of file + */ \ No newline at end of file