ec521

Dependencies:   mbed

Revision:
0:e2f78ce2a5cf
diff -r 000000000000 -r e2f78ce2a5cf flashwrite.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flashwrite.cpp	Sun Apr 22 19:20:10 2018 +0000
@@ -0,0 +1,28 @@
+#include "mbed.h"
+#include "FreescaleIAP.h"
+ 
+int flashwrite() {    
+    
+    int address = flash_size() - SECTOR_SIZE;           //Write in last sector
+    int *data = (int*)address;
+    
+    //By default flash is initialized at 0xFF, this is signed -1, so now we know
+    //the program runs for the first time. You of course need to make sure your program
+    //never writes -1 to this variable if you use this method
+    
+    //Alternatively you could also do the same, but with a seperate "initial run" variable added,
+    //so your other variables can take any value
+    if (data[0] == -1) {
+        printf("Initial run\r\n");
+        printf("Writing 42 and 42\r\n");
+        erase_sector(address);
+        int newvalues[2] = {42, 42};
+        program_flash(address,(char*) newvalues, 8);     //Two integers of 4 bytes = 8 bytes
+        while(1);
+    }
+    printf("Current = %d and %d, new is %d and %d\r\n", data[0], data[1], data[0]+1, data[1]-1);
+    int newvalues[2] = {data[0]+1, data[1]-1};
+    erase_sector(address);
+    program_flash(address, (char*) newvalues, 8);
+    while(1);
+}