real honest

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(PB_5, D12, D13, D10); // mosi, miso, sclk, cs
00010 
00011 // Data type 
00012 
00013 
00014  int main()
00015 {
00016     printf("Initialise\n");
00017     //FileSystemLike(*sd);
00018 
00019     // call the SDBlockDevice instance initialisation method.
00020     if ( sd.init() != 0) {
00021         printf("Init failed \n");
00022         errorCode(FATAL);
00023     }    
00024     
00025     //Create a filing system for SD Card
00026     FATFileSystem fs("sd", &sd);
00027     
00028     // *************
00029     // Open to WRITE
00030     // *************
00031     printf("Write to a file\n");
00032     FILE* fp = fopen("/sd/test.txt","a");
00033     //Check file handle (stream)
00034     if (fp == NULL) {
00035         error("Could not open file for write\n");
00036         errorCode(FATAL);
00037     }
00038     
00039     /*
00040     
00041     
00042     //Put some text in the file...
00043     //This 
00044     if ( 0 == sd.program(block, 0, 512)) {
00045     fprintf(fp, "Welcome to ELEC350\n");
00046     }
00047     
00048     
00049     */
00050     
00051     if (onBoardSwitch == 1) {
00052         press = true;
00053         }
00054         
00055         if (press == true) {
00056         
00057         //Close File
00058         fclose(fp);
00059         
00060         //Close down SD card (flush buffers)
00061         sd.deinit();
00062         
00063         //Let user know     
00064         puts("You can now remove the SD Card\n");
00065       
00066       for (unsigned int n=0; n<2; n++) {
00067         onBoardLED = 1;
00068         wait(0.2);
00069         onBoardLED = 0;
00070         wait(0.2);
00071         }
00072         
00073         //Flag to record the event
00074         ejected = true;
00075     }
00076     
00077     //Close the file
00078     fclose(fp);
00079     
00080     //Close down
00081     sd.deinit();
00082     
00083     printf("All done...\n");
00084     errorCode(OK);
00085     
00086     //Flash to indicate goodness
00087     while(true) {
00088         greenLED = 1;
00089         wait(0.5);
00090         greenLED = 0;
00091         wait(0.1);    
00092     }
00093 }
00094 
00095    /* 
00096     printf("sd erase size: %llu\n",   sd.get_erase_size());
00097 
00098     if ( 0 != sd.erase(0, sd.get_erase_size())) {
00099         printf("Error Erasing block \n");
00100     }
00101 
00102     // Write some the data block to the device
00103     if ( 0 == sd.program(block, 0, 512)) {
00104         // read the data block from the device
00105         if ( 0 == sd.read(block, 0, 512)) {
00106             // print the contents of the block
00107             printf("%s", block);
00108         }
00109     }
00110     */