real honest

Dependencies:   ELEC350-Practicals-FZ429

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

Committer:
rubbehducky
Date:
Sun Jan 07 19:45:04 2018 +0000
Revision:
5:49bcc61caa2c
Parent:
4:958cfd74ef1c
dodododo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martinsimpson 0:1ce5a958aaf8 1 /* Access an SD Card using SPI */
martinsimpson 0:1ce5a958aaf8 2
martinsimpson 0:1ce5a958aaf8 3 #include "mbed.h"
martinsimpson 0:1ce5a958aaf8 4 #include "SDBlockDevice.h"
martinsimpson 0:1ce5a958aaf8 5 #include "FATFileSystem.h"
noutram 2:fad34c30dcc4 6 #include "sample_hardware.hpp"
martinsimpson 0:1ce5a958aaf8 7
noutram 2:fad34c30dcc4 8 //SD Card Object
noutram 4:958cfd74ef1c 9 SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs
martinsimpson 0:1ce5a958aaf8 10
rubbehducky 5:49bcc61caa2c 11 // Data type
rubbehducky 5:49bcc61caa2c 12
rubbehducky 5:49bcc61caa2c 13
martinsimpson 0:1ce5a958aaf8 14 int main()
martinsimpson 0:1ce5a958aaf8 15 {
noutram 2:fad34c30dcc4 16 printf("Initialise\n");
martinsimpson 0:1ce5a958aaf8 17 //FileSystemLike(*sd);
martinsimpson 0:1ce5a958aaf8 18
martinsimpson 0:1ce5a958aaf8 19 // call the SDBlockDevice instance initialisation method.
noutram 2:fad34c30dcc4 20 if ( sd.init() != 0) {
martinsimpson 0:1ce5a958aaf8 21 printf("Init failed \n");
noutram 2:fad34c30dcc4 22 errorCode(FATAL);
noutram 2:fad34c30dcc4 23 }
martinsimpson 0:1ce5a958aaf8 24
noutram 2:fad34c30dcc4 25 //Create a filing system for SD Card
martinsimpson 0:1ce5a958aaf8 26 FATFileSystem fs("sd", &sd);
martinsimpson 0:1ce5a958aaf8 27
noutram 2:fad34c30dcc4 28 // *************
noutram 2:fad34c30dcc4 29 // Open to WRITE
noutram 2:fad34c30dcc4 30 // *************
noutram 2:fad34c30dcc4 31 printf("Write to a file\n");
noutram 3:13716f14b257 32 FILE* fp = fopen("/sd/test.txt","a");
noutram 2:fad34c30dcc4 33 //Check file handle (stream)
noutram 2:fad34c30dcc4 34 if (fp == NULL) {
martinsimpson 0:1ce5a958aaf8 35 error("Could not open file for write\n");
noutram 2:fad34c30dcc4 36 errorCode(FATAL);
martinsimpson 0:1ce5a958aaf8 37 }
martinsimpson 0:1ce5a958aaf8 38
rubbehducky 5:49bcc61caa2c 39 /*
rubbehducky 5:49bcc61caa2c 40
rubbehducky 5:49bcc61caa2c 41
martinsimpson 0:1ce5a958aaf8 42 //Put some text in the file...
rubbehducky 5:49bcc61caa2c 43 //This
rubbehducky 5:49bcc61caa2c 44 if ( 0 == sd.program(block, 0, 512)) {
noutram 2:fad34c30dcc4 45 fprintf(fp, "Welcome to ELEC350\n");
rubbehducky 5:49bcc61caa2c 46 }
rubbehducky 5:49bcc61caa2c 47
rubbehducky 5:49bcc61caa2c 48
rubbehducky 5:49bcc61caa2c 49 */
rubbehducky 5:49bcc61caa2c 50
rubbehducky 5:49bcc61caa2c 51 if (onBoardSwitch == 1) {
rubbehducky 5:49bcc61caa2c 52 press = true;
rubbehducky 5:49bcc61caa2c 53 }
rubbehducky 5:49bcc61caa2c 54
rubbehducky 5:49bcc61caa2c 55 if (press == true) {
rubbehducky 5:49bcc61caa2c 56
rubbehducky 5:49bcc61caa2c 57 //Close File
rubbehducky 5:49bcc61caa2c 58 fclose(fp);
rubbehducky 5:49bcc61caa2c 59
rubbehducky 5:49bcc61caa2c 60 //Close down SD card (flush buffers)
rubbehducky 5:49bcc61caa2c 61 sd.deinit();
rubbehducky 5:49bcc61caa2c 62
rubbehducky 5:49bcc61caa2c 63 //Let user know
rubbehducky 5:49bcc61caa2c 64 puts("You can now remove the SD Card\n");
rubbehducky 5:49bcc61caa2c 65
rubbehducky 5:49bcc61caa2c 66 for (unsigned int n=0; n<2; n++) {
rubbehducky 5:49bcc61caa2c 67 onBoardLED = 1;
rubbehducky 5:49bcc61caa2c 68 wait(0.2);
rubbehducky 5:49bcc61caa2c 69 onBoardLED = 0;
rubbehducky 5:49bcc61caa2c 70 wait(0.2);
rubbehducky 5:49bcc61caa2c 71 }
rubbehducky 5:49bcc61caa2c 72
rubbehducky 5:49bcc61caa2c 73 //Flag to record the event
rubbehducky 5:49bcc61caa2c 74 ejected = true;
rubbehducky 5:49bcc61caa2c 75 }
noutram 2:fad34c30dcc4 76
noutram 2:fad34c30dcc4 77 //Close the file
noutram 2:fad34c30dcc4 78 fclose(fp);
martinsimpson 0:1ce5a958aaf8 79
noutram 2:fad34c30dcc4 80 //Close down
martinsimpson 0:1ce5a958aaf8 81 sd.deinit();
rubbehducky 5:49bcc61caa2c 82
martinsimpson 0:1ce5a958aaf8 83 printf("All done...\n");
noutram 2:fad34c30dcc4 84 errorCode(OK);
martinsimpson 0:1ce5a958aaf8 85
noutram 2:fad34c30dcc4 86 //Flash to indicate goodness
noutram 2:fad34c30dcc4 87 while(true) {
noutram 2:fad34c30dcc4 88 greenLED = 1;
noutram 2:fad34c30dcc4 89 wait(0.5);
noutram 2:fad34c30dcc4 90 greenLED = 0;
noutram 2:fad34c30dcc4 91 wait(0.1);
noutram 2:fad34c30dcc4 92 }
noutram 2:fad34c30dcc4 93 }
noutram 2:fad34c30dcc4 94
martinsimpson 0:1ce5a958aaf8 95 /*
martinsimpson 0:1ce5a958aaf8 96 printf("sd erase size: %llu\n", sd.get_erase_size());
martinsimpson 0:1ce5a958aaf8 97
martinsimpson 0:1ce5a958aaf8 98 if ( 0 != sd.erase(0, sd.get_erase_size())) {
martinsimpson 0:1ce5a958aaf8 99 printf("Error Erasing block \n");
martinsimpson 0:1ce5a958aaf8 100 }
martinsimpson 0:1ce5a958aaf8 101
martinsimpson 0:1ce5a958aaf8 102 // Write some the data block to the device
martinsimpson 0:1ce5a958aaf8 103 if ( 0 == sd.program(block, 0, 512)) {
martinsimpson 0:1ce5a958aaf8 104 // read the data block from the device
martinsimpson 0:1ce5a958aaf8 105 if ( 0 == sd.read(block, 0, 512)) {
martinsimpson 0:1ce5a958aaf8 106 // print the contents of the block
martinsimpson 0:1ce5a958aaf8 107 printf("%s", block);
martinsimpson 0:1ce5a958aaf8 108 }
martinsimpson 0:1ce5a958aaf8 109 }
noutram 2:fad34c30dcc4 110 */