8 : _platform(platform), _deviceObject(deviceObject)
21 if (flashStart ==
nullptr)
23 println(
"no user flash available;");
27 printHex(
"RESTORED ", flashStart, _metadataSize);
29 uint16_t metadataBlockSize = alignToPageSize(_metadataSize);
31 _freeList =
new MemoryBlock(flashStart + metadataBlockSize, flashSize - metadataBlockSize);
33 uint16_t apiVersion = 0;
34 const uint8_t* buffer =
popWord(apiVersion, flashStart);
36 uint16_t manufacturerId = 0;
37 buffer =
popWord(manufacturerId, buffer);
39 uint8_t hardwareType[LEN_HARDWARE_TYPE] = {0};
40 buffer =
popByteArray(hardwareType, LEN_HARDWARE_TYPE, buffer);
43 buffer =
popWord(version, buffer);
50 if (_versionCheckCallback != 0)
52 versionCheck = _versionCheckCallback(manufacturerId, hardwareType, version);
56 memcmp(_deviceObject.
hardwareType(), hardwareType, LEN_HARDWARE_TYPE) == 0)
58 if (_deviceObject.
version() == version)
69 println(
"manufacturerId or hardwareType are different");
70 print(
"expexted manufacturerId: ");
72 print(
", stored manufacturerId: ");
74 print(
"expexted hardwareType: ");
76 print(
", stored hardwareType: ");
77 printHex(
"", hardwareType, LEN_HARDWARE_TYPE);
83 println(
"DataObject api changed, any data stored in flash is invalid.");
84 print(
"expexted DataObject api version: ");
86 print(
", stored api version: ");
92 println(
"ETS has to reprogram PA and application!");
96 println(
"restoring data from flash...");
97 print(
"saverestores ");
100 for (
int i = 0; i < _saveCount; i++)
104 buffer = _saveRestores[i]->
restore(buffer);
107 println(
"restored saveRestores");
111 println(
"TableObjects are referring to an older firmware version and are not loaded");
118 for (
int i = 0; i < _tableObjCount; i++)
122 buffer = _tableObjects[i]->
restore(buffer);
123 uint16_t memorySize = 0;
124 buffer =
popWord(memorySize, buffer);
131 addNewUsedBlock(_tableObjects[i]->_data, memorySize);
134 println(
"restored Tableobjects");
140 uint16_t writeBufferSize = _metadataSize;
142 for (
int i = 0; i < _saveCount; i++)
143 writeBufferSize = MAX(writeBufferSize, _saveRestores[i]->saveSize());
145 for (
int i = 0; i < _tableObjCount; i++)
146 writeBufferSize = MAX(writeBufferSize, _tableObjects[i]->saveSize() + 2 );
148 uint8_t buffer[writeBufferSize];
149 uint32_t flashPos = 0;
150 uint8_t* bufferPos = buffer;
159 print(
"save saveRestores ");
162 for (
int i = 0; i < _saveCount; i++)
164 bufferPos = _saveRestores[i]->
save(buffer);
168 print(
"save tableobjs ");
171 for (
int i = 0; i < _tableObjCount; i++)
173 bufferPos = _tableObjects[i]->
save(buffer);
176 if (_tableObjects[i]->_data !=
nullptr)
178 MemoryBlock* block = findBlockInList(_usedList, _tableObjects[i]->_data);
180 if (block ==
nullptr)
182 println(
"_data of TableObject not in _usedList");
211 if (_saveCount >= MAXSAVE - 1)
214 _saveRestores[_saveCount] = obj;
221 if (_tableObjCount >= MAXTABLEOBJ)
224 _tableObjects[_tableObjCount] = obj;
233 size = alignToPageSize(size);
241 if (freeBlock->
size >= size)
243 if (blockToUse !=
nullptr && (blockToUse->
size - size) > (freeBlock->
size - size))
244 blockToUse = freeBlock;
245 else if (blockToUse ==
nullptr)
246 blockToUse = freeBlock;
249 freeBlock = freeBlock->
next;
254 println(
"No available non volatile memory!");
258 if (blockToUse->
size == size)
261 removeFromFreeList(blockToUse);
262 addToUsedList(blockToUse);
269 addToUsedList(newBlock);
272 blockToUse->
size -= size;
297 println(
"freeMemory for not used pointer called");
302 removeFromUsedList(block);
303 addToFreeList(block);
305 if (_saveTimeout == 0)
311 if(_saveTimeout != 0)
314 if (_saveTimeout == 0)
342 head->
next =
nullptr;
348 println(
"invalid parameters of Memory::removeFromList");
358 if (block->
next == item)
370 println(
"tried to remove block from list not in it");
375 item->
next =
nullptr;
379 void Memory::removeFromFreeList(
MemoryBlock* block)
381 _freeList = removeFromList(_freeList, block);
385 void Memory::removeFromUsedList(
MemoryBlock* block)
387 _usedList = removeFromList(_usedList, block);
393 block->
next = _usedList;
400 if (_freeList ==
nullptr)
415 current->
next = block;
421 block->
next = current;
423 if (current == _freeList)
434 current = current->
next;
449 if (block->
next ==
nullptr)
461 uint16_t Memory::alignToPageSize(
size_t size)
465 return (size + pageSize - 1) & (-1 * pageSize);
470 while (head !=
nullptr)
481 void Memory::addNewUsedBlock(uint8_t* address,
size_t size)
486 while (smallerFreeBlock)
488 if (smallerFreeBlock->
next ==
nullptr ||
489 (smallerFreeBlock->
next !=
nullptr && smallerFreeBlock->
next->
address > address))
492 smallerFreeBlock = smallerFreeBlock->
next;
495 if (smallerFreeBlock ==
nullptr)
497 println(
"addNewUsedBlock: no smallerBlock found");
502 if ((smallerFreeBlock->
address + smallerFreeBlock->
size) < (address + size))
504 println(
"addNewUsedBlock: found block can't contain new block");
509 if (smallerFreeBlock->
address == address && smallerFreeBlock->
size == size)
512 removeFromFreeList(smallerFreeBlock);
513 addToUsedList(smallerFreeBlock);
517 if (smallerFreeBlock->
address == address)
520 smallerFreeBlock->
address += size;
521 smallerFreeBlock->
size -= size;
526 uint8_t* oldEndAddr = smallerFreeBlock->
address + smallerFreeBlock->
size;
527 smallerFreeBlock->
size = (address - smallerFreeBlock->
address);
529 if (address + size < oldEndAddr)
533 newFreeBlock->
next = smallerFreeBlock->
next;
534 newFreeBlock->
address = address + size;
535 newFreeBlock->
size = oldEndAddr - newFreeBlock->
address;
536 smallerFreeBlock->
next = newFreeBlock;
541 addToUsedList(newUsedBlock);
546 _versionCheckCallback = func;
551 return _versionCheckCallback;
556 if(_saveTimeout != 0 &&
millis() - _saveTimeout > 5000)
void printHex(const char *suffix, const uint8_t *data, size_t length, bool newline)
uint8_t * pushByteArray(const uint8_t *src, uint32_t size, uint8_t *data)
const uint8_t * popByteArray(uint8_t *dst, uint32_t size, const uint8_t *data)
uint8_t * pushWord(uint16_t w, uint8_t *data)
const uint8_t * popWord(uint16_t &w, const uint8_t *data)
const uint8_t * hardwareType()
uint16_t manufacturerId()
const uint16_t apiVersion
void freeMemory(uint8_t *ptr)
uint8_t * toAbsolute(uint32_t relativeAddress)
uint32_t toRelative(uint8_t *absoluteAddress)
VersionCheckCallback versionCheckCallback()
void addSaveRestore(SaveRestore *obj)
uint8_t * allocMemory(size_t size)
Memory(Platform &platform, DeviceObject &deviceObject)
Interface for classes that can save and restore data from a buffer.
virtual uint8_t * save(uint8_t *buffer)
This method is called when the object should save its state to the buffer.
virtual const uint8_t * restore(const uint8_t *buffer)
This method is called when the object should restore its state from the buffer.
virtual uint16_t saveSize()
This class provides common functionality for interface objects that are configured by ETS with MemorW...
const uint8_t * restore(const uint8_t *buffer) override
This method is called when the object should restore its state from the buffer.
uint16_t saveSize() override
uint8_t * save(uint8_t *buffer) override
This method is called when the object should save its state to the buffer.
@ FlashTablesInvalid
All table objects are invalid for this firmware, device object and saveRestores are OK.
@ FlashAllInvalid
All flash content is not valid for this firmware, we delete it.
@ FlashValid
Flash content is valid and will be used.
VersionCheckResult(* VersionCheckCallback)(uint16_t manufacturerId, uint8_t *hardwareType, uint16_t version)