add support for external flash system by using callbacks

This commit is contained in:
Marco Scholl 2023-08-11 15:32:37 +02:00
parent 15bf1c0a9d
commit 3202b33fcb
No known key found for this signature in database
2 changed files with 92 additions and 9 deletions

View File

@ -140,6 +140,10 @@ uint8_t* Platform::getNonVolatileMemoryStart()
{ {
if(_memoryType == Flash) if(_memoryType == Flash)
return userFlashStart(); return userFlashStart();
#ifdef KNX_FLASH_CALLBACK
else if(_memoryType == Callback)
return _callbackFlashRead();
#endif
else else
return getEepromBuffer(KNX_FLASH_SIZE); return getEepromBuffer(KNX_FLASH_SIZE);
} }
@ -148,6 +152,10 @@ size_t Platform::getNonVolatileMemorySize()
{ {
if(_memoryType == Flash) if(_memoryType == Flash)
return userFlashSizeEraseBlocks() * flashEraseBlockSize() * flashPageSize(); return userFlashSizeEraseBlocks() * flashEraseBlockSize() * flashPageSize();
#ifdef KNX_FLASH_CALLBACK
else if(_memoryType == Callback)
return _callbackFlashSize();
#endif
else else
return KNX_FLASH_SIZE; return KNX_FLASH_SIZE;
} }
@ -165,6 +173,10 @@ void Platform::commitNonVolatileMemory()
_bufferedEraseblockNumber = -1; // does that make sense? _bufferedEraseblockNumber = -1; // does that make sense?
} }
} }
#ifdef KNX_FLASH_CALLBACK
else if(_memoryType == Callback)
return _callbackFlashCommit();
#endif
else else
{ {
commitToEeprom(); commitToEeprom();
@ -194,6 +206,10 @@ uint32_t Platform::writeNonVolatileMemory(uint32_t relativeAddress, uint8_t* buf
} }
return relativeAddress; return relativeAddress;
} }
#ifdef KNX_FLASH_CALLBACK
else if(_memoryType == Callback)
return _callbackFlashWrite(relativeAddress, buffer, size);
#endif
else else
{ {
memcpy(getEepromBuffer(KNX_FLASH_SIZE)+relativeAddress, buffer, size); memcpy(getEepromBuffer(KNX_FLASH_SIZE)+relativeAddress, buffer, size);
@ -283,3 +299,38 @@ void Platform::bufferEraseBlock(int32_t eraseBlockNumber)
_bufferedEraseblockNumber = eraseBlockNumber; _bufferedEraseblockNumber = eraseBlockNumber;
_bufferedEraseblockDirty = false; _bufferedEraseblockDirty = false;
} }
#ifdef KNX_FLASH_CALLBACK
void Platform::registerFlashCallbacks(
FlashCallbackSize callbackFlashSize,
FlashCallbackRead callbackFlashRead,
FlashCallbackWrite callbackFlashWrite,
FlashCallbackCommit callbackFlashCommit)
{
println("Set Callback");
_memoryType = Callback;
_callbackFlashSize = callbackFlashSize;
_callbackFlashRead = callbackFlashRead;
_callbackFlashWrite = callbackFlashWrite;
_callbackFlashCommit = callbackFlashCommit;
_callbackFlashSize();
}
FlashCallbackSize Platform::callbackFlashSize()
{
return _callbackFlashSize;
}
FlashCallbackRead Platform::callbackFlashRead()
{
return _callbackFlashRead;
}
FlashCallbackWrite Platform::callbackFlashWrite()
{
return _callbackFlashWrite;
}
FlashCallbackCommit Platform::callbackFlashCommit()
{
return _callbackFlashCommit;
}
#endif

View File

@ -4,15 +4,28 @@
#include <stddef.h> #include <stddef.h>
#include "save_restore.h" #include "save_restore.h"
#ifndef KNX_FLASH_CALLBACK
#ifndef KNX_FLASH_SIZE #ifndef KNX_FLASH_SIZE
#define KNX_FLASH_SIZE 1024 #define KNX_FLASH_SIZE 1024
#pragma warning "KNX_FLASH_SIZE not defined, using 1024" #pragma warning "KNX_FLASH_SIZE not defined, using 1024"
#endif #endif
#endif
#ifdef KNX_FLASH_CALLBACK
#ifndef KNX_FLASH_SIZE
#define KNX_FLASH_SIZE 0
#endif
typedef uint32_t (*FlashCallbackSize)();
typedef uint8_t* (*FlashCallbackRead)();
typedef uint32_t (*FlashCallbackWrite)(uint32_t relativeAddress, uint8_t* buffer, size_t len);
typedef void (*FlashCallbackCommit)();
#endif
enum NvMemoryType enum NvMemoryType
{ {
Eeprom, Eeprom,
Flash Flash,
Callback
}; };
class Platform class Platform
@ -82,6 +95,19 @@ class Platform
// size of one flash page in bytes // size of one flash page in bytes
virtual size_t flashPageSize(); virtual size_t flashPageSize();
#ifdef KNX_FLASH_CALLBACK
void registerFlashCallbacks(
FlashCallbackSize callbackFlashSize,
FlashCallbackRead callbackFlashRead,
FlashCallbackWrite callbackFlashWrite,
FlashCallbackCommit callbackFlashCommit);
FlashCallbackSize callbackFlashSize();
FlashCallbackRead callbackFlashRead();
FlashCallbackWrite callbackFlashWrite();
FlashCallbackCommit callbackFlashCommit();
#endif
protected: protected:
// size of one EraseBlock in pages // size of one EraseBlock in pages
virtual size_t flashEraseBlockSize(); virtual size_t flashEraseBlockSize();
@ -97,7 +123,6 @@ class Platform
// ------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------
NvMemoryType _memoryType = Eeprom; NvMemoryType _memoryType = Eeprom;
void loadEraseblockContaining(uint32_t relativeAddress); void loadEraseblockContaining(uint32_t relativeAddress);
@ -113,4 +138,11 @@ class Platform
uint8_t* _eraseblockBuffer = nullptr; uint8_t* _eraseblockBuffer = nullptr;
int32_t _bufferedEraseblockNumber = -1; int32_t _bufferedEraseblockNumber = -1;
bool _bufferedEraseblockDirty = false; bool _bufferedEraseblockDirty = false;
#ifdef KNX_FLASH_CALLBACK
FlashCallbackSize _callbackFlashSize = nullptr;
FlashCallbackRead _callbackFlashRead = nullptr;
FlashCallbackWrite _callbackFlashWrite = nullptr;
FlashCallbackCommit _callbackFlashCommit = nullptr;
#endif
}; };