forked from thelsing/knx
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathesp32_platform.cpp
147 lines (123 loc) · 3.18 KB
/
esp32_platform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#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
{
#ifdef KNX_UART_RX_PIN
_rxPin = KNX_UART_RX_PIN;
#endif
#ifdef KNX_UART_TX_PIN
_txPin = KNX_UART_TX_PIN;
#endif
}
Esp32Platform::Esp32Platform(HardwareSerial* s) : ArduinoPlatform(s)
{
}
void Esp32Platform::knxUartPins(int8_t rxPin, int8_t txPin)
{
_rxPin = rxPin;
_txPin = txPin;
}
// ESP specific uart handling with pins
void Esp32Platform::setupUart()
{
_knxSerial->begin(19200, SERIAL_8E1, _rxPin, _txPin);
while (!_knxSerial)
;
}
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