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@0:1ce5a958aaf8, 2017-10-20 (annotated)
- Committer:
- martinsimpson
- Date:
- Fri Oct 20 11:29:41 2017 +0000
- Revision:
- 0:1ce5a958aaf8
- Child:
- 2:fad34c30dcc4
First draft
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" |
| martinsimpson | 0:1ce5a958aaf8 | 6 | |
| martinsimpson | 0:1ce5a958aaf8 | 7 | SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs |
| martinsimpson | 0:1ce5a958aaf8 | 8 | |
| martinsimpson | 0:1ce5a958aaf8 | 9 | uint8_t block[512] = "Hello World!\n"; |
| martinsimpson | 0:1ce5a958aaf8 | 10 | int main() |
| martinsimpson | 0:1ce5a958aaf8 | 11 | { |
| martinsimpson | 0:1ce5a958aaf8 | 12 | printf("Initialise and write to a file\n"); |
| martinsimpson | 0:1ce5a958aaf8 | 13 | //FileSystemLike(*sd); |
| martinsimpson | 0:1ce5a958aaf8 | 14 | |
| martinsimpson | 0:1ce5a958aaf8 | 15 | |
| martinsimpson | 0:1ce5a958aaf8 | 16 | // call the SDBlockDevice instance initialisation method. |
| martinsimpson | 0:1ce5a958aaf8 | 17 | if ( 0 != sd.init()) { |
| martinsimpson | 0:1ce5a958aaf8 | 18 | printf("Init failed \n"); |
| martinsimpson | 0:1ce5a958aaf8 | 19 | return -1; |
| martinsimpson | 0:1ce5a958aaf8 | 20 | } |
| martinsimpson | 0:1ce5a958aaf8 | 21 | //FileSystemLike(*sd); |
| martinsimpson | 0:1ce5a958aaf8 | 22 | |
| martinsimpson | 0:1ce5a958aaf8 | 23 | FATFileSystem fs("sd", &sd); |
| martinsimpson | 0:1ce5a958aaf8 | 24 | |
| martinsimpson | 0:1ce5a958aaf8 | 25 | |
| martinsimpson | 0:1ce5a958aaf8 | 26 | FILE *fp = fopen("/sd/test.txt","w"); |
| martinsimpson | 0:1ce5a958aaf8 | 27 | if(fp == NULL) { |
| martinsimpson | 0:1ce5a958aaf8 | 28 | error("Could not open file for write\n"); |
| martinsimpson | 0:1ce5a958aaf8 | 29 | } |
| martinsimpson | 0:1ce5a958aaf8 | 30 | |
| martinsimpson | 0:1ce5a958aaf8 | 31 | //Put some text in the file... |
| martinsimpson | 0:1ce5a958aaf8 | 32 | fprintf(fp, "Martin Says Hi! One for the good guys\n"); |
| martinsimpson | 0:1ce5a958aaf8 | 33 | |
| martinsimpson | 0:1ce5a958aaf8 | 34 | //Tidy up here |
| martinsimpson | 0:1ce5a958aaf8 | 35 | fclose(fp); |
| martinsimpson | 0:1ce5a958aaf8 | 36 | sd.deinit(); |
| martinsimpson | 0:1ce5a958aaf8 | 37 | printf("All done...\n"); |
| martinsimpson | 0:1ce5a958aaf8 | 38 | |
| martinsimpson | 0:1ce5a958aaf8 | 39 | /* |
| martinsimpson | 0:1ce5a958aaf8 | 40 | printf("sd size: %llu\n", sd.size()); |
| martinsimpson | 0:1ce5a958aaf8 | 41 | printf("sd read size: %llu\n", sd.get_read_size()); |
| martinsimpson | 0:1ce5a958aaf8 | 42 | printf("sd program size: %llu\n", sd.get_program_size()); |
| martinsimpson | 0:1ce5a958aaf8 | 43 | printf("sd erase size: %llu\n", sd.get_erase_size()); |
| martinsimpson | 0:1ce5a958aaf8 | 44 | |
| martinsimpson | 0:1ce5a958aaf8 | 45 | // set the frequency |
| martinsimpson | 0:1ce5a958aaf8 | 46 | if ( 0 != sd.frequency(5000000)) { |
| martinsimpson | 0:1ce5a958aaf8 | 47 | printf("Error setting frequency \n"); |
| martinsimpson | 0:1ce5a958aaf8 | 48 | } |
| martinsimpson | 0:1ce5a958aaf8 | 49 | |
| martinsimpson | 0:1ce5a958aaf8 | 50 | if ( 0 != sd.erase(0, sd.get_erase_size())) { |
| martinsimpson | 0:1ce5a958aaf8 | 51 | printf("Error Erasing block \n"); |
| martinsimpson | 0:1ce5a958aaf8 | 52 | } |
| martinsimpson | 0:1ce5a958aaf8 | 53 | |
| martinsimpson | 0:1ce5a958aaf8 | 54 | // Write some the data block to the device |
| martinsimpson | 0:1ce5a958aaf8 | 55 | if ( 0 == sd.program(block, 0, 512)) { |
| martinsimpson | 0:1ce5a958aaf8 | 56 | // read the data block from the device |
| martinsimpson | 0:1ce5a958aaf8 | 57 | if ( 0 == sd.read(block, 0, 512)) { |
| martinsimpson | 0:1ce5a958aaf8 | 58 | // print the contents of the block |
| martinsimpson | 0:1ce5a958aaf8 | 59 | printf("%s", block); |
| martinsimpson | 0:1ce5a958aaf8 | 60 | } |
| martinsimpson | 0:1ce5a958aaf8 | 61 | } |
| martinsimpson | 0:1ce5a958aaf8 | 62 | |
| martinsimpson | 0:1ce5a958aaf8 | 63 | // call the SDBlockDevice instance de-initialisation method. |
| martinsimpson | 0:1ce5a958aaf8 | 64 | |
| martinsimpson | 0:1ce5a958aaf8 | 65 | sd.deinit(); |
| martinsimpson | 0:1ce5a958aaf8 | 66 | */ |
| martinsimpson | 0:1ce5a958aaf8 | 67 | } |
