Elec351 MMB / Mbed OS SD_CARD_TWO

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task680solution-mbed-os-FZ429ZI by University of Plymouth - Stages 1, 2 and 3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Access an SD Card using SPI */
00002  
00003  #include "mbed.h"
00004  #include "SDBlockDevice.h"
00005  #include "FATFileSystem.h"
00006  #include "sample_hardware.hpp"
00007  
00008  //SD Card Object
00009  SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
00010 
00011 // DATA TYPE
00012 
00013 
00014 
00015 
00016  int main()
00017 {
00018     
00019     printf("Initialise\n");
00020     //FileSystemLike(*sd);
00021 
00022     // call the SDBlockDevice instance initialisation method. (not needed)
00023     if ( sd.init() != 0) {
00024         printf("Init failed \n");
00025         errorCode(FATAL);
00026     }    
00027     
00028     //Create a filing system for SD Card
00029     FATFileSystem fs("sd", &sd);
00030     
00031     // ************
00032     // Open to READ
00033     // ************
00034     printf("Read a file\n");
00035     FILE* fp = fopen("/sd/test.txt","r");
00036     
00037     if (fp == NULL) {
00038         error("Could not open file for read\n");
00039         errorCode(FATAL);
00040     }   
00041     
00042     //Read back all strings
00043     char s1[64];
00044     while (fscanf(fp, "%s", s1) == 1) {
00045         fgets(s1, sizeof(s1), fp);
00046     }
00047     //To read a whole line, use: fgets(s1, sizeof(s1), fp);
00048     
00049     
00050     //Close File
00051     fclose(fp);
00052     
00053     //Close down
00054     sd.deinit();
00055     printf("All done...\n");
00056     errorCode(OK);
00057     
00058     //Flash to indicate goodness
00059     while(true) {
00060         greenLED = 1;
00061         wait(0.5);
00062         greenLED = 0;
00063         wait(0.1);    
00064     }
00065 }
00066 
00067    /* 
00068     printf("sd size: %llu\n",         sd.size());
00069     printf("sd read size: %llu\n",    sd.get_read_size());
00070     printf("sd program size: %llu\n", sd.get_program_size());
00071     printf("sd erase size: %llu\n",   sd.get_erase_size());
00072 
00073     // set the frequency
00074     if ( 0 != sd.frequency(5000000)) {
00075         printf("Error setting frequency \n");
00076     }
00077 
00078     if ( 0 != sd.erase(0, sd.get_erase_size())) {
00079         printf("Error Erasing block \n");
00080     }
00081 
00082     // Write some the data block to the device
00083     if ( 0 == sd.program(block, 0, 512)) {
00084         // read the data block from the device
00085         if ( 0 == sd.read(block, 0, 512)) {
00086             // print the contents of the block
00087             printf("%s", block);
00088         }
00089     }
00090 
00091     // call the SDBlockDevice instance de-initialisation method.
00092     
00093     sd.deinit();
00094     */