Example of using SD card with Mbed OS5.

Committer:
hudakz
Date:
Wed Jul 17 06:07:09 2019 +0000
Revision:
2:d08c053d5a35
Parent:
1:9a5f06e7969e
Example of using SD card with Mbed OS5.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:87642278ede6 1 /* mbed Microcontroller Library
hudakz 0:87642278ede6 2 * Copyright (c) 2006-2013 ARM Limited
hudakz 0:87642278ede6 3 *
hudakz 0:87642278ede6 4 * Licensed under the Apache License, Version 2.0 (the "License");
hudakz 0:87642278ede6 5 * you may not use this file except in compliance with the License.
hudakz 0:87642278ede6 6 * You may obtain a copy of the License at
hudakz 0:87642278ede6 7 *
hudakz 0:87642278ede6 8 * http://www.apache.org/licenses/LICENSE-2.0
hudakz 0:87642278ede6 9 *
hudakz 0:87642278ede6 10 * Unless required by applicable law or agreed to in writing, software
hudakz 0:87642278ede6 11 * distributed under the License is distributed on an "AS IS" BASIS,
hudakz 0:87642278ede6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
hudakz 0:87642278ede6 13 * See the License for the specific language governing permissions and
hudakz 0:87642278ede6 14 * limitations under the License.
hudakz 0:87642278ede6 15 */
hudakz 0:87642278ede6 16 #include "mbed.h"
hudakz 0:87642278ede6 17 #include <stdio.h>
hudakz 0:87642278ede6 18 #include <errno.h>
hudakz 0:87642278ede6 19
hudakz 0:87642278ede6 20 // Block devices
hudakz 0:87642278ede6 21 //#include "SPIFBlockDevice.h"
hudakz 0:87642278ede6 22 //#include "DataFlashBlockDevice.h"
hudakz 0:87642278ede6 23 #include "SDBlockDevice.h"
hudakz 0:87642278ede6 24 //#include "HeapBlockDevice.h"
hudakz 0:87642278ede6 25
hudakz 0:87642278ede6 26 // File systems
hudakz 0:87642278ede6 27
hudakz 0:87642278ede6 28 //#include "LittleFileSystem.h"
hudakz 0:87642278ede6 29 #include "FATFileSystem.h"
hudakz 0:87642278ede6 30
hudakz 0:87642278ede6 31 // Physical block device, can be any device that supports the BlockDevice API
hudakz 1:9a5f06e7969e 32 SDBlockDevice blockDevice(p5, p6, p7, p8); // mosi, miso, sck, cs
hudakz 0:87642278ede6 33
hudakz 0:87642278ede6 34 // File system declaration
hudakz 0:87642278ede6 35 FATFileSystem fileSystem("fs");
hudakz 0:87642278ede6 36
hudakz 0:87642278ede6 37 // Entry point for the example
hudakz 0:87642278ede6 38 int main()
hudakz 0:87642278ede6 39 {
hudakz 0:87642278ede6 40 printf("--- Mbed OS filesystem example ---\n");
hudakz 0:87642278ede6 41
hudakz 0:87642278ede6 42 // Try to mount the filesystem
hudakz 0:87642278ede6 43 printf("Mounting the filesystem... ");
hudakz 0:87642278ede6 44 fflush(stdout);
hudakz 0:87642278ede6 45
hudakz 0:87642278ede6 46 int err = fileSystem.mount(&blockDevice);
hudakz 0:87642278ede6 47 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 48 if (err) {
hudakz 0:87642278ede6 49 // Reformat if we can't mount the filesystem
hudakz 0:87642278ede6 50 // this should only happen on the first boot
hudakz 0:87642278ede6 51 printf("No filesystem found, formatting... ");
hudakz 0:87642278ede6 52 fflush(stdout);
hudakz 0:87642278ede6 53 err = fileSystem.reformat(&blockDevice);
hudakz 0:87642278ede6 54 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 55 if (err) {
hudakz 0:87642278ede6 56 error("error: %s (%d)\n", strerror(-err), err);
hudakz 0:87642278ede6 57 }
hudakz 0:87642278ede6 58 }
hudakz 0:87642278ede6 59
hudakz 0:87642278ede6 60 // Open the numbers file
hudakz 0:87642278ede6 61 printf("Opening \"/fs/numbers.txt\"... ");
hudakz 0:87642278ede6 62 fflush(stdout);
hudakz 0:87642278ede6 63
hudakz 0:87642278ede6 64 FILE* f = fopen("/fs/numbers.txt", "r+");
hudakz 0:87642278ede6 65 printf("%s\n", (!f ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 66 if (!f) {
hudakz 0:87642278ede6 67 // Create the numbers file if it doesn't exist
hudakz 0:87642278ede6 68 printf("No file found, creating a new file... ");
hudakz 0:87642278ede6 69 fflush(stdout);
hudakz 0:87642278ede6 70 f = fopen("/fs/numbers.txt", "w+");
hudakz 0:87642278ede6 71 printf("%s\n", (!f ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 72 if (!f) {
hudakz 0:87642278ede6 73 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 74 }
hudakz 0:87642278ede6 75
hudakz 0:87642278ede6 76 for (int i = 0; i < 10; i++) {
hudakz 0:87642278ede6 77 printf("\rWriting numbers (%d/%d)... ", i, 10);
hudakz 0:87642278ede6 78 fflush(stdout);
hudakz 0:87642278ede6 79 err = fprintf(f, " %d\n", i);
hudakz 0:87642278ede6 80 if (err < 0) {
hudakz 0:87642278ede6 81 printf("Fail :(\n");
hudakz 0:87642278ede6 82 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 83 }
hudakz 0:87642278ede6 84 }
hudakz 0:87642278ede6 85
hudakz 0:87642278ede6 86 printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
hudakz 0:87642278ede6 87
hudakz 0:87642278ede6 88 printf("Seeking file... ");
hudakz 0:87642278ede6 89 fflush(stdout);
hudakz 0:87642278ede6 90 err = fseek(f, 0, SEEK_SET);
hudakz 0:87642278ede6 91 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 92 if (err < 0) {
hudakz 0:87642278ede6 93 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 94 }
hudakz 0:87642278ede6 95 }
hudakz 0:87642278ede6 96
hudakz 0:87642278ede6 97 // Go through and increment the numbers
hudakz 0:87642278ede6 98 for (int i = 0; i < 10; i++) {
hudakz 0:87642278ede6 99 printf("\rIncrementing numbers (%d/%d)... ", i, 10);
hudakz 0:87642278ede6 100 fflush(stdout);
hudakz 0:87642278ede6 101
hudakz 0:87642278ede6 102 // Get current stream position
hudakz 0:87642278ede6 103 long pos = ftell(f);
hudakz 0:87642278ede6 104
hudakz 0:87642278ede6 105 // Parse out the number and increment
hudakz 0:87642278ede6 106 int32_t number;
hudakz 0:87642278ede6 107 fscanf(f, "%ld", &number);
hudakz 0:87642278ede6 108 number += 1;
hudakz 0:87642278ede6 109
hudakz 0:87642278ede6 110 // Seek to beginning of number
hudakz 0:87642278ede6 111 fseek(f, pos, SEEK_SET);
hudakz 0:87642278ede6 112
hudakz 0:87642278ede6 113 // Store number
hudakz 0:87642278ede6 114 fprintf(f, " %ld\n", number);
hudakz 0:87642278ede6 115
hudakz 0:87642278ede6 116 // Flush between write and read on same file
hudakz 0:87642278ede6 117 fflush(f);
hudakz 0:87642278ede6 118 }
hudakz 0:87642278ede6 119
hudakz 0:87642278ede6 120 printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
hudakz 0:87642278ede6 121
hudakz 0:87642278ede6 122 // Close the file which also flushes any cached writes
hudakz 0:87642278ede6 123 printf("Closing \"/fs/numbers.txt\"... ");
hudakz 0:87642278ede6 124 fflush(stdout);
hudakz 0:87642278ede6 125 err = fclose(f);
hudakz 0:87642278ede6 126 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 127 if (err < 0) {
hudakz 0:87642278ede6 128 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 129 }
hudakz 0:87642278ede6 130
hudakz 0:87642278ede6 131 // Display the root directory
hudakz 0:87642278ede6 132 printf("Opening the root directory... ");
hudakz 0:87642278ede6 133 fflush(stdout);
hudakz 0:87642278ede6 134
hudakz 0:87642278ede6 135 DIR* d = opendir("/fs/");
hudakz 0:87642278ede6 136 printf("%s\n", (!d ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 137 if (!d) {
hudakz 0:87642278ede6 138 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 139 }
hudakz 0:87642278ede6 140
hudakz 0:87642278ede6 141 printf("root directory:\n");
hudakz 0:87642278ede6 142 while (true) {
hudakz 0:87642278ede6 143 struct dirent* e = readdir(d);
hudakz 0:87642278ede6 144 if (!e) {
hudakz 0:87642278ede6 145 break;
hudakz 0:87642278ede6 146 }
hudakz 0:87642278ede6 147
hudakz 0:87642278ede6 148 printf(" %s\n", e->d_name);
hudakz 0:87642278ede6 149 }
hudakz 0:87642278ede6 150
hudakz 0:87642278ede6 151 printf("Closing the root directory... ");
hudakz 0:87642278ede6 152 fflush(stdout);
hudakz 0:87642278ede6 153 err = closedir(d);
hudakz 0:87642278ede6 154 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 155 if (err < 0) {
hudakz 0:87642278ede6 156 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 157 }
hudakz 0:87642278ede6 158
hudakz 0:87642278ede6 159 // Display the numbers file
hudakz 0:87642278ede6 160 printf("Opening \"/fs/numbers.txt\"... ");
hudakz 0:87642278ede6 161 fflush(stdout);
hudakz 0:87642278ede6 162 f = fopen("/fs/numbers.txt", "r");
hudakz 0:87642278ede6 163 printf("%s\n", (!f ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 164 if (!f) {
hudakz 0:87642278ede6 165 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 166 }
hudakz 0:87642278ede6 167
hudakz 0:87642278ede6 168 printf("numbers:\n");
hudakz 0:87642278ede6 169 while (!feof(f)) {
hudakz 0:87642278ede6 170 int c = fgetc(f);
hudakz 0:87642278ede6 171 printf("%c", c);
hudakz 0:87642278ede6 172 }
hudakz 0:87642278ede6 173
hudakz 0:87642278ede6 174 printf("\rClosing \"/fs/numbers.txt\"... ");
hudakz 0:87642278ede6 175 fflush(stdout);
hudakz 0:87642278ede6 176 err = fclose(f);
hudakz 0:87642278ede6 177 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 178 if (err < 0) {
hudakz 0:87642278ede6 179 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 180 }
hudakz 0:87642278ede6 181
hudakz 0:87642278ede6 182 // Tidy up
hudakz 0:87642278ede6 183 printf("Unmounting... ");
hudakz 0:87642278ede6 184 fflush(stdout);
hudakz 0:87642278ede6 185 err = fileSystem.unmount();
hudakz 0:87642278ede6 186 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 187 if (err < 0) {
hudakz 0:87642278ede6 188 error("error: %s (%d)\n", strerror(-err), err);
hudakz 0:87642278ede6 189 }
hudakz 1:9a5f06e7969e 190
hudakz 1:9a5f06e7969e 191 printf("Initializing the block device... ");
hudakz 1:9a5f06e7969e 192 fflush(stdout);
hudakz 0:87642278ede6 193
hudakz 1:9a5f06e7969e 194 err = blockDevice.init();
hudakz 1:9a5f06e7969e 195 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 1:9a5f06e7969e 196 if (err) {
hudakz 1:9a5f06e7969e 197 error("error: %s (%d)\n", strerror(-err), err);
hudakz 1:9a5f06e7969e 198 }
hudakz 1:9a5f06e7969e 199
hudakz 1:9a5f06e7969e 200 printf("Erasing the block device... ");
hudakz 1:9a5f06e7969e 201 fflush(stdout);
hudakz 1:9a5f06e7969e 202 err = blockDevice.erase(0, blockDevice.size());
hudakz 1:9a5f06e7969e 203 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 1:9a5f06e7969e 204 if (err) {
hudakz 1:9a5f06e7969e 205 error("error: %s (%d)\n", strerror(-err), err);
hudakz 1:9a5f06e7969e 206 }
hudakz 1:9a5f06e7969e 207
hudakz 1:9a5f06e7969e 208 printf("Deinitializing the block device... ");
hudakz 1:9a5f06e7969e 209 fflush(stdout);
hudakz 1:9a5f06e7969e 210 err = blockDevice.deinit();
hudakz 1:9a5f06e7969e 211 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 1:9a5f06e7969e 212 if (err) {
hudakz 1:9a5f06e7969e 213 error("error: %s (%d)\n", strerror(-err), err);
hudakz 1:9a5f06e7969e 214 }
hudakz 1:9a5f06e7969e 215
hudakz 1:9a5f06e7969e 216 printf("\r\n");
hudakz 1:9a5f06e7969e 217
hudakz 0:87642278ede6 218 printf("Mbed OS filesystem example done!\n");
hudakz 0:87642278ede6 219 }