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
@ -40,7 +53,7 @@ class Platform
//unicast socket //unicast socket
virtual bool sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len); virtual bool sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len);
//UART //UART
virtual void setupUart(); virtual void setupUart();
virtual void closeUart(); virtual void closeUart();
@ -75,12 +88,25 @@ class Platform
NvMemoryType NonVolatileMemoryType(); NvMemoryType NonVolatileMemoryType();
void NonVolatileMemoryType(NvMemoryType type); void NonVolatileMemoryType(NvMemoryType type);
// --- Overwrite these methods in the device-plattform to use flash memory handling by the knx stack --- // --- Overwrite these methods in the device-plattform to use flash memory handling by the knx stack ---
// --- also set _memoryType = Flash in the device-plattform's contructor // --- also set _memoryType = Flash in the device-plattform's contructor
// --- optional: overwrite writeBufferedEraseBlock() in the device-plattform to reduce overhead when flashing multiple pages // --- optional: overwrite writeBufferedEraseBlock() in the device-plattform to reduce overhead when flashing multiple pages
// 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
@ -92,12 +118,11 @@ class Platform
//relativ to userFlashStart //relativ to userFlashStart
virtual void flashErase(uint16_t eraseBlockNum); virtual void flashErase(uint16_t eraseBlockNum);
//write a single page to flash (pageNumber relative to userFashStart //write a single page to flash (pageNumber relative to userFashStart
virtual void flashWritePage(uint16_t pageNumber, uint8_t* data); virtual void flashWritePage(uint16_t pageNumber, uint8_t* data);
// ------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------
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
};