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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include <stdio.h>
00018 #include <errno.h>
00019 
00020 #include "BlockDevice.h"
00021 #include "SDIOBlockDevice.h"
00022 
00023 // This will take the system's default block device
00024 //BlockDevice *bd = BlockDevice::get_default_instance();
00025 BlockDevice *bd = new SDIOBlockDevice();
00026 
00027 // Instead of the default block device, you can define your own block device.
00028 // For example: HeapBlockDevice with size of 2048 bytes, read size 1, write size 1 and erase size 512.
00029 // #include "HeapBlockDevice.h"
00030 // BlockDevice *bd = new HeapBlockDevice(2048, 1, 1, 512);
00031 
00032 
00033 // This example uses LittleFileSystem as the default file system
00034 #include "LittleFileSystem.h"
00035 LittleFileSystem fs("fs");
00036 
00037 // Uncomment the following two lines and comment the previous two to use FAT file system.
00038 // #include "FATFileSystem.h"
00039 // FATFileSystem fs("fs");
00040 
00041 
00042 // Set up the button to trigger an erase
00043 //InterruptIn irq(BUTTON1);
00044 InterruptIn irq(SW2);
00045 
00046 void erase() {
00047     printf("Initializing the block device... ");
00048     fflush(stdout);
00049     int err = bd->init();
00050     printf("%s\n", (err ? "Fail :(" : "OK"));
00051     if (err) {
00052         error("error: %s (%d)\n", strerror(-err), err);
00053     }
00054 
00055     printf("Erasing the block device... ");
00056     fflush(stdout);
00057     err = bd->erase(0, bd->size());
00058     printf("%s\n", (err ? "Fail :(" : "OK"));
00059     if (err) {
00060         error("error: %s (%d)\n", strerror(-err), err);
00061     }
00062 
00063     printf("Deinitializing the block device... ");
00064     fflush(stdout);
00065     err = bd->deinit();
00066     printf("%s\n", (err ? "Fail :(" : "OK"));
00067     if (err) {
00068         error("error: %s (%d)\n", strerror(-err), err);
00069     }
00070 }
00071 
00072 
00073 // Entry point for the example
00074 int main() {
00075     printf("--- Mbed OS filesystem example ---\n");
00076 
00077     // Setup the erase event on button press, use the event queue
00078     // to avoid running in interrupt context
00079     irq.fall(mbed_event_queue()->event(erase));
00080 
00081     // Try to mount the filesystem
00082     printf("Mounting the filesystem... ");
00083     fflush(stdout);
00084     int err = fs.mount(bd);
00085     printf("%s\n", (err ? "Fail :(" : "OK"));
00086     if (err) {
00087         // Reformat if we can't mount the filesystem
00088         // this should only happen on the first boot
00089         printf("No filesystem found, formatting... ");
00090         fflush(stdout);
00091         err = fs.reformat(bd);
00092         printf("%s\n", (err ? "Fail :(" : "OK"));
00093         if (err) {
00094             error("error: %s (%d)\n", strerror(-err), err);
00095         }
00096     }
00097 
00098     // Open the numbers file
00099     printf("Opening \"/fs/numbers.txt\"... ");
00100     fflush(stdout);
00101     FILE *f = fopen("/fs/numbers.txt", "r+");
00102     printf("%s\n", (!f ? "Fail :(" : "OK"));
00103     if (!f) {
00104         // Create the numbers file if it doesn't exist
00105         printf("No file found, creating a new file... ");
00106         fflush(stdout);
00107         f = fopen("/fs/numbers.txt", "w+");
00108         printf("%s\n", (!f ? "Fail :(" : "OK"));
00109         if (!f) {
00110             error("error: %s (%d)\n", strerror(errno), -errno);
00111         }
00112 
00113         for (int i = 0; i < 10; i++) {
00114             printf("\rWriting numbers (%d/%d)... ", i, 10);
00115             fflush(stdout);
00116             err = fprintf(f, "    %d\n", i);
00117             if (err < 0) {
00118                 printf("Fail :(\n");
00119                 error("error: %s (%d)\n", strerror(errno), -errno);
00120             }
00121         }
00122         printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
00123 
00124         printf("Seeking file... ");
00125         fflush(stdout);
00126         err = fseek(f, 0, SEEK_SET);
00127         printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00128         if (err < 0) {
00129             error("error: %s (%d)\n", strerror(errno), -errno);
00130         }
00131     }
00132 
00133     // Go through and increment the numbers
00134     for (int i = 0; i < 10; i++) {
00135         printf("\rIncrementing numbers (%d/%d)... ", i, 10);
00136         fflush(stdout);
00137 
00138         // Get current stream position
00139         long pos = ftell(f);
00140 
00141         // Parse out the number and increment
00142         int32_t number;
00143         fscanf(f, "%d", &number);
00144         number += 1;
00145 
00146         // Seek to beginning of number
00147         fseek(f, pos, SEEK_SET);
00148     
00149         // Store number
00150         fprintf(f, "    %d\n", number);
00151 
00152         // Flush between write and read on same file
00153         fflush(f);
00154     }
00155     printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
00156 
00157     // Close the file which also flushes any cached writes
00158     printf("Closing \"/fs/numbers.txt\"... ");
00159     fflush(stdout);
00160     err = fclose(f);
00161     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00162     if (err < 0) {
00163         error("error: %s (%d)\n", strerror(errno), -errno);
00164     }
00165     
00166     // Display the root directory
00167     printf("Opening the root directory... ");
00168     fflush(stdout);
00169     DIR *d = opendir("/fs/");
00170     printf("%s\n", (!d ? "Fail :(" : "OK"));
00171     if (!d) {
00172         error("error: %s (%d)\n", strerror(errno), -errno);
00173     }
00174 
00175     printf("root directory:\n");
00176     while (true) {
00177         struct dirent *e = readdir(d);
00178         if (!e) {
00179             break;
00180         }
00181 
00182         printf("    %s\n", e->d_name);
00183     }
00184 
00185     printf("Closing the root directory... ");
00186     fflush(stdout);
00187     err = closedir(d);
00188     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00189     if (err < 0) {
00190         error("error: %s (%d)\n", strerror(errno), -errno);
00191     }
00192 
00193     // Display the numbers file
00194     printf("Opening \"/fs/numbers.txt\"... ");
00195     fflush(stdout);
00196     f = fopen("/fs/numbers.txt", "r");
00197     printf("%s\n", (!f ? "Fail :(" : "OK"));
00198     if (!f) {
00199         error("error: %s (%d)\n", strerror(errno), -errno);
00200     }
00201 
00202     printf("numbers:\n");
00203     while (!feof(f)) {
00204         int c = fgetc(f);
00205         printf("%c", c);
00206     }
00207 
00208     printf("\rClosing \"/fs/numbers.txt\"... ");
00209     fflush(stdout);
00210     err = fclose(f);
00211     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00212     if (err < 0) {
00213         error("error: %s (%d)\n", strerror(errno), -errno);
00214     }
00215 
00216     // Tidy up
00217     printf("Unmounting... ");
00218     fflush(stdout);
00219     err = fs.unmount();
00220     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00221     if (err < 0) {
00222         error("error: %s (%d)\n", strerror(-err), err);
00223     }
00224         
00225     printf("Mbed OS filesystem example done!\n");
00226 }