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