AccellerationFileNV Assignment
Dependencies: BSP_B-L475E-IOT01
main.cpp@25:65a9183a2178, 2018-09-26 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Sep 26 15:01:07 2018 +0100
- Revision:
- 25:65a9183a2178
- Parent:
- 12:a07d0af60cc6
- Child:
- 26:130bb1173866
Merge pull request #68 from ARMmbed/mbed-os-5.10.0-oob
Update to Mbed os 5.10.0
.
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 | 0:8e251d9511b8 | 2 | * Copyright (c) 2006-2013 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 | 0:8e251d9511b8 | 20 | // Block devices |
mbed_official | 25:65a9183a2178 | 21 | #if COMPONENT_SPIF |
mbed_official | 0:8e251d9511b8 | 22 | #include "SPIFBlockDevice.h" |
mbed_official | 25:65a9183a2178 | 23 | #endif |
mbed_official | 25:65a9183a2178 | 24 | |
mbed_official | 25:65a9183a2178 | 25 | #if COMPONENT_DATAFLASH |
mbed_official | 1:2bfc377bcc2a | 26 | #include "DataFlashBlockDevice.h" |
mbed_official | 25:65a9183a2178 | 27 | #endif |
mbed_official | 25:65a9183a2178 | 28 | |
mbed_official | 25:65a9183a2178 | 29 | #if COMPONENT_SD |
mbed_official | 0:8e251d9511b8 | 30 | #include "SDBlockDevice.h" |
mbed_official | 25:65a9183a2178 | 31 | #endif |
mbed_official | 25:65a9183a2178 | 32 | |
mbed_official | 0:8e251d9511b8 | 33 | #include "HeapBlockDevice.h" |
mbed_official | 0:8e251d9511b8 | 34 | |
mbed_official | 0:8e251d9511b8 | 35 | // File systems |
mbed_official | 0:8e251d9511b8 | 36 | #include "LittleFileSystem.h" |
mbed_official | 0:8e251d9511b8 | 37 | #include "FATFileSystem.h" |
mbed_official | 0:8e251d9511b8 | 38 | |
mbed_official | 0:8e251d9511b8 | 39 | |
mbed_official | 0:8e251d9511b8 | 40 | // Physical block device, can be any device that supports the BlockDevice API |
mbed_official | 0:8e251d9511b8 | 41 | SPIFBlockDevice bd( |
mbed_official | 0:8e251d9511b8 | 42 | MBED_CONF_SPIF_DRIVER_SPI_MOSI, |
mbed_official | 0:8e251d9511b8 | 43 | MBED_CONF_SPIF_DRIVER_SPI_MISO, |
mbed_official | 0:8e251d9511b8 | 44 | MBED_CONF_SPIF_DRIVER_SPI_CLK, |
mbed_official | 0:8e251d9511b8 | 45 | MBED_CONF_SPIF_DRIVER_SPI_CS); |
mbed_official | 0:8e251d9511b8 | 46 | |
mbed_official | 0:8e251d9511b8 | 47 | // File system declaration |
mbed_official | 0:8e251d9511b8 | 48 | LittleFileSystem fs("fs"); |
mbed_official | 0:8e251d9511b8 | 49 | |
mbed_official | 0:8e251d9511b8 | 50 | |
mbed_official | 0:8e251d9511b8 | 51 | // Set up the button to trigger an erase |
mbed_official | 0:8e251d9511b8 | 52 | InterruptIn irq(BUTTON1); |
mbed_official | 0:8e251d9511b8 | 53 | void erase() { |
mbed_official | 0:8e251d9511b8 | 54 | printf("Initializing the block device... "); |
mbed_official | 0:8e251d9511b8 | 55 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 56 | int err = bd.init(); |
mbed_official | 0:8e251d9511b8 | 57 | printf("%s\n", (err ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 58 | if (err) { |
mbed_official | 0:8e251d9511b8 | 59 | error("error: %s (%d)\n", strerror(-err), err); |
mbed_official | 0:8e251d9511b8 | 60 | } |
mbed_official | 0:8e251d9511b8 | 61 | |
mbed_official | 0:8e251d9511b8 | 62 | printf("Erasing the block device... "); |
mbed_official | 0:8e251d9511b8 | 63 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 64 | err = bd.erase(0, bd.size()); |
mbed_official | 0:8e251d9511b8 | 65 | printf("%s\n", (err ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 66 | if (err) { |
mbed_official | 0:8e251d9511b8 | 67 | error("error: %s (%d)\n", strerror(-err), err); |
mbed_official | 0:8e251d9511b8 | 68 | } |
mbed_official | 0:8e251d9511b8 | 69 | |
mbed_official | 0:8e251d9511b8 | 70 | printf("Deinitializing the block device... "); |
mbed_official | 0:8e251d9511b8 | 71 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 72 | err = bd.deinit(); |
mbed_official | 0:8e251d9511b8 | 73 | printf("%s\n", (err ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 74 | if (err) { |
mbed_official | 0:8e251d9511b8 | 75 | error("error: %s (%d)\n", strerror(-err), err); |
mbed_official | 0:8e251d9511b8 | 76 | } |
mbed_official | 0:8e251d9511b8 | 77 | } |
mbed_official | 0:8e251d9511b8 | 78 | |
mbed_official | 0:8e251d9511b8 | 79 | |
mbed_official | 0:8e251d9511b8 | 80 | // Entry point for the example |
mbed_official | 0:8e251d9511b8 | 81 | int main() { |
mbed_official | 0:8e251d9511b8 | 82 | printf("--- Mbed OS filesystem example ---\n"); |
mbed_official | 0:8e251d9511b8 | 83 | |
mbed_official | 12:a07d0af60cc6 | 84 | // Setup the erase event on button press, use the event queue |
mbed_official | 12:a07d0af60cc6 | 85 | // to avoid running in interrupt context |
mbed_official | 12:a07d0af60cc6 | 86 | irq.fall(mbed_event_queue()->event(erase)); |
mbed_official | 0:8e251d9511b8 | 87 | |
mbed_official | 0:8e251d9511b8 | 88 | // Try to mount the filesystem |
mbed_official | 0:8e251d9511b8 | 89 | printf("Mounting the filesystem... "); |
mbed_official | 0:8e251d9511b8 | 90 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 91 | int err = fs.mount(&bd); |
mbed_official | 0:8e251d9511b8 | 92 | printf("%s\n", (err ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 93 | if (err) { |
mbed_official | 0:8e251d9511b8 | 94 | // Reformat if we can't mount the filesystem |
mbed_official | 0:8e251d9511b8 | 95 | // this should only happen on the first boot |
mbed_official | 0:8e251d9511b8 | 96 | printf("No filesystem found, formatting... "); |
mbed_official | 0:8e251d9511b8 | 97 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 98 | err = fs.reformat(&bd); |
mbed_official | 0:8e251d9511b8 | 99 | printf("%s\n", (err ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 100 | if (err) { |
mbed_official | 0:8e251d9511b8 | 101 | error("error: %s (%d)\n", strerror(-err), err); |
mbed_official | 0:8e251d9511b8 | 102 | } |
mbed_official | 0:8e251d9511b8 | 103 | } |
mbed_official | 0:8e251d9511b8 | 104 | |
mbed_official | 0:8e251d9511b8 | 105 | // Open the numbers file |
mbed_official | 0:8e251d9511b8 | 106 | printf("Opening \"/fs/numbers.txt\"... "); |
mbed_official | 0:8e251d9511b8 | 107 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 108 | FILE *f = fopen("/fs/numbers.txt", "r+"); |
mbed_official | 0:8e251d9511b8 | 109 | printf("%s\n", (!f ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 110 | if (!f) { |
mbed_official | 0:8e251d9511b8 | 111 | // Create the numbers file if it doesn't exist |
mbed_official | 0:8e251d9511b8 | 112 | printf("No file found, creating a new file... "); |
mbed_official | 0:8e251d9511b8 | 113 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 114 | f = fopen("/fs/numbers.txt", "w+"); |
mbed_official | 0:8e251d9511b8 | 115 | printf("%s\n", (!f ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 116 | if (!f) { |
mbed_official | 0:8e251d9511b8 | 117 | error("error: %s (%d)\n", strerror(errno), -errno); |
mbed_official | 0:8e251d9511b8 | 118 | } |
mbed_official | 0:8e251d9511b8 | 119 | |
mbed_official | 0:8e251d9511b8 | 120 | for (int i = 0; i < 10; i++) { |
mbed_official | 0:8e251d9511b8 | 121 | printf("\rWriting numbers (%d/%d)... ", i, 10); |
mbed_official | 0:8e251d9511b8 | 122 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 123 | err = fprintf(f, " %d\n", i); |
mbed_official | 0:8e251d9511b8 | 124 | if (err < 0) { |
mbed_official | 0:8e251d9511b8 | 125 | printf("Fail :(\n"); |
mbed_official | 0:8e251d9511b8 | 126 | error("error: %s (%d)\n", strerror(errno), -errno); |
mbed_official | 0:8e251d9511b8 | 127 | } |
mbed_official | 0:8e251d9511b8 | 128 | } |
mbed_official | 0:8e251d9511b8 | 129 | printf("\rWriting numbers (%d/%d)... OK\n", 10, 10); |
mbed_official | 0:8e251d9511b8 | 130 | |
mbed_official | 0:8e251d9511b8 | 131 | printf("Seeking file... "); |
mbed_official | 0:8e251d9511b8 | 132 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 133 | err = fseek(f, 0, SEEK_SET); |
mbed_official | 0:8e251d9511b8 | 134 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 135 | if (err < 0) { |
mbed_official | 0:8e251d9511b8 | 136 | error("error: %s (%d)\n", strerror(errno), -errno); |
mbed_official | 0:8e251d9511b8 | 137 | } |
mbed_official | 0:8e251d9511b8 | 138 | } |
mbed_official | 0:8e251d9511b8 | 139 | |
mbed_official | 0:8e251d9511b8 | 140 | // Go through and increment the numbers |
mbed_official | 0:8e251d9511b8 | 141 | for (int i = 0; i < 10; i++) { |
mbed_official | 0:8e251d9511b8 | 142 | printf("\rIncrementing numbers (%d/%d)... ", i, 10); |
mbed_official | 0:8e251d9511b8 | 143 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 144 | |
mbed_official | 0:8e251d9511b8 | 145 | // Get current stream position |
mbed_official | 0:8e251d9511b8 | 146 | long pos = ftell(f); |
mbed_official | 0:8e251d9511b8 | 147 | |
mbed_official | 0:8e251d9511b8 | 148 | // Parse out the number and increment |
mbed_official | 0:8e251d9511b8 | 149 | int32_t number; |
mbed_official | 0:8e251d9511b8 | 150 | fscanf(f, "%d", &number); |
mbed_official | 0:8e251d9511b8 | 151 | number += 1; |
mbed_official | 0:8e251d9511b8 | 152 | |
mbed_official | 0:8e251d9511b8 | 153 | // Seek to beginning of number |
mbed_official | 0:8e251d9511b8 | 154 | fseek(f, pos, SEEK_SET); |
mbed_official | 0:8e251d9511b8 | 155 | |
mbed_official | 0:8e251d9511b8 | 156 | // Store number |
mbed_official | 0:8e251d9511b8 | 157 | fprintf(f, " %d\n", number); |
mbed_official | 10:38d6b74b0eb7 | 158 | |
mbed_official | 10:38d6b74b0eb7 | 159 | // Flush between write and read on same file |
mbed_official | 10:38d6b74b0eb7 | 160 | fflush(f); |
mbed_official | 0:8e251d9511b8 | 161 | } |
mbed_official | 0:8e251d9511b8 | 162 | printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10); |
mbed_official | 0:8e251d9511b8 | 163 | |
mbed_official | 0:8e251d9511b8 | 164 | // Close the file which also flushes any cached writes |
mbed_official | 0:8e251d9511b8 | 165 | printf("Closing \"/fs/numbers.txt\"... "); |
mbed_official | 0:8e251d9511b8 | 166 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 167 | err = fclose(f); |
mbed_official | 0:8e251d9511b8 | 168 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 169 | if (err < 0) { |
mbed_official | 0:8e251d9511b8 | 170 | error("error: %s (%d)\n", strerror(errno), -errno); |
mbed_official | 0:8e251d9511b8 | 171 | } |
mbed_official | 0:8e251d9511b8 | 172 | |
mbed_official | 0:8e251d9511b8 | 173 | // Display the root directory |
mbed_official | 0:8e251d9511b8 | 174 | printf("Opening the root directory... "); |
mbed_official | 0:8e251d9511b8 | 175 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 176 | DIR *d = opendir("/fs/"); |
mbed_official | 0:8e251d9511b8 | 177 | printf("%s\n", (!d ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 178 | if (!d) { |
mbed_official | 0:8e251d9511b8 | 179 | error("error: %s (%d)\n", strerror(errno), -errno); |
mbed_official | 0:8e251d9511b8 | 180 | } |
mbed_official | 0:8e251d9511b8 | 181 | |
mbed_official | 0:8e251d9511b8 | 182 | printf("root directory:\n"); |
mbed_official | 0:8e251d9511b8 | 183 | while (true) { |
mbed_official | 0:8e251d9511b8 | 184 | struct dirent *e = readdir(d); |
mbed_official | 0:8e251d9511b8 | 185 | if (!e) { |
mbed_official | 0:8e251d9511b8 | 186 | break; |
mbed_official | 0:8e251d9511b8 | 187 | } |
mbed_official | 0:8e251d9511b8 | 188 | |
mbed_official | 0:8e251d9511b8 | 189 | printf(" %s\n", e->d_name); |
mbed_official | 0:8e251d9511b8 | 190 | } |
mbed_official | 0:8e251d9511b8 | 191 | |
mbed_official | 0:8e251d9511b8 | 192 | printf("Closing the root directory... "); |
mbed_official | 0:8e251d9511b8 | 193 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 194 | err = closedir(d); |
mbed_official | 0:8e251d9511b8 | 195 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 196 | if (err < 0) { |
mbed_official | 0:8e251d9511b8 | 197 | error("error: %s (%d)\n", strerror(errno), -errno); |
mbed_official | 0:8e251d9511b8 | 198 | } |
mbed_official | 0:8e251d9511b8 | 199 | |
mbed_official | 0:8e251d9511b8 | 200 | // Display the numbers file |
mbed_official | 0:8e251d9511b8 | 201 | printf("Opening \"/fs/numbers.txt\"... "); |
mbed_official | 0:8e251d9511b8 | 202 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 203 | f = fopen("/fs/numbers.txt", "r"); |
mbed_official | 0:8e251d9511b8 | 204 | printf("%s\n", (!f ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 205 | if (!f) { |
mbed_official | 0:8e251d9511b8 | 206 | error("error: %s (%d)\n", strerror(errno), -errno); |
mbed_official | 0:8e251d9511b8 | 207 | } |
mbed_official | 0:8e251d9511b8 | 208 | |
mbed_official | 0:8e251d9511b8 | 209 | printf("numbers:\n"); |
mbed_official | 0:8e251d9511b8 | 210 | while (!feof(f)) { |
mbed_official | 0:8e251d9511b8 | 211 | int c = fgetc(f); |
mbed_official | 0:8e251d9511b8 | 212 | printf("%c", c); |
mbed_official | 0:8e251d9511b8 | 213 | } |
mbed_official | 0:8e251d9511b8 | 214 | |
mbed_official | 0:8e251d9511b8 | 215 | printf("\rClosing \"/fs/numbers.txt\"... "); |
mbed_official | 0:8e251d9511b8 | 216 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 217 | err = fclose(f); |
mbed_official | 0:8e251d9511b8 | 218 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 219 | if (err < 0) { |
mbed_official | 0:8e251d9511b8 | 220 | error("error: %s (%d)\n", strerror(errno), -errno); |
mbed_official | 0:8e251d9511b8 | 221 | } |
mbed_official | 0:8e251d9511b8 | 222 | |
mbed_official | 0:8e251d9511b8 | 223 | // Tidy up |
mbed_official | 0:8e251d9511b8 | 224 | printf("Unmounting... "); |
mbed_official | 0:8e251d9511b8 | 225 | fflush(stdout); |
mbed_official | 0:8e251d9511b8 | 226 | err = fs.unmount(); |
mbed_official | 0:8e251d9511b8 | 227 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mbed_official | 0:8e251d9511b8 | 228 | if (err < 0) { |
mbed_official | 0:8e251d9511b8 | 229 | error("error: %s (%d)\n", strerror(-err), err); |
mbed_official | 0:8e251d9511b8 | 230 | } |
mbed_official | 0:8e251d9511b8 | 231 | |
mbed_official | 0:8e251d9511b8 | 232 | printf("Mbed OS filesystem example done!\n"); |
mbed_official | 0:8e251d9511b8 | 233 | } |
mbed_official | 0:8e251d9511b8 | 234 |