From 5bef967ed368ef392a6cc980ea7a1364eb933325 Mon Sep 17 00:00:00 2001 From: SirSydom Date: Mon, 14 Feb 2022 21:47:29 +0100 Subject: [PATCH] added support for both Eeprom and Flash (NvMemoryType) plattforms. --- src/knx/platform.cpp | 73 +++++++++++++++++++++++---------- src/knx/platform.h | 6 ++- src/rp2040_arduino_platform.cpp | 20 +++++++-- src/rp2040_arduino_platform.h | 6 ++- 4 files changed, 79 insertions(+), 26 deletions(-) diff --git a/src/knx/platform.cpp b/src/knx/platform.cpp index 2674c3c..e474151 100644 --- a/src/knx/platform.cpp +++ b/src/knx/platform.cpp @@ -107,7 +107,8 @@ size_t Platform::flashEraseBlockSize() size_t Platform::flashPageSize() { - return 0; + // align to 32bit as default for Eeprom Emulation plattforms + return 4; } uint8_t *Platform::userFlashStart() @@ -126,46 +127,76 @@ void Platform::flashErase(uint16_t eraseBlockNum) void Platform::flashWritePage(uint16_t pageNumber, uint8_t* data) {} +uint8_t * Platform::getEepromBuffer(uint16_t size) +{ + return nullptr; +} + +void Platform::commitToEeprom() +{} + uint8_t* Platform::getNonVolatileMemoryStart() { - return userFlashStart(); + if(_memoryType == Flash) + return userFlashStart(); + else + return getEepromBuffer(KNX_FLASH_SIZE); } size_t Platform::getNonVolatileMemorySize() { - return userFlashSizeEraseBlocks() * flashEraseBlockSize() * flashPageSize(); + if(_memoryType == Flash) + return userFlashSizeEraseBlocks() * flashEraseBlockSize() * flashPageSize(); + else + return KNX_FLASH_SIZE; + } void Platform::commitNonVolatileMemory() { - if(_bufferedEraseblockNumber > -1 && _bufferedEraseblockDirty) + if(_memoryType == Flash) { - writeBufferedEraseBlock(); - - free(_eraseblockBuffer); - _eraseblockBuffer = nullptr; - _bufferedEraseblockNumber = -1; // does that make sense? + if(_bufferedEraseblockNumber > -1 && _bufferedEraseblockDirty) + { + writeBufferedEraseBlock(); + + free(_eraseblockBuffer); + _eraseblockBuffer = nullptr; + _bufferedEraseblockNumber = -1; // does that make sense? + } + } + else + { + commitToEeprom(); } } uint32_t Platform::writeNonVolatileMemory(uint32_t relativeAddress, uint8_t* buffer, size_t size) { - while (size > 0) + if(_memoryType == Flash) { - loadEraseblockContaining(relativeAddress); - uint32_t start = bufferedEraseBlockStart(); - uint32_t end = bufferedEraseBlockEnd(); + while (size > 0) + { + loadEraseblockContaining(relativeAddress); + uint32_t start = bufferedEraseBlockStart(); + uint32_t end = bufferedEraseBlockEnd(); - ptrdiff_t offset = relativeAddress - start; - ptrdiff_t length = min(end - relativeAddress, size); - memcpy(_eraseblockBuffer + offset, buffer, length); - _bufferedEraseblockDirty = true; + ptrdiff_t offset = relativeAddress - start; + ptrdiff_t length = min(end - relativeAddress, size); + memcpy(_eraseblockBuffer + offset, buffer, length); + _bufferedEraseblockDirty = true; - relativeAddress += length; - buffer += length; - size -= length; + relativeAddress += length; + buffer += length; + size -= length; + } + return relativeAddress; + } + else + { + memcpy(getEepromBuffer(KNX_FLASH_SIZE)+relativeAddress, buffer, size); + return relativeAddress+size; } - return relativeAddress; } void Platform::loadEraseblockContaining(uint32_t relativeAddress) diff --git a/src/knx/platform.h b/src/knx/platform.h index be9a487..b075660 100644 --- a/src/knx/platform.h +++ b/src/knx/platform.h @@ -50,6 +50,11 @@ class Platform virtual void closeSpi(); virtual int readWriteSpi(uint8_t *data, size_t len); + //Memory + + virtual uint8_t* getEepromBuffer(uint16_t size); + virtual void commitToEeprom(); + virtual uint8_t* getNonVolatileMemoryStart(); virtual size_t getNonVolatileMemorySize(); virtual void commitNonVolatileMemory(); @@ -58,7 +63,6 @@ class Platform // size of one flash page in bytes virtual size_t flashPageSize(); - NvMemoryType NonVolatileMemoryType(); void NonVolatileMemoryType(NvMemoryType type); diff --git a/src/rp2040_arduino_platform.cpp b/src/rp2040_arduino_platform.cpp index ce03b71..12e0855 100644 --- a/src/rp2040_arduino_platform.cpp +++ b/src/rp2040_arduino_platform.cpp @@ -6,10 +6,12 @@ made to work with arduino-pico - "Raspberry Pi Pico Arduino core, for all RP2040 by Earl E. Philhower III https://github.com/earlephilhower/arduino-pico tested with V1.9.1 -by SirSydom 2021 +by SirSydom 2021-2022 -A maximum of 4kB emulated EEPROM is supported. -For more, use or own emulation (maybe with littlefs) +Uses direct flash reading/writing. +Size ist defined by KNX_FLASH_SIZE (default 4k). +Offset in Flash is defined by KNX_FLASH_OFFSET (default 1,5MiB / 0x180000). +EEPROM Emulation from arduino-pico core (max 4k) can be use by defining USE_RP2040_EEPROM_EMULATION ----------------------------------------------------*/ @@ -32,10 +34,16 @@ RP2040ArduinoPlatform::RP2040ArduinoPlatform() : ArduinoPlatform(&Serial1) #endif { + #ifndef USE_RP2040_EEPROM_EMULATION + _memoryType = Flash; + #endif } RP2040ArduinoPlatform::RP2040ArduinoPlatform( HardwareSerial* s) : ArduinoPlatform(s) { + #ifndef USE_RP2040_EEPROM_EMULATION + _memoryType = Flash; + #endif } uint32_t RP2040ArduinoPlatform::uniqueSerialNumber() @@ -55,6 +63,10 @@ void RP2040ArduinoPlatform::restart() watchdog_reboot(0,0,0); } +#ifdef USE_RP2040_EEPROM_EMULATION + +#pragma warning "Using EEPROM Simulation" + uint8_t * RP2040ArduinoPlatform::getEepromBuffer(uint16_t size) { if(size > 4096) @@ -79,6 +91,7 @@ void RP2040ArduinoPlatform::commitToEeprom() EEPROM.commit(); } +#else size_t RP2040ArduinoPlatform::flashEraseBlockSize() { @@ -142,5 +155,6 @@ void RP2040ArduinoPlatform::writeBufferedEraseBlock() } } #endif +#endif diff --git a/src/rp2040_arduino_platform.h b/src/rp2040_arduino_platform.h index 8f26e58..0bb8da7 100644 --- a/src/rp2040_arduino_platform.h +++ b/src/rp2040_arduino_platform.h @@ -12,6 +12,7 @@ #define KNX_FLASH_SIZE 1024 #endif + class RP2040ArduinoPlatform : public ArduinoPlatform { public: @@ -22,9 +23,11 @@ public: uint32_t uniqueSerialNumber(); //override; void restart(); + + #ifdef USE_RP2040_EEPROM_EMULATION uint8_t* getEepromBuffer(uint16_t size); void commitToEeprom(); - + #else // size of one EraseBlock in pages virtual size_t flashEraseBlockSize(); @@ -41,6 +44,7 @@ public: // writes _eraseblockBuffer to flash - overrides Plattform::writeBufferedEraseBlock() void writeBufferedEraseBlock(); + #endif }; #endif