Finish ESP IDF native support with sample project kn-demo-diy-idf but not pass real test.

This commit is contained in:
VietDzung
2025-07-02 13:29:50 +07:00
parent 1be9a2b3f8
commit cb58f2e73d
14 changed files with 2374 additions and 9 deletions

View File

@@ -1,3 +1,5 @@
#ifdef ARDUINO
#include "arduino_platform.h"
#include "knx/bits.h"
@@ -309,3 +311,5 @@ void println(void)
ArduinoPlatform::SerialDebug->println();
}
#endif // KNX_NO_PRINT
#endif // ARDUINO

View File

@@ -1,5 +1,7 @@
#pragma once
#ifdef ARDUINO
#include "knx/platform.h"
#include "Arduino.h"
@@ -42,3 +44,4 @@ class ArduinoPlatform : public Platform
protected:
HardwareSerial* _knxSerial;
};
#endif // ARDUINO

View File

@@ -1,13 +1,13 @@
#ifdef ESP_PLATFORM
// esp_idf_platform.cpp
#include "esp_efuse.h"
// esp32_idf_platform.cpp
#include <esp_system.h>
#include <esp_mac.h>
#include "esp32_idf_platform.h"
#include "esp_log.h"
#include "knx/bits.h"
#include "nvs.h"
#include <esp_timer.h>
// Define a logging tag for this file
static const char* KTAG = "Esp32IdfPlatform";
Esp32IdfPlatform::Esp32IdfPlatform(uart_port_t uart_num)
: _uart_num(uart_num)
@@ -391,7 +391,14 @@ void Esp32IdfPlatform::commitToEeprom()
}
else
{
ESP_LOGI(KTAG, "Committed %d bytes to NVS.", _eeprom_size);
ESP_LOGI(KTAG, "Committed %" PRIu32 " bytes to NVS.", _eeprom_size);
}
}
uint32_t millis()
{
// esp_timer_get_time() returns microseconds, so we divide by 1000 for milliseconds.
// Cast to uint32_t to match the Arduino function signature.
return (uint32_t)(esp_timer_get_time() / 1000);
}
#endif

View File

@@ -43,6 +43,7 @@
#include <esp_timer.h>
#include <esp_log.h>
#include <esp_netif.h>
#include <lwip/inet.h>
#include <nvs_flash.h>
#include <driver/uart.h>
// ESP-IDF: Use FreeRTOS and ESP-IDF APIs for timing, GPIO, etc.
@@ -50,9 +51,16 @@
#include <freertos/task.h>
#include <driver/gpio.h>
// Define Arduino-like macros if needed for compatibility
#define lowByte(val) ((val)&255)
#define highByte(val) (((val) >> ((sizeof(val) - 1) << 3)) & 255)
#define bitRead(val, bitno) (((val) >> (bitno)) & 1)
#define DEC 10
#define HEX 16
#define LOW 0
#define HIGH 1
// Implement or map Arduino-like functions if needed
uint32_t millis();
#else // Non-Arduino platforms
#define lowByte(val) ((val)&255)
#define highByte(val) (((val) >> ((sizeof(val) - 1) << 3)) & 255)

View File

@@ -279,20 +279,34 @@ template <class P, class B> class KnxFacade : private SaveRestore
void start()
{
if (_progLedOffCallback == 0 || _progLedOnCallback == 0)
if (_progLedOffCallback == 0 || _progLedOnCallback == 0){
#if defined(ESP_PLATFORM)
gpio_reset_pin((gpio_num_t)ledPin());
gpio_set_direction((gpio_num_t)ledPin(), GPIO_MODE_OUTPUT);
#else
pinMode(ledPin(), OUTPUT);
#endif // ESP_PLATFORM
}
progLedOff();
if (_progButtonISRFuncPtr && _buttonPin >= 0)
{
#if defined(ESP_PLATFORM)
// TODO: add support for buttonPin() in ESP_PLATFORM
gpio_reset_pin((gpio_num_t)buttonPin());
gpio_set_direction((gpio_num_t)buttonPin(), GPIO_MODE_INPUT);
gpio_pullup_en((gpio_num_t)buttonPin());
#else
pinMode(buttonPin(), INPUT_PULLUP);
// Workaround for https://github.com/arduino/ArduinoCore-samd/issues/587
#if (ARDUINO_API_VERSION >= 10200)
attachInterrupt(_buttonPin, _progButtonISRFuncPtr, (PinStatus)CHANGE);
#else
attachInterrupt(_buttonPin, _progButtonISRFuncPtr, CHANGE);
#endif
#endif // ARDUINO_API_VERSION
#endif // ESP_PLATFORM
}
enabled(true);
@@ -470,16 +484,27 @@ template <class P, class B> class KnxFacade : private SaveRestore
void progLedOn()
{
if (_progLedOnCallback == 0)
if (_progLedOnCallback == 0){
#if defined(ESP_PLATFORM)
gpio_set_level((gpio_num_t)ledPin(), _ledPinActiveOn);
#else
digitalWrite(ledPin(), _ledPinActiveOn);
#endif // ESP_PLATFORM
}
else
_progLedOnCallback();
}
void progLedOff()
{
if (_progLedOffCallback == 0)
if (_progLedOffCallback == 0){
#if defined(ESP_PLATFORM)
gpio_set_level((gpio_num_t)ledPin(), 1 - _ledPinActiveOn);
#else
digitalWrite(ledPin(), HIGH - _ledPinActiveOn);
#endif // ESP_PLATFORM
}
else
_progLedOffCallback();
}

View File

@@ -1,3 +1,4 @@
#ifdef ARDUINO
#pragma once
#include "arduino_platform.h"
@@ -153,3 +154,4 @@ class RP2040ArduinoPlatform : public ArduinoPlatform
};
#endif
#endif // ARDUINO

View File

@@ -1,3 +1,4 @@
#ifdef ARDUINO
#include "arduino_platform.h"
#include "Arduino.h"
@@ -53,3 +54,4 @@ class SamdPlatform : public ArduinoPlatform
};
#endif
#endif // ARDUINO