Filesystem example for LPC55S69 using on-board SD Card (SDIO).

Committer:
maclobdell
Date:
Wed May 01 20:11:38 2019 +0000
Revision:
2:798a7c4c0398
Parent:
0:ad2237362248
update to mbed os 5.12.2

Who changed what in which revision?

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