Example of using SD card with Mbed OS5.

Committer:
hudakz
Date:
Wed Feb 06 18:46:53 2019 +0000
Revision:
0:87642278ede6
Child:
1:9a5f06e7969e
Initial release.

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 0:87642278ede6 32 SDBlockDevice blockDevice(p5, p6, p7, p8);
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 // Set up the button to trigger an erase
hudakz 0:87642278ede6 38 InterruptIn btnErase(p11);
hudakz 0:87642278ede6 39
hudakz 0:87642278ede6 40 void erase()
hudakz 0:87642278ede6 41 {
hudakz 0:87642278ede6 42 printf("Initializing the block device... ");
hudakz 0:87642278ede6 43 fflush(stdout);
hudakz 0:87642278ede6 44
hudakz 0:87642278ede6 45 int err = blockDevice.init();
hudakz 0:87642278ede6 46 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 47 if (err) {
hudakz 0:87642278ede6 48 error("error: %s (%d)\n", strerror(-err), err);
hudakz 0:87642278ede6 49 }
hudakz 0:87642278ede6 50
hudakz 0:87642278ede6 51 printf("Erasing the block device... ");
hudakz 0:87642278ede6 52 fflush(stdout);
hudakz 0:87642278ede6 53 err = blockDevice.erase(0, blockDevice.size());
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 printf("Deinitializing the block device... ");
hudakz 0:87642278ede6 60 fflush(stdout);
hudakz 0:87642278ede6 61 err = blockDevice.deinit();
hudakz 0:87642278ede6 62 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 63 if (err) {
hudakz 0:87642278ede6 64 error("error: %s (%d)\n", strerror(-err), err);
hudakz 0:87642278ede6 65 }
hudakz 0:87642278ede6 66 }
hudakz 0:87642278ede6 67
hudakz 0:87642278ede6 68 // Entry point for the example
hudakz 0:87642278ede6 69 int main()
hudakz 0:87642278ede6 70 {
hudakz 0:87642278ede6 71 printf("--- Mbed OS filesystem example ---\n");
hudakz 0:87642278ede6 72
hudakz 0:87642278ede6 73 // Setup the erase event on button press, use the event queue
hudakz 0:87642278ede6 74 // to avoid running in interrupt context
hudakz 0:87642278ede6 75 btnErase.fall(mbed_event_queue()->event(erase));
hudakz 0:87642278ede6 76
hudakz 0:87642278ede6 77 // Try to mount the filesystem
hudakz 0:87642278ede6 78 printf("Mounting the filesystem... ");
hudakz 0:87642278ede6 79 fflush(stdout);
hudakz 0:87642278ede6 80
hudakz 0:87642278ede6 81 int err = fileSystem.mount(&blockDevice);
hudakz 0:87642278ede6 82 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 83 if (err) {
hudakz 0:87642278ede6 84 // Reformat if we can't mount the filesystem
hudakz 0:87642278ede6 85 // this should only happen on the first boot
hudakz 0:87642278ede6 86 printf("No filesystem found, formatting... ");
hudakz 0:87642278ede6 87 fflush(stdout);
hudakz 0:87642278ede6 88 err = fileSystem.reformat(&blockDevice);
hudakz 0:87642278ede6 89 printf("%s\n", (err ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 90 if (err) {
hudakz 0:87642278ede6 91 error("error: %s (%d)\n", strerror(-err), err);
hudakz 0:87642278ede6 92 }
hudakz 0:87642278ede6 93 }
hudakz 0:87642278ede6 94
hudakz 0:87642278ede6 95 // Open the numbers file
hudakz 0:87642278ede6 96 printf("Opening \"/fs/numbers.txt\"... ");
hudakz 0:87642278ede6 97 fflush(stdout);
hudakz 0:87642278ede6 98
hudakz 0:87642278ede6 99 FILE* f = fopen("/fs/numbers.txt", "r+");
hudakz 0:87642278ede6 100 printf("%s\n", (!f ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 101 if (!f) {
hudakz 0:87642278ede6 102 // Create the numbers file if it doesn't exist
hudakz 0:87642278ede6 103 printf("No file found, creating a new file... ");
hudakz 0:87642278ede6 104 fflush(stdout);
hudakz 0:87642278ede6 105 f = fopen("/fs/numbers.txt", "w+");
hudakz 0:87642278ede6 106 printf("%s\n", (!f ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 107 if (!f) {
hudakz 0:87642278ede6 108 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 109 }
hudakz 0:87642278ede6 110
hudakz 0:87642278ede6 111 for (int i = 0; i < 10; i++) {
hudakz 0:87642278ede6 112 printf("\rWriting numbers (%d/%d)... ", i, 10);
hudakz 0:87642278ede6 113 fflush(stdout);
hudakz 0:87642278ede6 114 err = fprintf(f, " %d\n", i);
hudakz 0:87642278ede6 115 if (err < 0) {
hudakz 0:87642278ede6 116 printf("Fail :(\n");
hudakz 0:87642278ede6 117 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 118 }
hudakz 0:87642278ede6 119 }
hudakz 0:87642278ede6 120
hudakz 0:87642278ede6 121 printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
hudakz 0:87642278ede6 122
hudakz 0:87642278ede6 123 printf("Seeking file... ");
hudakz 0:87642278ede6 124 fflush(stdout);
hudakz 0:87642278ede6 125 err = fseek(f, 0, SEEK_SET);
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
hudakz 0:87642278ede6 132 // Go through and increment the numbers
hudakz 0:87642278ede6 133 for (int i = 0; i < 10; i++) {
hudakz 0:87642278ede6 134 printf("\rIncrementing numbers (%d/%d)... ", i, 10);
hudakz 0:87642278ede6 135 fflush(stdout);
hudakz 0:87642278ede6 136
hudakz 0:87642278ede6 137 // Get current stream position
hudakz 0:87642278ede6 138 long pos = ftell(f);
hudakz 0:87642278ede6 139
hudakz 0:87642278ede6 140 // Parse out the number and increment
hudakz 0:87642278ede6 141 int32_t number;
hudakz 0:87642278ede6 142 fscanf(f, "%ld", &number);
hudakz 0:87642278ede6 143 number += 1;
hudakz 0:87642278ede6 144
hudakz 0:87642278ede6 145 // Seek to beginning of number
hudakz 0:87642278ede6 146 fseek(f, pos, SEEK_SET);
hudakz 0:87642278ede6 147
hudakz 0:87642278ede6 148 // Store number
hudakz 0:87642278ede6 149 fprintf(f, " %ld\n", number);
hudakz 0:87642278ede6 150
hudakz 0:87642278ede6 151 // Flush between write and read on same file
hudakz 0:87642278ede6 152 fflush(f);
hudakz 0:87642278ede6 153 }
hudakz 0:87642278ede6 154
hudakz 0:87642278ede6 155 printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
hudakz 0:87642278ede6 156
hudakz 0:87642278ede6 157 // Close the file which also flushes any cached writes
hudakz 0:87642278ede6 158 printf("Closing \"/fs/numbers.txt\"... ");
hudakz 0:87642278ede6 159 fflush(stdout);
hudakz 0:87642278ede6 160 err = fclose(f);
hudakz 0:87642278ede6 161 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 162 if (err < 0) {
hudakz 0:87642278ede6 163 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 164 }
hudakz 0:87642278ede6 165
hudakz 0:87642278ede6 166 // Display the root directory
hudakz 0:87642278ede6 167 printf("Opening the root directory... ");
hudakz 0:87642278ede6 168 fflush(stdout);
hudakz 0:87642278ede6 169
hudakz 0:87642278ede6 170 DIR* d = opendir("/fs/");
hudakz 0:87642278ede6 171 printf("%s\n", (!d ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 172 if (!d) {
hudakz 0:87642278ede6 173 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 174 }
hudakz 0:87642278ede6 175
hudakz 0:87642278ede6 176 printf("root directory:\n");
hudakz 0:87642278ede6 177 while (true) {
hudakz 0:87642278ede6 178 struct dirent* e = readdir(d);
hudakz 0:87642278ede6 179 if (!e) {
hudakz 0:87642278ede6 180 break;
hudakz 0:87642278ede6 181 }
hudakz 0:87642278ede6 182
hudakz 0:87642278ede6 183 printf(" %s\n", e->d_name);
hudakz 0:87642278ede6 184 }
hudakz 0:87642278ede6 185
hudakz 0:87642278ede6 186 printf("Closing the root directory... ");
hudakz 0:87642278ede6 187 fflush(stdout);
hudakz 0:87642278ede6 188 err = closedir(d);
hudakz 0:87642278ede6 189 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 190 if (err < 0) {
hudakz 0:87642278ede6 191 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 192 }
hudakz 0:87642278ede6 193
hudakz 0:87642278ede6 194 // Display the numbers file
hudakz 0:87642278ede6 195 printf("Opening \"/fs/numbers.txt\"... ");
hudakz 0:87642278ede6 196 fflush(stdout);
hudakz 0:87642278ede6 197 f = fopen("/fs/numbers.txt", "r");
hudakz 0:87642278ede6 198 printf("%s\n", (!f ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 199 if (!f) {
hudakz 0:87642278ede6 200 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 201 }
hudakz 0:87642278ede6 202
hudakz 0:87642278ede6 203 printf("numbers:\n");
hudakz 0:87642278ede6 204 while (!feof(f)) {
hudakz 0:87642278ede6 205 int c = fgetc(f);
hudakz 0:87642278ede6 206 printf("%c", c);
hudakz 0:87642278ede6 207 }
hudakz 0:87642278ede6 208
hudakz 0:87642278ede6 209 printf("\rClosing \"/fs/numbers.txt\"... ");
hudakz 0:87642278ede6 210 fflush(stdout);
hudakz 0:87642278ede6 211 err = fclose(f);
hudakz 0:87642278ede6 212 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 213 if (err < 0) {
hudakz 0:87642278ede6 214 error("error: %s (%d)\n", strerror(errno), -errno);
hudakz 0:87642278ede6 215 }
hudakz 0:87642278ede6 216
hudakz 0:87642278ede6 217 // Tidy up
hudakz 0:87642278ede6 218 printf("Unmounting... ");
hudakz 0:87642278ede6 219 fflush(stdout);
hudakz 0:87642278ede6 220 err = fileSystem.unmount();
hudakz 0:87642278ede6 221 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
hudakz 0:87642278ede6 222 if (err < 0) {
hudakz 0:87642278ede6 223 error("error: %s (%d)\n", strerror(-err), err);
hudakz 0:87642278ede6 224 }
hudakz 0:87642278ede6 225
hudakz 0:87642278ede6 226 printf("Mbed OS filesystem example done!\n");
hudakz 0:87642278ede6 227 }