knx/src/esp32_platform.cpp
Dom dce92403e5
32bit eeprom size (#230)
* add commitNonVolatileMemory overload to batch write chars n times to flash

* changed uint8_t getEepromBuffer(uint16_t size) to uint8_t getEepromBuffer(uint32_t size) to allow eeprom > 65k

* reworked PR #230
2023-02-13 21:39:30 +01:00

128 lines
2.8 KiB
C++

#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
{
}
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);
_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(uint32_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