Skip to content

Commit

Permalink
added support for RP2040 (Raspberry Pi Pico) (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ing-Dom authored Jul 8, 2021
1 parent 6254fc9 commit 4f6c837
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/knx/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#if defined(__linux__)
#include <arpa/inet.h>
#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) )
Expand All @@ -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 <Arduino.h>
#elif defined(ARDUINO_ARCH_ESP8266)
#include <Arduino.h>
Expand Down
2 changes: 1 addition & 1 deletion src/knx/group_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/knx_facade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<RP2040ArduinoPlatform, Bau07B0> knx(buttonUp);
#elif MASK_VERSION == 0x27B0
KnxFacade<RP2040ArduinoPlatform, Bau27B0> knx(buttonUp);
#elif MASK_VERSION == 0x2920
KnxFacade<RP2040ArduinoPlatform, Bau2920> 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
Expand Down
16 changes: 16 additions & 0 deletions src/knx_facade.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -387,6 +392,17 @@ template <class P, class B> 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<RP2040ArduinoPlatform, Bau07B0> knx;
#elif MASK_VERSION == 0x27B0
extern KnxFacade<RP2040ArduinoPlatform, Bau27B0> knx;
#elif MASK_VERSION == 0x2920
extern KnxFacade<RP2040ArduinoPlatform, Bau2920> 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
Expand Down
82 changes: 82 additions & 0 deletions src/rp2040_arduino_platform.cpp
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> 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 <knx/bits.h>

#include <Arduino.h>

//Pi Pico specific libs
#include <EEPROM.h> // EEPROM emulation in flash, part of Earl E Philhowers Pi Pico Arduino support
#include <pico/unique_id.h> // from Pico SDK
#include <hardware/watchdog.h> // 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


21 changes: 21 additions & 0 deletions src/rp2040_arduino_platform.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4f6c837

Please sign in to comment.