![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LED.H
Dependencies: ELEC350-Practicals-FZ429
Fork of ELEC350-SD_Card_Demo by
main.cpp@3:528e42a15d44, 2017-12-06 (annotated)
- Committer:
- noutram
- Date:
- Wed Dec 06 14:12:30 2017 +0000
- Revision:
- 3:528e42a15d44
- Parent:
- 2:f1b3a82f7738
- Child:
- 4:c2c24e09acd1
Changed pin for MISO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 2:f1b3a82f7738 | 1 | // NickO 2017 |
martinsimpson | 0:1ce5a958aaf8 | 2 | /* Access an SD Card using SPI */ |
martinsimpson | 0:1ce5a958aaf8 | 3 | |
martinsimpson | 0:1ce5a958aaf8 | 4 | #include "mbed.h" |
martinsimpson | 0:1ce5a958aaf8 | 5 | #include "SDBlockDevice.h" |
martinsimpson | 0:1ce5a958aaf8 | 6 | #include "FATFileSystem.h" |
noutram | 2:f1b3a82f7738 | 7 | #include "sample_hardware.hpp" |
martinsimpson | 0:1ce5a958aaf8 | 8 | |
noutram | 2:f1b3a82f7738 | 9 | //SD Card Object |
noutram | 3:528e42a15d44 | 10 | SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs |
martinsimpson | 0:1ce5a958aaf8 | 11 | |
martinsimpson | 0:1ce5a958aaf8 | 12 | uint8_t block[512] = "Hello World!\n"; |
martinsimpson | 0:1ce5a958aaf8 | 13 | int main() |
martinsimpson | 0:1ce5a958aaf8 | 14 | { |
noutram | 2:f1b3a82f7738 | 15 | //POWER ON SELF TEST |
noutram | 2:f1b3a82f7738 | 16 | post(); |
noutram | 2:f1b3a82f7738 | 17 | |
noutram | 2:f1b3a82f7738 | 18 | printf("Initialise\n"); |
martinsimpson | 0:1ce5a958aaf8 | 19 | //FileSystemLike(*sd); |
martinsimpson | 0:1ce5a958aaf8 | 20 | |
martinsimpson | 0:1ce5a958aaf8 | 21 | // call the SDBlockDevice instance initialisation method. |
noutram | 2:f1b3a82f7738 | 22 | if ( sd.init() != 0) { |
martinsimpson | 0:1ce5a958aaf8 | 23 | printf("Init failed \n"); |
noutram | 2:f1b3a82f7738 | 24 | errorCode(FATAL); |
noutram | 2:f1b3a82f7738 | 25 | } |
martinsimpson | 0:1ce5a958aaf8 | 26 | |
noutram | 2:f1b3a82f7738 | 27 | //Create a filing system for SD Card |
martinsimpson | 0:1ce5a958aaf8 | 28 | FATFileSystem fs("sd", &sd); |
martinsimpson | 0:1ce5a958aaf8 | 29 | |
noutram | 2:f1b3a82f7738 | 30 | // ************* |
noutram | 2:f1b3a82f7738 | 31 | // Open to WRITE |
noutram | 2:f1b3a82f7738 | 32 | // ************* |
noutram | 2:f1b3a82f7738 | 33 | printf("Write to a file\n"); |
noutram | 2:f1b3a82f7738 | 34 | FILE* fp = fopen("/sd/test.txt","w"); |
noutram | 2:f1b3a82f7738 | 35 | //Check file handle (stream) |
noutram | 2:f1b3a82f7738 | 36 | if (fp == NULL) { |
martinsimpson | 0:1ce5a958aaf8 | 37 | error("Could not open file for write\n"); |
noutram | 2:f1b3a82f7738 | 38 | errorCode(FATAL); |
martinsimpson | 0:1ce5a958aaf8 | 39 | } |
martinsimpson | 0:1ce5a958aaf8 | 40 | |
martinsimpson | 0:1ce5a958aaf8 | 41 | //Put some text in the file... |
noutram | 2:f1b3a82f7738 | 42 | fprintf(fp, "Welcome to ELEC350\n"); |
noutram | 2:f1b3a82f7738 | 43 | |
noutram | 2:f1b3a82f7738 | 44 | //Close the file |
noutram | 2:f1b3a82f7738 | 45 | fclose(fp); |
martinsimpson | 0:1ce5a958aaf8 | 46 | |
noutram | 2:f1b3a82f7738 | 47 | // ************ |
noutram | 2:f1b3a82f7738 | 48 | // Open to READ |
noutram | 2:f1b3a82f7738 | 49 | // ************ |
noutram | 2:f1b3a82f7738 | 50 | printf("Read a file\n"); |
noutram | 2:f1b3a82f7738 | 51 | fp = fopen("/sd/test.txt","r"); |
noutram | 2:f1b3a82f7738 | 52 | if (fp == NULL) { |
noutram | 2:f1b3a82f7738 | 53 | error("Could not open file for read\n"); |
noutram | 2:f1b3a82f7738 | 54 | errorCode(FATAL); |
noutram | 2:f1b3a82f7738 | 55 | } |
noutram | 2:f1b3a82f7738 | 56 | |
noutram | 2:f1b3a82f7738 | 57 | //Read in three strings |
noutram | 2:f1b3a82f7738 | 58 | char s1[64], s2[64], s3[64]; |
noutram | 2:f1b3a82f7738 | 59 | fscanf(fp, "%s %s %s", s1,s2,s3); |
noutram | 2:f1b3a82f7738 | 60 | //To read a whole line, use: fgets(s1, sizeof(s1), fp); |
noutram | 2:f1b3a82f7738 | 61 | printf("READ BACK: %s %s %s\n", s1, s2, s3); |
noutram | 2:f1b3a82f7738 | 62 | |
noutram | 2:f1b3a82f7738 | 63 | //Close File |
martinsimpson | 0:1ce5a958aaf8 | 64 | fclose(fp); |
noutram | 2:f1b3a82f7738 | 65 | |
noutram | 2:f1b3a82f7738 | 66 | //Close down |
martinsimpson | 0:1ce5a958aaf8 | 67 | sd.deinit(); |
martinsimpson | 0:1ce5a958aaf8 | 68 | printf("All done...\n"); |
noutram | 2:f1b3a82f7738 | 69 | errorCode(OK); |
martinsimpson | 0:1ce5a958aaf8 | 70 | |
noutram | 2:f1b3a82f7738 | 71 | //Flash to indicate goodness |
noutram | 2:f1b3a82f7738 | 72 | while(true) { |
noutram | 2:f1b3a82f7738 | 73 | greenLED = 1; |
noutram | 2:f1b3a82f7738 | 74 | wait(0.5); |
noutram | 2:f1b3a82f7738 | 75 | greenLED = 0; |
noutram | 2:f1b3a82f7738 | 76 | wait(0.1); |
noutram | 2:f1b3a82f7738 | 77 | } |
noutram | 2:f1b3a82f7738 | 78 | } |
noutram | 2:f1b3a82f7738 | 79 | |
martinsimpson | 0:1ce5a958aaf8 | 80 | /* |
martinsimpson | 0:1ce5a958aaf8 | 81 | printf("sd size: %llu\n", sd.size()); |
martinsimpson | 0:1ce5a958aaf8 | 82 | printf("sd read size: %llu\n", sd.get_read_size()); |
martinsimpson | 0:1ce5a958aaf8 | 83 | printf("sd program size: %llu\n", sd.get_program_size()); |
martinsimpson | 0:1ce5a958aaf8 | 84 | printf("sd erase size: %llu\n", sd.get_erase_size()); |
martinsimpson | 0:1ce5a958aaf8 | 85 | |
martinsimpson | 0:1ce5a958aaf8 | 86 | // set the frequency |
martinsimpson | 0:1ce5a958aaf8 | 87 | if ( 0 != sd.frequency(5000000)) { |
martinsimpson | 0:1ce5a958aaf8 | 88 | printf("Error setting frequency \n"); |
martinsimpson | 0:1ce5a958aaf8 | 89 | } |
martinsimpson | 0:1ce5a958aaf8 | 90 | |
martinsimpson | 0:1ce5a958aaf8 | 91 | if ( 0 != sd.erase(0, sd.get_erase_size())) { |
martinsimpson | 0:1ce5a958aaf8 | 92 | printf("Error Erasing block \n"); |
martinsimpson | 0:1ce5a958aaf8 | 93 | } |
martinsimpson | 0:1ce5a958aaf8 | 94 | |
martinsimpson | 0:1ce5a958aaf8 | 95 | // Write some the data block to the device |
martinsimpson | 0:1ce5a958aaf8 | 96 | if ( 0 == sd.program(block, 0, 512)) { |
martinsimpson | 0:1ce5a958aaf8 | 97 | // read the data block from the device |
martinsimpson | 0:1ce5a958aaf8 | 98 | if ( 0 == sd.read(block, 0, 512)) { |
martinsimpson | 0:1ce5a958aaf8 | 99 | // print the contents of the block |
martinsimpson | 0:1ce5a958aaf8 | 100 | printf("%s", block); |
martinsimpson | 0:1ce5a958aaf8 | 101 | } |
martinsimpson | 0:1ce5a958aaf8 | 102 | } |
martinsimpson | 0:1ce5a958aaf8 | 103 | |
martinsimpson | 0:1ce5a958aaf8 | 104 | // call the SDBlockDevice instance de-initialisation method. |
martinsimpson | 0:1ce5a958aaf8 | 105 | |
martinsimpson | 0:1ce5a958aaf8 | 106 | sd.deinit(); |
noutram | 2:f1b3a82f7738 | 107 | */ |