mangue baja 1st working logger on stm32
Dependencies: LSM6DS3 DebouncedInterrupt
example.txt@5:122b9fd3df1a, 2022-09-21 (annotated)
- Committer:
- einsteingustavo
- Date:
- Wed Sep 21 21:23:51 2022 +0000
- Revision:
- 5:122b9fd3df1a
- Parent:
- 3:15d63b793f7b
2022 data logger
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
einsteingustavo | 3:15d63b793f7b | 1 | /* mbed Microcontroller Library |
einsteingustavo | 3:15d63b793f7b | 2 | * Copyright (c) 2006-2013 ARM Limited |
einsteingustavo | 3:15d63b793f7b | 3 | * |
einsteingustavo | 3:15d63b793f7b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
einsteingustavo | 3:15d63b793f7b | 5 | * you may not use this file except in compliance with the License. |
einsteingustavo | 3:15d63b793f7b | 6 | * You may obtain a copy of the License at |
einsteingustavo | 3:15d63b793f7b | 7 | * |
einsteingustavo | 3:15d63b793f7b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
einsteingustavo | 3:15d63b793f7b | 9 | * |
einsteingustavo | 3:15d63b793f7b | 10 | * Unless required by applicable law or agreed to in writing, software |
einsteingustavo | 3:15d63b793f7b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
einsteingustavo | 3:15d63b793f7b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
einsteingustavo | 3:15d63b793f7b | 13 | * See the License for the specific language governing permissions and |
einsteingustavo | 3:15d63b793f7b | 14 | * limitations under the License. |
einsteingustavo | 3:15d63b793f7b | 15 | */ |
einsteingustavo | 3:15d63b793f7b | 16 | #include "mbed.h" |
einsteingustavo | 3:15d63b793f7b | 17 | #include <stdio.h> |
einsteingustavo | 3:15d63b793f7b | 18 | #include <errno.h> |
einsteingustavo | 3:15d63b793f7b | 19 | |
einsteingustavo | 3:15d63b793f7b | 20 | // Block devices |
einsteingustavo | 3:15d63b793f7b | 21 | //#include "SPIFBlockDevice.h" |
einsteingustavo | 3:15d63b793f7b | 22 | //#include "DataFlashBlockDevice.h" |
einsteingustavo | 3:15d63b793f7b | 23 | #include "SDBlockDevice.h" |
einsteingustavo | 3:15d63b793f7b | 24 | //#include "HeapBlockDevice.h" |
einsteingustavo | 3:15d63b793f7b | 25 | |
einsteingustavo | 3:15d63b793f7b | 26 | // File systems |
einsteingustavo | 3:15d63b793f7b | 27 | |
einsteingustavo | 3:15d63b793f7b | 28 | //#include "LittleFileSystem.h" |
einsteingustavo | 3:15d63b793f7b | 29 | #include "FATFileSystem.h" |
einsteingustavo | 3:15d63b793f7b | 30 | |
einsteingustavo | 3:15d63b793f7b | 31 | // Physical block device, can be any device that supports the BlockDevice API |
einsteingustavo | 3:15d63b793f7b | 32 | SDBlockDevice blockDevice(PB_15, PB_14, PB_13, PB_12); // mosi, miso, sck, cs |
einsteingustavo | 3:15d63b793f7b | 33 | |
einsteingustavo | 3:15d63b793f7b | 34 | // File system declaration |
einsteingustavo | 3:15d63b793f7b | 35 | FATFileSystem fileSystem("fs"); |
einsteingustavo | 3:15d63b793f7b | 36 | |
einsteingustavo | 3:15d63b793f7b | 37 | Serial pc(PA_2,PA_3); |
einsteingustavo | 3:15d63b793f7b | 38 | |
einsteingustavo | 3:15d63b793f7b | 39 | // Entry point for the example |
einsteingustavo | 3:15d63b793f7b | 40 | int main() |
einsteingustavo | 3:15d63b793f7b | 41 | { |
einsteingustavo | 3:15d63b793f7b | 42 | pc.printf("--- Mbed OS filesystem example ---\n"); |
einsteingustavo | 3:15d63b793f7b | 43 | |
einsteingustavo | 3:15d63b793f7b | 44 | // Try to mount the filesystem |
einsteingustavo | 3:15d63b793f7b | 45 | pc.printf("Mounting the filesystem... "); |
einsteingustavo | 3:15d63b793f7b | 46 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 47 | |
einsteingustavo | 3:15d63b793f7b | 48 | int err = fileSystem.mount(&blockDevice); |
einsteingustavo | 3:15d63b793f7b | 49 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 50 | if (err) { |
einsteingustavo | 3:15d63b793f7b | 51 | // Reformat if we can't mount the filesystem |
einsteingustavo | 3:15d63b793f7b | 52 | // this should only happen on the first boot |
einsteingustavo | 3:15d63b793f7b | 53 | pc.printf("No filesystem found, formatting... "); |
einsteingustavo | 3:15d63b793f7b | 54 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 55 | err = fileSystem.reformat(&blockDevice); |
einsteingustavo | 3:15d63b793f7b | 56 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 57 | if (err) { |
einsteingustavo | 3:15d63b793f7b | 58 | error("error: %s (%d)\n", strerror(-err), err); |
einsteingustavo | 3:15d63b793f7b | 59 | } |
einsteingustavo | 3:15d63b793f7b | 60 | } |
einsteingustavo | 3:15d63b793f7b | 61 | |
einsteingustavo | 3:15d63b793f7b | 62 | // Open the numbers file |
einsteingustavo | 3:15d63b793f7b | 63 | pc.printf("Opening \"/fs/numbers.txt\"... "); |
einsteingustavo | 3:15d63b793f7b | 64 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 65 | |
einsteingustavo | 3:15d63b793f7b | 66 | FILE* f = fopen("/fs/numbers.txt", "r+"); |
einsteingustavo | 3:15d63b793f7b | 67 | pc.printf("%s\n", (!f ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 68 | if (!f) { |
einsteingustavo | 3:15d63b793f7b | 69 | // Create the numbers file if it doesn't exist |
einsteingustavo | 3:15d63b793f7b | 70 | pc.printf("No file found, creating a new file... "); |
einsteingustavo | 3:15d63b793f7b | 71 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 72 | f = fopen("/fs/numbers.txt", "w+"); |
einsteingustavo | 3:15d63b793f7b | 73 | pc.printf("%s\n", (!f ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 74 | if (!f) { |
einsteingustavo | 3:15d63b793f7b | 75 | error("error: %s (%d)\n", strerror(errno), -errno); |
einsteingustavo | 3:15d63b793f7b | 76 | } |
einsteingustavo | 3:15d63b793f7b | 77 | |
einsteingustavo | 3:15d63b793f7b | 78 | for (int i = 0; i < 10; i++) { |
einsteingustavo | 3:15d63b793f7b | 79 | pc.printf("\rWriting numbers (%d/%d)... ", i, 10); |
einsteingustavo | 3:15d63b793f7b | 80 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 81 | err = fprintf(f, " %d\n", i); |
einsteingustavo | 3:15d63b793f7b | 82 | if (err < 0) { |
einsteingustavo | 3:15d63b793f7b | 83 | pc.printf("Fail :(\n"); |
einsteingustavo | 3:15d63b793f7b | 84 | error("error: %s (%d)\n", strerror(errno), -errno); |
einsteingustavo | 3:15d63b793f7b | 85 | } |
einsteingustavo | 3:15d63b793f7b | 86 | } |
einsteingustavo | 3:15d63b793f7b | 87 | |
einsteingustavo | 3:15d63b793f7b | 88 | pc.printf("\rWriting numbers (%d/%d)... OK\n", 10, 10); |
einsteingustavo | 3:15d63b793f7b | 89 | |
einsteingustavo | 3:15d63b793f7b | 90 | pc.printf("Seeking file... "); |
einsteingustavo | 3:15d63b793f7b | 91 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 92 | err = fseek(f, 0, SEEK_SET); |
einsteingustavo | 3:15d63b793f7b | 93 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 94 | if (err < 0) { |
einsteingustavo | 3:15d63b793f7b | 95 | error("error: %s (%d)\n", strerror(errno), -errno); |
einsteingustavo | 3:15d63b793f7b | 96 | } |
einsteingustavo | 3:15d63b793f7b | 97 | } |
einsteingustavo | 3:15d63b793f7b | 98 | |
einsteingustavo | 3:15d63b793f7b | 99 | // Go through and increment the numbers |
einsteingustavo | 3:15d63b793f7b | 100 | for (int i = 0; i < 10; i++) { |
einsteingustavo | 3:15d63b793f7b | 101 | pc.printf("\rIncrementing numbers (%d/%d)... ", i, 10); |
einsteingustavo | 3:15d63b793f7b | 102 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 103 | |
einsteingustavo | 3:15d63b793f7b | 104 | // Get current stream position |
einsteingustavo | 3:15d63b793f7b | 105 | long pos = ftell(f); |
einsteingustavo | 3:15d63b793f7b | 106 | |
einsteingustavo | 3:15d63b793f7b | 107 | // Parse out the number and increment |
einsteingustavo | 3:15d63b793f7b | 108 | int32_t number; |
einsteingustavo | 3:15d63b793f7b | 109 | fscanf(f, "%ld", &number); |
einsteingustavo | 3:15d63b793f7b | 110 | number += 1; |
einsteingustavo | 3:15d63b793f7b | 111 | |
einsteingustavo | 3:15d63b793f7b | 112 | // Seek to beginning of number |
einsteingustavo | 3:15d63b793f7b | 113 | fseek(f, pos, SEEK_SET); |
einsteingustavo | 3:15d63b793f7b | 114 | |
einsteingustavo | 3:15d63b793f7b | 115 | // Store number |
einsteingustavo | 3:15d63b793f7b | 116 | fprintf(f, " %ld\n", number); |
einsteingustavo | 3:15d63b793f7b | 117 | |
einsteingustavo | 3:15d63b793f7b | 118 | // Flush between write and read on same file |
einsteingustavo | 3:15d63b793f7b | 119 | fflush(f); |
einsteingustavo | 3:15d63b793f7b | 120 | } |
einsteingustavo | 3:15d63b793f7b | 121 | |
einsteingustavo | 3:15d63b793f7b | 122 | pc.printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10); |
einsteingustavo | 3:15d63b793f7b | 123 | |
einsteingustavo | 3:15d63b793f7b | 124 | // Close the file which also flushes any cached writes |
einsteingustavo | 3:15d63b793f7b | 125 | pc.printf("Closing \"/fs/numbers.txt\"... "); |
einsteingustavo | 3:15d63b793f7b | 126 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 127 | err = fclose(f); |
einsteingustavo | 3:15d63b793f7b | 128 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 129 | if (err < 0) { |
einsteingustavo | 3:15d63b793f7b | 130 | error("error: %s (%d)\n", strerror(errno), -errno); |
einsteingustavo | 3:15d63b793f7b | 131 | } |
einsteingustavo | 3:15d63b793f7b | 132 | |
einsteingustavo | 3:15d63b793f7b | 133 | // Display the root directory |
einsteingustavo | 3:15d63b793f7b | 134 | pc.printf("Opening the root directory... "); |
einsteingustavo | 3:15d63b793f7b | 135 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 136 | |
einsteingustavo | 3:15d63b793f7b | 137 | DIR* d = opendir("/fs/"); |
einsteingustavo | 3:15d63b793f7b | 138 | pc.printf("%s\n", (!d ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 139 | if (!d) { |
einsteingustavo | 3:15d63b793f7b | 140 | error("error: %s (%d)\n", strerror(errno), -errno); |
einsteingustavo | 3:15d63b793f7b | 141 | } |
einsteingustavo | 3:15d63b793f7b | 142 | |
einsteingustavo | 3:15d63b793f7b | 143 | pc.printf("root directory:\n"); |
einsteingustavo | 3:15d63b793f7b | 144 | while (true) { |
einsteingustavo | 3:15d63b793f7b | 145 | struct dirent* e = readdir(d); |
einsteingustavo | 3:15d63b793f7b | 146 | if (!e) { |
einsteingustavo | 3:15d63b793f7b | 147 | break; |
einsteingustavo | 3:15d63b793f7b | 148 | } |
einsteingustavo | 3:15d63b793f7b | 149 | |
einsteingustavo | 3:15d63b793f7b | 150 | pc.printf(" %s\n", e->d_name); |
einsteingustavo | 3:15d63b793f7b | 151 | } |
einsteingustavo | 3:15d63b793f7b | 152 | |
einsteingustavo | 3:15d63b793f7b | 153 | pc.printf("Closing the root directory... "); |
einsteingustavo | 3:15d63b793f7b | 154 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 155 | err = closedir(d); |
einsteingustavo | 3:15d63b793f7b | 156 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 157 | if (err < 0) { |
einsteingustavo | 3:15d63b793f7b | 158 | error("error: %s (%d)\n", strerror(errno), -errno); |
einsteingustavo | 3:15d63b793f7b | 159 | } |
einsteingustavo | 3:15d63b793f7b | 160 | |
einsteingustavo | 3:15d63b793f7b | 161 | // Display the numbers file |
einsteingustavo | 3:15d63b793f7b | 162 | pc.printf("Opening \"/fs/numbers.txt\"... "); |
einsteingustavo | 3:15d63b793f7b | 163 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 164 | f = fopen("/fs/numbers.txt", "r"); |
einsteingustavo | 3:15d63b793f7b | 165 | pc.printf("%s\n", (!f ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 166 | if (!f) { |
einsteingustavo | 3:15d63b793f7b | 167 | error("error: %s (%d)\n", strerror(errno), -errno); |
einsteingustavo | 3:15d63b793f7b | 168 | } |
einsteingustavo | 3:15d63b793f7b | 169 | |
einsteingustavo | 3:15d63b793f7b | 170 | pc.printf("numbers:\n"); |
einsteingustavo | 3:15d63b793f7b | 171 | while (!feof(f)) { |
einsteingustavo | 3:15d63b793f7b | 172 | int c = fgetc(f); |
einsteingustavo | 3:15d63b793f7b | 173 | pc.printf("%c", c); |
einsteingustavo | 3:15d63b793f7b | 174 | } |
einsteingustavo | 3:15d63b793f7b | 175 | |
einsteingustavo | 3:15d63b793f7b | 176 | pc.printf("\rClosing \"/fs/numbers.txt\"... "); |
einsteingustavo | 3:15d63b793f7b | 177 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 178 | err = fclose(f); |
einsteingustavo | 3:15d63b793f7b | 179 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 180 | if (err < 0) { |
einsteingustavo | 3:15d63b793f7b | 181 | error("error: %s (%d)\n", strerror(errno), -errno); |
einsteingustavo | 3:15d63b793f7b | 182 | } |
einsteingustavo | 3:15d63b793f7b | 183 | |
einsteingustavo | 3:15d63b793f7b | 184 | // Tidy up |
einsteingustavo | 3:15d63b793f7b | 185 | pc.printf("Unmounting... "); |
einsteingustavo | 3:15d63b793f7b | 186 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 187 | err = fileSystem.unmount(); |
einsteingustavo | 3:15d63b793f7b | 188 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 189 | if (err < 0) { |
einsteingustavo | 3:15d63b793f7b | 190 | error("error: %s (%d)\n", strerror(-err), err); |
einsteingustavo | 3:15d63b793f7b | 191 | } |
einsteingustavo | 3:15d63b793f7b | 192 | |
einsteingustavo | 3:15d63b793f7b | 193 | pc.printf("Initializing the block device... "); |
einsteingustavo | 3:15d63b793f7b | 194 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 195 | |
einsteingustavo | 3:15d63b793f7b | 196 | err = blockDevice.init(); |
einsteingustavo | 3:15d63b793f7b | 197 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 198 | if (err) { |
einsteingustavo | 3:15d63b793f7b | 199 | error("error: %s (%d)\n", strerror(-err), err); |
einsteingustavo | 3:15d63b793f7b | 200 | } |
einsteingustavo | 3:15d63b793f7b | 201 | |
einsteingustavo | 3:15d63b793f7b | 202 | pc.printf("Erasing the block device... "); |
einsteingustavo | 3:15d63b793f7b | 203 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 204 | err = blockDevice.erase(0, blockDevice.size()); |
einsteingustavo | 3:15d63b793f7b | 205 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 206 | if (err) { |
einsteingustavo | 3:15d63b793f7b | 207 | error("error: %s (%d)\n", strerror(-err), err); |
einsteingustavo | 3:15d63b793f7b | 208 | } |
einsteingustavo | 3:15d63b793f7b | 209 | |
einsteingustavo | 3:15d63b793f7b | 210 | pc.printf("Deinitializing the block device... "); |
einsteingustavo | 3:15d63b793f7b | 211 | fflush(stdout); |
einsteingustavo | 3:15d63b793f7b | 212 | err = blockDevice.deinit(); |
einsteingustavo | 3:15d63b793f7b | 213 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
einsteingustavo | 3:15d63b793f7b | 214 | if (err) { |
einsteingustavo | 3:15d63b793f7b | 215 | error("error: %s (%d)\n", strerror(-err), err); |
einsteingustavo | 3:15d63b793f7b | 216 | } |
einsteingustavo | 3:15d63b793f7b | 217 | |
einsteingustavo | 3:15d63b793f7b | 218 | pc.printf("\r\n"); |
einsteingustavo | 3:15d63b793f7b | 219 | |
einsteingustavo | 3:15d63b793f7b | 220 | pc.printf("Mbed OS filesystem example done!\n"); |
einsteingustavo | 3:15d63b793f7b | 221 | } |