Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task690-mbed-os-FZ429ZI by
main.cpp@4:04cd5171c7ff, 2017-11-23 (annotated)
- Committer:
- noutram
- Date:
- Thu Nov 23 14:03:12 2017 +0000
- Revision:
- 4:04cd5171c7ff
- Parent:
- 3:22e5460a8b42
- Child:
- 5:cc1b9f5c27a0
Removed unnecessary comments
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 | 3:22e5460a8b42 | 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:22e5460a8b42 | 56 | //Read back all strings |
| noutram | 3:22e5460a8b42 | 57 | char s1[64]; |
| noutram | 3:22e5460a8b42 | 58 | while (fscanf(fp, "%s", s1) == 1) { |
| noutram | 3:22e5460a8b42 | 59 | printf("READ BACK: %s\n", s1); |
| noutram | 3:22e5460a8b42 | 60 | } |
| noutram | 2:fad34c30dcc4 | 61 | //To read a whole line, use: fgets(s1, sizeof(s1), fp); |
| noutram | 3:22e5460a8b42 | 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 | } |
