From 14d2da82f17a918488968b0e6d71740a4b1f528e Mon Sep 17 00:00:00 2001 From: Fabian Schmieder Date: Mon, 26 Aug 2019 13:45:14 +0200 Subject: [PATCH] added ESP32 platform (#30) * added ESP32 platform * fixed typo in comments --- examples/knx-demo/knx-demo.ino | 2 +- src/esp32_platform.cpp | 331 +++++++++++++++++++++++++++++++++ src/esp32_platform.h | 48 +++++ src/knx/bits.h | 3 + src/knx_facade.cpp | 4 +- src/knx_facade.h | 6 + 6 files changed, 392 insertions(+), 2 deletions(-) create mode 100644 src/esp32_platform.cpp create mode 100644 src/esp32_platform.h diff --git a/examples/knx-demo/knx-demo.ino b/examples/knx-demo/knx-demo.ino index a4ee3f4..34cc8fd 100644 --- a/examples/knx-demo/knx-demo.ino +++ b/examples/knx-demo/knx-demo.ino @@ -84,7 +84,7 @@ void setup() SerialDBG.println(knx.paramByte(4)); } - // pin or GPIO the programming led is connected to. Default is LED_BUILDIN + // pin or GPIO the programming led is connected to. Default is LED_BUILTIN // knx.ledPin(LED_BUILTIN); // is the led active on HIGH or low? Default is LOW // knx.ledPinActiveOn(HIGH); diff --git a/src/esp32_platform.cpp b/src/esp32_platform.cpp new file mode 100644 index 0000000..3560900 --- /dev/null +++ b/src/esp32_platform.cpp @@ -0,0 +1,331 @@ +#include "esp32_platform.h" + +#ifdef ARDUINO_ARCH_ESP32 +#include +#include + +#include "knx/bits.h" + +#define SerialDBG Serial + +Esp32Platform::Esp32Platform() +{ +} + +uint32_t Esp32Platform::currentIpAddress() +{ + return WiFi.localIP(); +} + +uint32_t Esp32Platform::currentSubnetMask() +{ + return WiFi.subnetMask(); +} + +uint32_t Esp32Platform::currentDefaultGateway() +{ + return WiFi.gatewayIP(); +} + +void Esp32Platform::macAddress(uint8_t * addr) +{ + esp_wifi_get_mac(WIFI_IF_STA, addr); +} + +void Esp32Platform::restart() +{ + Serial.println("restart"); + ESP.restart(); +} + +void Esp32Platform::fatalError() +{ + Serial.println("GURU MEDITATION - fatal error!"); + const int period = 200; + while (true) + { +#ifdef LED_BUILTIN + if ((millis() % period) > (period / 2)) + digitalWrite(LED_BUILTIN, HIGH); + else + digitalWrite(LED_BUILTIN, LOW); +#endif + } +} + +void Esp32Platform::setupMultiCast(uint32_t addr, uint16_t port) +{ + _mulitcastAddr = htonl(addr); + _mulitcastPort = port; + IPAddress mcastaddr(_mulitcastAddr); + + Serial.printf("setup multicast addr: %s port: %d ip: %s\n", mcastaddr.toString().c_str(), port, + WiFi.localIP().toString().c_str()); + uint8_t result = _udp.beginMulticast(mcastaddr, port); + Serial.printf("result %d\n", result); +} + +void Esp32Platform::closeMultiCast() +{ + _udp.stop(); +} + +bool Esp32Platform::sendBytes(uint8_t * buffer, uint16_t len) +{ + //printHex("<- ",buffer, len); + int result = 0; + result = _udp.beginMulticastPacket(); + result = _udp.write(buffer, len); + result = _udp.endPacket(); + return true; +} + +int Esp32Platform::readBytes(uint8_t * buffer, uint16_t maxLen) +{ + int len = _udp.parsePacket(); + if (len == 0) + return 0; + + if (len > maxLen) + { + Serial.printf("udp buffer to small. was %d, needed %d\n", maxLen, len); + fatalError(); + } + + _udp.read(buffer, len); + //printHex("-> ", buffer, len); + return len; +} + +uint8_t * Esp32Platform::getEepromBuffer(uint16_t size) +{ + EEPROM.begin(size); + return EEPROM.getDataPtr(); +} + +void Esp32Platform::commitToEeprom() +{ + EEPROM.commit(); +} + +void Esp32Platform::setupUart() +{ + Serial.begin(19200, SERIAL_8E1); + while (!Serial) ; +} + + +void Esp32Platform::closeUart() +{ + Serial.end(); +} + + +int Esp32Platform::uartAvailable() +{ + return Serial.available(); +} + + +size_t Esp32Platform::writeUart(const uint8_t data) +{ + printHex(" 0) + printHex("p>", (uint8_t*)&val, 1); + return val; +} + + +size_t Esp32Platform::readBytesUart(uint8_t *buffer, size_t length) +{ + size_t toRead = length; + uint8_t* pos = buffer; + while (toRead > 0) + { + size_t val = Serial.readBytes(pos, toRead); + pos += val; + toRead -= val; + } + + + printHex("p>", buffer, length); + return length; +} + +void print(const char* s) +{ + Serial.printf("%s", s); +} +void print(char c) +{ + Serial.printf("%c", c); +} + +void print(unsigned char num) +{ + print(num, DEC); +} + +void print(unsigned char num, int base) +{ + if (base == HEX) + Serial.printf("%X", num); + else + Serial.printf("%d", num); +} + +void print(int num) +{ + print(num, DEC); +} + +void print(int num, int base) +{ + if (base == HEX) + Serial.printf("%X", num); + else + Serial.printf("%d", num); +} + +void print(unsigned int num) +{ + print(num, DEC); +} + +void print(unsigned int num, int base) +{ + if (base == HEX) + Serial.printf("%X", num); + else + Serial.printf("%d", num); +} + +void print(long num) +{ + print(num, DEC); +} + +void print(long num, int base) +{ + if (base == HEX) + Serial.printf("%lX", num); + else + Serial.printf("%ld", num); +} + +void print(unsigned long num) +{ + print(num, DEC); +} + +void print(unsigned long num, int base) +{ + if (base == HEX) + Serial.printf("%lX", num); + else + Serial.printf("%ld", num); +} + +void print(double num) +{ + Serial.printf("%f", num); +} + +void println(const char* s) +{ + Serial.printf("%s\n", s); +} +void println(char c) +{ + Serial.printf("%c\n", c); +} + +void println(unsigned char num) +{ + println(num, DEC); +} + +void println(unsigned char num, int base) +{ + if (base == HEX) + Serial.printf("%X\n", num); + else + Serial.printf("%d\n", num); +} + +void println(int num) +{ + println(num, DEC); +} + +void println(int num, int base) +{ + if (base == HEX) + Serial.printf("%X\n", num); + else + Serial.printf("%d\n", num); +} + +void println(unsigned int num) +{ + println(num, DEC); +} + +void println(unsigned int num, int base) +{ + if (base == HEX) + Serial.printf("%X\n", num); + else + Serial.printf("%d\n", num); +} + +void println(long num) +{ + println(num, DEC); +} + +void println(long num, int base) +{ + if (base == HEX) + Serial.printf("%lX\n", num); + else + Serial.printf("%ld\n", num); +} + +void println(unsigned long num) +{ + println(num, DEC); +} + +void println(unsigned long num, int base) +{ + if (base == HEX) + Serial.printf("%lX\n", num); + else + Serial.printf("%ld\n", num); +} + +void println(double num) +{ + Serial.printf("%f\n", num); +} + +void println(void) +{ + Serial.printf("\n"); +} +#endif diff --git a/src/esp32_platform.h b/src/esp32_platform.h new file mode 100644 index 0000000..4f89656 --- /dev/null +++ b/src/esp32_platform.h @@ -0,0 +1,48 @@ +#ifdef ARDUINO_ARCH_ESP32 +#include "knx/platform.h" +#include "knx/platform.h" +#include +#include + +#define SerialDBG Serial + +class Esp32Platform : public Platform +{ +public: + Esp32Platform(); + + // ip stuff + uint32_t currentIpAddress(); + uint32_t currentSubnetMask(); + uint32_t currentDefaultGateway(); + void macAddress(uint8_t* addr); + + // basic stuff + void restart(); + void fatalError(); + + //multicast + void setupMultiCast(uint32_t addr, uint16_t port); + void closeMultiCast(); + bool sendBytes(uint8_t* buffer, uint16_t len); + int readBytes(uint8_t* buffer, uint16_t maxLen); + + //uart + void setupUart(); + void closeUart(); + int uartAvailable(); + size_t writeUart(const uint8_t data); + size_t writeUart(const uint8_t *buffer, size_t size); + int readUart(); + size_t readBytesUart(uint8_t *buffer, size_t length); + + //memory + uint8_t* getEepromBuffer(uint16_t size); + void commitToEeprom(); +private: + uint32_t _mulitcastAddr; + uint16_t _mulitcastPort; + WiFiUDP _udp; +}; + +#endif \ No newline at end of file diff --git a/src/knx/bits.h b/src/knx/bits.h index 49f20e8..fbe8d90 100644 --- a/src/knx/bits.h +++ b/src/knx/bits.h @@ -44,6 +44,9 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode); #elif ARDUINO_ARCH_ESP8266 #include #include +#elif ARDUINO_ARCH_ESP32 +#include +#include #endif void print(const char[]); diff --git a/src/knx_facade.cpp b/src/knx_facade.cpp index 63feeb0..9f32b9b 100644 --- a/src/knx_facade.cpp +++ b/src/knx_facade.cpp @@ -7,6 +7,8 @@ KnxFacade knx; #define ICACHE_RAM_ATTR #elif ARDUINO_ARCH_ESP8266 KnxFacade knx; +#elif ARDUINO_ARCH_ESP32 +KnxFacade knx; #elif __linux__ #define ICACHE_RAM_ATTR #endif @@ -15,5 +17,5 @@ ICACHE_RAM_ATTR void buttonUp() { #ifndef __linux__ knx._toogleProgMode = true; - #endif + #endif } \ No newline at end of file diff --git a/src/knx_facade.h b/src/knx_facade.h index 469c4d3..bf1368d 100644 --- a/src/knx_facade.h +++ b/src/knx_facade.h @@ -8,6 +8,10 @@ #elif ARDUINO_ARCH_ESP8266 #include "esp_platform.h" #include "knx/bau57B0.h" +#elif ARDUINO_ARCH_ESP32 +#define LED_BUILTIN 13 +#include "esp32_platform.h" +#include "knx/bau57B0.h" #else #include "linux_platform.h" #include "knx/bau57B0.h" @@ -274,6 +278,8 @@ template class KnxFacade : private SaveRestore extern KnxFacade knx; #elif ARDUINO_ARCH_ESP8266 extern KnxFacade knx; +#elif ARDUINO_ARCH_ESP32 +extern KnxFacade knx; #elif __linux__ // no predefined global instance #endif \ No newline at end of file