From 3202b33fcb53458973a00aa6a364658ace4a771c Mon Sep 17 00:00:00 2001 From: Marco Scholl Date: Fri, 11 Aug 2023 15:32:37 +0200 Subject: [PATCH] add support for external flash system by using callbacks --- src/knx/platform.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/knx/platform.h | 50 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/src/knx/platform.cpp b/src/knx/platform.cpp index 2b77893..545e3ad 100644 --- a/src/knx/platform.cpp +++ b/src/knx/platform.cpp @@ -140,6 +140,10 @@ uint8_t* Platform::getNonVolatileMemoryStart() { if(_memoryType == Flash) return userFlashStart(); +#ifdef KNX_FLASH_CALLBACK + else if(_memoryType == Callback) + return _callbackFlashRead(); +#endif else return getEepromBuffer(KNX_FLASH_SIZE); } @@ -148,6 +152,10 @@ size_t Platform::getNonVolatileMemorySize() { if(_memoryType == Flash) return userFlashSizeEraseBlocks() * flashEraseBlockSize() * flashPageSize(); +#ifdef KNX_FLASH_CALLBACK + else if(_memoryType == Callback) + return _callbackFlashSize(); +#endif else return KNX_FLASH_SIZE; } @@ -165,6 +173,10 @@ void Platform::commitNonVolatileMemory() _bufferedEraseblockNumber = -1; // does that make sense? } } +#ifdef KNX_FLASH_CALLBACK + else if(_memoryType == Callback) + return _callbackFlashCommit(); +#endif else { commitToEeprom(); @@ -194,6 +206,10 @@ uint32_t Platform::writeNonVolatileMemory(uint32_t relativeAddress, uint8_t* buf } return relativeAddress; } +#ifdef KNX_FLASH_CALLBACK + else if(_memoryType == Callback) + return _callbackFlashWrite(relativeAddress, buffer, size); +#endif else { memcpy(getEepromBuffer(KNX_FLASH_SIZE)+relativeAddress, buffer, size); @@ -283,3 +299,38 @@ void Platform::bufferEraseBlock(int32_t eraseBlockNumber) _bufferedEraseblockNumber = eraseBlockNumber; _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 diff --git a/src/knx/platform.h b/src/knx/platform.h index b8fafff..b9e4c5c 100644 --- a/src/knx/platform.h +++ b/src/knx/platform.h @@ -4,15 +4,28 @@ #include #include "save_restore.h" +#ifndef KNX_FLASH_CALLBACK #ifndef KNX_FLASH_SIZE #define KNX_FLASH_SIZE 1024 #pragma warning "KNX_FLASH_SIZE not defined, using 1024" #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 { Eeprom, - Flash + Flash, + Callback }; class Platform @@ -40,7 +53,7 @@ class Platform //unicast socket virtual bool sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len); - + //UART virtual void setupUart(); virtual void closeUart(); @@ -75,12 +88,25 @@ class Platform NvMemoryType NonVolatileMemoryType(); void NonVolatileMemoryType(NvMemoryType type); - // --- 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 - // --- optional: overwrite writeBufferedEraseBlock() in the device-plattform to reduce overhead when flashing multiple pages + // --- 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 + // --- optional: overwrite writeBufferedEraseBlock() in the device-plattform to reduce overhead when flashing multiple pages // size of one flash page in bytes 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: // size of one EraseBlock in pages @@ -92,12 +118,11 @@ class Platform //relativ to userFlashStart virtual void flashErase(uint16_t eraseBlockNum); //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; void loadEraseblockContaining(uint32_t relativeAddress); @@ -113,4 +138,11 @@ class Platform uint8_t* _eraseblockBuffer = nullptr; int32_t _bufferedEraseblockNumber = -1; bool _bufferedEraseblockDirty = false; -}; \ No newline at end of file + +#ifdef KNX_FLASH_CALLBACK + FlashCallbackSize _callbackFlashSize = nullptr; + FlashCallbackRead _callbackFlashRead = nullptr; + FlashCallbackWrite _callbackFlashWrite = nullptr; + FlashCallbackCommit _callbackFlashCommit = nullptr; +#endif +};