Start support for native ESP IDF. Not working

This commit is contained in:
VietDzung 2025-07-02 10:52:57 +07:00
parent 8767a60f79
commit c5dada7eb0
6 changed files with 245 additions and 1 deletions

135
src/esp32_idf_platform.cpp Normal file
View File

@ -0,0 +1,135 @@
#ifdef ESP_PLATFORM
#include "esp32_idf_platform.h"
#include <cstring>
#include <esp_mac.h>
#include <esp_wifi.h>
#include <esp_netif.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <esp_log.h>
#include <esp_event.h>
#include <lwip/sockets.h>
#include <lwip/inet.h>
#include <lwip/ip_addr.h>
#include <driver/uart.h>
#define KNX_IDF_UART_NUM UART_NUM_1
#define KNX_IDF_UART_BAUD 19200
#define KNX_IDF_UART_TX_BUF_SIZE 256
#define KNX_IDF_UART_RX_BUF_SIZE 256
Esp32IdfPlatform::Esp32IdfPlatform()
: ArduinoPlatform(nullptr) // No HardwareSerial in IDF
{
// Optionally initialize NVS, WiFi, etc. here
}
void Esp32IdfPlatform::knxUartPins(int8_t rxPin, int8_t txPin) {
_rxPin = rxPin;
_txPin = txPin;
}
void Esp32IdfPlatform::setupUart() {
uart_config_t uart_config = {
.baud_rate = KNX_IDF_UART_BAUD,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_EVEN,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(KNX_IDF_UART_NUM, &uart_config);
uart_set_pin(KNX_IDF_UART_NUM, _txPin, _rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(KNX_IDF_UART_NUM, KNX_IDF_UART_RX_BUF_SIZE, KNX_IDF_UART_TX_BUF_SIZE, 0, NULL, 0);
}
uint32_t Esp32IdfPlatform::currentIpAddress() {
esp_netif_ip_info_t ip_info;
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (netif && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) {
return ip_info.ip.addr;
}
return 0;
}
uint32_t Esp32IdfPlatform::currentSubnetMask() {
esp_netif_ip_info_t ip_info;
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (netif && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) {
return ip_info.netmask.addr;
}
return 0;
}
uint32_t Esp32IdfPlatform::currentDefaultGateway() {
esp_netif_ip_info_t ip_info;
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (netif && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) {
return ip_info.gw.addr;
}
return 0;
}
void Esp32IdfPlatform::macAddress(uint8_t* addr) {
esp_read_mac(addr, ESP_MAC_WIFI_STA);
}
uint32_t Esp32IdfPlatform::uniqueSerialNumber() {
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_STA);
uint32_t upper = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
uint32_t lower = (mac[4] << 8) | mac[5];
return upper ^ lower;
}
void Esp32IdfPlatform::restart() {
esp_restart();
}
void Esp32IdfPlatform::setupMultiCast(uint32_t addr, uint16_t port) {
if (_udpSock >= 0) close(_udpSock);
_udpSock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in mcast_addr = {};
mcast_addr.sin_family = AF_INET;
mcast_addr.sin_addr.s_addr = htonl(addr);
mcast_addr.sin_port = htons(port);
// Set socket options for multicast as needed
// ...
}
void Esp32IdfPlatform::closeMultiCast() {
if (_udpSock >= 0) {
close(_udpSock);
_udpSock = -1;
}
}
bool Esp32IdfPlatform::sendBytesMultiCast(uint8_t* buffer, uint16_t len) {
// Implement sending to multicast group
// ...
return true;
}
int Esp32IdfPlatform::readBytesMultiCast(uint8_t* buffer, uint16_t maxLen, uint32_t& src_addr, uint16_t& src_port) {
// Implement reading from multicast socket
// ...
return 0;
}
bool Esp32IdfPlatform::sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len) {
// Implement sending to unicast address
// ...
return true;
}
uint8_t* Esp32IdfPlatform::getEepromBuffer(uint32_t size) {
// Use NVS or other ESP-IDF storage
// ...
return nullptr;
}
void Esp32IdfPlatform::commitToEeprom() {
// Commit NVS or other storage
// ...
}
#endif

59
src/esp32_idf_platform.h Normal file
View File

@ -0,0 +1,59 @@
#ifdef ESP_PLATFORM
#include "arduino_platform.h"
#include <stdint.h>
#include <esp_netif.h>
#include <esp_wifi.h>
#include <nvs_flash.h>
#include <esp_system.h>
#include <esp_event.h>
#include <esp_log.h>
#include <lwip/sockets.h>
#include <lwip/inet.h>
class Esp32IdfPlatform : public ArduinoPlatform
{
public:
Esp32IdfPlatform();
Esp32IdfPlatform(/* UART params if needed */);
// uart
void knxUartPins(int8_t rxPin, int8_t txPin);
void setupUart() override;
// ip stuff
uint32_t currentIpAddress() override;
uint32_t currentSubnetMask() override;
uint32_t currentDefaultGateway() override;
void macAddress(uint8_t* addr) override;
// unique serial number
uint32_t uniqueSerialNumber() override;
// basic stuff
void restart();
//multicast
void setupMultiCast(uint32_t addr, uint16_t port) override;
void closeMultiCast() override;
bool sendBytesMultiCast(uint8_t* buffer, uint16_t len) override;
int readBytesMultiCast(uint8_t* buffer, uint16_t maxLen, uint32_t& src_addr, uint16_t& src_port) override;
//unicast
bool sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len) override;
//memory
uint8_t* getEepromBuffer(uint32_t size);
void commitToEeprom();
protected:
in_addr _remoteIP;
protected:
uint16_t _remotePort;
private:
int _udpSock = -1;
int8_t _rxPin = -1;
int8_t _txPin = -1;
// Add NVS handle, etc. as needed
};
#endif

View File

@ -2,7 +2,10 @@
#include <cstddef>
#include <cstdint>
#if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP32)
#include "knx_log.h" // ESP32 Logs
#endif
#if defined(__linux__)
#include <arpa/inet.h>
@ -34,6 +37,22 @@
#elif defined(ARDUINO_ARCH_ESP32)
#include <Arduino.h>
#include <esp_wifi.h>
#elif defined(ESP_PLATFORM)
#include <esp_wifi.h>
#include <esp_system.h>
#include <esp_timer.h>
#include <esp_log.h>
#include <esp_netif.h>
#include <nvs_flash.h>
#include <driver/uart.h>
// ESP-IDF: Use FreeRTOS and ESP-IDF APIs for timing, GPIO, etc.
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/gpio.h>
// Define Arduino-like macros if needed for compatibility
#define LOW 0
#define HIGH 1
// Implement or map Arduino-like functions if needed
#else // Non-Arduino platforms
#define lowByte(val) ((val)&255)
#define highByte(val) (((val) >> ((sizeof(val) - 1) << 3)) & 255)

View File

@ -21,7 +21,7 @@ enum ComFlag : uint8_t
class GroupObject;
#ifndef HAS_FUNCTIONAL
#if defined(__linux__) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32) || defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_RP2040)
#if defined(__linux__) || defined(ARDUINO_ARCH_ESP32) || defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_STM32) || defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_RP2040)
#define HAS_FUNCTIONAL 1
#else
#define HAS_FUNCTIONAL 0

View File

@ -95,6 +95,18 @@ ICACHE_RAM_ATTR void buttonEvent()
#error "Mask version not supported on ARDUINO_ARCH_ESP32"
#endif
#elif defined(ESP_PLATFORM)
// predefined global instance for TP or IP or TP/IP coupler
#if MASK_VERSION == 0x07B0
KnxFacade<Esp32IdfPlatform, Bau07B0> knx(buttonEvent);
#elif MASK_VERSION == 0x57B0
KnxFacade<Esp32IdfPlatform, Bau57B0> knx(buttonEvent);
#elif MASK_VERSION == 0x091A
KnxFacade<Esp32IdfPlatform, Bau091A> knx(buttonEvent);
#else
#error "Mask version not supported on ESP_IDF_ESP32"
#endif
#elif defined(ARDUINO_ARCH_STM32)
#if MASK_VERSION == 0x07B0
KnxFacade<Stm32Platform, Bau07B0> knx(buttonEvent);

View File

@ -35,6 +35,14 @@
#ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE
void buttonUp();
#endif
#elif defined(ESP_PLATFORM)
#if !defined(LED_BUILTIN)
#define LED_BUILTIN 13
#endif
#include "esp32_idf_platform.h"
#ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE
void buttonUp();
#endif
#elif defined(ARDUINO_ARCH_STM32)
#include "stm32_platform.h"
#ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE
@ -526,6 +534,17 @@ template <class P, class B> class KnxFacade : private SaveRestore
#else
#error "Mask version not supported on ARDUINO_ARCH_ESP32"
#endif
#elif defined(ESP_PLATFORM)
// predefined global instance for TP or IP or TP/IP coupler
#if MASK_VERSION == 0x07B0
extern KnxFacade<Esp32IdfPlatform, Bau07B0> knx;
#elif MASK_VERSION == 0x57B0
extern KnxFacade<Esp32IdfPlatform, Bau57B0> knx;
#elif MASK_VERSION == 0x091A
extern KnxFacade<Esp32IdfPlatform, Bau091A> knx;
#else
#error "Mask version not supported on ESP_PLATFORM"
#endif
#elif defined(ARDUINO_ARCH_STM32)
// predefined global instance for TP only
#if MASK_VERSION == 0x07B0