Solution

Dependencies:   ELEC350-Practicals-FZ429

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

Committer:
noutram
Date:
Wed Dec 06 11:20:21 2017 +0000
Revision:
4:958cfd74ef1c
Parent:
3:13716f14b257
changed MOSI pin to PB_5

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
martinsimpson 0:1ce5a958aaf8 11 uint8_t block[512] = "Hello World!\n";
martinsimpson 0:1ce5a958aaf8 12 int main()
martinsimpson 0:1ce5a958aaf8 13 {
noutram 2:fad34c30dcc4 14 //POWER ON SELF TEST
noutram 2:fad34c30dcc4 15 post();
noutram 2:fad34c30dcc4 16
noutram 2:fad34c30dcc4 17 printf("Initialise\n");
martinsimpson 0:1ce5a958aaf8 18 //FileSystemLike(*sd);
martinsimpson 0:1ce5a958aaf8 19
martinsimpson 0:1ce5a958aaf8 20 // call the SDBlockDevice instance initialisation method.
noutram 2:fad34c30dcc4 21 if ( sd.init() != 0) {
martinsimpson 0:1ce5a958aaf8 22 printf("Init failed \n");
noutram 2:fad34c30dcc4 23 errorCode(FATAL);
noutram 2:fad34c30dcc4 24 }
martinsimpson 0:1ce5a958aaf8 25
noutram 2:fad34c30dcc4 26 //Create a filing system for SD Card
martinsimpson 0:1ce5a958aaf8 27 FATFileSystem fs("sd", &sd);
martinsimpson 0:1ce5a958aaf8 28
noutram 2:fad34c30dcc4 29 // *************
noutram 2:fad34c30dcc4 30 // Open to WRITE
noutram 2:fad34c30dcc4 31 // *************
noutram 2:fad34c30dcc4 32 printf("Write to a file\n");
noutram 3:13716f14b257 33 FILE* fp = fopen("/sd/test.txt","a");
noutram 2:fad34c30dcc4 34 //Check file handle (stream)
noutram 2:fad34c30dcc4 35 if (fp == NULL) {
martinsimpson 0:1ce5a958aaf8 36 error("Could not open file for write\n");
noutram 2:fad34c30dcc4 37 errorCode(FATAL);
martinsimpson 0:1ce5a958aaf8 38 }
martinsimpson 0:1ce5a958aaf8 39
martinsimpson 0:1ce5a958aaf8 40 //Put some text in the file...
noutram 2:fad34c30dcc4 41 fprintf(fp, "Welcome to ELEC350\n");
noutram 2:fad34c30dcc4 42
noutram 2:fad34c30dcc4 43 //Close the file
noutram 2:fad34c30dcc4 44 fclose(fp);
martinsimpson 0:1ce5a958aaf8 45
noutram 2:fad34c30dcc4 46 // ************
noutram 2:fad34c30dcc4 47 // Open to READ
noutram 2:fad34c30dcc4 48 // ************
noutram 2:fad34c30dcc4 49 printf("Read a file\n");
noutram 2:fad34c30dcc4 50 fp = fopen("/sd/test.txt","r");
noutram 2:fad34c30dcc4 51 if (fp == NULL) {
noutram 2:fad34c30dcc4 52 error("Could not open file for read\n");
noutram 2:fad34c30dcc4 53 errorCode(FATAL);
noutram 2:fad34c30dcc4 54 }
noutram 2:fad34c30dcc4 55
noutram 3:13716f14b257 56 //Read back all strings
noutram 3:13716f14b257 57 char s1[64];
noutram 3:13716f14b257 58 while (fscanf(fp, "%s", s1) == 1) {
noutram 3:13716f14b257 59 printf("READ BACK: %s\n", s1);
noutram 3:13716f14b257 60 }
noutram 2:fad34c30dcc4 61 //To read a whole line, use: fgets(s1, sizeof(s1), fp);
noutram 3:13716f14b257 62
noutram 2:fad34c30dcc4 63
noutram 2:fad34c30dcc4 64 //Close File
martinsimpson 0:1ce5a958aaf8 65 fclose(fp);
noutram 2:fad34c30dcc4 66
noutram 2:fad34c30dcc4 67 //Close down
martinsimpson 0:1ce5a958aaf8 68 sd.deinit();
martinsimpson 0:1ce5a958aaf8 69 printf("All done...\n");
noutram 2:fad34c30dcc4 70 errorCode(OK);
martinsimpson 0:1ce5a958aaf8 71
noutram 2:fad34c30dcc4 72 //Flash to indicate goodness
noutram 2:fad34c30dcc4 73 while(true) {
noutram 2:fad34c30dcc4 74 greenLED = 1;
noutram 2:fad34c30dcc4 75 wait(0.5);
noutram 2:fad34c30dcc4 76 greenLED = 0;
noutram 2:fad34c30dcc4 77 wait(0.1);
noutram 2:fad34c30dcc4 78 }
noutram 2:fad34c30dcc4 79 }
noutram 2:fad34c30dcc4 80
martinsimpson 0:1ce5a958aaf8 81 /*
martinsimpson 0:1ce5a958aaf8 82 printf("sd size: %llu\n", sd.size());
martinsimpson 0:1ce5a958aaf8 83 printf("sd read size: %llu\n", sd.get_read_size());
martinsimpson 0:1ce5a958aaf8 84 printf("sd program size: %llu\n", sd.get_program_size());
martinsimpson 0:1ce5a958aaf8 85 printf("sd erase size: %llu\n", sd.get_erase_size());
martinsimpson 0:1ce5a958aaf8 86
martinsimpson 0:1ce5a958aaf8 87 // set the frequency
martinsimpson 0:1ce5a958aaf8 88 if ( 0 != sd.frequency(5000000)) {
martinsimpson 0:1ce5a958aaf8 89 printf("Error setting frequency \n");
martinsimpson 0:1ce5a958aaf8 90 }
martinsimpson 0:1ce5a958aaf8 91
martinsimpson 0:1ce5a958aaf8 92 if ( 0 != sd.erase(0, sd.get_erase_size())) {
martinsimpson 0:1ce5a958aaf8 93 printf("Error Erasing block \n");
martinsimpson 0:1ce5a958aaf8 94 }
martinsimpson 0:1ce5a958aaf8 95
martinsimpson 0:1ce5a958aaf8 96 // Write some the data block to the device
martinsimpson 0:1ce5a958aaf8 97 if ( 0 == sd.program(block, 0, 512)) {
martinsimpson 0:1ce5a958aaf8 98 // read the data block from the device
martinsimpson 0:1ce5a958aaf8 99 if ( 0 == sd.read(block, 0, 512)) {
martinsimpson 0:1ce5a958aaf8 100 // print the contents of the block
martinsimpson 0:1ce5a958aaf8 101 printf("%s", block);
martinsimpson 0:1ce5a958aaf8 102 }
martinsimpson 0:1ce5a958aaf8 103 }
martinsimpson 0:1ce5a958aaf8 104
martinsimpson 0:1ce5a958aaf8 105 // call the SDBlockDevice instance de-initialisation method.
martinsimpson 0:1ce5a958aaf8 106
martinsimpson 0:1ce5a958aaf8 107 sd.deinit();
noutram 2:fad34c30dcc4 108 */