![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mbed studio
main.cpp@0:6444a20e1a0e, 2022-05-18 (annotated)
- Committer:
- rainerraul
- Date:
- Wed May 18 14:43:41 2022 +0000
- Revision:
- 0:6444a20e1a0e
mbed studio
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rainerraul | 0:6444a20e1a0e | 1 | #include "mbed.h" |
rainerraul | 0:6444a20e1a0e | 2 | #include "stdio.h" |
rainerraul | 0:6444a20e1a0e | 3 | #include "stdint.h" |
rainerraul | 0:6444a20e1a0e | 4 | #include "stdlib.h" |
rainerraul | 0:6444a20e1a0e | 5 | #include "SPI.h" |
rainerraul | 0:6444a20e1a0e | 6 | #include "SDFileSystem.h" |
rainerraul | 0:6444a20e1a0e | 7 | |
rainerraul | 0:6444a20e1a0e | 8 | Serial terminal(USBTX, USBRX); |
rainerraul | 0:6444a20e1a0e | 9 | |
rainerraul | 0:6444a20e1a0e | 10 | //Create an SDFileSystem object |
rainerraul | 0:6444a20e1a0e | 11 | SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd"); // mosi, miso, sclk, cs |
rainerraul | 0:6444a20e1a0e | 12 | |
rainerraul | 0:6444a20e1a0e | 13 | int main() |
rainerraul | 0:6444a20e1a0e | 14 | { |
rainerraul | 0:6444a20e1a0e | 15 | wait(0.5); |
rainerraul | 0:6444a20e1a0e | 16 | terminal.baud(115200); |
rainerraul | 0:6444a20e1a0e | 17 | //Mount the filesystem |
rainerraul | 0:6444a20e1a0e | 18 | sd.mount(); |
rainerraul | 0:6444a20e1a0e | 19 | //Perform a write test |
rainerraul | 0:6444a20e1a0e | 20 | printf("\nWriting to SD card..."); |
rainerraul | 0:6444a20e1a0e | 21 | FILE *fp = fopen("/sd/sdtest.txt", "w"); |
rainerraul | 0:6444a20e1a0e | 22 | if (fp != NULL) { |
rainerraul | 0:6444a20e1a0e | 23 | fprintf(fp, "We're writing to an SD card!"); |
rainerraul | 0:6444a20e1a0e | 24 | fclose(fp); |
rainerraul | 0:6444a20e1a0e | 25 | printf("success!\n"); |
rainerraul | 0:6444a20e1a0e | 26 | } else { |
rainerraul | 0:6444a20e1a0e | 27 | printf("failed!\n"); |
rainerraul | 0:6444a20e1a0e | 28 | } |
rainerraul | 0:6444a20e1a0e | 29 | |
rainerraul | 0:6444a20e1a0e | 30 | //Perform a read test |
rainerraul | 0:6444a20e1a0e | 31 | printf("Reading from SD card..."); |
rainerraul | 0:6444a20e1a0e | 32 | fp = fopen("/sd/sdtest.txt", "r"); |
rainerraul | 0:6444a20e1a0e | 33 | if (fp != NULL) { |
rainerraul | 0:6444a20e1a0e | 34 | char c = fgetc(fp); |
rainerraul | 0:6444a20e1a0e | 35 | if (c == 'W') |
rainerraul | 0:6444a20e1a0e | 36 | printf("success!\n"); |
rainerraul | 0:6444a20e1a0e | 37 | else |
rainerraul | 0:6444a20e1a0e | 38 | printf("incorrect char (%c)!\n", c); |
rainerraul | 0:6444a20e1a0e | 39 | fclose(fp); |
rainerraul | 0:6444a20e1a0e | 40 | } else { |
rainerraul | 0:6444a20e1a0e | 41 | printf("failed!\n"); |
rainerraul | 0:6444a20e1a0e | 42 | } |
rainerraul | 0:6444a20e1a0e | 43 | sd.unmount(); |
rainerraul | 0:6444a20e1a0e | 44 | //Unmount the filesystem |
rainerraul | 0:6444a20e1a0e | 45 | // sd.unmount(); |
rainerraul | 0:6444a20e1a0e | 46 | } |