![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Solution
Dependencies: ELEC350-Practicals-FZ429
Fork of Task680-mbed-os-FZ429ZI by
main.cpp@2:fad34c30dcc4, 2017-11-22 (annotated)
- Committer:
- noutram
- Date:
- Wed Nov 22 15:18:28 2017 +0000
- Revision:
- 2:fad34c30dcc4
- Parent:
- 0:1ce5a958aaf8
- Child:
- 3:13716f14b257
Demo of the SD card reader
Who changed what in which revision?
User | Revision | Line number | New 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 |
martinsimpson | 0:1ce5a958aaf8 | 9 | SDBlockDevice sd(D11, 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 | 2:fad34c30dcc4 | 33 | FILE* fp = fopen("/sd/test.txt","w"); |
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 | 2:fad34c30dcc4 | 56 | //Read in three strings |
noutram | 2:fad34c30dcc4 | 57 | char s1[64], s2[64], s3[64]; |
noutram | 2:fad34c30dcc4 | 58 | fscanf(fp, "%s %s %s", s1,s2,s3); |
noutram | 2:fad34c30dcc4 | 59 | //To read a whole line, use: fgets(s1, sizeof(s1), fp); |
noutram | 2:fad34c30dcc4 | 60 | printf("READ BACK: %s %s %s\n", s1, s2, s3); |
noutram | 2:fad34c30dcc4 | 61 | |
noutram | 2:fad34c30dcc4 | 62 | //Close File |
martinsimpson | 0:1ce5a958aaf8 | 63 | fclose(fp); |
noutram | 2:fad34c30dcc4 | 64 | |
noutram | 2:fad34c30dcc4 | 65 | //Close down |
martinsimpson | 0:1ce5a958aaf8 | 66 | sd.deinit(); |
martinsimpson | 0:1ce5a958aaf8 | 67 | printf("All done...\n"); |
noutram | 2:fad34c30dcc4 | 68 | errorCode(OK); |
martinsimpson | 0:1ce5a958aaf8 | 69 | |
noutram | 2:fad34c30dcc4 | 70 | //Flash to indicate goodness |
noutram | 2:fad34c30dcc4 | 71 | while(true) { |
noutram | 2:fad34c30dcc4 | 72 | greenLED = 1; |
noutram | 2:fad34c30dcc4 | 73 | wait(0.5); |
noutram | 2:fad34c30dcc4 | 74 | greenLED = 0; |
noutram | 2:fad34c30dcc4 | 75 | wait(0.1); |
noutram | 2:fad34c30dcc4 | 76 | } |
noutram | 2:fad34c30dcc4 | 77 | } |
noutram | 2:fad34c30dcc4 | 78 | |
martinsimpson | 0:1ce5a958aaf8 | 79 | /* |
martinsimpson | 0:1ce5a958aaf8 | 80 | printf("sd size: %llu\n", sd.size()); |
martinsimpson | 0:1ce5a958aaf8 | 81 | printf("sd read size: %llu\n", sd.get_read_size()); |
martinsimpson | 0:1ce5a958aaf8 | 82 | printf("sd program size: %llu\n", sd.get_program_size()); |
martinsimpson | 0:1ce5a958aaf8 | 83 | printf("sd erase size: %llu\n", sd.get_erase_size()); |
martinsimpson | 0:1ce5a958aaf8 | 84 | |
martinsimpson | 0:1ce5a958aaf8 | 85 | // set the frequency |
martinsimpson | 0:1ce5a958aaf8 | 86 | if ( 0 != sd.frequency(5000000)) { |
martinsimpson | 0:1ce5a958aaf8 | 87 | printf("Error setting frequency \n"); |
martinsimpson | 0:1ce5a958aaf8 | 88 | } |
martinsimpson | 0:1ce5a958aaf8 | 89 | |
martinsimpson | 0:1ce5a958aaf8 | 90 | if ( 0 != sd.erase(0, sd.get_erase_size())) { |
martinsimpson | 0:1ce5a958aaf8 | 91 | printf("Error Erasing block \n"); |
martinsimpson | 0:1ce5a958aaf8 | 92 | } |
martinsimpson | 0:1ce5a958aaf8 | 93 | |
martinsimpson | 0:1ce5a958aaf8 | 94 | // Write some the data block to the device |
martinsimpson | 0:1ce5a958aaf8 | 95 | if ( 0 == sd.program(block, 0, 512)) { |
martinsimpson | 0:1ce5a958aaf8 | 96 | // read the data block from the device |
martinsimpson | 0:1ce5a958aaf8 | 97 | if ( 0 == sd.read(block, 0, 512)) { |
martinsimpson | 0:1ce5a958aaf8 | 98 | // print the contents of the block |
martinsimpson | 0:1ce5a958aaf8 | 99 | printf("%s", block); |
martinsimpson | 0:1ce5a958aaf8 | 100 | } |
martinsimpson | 0:1ce5a958aaf8 | 101 | } |
martinsimpson | 0:1ce5a958aaf8 | 102 | |
martinsimpson | 0:1ce5a958aaf8 | 103 | // call the SDBlockDevice instance de-initialisation method. |
martinsimpson | 0:1ce5a958aaf8 | 104 | |
martinsimpson | 0:1ce5a958aaf8 | 105 | sd.deinit(); |
noutram | 2:fad34c30dcc4 | 106 | */ |