This example demonstrates the reading of the SD card in the Nucleo.
Dependencies: mbed SDFileSystem
main.cpp@1:9d8797b91dda, 2020-12-03 (annotated)
- Committer:
- hakusan270
- Date:
- Thu Dec 03 11:25:46 2020 +0000
- Revision:
- 1:9d8797b91dda
- Parent:
- 0:b0a3ecd53c7d
SD card
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
beaglescout007 | 0:b0a3ecd53c7d | 1 | #include "mbed.h" |
beaglescout007 | 0:b0a3ecd53c7d | 2 | #include "SDFileSystem.h" |
beaglescout007 | 0:b0a3ecd53c7d | 3 | |
beaglescout007 | 0:b0a3ecd53c7d | 4 | DigitalIn btn(USER_BUTTON); |
beaglescout007 | 0:b0a3ecd53c7d | 5 | |
beaglescout007 | 0:b0a3ecd53c7d | 6 | // trim '\n' |
beaglescout007 | 0:b0a3ecd53c7d | 7 | void ntrim(char *str) |
beaglescout007 | 0:b0a3ecd53c7d | 8 | { |
beaglescout007 | 0:b0a3ecd53c7d | 9 | int i; |
beaglescout007 | 0:b0a3ecd53c7d | 10 | for (i = 0; str[i] != 0; ++i); |
beaglescout007 | 0:b0a3ecd53c7d | 11 | |
beaglescout007 | 0:b0a3ecd53c7d | 12 | if (i > 0 && str[i - 1] == '\n') |
beaglescout007 | 0:b0a3ecd53c7d | 13 | str[i - 1] = 0; |
beaglescout007 | 0:b0a3ecd53c7d | 14 | } |
beaglescout007 | 0:b0a3ecd53c7d | 15 | |
beaglescout007 | 0:b0a3ecd53c7d | 16 | int main() |
beaglescout007 | 0:b0a3ecd53c7d | 17 | { |
hakusan270 | 1:9d8797b91dda | 18 | printf("Hello\r\n"); |
beaglescout007 | 0:b0a3ecd53c7d | 19 | // SD filesystem |
hakusan270 | 1:9d8797b91dda | 20 | SDFileSystem *sd = new SDFileSystem(D11, D12, D13, D10, "sd", NC, SDFileSystem::SWITCH_NONE, 20000000); // mosi, miso, sclk, name, card detect, sw type, freq |
beaglescout007 | 0:b0a3ecd53c7d | 21 | |
beaglescout007 | 0:b0a3ecd53c7d | 22 | while (1) |
beaglescout007 | 0:b0a3ecd53c7d | 23 | { |
beaglescout007 | 0:b0a3ecd53c7d | 24 | if (btn) continue; |
beaglescout007 | 0:b0a3ecd53c7d | 25 | |
beaglescout007 | 0:b0a3ecd53c7d | 26 | // file open |
hakusan270 | 1:9d8797b91dda | 27 | FILE *fp = fopen("/sd/sd/test.txt", "r"); |
beaglescout007 | 0:b0a3ecd53c7d | 28 | if (fp == NULL) |
beaglescout007 | 0:b0a3ecd53c7d | 29 | { |
beaglescout007 | 0:b0a3ecd53c7d | 30 | printf("open error!!\r\n"); |
beaglescout007 | 0:b0a3ecd53c7d | 31 | while(1); |
beaglescout007 | 0:b0a3ecd53c7d | 32 | } |
hakusan270 | 1:9d8797b91dda | 33 | // fprintf(fp,"test write\n"); |
hakusan270 | 1:9d8797b91dda | 34 | // fclose(fp); |
beaglescout007 | 0:b0a3ecd53c7d | 35 | // read text file |
beaglescout007 | 0:b0a3ecd53c7d | 36 | char buf[1024]; |
beaglescout007 | 0:b0a3ecd53c7d | 37 | while (fgets(buf, sizeof(buf), fp) != NULL) |
beaglescout007 | 0:b0a3ecd53c7d | 38 | { |
beaglescout007 | 0:b0a3ecd53c7d | 39 | ntrim(buf); |
beaglescout007 | 0:b0a3ecd53c7d | 40 | printf("%s\r\n", buf); |
beaglescout007 | 0:b0a3ecd53c7d | 41 | } |
beaglescout007 | 0:b0a3ecd53c7d | 42 | |
beaglescout007 | 0:b0a3ecd53c7d | 43 | // file close |
beaglescout007 | 0:b0a3ecd53c7d | 44 | fclose(fp); |
beaglescout007 | 0:b0a3ecd53c7d | 45 | |
beaglescout007 | 0:b0a3ecd53c7d | 46 | wait(1); |
beaglescout007 | 0:b0a3ecd53c7d | 47 | } |
beaglescout007 | 0:b0a3ecd53c7d | 48 | } |