NVProperty generic key value store using the MCU flash area.

Dependents:   Turtle_RadioShuttle

Files at this revision

API Documentation at this revision

Comitter:
Helmut Tschemernjak
Date:
Sun Feb 10 18:39:29 2019 +0100
Parent:
11:d5dc788ec805
Child:
13:2436a800c0fa
Commit message:
Updated

Changed in this revision

NVProperty.h Show annotated file Show diff for this revision Revisions of this file
NVProperty_Editor.cpp Show annotated file Show diff for this revision Revisions of this file
NVProperty_L4OTP.cpp Show annotated file Show diff for this revision Revisions of this file
NVProperty_L4OTP.h Show annotated file Show diff for this revision Revisions of this file
NVProperty_MBEDFlash.cpp Show annotated file Show diff for this revision Revisions of this file
NVProperty_MBEDFlash.h Show annotated file Show diff for this revision Revisions of this file
--- a/NVProperty.h	Sun Feb 10 10:43:09 2019 +0100
+++ b/NVProperty.h	Sun Feb 10 18:39:29 2019 +0100
@@ -28,7 +28,11 @@
      * flash blocks are less busy. For the D21 it is a good idea to
 	 * use increments of 16kB because this is the region locking area size
  	 */
+#ifndef TARGET_STM32L0
     NVProperty(int propSizekB = 16, bool erase = false);
+#else
+    NVProperty(int propSizekB = 4, bool erase = false);
+#endif
     ~NVProperty();
 public:
     enum NVPType {
@@ -160,8 +164,7 @@
 		
 		PRIVATE_RANGE_START = 128,
 		PRIVATE_RANGE_END 	= 254,
-		PROPERTIES_EOF		= 255,
-		MAX_PROPERTIES		= 256,
+		MAX_PROPERTIES		= 254, // 1-254
     };
 
 private:
--- a/NVProperty_Editor.cpp	Sun Feb 10 10:43:09 2019 +0100
+++ b/NVProperty_Editor.cpp	Sun Feb 10 18:39:29 2019 +0100
@@ -11,6 +11,7 @@
 
 
 #include <mbed.h>
+#include "PinMap.h"
 #include "main.h"
 #include "arch.h"
 
--- a/NVProperty_L4OTP.cpp	Sun Feb 10 10:43:09 2019 +0100
+++ b/NVProperty_L4OTP.cpp	Sun Feb 10 18:39:29 2019 +0100
@@ -55,7 +55,7 @@
 	static uint8_t *savedStart;
 	if (!savedStart) {
  		_startAddress = (uint8_t *) malloc(512);
-		memset(_startAddress, 0xff, 512);
+		memset(_startAddress, _flashErasedValue, 512);
 		savedStart = _startAddress;
 	} else {
 		_startAddress = savedStart;
@@ -91,7 +91,7 @@
 	
 	uint8_t *p = _startAddress;
 	for (int i = 0; i < (int)sizeof(_flash_header); i++) {
-		if (*p++ != 0xff)
+		if (*p++ != _flashErasedValue)
 			return; // invalid data
 	}
 	
@@ -395,7 +395,7 @@
 
 	int index = 0;
 	_flashEntry *p = (_flashEntry *)(_startAddress + sizeof(_flash_header));
-	while((uint8_t *)p < _endAddress && p->key != NVProperty::PROPERTIES_EOF) {
+	while((uint8_t *)p < _endAddress && p->key != _flashErasedValue) {
 
 		int64_t value = 0;
     	switch(p->t.type) {
@@ -466,7 +466,7 @@
 		p = (_flashEntry *)(_startAddress + sizeof(_flash_header));
 	_flashEntry *lastP = NULL;
 	while(true) {
-		if ((uint8_t*)p >= _endAddress || p->key == NVProperty::PROPERTIES_EOF) {
+		if ((uint8_t*)p >= _endAddress || p->key == _flashErasedValue) {
 			if ((uint8_t*)p <= _endAddress)
 				_lastEntry = p;
 			if (!lastP || lastP->t.deleted)
--- a/NVProperty_L4OTP.h	Sun Feb 10 10:43:09 2019 +0100
+++ b/NVProperty_L4OTP.h	Sun Feb 10 18:39:29 2019 +0100
@@ -80,6 +80,7 @@
 	bool _debug;
 	int _propSize;
 
+	static const uint8_t _flashErasedValue = 0xff;
 	static const int FLASH_PROP_MAGIC = 0x4c4f5450; // "LOTP"
 	static const int FLASH_PROP_VERSION = 1;
 };
--- a/NVProperty_MBEDFlash.cpp	Sun Feb 10 10:43:09 2019 +0100
+++ b/NVProperty_MBEDFlash.cpp	Sun Feb 10 18:39:29 2019 +0100
@@ -44,8 +44,6 @@
 	_flashIAP = new FlashIAP();
 	_flashIAP->init();
 	
-	// at present erased flash bits are assumed with 0xff value
-	// MBED_ASSERT(_flashIAP->get_erase_value() == NVProperty::PROPERTIES_EOF);
 	// a min page size > 8 looks strange
 	MBED_ASSERT(_flashIAP->get_page_size() <= sizeof(int64_t));
 	
@@ -57,6 +55,7 @@
 	_rowSize = _flashIAP->get_sector_size(_flashIAP->get_flash_start()); //  pageSize * 4;
 	_startAddress = (uint8_t*)_flashIAP->get_flash_start() + ((_numPages-(_propSizekB * 1024)/_pageSize) * _pageSize);
 	_endAddress = _startAddress + (_propSizekB * 1024);
+	_flashErasedValue = _flashIAP->get_erase_value();
 	_lastEntry = NULL;
 
 	if (_debug) {
@@ -125,8 +124,9 @@
 		uint32_t *startAddr = (uint32_t *)((startRow + i) * _rowSize);
 		uint32_t *addr = startAddr;
 		bool foundData = false;
+		uint32_t emtpyValue = _flashErasedValue << 24 | _flashErasedValue << 16 | _flashErasedValue << 8 | _flashErasedValue;
 		for (int offset = 0; offset < _rowSize; offset += sizeof(uint32_t)) {
-			if (*addr++ != 0xffffffff) {
+			if (*addr++ != emtpyValue) {
 				foundData = true;
 				break;
 			}
@@ -204,7 +204,7 @@
 NVProperty_MBEDFlash::_FlashIsCleared(uint8_t *address, int len)
 {
 	while (len-- > 0) {
-		if (*address++ != NVProperty::PROPERTIES_EOF) {
+		if (*address++ != _flashErasedValue) {
 			return false;
 		}
 	}
@@ -520,7 +520,7 @@
 
 	int index = 0;
 	_flashEntry *p = (_flashEntry *)(_startAddress + sizeof(_flash_header));
-	while((uint8_t *)p < _endAddress && p->key != NVProperty::PROPERTIES_EOF) {
+	while((uint8_t *)p < _endAddress && p->key != _flashErasedValue) {
 
 		int64_t value = 0;
     	switch(p->t.type) {
@@ -591,7 +591,7 @@
 		p = (_flashEntry *)(_startAddress + sizeof(_flash_header));
 	_flashEntry *lastP = NULL;
 	while(true) {
-		if ((uint8_t*)p >= _endAddress || p->key == NVProperty::PROPERTIES_EOF) {
+		if ((uint8_t*)p >= _endAddress || p->key == _flashErasedValue) {
 			if ((uint8_t*)p <= _endAddress)
 				_lastEntry = p;
 			if (!lastP || lastP->t.deleted)
@@ -648,7 +648,7 @@
 	int freeSpace = 0;
 	
 	_flashEntry *p = (_flashEntry *)(_startAddress + sizeof(_flash_header));
-	while((uint8_t *)p < _endAddress && p->key != NVProperty::PROPERTIES_EOF) {
+	while((uint8_t *)p < _endAddress && p->key != _flashErasedValue) {
 		_flashEntry *k = _GetFlashEntry(p->key);
 		if (k == p) { // current entry is the lastest one.
 			totalLen += _GetFlashEntryLen(k);
@@ -685,7 +685,7 @@
 	memcpy(t, _startAddress, sizeof(_flash_header));
 	t += sizeof(_flash_header);
 	
-	while((uint8_t *)p < _endAddress && p->key != NVProperty::PROPERTIES_EOF) {
+	while((uint8_t *)p < _endAddress && p->key != _flashErasedValue) {
 		_flashEntry *k = _GetFlashEntry(p->key, (uint8_t *)p);
 		if (k == p) {	// current entry is the lastest one.
 			if (!p->t.deleted) {
--- a/NVProperty_MBEDFlash.h	Sun Feb 10 10:43:09 2019 +0100
+++ b/NVProperty_MBEDFlash.h	Sun Feb 10 18:39:29 2019 +0100
@@ -82,6 +82,7 @@
 	int _pageSize;
 	int _numPages;
 	int _rowSize;
+	uint8_t _flashErasedValue;
 	FlashIAP *_flashIAP;
 	uint8_t *_startAddress;
 	uint8_t *_endAddress;