knx/src/esp32_platform.cpp

128 lines
2.8 KiB
C++
Raw Normal View History

#include "esp32_platform.h"
#ifdef ARDUINO_ARCH_ESP32
#include <Arduino.h>
#include <EEPROM.h>
#include "knx/bits.h"
#ifndef KNX_SERIAL
#define KNX_SERIAL Serial1
#endif
Esp32Platform::Esp32Platform()
#ifndef KNX_NO_DEFAULT_UART
: ArduinoPlatform(&KNX_SERIAL)
#endif
{
}
opt-out global KNX, ability to DIY construct knx object, minor cleanups (#121) * Refactored the knx_facade a bit to make the auto-building of the global knx object and the buttonUp() ISR function opt-in. Also added the ability to quite easily attach another serial (in this case tested with Serial2 of an ESP32). * Added visual studio code temp files to .gitignore * Fix for missing build flag for WifiManager in ESP32_ip demo * Added example in which we DIY build the KNX object and use ESP32's second UART to connect so that you can debug with the first one that's connected to USB. * Minor platform cleanups to remove compilation warnings. * ESP32 can't handle the defaulted 8192 byte EPROM. Next to that I needed to begin() the eprom lib in my setup() to get it to work and that requires knowing how much memory KNX will use. This fix makes the default more conservative and moves it's definition to the .h file so that other files can always use it as well. * After comments from thelsing, inverted the logic. The default global knx object is now opt-out by defining KNX_NO_AUTOMATIC_GLOBAL_INSTANCE. See the knx-demo-diy project for an example. Minor syntactic cleanups. * Removed hardwareserial from facade (detail which was not needed) Placed facade constructors together Minor syntactic cleanup * Fixed knx-cc1310 where own button routine was calling now private internal knx facade variable. Renamed files in knx-demo-diy Minor syntactic stuff * Added gitattributes for binary knxprod files
2021-02-05 15:57:45 +01:00
Esp32Platform::Esp32Platform(HardwareSerial* s) : ArduinoPlatform(s)
{
}
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);
}
uint32_t Esp32Platform::uniqueSerialNumber()
{
uint64_t chipid = ESP.getEfuseMac();
uint32_t upperId = (chipid >> 32) & 0xFFFFFFFF;
uint32_t lowerId = (chipid & 0xFFFFFFFF);
return (upperId ^ lowerId);
}
void Esp32Platform::restart()
{
println("restart");
ESP.restart();
}
void Esp32Platform::setupMultiCast(uint32_t addr, uint16_t port)
{
IPAddress mcastaddr(htonl(addr));
KNX_DEBUG_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);
KNX_DEBUG_SERIAL.printf("result %d\n", result);
}
void Esp32Platform::closeMultiCast()
{
_udp.stop();
}
bool Esp32Platform::sendBytesMultiCast(uint8_t * buffer, uint16_t len)
{
//printHex("<- ",buffer, len);
opt-out global KNX, ability to DIY construct knx object, minor cleanups (#121) * Refactored the knx_facade a bit to make the auto-building of the global knx object and the buttonUp() ISR function opt-in. Also added the ability to quite easily attach another serial (in this case tested with Serial2 of an ESP32). * Added visual studio code temp files to .gitignore * Fix for missing build flag for WifiManager in ESP32_ip demo * Added example in which we DIY build the KNX object and use ESP32's second UART to connect so that you can debug with the first one that's connected to USB. * Minor platform cleanups to remove compilation warnings. * ESP32 can't handle the defaulted 8192 byte EPROM. Next to that I needed to begin() the eprom lib in my setup() to get it to work and that requires knowing how much memory KNX will use. This fix makes the default more conservative and moves it's definition to the .h file so that other files can always use it as well. * After comments from thelsing, inverted the logic. The default global knx object is now opt-out by defining KNX_NO_AUTOMATIC_GLOBAL_INSTANCE. See the knx-demo-diy project for an example. Minor syntactic cleanups. * Removed hardwareserial from facade (detail which was not needed) Placed facade constructors together Minor syntactic cleanup * Fixed knx-cc1310 where own button routine was calling now private internal knx facade variable. Renamed files in knx-demo-diy Minor syntactic stuff * Added gitattributes for binary knxprod files
2021-02-05 15:57:45 +01:00
_udp.beginMulticastPacket();
_udp.write(buffer, len);
_udp.endPacket();
return true;
}
int Esp32Platform::readBytesMultiCast(uint8_t * buffer, uint16_t maxLen)
{
int len = _udp.parsePacket();
if (len == 0)
return 0;
if (len > maxLen)
{
KNX_DEBUG_SERIAL.printf("udp buffer to small. was %d, needed %d\n", maxLen, len);
fatalError();
}
_udp.read(buffer, len);
//printHex("-> ", buffer, len);
return len;
}
bool Esp32Platform::sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len)
{
IPAddress ucastaddr(htonl(addr));
println("sendBytesUniCast endPacket fail");
if(_udp.beginPacket(ucastaddr, port) == 1) {
_udp.write(buffer, len);
if(_udp.endPacket() == 0) println("sendBytesUniCast endPacket fail");
}
else println("sendBytesUniCast beginPacket fail");
return true;
}
uint8_t * Esp32Platform::getEepromBuffer(uint16_t size)
{
uint8_t * eepromptr = EEPROM.getDataPtr();
if(eepromptr == nullptr) {
EEPROM.begin(size);
eepromptr = EEPROM.getDataPtr();
}
return eepromptr;
}
void Esp32Platform::commitToEeprom()
{
EEPROM.getDataPtr(); // trigger dirty flag in EEPROM lib to make sure data will be written to flash
EEPROM.commit();
}
#endif