SD Card 1
Dependencies: mbed SDFileSystem
main.cpp@1:36503ac7a3ba, 2020-12-06 (annotated)
- Committer:
- devananda
- Date:
- Sun Dec 06 11:39:59 2020 +0000
- Revision:
- 1:36503ac7a3ba
- Parent:
- 0:fad6683294f8
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:fad6683294f8 | 1 | #include "mbed.h" |
hudakz | 0:fad6683294f8 | 2 | #include "SDFileSystem.h" |
hudakz | 0:fad6683294f8 | 3 | #include "errno.h" |
hudakz | 0:fad6683294f8 | 4 | |
devananda | 1:36503ac7a3ba | 5 | SDFileSystem fs(D11, D12, D13, D7, "sd"); // mosi, miso, sck, cs |
hudakz | 0:fad6683294f8 | 6 | FILE* fp; |
hudakz | 0:fad6683294f8 | 7 | |
hudakz | 0:fad6683294f8 | 8 | /** |
hudakz | 0:fad6683294f8 | 9 | * @brief |
hudakz | 0:fad6683294f8 | 10 | * @note |
hudakz | 0:fad6683294f8 | 11 | * @param |
hudakz | 0:fad6683294f8 | 12 | * @retval |
hudakz | 0:fad6683294f8 | 13 | */ |
hudakz | 0:fad6683294f8 | 14 | int main() |
hudakz | 0:fad6683294f8 | 15 | { |
hudakz | 0:fad6683294f8 | 16 | printf("Mounting file system...\r\n"); |
hudakz | 0:fad6683294f8 | 17 | |
hudakz | 0:fad6683294f8 | 18 | int err = fs.mount(); |
hudakz | 0:fad6683294f8 | 19 | printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); |
hudakz | 0:fad6683294f8 | 20 | if (err) |
hudakz | 0:fad6683294f8 | 21 | return err; |
hudakz | 0:fad6683294f8 | 22 | |
hudakz | 0:fad6683294f8 | 23 | // Open the file. |
hudakz | 0:fad6683294f8 | 24 | printf("Opening file '/sd/mytest/sdtest.txt'... "); |
hudakz | 0:fad6683294f8 | 25 | fp = fopen("/sd/mytest/sdtest.txt", "w+"); |
hudakz | 0:fad6683294f8 | 26 | printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n")); |
hudakz | 0:fad6683294f8 | 27 | |
hudakz | 0:fad6683294f8 | 28 | if (!fp) |
hudakz | 0:fad6683294f8 | 29 | { |
hudakz | 0:fad6683294f8 | 30 | // Check whether directory '/sd/mytest' exists. |
hudakz | 0:fad6683294f8 | 31 | printf("\r\nChecking directory '/sd/mytest'...\r\n"); |
hudakz | 0:fad6683294f8 | 32 | |
hudakz | 0:fad6683294f8 | 33 | struct stat info; |
hudakz | 0:fad6683294f8 | 34 | err = stat("/sd/mytest", &info); |
hudakz | 0:fad6683294f8 | 35 | if (err) |
hudakz | 0:fad6683294f8 | 36 | { |
hudakz | 0:fad6683294f8 | 37 | printf("Directory '/sd/mytest' does not exist.\r\n"); |
hudakz | 0:fad6683294f8 | 38 | printf("Trying to create it..."); |
hudakz | 0:fad6683294f8 | 39 | err = mkdir("/sd/mytest", 0777); |
hudakz | 0:fad6683294f8 | 40 | printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); |
hudakz | 0:fad6683294f8 | 41 | if (err) |
hudakz | 0:fad6683294f8 | 42 | return err; |
hudakz | 0:fad6683294f8 | 43 | } |
hudakz | 0:fad6683294f8 | 44 | |
hudakz | 0:fad6683294f8 | 45 | // Create a new 'sdtest.txt' file. |
hudakz | 0:fad6683294f8 | 46 | printf("File not found, creating a new one...\r\n"); |
hudakz | 0:fad6683294f8 | 47 | fp = fopen("/sd/mytest/sdtest.txt", "w+"); |
hudakz | 0:fad6683294f8 | 48 | printf("%s\r\n", (!fp ? "Failed :(" : "OK")); |
hudakz | 0:fad6683294f8 | 49 | if (!fp) |
hudakz | 0:fad6683294f8 | 50 | { |
hudakz | 0:fad6683294f8 | 51 | error("error: %s (%d)\r\n", strerror(errno), -errno); |
hudakz | 0:fad6683294f8 | 52 | return errno; |
hudakz | 0:fad6683294f8 | 53 | } |
hudakz | 0:fad6683294f8 | 54 | } |
hudakz | 0:fad6683294f8 | 55 | |
hudakz | 0:fad6683294f8 | 56 | for (int i = 0; i < 10; i++) |
hudakz | 0:fad6683294f8 | 57 | { |
hudakz | 0:fad6683294f8 | 58 | printf("Writing numbers (%d/%d)... ", i, 10); |
hudakz | 0:fad6683294f8 | 59 | err = fprintf(fp, " %d\r\n", i); |
hudakz | 0:fad6683294f8 | 60 | if (err < 0) |
hudakz | 0:fad6683294f8 | 61 | { |
hudakz | 0:fad6683294f8 | 62 | printf("Fail :(\r\n"); |
hudakz | 0:fad6683294f8 | 63 | error("error: %s (%d)\r\n", strerror(errno), -errno); |
hudakz | 0:fad6683294f8 | 64 | } |
hudakz | 0:fad6683294f8 | 65 | else |
hudakz | 0:fad6683294f8 | 66 | printf("OK\r\n"); |
hudakz | 0:fad6683294f8 | 67 | } |
hudakz | 0:fad6683294f8 | 68 | |
hudakz | 0:fad6683294f8 | 69 | printf("Writing numbers (%d/%d)... OK\r\n\r\n", 10, 10); |
hudakz | 0:fad6683294f8 | 70 | err = fclose(fp); |
hudakz | 0:fad6683294f8 | 71 | printf("Closing file '/sd/mytest/sdtest.txt'... "); |
hudakz | 0:fad6683294f8 | 72 | printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); |
hudakz | 0:fad6683294f8 | 73 | if (err) |
hudakz | 0:fad6683294f8 | 74 | return err; |
hudakz | 0:fad6683294f8 | 75 | |
hudakz | 0:fad6683294f8 | 76 | return 0; |
hudakz | 0:fad6683294f8 | 77 | } |