diff --git a/src/knx/bits.h b/src/knx/bits.h index cd0ca8a..b513ef9 100644 --- a/src/knx/bits.h +++ b/src/knx/bits.h @@ -5,7 +5,7 @@ #if defined(__linux__) #include -#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) || defined (DeviceFamily_CC13X0) +#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) || defined (DeviceFamily_CC13X0) #define getbyte(x,n) (*(((uint8_t*)&(x))+n)) #define htons(x) ( (getbyte(x,0)<<8) | getbyte(x,1) ) #define htonl(x) ( (getbyte(x,0)<<24) | (getbyte(x,1)<<16) | (getbyte(x,2)<<8) | getbyte(x,3) ) @@ -25,7 +25,7 @@ #define ABS(x) ((x > 0) ? (x) : (-x)) #endif -#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) +#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) #include #elif defined(ARDUINO_ARCH_ESP8266) #include diff --git a/src/knx/group_object.h b/src/knx/group_object.h index 6a07b5f..91c8b56 100644 --- a/src/knx/group_object.h +++ b/src/knx/group_object.h @@ -20,7 +20,7 @@ enum ComFlag class GroupObject; #ifndef HAS_FUNCTIONAL -# if defined(__linux__) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32) || defined (ARDUINO_ARCH_SAMD) +# if defined(__linux__) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32) || defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_RP2040) # define HAS_FUNCTIONAL 1 # else # define HAS_FUNCTIONAL 0 diff --git a/src/knx_facade.cpp b/src/knx_facade.cpp index ee92547..5d48c7e 100644 --- a/src/knx_facade.cpp +++ b/src/knx_facade.cpp @@ -7,7 +7,8 @@ #if (defined(ARDUINO_ARCH_STM32) || \ defined(ARDUINO_ARCH_ESP32) || \ defined(ARDUINO_ARCH_ESP8266) || \ - defined(ARDUINO_ARCH_SAMD)) + defined(ARDUINO_ARCH_SAMD) || \ + defined(ARDUINO_ARCH_RP2040)) // Only ESP8266 and ESP32 have this define. For all other platforms this is just empty. #ifndef ICACHE_RAM_ATTR @@ -35,6 +36,17 @@ #else #error "Mask version not supported on ARDUINO_ARCH_SAMD" #endif + #elif defined(ARDUINO_ARCH_RP2040) + // predefined global instance for TP or RF or TP/RF coupler + #if MASK_VERSION == 0x07B0 + KnxFacade knx(buttonUp); + #elif MASK_VERSION == 0x27B0 + KnxFacade knx(buttonUp); + #elif MASK_VERSION == 0x2920 + KnxFacade knx(buttonUp); + #else + #error "Mask version not supported on ARDUINO_ARCH_RP2040" + #endif #elif defined(ARDUINO_ARCH_ESP8266) // predefined global instance for TP or IP or TP/IP coupler diff --git a/src/knx_facade.h b/src/knx_facade.h index c23bdd2..f2830a3 100644 --- a/src/knx_facade.h +++ b/src/knx_facade.h @@ -17,6 +17,11 @@ #ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE void buttonUp(); #endif +#elif defined(ARDUINO_ARCH_RP2040) + #include "rp2040_arduino_platform.h" + #ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE + void buttonUp(); + #endif #elif defined(ARDUINO_ARCH_ESP8266) #include "esp_platform.h" #ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE @@ -387,6 +392,17 @@ template class KnxFacade : private SaveRestore #else #error "Mask version not supported on ARDUINO_ARCH_SAMD" #endif + #elif defined(ARDUINO_ARCH_RP2040) + // predefined global instance for TP or RF or TP/RF coupler + #if MASK_VERSION == 0x07B0 + extern KnxFacade knx; + #elif MASK_VERSION == 0x27B0 + extern KnxFacade knx; + #elif MASK_VERSION == 0x2920 + extern KnxFacade knx; + #else + #error "Mask version not supported on ARDUINO_ARCH_RP2040" + #endif #elif defined(ARDUINO_ARCH_ESP8266) // predefined global instance for TP or IP or TP/IP coupler #if MASK_VERSION == 0x07B0 diff --git a/src/rp2040_arduino_platform.cpp b/src/rp2040_arduino_platform.cpp new file mode 100644 index 0000000..72341f5 --- /dev/null +++ b/src/rp2040_arduino_platform.cpp @@ -0,0 +1,82 @@ +/*----------------------------------------------------- + +Plattform for Raspberry Pi Pico and other RP2040 boards + +made to work with arduino-pico - "Raspberry Pi Pico Arduino core, for all RP2040 boards" +by Earl E. Philhower III https://github.com/earlephilhower/arduino-pico +tested with V1.9.1 + +by SirSydom 2021 + +A maximum of 4kB emulated EEPROM is supported. +For more, use or own emulation (maybe with littlefs) + +----------------------------------------------------*/ + +#include "rp2040_arduino_platform.h" + +#ifdef ARDUINO_ARCH_RP2040 +#include + +#include + +//Pi Pico specific libs +#include // EEPROM emulation in flash, part of Earl E Philhowers Pi Pico Arduino support +#include // from Pico SDK +#include // from Pico SDK + + +RP2040ArduinoPlatform::RP2040ArduinoPlatform() +#ifndef KNX_NO_DEFAULT_UART + : ArduinoPlatform(&Serial1) +#endif +{ +} + +RP2040ArduinoPlatform::RP2040ArduinoPlatform( HardwareSerial* s) : ArduinoPlatform(s) +{ +} + +uint32_t RP2040ArduinoPlatform::uniqueSerialNumber() +{ + pico_unique_board_id_t id; // 64Bit unique serial number from the QSPI flash + pico_get_unique_board_id(&id); + + // use lower 4 byte and convert to unit32_t + uint32_t uid = ((uint32_t)(id.id[4]) << 24) | ((uint32_t)(id.id[5]) << 16) | ((uint32_t)(id.id[6]) << 8) | (uint32_t)(id.id[7]); + + return uid; +} + +void RP2040ArduinoPlatform::restart() +{ + println("restart"); + watchdog_reboot(0,0,0); +} + +uint8_t * RP2040ArduinoPlatform::getEepromBuffer(uint16_t size) +{ + if(size > 4096) + { + println("KNX_FLASH_SIZE to big for EEPROM emulation (max. 4kB)"); + fatalError(); + } + + uint8_t * eepromptr = EEPROM.getDataPtr(); + + if(eepromptr == nullptr) + { + EEPROM.begin(4096); + eepromptr = EEPROM.getDataPtr(); + } + + return eepromptr; +} + +void RP2040ArduinoPlatform::commitToEeprom() +{ + EEPROM.commit(); +} +#endif + + diff --git a/src/rp2040_arduino_platform.h b/src/rp2040_arduino_platform.h new file mode 100644 index 0000000..3077b21 --- /dev/null +++ b/src/rp2040_arduino_platform.h @@ -0,0 +1,21 @@ +#include "arduino_platform.h" + +#include "Arduino.h" + +#ifdef ARDUINO_ARCH_RP2040 + +class RP2040ArduinoPlatform : public ArduinoPlatform +{ +public: + RP2040ArduinoPlatform(); + RP2040ArduinoPlatform( HardwareSerial* s); + + // unique serial number + uint32_t uniqueSerialNumber() override; + + void restart(); + uint8_t* getEepromBuffer(uint16_t size); + void commitToEeprom(); +}; + +#endif