From 72259b4eac796b2788b6365dbb99734ea5380c02 Mon Sep 17 00:00:00 2001 From: Paula Scharf Date: Thu, 12 Jun 2025 13:12:16 +0200 Subject: [PATCH 01/13] fix(board): Update variant.cpp for senseBox MCU-S2 ESP32-S2 --- variants/sensebox_mcu_esp32s2/variant.cpp | 78 +++++++++++++++-------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/variants/sensebox_mcu_esp32s2/variant.cpp b/variants/sensebox_mcu_esp32s2/variant.cpp index 0c58ef2cbe2..c34ab772291 100644 --- a/variants/sensebox_mcu_esp32s2/variant.cpp +++ b/variants/sensebox_mcu_esp32s2/variant.cpp @@ -1,29 +1,10 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2021 Ha Thach (tinyusb.org) for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - #include "esp32-hal-gpio.h" #include "pins_arduino.h" +#include "esp_partition.h" +#include "esp_system.h" +#include "esp_ota_ops.h" +#include "esp_log.h" +#include extern "C" { @@ -41,12 +22,53 @@ void initVariant(void) { pinMode(PIN_XB1_ENABLE, OUTPUT); digitalWrite(PIN_XB1_ENABLE, LOW); - //enable UART by default - pinMode(PIN_UART_ENABLE, OUTPUT); - digitalWrite(PIN_UART_ENABLE, LOW); + //enable UART only for chip without PSRAM + esp_chip_info_t chip_info; + esp_chip_info(&chip_info); + if (chip_info.revision <= 0) { + pinMode(PIN_UART_ENABLE, OUTPUT); + digitalWrite(PIN_UART_ENABLE, LOW); + } //enable PD-Sensor by default pinMode(PD_ENABLE, OUTPUT); digitalWrite(PD_ENABLE, HIGH); + + // Neuen Button-Pin definieren (z.B. GPIO 0 als Beispiel) + const int PIN_BUTTON = 0; + pinMode(PIN_BUTTON, INPUT_PULLUP); + + // Button gedrückt halten + unsigned long pressStartTime = 0; + bool buttonPressed = false; + + // Wait 5 seconds for the button to be pressed + unsigned long startTime = millis(); + + // Check if button is pressed + while (millis() - startTime < 5000) { + if (digitalRead(PIN_BUTTON) == LOW) { + if (!buttonPressed) { + // The button was pressed + buttonPressed = true; + } + } else if (buttonPressed) { + // When the button is pressed and then released, boot into the OTA1 partition + const esp_partition_t* ota1_partition = esp_partition_find_first( + ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL); + + if (ota1_partition) { + esp_err_t err = esp_ota_set_boot_partition(ota1_partition); + if (err == ESP_OK) { + esp_restart(); // restart, to boot OTA1 partition + } else { + ESP_LOGE("OTA", "Error setting OTA1 partition: %s", esp_err_to_name(err)); + } + } + // Abort after releasing the button + break; + } + } } -} + +} \ No newline at end of file From 0501922e1158cca16099fb995e937db9bffdddae Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Mon, 16 Jun 2025 07:15:54 -0300 Subject: [PATCH 02/13] feat(openthread): adds native api (#11474) * feat(openthread): adds native api * feat(openthread): adds source code to CMakeLists.txt * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- CMakeLists.txt | 1 + libraries/OpenThread/README.md | 201 +++++++++- .../examples/{ => CLI}/COAP/coap_lamp/ci.json | 0 .../{ => CLI}/COAP/coap_lamp/coap_lamp.ino | 9 +- .../{ => CLI}/COAP/coap_switch/ci.json | 0 .../COAP/coap_switch/coap_switch.ino | 9 +- .../{ => CLI}/SimpleCLI/SimpleCLI.ino | 5 +- .../examples/{ => CLI}/SimpleCLI/ci.json | 0 .../{ => CLI}/SimpleNode/SimpleNode.ino | 9 +- .../examples/{ => CLI}/SimpleNode/ci.json | 0 .../ExtendedRouterNode/ExtendedRouterNode.ino | 9 +- .../ExtendedRouterNode/ci.json | 0 .../LeaderNode/LeaderNode.ino | 9 +- .../SimpleThreadNetwork/LeaderNode/ci.json | 0 .../RouterNode/RouterNode.ino | 9 +- .../SimpleThreadNetwork/RouterNode/ci.json | 0 .../{ => CLI}/ThreadScan/ThreadScan.ino | 7 +- .../examples/{ => CLI}/ThreadScan/ci.json | 0 .../examples/{ => CLI}/onReceive/ci.json | 0 .../{ => CLI}/onReceive/onReceive.ino | 3 +- .../LeaderNode/LeaderNode.ino | 34 ++ .../SimpleThreadNetwork/LeaderNode/ci.json | 6 + .../RouterNode/RouterNode.ino | 29 ++ .../SimpleThreadNetwork/RouterNode/ci.json | 6 + libraries/OpenThread/keywords.txt | 26 ++ libraries/OpenThread/src/OThread.cpp | 364 ++++++++++++++++++ libraries/OpenThread/src/OThread.h | 107 +++++ libraries/OpenThread/src/OThreadCLI.cpp | 156 ++------ libraries/OpenThread/src/OThreadCLI.h | 6 +- libraries/OpenThread/src/OThreadCLI_Util.cpp | 22 +- libraries/OpenThread/src/OThreadCLI_Util.h | 11 - 31 files changed, 833 insertions(+), 205 deletions(-) rename libraries/OpenThread/examples/{ => CLI}/COAP/coap_lamp/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/COAP/coap_lamp/coap_lamp.ino (95%) rename libraries/OpenThread/examples/{ => CLI}/COAP/coap_switch/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/COAP/coap_switch/coap_switch.ino (95%) rename libraries/OpenThread/examples/{ => CLI}/SimpleCLI/SimpleCLI.ino (89%) rename libraries/OpenThread/examples/{ => CLI}/SimpleCLI/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/SimpleNode/SimpleNode.ino (82%) rename libraries/OpenThread/examples/{ => CLI}/SimpleNode/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/SimpleThreadNetwork/ExtendedRouterNode/ExtendedRouterNode.ino (89%) rename libraries/OpenThread/examples/{ => CLI}/SimpleThreadNetwork/ExtendedRouterNode/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/SimpleThreadNetwork/LeaderNode/LeaderNode.ino (93%) rename libraries/OpenThread/examples/{ => CLI}/SimpleThreadNetwork/LeaderNode/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/SimpleThreadNetwork/RouterNode/RouterNode.ino (92%) rename libraries/OpenThread/examples/{ => CLI}/SimpleThreadNetwork/RouterNode/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/ThreadScan/ThreadScan.ino (89%) rename libraries/OpenThread/examples/{ => CLI}/ThreadScan/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/onReceive/ci.json (100%) rename libraries/OpenThread/examples/{ => CLI}/onReceive/onReceive.ino (96%) create mode 100644 libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/LeaderNode.ino create mode 100644 libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/ci.json create mode 100644 libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/RouterNode.ino create mode 100644 libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/ci.json create mode 100644 libraries/OpenThread/src/OThread.cpp create mode 100644 libraries/OpenThread/src/OThread.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d53d612962d..e8f44ac5ee0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -165,6 +165,7 @@ set(ARDUINO_LIBRARY_LittleFS_SRCS libraries/LittleFS/src/LittleFS.cpp) set(ARDUINO_LIBRARY_NetBIOS_SRCS libraries/NetBIOS/src/NetBIOS.cpp) set(ARDUINO_LIBRARY_OpenThread_SRCS + libraries/OpenThread/src/OThread.cpp libraries/OpenThread/src/OThreadCLI.cpp libraries/OpenThread/src/OThreadCLI_Util.cpp) diff --git a/libraries/OpenThread/README.md b/libraries/OpenThread/README.md index cd9deb9ebf6..8d0386f34fb 100644 --- a/libraries/OpenThread/README.md +++ b/libraries/OpenThread/README.md @@ -1,9 +1,177 @@ | Supported Targets | ESP32-C6 | ESP32-H2 | | ----------------- | -------- | -------- | -# ESP32 Arduino OpenThreadCLI +# General View -The `OpenThreadCLI` class is an Arduino API for interacting with the OpenThread Command Line Interface (CLI). It allows you to manage and configure the Thread stack using a command-line interface. +This Arduino OpenThread Library allows using ESP OpenThread implementation using CLI and/or Native OpenThread API. + +The Library implements 3 C++ Classes: +- `OThread` Class for Native OpenThread API +- `OThreadCLI` Class for CLI OpenThread API +- `DataSet` Class for OpenThread dataset manipulation using Native `OThread` Class + +# ESP32 Arduino OpenThread Native + +The `OThread` class provides methods for managing the OpenThread instance and controlling the Thread network. It allows you to initialize, start, stop, and manage the Thread network using native OpenThread APIs. + +## Class Definition + +```cpp +class OpenThread { + public: + static bool otStarted; // Indicates whether the OpenThread stack is running. + + // Get the current Thread device role (e.g., Leader, Router, Child, etc.). + static ot_device_role_t otGetDeviceRole(); + + // Get the current Thread device role as a string. + static const char *otGetStringDeviceRole(); + + // Print network information (e.g., network name, channel, PAN ID) to the specified stream. + static void otPrintNetworkInformation(Stream &output); + + OpenThread(); + ~OpenThread(); + + // Returns true if the OpenThread stack is running. + operator bool() const; + + // Initialize the OpenThread stack. + static void begin(bool OThreadAutoStart = true); + + // Deinitialize the OpenThread stack. + static void end(); + + // Start the Thread network. + void start(); + + // Stop the Thread network. + void stop(); + + // Bring up the Thread network interface (equivalent to "ifconfig up"). + void networkInterfaceUp(); + + // Bring down the Thread network interface (equivalent to "ifconfig down"). + void networkInterfaceDown(); + + // Commit a dataset to the OpenThread instance. + void commitDataSet(const DataSet &dataset); + +private: + static otInstance *mInstance; // Pointer to the OpenThread instance. + DataSet mCurrentDataSet; // Current dataset being used by the OpenThread instance. +}; + +extern OpenThread OThread; +``` +## Class Overview + +The `OThread` class provides a simple and intuitive interface for managing the OpenThread stack and Thread network. It abstracts the complexity of the OpenThread APIs and provides Arduino-style methods for common operations. + +## Public Methods +### Initialization and Deinitialization +- `begin(bool OThreadAutoStart = true)`: Initializes the OpenThread stack. If `OThreadAutoStart` is `true`, the Thread network will start automatically using NVS data. +- `end()`: Deinitializes the OpenThread stack and releases resources. +### Thread Network Control +- `start()`: Starts the Thread network. This is equivalent to the CLI command "thread start". +- `stop()`: Stops the Thread network. This is equivalent to the CLI command "thread stop". +### Network Interface Control +- `networkInterfaceUp()`: Brings up the Thread network interface. This is equivalent to the CLI command "ifconfig up". +- `networkInterfaceDown()`: Brings down the Thread network interface. This is equivalent to the CLI command "ifconfig down". +### Dataset Management +- `commitDataSet(const DataSet &dataset)`: Commits a dataset to the OpenThread instance. This is used to configure the Thread network with specific parameters (e.g., network name, channel, PAN ID). +### Network Information +- `otGetDeviceRole()`: Returns the current Thread device role as an `ot_device_role_t` enum (e.g., `OT_ROLE_LEADER`, `OT_ROLE_ROUTER`). +- `otGetStringDeviceRole()`: Returns the current Thread device role as a string (e.g., "Leader", "Router"). +- `otPrintNetworkInformation(Stream &output)`: Prints the current network information (e.g., network name, channel, PAN ID) to the specified stream. + +## Key Features +- **Initialization and Cleanup**: Easily initialize and deinitialize the OpenThread stack. +- **Network Control**: Start and stop the Thread network with simple method calls. +- **Dataset Management**: Configure the Thread network using the `DataSet` class and commit it to the OpenThread instance. +- **Network Information**: Retrieve and print the current network information and device role. + +## Notes +- The `OThread` class is designed to simplify the use of OpenThread APIs in Arduino sketches. +- It works seamlessly with the DataSet class for managing Thread network configurations. +- Ensure that the OpenThread stack is initialized (`OThread.begin()`) before calling other methods. + +This documentation provides a comprehensive overview of the `OThread` class, its methods, and example usage. It is designed to help developers quickly integrate OpenThread functionality into their Arduino projects. + +# DataSet Class + +The `DataSet` class provides a structured way to manage and configure Thread network datasets using native OpenThread APIs. It allows you to set and retrieve network parameters such as the network name, channel, PAN ID, and more. The `DataSet` class works seamlessly with the `OThread` class to apply these configurations to the OpenThread instance. + +## Class Definition + +```cpp +class DataSet { +public: + DataSet(); + void clear(); + void initNew(); + const otOperationalDataset &getDataset() const; + + // Setters + void setNetworkName(const char *name); + void setExtendedPanId(const uint8_t *extPanId); + void setNetworkKey(const uint8_t *key); + void setChannel(uint8_t channel); + void setPanId(uint16_t panId); + + // Getters + const char *getNetworkName() const; + const uint8_t *getExtendedPanId() const; + const uint8_t *getNetworkKey() const; + uint8_t getChannel() const; + uint16_t getPanId() const; + + // Apply the dataset to the OpenThread instance + void apply(otInstance *instance); + +private: + otOperationalDataset mDataset; // Internal representation of the dataset +}; +``` + +## Class Overview +The DataSet` class simplifies the management of Thread network datasets by providing intuitive methods for setting, retrieving, and applying network parameters. It abstracts the complexity of the OpenThread dataset APIs and provides Arduino-style methods for common operations. + +## Public Methods +### Initialization +- `DataSet()`: Constructor that initializes an empty dataset. +- `void clear()`: Clears the dataset, resetting all fields to their default values. +- `void initNew()`: Initializes a new dataset with default values (equivalent to the CLI command dataset init new). +### Setters +- `void setNetworkName(const char *name)`: Sets the network name. +- `void setExtendedPanId(const uint8_t *extPanId)`: Sets the extended PAN ID. +- `void setNetworkKey(const uint8_t *key)`: Sets the network key. +- `void setChannel(uint8_t channel)`: Sets the channel. +- `void setPanId(uint16_t panId)`: Sets the PAN ID. +### Getters +- `const char *getNetworkName() const`: Retrieves the network name. +- `const uint8_t *getExtendedPanId() const`: Retrieves the extended PAN ID. +- `const uint8_t *getNetworkKey() const`: Retrieves the network key. +- `uint8_t getChannel() const`: Retrieves the channel. +- `uint16_t getPanId() const`: Retrieves the PAN ID. +### Dataset Application +- `void apply(otInstance *instance)`: Applies the dataset to the specified OpenThread instance. + +## Key Features +- **Dataset Initialization**: Easily initialize a new dataset with default values using initNew(). +- **Custom Configuration**: Set custom network parameters such as the network name, channel, and PAN ID using setter methods. +- **Dataset Application**: Apply the configured dataset to the OpenThread instance using apply(). + +** Notes +- The `DataSet` class is designed to work seamlessly with the `OThread` class for managing Thread network configurations. +- Ensure that the OpenThread stack is initialized (`OThread.begin()`) before applying a dataset. +- The initNew()`` method provides default values for the dataset, which can be customized using the setter methods. + +This documentation provides a comprehensive overview of the `DataSet` class, its methods, and example usage. It is designed to help developers easily manage Thread network configurations in their Arduino projects. + +# OpenThreadCLI Class + +The `OpenThreadCLI` class is an Arduino API for interacting with the OpenThread Command Line Interface (CLI). It allows you to send commands to the OpenThread stack and receive responses. This class is designed to simplify the use of OpenThread CLI commands in Arduino sketches. There is one main class called `OpenThreadCLI` and a global object used to operate OpenThread CLI, called `OThreadCLI`.\ Some [helper functions](helper_functions.md) were made available for working with the OpenThread CLI environment. @@ -20,7 +188,7 @@ Below are the details of the class: class OpenThreadCLI : public Stream { private: static size_t setBuffer(QueueHandle_t &queue, size_t len); - bool otStarted = false; + static bool otCLIStarted = false; public: OpenThreadCLI(); @@ -59,14 +227,35 @@ extern OpenThreadCLI OThreadCLI; - You can customize the console behavior by adjusting parameters such as echoback and buffer sizes. ## Public Methods +### Initialization and Deinitialization +- `begin()`: Initializes the OpenThread stack (optional auto-start). +- `end()`: Deinitializes the OpenThread stack and releases resources. +### Console Management - `startConsole(Stream& otStream, bool echoback = true, const char* prompt = "ot> ")`: Starts the OpenThread console with the specified stream, echoback option, and prompt. - `stopConsole()`: Stops the OpenThread console. - `setPrompt(char* prompt)`: Changes the console prompt (set to NULL for an empty prompt). - `setEchoBack(bool echoback)`: Changes the console echoback option. - `setStream(Stream& otStream)`: Changes the console Stream object. - `onReceive(OnReceiveCb_t func)`: Sets a callback function to handle complete lines of output from the OT CLI. -- `begin(bool OThreadAutoStart = true)`: Initializes the OpenThread stack (optional auto-start). -- `end()`: Deinitializes the OpenThread stack. +### Buffer Management - `setTxBufferSize(size_t tx_queue_len)`: Sets the transmit buffer size (default is 256 bytes). - `setRxBufferSize(size_t rx_queue_len)`: Sets the receive buffer size (default is 1024 bytes). -- `write(uint8_t)`, `available()`, `read()`, `peek()`, `flush()`: Standard Stream methods implementation for OpenThread CLI object. +### Stream Methods +- `write(uint8_t)`: Writes a byte to the CLI. +- `available()`: Returns the number of bytes available to read. +- `read()`: Reads a byte from the CLI. +- `peek()`: Returns the next byte without removing it from the buffer. +- `flush()`: Flushes the CLI buffer. + +## Key Features +- **Arduino Stream Compatibility**: Inherits from the Stream class, making it compatible with Arduino's standard I/O functions. +- **Customizable Console**: Allows customization of the CLI prompt, echoback behavior, and buffer sizes. +- **Callback Support**: Provides a callback mechanism to handle CLI responses asynchronously. +- **Seamless Integration**: Designed to work seamlessly with the OThread and DataSet classes + +## Notes +- The `OThreadCLI` class is designed to simplify the use of OpenThread CLI commands in Arduino sketches. +- It works seamlessly with the `OThread` and `DataSet` classes for managing Thread networks. +- Ensure that the OpenThread stack is initialized (`OThreadCLI.begin()`) before starting the CLI console. + +This documentation provides a comprehensive overview of the `OThreadCLI` class, its methods, and example usage. It is designed to help developers easily integrate OpenThread CLI functionality into their Arduino projects. diff --git a/libraries/OpenThread/examples/COAP/coap_lamp/ci.json b/libraries/OpenThread/examples/CLI/COAP/coap_lamp/ci.json similarity index 100% rename from libraries/OpenThread/examples/COAP/coap_lamp/ci.json rename to libraries/OpenThread/examples/CLI/COAP/coap_lamp/ci.json diff --git a/libraries/OpenThread/examples/COAP/coap_lamp/coap_lamp.ino b/libraries/OpenThread/examples/CLI/COAP/coap_lamp/coap_lamp.ino similarity index 95% rename from libraries/OpenThread/examples/COAP/coap_lamp/coap_lamp.ino rename to libraries/OpenThread/examples/CLI/COAP/coap_lamp/coap_lamp.ino index 51483bb4c7c..bcaf8ae9793 100644 --- a/libraries/OpenThread/examples/COAP/coap_lamp/coap_lamp.ino +++ b/libraries/OpenThread/examples/CLI/COAP/coap_lamp/coap_lamp.ino @@ -75,18 +75,18 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role."); // wait for the expected Device Role to start uint8_t tries = 24; // 24 x 2.5 sec = 1 min - while (tries && otGetDeviceRole() != expectedRole) { + while (tries && OThread.otGetDeviceRole() != expectedRole) { Serial.print("."); delay(2500); tries--; } Serial.println(); if (!tries) { - log_e("Sorry, Device Role failed by timeout! Current Role: %s.", otGetStringDeviceRole()); + log_e("Sorry, Device Role failed by timeout! Current Role: %s.", OThread.otGetStringDeviceRole()); rgbLedWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! return false; } - Serial.printf("Device is %s.\r\n", otGetStringDeviceRole()); + Serial.printf("Device is %s.\r\n", OThread.otGetStringDeviceRole()); for (i = 0; i < nCmds2; i++) { if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) { break; @@ -151,7 +151,8 @@ void setup() { Serial.begin(115200); // LED starts RED, indicating not connected to Thread network. rgbLedWrite(RGB_BUILTIN, 64, 0, 0); - OThreadCLI.begin(false); // No AutoStart is necessary + OThread.begin(false); // No AutoStart is necessary + OThreadCLI.begin(); OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response setupNode(); // LED goes Green when all is ready and Red when failed. diff --git a/libraries/OpenThread/examples/COAP/coap_switch/ci.json b/libraries/OpenThread/examples/CLI/COAP/coap_switch/ci.json similarity index 100% rename from libraries/OpenThread/examples/COAP/coap_switch/ci.json rename to libraries/OpenThread/examples/CLI/COAP/coap_switch/ci.json diff --git a/libraries/OpenThread/examples/COAP/coap_switch/coap_switch.ino b/libraries/OpenThread/examples/CLI/COAP/coap_switch/coap_switch.ino similarity index 95% rename from libraries/OpenThread/examples/COAP/coap_switch/coap_switch.ino rename to libraries/OpenThread/examples/CLI/COAP/coap_switch/coap_switch.ino index aac5db0bc82..bacac47ddeb 100644 --- a/libraries/OpenThread/examples/COAP/coap_switch/coap_switch.ino +++ b/libraries/OpenThread/examples/CLI/COAP/coap_switch/coap_switch.ino @@ -69,18 +69,18 @@ bool otDeviceSetup( Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role."); // wait for the expected Device Role to start uint8_t tries = 24; // 24 x 2.5 sec = 1 min - while (tries && otGetDeviceRole() != expectedRole1 && otGetDeviceRole() != expectedRole2) { + while (tries && OThread.otGetDeviceRole() != expectedRole1 && OThread.otGetDeviceRole() != expectedRole2) { Serial.print("."); delay(2500); tries--; } Serial.println(); if (!tries) { - log_e("Sorry, Device Role failed by timeout! Current Role: %s.", otGetStringDeviceRole()); + log_e("Sorry, Device Role failed by timeout! Current Role: %s.", OThread.otGetStringDeviceRole()); rgbLedWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! return false; } - Serial.printf("Device is %s.\r\n", otGetStringDeviceRole()); + Serial.printf("Device is %s.\r\n", OThread.otGetStringDeviceRole()); for (i = 0; i < nCmds2; i++) { if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) { break; @@ -176,7 +176,8 @@ void setup() { Serial.begin(115200); // LED starts RED, indicating not connected to Thread network. rgbLedWrite(RGB_BUILTIN, 64, 0, 0); - OThreadCLI.begin(false); // No AutoStart is necessary + OThread.begin(false); // No AutoStart is necessary + OThreadCLI.begin(); OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response setupNode(); // LED goes and keeps Blue when all is ready and Red when failed. diff --git a/libraries/OpenThread/examples/SimpleCLI/SimpleCLI.ino b/libraries/OpenThread/examples/CLI/SimpleCLI/SimpleCLI.ino similarity index 89% rename from libraries/OpenThread/examples/SimpleCLI/SimpleCLI.ino rename to libraries/OpenThread/examples/CLI/SimpleCLI/SimpleCLI.ino index feef800c0fa..dec3aaeb218 100644 --- a/libraries/OpenThread/examples/SimpleCLI/SimpleCLI.ino +++ b/libraries/OpenThread/examples/CLI/SimpleCLI/SimpleCLI.ino @@ -1,4 +1,4 @@ -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,7 +26,8 @@ void setup() { Serial.begin(115200); - OThreadCLI.begin(false); // No AutoStart - fresh start + OThread.begin(false); // No AutoStart - fresh start + OThreadCLI.begin(); Serial.println("OpenThread CLI started - type 'help' for a list of commands."); OThreadCLI.startConsole(Serial); } diff --git a/libraries/OpenThread/examples/SimpleCLI/ci.json b/libraries/OpenThread/examples/CLI/SimpleCLI/ci.json similarity index 100% rename from libraries/OpenThread/examples/SimpleCLI/ci.json rename to libraries/OpenThread/examples/CLI/SimpleCLI/ci.json diff --git a/libraries/OpenThread/examples/SimpleNode/SimpleNode.ino b/libraries/OpenThread/examples/CLI/SimpleNode/SimpleNode.ino similarity index 82% rename from libraries/OpenThread/examples/SimpleNode/SimpleNode.ino rename to libraries/OpenThread/examples/CLI/SimpleNode/SimpleNode.ino index 95bf7a2401a..d17b692cc74 100644 --- a/libraries/OpenThread/examples/SimpleNode/SimpleNode.ino +++ b/libraries/OpenThread/examples/CLI/SimpleNode/SimpleNode.ino @@ -1,4 +1,4 @@ -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -35,12 +35,13 @@ void setup() { Serial.begin(115200); - OThreadCLI.begin(); // AutoStart using Thread default settings - otPrintNetworkInformation(Serial); // Print Current Thread Network Information + OThread.begin(); // AutoStart using Thread default settings + OThreadCLI.begin(); + OThread.otPrintNetworkInformation(Serial); // Print Current Thread Network Information } void loop() { Serial.print("Thread Node State: "); - Serial.println(otGetStringDeviceRole()); + Serial.println(OThread.otGetStringDeviceRole()); delay(5000); } diff --git a/libraries/OpenThread/examples/SimpleNode/ci.json b/libraries/OpenThread/examples/CLI/SimpleNode/ci.json similarity index 100% rename from libraries/OpenThread/examples/SimpleNode/ci.json rename to libraries/OpenThread/examples/CLI/SimpleNode/ci.json diff --git a/libraries/OpenThread/examples/SimpleThreadNetwork/ExtendedRouterNode/ExtendedRouterNode.ino b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/ExtendedRouterNode/ExtendedRouterNode.ino similarity index 89% rename from libraries/OpenThread/examples/SimpleThreadNetwork/ExtendedRouterNode/ExtendedRouterNode.ino rename to libraries/OpenThread/examples/CLI/SimpleThreadNetwork/ExtendedRouterNode/ExtendedRouterNode.ino index 4fc8a921584..40f046aeab5 100644 --- a/libraries/OpenThread/examples/SimpleThreadNetwork/ExtendedRouterNode/ExtendedRouterNode.ino +++ b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/ExtendedRouterNode/ExtendedRouterNode.ino @@ -1,4 +1,4 @@ -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -22,7 +22,8 @@ bool otStatus = true; void setup() { Serial.begin(115200); - OThreadCLI.begin(false); // No AutoStart - fresh start + OThread.begin(false); // No AutoStart - fresh start + OThreadCLI.begin(); Serial.println("Setting up OpenThread Node as Router/Child"); Serial.println("Make sure the Leader Node is already running"); @@ -39,7 +40,7 @@ void setup() { } // wait for the node to enter in the router state uint32_t timeout = millis() + 90000; // waits 90 seconds to - while (otGetDeviceRole() != OT_ROLE_CHILD && otGetDeviceRole() != OT_ROLE_ROUTER) { + while (OThread.otGetDeviceRole() != OT_ROLE_CHILD && OThread.otGetDeviceRole() != OT_ROLE_ROUTER) { Serial.print("."); if (millis() > timeout) { Serial.println("\r\n\t===> Timeout! Failed."); @@ -70,7 +71,7 @@ void loop() { if (otStatus) { Serial.println("Thread NetworkInformation: "); Serial.println("---------------------------"); - otPrintNetworkInformation(Serial); + OThread.otPrintNetworkInformation(Serial); Serial.println("---------------------------"); } else { Serial.println("Some OpenThread operation has failed..."); diff --git a/libraries/OpenThread/examples/SimpleThreadNetwork/ExtendedRouterNode/ci.json b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/ExtendedRouterNode/ci.json similarity index 100% rename from libraries/OpenThread/examples/SimpleThreadNetwork/ExtendedRouterNode/ci.json rename to libraries/OpenThread/examples/CLI/SimpleThreadNetwork/ExtendedRouterNode/ci.json diff --git a/libraries/OpenThread/examples/SimpleThreadNetwork/LeaderNode/LeaderNode.ino b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/LeaderNode/LeaderNode.ino similarity index 93% rename from libraries/OpenThread/examples/SimpleThreadNetwork/LeaderNode/LeaderNode.ino rename to libraries/OpenThread/examples/CLI/SimpleThreadNetwork/LeaderNode/LeaderNode.ino index 7b709717692..a945a5c8f77 100644 --- a/libraries/OpenThread/examples/SimpleThreadNetwork/LeaderNode/LeaderNode.ino +++ b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/LeaderNode/LeaderNode.ino @@ -1,4 +1,4 @@ -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -35,7 +35,8 @@ otInstance *aInstance = NULL; void setup() { Serial.begin(115200); - OThreadCLI.begin(false); // No AutoStart - fresh start + OThread.begin(false); // No AutoStart - fresh start + OThreadCLI.begin(); Serial.println(); Serial.println("Setting up OpenThread Node as Leader"); aInstance = esp_openthread_get_instance(); @@ -51,11 +52,11 @@ void setup() { void loop() { Serial.println("============================================="); Serial.print("Thread Node State: "); - Serial.println(otGetStringDeviceRole()); + Serial.println(OThread.otGetStringDeviceRole()); // Native OpenThread API calls: // wait until the node become Child or Router - if (otGetDeviceRole() == OT_ROLE_LEADER) { + if (OThread.otGetDeviceRole() == OT_ROLE_LEADER) { // Network Name const char *networkName = otThreadGetNetworkName(aInstance); Serial.printf("Network Name: %s\r\n", networkName); diff --git a/libraries/OpenThread/examples/SimpleThreadNetwork/LeaderNode/ci.json b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/LeaderNode/ci.json similarity index 100% rename from libraries/OpenThread/examples/SimpleThreadNetwork/LeaderNode/ci.json rename to libraries/OpenThread/examples/CLI/SimpleThreadNetwork/LeaderNode/ci.json diff --git a/libraries/OpenThread/examples/SimpleThreadNetwork/RouterNode/RouterNode.ino b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/RouterNode/RouterNode.ino similarity index 92% rename from libraries/OpenThread/examples/SimpleThreadNetwork/RouterNode/RouterNode.ino rename to libraries/OpenThread/examples/CLI/SimpleThreadNetwork/RouterNode/RouterNode.ino index 45475fa0c6a..f802bd7ef01 100644 --- a/libraries/OpenThread/examples/SimpleThreadNetwork/RouterNode/RouterNode.ino +++ b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/RouterNode/RouterNode.ino @@ -1,4 +1,4 @@ -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -34,7 +34,8 @@ otInstance *aInstance = NULL; void setup() { Serial.begin(115200); - OThreadCLI.begin(false); // No AutoStart - fresh start + OThread.begin(false); // No AutoStart - fresh start + OThreadCLI.begin(); Serial.println(); Serial.println("Setting up OpenThread Node as Router/Child"); Serial.println("Make sure the Leader Node is already running"); @@ -51,11 +52,11 @@ void setup() { void loop() { Serial.println("============================================="); Serial.print("Thread Node State: "); - Serial.println(otGetStringDeviceRole()); + Serial.println(OThread.otGetStringDeviceRole()); // Native OpenThread API calls: // wait until the node become Child or Router - if (otGetDeviceRole() == OT_ROLE_CHILD || otGetDeviceRole() == OT_ROLE_ROUTER) { + if (OThread.otGetDeviceRole() == OT_ROLE_CHILD || OThread.otGetDeviceRole() == OT_ROLE_ROUTER) { // Network Name const char *networkName = otThreadGetNetworkName(aInstance); Serial.printf("Network Name: %s\r\n", networkName); diff --git a/libraries/OpenThread/examples/SimpleThreadNetwork/RouterNode/ci.json b/libraries/OpenThread/examples/CLI/SimpleThreadNetwork/RouterNode/ci.json similarity index 100% rename from libraries/OpenThread/examples/SimpleThreadNetwork/RouterNode/ci.json rename to libraries/OpenThread/examples/CLI/SimpleThreadNetwork/RouterNode/ci.json diff --git a/libraries/OpenThread/examples/ThreadScan/ThreadScan.ino b/libraries/OpenThread/examples/CLI/ThreadScan/ThreadScan.ino similarity index 89% rename from libraries/OpenThread/examples/ThreadScan/ThreadScan.ino rename to libraries/OpenThread/examples/CLI/ThreadScan/ThreadScan.ino index 9d0074bb180..87c29339fb7 100644 --- a/libraries/OpenThread/examples/ThreadScan/ThreadScan.ino +++ b/libraries/OpenThread/examples/CLI/ThreadScan/ThreadScan.ino @@ -1,4 +1,4 @@ -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,7 +27,8 @@ void setup() { Serial.begin(115200); - OThreadCLI.begin(true); // For scanning, AutoStart must be active, any setup + OThread.begin(true); // For scanning, AutoStart must be active, any setup + OThreadCLI.begin(); OThreadCLI.setTimeout(100); // Set a timeout for the CLI response Serial.println(); Serial.println("This sketch will continuously scan the Thread Local Network and all devices IEEE 802.15.4 compatible"); @@ -41,7 +42,7 @@ void loop() { Serial.println("Scan Failed..."); } delay(5000); - if (otGetDeviceRole() < OT_ROLE_CHILD) { + if (OThread.otGetDeviceRole() < OT_ROLE_CHILD) { Serial.println(); Serial.println("This device has not started Thread yet, bypassing Discovery Scan"); return; diff --git a/libraries/OpenThread/examples/ThreadScan/ci.json b/libraries/OpenThread/examples/CLI/ThreadScan/ci.json similarity index 100% rename from libraries/OpenThread/examples/ThreadScan/ci.json rename to libraries/OpenThread/examples/CLI/ThreadScan/ci.json diff --git a/libraries/OpenThread/examples/onReceive/ci.json b/libraries/OpenThread/examples/CLI/onReceive/ci.json similarity index 100% rename from libraries/OpenThread/examples/onReceive/ci.json rename to libraries/OpenThread/examples/CLI/onReceive/ci.json diff --git a/libraries/OpenThread/examples/onReceive/onReceive.ino b/libraries/OpenThread/examples/CLI/onReceive/onReceive.ino similarity index 96% rename from libraries/OpenThread/examples/onReceive/onReceive.ino rename to libraries/OpenThread/examples/CLI/onReceive/onReceive.ino index b37c2fc7931..f53cc33f5ec 100644 --- a/libraries/OpenThread/examples/onReceive/onReceive.ino +++ b/libraries/OpenThread/examples/CLI/onReceive/onReceive.ino @@ -39,7 +39,8 @@ void otReceivedLine() { void setup() { Serial.begin(115200); - OThreadCLI.begin(); // AutoStart + OThread.begin(); // AutoStart + OThreadCLI.begin(); OThreadCLI.onReceive(otReceivedLine); } diff --git a/libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/LeaderNode.ino b/libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/LeaderNode.ino new file mode 100644 index 00000000000..dfea9776838 --- /dev/null +++ b/libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/LeaderNode.ino @@ -0,0 +1,34 @@ +#include "OThread.h" + +OpenThread threadLeaderNode; +DataSet dataset; + +void setup() { + Serial.begin(115200); + + // Start OpenThread Stack - false for not using NVS dataset information + threadLeaderNode.begin(false); + + // Create a new Thread Network Dataset for a Leader Node + dataset.initNew(); + // Configure the dataset + dataset.setNetworkName("ESP_OpenThread"); + uint8_t extPanId[OT_EXT_PAN_ID_SIZE] = {0xDE, 0xAD, 0x00, 0xBE, 0xEF, 0x00, 0xCA, 0xFE}; + dataset.setExtendedPanId(extPanId); + uint8_t networkKey[OT_NETWORK_KEY_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; + dataset.setNetworkKey(networkKey); + dataset.setChannel(15); + dataset.setPanId(0x1234); + + // Apply the dataset and start the network + threadLeaderNode.commitDataSet(dataset); + threadLeaderNode.networkInterfaceUp(); + threadLeaderNode.start(); +} + +void loop() { + // Print network information every 5 seconds + Serial.println("=============================================="); + threadLeaderNode.otPrintNetworkInformation(Serial); + delay(5000); +} diff --git a/libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/ci.json b/libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/ci.json new file mode 100644 index 00000000000..2ee6af3490e --- /dev/null +++ b/libraries/OpenThread/examples/Native/SimpleThreadNetwork/LeaderNode/ci.json @@ -0,0 +1,6 @@ +{ + "requires": [ + "CONFIG_OPENTHREAD_ENABLED=y", + "CONFIG_SOC_IEEE802154_SUPPORTED=y" + ] +} diff --git a/libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/RouterNode.ino b/libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/RouterNode.ino new file mode 100644 index 00000000000..5ffa535ad51 --- /dev/null +++ b/libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/RouterNode.ino @@ -0,0 +1,29 @@ +#include "OThread.h" + +OpenThread threadChildNode; +DataSet dataset; + +void setup() { + Serial.begin(115200); + + // Start OpenThread Stack - false for not using NVS dataset information + threadChildNode.begin(false); + + // clear dataset + dataset.clear(); + // Configure the dataset with the same Network Key of the Leader Node + uint8_t networkKey[OT_NETWORK_KEY_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; + dataset.setNetworkKey(networkKey); + + // Apply the dataset and start the network + threadChildNode.commitDataSet(dataset); + threadChildNode.networkInterfaceUp(); + threadChildNode.start(); +} + +void loop() { + // Print network information every 5 seconds + Serial.println("=============================================="); + threadChildNode.otPrintNetworkInformation(Serial); + delay(5000); +} diff --git a/libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/ci.json b/libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/ci.json new file mode 100644 index 00000000000..2ee6af3490e --- /dev/null +++ b/libraries/OpenThread/examples/Native/SimpleThreadNetwork/RouterNode/ci.json @@ -0,0 +1,6 @@ +{ + "requires": [ + "CONFIG_OPENTHREAD_ENABLED=y", + "CONFIG_SOC_IEEE802154_SUPPORTED=y" + ] +} diff --git a/libraries/OpenThread/keywords.txt b/libraries/OpenThread/keywords.txt index d7193de188d..b62c2c23ddc 100644 --- a/libraries/OpenThread/keywords.txt +++ b/libraries/OpenThread/keywords.txt @@ -7,7 +7,10 @@ ####################################### OThreadCLI KEYWORD1 +OThread KEYWORD1 OpenThreadCLI KEYWORD1 +OpenThread KEYWORD1 +DataSet KEYWORD1 ot_cmd_return_t KEYWORD1 ot_device_role_t KEYWORD1 @@ -35,6 +38,27 @@ otGetRespCmd KEYWORD2 otExecCommand KEYWORD2 otPrintRespCLI KEYWORD2 otPrintNetworkInformation KEYWORD2 +clear KEYWORD2 +initNew KEYWORD2 +getDataset KEYWORD2 +setNetworkName KEYWORD2 +getNetworkName KEYWORD2 +setExtendedPanId KEYWORD2 +getExtendedPanId KEYWORD2 +setNetworkKey KEYWORD2 +getNetworkKey KEYWORD2 +setChannel KEYWORD2 +getChannel KEYWORD2 +setPanId KEYWORD2 +getPanId KEYWORD2 +apply KEYWORD2 +otStarted KEYWORD2 +otCLIStarted KEYWORD2 +start KEYWORD2 +stop KEYWORD2 +networkInterfaceUp KEYWORD2 +networkInterfaceDown KEYWORD2 +commitDataSet KEYWORD2 ####################################### # Constants (LITERAL1) @@ -45,3 +69,5 @@ OT_ROLE_DETACHED LITERAL1 OT_ROLE_CHILD LITERAL1 OT_ROLE_ROUTER LITERAL1 OT_ROLE_LEADER LITERAL1 +OT_EXT_PAN_ID_SIZE LITERAL1 +OT_NETWORK_KEY_SIZE LITERAL1 diff --git a/libraries/OpenThread/src/OThread.cpp b/libraries/OpenThread/src/OThread.cpp new file mode 100644 index 00000000000..43d8ce02918 --- /dev/null +++ b/libraries/OpenThread/src/OThread.cpp @@ -0,0 +1,364 @@ +#include "OThread.h" +#if SOC_IEEE802154_SUPPORTED +#if CONFIG_OPENTHREAD_ENABLED + +#include "esp_err.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_netif_types.h" +#include "esp_vfs_eventfd.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" + +#include "esp_netif_net_stack.h" +#include "esp_openthread_netif_glue.h" +#include "lwip/netif.h" + +static esp_openthread_platform_config_t ot_native_config; +static esp_netif_t *openthread_netif = NULL; + +const char *otRoleString[] = { + "Disabled", ///< The Thread stack is disabled. + "Detached", ///< Not currently participating in a Thread network/partition. + "Child", ///< The Thread Child role. + "Router", ///< The Thread Router role. + "Leader", ///< The Thread Leader role. + "Unknown", ///< Unknown role, not initialized or not started. +}; + +static TaskHandle_t s_ot_task = NULL; +#if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM +static struct netif *ot_lwip_netif = NULL; +#endif + +#if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM +extern "C" int lwip_hook_ip6_input(struct pbuf *p, struct netif *inp) { + if (ot_lwip_netif && ot_lwip_netif == inp) { + return 0; + } + if (ip6_addr_isany_val(inp->ip6_addr[0].u_addr.ip6)) { + // We don't have an LL address -> eat this packet here, so it won't get accepted on input netif + pbuf_free(p); + return 1; + } + return 0; +} +#endif + +static void ot_task_worker(void *aContext) { + esp_vfs_eventfd_config_t eventfd_config = { + .max_fds = 3, + }; + bool err = false; + if (ESP_OK != esp_event_loop_create_default()) { + log_e("Failed to create OpentThread event loop"); + err = true; + } + if (!err && ESP_OK != esp_netif_init()) { + log_e("Failed to initialize OpentThread netif"); + err = true; + } + if (!err && ESP_OK != esp_vfs_eventfd_register(&eventfd_config)) { + log_e("Failed to register OpentThread eventfd"); + err = true; + } + + // Initialize the OpenThread stack + if (!err && ESP_OK != esp_openthread_init(&ot_native_config)) { + log_e("Failed to initialize OpenThread stack"); + err = true; + } + if (!err) { + // Initialize the esp_netif bindings + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD(); + openthread_netif = esp_netif_new(&cfg); +#if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM + // Get LwIP Netif + if (openthread_netif != NULL) { + ot_lwip_netif = (struct netif *)esp_netif_get_netif_impl(openthread_netif); + if (ot_lwip_netif == NULL) { + log_e("Failed to get OpenThread LwIP netif"); + } + } +#endif + } + if (!err && openthread_netif == NULL) { + log_e("Failed to create OpenThread esp_netif"); + err = true; + } + if (!err && ESP_OK != esp_netif_attach(openthread_netif, esp_openthread_netif_glue_init(&ot_native_config))) { + log_e("Failed to attach OpenThread esp_netif"); + err = true; + } + if (!err && ESP_OK != esp_netif_set_default_netif(openthread_netif)) { + log_e("Failed to set default OpenThread esp_netif"); + err = true; + } + if (!err) { + // only returns in case there is an OpenThread Stack failure... + esp_openthread_launch_mainloop(); + } + // Clean up + esp_openthread_netif_glue_deinit(); + esp_netif_destroy(openthread_netif); + esp_vfs_eventfd_unregister(); + vTaskDelete(NULL); +} + +// DataSet Implementation +DataSet::DataSet() { + memset(&mDataset, 0, sizeof(mDataset)); +} + +void DataSet::clear() { + memset(&mDataset, 0, sizeof(mDataset)); +} + +void DataSet::initNew() { + otInstance *mInstance = esp_openthread_get_instance(); + if (!mInstance) { + log_e("OpenThread not started. Please begin() it before initializing a new dataset."); + return; + } + clear(); + otDatasetCreateNewNetwork(mInstance, &mDataset); +} + +const otOperationalDataset &DataSet::getDataset() const { + return mDataset; +} + +void DataSet::setNetworkName(const char *name) { + strncpy(mDataset.mNetworkName.m8, name, sizeof(mDataset.mNetworkName.m8)); + mDataset.mComponents.mIsNetworkNamePresent = true; +} + +void DataSet::setExtendedPanId(const uint8_t *extPanId) { + memcpy(mDataset.mExtendedPanId.m8, extPanId, OT_EXT_PAN_ID_SIZE); + mDataset.mComponents.mIsExtendedPanIdPresent = true; +} + +void DataSet::setNetworkKey(const uint8_t *key) { + memcpy(mDataset.mNetworkKey.m8, key, OT_NETWORK_KEY_SIZE); + mDataset.mComponents.mIsNetworkKeyPresent = true; +} + +void DataSet::setChannel(uint8_t channel) { + mDataset.mChannel = channel; + mDataset.mComponents.mIsChannelPresent = true; +} + +void DataSet::setPanId(uint16_t panId) { + mDataset.mPanId = panId; + mDataset.mComponents.mIsPanIdPresent = true; +} + +const char *DataSet::getNetworkName() const { + return mDataset.mNetworkName.m8; +} + +const uint8_t *DataSet::getExtendedPanId() const { + return mDataset.mExtendedPanId.m8; +} + +const uint8_t *DataSet::getNetworkKey() const { + return mDataset.mNetworkKey.m8; +} + +uint8_t DataSet::getChannel() const { + return mDataset.mChannel; +} + +uint16_t DataSet::getPanId() const { + return mDataset.mPanId; +} + +void DataSet::apply(otInstance *instance) { + otDatasetSetActive(instance, &mDataset); +} + +// OpenThread Implementation +bool OpenThread::otStarted = false; + +otInstance *OpenThread::mInstance = nullptr; +OpenThread::OpenThread() {} + +OpenThread::~OpenThread() { + end(); +} + +OpenThread::operator bool() const { + return otStarted; +} + +void OpenThread::begin(bool OThreadAutoStart) { + if (otStarted) { + log_w("OpenThread already started"); + return; + } + + memset(&ot_native_config, 0, sizeof(esp_openthread_platform_config_t)); + ot_native_config.radio_config.radio_mode = RADIO_MODE_NATIVE; + ot_native_config.host_config.host_connection_mode = HOST_CONNECTION_MODE_NONE; + ot_native_config.port_config.storage_partition_name = "nvs"; + ot_native_config.port_config.netif_queue_size = 10; + ot_native_config.port_config.task_queue_size = 10; + + // Initialize OpenThread stack + xTaskCreate(ot_task_worker, "ot_main_loop", 10240, NULL, 20, &s_ot_task); + if (s_ot_task == NULL) { + log_e("Error: Failed to create OpenThread task"); + return; + } + log_d("OpenThread task created successfully"); + // get the OpenThread instance that will be used for all operations + mInstance = esp_openthread_get_instance(); + if (!mInstance) { + log_e("Error: Failed to initialize OpenThread instance"); + end(); + return; + } + // starts Thread with default dataset from NVS or from IDF default settings + if (OThreadAutoStart) { + otOperationalDatasetTlvs dataset; + otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset); + // error = OT_ERROR_FAILED; // teste para forçar NULL dataset + if (error != OT_ERROR_NONE) { + log_i("Failed to get active NVS dataset from OpenThread"); + } else { + log_i("Got active NVS dataset from OpenThread"); + } + esp_err_t err = esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL); + if (err != ESP_OK) { + log_i("Failed to AUTO start OpenThread"); + } else { + log_i("AUTO start OpenThread done"); + } + } + otStarted = true; +} + +void OpenThread::end() { + if (s_ot_task != NULL) { + vTaskDelete(s_ot_task); + s_ot_task = NULL; + // Clean up + esp_openthread_deinit(); + esp_openthread_netif_glue_deinit(); +#if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM + ot_lwip_netif = NULL; +#endif + esp_netif_destroy(openthread_netif); + esp_vfs_eventfd_unregister(); + } + otStarted = false; +} + +void OpenThread::start() { + if (!mInstance) { + log_w("Error: OpenThread instance not initialized"); + return; + } + otThreadSetEnabled(mInstance, true); + log_d("Thread network started"); +} + +void OpenThread::stop() { + if (!mInstance) { + log_w("Error: OpenThread instance not initialized"); + return; + } + otThreadSetEnabled(mInstance, false); + log_d("Thread network stopped"); +} + +void OpenThread::networkInterfaceUp() { + if (!mInstance) { + log_w("Error: OpenThread instance not initialized"); + return; + } + // Enable the Thread interface (equivalent to CLI Command "ifconfig up") + otError error = otIp6SetEnabled(mInstance, true); + if (error != OT_ERROR_NONE) { + log_e("Error: Failed to enable Thread interface (error code: %d)\n", error); + } + log_d("OpenThread Network Interface is up"); +} + +void OpenThread::networkInterfaceDown() { + if (!mInstance) { + log_w("Error: OpenThread instance not initialized"); + return; + } + // Disable the Thread interface (equivalent to CLI Command "ifconfig down") + otError error = otIp6SetEnabled(mInstance, false); + if (error != OT_ERROR_NONE) { + log_e("Error: Failed to disable Thread interface (error code: %d)\n", error); + } + log_d("OpenThread Network Interface is down"); +} + +void OpenThread::commitDataSet(const DataSet &dataset) { + if (!mInstance) { + log_w("Error: OpenThread instance not initialized"); + return; + } + // Commit the dataset as the active dataset + otError error = otDatasetSetActive(mInstance, &(dataset.getDataset())); + if (error != OT_ERROR_NONE) { + log_e("Error: Failed to commit dataset (error code: %d)\n", error); + return; + } + log_d("Dataset committed successfully"); +} + +ot_device_role_t OpenThread::otGetDeviceRole() { + if (!mInstance) { + log_w("Error: OpenThread instance not initialized"); + return OT_ROLE_DISABLED; + } + return (ot_device_role_t)otThreadGetDeviceRole(mInstance); +} + +const char *OpenThread::otGetStringDeviceRole() { + return otRoleString[otGetDeviceRole()]; +} + +void OpenThread::otPrintNetworkInformation(Stream &output) { + if (!mInstance) { + log_w("Error: OpenThread instance not initialized"); + return; + } + + output.printf("Role: %s", otGetStringDeviceRole()); + output.println(); + output.printf("Network Name: %s", otThreadGetNetworkName(mInstance)); + output.println(); + output.printf("Channel: %d", otLinkGetChannel(mInstance)); + output.println(); + output.printf("PAN ID: 0x%04x", otLinkGetPanId(mInstance)); + output.println(); + + const otExtendedPanId *extPanId = otThreadGetExtendedPanId(mInstance); + output.print("Extended PAN ID: "); + for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) { + output.printf("%02x", extPanId->m8[i]); + } + output.println(); + + otNetworkKey networkKey; + otThreadGetNetworkKey(mInstance, &networkKey); + output.print("Network Key: "); + for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) { + output.printf("%02x", networkKey.m8[i]); + } + output.println(); +} + +OpenThread OThread; + +#endif /* CONFIG_OPENTHREAD_ENABLED */ +#endif /* SOC_IEEE802154_SUPPORTED */ diff --git a/libraries/OpenThread/src/OThread.h b/libraries/OpenThread/src/OThread.h new file mode 100644 index 00000000000..359d581bb9d --- /dev/null +++ b/libraries/OpenThread/src/OThread.h @@ -0,0 +1,107 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once +#include "soc/soc_caps.h" +#include "sdkconfig.h" +#if SOC_IEEE802154_SUPPORTED +#if CONFIG_OPENTHREAD_ENABLED + +#include +#include +#include +#include +#include +#include +#include + +typedef enum { + OT_ROLE_DISABLED = 0, ///< The Thread stack is disabled. + OT_ROLE_DETACHED = 1, ///< Not currently participating in a Thread network/partition. + OT_ROLE_CHILD = 2, ///< The Thread Child role. + OT_ROLE_ROUTER = 3, ///< The Thread Router role. + OT_ROLE_LEADER = 4, ///< The Thread Leader role. +} ot_device_role_t; +extern const char *otRoleString[]; + +class DataSet { +public: + DataSet(); + void clear(); + void initNew(); + const otOperationalDataset &getDataset() const; + + // Setters + void setNetworkName(const char *name); + void setExtendedPanId(const uint8_t *extPanId); + void setNetworkKey(const uint8_t *key); + void setChannel(uint8_t channel); + void setPanId(uint16_t panId); + + // Getters + const char *getNetworkName() const; + const uint8_t *getExtendedPanId() const; + const uint8_t *getNetworkKey() const; + uint8_t getChannel() const; + uint16_t getPanId() const; + + // Apply the dataset to the OpenThread instance + void apply(otInstance *instance); + +private: + otOperationalDataset mDataset; +}; + +class OpenThread { +public: + static bool otStarted; + static ot_device_role_t otGetDeviceRole(); + static const char *otGetStringDeviceRole(); + static void otPrintNetworkInformation(Stream &output); + + OpenThread(); + ~OpenThread(); + // returns true if OpenThread Stack is running + operator bool() const; + + // Initialize OpenThread + static void begin(bool OThreadAutoStart = true); + + // Initialize OpenThread + static void end(); + + // Start the Thread network + void start(); + + // Stop the Thread network + void stop(); + + // Start Thread Network Interface + void networkInterfaceUp(); + + // Stop Thread Network Interface + void networkInterfaceDown(); + + // Set the dataset + void commitDataSet(const DataSet &dataset); + +private: + static otInstance *mInstance; + DataSet mCurrentDataSet; +}; + +extern OpenThread OThread; + +#endif /* CONFIG_OPENTHREAD_ENABLED */ +#endif /* SOC_IEEE802154_SUPPORTED */ diff --git a/libraries/OpenThread/src/OThreadCLI.cpp b/libraries/OpenThread/src/OThreadCLI.cpp index 85ba03563e8..2f0f97a0539 100644 --- a/libraries/OpenThread/src/OThreadCLI.cpp +++ b/libraries/OpenThread/src/OThreadCLI.cpp @@ -33,18 +33,12 @@ #include "esp_netif_net_stack.h" #include "lwip/netif.h" +bool OpenThreadCLI::otCLIStarted = false; static TaskHandle_t s_cli_task = NULL; static TaskHandle_t s_console_cli_task = NULL; static QueueHandle_t rx_queue = NULL; static QueueHandle_t tx_queue = NULL; -static esp_openthread_platform_config_t ot_native_config; -static TaskHandle_t s_ot_task = NULL; -static esp_netif_t *openthread_netif = NULL; -#if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM -static struct netif *ot_lwip_netif = NULL; -#endif - #define OT_CLI_MAX_LINE_LENGTH 512 typedef struct { @@ -53,21 +47,7 @@ typedef struct { String prompt; OnReceiveCb_t responseCallBack; } ot_cli_console_t; -static ot_cli_console_t otConsole = {NULL, false, (const char *)NULL, NULL}; - -#if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM -extern "C" int lwip_hook_ip6_input(struct pbuf *p, struct netif *inp) { - if (ot_lwip_netif && ot_lwip_netif == inp) { - return 0; - } - if (ip6_addr_isany_val(inp->ip6_addr[0].u_addr.ip6)) { - // We don't have an LL address -> eat this packet here, so it won't get accepted on input netif - pbuf_free(p); - return 1; - } - return 0; -} -#endif +static ot_cli_console_t otConsole = {nullptr, false, (const char *)nullptr, nullptr}; // process the CLI commands sent to the OpenThread stack static void ot_cli_loop(void *context) { @@ -131,7 +111,7 @@ static int ot_cli_output_callback(void *context, const char *format, va_list arg } // if there is a user callback function in place, it shall have the priority // to process/consume the Stream data received from OpenThread CLI, which is available in its RX Buffer - if (otConsole.responseCallBack != NULL) { + if (otConsole.responseCallBack != nullptr) { otConsole.responseCallBack(); } } @@ -205,7 +185,7 @@ void OpenThreadCLI::setEchoBack(bool echoback) { } void OpenThreadCLI::setPrompt(char *prompt) { - otConsole.prompt = prompt; // NULL will make the prompt not visible + otConsole.prompt = prompt; // nullptr can make the prompt not visible } void OpenThreadCLI::setStream(Stream &otStream) { @@ -213,12 +193,12 @@ void OpenThreadCLI::setStream(Stream &otStream) { } void OpenThreadCLI::onReceive(OnReceiveCb_t func) { - otConsole.responseCallBack = func; // NULL will set it off + otConsole.responseCallBack = func; // nullptr will set it off } // Stream object shall be already started and configured before calling this function void OpenThreadCLI::startConsole(Stream &otStream, bool echoback, const char *prompt) { - if (!otStarted) { + if (!otCLIStarted) { log_e("OpenThread CLI has not started. Please begin() it before starting the console."); return; } @@ -226,7 +206,7 @@ void OpenThreadCLI::startConsole(Stream &otStream, bool echoback, const char *pr if (s_console_cli_task == NULL) { otConsole.cliStream = &otStream; otConsole.echoback = echoback; - otConsole.prompt = prompt; // NULL will invalidate the String + otConsole.prompt = prompt; // nullptr will invalidate the String // it will run in the same priority (1) as the Arduino setup()/loop() task xTaskCreate(ot_cli_console_worker, "ot_cli_console", 4096, &otConsole, 1, &s_console_cli_task); } else { @@ -242,12 +222,6 @@ void OpenThreadCLI::stopConsole() { } OpenThreadCLI::OpenThreadCLI() { - memset(&ot_native_config, 0, sizeof(esp_openthread_platform_config_t)); - ot_native_config.radio_config.radio_mode = RADIO_MODE_NATIVE; - ot_native_config.host_config.host_connection_mode = HOST_CONNECTION_MODE_NONE; - ot_native_config.port_config.storage_partition_name = "nvs"; - ot_native_config.port_config.netif_queue_size = 10; - ot_native_config.port_config.task_queue_size = 10; //sTxString = ""; } @@ -256,79 +230,19 @@ OpenThreadCLI::~OpenThreadCLI() { } OpenThreadCLI::operator bool() const { - return otStarted; + return otCLIStarted; } -static void ot_task_worker(void *aContext) { - esp_vfs_eventfd_config_t eventfd_config = { - .max_fds = 3, - }; - bool err = false; - if (ESP_OK != esp_event_loop_create_default()) { - log_e("Failed to create OpentThread event loop"); - err = true; - } - if (!err && ESP_OK != esp_netif_init()) { - log_e("Failed to initialize OpentThread netif"); - err = true; - } - if (!err && ESP_OK != esp_vfs_eventfd_register(&eventfd_config)) { - log_e("Failed to register OpentThread eventfd"); - err = true; - } - - // Initialize the OpenThread stack - if (!err && ESP_OK != esp_openthread_init(&ot_native_config)) { - log_e("Failed to initialize OpenThread stack"); - err = true; - } - if (!err) { - // Initialize the OpenThread cli - otCliInit(esp_openthread_get_instance(), ot_cli_output_callback, NULL); - - // Initialize the esp_netif bindings - esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD(); - openthread_netif = esp_netif_new(&cfg); -#if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM - // Get LwIP Netif - if (openthread_netif != NULL) { - ot_lwip_netif = (struct netif *)esp_netif_get_netif_impl(openthread_netif); - if (ot_lwip_netif == NULL) { - log_e("Failed to get OpenThread LwIP netif"); - } - } -#endif - } - if (!err && openthread_netif == NULL) { - log_e("Failed to create OpenThread esp_netif"); - err = true; - } - if (!err && ESP_OK != esp_netif_attach(openthread_netif, esp_openthread_netif_glue_init(&ot_native_config))) { - log_e("Failed to attach OpenThread esp_netif"); - err = true; - } - if (!err && ESP_OK != esp_netif_set_default_netif(openthread_netif)) { - log_e("Failed to set default OpenThread esp_netif"); - err = true; - } - if (!err) { - // only returns in case there is an OpenThread Stack failure... - esp_openthread_launch_mainloop(); - } - // Clean up - esp_openthread_netif_glue_deinit(); - esp_netif_destroy(openthread_netif); - esp_vfs_eventfd_unregister(); - vTaskDelete(NULL); -} - -void OpenThreadCLI::begin(bool OThreadAutoStart) { - if (otStarted) { +void OpenThreadCLI::begin() { + if (otCLIStarted) { log_w("OpenThread CLI already started. Please end() it before starting again."); return; } - xTaskCreate(ot_task_worker, "ot_main_loop", 10240, NULL, 20, &s_ot_task); + if (!OpenThread::otStarted) { + log_w("OpenThread not started. Please begin() it before starting CLI."); + return; + } //RX Buffer default has 1024 bytes if not preset if (rx_queue == NULL) { @@ -342,55 +256,29 @@ void OpenThreadCLI::begin(bool OThreadAutoStart) { log_e("HW CDC RX Buffer error"); } } + xTaskCreate(ot_cli_loop, "ot_cli", 4096, xTaskGetCurrentTaskHandle(), 2, &s_cli_task); + // Initialize the OpenThread cli + otCliInit(esp_openthread_get_instance(), ot_cli_output_callback, NULL); - // starts Thread with default dataset from NVS or from IDF default settings - if (OThreadAutoStart) { - otOperationalDatasetTlvs dataset; - otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset); - // error = OT_ERROR_FAILED; // teste para forçar NULL dataset - if (error != OT_ERROR_NONE) { - log_i("Failed to get active NVS dataset from OpenThread"); - } else { - log_i("Got active NVS dataset from OpenThread"); - } - esp_err_t err = esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL); - if (err != ESP_OK) { - log_i("Failed to AUTO start OpenThread"); - } else { - log_i("AUTO start OpenThread done"); - } - } - otStarted = true; + otCLIStarted = true; return; } void OpenThreadCLI::end() { - if (!otStarted) { + if (!otCLIStarted) { log_w("OpenThread CLI already stopped. Please begin() it before stopping again."); return; } - if (s_ot_task != NULL) { - vTaskDelete(s_ot_task); - // Clean up - esp_openthread_deinit(); - esp_openthread_netif_glue_deinit(); -#if CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM - ot_lwip_netif = NULL; -#endif - esp_netif_destroy(openthread_netif); - esp_vfs_eventfd_unregister(); - } if (s_cli_task != NULL) { vTaskDelete(s_cli_task); + s_cli_task = NULL; } - if (s_console_cli_task != NULL) { - vTaskDelete(s_console_cli_task); - } + stopConsole(); esp_event_loop_delete_default(); setRxBufferSize(0); setTxBufferSize(0); - otStarted = false; + otCLIStarted = false; } size_t OpenThreadCLI::write(uint8_t c) { diff --git a/libraries/OpenThread/src/OThreadCLI.h b/libraries/OpenThread/src/OThreadCLI.h index bc8dc5d2b19..788edc2709b 100644 --- a/libraries/OpenThread/src/OThreadCLI.h +++ b/libraries/OpenThread/src/OThreadCLI.h @@ -21,7 +21,6 @@ #include "esp_openthread.h" #include "esp_openthread_cli.h" #include "esp_openthread_lock.h" -#include "esp_openthread_netif_glue.h" #include "esp_openthread_types.h" #include "openthread/cli.h" @@ -31,13 +30,14 @@ #include "openthread/dataset_ftd.h" #include "Arduino.h" +#include "OThread.h" typedef std::function OnReceiveCb_t; class OpenThreadCLI : public Stream { private: static size_t setBuffer(QueueHandle_t &queue, size_t len); - bool otStarted = false; + static bool otCLIStarted; public: OpenThreadCLI(); @@ -53,7 +53,7 @@ class OpenThreadCLI : public Stream { void setStream(Stream &otStream); // changes the console Stream object void onReceive(OnReceiveCb_t func); // called on a complete line of output from OT CLI, as OT Response - void begin(bool OThreadAutoStart = true); + void begin(); void end(); // default size is 256 bytes diff --git a/libraries/OpenThread/src/OThreadCLI_Util.cpp b/libraries/OpenThread/src/OThreadCLI_Util.cpp index d21daa1effc..aad435107bb 100644 --- a/libraries/OpenThread/src/OThreadCLI_Util.cpp +++ b/libraries/OpenThread/src/OThreadCLI_Util.cpp @@ -19,26 +19,6 @@ #include "OThreadCLI_Util.h" #include -static const char *otRoleString[] = { - "Disabled", ///< The Thread stack is disabled. - "Detached", ///< Not currently participating in a Thread network/partition. - "Child", ///< The Thread Child role. - "Router", ///< The Thread Router role. - "Leader", ///< The Thread Leader role. -}; - -ot_device_role_t otGetDeviceRole() { - if (!OThreadCLI) { - return OT_ROLE_DISABLED; - } - otInstance *instance = esp_openthread_get_instance(); - return (ot_device_role_t)otThreadGetDeviceRole(instance); -} - -const char *otGetStringDeviceRole() { - return otRoleString[otGetDeviceRole()]; -} - bool otGetRespCmd(const char *cmd, char *resp, uint32_t respTimeout) { if (!OThreadCLI) { return false; @@ -174,7 +154,7 @@ bool otPrintRespCLI(const char *cmd, Stream &output, uint32_t respTimeout) { return true; } -void otPrintNetworkInformation(Stream &output) { +void otCLIPrintNetworkInformation(Stream &output) { if (!OThreadCLI) { return; } diff --git a/libraries/OpenThread/src/OThreadCLI_Util.h b/libraries/OpenThread/src/OThreadCLI_Util.h index 1ab2e061dfc..116b886e095 100644 --- a/libraries/OpenThread/src/OThreadCLI_Util.h +++ b/libraries/OpenThread/src/OThreadCLI_Util.h @@ -18,25 +18,14 @@ #if SOC_IEEE802154_SUPPORTED #if CONFIG_OPENTHREAD_ENABLED -typedef enum { - OT_ROLE_DISABLED = 0, ///< The Thread stack is disabled. - OT_ROLE_DETACHED = 1, ///< Not currently participating in a Thread network/partition. - OT_ROLE_CHILD = 2, ///< The Thread Child role. - OT_ROLE_ROUTER = 3, ///< The Thread Router role. - OT_ROLE_LEADER = 4, ///< The Thread Leader role. -} ot_device_role_t; - typedef struct { int errorCode; String errorMessage; } ot_cmd_return_t; -ot_device_role_t otGetDeviceRole(); -const char *otGetStringDeviceRole(); bool otGetRespCmd(const char *cmd, char *resp = NULL, uint32_t respTimeout = 5000); bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode = NULL); bool otPrintRespCLI(const char *cmd, Stream &output, uint32_t respTimeout); -void otPrintNetworkInformation(Stream &output); #endif /* CONFIG_OPENTHREAD_ENABLED */ #endif /* SOC_IEEE802154_SUPPORTED */ From c7420575f1641bf3caad379608d83df3245e45a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 16 Jun 2025 13:44:11 +0200 Subject: [PATCH 03/13] feat(LEDC): Add Gamma Fade support and enhance auto channel/timer selection for multi-group (#11464) * feat(ledc): Enhance LEDC auto channel/timer selection for multi-group support * feat(ledc): Add Gamma Fade support * fix(example): Update comments * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- cores/esp32/esp32-hal-ledc.c | 264 ++++++++++++++++-- cores/esp32/esp32-hal-ledc.h | 79 ++++++ .../AnalogOut/LEDCGammaFade/LEDCGammaFade.ino | 111 ++++++++ .../examples/AnalogOut/LEDCGammaFade/ci.json | 5 + 4 files changed, 428 insertions(+), 31 deletions(-) create mode 100644 libraries/ESP32/examples/AnalogOut/LEDCGammaFade/LEDCGammaFade.ino create mode 100644 libraries/ESP32/examples/AnalogOut/LEDCGammaFade/ci.json diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 764c2803b4b..18b722f80a2 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -22,6 +22,9 @@ #include "soc/gpio_sig_map.h" #include "esp_rom_gpio.h" #include "hal/ledc_ll.h" +#if SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED +#include +#endif #ifdef SOC_LEDC_SUPPORT_HS_MODE #define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM << 1) @@ -56,7 +59,7 @@ static bool find_matching_timer(uint8_t speed_mode, uint32_t freq, uint8_t resol peripheral_bus_type_t type = perimanGetPinBusType(i); if (type == ESP32_BUS_TYPE_LEDC) { ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(i, ESP32_BUS_TYPE_LEDC); - if (bus != NULL && (bus->channel / 8) == speed_mode && bus->freq_hz == freq && bus->channel_resolution == resolution) { + if (bus != NULL && (bus->channel / SOC_LEDC_CHANNEL_NUM) == speed_mode && bus->freq_hz == freq && bus->channel_resolution == resolution) { log_d("Found matching timer %u for freq=%u, resolution=%u", bus->timer_num, freq, resolution); *timer_num = bus->timer_num; return true; @@ -78,7 +81,7 @@ static bool find_free_timer(uint8_t speed_mode, uint8_t *timer_num) { peripheral_bus_type_t type = perimanGetPinBusType(i); if (type == ESP32_BUS_TYPE_LEDC) { ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(i, ESP32_BUS_TYPE_LEDC); - if (bus != NULL && (bus->channel / 8) == speed_mode) { + if (bus != NULL && (bus->channel / SOC_LEDC_CHANNEL_NUM) == speed_mode) { log_d("Timer %u is in use by channel %u", bus->timer_num, bus->channel); used_timers |= (1 << bus->timer_num); } @@ -110,7 +113,7 @@ static void remove_channel_from_timer(uint8_t speed_mode, uint8_t timer_num, uin peripheral_bus_type_t type = perimanGetPinBusType(i); if (type == ESP32_BUS_TYPE_LEDC) { ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(i, ESP32_BUS_TYPE_LEDC); - if (bus != NULL && (bus->channel / 8) == speed_mode && bus->timer_num == timer_num && bus->channel != channel) { + if (bus != NULL && (bus->channel / SOC_LEDC_CHANNEL_NUM) == speed_mode && bus->timer_num == timer_num && bus->channel != channel) { log_d("Timer %u is still in use by channel %u", timer_num, bus->channel); timer_in_use = true; break; @@ -168,8 +171,8 @@ static bool ledcDetachBus(void *bus) { } pinMatrixOutDetach(handle->pin, false, false); if (!channel_found) { - uint8_t group = (handle->channel / 8); - remove_channel_from_timer(group, handle->timer_num, handle->channel % 8); + uint8_t group = (handle->channel / SOC_LEDC_CHANNEL_NUM); + remove_channel_from_timer(group, handle->timer_num, handle->channel % SOC_LEDC_CHANNEL_NUM); ledc_handle.used_channels &= ~(1UL << handle->channel); } free(handle); @@ -206,13 +209,13 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c return false; } - uint8_t group = (channel / 8); + uint8_t group = (channel / SOC_LEDC_CHANNEL_NUM); uint8_t timer = 0; bool channel_used = ledc_handle.used_channels & (1UL << channel); if (channel_used) { log_i("Channel %u is already set up, given frequency and resolution will be ignored", channel); - if (ledc_set_pin(pin, group, channel % 8) != ESP_OK) { + if (ledc_set_pin(pin, group, channel % SOC_LEDC_CHANNEL_NUM) != ESP_OK) { log_e("Attaching pin to already used channel failed!"); return false; } @@ -220,7 +223,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c // Find a timer with matching frequency and resolution, or a free timer if (!find_matching_timer(group, freq, resolution, &timer)) { if (!find_free_timer(group, &timer)) { - log_e("No free timers available for speed mode %u", group); + log_w("No free timers available for speed mode %u", group); return false; } @@ -239,12 +242,12 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c } } - uint32_t duty = ledc_get_duty(group, (channel % 8)); + uint32_t duty = ledc_get_duty(group, (channel % SOC_LEDC_CHANNEL_NUM)); ledc_channel_config_t ledc_channel; memset((void *)&ledc_channel, 0, sizeof(ledc_channel_config_t)); ledc_channel.speed_mode = group; - ledc_channel.channel = (channel % 8); + ledc_channel.channel = (channel % SOC_LEDC_CHANNEL_NUM); ledc_channel.timer_sel = timer; ledc_channel.intr_type = LEDC_INTR_DISABLE; ledc_channel.gpio_num = pin; @@ -274,7 +277,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c ledc_handle.used_channels |= 1UL << channel; } - if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_LEDC, (void *)handle, group, channel)) { + if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_LEDC, (void *)handle, channel, timer)) { ledcDetachBus((void *)handle); return false; } @@ -291,14 +294,40 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) { } uint8_t channel = __builtin_ctz(free_channel); // Convert the free_channel bit to channel number - return ledcAttachChannel(pin, freq, resolution, channel); + // Try the first available channel + if (ledcAttachChannel(pin, freq, resolution, channel)) { + return true; + } + +#ifdef SOC_LEDC_SUPPORT_HS_MODE + // If first attempt failed and HS mode is supported, try to find a free channel in group 1 + if ((channel / SOC_LEDC_CHANNEL_NUM) == 0) { // First attempt was in group 0 + log_d("LEDC: Group 0 channel %u failed, trying to find a free channel in group 1", channel); + // Find free channels specifically in group 1 + uint32_t group1_mask = ((1UL << SOC_LEDC_CHANNEL_NUM) - 1) << SOC_LEDC_CHANNEL_NUM; + int group1_free_channel = (~ledc_handle.used_channels) & group1_mask; + if (group1_free_channel != 0) { + uint8_t group1_channel = __builtin_ctz(group1_free_channel); + if (ledcAttachChannel(pin, freq, resolution, group1_channel)) { + return true; + } + } + } +#endif + + log_e( + "No free timers available for freq=%u, resolution=%u. To attach a new channel, use the same frequency and resolution as an already attached channel to " + "share its timer.", + freq, resolution + ); + return false; } bool ledcWrite(uint8_t pin, uint32_t duty) { ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC); if (bus != NULL) { - uint8_t group = (bus->channel / 8), channel = (bus->channel % 8); + uint8_t group = (bus->channel / SOC_LEDC_CHANNEL_NUM), channel = (bus->channel % SOC_LEDC_CHANNEL_NUM); //Fixing if all bits in resolution is set = LEDC FULL ON uint32_t max_duty = (1 << bus->channel_resolution) - 1; @@ -307,8 +336,14 @@ bool ledcWrite(uint8_t pin, uint32_t duty) { duty = max_duty + 1; } - ledc_set_duty(group, channel, duty); - ledc_update_duty(group, channel); + if (ledc_set_duty(group, channel, duty) != ESP_OK) { + log_e("ledc_set_duty failed"); + return false; + } + if (ledc_update_duty(group, channel) != ESP_OK) { + log_e("ledc_update_duty failed"); + return false; + } return true; } @@ -321,7 +356,11 @@ bool ledcWriteChannel(uint8_t channel, uint32_t duty) { log_e("Channel %u is not available (maximum %u) or not used!", channel, LEDC_CHANNELS); return false; } - uint8_t group = (channel / 8), timer = ((channel / 2) % 4); + uint8_t group = (channel / SOC_LEDC_CHANNEL_NUM); + ledc_timer_t timer; + + // Get the actual timer being used by this channel + ledc_ll_get_channel_timer(LEDC_LL_GET_HW(), group, (channel % SOC_LEDC_CHANNEL_NUM), &timer); //Fixing if all bits in resolution is set = LEDC FULL ON uint32_t resolution = 0; @@ -333,8 +372,14 @@ bool ledcWriteChannel(uint8_t channel, uint32_t duty) { duty = max_duty + 1; } - ledc_set_duty(group, channel, duty); - ledc_update_duty(group, channel); + if (ledc_set_duty(group, channel, duty) != ESP_OK) { + log_e("ledc_set_duty failed"); + return false; + } + if (ledc_update_duty(group, channel) != ESP_OK) { + log_e("ledc_update_duty failed"); + return false; + } return true; } @@ -343,7 +388,7 @@ uint32_t ledcRead(uint8_t pin) { ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC); if (bus != NULL) { - uint8_t group = (bus->channel / 8), channel = (bus->channel % 8); + uint8_t group = (bus->channel / SOC_LEDC_CHANNEL_NUM), channel = (bus->channel % SOC_LEDC_CHANNEL_NUM); return ledc_get_duty(group, channel); } return 0; @@ -355,8 +400,8 @@ uint32_t ledcReadFreq(uint8_t pin) { if (!ledcRead(pin)) { return 0; } - uint8_t group = (bus->channel / 8), timer = ((bus->channel / 2) % 4); - return ledc_get_freq(group, timer); + uint8_t group = (bus->channel / SOC_LEDC_CHANNEL_NUM); + return ledc_get_freq(group, bus->timer_num); } return 0; } @@ -370,12 +415,12 @@ uint32_t ledcWriteTone(uint8_t pin, uint32_t freq) { return 0; } - uint8_t group = (bus->channel / 8), timer = ((bus->channel / 2) % 4); + uint8_t group = (bus->channel / SOC_LEDC_CHANNEL_NUM); ledc_timer_config_t ledc_timer; memset((void *)&ledc_timer, 0, sizeof(ledc_timer_config_t)); ledc_timer.speed_mode = group; - ledc_timer.timer_num = timer; + ledc_timer.timer_num = bus->timer_num; ledc_timer.duty_resolution = 10; ledc_timer.freq_hz = freq; ledc_timer.clk_cfg = clock_source; @@ -386,7 +431,7 @@ uint32_t ledcWriteTone(uint8_t pin, uint32_t freq) { } bus->channel_resolution = 10; - uint32_t res_freq = ledc_get_freq(group, timer); + uint32_t res_freq = ledc_get_freq(group, bus->timer_num); ledcWrite(pin, 0x1FF); return res_freq; } @@ -427,12 +472,12 @@ uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution) { log_e("LEDC pin %u - resolution is zero or it is too big (maximum %u)", pin, LEDC_MAX_BIT_WIDTH); return 0; } - uint8_t group = (bus->channel / 8), timer = ((bus->channel / 2) % 4); + uint8_t group = (bus->channel / SOC_LEDC_CHANNEL_NUM); ledc_timer_config_t ledc_timer; memset((void *)&ledc_timer, 0, sizeof(ledc_timer_config_t)); ledc_timer.speed_mode = group; - ledc_timer.timer_num = timer; + ledc_timer.timer_num = bus->timer_num; ledc_timer.duty_resolution = resolution; ledc_timer.freq_hz = freq; ledc_timer.clk_cfg = clock_source; @@ -442,7 +487,7 @@ uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution) { return 0; } bus->channel_resolution = resolution; - return ledc_get_freq(group, timer); + return ledc_get_freq(group, bus->timer_num); } return 0; } @@ -453,12 +498,14 @@ bool ledcOutputInvert(uint8_t pin, bool out_invert) { gpio_set_level(pin, out_invert); #ifdef CONFIG_IDF_TARGET_ESP32P4 - esp_rom_gpio_connect_out_signal(pin, LEDC_LS_SIG_OUT_PAD_OUT0_IDX + ((bus->channel) % 8), out_invert, 0); + esp_rom_gpio_connect_out_signal(pin, LEDC_LS_SIG_OUT_PAD_OUT0_IDX + ((bus->channel) % SOC_LEDC_CHANNEL_NUM), out_invert, 0); #else #ifdef SOC_LEDC_SUPPORT_HS_MODE - esp_rom_gpio_connect_out_signal(pin, ((bus->channel / 8 == 0) ? LEDC_HS_SIG_OUT0_IDX : LEDC_LS_SIG_OUT0_IDX) + ((bus->channel) % 8), out_invert, 0); + esp_rom_gpio_connect_out_signal( + pin, ((bus->channel / SOC_LEDC_CHANNEL_NUM == 0) ? LEDC_HS_SIG_OUT0_IDX : LEDC_LS_SIG_OUT0_IDX) + ((bus->channel) % SOC_LEDC_CHANNEL_NUM), out_invert, 0 + ); #else - esp_rom_gpio_connect_out_signal(pin, LEDC_LS_SIG_OUT0_IDX + ((bus->channel) % 8), out_invert, 0); + esp_rom_gpio_connect_out_signal(pin, LEDC_LS_SIG_OUT0_IDX + ((bus->channel) % SOC_LEDC_CHANNEL_NUM), out_invert, 0); #endif #endif // ifdef CONFIG_IDF_TARGET_ESP32P4 return true; @@ -505,7 +552,7 @@ static bool ledcFadeConfig(uint8_t pin, uint32_t start_duty, uint32_t target_dut } #endif #endif - uint8_t group = (bus->channel / 8), channel = (bus->channel % 8); + uint8_t group = (bus->channel / SOC_LEDC_CHANNEL_NUM), channel = (bus->channel % SOC_LEDC_CHANNEL_NUM); // Initialize fade service. if (!fade_initialized) { @@ -562,6 +609,161 @@ bool ledcFadeWithInterruptArg(uint8_t pin, uint32_t start_duty, uint32_t target_ return ledcFadeConfig(pin, start_duty, target_duty, max_fade_time_ms, userFunc, arg); } +#ifdef SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED +// Default gamma factor for gamma correction (common value for LEDs) +static float ledcGammaFactor = 2.8; +// Gamma correction LUT support +static const float *ledcGammaLUT = NULL; +static uint16_t ledcGammaLUTSize = 0; +// Global variable to store current resolution for gamma callback +static uint8_t ledcGammaResolution = 13; + +bool ledcSetGammaTable(const float *gamma_table, uint16_t size) { + if (gamma_table == NULL || size == 0) { + log_e("Invalid gamma table or size"); + return false; + } + ledcGammaLUT = gamma_table; + ledcGammaLUTSize = size; + log_i("Custom gamma LUT set with %u entries", size); + return true; +} + +void ledcClearGammaTable(void) { + ledcGammaLUT = NULL; + ledcGammaLUTSize = 0; + log_i("Gamma LUT cleared, using mathematical calculation"); +} + +void ledcSetGammaFactor(float factor) { + ledcGammaFactor = factor; +} + +// Gamma correction calculator function +static uint32_t ledcGammaCorrection(uint32_t duty) { + if (duty == 0) { + return 0; + } + + uint32_t max_duty = (1U << ledcGammaResolution) - 1; + if (duty >= (1U << ledcGammaResolution)) { + return max_duty; + } + + // Use LUT if provided, otherwise use mathematical calculation + if (ledcGammaLUT != NULL && ledcGammaLUTSize > 0) { + // LUT-based gamma correction + uint32_t lut_index = (duty * (ledcGammaLUTSize - 1)) / max_duty; + if (lut_index >= ledcGammaLUTSize) { + lut_index = ledcGammaLUTSize - 1; + } + + float corrected_normalized = ledcGammaLUT[lut_index]; + return (uint32_t)(corrected_normalized * max_duty); + } else { + // Mathematical gamma correction + double normalized = (double)duty / (1U << ledcGammaResolution); + double corrected = pow(normalized, ledcGammaFactor); + return (uint32_t)(corrected * (1U << ledcGammaResolution)); + } +} + +static bool ledcFadeGammaConfig(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void *), void *arg) { + ledc_channel_handle_t *bus = (ledc_channel_handle_t *)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC); + if (bus != NULL) { + +#ifndef SOC_LEDC_SUPPORT_FADE_STOP +#if !CONFIG_DISABLE_HAL_LOCKS + if (bus->lock == NULL) { + bus->lock = xSemaphoreCreateBinary(); + if (bus->lock == NULL) { + log_e("xSemaphoreCreateBinary failed"); + return false; + } + xSemaphoreGive(bus->lock); + } + //acquire lock + if (xSemaphoreTake(bus->lock, 0) != pdTRUE) { + log_e("LEDC Fade is still running on pin %u! SoC does not support stopping fade.", pin); + return false; + } +#endif +#endif + uint8_t group = (bus->channel / SOC_LEDC_CHANNEL_NUM), channel = (bus->channel % SOC_LEDC_CHANNEL_NUM); + + // Initialize fade service. + if (!fade_initialized) { + ledc_fade_func_install(0); + fade_initialized = true; + } + + bus->fn = (voidFuncPtr)userFunc; + bus->arg = arg; + + ledc_cbs_t callbacks = {.fade_cb = ledcFnWrapper}; + ledc_cb_register(group, channel, &callbacks, (void *)bus); + + // Prepare gamma curve fade parameters + ledc_fade_param_config_t fade_params[SOC_LEDC_GAMMA_CURVE_FADE_RANGE_MAX]; + uint32_t actual_fade_ranges = 0; + + // Use a moderate number of linear segments for smooth gamma curve + const uint32_t linear_fade_segments = 12; + + // Set the global resolution for gamma correction + ledcGammaResolution = bus->channel_resolution; + + // Fill multi-fade parameter list using ESP-IDF API + esp_err_t err = ledc_fill_multi_fade_param_list( + group, channel, start_duty, target_duty, linear_fade_segments, max_fade_time_ms, ledcGammaCorrection, SOC_LEDC_GAMMA_CURVE_FADE_RANGE_MAX, fade_params, + &actual_fade_ranges + ); + + if (err != ESP_OK) { + log_e("ledc_fill_multi_fade_param_list failed: %s", esp_err_to_name(err)); + return false; + } + + // Apply the gamma-corrected start duty + uint32_t gamma_start_duty = ledcGammaCorrection(start_duty); + + // Set multi-fade parameters + err = ledc_set_multi_fade(group, channel, gamma_start_duty, fade_params, actual_fade_ranges); + if (err != ESP_OK) { + log_e("ledc_set_multi_fade failed: %s", esp_err_to_name(err)); + return false; + } + + // Start the gamma curve fade + err = ledc_fade_start(group, channel, LEDC_FADE_NO_WAIT); + if (err != ESP_OK) { + log_e("ledc_fade_start failed: %s", esp_err_to_name(err)); + return false; + } + + log_d("Gamma curve fade started on pin %u: %u -> %u over %dms", pin, start_duty, target_duty, max_fade_time_ms); + + } else { + log_e("Pin %u is not attached to LEDC. Call ledcAttach first!", pin); + return false; + } + return true; +} + +bool ledcFadeGamma(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms) { + return ledcFadeGammaConfig(pin, start_duty, target_duty, max_fade_time_ms, NULL, NULL); +} + +bool ledcFadeGammaWithInterrupt(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, voidFuncPtr userFunc) { + return ledcFadeGammaConfig(pin, start_duty, target_duty, max_fade_time_ms, (voidFuncPtrArg)userFunc, NULL); +} + +bool ledcFadeGammaWithInterruptArg(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void *), void *arg) { + return ledcFadeGammaConfig(pin, start_duty, target_duty, max_fade_time_ms, userFunc, arg); +} + +#endif /* SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED */ + static uint8_t analog_resolution = 8; static int analog_frequency = 1000; void analogWrite(uint8_t pin, int value) { diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index f1a27dd4f7a..1663b884a3f 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -232,6 +232,85 @@ bool ledcFadeWithInterrupt(uint8_t pin, uint32_t start_duty, uint32_t target_dut */ bool ledcFadeWithInterruptArg(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void *), void *arg); +//Gamma Curve Fade functions - only available on supported chips +#ifdef SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED + +/** + * @brief Set a custom gamma correction lookup table for gamma curve fading. + * The LUT should contain normalized values (0.0 to 1.0) representing + * the gamma-corrected brightness curve. + * + * @param gamma_table Pointer to array of float values (0.0 to 1.0) + * @param size Number of entries in the lookup table + * + * @return true if gamma table was successfully set, false otherwise. + * + * @note The LUT array must remain valid for as long as gamma fading is used. + * Larger tables provide smoother transitions but use more memory. + */ +bool ledcSetGammaTable(const float *gamma_table, uint16_t size); + +/** + * @brief Clear the current gamma correction lookup table. + * After calling this, gamma correction will use mathematical + * calculation with the default gamma factor (2.8). + */ +void ledcClearGammaTable(void); + +/** + * @brief Set the gamma factor for gamma correction. + * + * @param factor Gamma factor to use for gamma correction. + */ +void ledcSetGammaFactor(float factor); + +/** + * @brief Setup and start a gamma curve fade on a given LEDC pin. + * Gamma correction makes LED brightness changes appear more gradual to human eyes. + * + * @param pin GPIO pin + * @param start_duty initial duty cycle of the fade + * @param target_duty target duty cycle of the fade + * @param max_fade_time_ms maximum fade time in milliseconds + * + * @return true if gamma fade was successfully set and started, false otherwise. + * + * @note This function is only available on ESP32 variants that support gamma curve fading. + */ +bool ledcFadeGamma(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms); + +/** + * @brief Setup and start a gamma curve fade on a given LEDC pin with a callback function. + * + * @param pin GPIO pin + * @param start_duty initial duty cycle of the fade + * @param target_duty target duty cycle of the fade + * @param max_fade_time_ms maximum fade time in milliseconds + * @param userFunc callback function to be called after fade is finished + * + * @return true if gamma fade was successfully set and started, false otherwise. + * + * @note This function is only available on ESP32 variants that support gamma curve fading. + */ +bool ledcFadeGammaWithInterrupt(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void)); + +/** + * @brief Setup and start a gamma curve fade on a given LEDC pin with a callback function and argument. + * + * @param pin GPIO pin + * @param start_duty initial duty cycle of the fade + * @param target_duty target duty cycle of the fade + * @param max_fade_time_ms maximum fade time in milliseconds + * @param userFunc callback function to be called after fade is finished + * @param arg argument to be passed to the callback function + * + * @return true if gamma fade was successfully set and started, false otherwise. + * + * @note This function is only available on ESP32 variants that support gamma curve fading. + */ +bool ledcFadeGammaWithInterruptArg(uint8_t pin, uint32_t start_duty, uint32_t target_duty, int max_fade_time_ms, void (*userFunc)(void *), void *arg); +#endif // SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED + #ifdef __cplusplus } #endif diff --git a/libraries/ESP32/examples/AnalogOut/LEDCGammaFade/LEDCGammaFade.ino b/libraries/ESP32/examples/AnalogOut/LEDCGammaFade/LEDCGammaFade.ino new file mode 100644 index 00000000000..4ca6c136ddf --- /dev/null +++ b/libraries/ESP32/examples/AnalogOut/LEDCGammaFade/LEDCGammaFade.ino @@ -0,0 +1,111 @@ +/* LEDC Gamma Curve Fade Arduino Example + + This example demonstrates gamma curve fading on ESP32 variants that support it. + Gamma correction makes LED brightness changes appear more gradual and natural + to human eyes compared to linear fading. + + Two methods are supported: + 1. Using a pre-computed Gamma Look-Up Table (LUT) for better performance + 2. Using mathematical gamma correction with a gamma factor + + Supported chips: ESP32-C6, ESP32-C5, ESP32-H2, ESP32-P4 and future chips with Gamma Fade support + + Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/) +*/ + +// use 12 bit precision for LEDC timer +#define LEDC_TIMER_12_BIT 12 + +// use 5000 Hz as a LEDC base frequency +#define LEDC_BASE_FREQ 5000 + +// define starting duty, target duty and maximum fade time +#define LEDC_START_DUTY (0) +#define LEDC_TARGET_DUTY (4095) +#define LEDC_FADE_TIME (2000) + +// gamma factor for mathematical calculation +#define LEDC_GAMMA_FACTOR (2.6) + +// use gamma LUT for better performance instead of mathematical calculation (gamma factor) +#define USE_GAMMA_LUT 1 + +// fade LED pins +const uint8_t ledPinR = 4; +const uint8_t ledPinG = 5; +const uint8_t ledPinB = 6; + +uint8_t fade_ended = 0; // status of LED gamma fade +bool fade_in = true; + +#ifdef USE_GAMMA_LUT +// Custom Gamma LUT demonstration with 101 steps (Brightness 0 - 100% gamma correction look up table (gamma = 2.6)) +// Y = B ^ 2.6 - Pre-computed LUT to save runtime computation +static const float ledcGammaLUT[101] = { + 0.000000, 0.000006, 0.000038, 0.000110, 0.000232, 0.000414, 0.000666, 0.000994, 0.001406, 0.001910, 0.002512, 0.003218, 0.004035, 0.004969, 0.006025, + 0.007208, 0.008525, 0.009981, 0.011580, 0.013328, 0.015229, 0.017289, 0.019512, 0.021902, 0.024465, 0.027205, 0.030125, 0.033231, 0.036527, 0.040016, + 0.043703, 0.047593, 0.051688, 0.055993, 0.060513, 0.065249, 0.070208, 0.075392, 0.080805, 0.086451, 0.092333, 0.098455, 0.104821, 0.111434, 0.118298, + 0.125416, 0.132792, 0.140428, 0.148329, 0.156498, 0.164938, 0.173653, 0.182645, 0.191919, 0.201476, 0.211321, 0.221457, 0.231886, 0.242612, 0.253639, + 0.264968, 0.276603, 0.288548, 0.300805, 0.313378, 0.326268, 0.339480, 0.353016, 0.366879, 0.381073, 0.395599, 0.410461, 0.425662, 0.441204, 0.457091, + 0.473325, 0.489909, 0.506846, 0.524138, 0.541789, 0.559801, 0.578177, 0.596920, 0.616032, 0.635515, 0.655374, 0.675610, 0.696226, 0.717224, 0.738608, + 0.760380, 0.782542, 0.805097, 0.828048, 0.851398, 0.875148, 0.899301, 0.923861, 0.948829, 0.974208, 1.000000, +}; +#endif + +void ARDUINO_ISR_ATTR LED_FADE_ISR() { + fade_ended += 1; +} + +void setup() { + // Initialize serial communication at 115200 bits per second: + Serial.begin(115200); + + // Setup timer with given frequency, resolution and attach it to a led pin with auto-selected channel + ledcAttach(ledPinR, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT); + ledcAttach(ledPinG, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT); + ledcAttach(ledPinB, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT); + +#if USE_GAMMA_LUT // Use default gamma LUT for better performance + ledcSetGammaTable(ledcGammaLUT, 101); +#else // Use mathematical gamma correction (default, more flexible) + ledcSetGammaFactor(LEDC_GAMMA_FACTOR); // This is optional to set custom gamma factor (default is 2.8) +#endif + + // Setup and start gamma curve fade on led (duty from 0 to 4095) + ledcFadeGamma(ledPinR, LEDC_START_DUTY, LEDC_TARGET_DUTY, LEDC_FADE_TIME); + ledcFadeGamma(ledPinG, LEDC_START_DUTY, LEDC_TARGET_DUTY, LEDC_FADE_TIME); + ledcFadeGamma(ledPinB, LEDC_START_DUTY, LEDC_TARGET_DUTY, LEDC_FADE_TIME); + Serial.println("LED Gamma Fade on started."); + + // Wait for fade to end + delay(LEDC_FADE_TIME); + + // Setup and start gamma curve fade off led and use ISR (duty from 4095 to 0) + ledcFadeGammaWithInterrupt(ledPinR, LEDC_TARGET_DUTY, LEDC_START_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + ledcFadeGammaWithInterrupt(ledPinG, LEDC_TARGET_DUTY, LEDC_START_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + ledcFadeGammaWithInterrupt(ledPinB, LEDC_TARGET_DUTY, LEDC_START_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + Serial.println("LED Gamma Fade off started."); +} + +void loop() { + // Check if fade_ended flag was set to true in ISR + if (fade_ended == 3) { + Serial.println("LED gamma fade ended"); + fade_ended = 0; + + // Check what gamma fade should be started next + if (fade_in) { + ledcFadeGammaWithInterrupt(ledPinR, LEDC_START_DUTY, LEDC_TARGET_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + ledcFadeGammaWithInterrupt(ledPinG, LEDC_START_DUTY, LEDC_TARGET_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + ledcFadeGammaWithInterrupt(ledPinB, LEDC_START_DUTY, LEDC_TARGET_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + Serial.println("LED Gamma Fade in started."); + fade_in = false; + } else { + ledcFadeGammaWithInterrupt(ledPinR, LEDC_TARGET_DUTY, LEDC_START_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + ledcFadeGammaWithInterrupt(ledPinG, LEDC_TARGET_DUTY, LEDC_START_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + ledcFadeGammaWithInterrupt(ledPinB, LEDC_TARGET_DUTY, LEDC_START_DUTY, LEDC_FADE_TIME, LED_FADE_ISR); + Serial.println("LED Gamma Fade out started."); + fade_in = true; + } + } +} diff --git a/libraries/ESP32/examples/AnalogOut/LEDCGammaFade/ci.json b/libraries/ESP32/examples/AnalogOut/LEDCGammaFade/ci.json new file mode 100644 index 00000000000..a9d8603b7bf --- /dev/null +++ b/libraries/ESP32/examples/AnalogOut/LEDCGammaFade/ci.json @@ -0,0 +1,5 @@ +{ + "requires": [ + "CONFIG_SOC_LEDC_GAMMA_CURVE_FADE_SUPPORTED=y" + ] +} From 5986e5588240f77342c80de6cc9967ea982faf36 Mon Sep 17 00:00:00 2001 From: is-qian <2716275834@qq.com> Date: Tue, 17 Jun 2025 18:39:33 +0800 Subject: [PATCH 04/13] fix: Delete 8M flash option for xiao_esp32_s3_plus. (#11476) --- boards.txt | 6 ++---- variants/XIAO_ESP32S3_Plus/pins_arduino.h | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/boards.txt b/boards.txt index 15a75eaad8b..b51a8840757 100644 --- a/boards.txt +++ b/boards.txt @@ -36051,12 +36051,12 @@ XIAO_ESP32S3_Plus.build.cdc_on_boot=1 XIAO_ESP32S3_Plus.build.msc_on_boot=0 XIAO_ESP32S3_Plus.build.dfu_on_boot=0 XIAO_ESP32S3_Plus.build.f_cpu=240000000L -XIAO_ESP32S3_Plus.build.flash_size=8MB +XIAO_ESP32S3_Plus.build.flash_size=16MB XIAO_ESP32S3_Plus.build.flash_freq=80m XIAO_ESP32S3_Plus.build.flash_mode=dio XIAO_ESP32S3_Plus.build.boot=qio XIAO_ESP32S3_Plus.build.boot_freq=80m -XIAO_ESP32S3_Plus.build.partitions=default_8MB +XIAO_ESP32S3_Plus.build.partitions=ffat XIAO_ESP32S3_Plus.build.defines= XIAO_ESP32S3_Plus.build.loop_core= XIAO_ESP32S3_Plus.build.event_core= @@ -36093,8 +36093,6 @@ XIAO_ESP32S3_Plus.menu.FlashMode.dio.build.boot=dio XIAO_ESP32S3_Plus.menu.FlashMode.dio.build.boot_freq=80m XIAO_ESP32S3_Plus.menu.FlashMode.dio.build.flash_freq=80m -XIAO_ESP32S3_Plus.menu.FlashSize.8M=8MB (64Mb) -XIAO_ESP32S3_Plus.menu.FlashSize.8M.build.flash_size=8MB XIAO_ESP32S3_Plus.menu.FlashSize.16M=16MB (128Mb) XIAO_ESP32S3_Plus.menu.FlashSize.16M.build.flash_size=16MB diff --git a/variants/XIAO_ESP32S3_Plus/pins_arduino.h b/variants/XIAO_ESP32S3_Plus/pins_arduino.h index fb887287e35..de1c6093d44 100644 --- a/variants/XIAO_ESP32S3_Plus/pins_arduino.h +++ b/variants/XIAO_ESP32S3_Plus/pins_arduino.h @@ -4,7 +4,7 @@ #include #define USB_VID 0x2886 -#define USB_PID 0x0056 +#define USB_PID 0x0063 static const uint8_t LED_BUILTIN = 21; #define BUILTIN_LED LED_BUILTIN // backward compatibility From b956da229b7d6b0946487efc2964b1e45500c29c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Proch=C3=A1zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 17 Jun 2025 15:23:35 +0200 Subject: [PATCH 05/13] feat(spi): Add return values to SPI begin (#11477) --- cores/esp32/esp32-hal-spi.h | 4 ++-- libraries/SPI/src/SPI.cpp | 9 +++++---- libraries/SPI/src/SPI.h | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cores/esp32/esp32-hal-spi.h b/cores/esp32/esp32-hal-spi.h index 7d56f0820d3..565ca43f60d 100644 --- a/cores/esp32/esp32-hal-spi.h +++ b/cores/esp32/esp32-hal-spi.h @@ -38,8 +38,8 @@ extern "C" { #define HSPI 2 //SPI 2 bus normally mapped to pins 12 - 15, but can be matrixed to any pins #define VSPI 3 //SPI 3 bus normally attached to pins 5, 18, 19 and 23, but can be matrixed to any pins #else -#define FSPI 0 -#define HSPI 1 +#define FSPI 0 // ESP32C2, C3, C6, H2, S3, P4 - SPI 2 bus +#define HSPI 1 // ESP32S3, P4 - SPI 3 bus #endif // This defines are not representing the real Divider of the ESP32 diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index ae207a7ff3c..c0d7665df03 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -63,9 +63,9 @@ SPIClass::~SPIClass() { #endif } -void SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) { +bool SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) { if (_spi) { - return; + return true; } if (!_div) { @@ -74,7 +74,7 @@ void SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) { _spi = spiStartBus(_spi_num, _div, SPI_MODE0, SPI_MSBFIRST); if (!_spi) { - return; + return false; } if (sck == -1 && miso == -1 && mosi == -1 && ss == -1) { @@ -110,10 +110,11 @@ void SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) { if (_mosi >= 0 && !spiAttachMOSI(_spi, _mosi)) { goto err; } - return; + return true; err: log_e("Attaching pins to SPI failed."); + return false; } void SPIClass::end() { diff --git a/libraries/SPI/src/SPI.h b/libraries/SPI/src/SPI.h index 628c2190f50..6c300e53df2 100644 --- a/libraries/SPI/src/SPI.h +++ b/libraries/SPI/src/SPI.h @@ -61,7 +61,7 @@ class SPIClass { public: SPIClass(uint8_t spi_bus = HSPI); ~SPIClass(); - void begin(int8_t sck = -1, int8_t miso = -1, int8_t mosi = -1, int8_t ss = -1); + bool begin(int8_t sck = -1, int8_t miso = -1, int8_t mosi = -1, int8_t ss = -1); void end(); void setHwCs(bool use); From 15f6f41a6721102b1aafeab87dbfa3005a47cfff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20BOU=C3=89?= Date: Wed, 18 Jun 2025 19:55:11 +0200 Subject: [PATCH 06/13] feat(openthread): Add RLOC16 in otPrintNetworkInformation() (#11480) * feat(openthread): Add RLOC16 in otPrintNetworkInformation() --- libraries/OpenThread/src/OThread.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/OpenThread/src/OThread.cpp b/libraries/OpenThread/src/OThread.cpp index 43d8ce02918..7714d870cce 100644 --- a/libraries/OpenThread/src/OThread.cpp +++ b/libraries/OpenThread/src/OThread.cpp @@ -335,6 +335,8 @@ void OpenThread::otPrintNetworkInformation(Stream &output) { output.printf("Role: %s", otGetStringDeviceRole()); output.println(); + output.printf("RLOC16: 0x%04x", otThreadGetRloc16(mInstance)); // RLOC16 + output.println(); output.printf("Network Name: %s", otThreadGetNetworkName(mInstance)); output.println(); output.printf("Channel: %d", otLinkGetChannel(mInstance)); From 02653b0db26049914ce0bec70199e6de7e0a6afb Mon Sep 17 00:00:00 2001 From: Wulu Date: Fri, 20 Jun 2025 17:22:01 +0800 Subject: [PATCH 07/13] fix(docs): correct code block indentation in core_compatibility.rst (#11471) * fix(docs): correct code block indentation in core compatibility guide * fix(docs): remove extra colon causing rendering error in core_compatibility.rst --- docs/en/guides/core_compatibility.rst | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/en/guides/core_compatibility.rst b/docs/en/guides/core_compatibility.rst index cb530ac5e7c..8b6a8e79d26 100644 --- a/docs/en/guides/core_compatibility.rst +++ b/docs/en/guides/core_compatibility.rst @@ -9,28 +9,28 @@ Welcome to the compatibility guide for library developers aiming to support mult Code Adaptations ---------------- -To ensure compatibility with both versions of the ESP32 Arduino core, developers should utilize conditional compilation directives in their code. Below is an example of how to conditionally include code based on the ESP32 Arduino core version:: +To ensure compatibility with both versions of the ESP32 Arduino core, developers should utilize conditional compilation directives in their code. Below is an example of how to conditionally include code based on the ESP32 Arduino core version: - .. code-block:: cpp +.. code-block:: cpp - #ifdef ESP_ARDUINO_VERSION_MAJOR - #if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0) - // Code for version 3.x - #else - // Code for version 2.x - #endif - #else - // Code for version 1.x - #endif + #ifdef ESP_ARDUINO_VERSION_MAJOR + #if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0) + // Code for version 3.x + #else + // Code for version 2.x + #endif + #else + // Code for version 1.x + #endif Version Print ------------- -To easily print the ESP32 Arduino core version at runtime, developers can use the `ESP_ARDUINO_VERSION_STR` macro. Below is an example of how to print the ESP32 Arduino core version:: +To easily print the ESP32 Arduino core version at runtime, developers can use the `ESP_ARDUINO_VERSION_STR` macro. Below is an example of how to print the ESP32 Arduino core version: - .. code-block:: cpp +.. code-block:: cpp - Serial.printf(" ESP32 Arduino core version: %s\n", ESP_ARDUINO_VERSION_STR); + Serial.printf(" ESP32 Arduino core version: %s\n", ESP_ARDUINO_VERSION_STR); API Differences --------------- From 9fd0b1546afcfafd9eb67fc05693f0efd5e3ce12 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 20 Jun 2025 06:23:18 -0300 Subject: [PATCH 08/13] feat(esptool): Upgrade to esptool v5 (#11487) --- .github/scripts/package_esptool.sh | 129 ++++++++++++++++++++++ package/package_esp32_index.template.json | 70 ++++++------ platform.txt | 12 +- 3 files changed, 170 insertions(+), 41 deletions(-) create mode 100755 .github/scripts/package_esptool.sh diff --git a/.github/scripts/package_esptool.sh b/.github/scripts/package_esptool.sh new file mode 100755 index 00000000000..32b87b277e9 --- /dev/null +++ b/.github/scripts/package_esptool.sh @@ -0,0 +1,129 @@ +#!/bin/bash + +set -euo pipefail + +# Check version argument +if [[ $# -ne 3 ]]; then + echo "Usage: $0 " + echo "Example: $0 5.0.dev1 /tmp/esptool /tmp/esptool-5.0.dev1.json" + exit 1 +fi + +VERSION=$1 +BASE_FOLDER=$2 +JSON_PATH=$3 + +export COPYFILE_DISABLE=1 + +shopt -s nullglob # So for loop doesn't run if no matches + +# Function to update JSON for a given host +function update_json_for_host { + local host=$1 + local archive=$2 + + # Extract the old url from the JSON for this host, then replace only the filename + old_url=$(jq -r --arg host "$host" ' + .packages[].tools[] | select(.name == "esptool_py") | .systems[] | select(.host == $host) | .url // empty + ' "$tmp_json") + if [[ -n "$old_url" ]]; then + base_url="${old_url%/*}" + url="$base_url/$archive" + else + echo "No old url found for $host" + exit 1 + fi + + archiveFileName="$archive" + checksum="SHA-256:$(shasum -a 256 "$archive" | awk '{print $1}')" + size=$(stat -f%z "$archive") + + # Use jq to update the JSON + jq --arg host "$host" \ + --arg url "$url" \ + --arg archiveFileName "$archiveFileName" \ + --arg checksum "$checksum" \ + --arg size "$size" \ + ' + .packages[].tools[] + |= if .name == "esptool_py" then + .systems = ( + ((.systems // []) | map(select(.host != $host))) + [{ + host: $host, + url: $url, + archiveFileName: $archiveFileName, + checksum: $checksum, + size: $size + }] + ) + else + . + end + ' "$tmp_json" > "$tmp_json.new" && mv "$tmp_json.new" "$tmp_json" +} + +cd "$BASE_FOLDER" + +# Delete all archives before starting +rm -f esptool-*.tar.gz esptool-*.zip + +for dir in esptool-*; do + # Check if directory exists and is a directory + if [[ ! -d "$dir" ]]; then + continue + fi + + base="${dir#esptool-}" + + # Add 'linux-' prefix if base doesn't contain linux/macos/win64 + if [[ "$base" != *linux* && "$base" != *macos* && "$base" != *win64* ]]; then + base="linux-${base}" + fi + + if [[ "$dir" == esptool-win* ]]; then + # Windows zip archive + zipfile="esptool-v${VERSION}-${base}.zip" + echo "Creating $zipfile from $dir ..." + zip -r "$zipfile" "$dir" + else + # Non-Windows: set permissions and tar.gz archive + tarfile="esptool-v${VERSION}-${base}.tar.gz" + echo "Setting permissions and creating $tarfile from $dir ..." + chmod -R u=rwx,g=rx,o=rx "$dir" + tar -cvzf "$tarfile" "$dir" + fi +done + +# After the for loop, update the JSON for each archive +# Create a temporary JSON file to accumulate changes +tmp_json="${JSON_PATH}.tmp" +cp "$JSON_PATH" "$tmp_json" + +for archive in esptool-v"${VERSION}"-*.tar.gz esptool-v"${VERSION}"-*.zip; do + [ -f "$archive" ] || continue + + echo "Updating JSON for $archive" + + # Determine host from archive name + case "$archive" in + *linux-amd64*) host="x86_64-pc-linux-gnu" ;; + *linux-armv7*) host="arm-linux-gnueabihf" ;; + *linux-aarch64*) host="aarch64-linux-gnu" ;; + *macos-amd64*) host="x86_64-apple-darwin" ;; + *macos-arm64*) host="arm64-apple-darwin" ;; + *win64*) hosts=("x86_64-mingw32" "i686-mingw32") ;; + *) echo "Unknown host for $archive"; continue ;; + esac + + # For win64, loop over both hosts; otherwise, use a single host + if [[ "$archive" == *win64* ]]; then + for host in "${hosts[@]}"; do + update_json_for_host "$host" "$archive" + done + else + update_json_for_host "$host" "$archive" + fi +done + +# After all archives are processed, move the temporary JSON to the final file +mv "$tmp_json" "$JSON_PATH" diff --git a/package/package_esp32_index.template.json b/package/package_esp32_index.template.json index 14aa8e9b4cf..22d3cc05455 100644 --- a/package/package_esp32_index.template.json +++ b/package/package_esp32_index.template.json @@ -81,7 +81,7 @@ { "packager": "esp32", "name": "esptool_py", - "version": "4.9.dev3" + "version": "5.0.dev1" }, { "packager": "esp32", @@ -469,56 +469,56 @@ }, { "name": "esptool_py", - "version": "4.9.dev3", + "version": "5.0.dev1", "systems": [ { - "host": "x86_64-pc-linux-gnu", - "url": "https://github.com/espressif/arduino-esp32/releases/download/3.1.0-RC3/esptool-v4.9.dev3-linux-amd64.tar.gz", - "archiveFileName": "esptool-v4.9.dev3-linux-amd64.tar.gz", - "checksum": "SHA-256:4ecaf51836cbf4ea3c19840018bfef3b0b8cd8fc3c95f6e1e043ca5bbeab9bf0", - "size": "64958202" + "host": "aarch64-linux-gnu", + "url": "https://github.com/espressif/arduino-esp32/releases/download/3.2.0/esptool-v5.0.dev1-linux-aarch64.tar.gz", + "archiveFileName": "esptool-v5.0.dev1-linux-aarch64.tar.gz", + "checksum": "SHA-256:bfafa7a7723ebbabfd8b6e3ca5ae00bfead0331de923754aeddb43b2c116a078", + "size": "58241736" }, { - "host": "arm-linux-gnueabihf", - "url": "https://github.com/espressif/arduino-esp32/releases/download/3.1.0-RC3/esptool-v4.9.dev3-linux-armv7.tar.gz", - "archiveFileName": "esptool-v4.9.dev3-linux-armv7.tar.gz", - "checksum": "SHA-256:fff818573bce483ee793ac83c8211f6abf764aa3350f198228859f696a0a0b36", - "size": "31530030" + "host": "x86_64-pc-linux-gnu", + "url": "https://github.com/espressif/arduino-esp32/releases/download/3.2.0/esptool-v5.0.dev1-linux-amd64.tar.gz", + "archiveFileName": "esptool-v5.0.dev1-linux-amd64.tar.gz", + "checksum": "SHA-256:acd0486e96586b99d053a1479acbbbfcae8667227c831cdc53a171f9ccfa27ee", + "size": "100740042" }, { - "host": "aarch64-linux-gnu", - "url": "https://github.com/espressif/arduino-esp32/releases/download/3.1.0-RC3/esptool-v4.9.dev3-linux-aarch64.tar.gz", - "archiveFileName": "esptool-v4.9.dev3-linux-aarch64.tar.gz", - "checksum": "SHA-256:5b274bdff2f62e6a07c3c1dfa51b1128924621f661747eca3dbe0f77972f2f06", - "size": "33663882" + "host": "arm-linux-gnueabihf", + "url": "https://github.com/espressif/arduino-esp32/releases/download/3.2.0/esptool-v5.0.dev1-linux-armv7.tar.gz", + "archiveFileName": "esptool-v5.0.dev1-linux-armv7.tar.gz", + "checksum": "SHA-256:ea77a38681506761bbb7b0b39c130811ed565667b67ebbdb4d6dcc6cb6e07368", + "size": "53451939" }, { "host": "x86_64-apple-darwin", - "url": "https://github.com/espressif/arduino-esp32/releases/download/3.1.0-RC3/esptool-v4.9.dev3-macos-amd64.tar.gz", - "archiveFileName": "esptool-v4.9.dev3-macos-amd64.tar.gz", - "checksum": "SHA-256:c733c83b58fcf5f642fbb2fddb8ff24640c2c785126cba0821fb70c4a5ceea7a", - "size": "32767836" + "url": "https://github.com/espressif/arduino-esp32/releases/download/3.2.0/esptool-v5.0.dev1-macos-amd64.tar.gz", + "archiveFileName": "esptool-v5.0.dev1-macos-amd64.tar.gz", + "checksum": "SHA-256:900a8e90731208bee96647e0e207a43612b9452c2120c4fdc0ff4c6be226257b", + "size": "59631998" }, { "host": "arm64-apple-darwin", - "url": "https://github.com/espressif/arduino-esp32/releases/download/3.1.0-RC3/esptool-v4.9.dev3-macos-arm64.tar.gz", - "archiveFileName": "esptool-v4.9.dev3-macos-arm64.tar.gz", - "checksum": "SHA-256:83c195a15981e6a5e7a130db2ccfb21e2d8093912e5b003681f9a5abadd71af7", - "size": "30121441" + "url": "https://github.com/espressif/arduino-esp32/releases/download/3.2.0/esptool-v5.0.dev1-macos-arm64.tar.gz", + "archiveFileName": "esptool-v5.0.dev1-macos-arm64.tar.gz", + "checksum": "SHA-256:3653f4de73cb4fc6a25351eaf663708e91c65ae3265d75bd54ca4315a4350bb4", + "size": "56349992" }, { - "host": "i686-mingw32", - "url": "https://github.com/espressif/arduino-esp32/releases/download/3.1.0-RC3/esptool-v4.9.dev3-win64.zip", - "archiveFileName": "esptool-v4.9.dev3-win64.zip", - "checksum": "SHA-256:890051a4fdc684ff6f4af18d0bb27d274ca940ee0eef716a9455f8c64b25b215", - "size": "36072564" + "host": "x86_64-mingw32", + "url": "https://github.com/espressif/arduino-esp32/releases/download/3.2.0/esptool-v5.0.dev1-win64.zip", + "archiveFileName": "esptool-v5.0.dev1-win64.zip", + "checksum": "SHA-256:1e8fd89645daf94f2d4406ec73c9004e617ea921079515f9fd749205eece4d6d", + "size": "59102658" }, { - "host": "x86_64-mingw32", - "url": "https://github.com/espressif/arduino-esp32/releases/download/3.1.0-RC3/esptool-v4.9.dev3-win64.zip", - "archiveFileName": "esptool-v4.9.dev3-win64.zip", - "checksum": "SHA-256:890051a4fdc684ff6f4af18d0bb27d274ca940ee0eef716a9455f8c64b25b215", - "size": "36072564" + "host": "i686-mingw32", + "url": "https://github.com/espressif/arduino-esp32/releases/download/3.2.0/esptool-v5.0.dev1-win64.zip", + "archiveFileName": "esptool-v5.0.dev1-win64.zip", + "checksum": "SHA-256:1e8fd89645daf94f2d4406ec73c9004e617ea921079515f9fd749205eece4d6d", + "size": "59102658" } ] }, diff --git a/platform.txt b/platform.txt index 65be05b3bf4..8c7f4432377 100644 --- a/platform.txt +++ b/platform.txt @@ -119,7 +119,7 @@ recipe.hooks.prebuild.2.pattern.windows=cmd /c if not exist "{build.path}\partit recipe.hooks.prebuild.3.pattern.windows=cmd /c if not exist "{build.path}\partitions.csv" COPY "{runtime.platform.path}\tools\partitions\{build.partitions}.csv" "{build.path}\partitions.csv" # Check if custom bootloader exist: source > variant > build.boot -recipe.hooks.prebuild.4.pattern_args=--chip {build.mcu} elf2image --flash_mode {build.flash_mode} --flash_freq {build.img_freq} --flash_size {build.flash_size} -o +recipe.hooks.prebuild.4.pattern_args=--chip {build.mcu} elf2image --flash-mode {build.flash_mode} --flash-freq {build.img_freq} --flash-size {build.flash_size} -o recipe.hooks.prebuild.4.pattern=/usr/bin/env bash -c "[ -f "{build.source.path}"/bootloader.bin ] && cp -f "{build.source.path}"/bootloader.bin "{build.path}"/{build.project_name}.bootloader.bin || ( [ -f "{build.variant.path}"/{build.custom_bootloader}.bin ] && cp "{build.variant.path}"/{build.custom_bootloader}.bin "{build.path}"/{build.project_name}.bootloader.bin || "{tools.esptool_py.path}"/{tools.esptool_py.cmd} {recipe.hooks.prebuild.4.pattern_args} "{build.path}"/{build.project_name}.bootloader.bin "{compiler.sdk.path}"/bin/bootloader_{build.boot}_{build.boot_freq}.elf )" recipe.hooks.prebuild.4.pattern.windows=cmd /c IF EXIST "{build.source.path}\bootloader.bin" ( COPY /y "{build.source.path}\bootloader.bin" "{build.path}\{build.project_name}.bootloader.bin" ) ELSE ( IF EXIST "{build.variant.path}\{build.custom_bootloader}.bin" ( COPY "{build.variant.path}\{build.custom_bootloader}.bin" "{build.path}\{build.project_name}.bootloader.bin" ) ELSE ( "{tools.esptool_py.path}\{tools.esptool_py.cmd}" {recipe.hooks.prebuild.4.pattern_args} "{build.path}\{build.project_name}.bootloader.bin" "{compiler.sdk.path}\bin\bootloader_{build.boot}_{build.boot_freq}.elf" ) ) @@ -163,7 +163,7 @@ recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.f recipe.objcopy.partitions.bin.pattern={tools.gen_esp32part.cmd} -q "{build.path}/partitions.csv" "{build.path}/{build.project_name}.partitions.bin" ## Create bin -recipe.objcopy.bin.pattern_args=--chip {build.mcu} elf2image --flash_mode "{build.flash_mode}" --flash_freq "{build.img_freq}" --flash_size "{build.flash_size}" --elf-sha256-offset 0xb0 -o "{build.path}/{build.project_name}.bin" "{build.path}/{build.project_name}.elf" +recipe.objcopy.bin.pattern_args=--chip {build.mcu} elf2image --flash-mode "{build.flash_mode}" --flash-freq "{build.img_freq}" --flash-size "{build.flash_size}" --elf-sha256-offset 0xb0 -o "{build.path}/{build.project_name}.bin" "{build.path}/{build.project_name}.elf" recipe.objcopy.bin.pattern="{tools.esptool_py.path}/{tools.esptool_py.cmd}" {recipe.objcopy.bin.pattern_args} ## Create Insights Firmware Package @@ -176,7 +176,7 @@ recipe.hooks.objcopy.postobjcopy.2.pattern=/usr/bin/env bash -c "[ ! -d "{build. recipe.hooks.objcopy.postobjcopy.2.pattern.windows=cmd /c if exist "{build.path}\libraries\ESP_SR" if exist "{compiler.sdk.path}\esp_sr\srmodels.bin" COPY /y "{compiler.sdk.path}\esp_sr\srmodels.bin" "{build.path}\srmodels.bin" # Create merged binary -recipe.hooks.objcopy.postobjcopy.3.pattern_args=--chip {build.mcu} merge_bin -o "{build.path}/{build.project_name}.merged.bin" --fill-flash-size {build.flash_size} --flash_mode keep --flash_freq keep --flash_size keep {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0x10000 "{build.path}/{build.project_name}.bin" +recipe.hooks.objcopy.postobjcopy.3.pattern_args=--chip {build.mcu} merge-bin -o "{build.path}/{build.project_name}.merged.bin" --pad-to-size {build.flash_size} --flash-mode keep --flash-freq keep --flash-size keep {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0x10000 "{build.path}/{build.project_name}.bin" recipe.hooks.objcopy.postobjcopy.3.pattern="{tools.esptool_py.path}/{tools.esptool_py.cmd}" {recipe.hooks.objcopy.postobjcopy.3.pattern_args} ## Save bin @@ -285,14 +285,14 @@ debug.additional_config=debug_config.{build.mcu} tools.esptool_py.upload.protocol=serial tools.esptool_py.upload.params.verbose= tools.esptool_py.upload.params.quiet= -tools.esptool_py.upload.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default_reset --after hard_reset write_flash {upload.erase_cmd} -z --flash_mode keep --flash_freq keep --flash_size keep {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0x10000 "{build.path}/{build.project_name}.bin" {upload.extra_flags} +tools.esptool_py.upload.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default-reset --after hard-reset write-flash {upload.erase_cmd} -z --flash-mode keep --flash-freq keep --flash-size keep {build.bootloader_addr} "{build.path}/{build.project_name}.bootloader.bin" 0x8000 "{build.path}/{build.project_name}.partitions.bin" 0xe000 "{runtime.platform.path}/tools/partitions/boot_app0.bin" 0x10000 "{build.path}/{build.project_name}.bin" {upload.extra_flags} tools.esptool_py.upload.pattern="{path}/{cmd}" {upload.pattern_args} ## Program Application ## ------------------- tools.esptool_py.program.params.verbose= tools.esptool_py.program.params.quiet= -tools.esptool_py.program.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default_reset --after hard_reset write_flash -z --flash_mode keep --flash_freq keep --flash_size keep 0x10000 "{build.path}/{build.project_name}.bin" +tools.esptool_py.program.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default-reset --after hard-reset write-flash -z --flash-mode keep --flash-freq keep --flash-size keep 0x10000 "{build.path}/{build.project_name}.bin" tools.esptool_py.program.pattern="{path}/{cmd}" {program.pattern_args} ## Erase Chip (before burning the bootloader) @@ -300,7 +300,7 @@ tools.esptool_py.program.pattern="{path}/{cmd}" {program.pattern_args} tools.esptool_py.erase.protocol=serial tools.esptool_py.erase.params.verbose= tools.esptool_py.erase.params.quiet= -tools.esptool_py.erase.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default_reset --after hard_reset erase_flash +tools.esptool_py.erase.pattern_args=--chip {build.mcu} --port "{serial.port}" --baud {upload.speed} {upload.flags} --before default-reset --after hard-reset erase-flash tools.esptool_py.erase.pattern="{path}/{cmd}" {erase.pattern_args} ## Burn Bootloader From 38c8e98b84a55792f0b769e3055a79da339ebe32 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Fri, 20 Jun 2025 11:25:03 +0200 Subject: [PATCH 09/13] changes for updated esptool.py v5 (#11488) --- tools/pioarduino-build.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/pioarduino-build.py b/tools/pioarduino-build.py index 3335a716888..b6a0c0435c5 100644 --- a/tools/pioarduino-build.py +++ b/tools/pioarduino-build.py @@ -99,11 +99,11 @@ def generate_bootloader_image(bootloader_elf): "--chip", build_mcu, "elf2image", - "--flash_mode", + "--flash-mode", "${__get_board_flash_mode(__env__)}", - "--flash_freq", + "--flash-freq", "${__get_board_f_image(__env__)}", - "--flash_size", + "--flash-size", board_config.get("upload.flash_size", "4MB"), "-o", "$TARGET", From ddc27de96388d1746fd69fd903d0460b597f6f81 Mon Sep 17 00:00:00 2001 From: Olaf Date: Fri, 20 Jun 2025 11:52:21 +0200 Subject: [PATCH 10/13] Proper EDNS handling and cleaner NOERROR response in DNSSERVER (#11411) * Proper EDNS handling and cleaner NOERROR response * fix: library.properties reverting version number update - as it is done automatically * ci(pre-commit): Apply automatic fixes * Spelling Corrected and minor clarification in comments * Removing commented out code fragments * ci(pre-commit): Apply automatic fixes * fix(pr): Fix typo --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Me No Dev --- libraries/DNSServer/src/DNSServer.cpp | 86 +++++++++++++++++++++++++-- libraries/DNSServer/src/DNSServer.h | 22 +++++++ 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/libraries/DNSServer/src/DNSServer.cpp b/libraries/DNSServer/src/DNSServer.cpp index 28cf89d6ede..1f74c96c733 100644 --- a/libraries/DNSServer/src/DNSServer.cpp +++ b/libraries/DNSServer/src/DNSServer.cpp @@ -111,16 +111,22 @@ void DNSServer::_handleUDP(AsyncUDPPacket &pkt) { // will reply with IP only to "*" or if domain matches without www. subdomain if (dnsHeader.OPCode == DNS_OPCODE_QUERY && requestIncludesOnlyOneQuestion(dnsHeader) && (_domainName.isEmpty() || getDomainNameWithoutWwwPrefix(static_cast(dnsQuestion.QName), dnsQuestion.QNameLength) == _domainName)) { - replyWithIP(pkt, dnsHeader, dnsQuestion); + + // Qtype = A (1) or ANY (255): send an A record otherwise an empty response + if (ntohs(dnsQuestion.QType) == 1 || ntohs(dnsQuestion.QType) == 255) { + replyWithIP(pkt, dnsHeader, dnsQuestion); + } else { + replyWithNoAnsw(pkt, dnsHeader, dnsQuestion); + } return; } - // otherwise reply with custom code replyWithCustomCode(pkt, dnsHeader); } bool DNSServer::requestIncludesOnlyOneQuestion(DNSHeader &dnsHeader) { - return ntohs(dnsHeader.QDCount) == 1 && dnsHeader.ANCount == 0 && dnsHeader.NSCount == 0 && dnsHeader.ARCount == 0; + dnsHeader.ARCount = 0; // We assume that if ARCount !=0 there is a EDNS OPT packet, just ignore + return ntohs(dnsHeader.QDCount) == 1 && dnsHeader.ANCount == 0 && dnsHeader.NSCount == 0; } String DNSServer::getDomainNameWithoutWwwPrefix(const unsigned char *start, size_t len) { @@ -139,7 +145,6 @@ String DNSServer::getDomainNameWithoutWwwPrefix(const unsigned char *start, size void DNSServer::replyWithIP(AsyncUDPPacket &req, DNSHeader &dnsHeader, DNSQuestion &dnsQuestion) { AsyncUDPMessage rpl; - // Change the type of message to a response and set the number of answers equal to // the number of questions in the header dnsHeader.QR = DNS_QR_RESPONSE; @@ -187,3 +192,76 @@ void DNSServer::replyWithCustomCode(AsyncUDPPacket &req, DNSHeader &dnsHeader) { rpl.write(reinterpret_cast(&dnsHeader), sizeof(DNSHeader)); _udp.sendTo(rpl, req.remoteIP(), req.remotePort()); } + +void DNSServer::replyWithNoAnsw(AsyncUDPPacket &req, DNSHeader &dnsHeader, DNSQuestion &dnsQuestion) { + + dnsHeader.QR = DNS_QR_RESPONSE; + dnsHeader.ANCount = 0; + dnsHeader.NSCount = htons(1); + + AsyncUDPMessage rpl; + rpl.write(reinterpret_cast(&dnsHeader), sizeof(DNSHeader)); + + // Write the question + rpl.write(dnsQuestion.QName, dnsQuestion.QNameLength); + rpl.write((uint8_t *)&dnsQuestion.QType, 2); + rpl.write((uint8_t *)&dnsQuestion.QClass, 2); + + // An empty answer contains an authority section with a SOA, + // We take the name of the query as the root of the zone for which the SOA is generated + // and use a value of DNS_MINIMAL_TTL seconds in order to minimize negative caching + // Write the authority section: + // The SOA RR's ownername is set equal to the query name, and we use made up names for + // the MNAME and RNAME - it doesn't really matter from a protocol perspective - as for + // a no such QTYPE answer only the timing fields are used. + // a protocol perspective - it + // Use DNS name compression : instead of repeating the name in this RNAME occurrence, + // set the two MSB of the byte corresponding normally to the length to 1. The following + // 14 bits must be used to specify the offset of the domain name in the message + // (<255 here so the first byte has the 6 LSB at 0) + rpl.write((uint8_t)0xC0); + rpl.write((uint8_t)DNS_OFFSET_DOMAIN_NAME); + + // DNS type A : host address, DNS class IN for INternet, returning an IPv4 address + uint16_t answerType = htons(DNS_TYPE_SOA), answerClass = htons(DNS_CLASS_IN); + uint32_t Serial = htonl(DNS_SOA_SERIAL); // Date type serial based on the date this piece of code was written + uint32_t Refresh = htonl(DNS_SOA_REFRESH); // These timers don't matter, we don't serve zone transfers + uint32_t Retry = htonl(DNS_SOA_RETRY); + uint32_t Expire = htonl(DNS_SOA_EXPIRE); + uint32_t MinTTL = htonl(DNS_MINIMAL_TTL); // See RFC2308 section 5 + char MLabel[] = DNS_SOA_MNAME_LABEL; + char RLabel[] = DNS_SOA_RNAME_LABEL; + char PostFixLabel[] = DNS_SOA_POSTFIX_LABEL; + + // 4 accounts for len fields and for both rname + // and lname and their postfix labels and there are 5 32 bit fields + + uint16_t RdataLength = htons((uint16_t)(strlen(MLabel) + strlen(RLabel) + 2 * strlen(PostFixLabel) + 4 + 5 * sizeof(Serial))); + + rpl.write((unsigned char *)&answerType, 2); + rpl.write((unsigned char *)&answerClass, 2); + rpl.write((unsigned char *)&MinTTL, 4); // DNS Time To Live + + rpl.write((unsigned char *)&RdataLength, 2); + + rpl.write((uint8_t)strlen(MLabel)); + rpl.write((unsigned char *)&MLabel, strlen(MLabel)); + + rpl.write((unsigned char *)&PostFixLabel, strlen(PostFixLabel)); + rpl.write((uint8_t)0); + // rpl.write((uint8_t)0xC0); + // rpl.write((uint8_t)DNS_OFFSET_DOMAIN_NAME); + + rpl.write((uint8_t)strlen(RLabel)); + rpl.write((unsigned char *)&RLabel, strlen(RLabel)); + rpl.write((unsigned char *)&PostFixLabel, strlen(PostFixLabel)); + rpl.write((uint8_t)0); + + rpl.write((unsigned char *)&Serial, 4); + rpl.write((unsigned char *)&Refresh, 4); + rpl.write((unsigned char *)&Retry, 4); + rpl.write((unsigned char *)&Expire, 4); + rpl.write((unsigned char *)&MinTTL, 4); + + _udp.sendTo(rpl, req.remoteIP(), req.remotePort()); +} diff --git a/libraries/DNSServer/src/DNSServer.h b/libraries/DNSServer/src/DNSServer.h index dfd9a45604d..f2716defc7d 100644 --- a/libraries/DNSServer/src/DNSServer.h +++ b/libraries/DNSServer/src/DNSServer.h @@ -9,6 +9,26 @@ #define DNS_OFFSET_DOMAIN_NAME DNS_HEADER_SIZE // Offset in bytes to reach the domain name labels in the DNS message #define DNS_DEFAULT_PORT 53 +#define DNS_SOA_MNAME_LABEL "ns" +#define DNS_SOA_RNAME_LABEL "esp32" +// The POSTFIX_LABEL will be concatenated to the RName and MName Label label +// do not use a multilabel name here. "local" is a good choice as it is reserved for +// local use by IANA +// The postfix label is defined as an array of characters that follows the +// definition of RFC1035 3.1 +// for instance, a postfix of example.com would be defined as: +// #define DNS_SOA_POSTFIX_LABEL {'\7', 'e', 'x', 'a', 'm', 'p', 'l', 'e', '\3', 'c', 'o', 'm', '\0'} +#define DNS_SOA_POSTFIX_LABEL \ + { '\5', 'l', 'o', 'c', 'a', 'l', '\0' } +// From the following values only the MINIMAL_TTL has relevance +// in the context of client-server protocol interactions. +// The other values are arbitrary chosen as they are only relevant for +// in a zone-transfer scenario. +#define DNS_SOA_SERIAL 2025052900 // Arbitrary serial (format: YYYYMMDDnn) +#define DNS_SOA_REFRESH 100000 // Arbitrary (seconds) +#define DNS_SOA_RETRY 10000 // Arbitrary (seconds) +#define DNS_SOA_EXPIRE 1000000 // Arbitrary (seconds) +#define DNS_MINIMAL_TTL 5 // Time to live for negative answers RFC2308 enum class DNSReplyCode : uint16_t { NoError = 0, FormError = 1, @@ -179,5 +199,7 @@ class DNSServer { inline bool requestIncludesOnlyOneQuestion(DNSHeader &dnsHeader); void replyWithIP(AsyncUDPPacket &req, DNSHeader &dnsHeader, DNSQuestion &dnsQuestion); inline void replyWithCustomCode(AsyncUDPPacket &req, DNSHeader &dnsHeader); + inline void replyWithNoAnsw(AsyncUDPPacket &req, DNSHeader &dnsHeader, DNSQuestion &dnsQuestion); + void _handleUDP(AsyncUDPPacket &pkt); }; From 11f46081fcb17a61dfd79dc0858fd5a7e73ee89c Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 20 Jun 2025 15:28:07 -0300 Subject: [PATCH 11/13] feat(matter): Adds Matter Events callback plus example (#11465) * feat(matter): Adds Matter Events callback and related example --- .../examples/MatterEvents/MatterEvents.ino | 168 ++++++++++++++++++ .../Matter/examples/MatterEvents/ci.json | 7 + libraries/Matter/keywords.txt | 39 +++- libraries/Matter/src/Matter.cpp | 37 ++-- libraries/Matter/src/Matter.h | 126 +++++++++++++ libraries/Matter/src/MatterEndPoint.h | 20 +-- 6 files changed, 369 insertions(+), 28 deletions(-) create mode 100644 libraries/Matter/examples/MatterEvents/MatterEvents.ino create mode 100644 libraries/Matter/examples/MatterEvents/ci.json diff --git a/libraries/Matter/examples/MatterEvents/MatterEvents.ino b/libraries/Matter/examples/MatterEvents/MatterEvents.ino new file mode 100644 index 00000000000..dac599bf9fa --- /dev/null +++ b/libraries/Matter/examples/MatterEvents/MatterEvents.ino @@ -0,0 +1,168 @@ +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Matter Manager +#include +#include + +// WiFi is manually set and started +const char *ssid = "your-ssid"; // Change this to your WiFi SSID +const char *password = "your-password"; // Change this to your WiFi password + +// List of Matter Endpoints for this Node +// On/Off Light Endpoint +MatterOnOffLight OnOffLight; + +// This function is called when a Matter event occurs +void onMatterEvent(matterEvent_t eventType, const chip::DeviceLayer::ChipDeviceEvent *eventInfo) { + // Print the event type to Serial + Serial.print("===> Got a Matter Event: "); + switch (eventType) { + case MATTER_WIFI_CONNECTIVITY_CHANGE: Serial.println("WiFi Connectivity Change"); break; + case MATTER_THREAD_CONNECTIVITY_CHANGE: Serial.println("Thread Connectivity Change"); break; + case MATTER_INTERNET_CONNECTIVITY_CHANGE: + { + bool newIPAddress = false; + Serial.print("Internet Connectivity Change :: "); + if (eventInfo->InternetConnectivityChange.IPv4 != chip::DeviceLayer::ConnectivityChange::kConnectivity_NoChange) { + Serial.print("IPv4 Connectivity: "); + switch (eventInfo->InternetConnectivityChange.IPv4) { + case chip::DeviceLayer::ConnectivityChange::kConnectivity_Established: + { + newIPAddress = true; + break; + } + case chip::DeviceLayer::ConnectivityChange::kConnectivity_Lost: Serial.println("Lost"); break; + default: Serial.println("Unknown"); break; + } + } + if (eventInfo->InternetConnectivityChange.IPv6 != chip::DeviceLayer::ConnectivityChange::kConnectivity_NoChange) { + Serial.print("IPv6 Connectivity: "); + switch (eventInfo->InternetConnectivityChange.IPv6) { + case chip::DeviceLayer::ConnectivityChange::kConnectivity_Established: + { + newIPAddress = true; + break; + } + case chip::DeviceLayer::ConnectivityChange::kConnectivity_Lost: Serial.println("Lost"); break; + default: Serial.println("Unknown"); break; + } + } + // Print the IP address if it was established + if (newIPAddress) { + Serial.print("Established - IP Address: "); + char ipAddressStr[chip::Transport::PeerAddress::kMaxToStringSize]; + eventInfo->InternetConnectivityChange.ipAddress.ToString(ipAddressStr); + Serial.println(ipAddressStr); + } + break; + } + case MATTER_SERVICE_CONNECTIVITY_CHANGE: Serial.println("Service Connectivity Change"); break; + case MATTER_SERVICE_PROVISIONING_CHANGE: Serial.println("Service Provisioning Change"); break; + case MATTER_TIME_SYNC_CHANGE: Serial.println("Time Sync Change"); break; + case MATTER_CHIPOBLE_CONNECTION_ESTABLISHED: Serial.println("CHIPoBLE Connection Established"); break; + case MATTER_CHIPOBLE_CONNECTION_CLOSED: Serial.println("CHIPoBLE Connection Closed"); break; + case MATTER_CLOSE_ALL_BLE_CONNECTIONS: Serial.println("Close All BLE Connections"); break; + case MATTER_WIFI_DEVICE_AVAILABLE: Serial.println("WiFi Device Available"); break; + case MATTER_OPERATIONAL_NETWORK_STARTED: Serial.println("Operational Network Started"); break; + case MATTER_THREAD_STATE_CHANGE: Serial.println("Thread State Change"); break; + case MATTER_THREAD_INTERFACE_STATE_CHANGE: Serial.println("Thread Interface State Change"); break; + case MATTER_CHIPOBLE_ADVERTISING_CHANGE: Serial.println("CHIPoBLE Advertising Change"); break; + case MATTER_INTERFACE_IP_ADDRESS_CHANGED: + switch (eventInfo->InterfaceIpAddressChanged.Type) { + case chip::DeviceLayer::InterfaceIpChangeType::kIpV4_Assigned: Serial.println("IPv4 Address Assigned"); break; + case chip::DeviceLayer::InterfaceIpChangeType::kIpV4_Lost: Serial.println("IPv4 Address Lost"); break; + case chip::DeviceLayer::InterfaceIpChangeType::kIpV6_Assigned: Serial.println("IPv6 Address Assigned"); break; + case chip::DeviceLayer::InterfaceIpChangeType::kIpV6_Lost: Serial.println("IPv6 Address Lost"); break; + } + break; + case MATTER_COMMISSIONING_COMPLETE: Serial.println("Commissioning Complete"); break; + case MATTER_FAIL_SAFE_TIMER_EXPIRED: Serial.println("Fail Safe Timer Expired"); break; + case MATTER_OPERATIONAL_NETWORK_ENABLED: Serial.println("Operational Network Enabled"); break; + case MATTER_DNSSD_INITIALIZED: Serial.println("DNS-SD Initialized"); break; + case MATTER_DNSSD_RESTART_NEEDED: Serial.println("DNS-SD Restart Needed"); break; + case MATTER_BINDINGS_CHANGED_VIA_CLUSTER: Serial.println("Bindings Changed Via Cluster"); break; + case MATTER_OTA_STATE_CHANGED: Serial.println("OTA State Changed"); break; + case MATTER_SERVER_READY: Serial.println("Server Ready"); break; + case MATTER_BLE_DEINITIALIZED: Serial.println("BLE Deinitialized"); break; + case MATTER_COMMISSIONING_SESSION_STARTED: Serial.println("Commissioning Session Started"); break; + case MATTER_COMMISSIONING_SESSION_STOPPED: Serial.println("Commissioning Session Stopped"); break; + case MATTER_COMMISSIONING_WINDOW_OPEN: Serial.println("Commissioning Window Opened"); break; + case MATTER_COMMISSIONING_WINDOW_CLOSED: Serial.println("Commissioning Window Closed"); break; + case MATTER_FABRIC_WILL_BE_REMOVED: Serial.println("Fabric Will Be Removed"); break; + case MATTER_FABRIC_REMOVED: Serial.println("Fabric Removed"); break; + case MATTER_FABRIC_COMMITTED: Serial.println("Fabric Committed"); break; + case MATTER_FABRIC_UPDATED: Serial.println("Fabric Updated"); break; + case MATTER_ESP32_SPECIFIC_EVENT: Serial.println("Sending ESP32 Platform Specific Events"); break; + case MATTER_ESP32_PUBLIC_SPECIFIC_EVENT: Serial.println("Next Event Has Populated EventInfo"); break; + default: + // If the event type is not recognized, print "Unknown" and the event ID + Serial.println("Unknown, EventID = 0x" + String(eventType, HEX)); + break; + } +} + +void setup() { + Serial.begin(115200); + while (!Serial) { + delay(10); // Wait for Serial to initialize + } + + // We start by connecting to a WiFi network + Serial.print("Connecting to "); + Serial.println(ssid); + // Manually connect to WiFi + WiFi.enableIPv6(true); // Enable IPv6 if needed + WiFi.begin(ssid, password); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("\r\nWiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + delay(500); + + // Initialize at least one Matter EndPoint + OnOffLight.begin(); + + // Set the Matter Event Callback + Matter.onEvent(onMatterEvent); + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + Serial.println("Starting Matter Commission Test..."); +} + +void loop() { + // Check Matter Commissioning state + if (!Matter.isDeviceCommissioned()) { + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Light Commissioning. + while (!Matter.isDeviceCommissioned()) { + delay(5000); + Serial.println("Matter Fabric not commissioned yet. Waiting for commissioning."); + } + } + Serial.println("Matter Node is commissioned and connected to Wi-Fi."); + Serial.println("====> Decommissioning in 60 seconds. <===="); + delay(60000); + Matter.decommission(); + Serial.println("Matter Node is decommissioned. Commissioning widget shall start over."); +} diff --git a/libraries/Matter/examples/MatterEvents/ci.json b/libraries/Matter/examples/MatterEvents/ci.json new file mode 100644 index 00000000000..556a8a9ee6b --- /dev/null +++ b/libraries/Matter/examples/MatterEvents/ci.json @@ -0,0 +1,7 @@ +{ + "fqbn_append": "PartitionScheme=huge_app", + "requires": [ + "CONFIG_SOC_WIFI_SUPPORTED=y", + "CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y" + ] +} diff --git a/libraries/Matter/keywords.txt b/libraries/Matter/keywords.txt index a63d9a65acb..68aaebb1d4d 100644 --- a/libraries/Matter/keywords.txt +++ b/libraries/Matter/keywords.txt @@ -36,6 +36,8 @@ EndPointSpeedCB KEYWORD1 EndPointOnOffCB KEYWORD1 EndPointBrightnessCB KEYWORD1 EndPointRGBColorCB KEYWORD1 +matterEvent_t KEYWORD1 +matterEventCB KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -108,6 +110,7 @@ onChangeMode KEYWORD2 onChangeLocalTemperature KEYWORD2 onChangeCoolingSetpoint KEYWORD2 onChangeHeatingSetpoint KEYWORD2 +onEvent KEYWORD2 ####################################### # Constants (LITERAL1) @@ -144,5 +147,37 @@ THERMOSTAT_MODE_OFF LITERAL1 THERMOSTAT_MODE_AUTO LITERAL1 THERMOSTAT_MODE_COOL LITERAL1 THERMOSTAT_MODE_HEAT LITERAL1 -THERMOSTAT_AUTO_MODE_DISABLED LITERAL1 -THERMOSTAT_AUTO_MODE_ENABLED LITERAL1 +MATTER_WIFI_CONNECTIVITY_CHANGE LITERAL1 +MATTER_THREAD_CONNECTIVITY_CHANGE LITERAL1 +MATTER_INTERNET_CONNECTIVITY_CHANGE LITERAL1 +MATTER_SERVICE_CONNECTIVITY_CHANGE LITERAL1 +MATTER_SERVICE_PROVISIONING_CHANGE LITERAL1 +MATTER_TIME_SYNC_CHANGE LITERAL1 +MATTER_CHIPOBLE_CONNECTION_ESTABLISHED LITERAL1 +MATTER_CHIPOBLE_CONNECTION_CLOSED LITERAL1 +MATTER_CLOSE_ALL_BLE_CONNECTIONS LITERAL1 +MATTER_WIFI_DEVICE_AVAILABLE LITERAL1 +MATTER_OPERATIONAL_NETWORK_STARTED LITERAL1 +MATTER_THREAD_STATE_CHANGE LITERAL1 +MATTER_THREAD_INTERFACE_STATE_CHANGE LITERAL1 +MATTER_CHIPOBLE_ADVERTISING_CHANGE LITERAL1 +MATTER_INTERFACE_IP_ADDRESS_CHANGED LITERAL1 +MATTER_COMMISSIONING_COMPLETE LITERAL1 +MATTER_FAIL_SAFE_TIMER_EXPIRED LITERAL1 +MATTER_OPERATIONAL_NETWORK_ENABLED LITERAL1 +MATTER_DNSSD_INITIALIZED LITERAL1 +MATTER_DNSSD_RESTART_NEEDED LITERAL1 +MATTER_BINDINGS_CHANGED_VIA_CLUSTER LITERAL1 +MATTER_OTA_STATE_CHANGED LITERAL1 +MATTER_SERVER_READY LITERAL1 +MATTER_BLE_DEINITIALIZED LITERAL1 +MATTER_ESP32_SPECIFIC_EVENT LITERAL1 +MATTER_COMMISSIONING_SESSION_STARTED LITERAL1 +MATTER_COMMISSIONING_SESSION_STOPPED LITERAL1 +MATTER_COMMISSIONING_WINDOW_OPEN LITERAL1 +MATTER_COMMISSIONING_WINDOW_CLOSED LITERAL1 +MATTER_FABRIC_WILL_BE_REMOVED LITERAL1 +MATTER_FABRIC_REMOVED LITERAL1 +MATTER_FABRIC_COMMITTED LITERAL1 +MATTER_FABRIC_UPDATED LITERAL1 +MATTER_ESP32_PUBLIC_SPECIFIC_EVENT LITERAL1 diff --git a/libraries/Matter/src/Matter.cpp b/libraries/Matter/src/Matter.cpp index af7c4c8657e..b16edfd85c1 100644 --- a/libraries/Matter/src/Matter.cpp +++ b/libraries/Matter/src/Matter.cpp @@ -28,7 +28,8 @@ constexpr auto k_timeout_seconds = 300; static bool _matter_has_started = false; static node::config_t node_config; -static node_t *deviceNode = NULL; +static node_t *deviceNode = nullptr; +ArduinoMatter::matterEventCB ArduinoMatter::_matterEventCB = nullptr; // This callback is called for every attribute update. The callback implementation shall // handle the desired attributes and return an appropriate error code. If the attribute @@ -42,7 +43,7 @@ static esp_err_t app_attribute_update_cb( switch (type) { case PRE_UPDATE: // Callback before updating the value in the database log_v("Attribute update callback: PRE_UPDATE"); - if (ep != NULL) { + if (ep != nullptr) { err = ep->attributeChangeCB(endpoint_id, cluster_id, attribute_id, val) ? ESP_OK : ESP_FAIL; } break; @@ -78,7 +79,7 @@ static esp_err_t app_identification_cb(identification::callback_type_t type, uin identifyIsActive = false; log_v("Identification callback: STOP"); } - if (ep != NULL) { + if (ep != nullptr) { err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive) ? ESP_OK : ESP_FAIL; } @@ -89,21 +90,21 @@ static esp_err_t app_identification_cb(identification::callback_type_t type, uin static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) { switch (event->Type) { case chip::DeviceLayer::DeviceEventType::kInterfaceIpAddressChanged: - log_i( + log_d( "Interface %s Address changed", event->InterfaceIpAddressChanged.Type == chip::DeviceLayer::InterfaceIpChangeType::kIpV4_Assigned ? "IPv4" : "IPV6" ); break; - case chip::DeviceLayer::DeviceEventType::kCommissioningComplete: log_i("Commissioning complete"); break; - case chip::DeviceLayer::DeviceEventType::kFailSafeTimerExpired: log_i("Commissioning failed, fail safe timer expired"); break; - case chip::DeviceLayer::DeviceEventType::kCommissioningSessionStarted: log_i("Commissioning session started"); break; - case chip::DeviceLayer::DeviceEventType::kCommissioningSessionStopped: log_i("Commissioning session stopped"); break; - case chip::DeviceLayer::DeviceEventType::kCommissioningWindowOpened: log_i("Commissioning window opened"); break; - case chip::DeviceLayer::DeviceEventType::kCommissioningWindowClosed: log_i("Commissioning window closed"); break; + case chip::DeviceLayer::DeviceEventType::kCommissioningComplete: log_d("Commissioning complete"); break; + case chip::DeviceLayer::DeviceEventType::kFailSafeTimerExpired: log_d("Commissioning failed, fail safe timer expired"); break; + case chip::DeviceLayer::DeviceEventType::kCommissioningSessionStarted: log_d("Commissioning session started"); break; + case chip::DeviceLayer::DeviceEventType::kCommissioningSessionStopped: log_d("Commissioning session stopped"); break; + case chip::DeviceLayer::DeviceEventType::kCommissioningWindowOpened: log_d("Commissioning window opened"); break; + case chip::DeviceLayer::DeviceEventType::kCommissioningWindowClosed: log_d("Commissioning window closed"); break; case chip::DeviceLayer::DeviceEventType::kFabricRemoved: { - log_i("Fabric removed successfully"); + log_d("Fabric removed successfully"); if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0) { - log_i("No fabric left, opening commissioning window"); + log_d("No fabric left, opening commissioning window"); chip::CommissioningWindowManager &commissionMgr = chip::Server::GetInstance().GetCommissioningWindowManager(); constexpr auto kTimeoutSeconds = chip::System::Clock::Seconds16(k_timeout_seconds); if (!commissionMgr.IsCommissioningWindowOpen()) { @@ -116,12 +117,16 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) { } break; } - case chip::DeviceLayer::DeviceEventType::kFabricWillBeRemoved: log_i("Fabric will be removed"); break; - case chip::DeviceLayer::DeviceEventType::kFabricUpdated: log_i("Fabric is updated"); break; - case chip::DeviceLayer::DeviceEventType::kFabricCommitted: log_i("Fabric is committed"); break; - case chip::DeviceLayer::DeviceEventType::kBLEDeinitialized: log_i("BLE deinitialized and memory reclaimed"); break; + case chip::DeviceLayer::DeviceEventType::kFabricWillBeRemoved: log_d("Fabric will be removed"); break; + case chip::DeviceLayer::DeviceEventType::kFabricUpdated: log_d("Fabric is updated"); break; + case chip::DeviceLayer::DeviceEventType::kFabricCommitted: log_d("Fabric is committed"); break; + case chip::DeviceLayer::DeviceEventType::kBLEDeinitialized: log_d("BLE deinitialized and memory reclaimed"); break; default: break; } + // Check if the user-defined callback is set + if (ArduinoMatter::_matterEventCB != nullptr) { + ArduinoMatter::_matterEventCB(static_cast(event->Type), event); + } } void ArduinoMatter::_init() { diff --git a/libraries/Matter/src/Matter.h b/libraries/Matter/src/Matter.h index e54ceb47e5e..682a0498076 100644 --- a/libraries/Matter/src/Matter.h +++ b/libraries/Matter/src/Matter.h @@ -34,10 +34,136 @@ #include #include +// Matter Event types used when there is a user callback for Matter Events +enum matterEvent_t { + // Starting from 0x8000, these events are public and can be used by applications. + // Defined in CHIPDeviceEvent.h + + // WiFi Connectivity Change: Signals a change in connectivity of the device's WiFi station interface. + MATTER_WIFI_CONNECTIVITY_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kWiFiConnectivityChange, + + // Thread Connectivity Change: Signals a change in connectivity of the device's Thread interface. + MATTER_THREAD_CONNECTIVITY_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kThreadConnectivityChange, + + // Internet Connectivity Change: Signals a change in the device's ability to communicate via the Internet. + MATTER_INTERNET_CONNECTIVITY_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kInternetConnectivityChange, + + // Service Connectivity Change: Signals a change in the device's ability to communicate with a chip-enabled service. + MATTER_SERVICE_CONNECTIVITY_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kServiceConnectivityChange, + + // Service Provisioning Change: Signals a change to the device's service provisioning state. + MATTER_SERVICE_PROVISIONING_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kServiceProvisioningChange, + + // Time Sync Change: Signals a change to the device's real time clock synchronization state. + MATTER_TIME_SYNC_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kTimeSyncChange, + + // CHIPoBLE Connection Established: Signals that an external entity has established a new + // CHIPoBLE connection with the device. + MATTER_CHIPOBLE_CONNECTION_ESTABLISHED = (uint16_t)chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionEstablished, + + // CHIPoBLE Connection Closed: Signals that an external entity has closed existing CHIPoBLE + // connection with the device. + MATTER_CHIPOBLE_CONNECTION_CLOSED = (uint16_t)chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionClosed, + + // Request BLE connections to be closed. This is used in the supportsConcurrentConnection = False case. + MATTER_CLOSE_ALL_BLE_CONNECTIONS = (uint16_t)chip::DeviceLayer::DeviceEventType::kCloseAllBleConnections, + + // WiFi Device Available: When supportsConcurrentConnection = False, the ConnectNetwork + // command cannot start until the BLE device is closed and the Operation Network device (e.g. WiFi) has been started. + MATTER_WIFI_DEVICE_AVAILABLE = (uint16_t)chip::DeviceLayer::DeviceEventType::kWiFiDeviceAvailable, + + MATTER_OPERATIONAL_NETWORK_STARTED = (uint16_t)chip::DeviceLayer::DeviceEventType::kOperationalNetworkStarted, + + // Thread State Change: Signals that a state change has occurred in the Thread stack. + MATTER_THREAD_STATE_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kThreadStateChange, + + // Thread Interface State Change: Signals that the state of the Thread network interface has changed. + MATTER_THREAD_INTERFACE_STATE_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kThreadInterfaceStateChange, + + // CHIPoBLE Advertising Change: Signals that the state of CHIPoBLE advertising has changed. + MATTER_CHIPOBLE_ADVERTISING_CHANGE = (uint16_t)chip::DeviceLayer::DeviceEventType::kCHIPoBLEAdvertisingChange, + + // Interface IP Address Changed: IP address availability - either ipv4 or ipv6 + // addresses assigned to the underlying wifi/ethernet interface. + MATTER_INTERFACE_IP_ADDRESS_CHANGED = (uint16_t)chip::DeviceLayer::DeviceEventType::kInterfaceIpAddressChanged, + + // Commissioning Complete: Commissioning has completed by a call to the general + // commissioning cluster command. + MATTER_COMMISSIONING_COMPLETE = (uint16_t)chip::DeviceLayer::DeviceEventType::kCommissioningComplete, + + // Fail Safe Timer Expired: Signals that the fail-safe timer expired before + // the CommissioningComplete command was successfully invoked. + MATTER_FAIL_SAFE_TIMER_EXPIRED = (uint16_t)chip::DeviceLayer::DeviceEventType::kFailSafeTimerExpired, + + // Operational Network Enabled. + MATTER_OPERATIONAL_NETWORK_ENABLED = (uint16_t)chip::DeviceLayer::DeviceEventType::kOperationalNetworkEnabled, + + // DNS-SD Initialized: Signals that DNS-SD has been initialized and is ready to operate. + MATTER_DNSSD_INITIALIZED = (uint16_t)chip::DeviceLayer::DeviceEventType::kDnssdInitialized, + + // DNS-SD Restart Needed: Signals that DNS-SD backend was restarted and services must be published again. + MATTER_DNSSD_RESTART_NEEDED = (uint16_t)chip::DeviceLayer::DeviceEventType::kDnssdRestartNeeded, + + // Bindings Changed Via Cluster: Signals that bindings were updated. + MATTER_BINDINGS_CHANGED_VIA_CLUSTER = (uint16_t)chip::DeviceLayer::DeviceEventType::kBindingsChangedViaCluster, + + // OTA State Changed: Signals that the state of the OTA engine changed. + MATTER_OTA_STATE_CHANGED = (uint16_t)chip::DeviceLayer::DeviceEventType::kOtaStateChanged, + + // Server Ready: Server initialization has completed. Signals that all server components have been initialized + // and the node is ready to establish connections with other nodes. This event can be used to trigger on-boot actions + // that require sending messages to other nodes. + MATTER_SERVER_READY = (uint16_t)chip::DeviceLayer::DeviceEventType::kServerReady, + + // BLE Deinitialized: Signals that BLE stack is deinitialized and memory reclaimed + MATTER_BLE_DEINITIALIZED = (uint16_t)chip::DeviceLayer::DeviceEventType::kBLEDeinitialized, + + // Starting ESP32 Platform Specific Events from 0x9000 + MATTER_ESP32_SPECIFIC_EVENT, // value is previous + 1 + + // Commissioning Session Started: Signals that Commissioning session has started + MATTER_COMMISSIONING_SESSION_STARTED = (uint16_t)chip::DeviceLayer::DeviceEventType::kCommissioningSessionStarted, + + // Commissioning Session Stopped: Signals that Commissioning session has stopped + MATTER_COMMISSIONING_SESSION_STOPPED = (uint16_t)chip::DeviceLayer::DeviceEventType::kCommissioningSessionStopped, + + // Commissioning Window Opened: Signals that Commissioning window is now opened + MATTER_COMMISSIONING_WINDOW_OPEN = (uint16_t)chip::DeviceLayer::DeviceEventType::kCommissioningWindowOpened, + + // Commissioning Window Closed: Signals that Commissioning window is now closed + MATTER_COMMISSIONING_WINDOW_CLOSED = (uint16_t)chip::DeviceLayer::DeviceEventType::kCommissioningWindowClosed, + + // Fabric Will Be Removed: Signals that a fabric is about to be deleted. This allows actions to be taken that need the + // fabric to still be around before we delete it + MATTER_FABRIC_WILL_BE_REMOVED = (uint16_t)chip::DeviceLayer::DeviceEventType::kFabricWillBeRemoved, + + // Fabric Has Been Removed: Signals that a fabric is effectively deleted + MATTER_FABRIC_REMOVED = (uint16_t)chip::DeviceLayer::DeviceEventType::kFabricRemoved, + + // Fabric Has Been Committed: Signals that a fabric in Fabric Table is persisted to storage, by CommitPendingFabricData + MATTER_FABRIC_COMMITTED = (uint16_t)chip::DeviceLayer::DeviceEventType::kFabricCommitted, + + // Fabric Has Been Updated: Signals that operational credentials are changed, which may not be persistent. + // Can be used to affect what is needed for UpdateNOC prior to commit + MATTER_FABRIC_UPDATED = (uint16_t)chip::DeviceLayer::DeviceEventType::kFabricUpdated, + + // ESP32 Matter Events: These are custom ESP32 Matter events as defined in CHIPDevicePlatformEvent.h. + MATTER_ESP32_PUBLIC_SPECIFIC_EVENT = (uint16_t)chip::DeviceLayer::DeviceEventType::kRange_PublicPlatformSpecific, // ESPSystemEvent +}; + using namespace esp_matter; class ArduinoMatter { public: + // Matter Event Callback type + using matterEventCB = std::function; + // Matter Event Callback + static matterEventCB _matterEventCB; + // set the Matter Event Callback + static void onEvent(matterEventCB cb) { + _matterEventCB = cb; + } + static inline String getManualPairingCode() { // return the pairing code for manual pairing return String("34970112332"); diff --git a/libraries/Matter/src/MatterEndPoint.h b/libraries/Matter/src/MatterEndPoint.h index 5baa4747d18..95d3d3c08df 100644 --- a/libraries/Matter/src/MatterEndPoint.h +++ b/libraries/Matter/src/MatterEndPoint.h @@ -41,22 +41,22 @@ class MatterEndPoint { esp_matter::attribute_t *getAttribute(uint32_t cluster_id, uint32_t attribute_id) { if (endpoint_id == 0) { log_e("Endpoint ID is not set"); - return NULL; + return nullptr; } endpoint_t *endpoint = endpoint::get(node::get(), endpoint_id); - if (endpoint == NULL) { + if (endpoint == nullptr) { log_e("Endpoint [%d] not found", endpoint_id); - return NULL; + return nullptr; } cluster_t *cluster = cluster::get(endpoint, cluster_id); - if (cluster == NULL) { + if (cluster == nullptr) { log_e("Cluster [%d] not found", cluster_id); - return NULL; + return nullptr; } esp_matter::attribute_t *attribute = attribute::get(cluster, attribute_id); - if (attribute == NULL) { + if (attribute == nullptr) { log_e("Attribute [%d] not found", attribute_id); - return NULL; + return nullptr; } return attribute; } @@ -64,7 +64,7 @@ class MatterEndPoint { // get the value of an attribute from its cluster id and attribute it bool getAttributeVal(uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *attrVal) { esp_matter::attribute_t *attribute = getAttribute(cluster_id, attribute_id); - if (attribute == NULL) { + if (attribute == nullptr) { return false; } if (attribute::get_val(attribute, attrVal) == ESP_OK) { @@ -78,7 +78,7 @@ class MatterEndPoint { // set the value of an attribute from its cluster id and attribute it bool setAttributeVal(uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *attrVal) { esp_matter::attribute_t *attribute = getAttribute(cluster_id, attribute_id); - if (attribute == NULL) { + if (attribute == nullptr) { return false; } if (attribute::set_val(attribute, attrVal) == ESP_OK) { @@ -117,6 +117,6 @@ class MatterEndPoint { protected: uint16_t endpoint_id = 0; - EndPointIdentifyCB _onEndPointIdentifyCB = NULL; + EndPointIdentifyCB _onEndPointIdentifyCB = nullptr; }; #endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ From 7e67dbf5758646bd0bd248bee7bd17fdc290dd00 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Sat, 21 Jun 2025 09:45:26 -0300 Subject: [PATCH 12/13] feat(uart): fixes loopback function after IDF changes (#11492) * feat(uart): fixes loopback function after IDF changes IDF 5.4.1 has added a new function called uart_release_pin() that is called whenever new pins are set or when uart driver is deleted. This has a side effect that causes RX pin to never work again with the loopback function. Other changes also have removed some GPIO setup that was necessary for the GPIO loopback mode work. The PR forces a full RX Pin setup in order to make it work in GPIO Matrix with Loopback TX Signal * feat(uart): adds missing include file * feat(uart): removes not necessary part of the code * fix(uart): commentaries style fix Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- cores/esp32/esp32-hal-uart.c | 46 +++++++++--------------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 5311aff4f37..3c9e6bf178b 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -32,10 +32,11 @@ #include "driver/gpio.h" #include "hal/gpio_hal.h" #include "esp_rom_gpio.h" +#include "esp_private/gpio.h" #include "driver/rtc_io.h" #include "driver/lp_io.h" -#include "soc/uart_periph.h" +#include "soc/uart_pins.h" #include "esp_private/uart_share_hw_ctrl.h" static int s_uart_debug_nr = 0; // UART number for debug output @@ -1383,39 +1384,9 @@ unsigned long uartDetectBaudrate(uart_t *uart) { } /* - These functions are for testing purpose only and can be used in Arduino Sketches - Those are used in the UART examples -*/ - -/* - This is intended to make an internal loopback connection using IOMUX - The function uart_internal_loopback() shall be used right after Arduino Serial.begin(...) - This code "replaces" the physical wiring for connecting TX <--> RX in a loopback -*/ - -// gets the right TX or RX SIGNAL, based on the UART number from gpio_sig_map.h -#ifdef CONFIG_IDF_TARGET_ESP32P4 -#define UART_TX_SIGNAL(uartNumber) \ - (uartNumber == UART_NUM_0 \ - ? UART0_TXD_PAD_OUT_IDX \ - : (uartNumber == UART_NUM_1 \ - ? UART1_TXD_PAD_OUT_IDX \ - : (uartNumber == UART_NUM_2 ? UART2_TXD_PAD_OUT_IDX : (uartNumber == UART_NUM_3 ? UART3_TXD_PAD_OUT_IDX : UART4_TXD_PAD_OUT_IDX)))) -#define UART_RX_SIGNAL(uartNumber) \ - (uartNumber == UART_NUM_0 \ - ? UART0_RXD_PAD_IN_IDX \ - : (uartNumber == UART_NUM_1 \ - ? UART1_RXD_PAD_IN_IDX \ - : (uartNumber == UART_NUM_2 ? UART2_RXD_PAD_IN_IDX : (uartNumber == UART_NUM_3 ? UART3_RXD_PAD_IN_IDX : UART4_RXD_PAD_IN_IDX)))) -#else -#if SOC_UART_HP_NUM > 2 -#define UART_TX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0TXD_OUT_IDX : (uartNumber == UART_NUM_1 ? U1TXD_OUT_IDX : U2TXD_OUT_IDX)) -#define UART_RX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0RXD_IN_IDX : (uartNumber == UART_NUM_1 ? U1RXD_IN_IDX : U2RXD_IN_IDX)) -#else -#define UART_TX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0TXD_OUT_IDX : U1TXD_OUT_IDX) -#define UART_RX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0RXD_IN_IDX : U1RXD_IN_IDX) -#endif -#endif // ifdef CONFIG_IDF_TARGET_ESP32P4 + * These functions are for testing purposes only and can be used in Arduino Sketches. + * They are utilized in the UART examples and CI. + */ /* This function internally binds defined UARTs TX signal with defined RX pin of any UART (same or different). @@ -1427,7 +1398,12 @@ void uart_internal_loopback(uint8_t uartNum, int8_t rxPin) { log_e("UART%d is not supported for loopback or RX pin %d is invalid.", uartNum, rxPin); return; } - esp_rom_gpio_connect_out_signal(rxPin, UART_TX_SIGNAL(uartNum), false, false); + // forces rxPin to use GPIO Matrix and setup the pin to receive UART TX Signal - IDF 5.4.1 Change with uart_release_pin() + gpio_func_sel((gpio_num_t)rxPin, PIN_FUNC_GPIO); + gpio_pullup_en((gpio_num_t)rxPin); + gpio_input_enable((gpio_num_t)rxPin); + esp_rom_gpio_connect_in_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_RX_PIN_IDX].signal, false); + esp_rom_gpio_connect_out_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_TX_PIN_IDX].signal, false, false); } /* From 5246f27044b73ef33c3e6dbbf46aafbbe5449066 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Sun, 22 Jun 2025 15:02:36 -0300 Subject: [PATCH 13/13] feat(uart): fixes pin attach for any IDF 5.x (#11499) * feat(uart): fixes pin attach for any IDF 5.x * fix(uart): commentary typo error Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix(uart): commentary typo error Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix(uart): commentary typo error Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix(uart): commentary style fix * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- cores/esp32/esp32-hal-uart.c | 131 +++++++++++++++++++++++++++++++++-- 1 file changed, 126 insertions(+), 5 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 3c9e6bf178b..2163e9b5f42 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -295,6 +295,125 @@ static bool _uartDetachBus_RTS(void *busptr) { return _uartDetachPins(bus->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, bus->_rtsPin); } +static bool _uartTrySetIomuxPin(uart_port_t uart_num, int io_num, uint32_t idx) { + // Store a pointer to the default pin, to optimize access to its fields. + const uart_periph_sig_t *upin = &uart_periph_signal[uart_num].pins[idx]; + + // In theory, if default_gpio is -1, iomux_func should also be -1, but let's be safe and test both. + if (upin->iomux_func == -1 || upin->default_gpio == -1 || upin->default_gpio != io_num) { + return false; + } + + // Assign the correct function to the GPIO. + assert(upin->iomux_func != -1); + if (uart_num < SOC_UART_HP_NUM) { + gpio_iomux_out(io_num, upin->iomux_func, false); + // If the pin is input, we also have to redirect the signal, in order to bypass the GPIO matrix. + if (upin->input) { + gpio_iomux_in(io_num, upin->signal); + } + } +#if (SOC_UART_LP_NUM >= 1) && (SOC_RTCIO_PIN_COUNT >= 1) + else { + if (upin->input) { + rtc_gpio_set_direction(io_num, RTC_GPIO_MODE_INPUT_ONLY); + } else { + rtc_gpio_set_direction(io_num, RTC_GPIO_MODE_OUTPUT_ONLY); + } + rtc_gpio_init(io_num); + rtc_gpio_iomux_func_sel(io_num, upin->iomux_func); + } +#endif + return true; +} + +static esp_err_t _uartInternalSetPin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num) { + // Since an IO cannot route peripheral signals via IOMUX and GPIO matrix at the same time, + // if tx and rx share the same IO, both signals need to be routed to IOs through GPIO matrix + bool tx_rx_same_io = (tx_io_num == rx_io_num); + + // In the following statements, if the io_num is negative, no need to configure anything. + if (tx_io_num >= 0) { +#if CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND || CONFIG_PM_SLP_DISABLE_GPIO + // In such case, IOs are going to switch to sleep configuration (isolate) when entering sleep for power saving reason + // But TX IO in isolate state could write garbled data to the other end + // Therefore, we should disable the switch of the TX pin to sleep configuration + gpio_sleep_sel_dis(tx_io_num); +#endif + if (tx_rx_same_io || !_uartTrySetIomuxPin(uart_num, tx_io_num, SOC_UART_TX_PIN_IDX)) { + if (uart_num < SOC_UART_HP_NUM) { + gpio_func_sel(tx_io_num, PIN_FUNC_GPIO); + esp_rom_gpio_connect_out_signal(tx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX), 0, 0); + // output enable is set inside esp_rom_gpio_connect_out_signal func after the signal is connected + // (output enabled too early may cause unnecessary level change at the pad) + } +#if SOC_LP_GPIO_MATRIX_SUPPORTED + else { + rtc_gpio_init(tx_io_num); // set as a LP_GPIO pin + lp_gpio_connect_out_signal(tx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX), 0, 0); + // output enable is set inside lp_gpio_connect_out_signal func after the signal is connected + } +#endif + } + } + + if (rx_io_num >= 0) { +#if CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND || CONFIG_PM_SLP_DISABLE_GPIO + // In such case, IOs are going to switch to sleep configuration (isolate) when entering sleep for power saving reason + // But RX IO in isolate state could receive garbled data into FIFO, which is not desired + // Therefore, we should disable the switch of the RX pin to sleep configuration + gpio_sleep_sel_dis(rx_io_num); +#endif + if (tx_rx_same_io || !_uartTrySetIomuxPin(uart_num, rx_io_num, SOC_UART_RX_PIN_IDX)) { + if (uart_num < SOC_UART_HP_NUM) { + gpio_input_enable(rx_io_num); + esp_rom_gpio_connect_in_signal(rx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX), 0); + } +#if SOC_LP_GPIO_MATRIX_SUPPORTED + else { + rtc_gpio_mode_t mode = (tx_rx_same_io ? RTC_GPIO_MODE_INPUT_OUTPUT : RTC_GPIO_MODE_INPUT_ONLY); + rtc_gpio_set_direction(rx_io_num, mode); + if (!tx_rx_same_io) { // set the same pin again as a LP_GPIO will overwrite connected out_signal, not desired, so skip + rtc_gpio_init(rx_io_num); // set as a LP_GPIO pin + } + lp_gpio_connect_in_signal(rx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX), 0); + } +#endif + } + } + + if (rts_io_num >= 0 && !_uartTrySetIomuxPin(uart_num, rts_io_num, SOC_UART_RTS_PIN_IDX)) { + if (uart_num < SOC_UART_HP_NUM) { + gpio_func_sel(rts_io_num, PIN_FUNC_GPIO); + esp_rom_gpio_connect_out_signal(rts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RTS_PIN_IDX), 0, 0); + // output enable is set inside esp_rom_gpio_connect_out_signal func after the signal is connected + } +#if SOC_LP_GPIO_MATRIX_SUPPORTED + else { + rtc_gpio_init(rts_io_num); // set as a LP_GPIO pin + lp_gpio_connect_out_signal(rts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RTS_PIN_IDX), 0, 0); + // output enable is set inside lp_gpio_connect_out_signal func after the signal is connected + } +#endif + } + + if (cts_io_num >= 0 && !_uartTrySetIomuxPin(uart_num, cts_io_num, SOC_UART_CTS_PIN_IDX)) { + if (uart_num < SOC_UART_HP_NUM) { + gpio_pullup_en(cts_io_num); + gpio_input_enable(cts_io_num); + esp_rom_gpio_connect_in_signal(cts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_CTS_PIN_IDX), 0); + } +#if SOC_LP_GPIO_MATRIX_SUPPORTED + else { + rtc_gpio_set_direction(cts_io_num, RTC_GPIO_MODE_INPUT_ONLY); + rtc_gpio_init(cts_io_num); // set as a LP_GPIO pin + lp_gpio_connect_in_signal(cts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_CTS_PIN_IDX), 0); + } +#endif + } + return ESP_OK; +} + // Attach function for UART // connects the IO Pad, set Paripheral Manager and internal UART structure data static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) { @@ -307,7 +426,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t //log_v("attaching UART%d pins: prev,new RX(%d,%d) TX(%d,%d) CTS(%d,%d) RTS(%d,%d)", uart_num, // uart->_rxPin, rxPin, uart->_txPin, txPin, uart->_ctsPin, ctsPin, uart->_rtsPin, rtsPin); vTaskDelay(10); - // IDF uart_set_pin() checks if the pin is used within LP UART and if it is a valid RTC IO pin + // IDF _uartInternalSetPin() checks if the pin is used within LP UART and if it is a valid RTC IO pin // No need for Arduino Layer to check it again bool retCode = true; if (rxPin >= 0) { @@ -316,7 +435,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t perimanClearPinBus(rxPin); } // connect RX Pad - bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); + bool ret = ESP_OK == _uartInternalSetPin(uart->num, UART_PIN_NO_CHANGE, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); #if SOC_UART_LP_NUM >= 1 if (ret && uart_num >= SOC_UART_HP_NUM) { // it is a LP UART NUM ret &= lp_uart_config_io(uart->num, rxPin, RTC_GPIO_MODE_INPUT_ONLY, SOC_UART_RX_PIN_IDX); @@ -339,7 +458,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t perimanClearPinBus(txPin); } // connect TX Pad - bool ret = ESP_OK == uart_set_pin(uart->num, txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); + bool ret = ESP_OK == _uartInternalSetPin(uart->num, txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); #if SOC_UART_LP_NUM >= 1 if (ret && uart_num >= SOC_UART_HP_NUM) { // it is a LP UART NUM ret &= lp_uart_config_io(uart->num, txPin, RTC_GPIO_MODE_OUTPUT_ONLY, SOC_UART_TX_PIN_IDX); @@ -362,7 +481,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t perimanClearPinBus(ctsPin); } // connect CTS Pad - bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, ctsPin); + bool ret = ESP_OK == _uartInternalSetPin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, ctsPin); #if SOC_UART_LP_NUM >= 1 if (ret && uart_num >= SOC_UART_HP_NUM) { // it is a LP UART NUM ret &= lp_uart_config_io(uart->num, ctsPin, RTC_GPIO_MODE_INPUT_ONLY, SOC_UART_CTS_PIN_IDX); @@ -385,7 +504,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t perimanClearPinBus(rtsPin); } // connect RTS Pad - bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, rtsPin, UART_PIN_NO_CHANGE); + bool ret = ESP_OK == _uartInternalSetPin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, rtsPin, UART_PIN_NO_CHANGE); #if SOC_UART_LP_NUM >= 1 if (ret && uart_num >= SOC_UART_HP_NUM) { // it is a LP UART NUM ret &= lp_uart_config_io(uart->num, rtsPin, RTC_GPIO_MODE_OUTPUT_ONLY, SOC_UART_RTS_PIN_IDX); @@ -1398,11 +1517,13 @@ void uart_internal_loopback(uint8_t uartNum, int8_t rxPin) { log_e("UART%d is not supported for loopback or RX pin %d is invalid.", uartNum, rxPin); return; } +#if 0 // leave this code here for future reference and need // forces rxPin to use GPIO Matrix and setup the pin to receive UART TX Signal - IDF 5.4.1 Change with uart_release_pin() gpio_func_sel((gpio_num_t)rxPin, PIN_FUNC_GPIO); gpio_pullup_en((gpio_num_t)rxPin); gpio_input_enable((gpio_num_t)rxPin); esp_rom_gpio_connect_in_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_RX_PIN_IDX].signal, false); +#endif esp_rom_gpio_connect_out_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_TX_PIN_IDX].signal, false, false); }