From 4f6c837b78603dbdfc67c192d116ce9c78be4f57 Mon Sep 17 00:00:00 2001 From: SirSydom Date: Thu, 8 Jul 2021 19:45:33 +0200 Subject: [PATCH] added support for RP2040 (Raspberry Pi Pico) (#145) --- src/knx/bits.h | 4 +- src/knx/group_object.h | 2 +- src/knx_facade.cpp | 14 +++++- src/knx_facade.h | 16 +++++++ src/rp2040_arduino_platform.cpp | 82 +++++++++++++++++++++++++++++++++ src/rp2040_arduino_platform.h | 21 +++++++++ 6 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 src/rp2040_arduino_platform.cpp create mode 100644 src/rp2040_arduino_platform.h diff --git a/src/knx/bits.h b/src/knx/bits.h index cd0ca8a5..b513ef99 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 6a07b5f2..91c8b566 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 ee925479..5d48c7e0 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 c23bdd21..f2830a32 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 00000000..72341f58 --- /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 00000000..3077b217 --- /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