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