AccellerationFileNV Assignment

Dependencies:   BSP_B-L475E-IOT01

Committer:
framtk
Date:
Thu Dec 06 20:47:20 2018 +0000
Revision:
28:6afd8979a690
Parent:
27:0cf170538518
done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:8e251d9511b8 1 #include "mbed.h"
mbed_official 0:8e251d9511b8 2 #include <stdio.h>
mbed_official 0:8e251d9511b8 3 #include <errno.h>
mbed_official 0:8e251d9511b8 4
mbed_official 0:8e251d9511b8 5 // Block devices
mbed_official 25:65a9183a2178 6 #if COMPONENT_SPIF
mbed_official 0:8e251d9511b8 7 #include "SPIFBlockDevice.h"
mbed_official 25:65a9183a2178 8 #endif
mbed_official 25:65a9183a2178 9
mbed_official 25:65a9183a2178 10 #if COMPONENT_DATAFLASH
mbed_official 1:2bfc377bcc2a 11 #include "DataFlashBlockDevice.h"
mbed_official 25:65a9183a2178 12 #endif
mbed_official 25:65a9183a2178 13
mbed_official 25:65a9183a2178 14 #if COMPONENT_SD
mbed_official 0:8e251d9511b8 15 #include "SDBlockDevice.h"
mbed_official 25:65a9183a2178 16 #endif
mbed_official 25:65a9183a2178 17
mbed_official 0:8e251d9511b8 18 #include "HeapBlockDevice.h"
mbed_official 0:8e251d9511b8 19
mbed_official 0:8e251d9511b8 20 // File systems
mbed_official 0:8e251d9511b8 21 #include "LittleFileSystem.h"
mbed_official 0:8e251d9511b8 22 #include "FATFileSystem.h"
framtk 27:0cf170538518 23 #include "stm32l475e_iot01_accelero.h"
framtk 28:6afd8979a690 24 #include "nvstore.h"
mbed_official 0:8e251d9511b8 25
mbed_official 0:8e251d9511b8 26 // Physical block device, can be any device that supports the BlockDevice API
lmottola 26:130bb1173866 27 /*SPIFBlockDevice bd(
mbed_official 0:8e251d9511b8 28 MBED_CONF_SPIF_DRIVER_SPI_MOSI,
mbed_official 0:8e251d9511b8 29 MBED_CONF_SPIF_DRIVER_SPI_MISO,
mbed_official 0:8e251d9511b8 30 MBED_CONF_SPIF_DRIVER_SPI_CLK,
lmottola 26:130bb1173866 31 MBED_CONF_SPIF_DRIVER_SPI_CS);*/
lmottola 26:130bb1173866 32
framtk 27:0cf170538518 33 DigitalOut led_1(LED1);
framtk 27:0cf170538518 34 DigitalOut led_2(LED2);
framtk 27:0cf170538518 35 DigitalOut led_3(LED3);
framtk 27:0cf170538518 36 Ticker check_led_status;
framtk 27:0cf170538518 37
framtk 27:0cf170538518 38 EventQueue queue(32 * EVENTS_EVENT_SIZE);
framtk 27:0cf170538518 39 Thread t;
framtk 27:0cf170538518 40
lmottola 26:130bb1173866 41 #define BLOCK_SIZE 512
framtk 27:0cf170538518 42 HeapBlockDevice bd(32768, BLOCK_SIZE);
framtk 27:0cf170538518 43
framtk 27:0cf170538518 44 #define LED_1_VALUE 1
framtk 27:0cf170538518 45 #define LED_2_VALUE 2
framtk 27:0cf170538518 46 #define LED_3_VALUE 3
mbed_official 0:8e251d9511b8 47
mbed_official 0:8e251d9511b8 48 // File system declaration
mbed_official 0:8e251d9511b8 49 LittleFileSystem fs("fs");
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
framtk 27:0cf170538518 79 static FILE *f;
framtk 28:6afd8979a690 80
framtk 28:6afd8979a690 81 static uint16_t key_led1 = 1;
framtk 28:6afd8979a690 82 static uint16_t key_led2 = 2;
framtk 28:6afd8979a690 83 static uint16_t key_led3 = 3;
framtk 28:6afd8979a690 84
framtk 28:6afd8979a690 85 static uint32_t value_led1;
framtk 28:6afd8979a690 86 static uint32_t value_led2;
framtk 28:6afd8979a690 87 static uint32_t value_led3;
framtk 28:6afd8979a690 88
framtk 27:0cf170538518 89 volatile int counter = 0;
framtk 27:0cf170538518 90
framtk 27:0cf170538518 91 void read_acc() {
framtk 27:0cf170538518 92 int16_t pDataXYZ[3] = {0};
framtk 27:0cf170538518 93 BSP_ACCELERO_AccGetXYZ(pDataXYZ);
framtk 27:0cf170538518 94 if (abs(pDataXYZ[2]) > 900) { // board laying down
framtk 27:0cf170538518 95 fprintf(f, "%d\n", LED_1_VALUE);
framtk 27:0cf170538518 96 } else if (abs(pDataXYZ[1]) > 900) { // long side
framtk 27:0cf170538518 97 fprintf(f, "%d\n", LED_2_VALUE);
framtk 27:0cf170538518 98 } else if (abs(pDataXYZ[0]) > 900) { // short side
framtk 27:0cf170538518 99 fprintf(f, "%d\n", LED_3_VALUE);
framtk 27:0cf170538518 100 } else { // all other positions
framtk 27:0cf170538518 101 fprintf(f, "%d\n", -1);
framtk 27:0cf170538518 102 }
framtk 27:0cf170538518 103
framtk 27:0cf170538518 104 fflush(f);
framtk 27:0cf170538518 105 fflush(stdout);
framtk 27:0cf170538518 106 }
framtk 27:0cf170538518 107
framtk 27:0cf170538518 108 void read_data_from_file() {
framtk 27:0cf170538518 109 int led1_counter = 0;
framtk 27:0cf170538518 110 int led2_counter = 0;
framtk 27:0cf170538518 111 int led3_counter = 0;
framtk 27:0cf170538518 112 fflush(stdout);
framtk 27:0cf170538518 113 fflush(f);
framtk 27:0cf170538518 114
framtk 27:0cf170538518 115 // read file and count occurrences of position numbers
framtk 27:0cf170538518 116 fseek(f, 0, SEEK_SET);
framtk 27:0cf170538518 117 int number;
framtk 27:0cf170538518 118 while (!feof(f)) {
framtk 27:0cf170538518 119 fscanf(f, "%d", &number);
framtk 27:0cf170538518 120
framtk 27:0cf170538518 121 switch (number){
framtk 27:0cf170538518 122 case LED_1_VALUE:
framtk 27:0cf170538518 123 led1_counter += 1;
framtk 27:0cf170538518 124 break;
framtk 27:0cf170538518 125 case LED_2_VALUE:
framtk 27:0cf170538518 126 led2_counter += 1;
framtk 27:0cf170538518 127 break;
framtk 27:0cf170538518 128 case LED_3_VALUE:
framtk 27:0cf170538518 129 led3_counter += 1;
framtk 27:0cf170538518 130 break;
framtk 27:0cf170538518 131 default:
framtk 27:0cf170538518 132 break;
framtk 27:0cf170538518 133 }
framtk 27:0cf170538518 134 }
framtk 27:0cf170538518 135
framtk 28:6afd8979a690 136 NVStore &nvstore = NVStore::get_instance();
framtk 27:0cf170538518 137 // check which position is the one present the most
framtk 27:0cf170538518 138 if (led1_counter > led2_counter && led1_counter > led3_counter) {
framtk 27:0cf170538518 139 led_1 = 1;
framtk 27:0cf170538518 140 led_2 = 0;
framtk 27:0cf170538518 141 led_3 = 0;
framtk 28:6afd8979a690 142 value_led1 += 1;
framtk 28:6afd8979a690 143 nvstore.set(key_led1, sizeof(value_led1), &value_led1);
framtk 27:0cf170538518 144 } else if (led2_counter > led1_counter && led2_counter > led3_counter ) {
framtk 27:0cf170538518 145 led_1 = 0;
framtk 27:0cf170538518 146 led_2 = 1;
framtk 27:0cf170538518 147 led_3 = 0;
framtk 28:6afd8979a690 148 value_led2 += 1;
framtk 28:6afd8979a690 149 nvstore.set(key_led2, sizeof(value_led2), &value_led2);
framtk 27:0cf170538518 150 } else if (led3_counter > led1_counter && led3_counter > led2_counter) {
framtk 27:0cf170538518 151 led_1 = 0;
framtk 27:0cf170538518 152 led_2 = 0;
framtk 27:0cf170538518 153 led_3 = 1;
framtk 28:6afd8979a690 154 value_led3 += 1;
framtk 28:6afd8979a690 155 nvstore.set(key_led3, sizeof(value_led3), &value_led3);
framtk 27:0cf170538518 156 }
framtk 27:0cf170538518 157
framtk 27:0cf170538518 158 fflush(stdout);
framtk 27:0cf170538518 159 int err = fclose(f);
framtk 27:0cf170538518 160 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
framtk 27:0cf170538518 161 if (err < 0) {
framtk 27:0cf170538518 162 error("error: %s (%d)\n", strerror(err), -err);
framtk 27:0cf170538518 163 }
framtk 27:0cf170538518 164 err = fs.unmount();
framtk 27:0cf170538518 165 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
framtk 27:0cf170538518 166 if (err < 0) {
framtk 27:0cf170538518 167 error("error: %s (%d)\n", strerror(-err), err);
framtk 27:0cf170538518 168 }
framtk 27:0cf170538518 169
framtk 27:0cf170538518 170 if (led_1){
framtk 27:0cf170538518 171 printf("Lighting up corresponding LED 1!\n");
framtk 27:0cf170538518 172 } else if (led_2){
framtk 27:0cf170538518 173 printf("Lighting up corresponding LED 2!\n");
framtk 27:0cf170538518 174 } else if (led_3){
framtk 27:0cf170538518 175 printf("Lighting up corresponding LED 3!\n");
framtk 27:0cf170538518 176 } else {
framtk 27:0cf170538518 177 printf("No valid position detected during the sampling time, no LED lighting up!\n");
framtk 27:0cf170538518 178 }
framtk 27:0cf170538518 179
framtk 27:0cf170538518 180 }
framtk 27:0cf170538518 181
framtk 27:0cf170538518 182 void toggle_led() {
framtk 27:0cf170538518 183 queue.call(read_acc);
framtk 27:0cf170538518 184 counter++;
framtk 27:0cf170538518 185 if (counter == 100*10) {
framtk 27:0cf170538518 186 check_led_status.detach();
framtk 27:0cf170538518 187 queue.call(read_data_from_file);
framtk 27:0cf170538518 188 }
framtk 27:0cf170538518 189
framtk 27:0cf170538518 190 }
mbed_official 0:8e251d9511b8 191
mbed_official 0:8e251d9511b8 192 // Entry point for the example
mbed_official 0:8e251d9511b8 193 int main() {
framtk 27:0cf170538518 194 t.start(callback(&queue, &EventQueue::dispatch_forever));
framtk 27:0cf170538518 195 BSP_ACCELERO_Init();
framtk 27:0cf170538518 196
framtk 28:6afd8979a690 197 NVStore &nvstore = NVStore::get_instance();
framtk 28:6afd8979a690 198
framtk 28:6afd8979a690 199 uint16_t actual_len_bytes = 0;
framtk 28:6afd8979a690 200 int rc;
framtk 28:6afd8979a690 201
framtk 28:6afd8979a690 202 rc = nvstore.init();
framtk 28:6afd8979a690 203
framtk 28:6afd8979a690 204 rc = nvstore.get(key_led1, sizeof(value_led1), &value_led1, actual_len_bytes);
framtk 28:6afd8979a690 205 if (rc == NVSTORE_NOT_FOUND) {
framtk 28:6afd8979a690 206 value_led1 = 0;
framtk 28:6afd8979a690 207 value_led2 = 0;
framtk 28:6afd8979a690 208 value_led3 = 0;
framtk 28:6afd8979a690 209 nvstore.set(key_led1, sizeof(value_led1), &value_led1);
framtk 28:6afd8979a690 210 nvstore.set(key_led2, sizeof(value_led2), &value_led2);
framtk 28:6afd8979a690 211 nvstore.set(key_led3, sizeof(value_led3), &value_led3);
framtk 28:6afd8979a690 212 } else {
framtk 28:6afd8979a690 213 nvstore.get(key_led2, sizeof(value_led2), &value_led2, actual_len_bytes);
framtk 28:6afd8979a690 214 nvstore.get(key_led3, sizeof(value_led3), &value_led3, actual_len_bytes);
framtk 28:6afd8979a690 215 }
framtk 28:6afd8979a690 216
framtk 28:6afd8979a690 217 printf("\nLED1 has been on: %d times\nLED2 has been on: %d times\nLED3 has been on: %d times\n", value_led1, value_led2, value_led3);
framtk 27:0cf170538518 218
mbed_official 0:8e251d9511b8 219 printf("--- Mbed OS filesystem example ---\n");
mbed_official 0:8e251d9511b8 220
mbed_official 12:a07d0af60cc6 221 // Setup the erase event on button press, use the event queue
mbed_official 12:a07d0af60cc6 222 // to avoid running in interrupt context
mbed_official 12:a07d0af60cc6 223 irq.fall(mbed_event_queue()->event(erase));
mbed_official 0:8e251d9511b8 224
mbed_official 0:8e251d9511b8 225 // Try to mount the filesystem
mbed_official 0:8e251d9511b8 226 printf("Mounting the filesystem... ");
mbed_official 0:8e251d9511b8 227 fflush(stdout);
mbed_official 0:8e251d9511b8 228 int err = fs.mount(&bd);
mbed_official 0:8e251d9511b8 229 printf("%s\n", (err ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 230 if (err) {
mbed_official 0:8e251d9511b8 231 // Reformat if we can't mount the filesystem
mbed_official 0:8e251d9511b8 232 // this should only happen on the first boot
mbed_official 0:8e251d9511b8 233 printf("No filesystem found, formatting... ");
mbed_official 0:8e251d9511b8 234 fflush(stdout);
mbed_official 0:8e251d9511b8 235 err = fs.reformat(&bd);
mbed_official 0:8e251d9511b8 236 printf("%s\n", (err ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 237 if (err) {
mbed_official 0:8e251d9511b8 238 error("error: %s (%d)\n", strerror(-err), err);
mbed_official 0:8e251d9511b8 239 }
mbed_official 0:8e251d9511b8 240 }
mbed_official 0:8e251d9511b8 241
mbed_official 0:8e251d9511b8 242 // Open the numbers file
mbed_official 0:8e251d9511b8 243 printf("Opening \"/fs/numbers.txt\"... ");
mbed_official 0:8e251d9511b8 244 fflush(stdout);
framtk 27:0cf170538518 245 f = fopen("/fs/numbers.txt", "r +");
mbed_official 0:8e251d9511b8 246 printf("%s\n", (!f ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 247 if (!f) {
mbed_official 0:8e251d9511b8 248 // Create the numbers file if it doesn't exist
mbed_official 0:8e251d9511b8 249 printf("No file found, creating a new file... ");
mbed_official 0:8e251d9511b8 250 fflush(stdout);
mbed_official 0:8e251d9511b8 251 f = fopen("/fs/numbers.txt", "w+");
mbed_official 0:8e251d9511b8 252 printf("%s\n", (!f ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 253 if (!f) {
mbed_official 0:8e251d9511b8 254 error("error: %s (%d)\n", strerror(errno), -errno);
mbed_official 0:8e251d9511b8 255 }
mbed_official 0:8e251d9511b8 256
mbed_official 0:8e251d9511b8 257 printf("Seeking file... ");
mbed_official 0:8e251d9511b8 258 fflush(stdout);
mbed_official 0:8e251d9511b8 259 err = fseek(f, 0, SEEK_SET);
mbed_official 0:8e251d9511b8 260 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 261 if (err < 0) {
mbed_official 0:8e251d9511b8 262 error("error: %s (%d)\n", strerror(errno), -errno);
mbed_official 0:8e251d9511b8 263 }
mbed_official 0:8e251d9511b8 264 }
mbed_official 0:8e251d9511b8 265
framtk 27:0cf170538518 266 check_led_status.attach(&toggle_led, 0.01);
framtk 27:0cf170538518 267 }