This is library for storing data array in internal flash memory of MCU LPC1768. The flash data is empty every time we download program to MCU so it can be used in project where we don't have acces to file system like mbed development board uses.

Dependencies:   IAP

Dependents:   Flash_Example

Files at this revision

API Documentation at this revision

Comitter:
bosko1523
Date:
Sun Jan 15 20:37:39 2017 +0000
Commit message:
This is library for storing data array in internal flash memory of MCU LPC1768. The flash data is empty every time we download program to MCU so it can be used in project where we don't have acces to file system like mbed development board uses.

Changed in this revision

Flash.cpp Show annotated file Show diff for this revision Revisions of this file
Flash.h Show annotated file Show diff for this revision Revisions of this file
IAP.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 0414cef3e9d6 Flash.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flash.cpp	Sun Jan 15 20:37:39 2017 +0000
@@ -0,0 +1,32 @@
+#include "Flash.h"
+
+Flash::Flash() {
+
+}
+
+void Flash::writeFlash(char data[MEM_SIZE]) {
+    __disable_irq();
+    
+    // varijabla za spremanje odgovora
+    int r;  
+    // provijera ako je sektor flasha prazan, nakon svakog novog programiranja se flash prazni
+    r = iap.blank_check(TARGET_SECTOR, TARGET_SECTOR);
+    // brisanje sektora ako je potrebno
+    if (r == SECTOR_NOT_BLANK) {
+        iap.prepare(TARGET_SECTOR, TARGET_SECTOR);
+        r = iap.erase(TARGET_SECTOR, TARGET_SECTOR);
+    }
+    // pisanje podataka u memoriju
+    iap.prepare(TARGET_SECTOR, TARGET_SECTOR);
+    iap.write(data, sector_start_adress[TARGET_SECTOR], MEM_SIZE);
+    
+    __enable_irq();
+}
+
+void Flash::readFlash(char *data) {
+    __disable_irq();
+    
+    memcpy(data, sector_start_adress[TARGET_SECTOR], MEM_SIZE);
+    
+    __enable_irq();
+}
\ No newline at end of file
diff -r 000000000000 -r 0414cef3e9d6 Flash.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flash.h	Sun Jan 15 20:37:39 2017 +0000
@@ -0,0 +1,70 @@
+#ifndef _FLASH_H_MB_
+#define _FLASH_H_MB_
+
+#include "mbed.h"
+#include "IAP.h"
+
+// Data for NXP LPC1768 from data table which is explained in datasheet UM10360 page 626.
+#define MEM_SIZE 256
+#define TARGET_SECTOR 29
+
+/** This is simple library for storing data to flash memory and
+ * reading it. This allows us to have retain data without having to have  
+ * external flash chip. This class uses NXP LPC1768 internal flash memory
+ * which give us 256B of flash size.
+ * 
+ * Author: TVZ Mechatronics Team
+ *
+ * Example of use:
+ * @code
+ * #include "mbed.h"
+ * #include "Flash.h"
+ *
+ * BusOut display(LED1, LED2, LED3, LED4);
+ * InterruptIn cntUp(p5);
+ * Timer debounceUp;
+ * Flash flash;
+ *
+ * char retainData[MEM_SIZE];
+ * uint8_t counter;
+ *
+ * void countUp(void) {
+ *    if (debounceUp.read_ms() > 500) {
+ *        if (counter > 15)
+ *            counter = 0;
+ *        else
+ *            counter++;
+ *            
+ *        retainData[0] = counter;
+ *        flash.writeFlash(retainData);
+ *        
+ *        debounceUp.reset();
+ *    }
+ * }
+ * 
+ * int main() {
+ *    cntUp.rise(&countUp);
+ *    debounceUp.start();
+ *
+ *    flash.readFlash(retainData);
+ *    counter = retainData[0];
+ *
+ *    while(1) {
+ *        display = counter;
+ *    }
+ * }
+ * @endcode
+ */
+class Flash {
+    public:
+        /** Constructor */
+        Flash();
+        /** Function recevies data array wich is stored in flash memory */
+        void writeFlash(char data[MEM_SIZE]);
+        /** Function returning data to pointer to array of size 256 wich is stored in flash memory */
+        void readFlash(char *data);
+    private:
+        IAP iap;
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 0414cef3e9d6 IAP.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IAP.lib	Sun Jan 15 20:37:39 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/okano/code/IAP/#c8bf974ecb33