feat(openthread): add common components for openthread examples

This commit is contained in:
Xu Si Yu
2025-09-16 14:07:24 +08:00
parent a98de91e5f
commit c6359c36b3
11 changed files with 377 additions and 0 deletions
@@ -0,0 +1,19 @@
set(srcs "")
if(CONFIG_OPENTHREAD_FTD OR CONFIG_OPENTHREAD_MTD)
list(APPEND srcs "ot_network.c")
endif()
if(CONFIG_OPENTHREAD_CLI)
list(APPEND srcs "ot_console.c")
endif()
if(CONFIG_ESP_COEX_EXTERNAL_COEXIST_ENABLE)
list(APPEND srcs "ot_external_coexist.c")
endif()
idf_component_register(
SRCS "${srcs}"
INCLUDE_DIRS "include"
PRIV_REQUIRES console cmd_system esp_coex openthread
)
@@ -0,0 +1,67 @@
menu "Config for OpenThread Examples"
depends on OPENTHREAD_ENABLED
config OPENTHREAD_NETWORK_AUTO_START
bool 'Enable the automatic start mode of Thread network.'
depends on OPENTHREAD_FTD || OPENTHREAD_MTD
default n
help
If enabled, the Openthread Device will create or connect to Thread network with pre-configured
network parameters automatically. Otherwise, user need to configure Thread via CLI command manually.
menu "External coexist wire type and pin config"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE
choice EXTERNAL_COEX_WORK_MODE
prompt "The work mode of external coexist"
default EXTERNAL_COEX_WORK_MODE_FOLLOWER if SOC_IEEE802154_SUPPORTED
default EXTERNAL_COEX_WORK_MODE_LEADER if !SOC_IEEE802154_SUPPORTED && SOC_WIFI_SUPPORTED
help
Select work mode for external coexist, the work mode defined in esp_extern_coex_work_mode_t.
config EXTERNAL_COEX_WORK_MODE_LEADER
bool "Leader mode"
help
Select this to set the external coexistence work mode to leader mode.
config EXTERNAL_COEX_WORK_MODE_FOLLOWER
bool "Follower mode"
help
Select this to set the external coexistence work mode to follower mode.
config EXTERNAL_COEX_WORK_MODE_UNKNOWN
bool "Unknown mode"
help
Select this to set the external coexistence work mode to unknown mode.
endchoice
config EXTERNAL_COEX_WIRE_TYPE
int "The wire_type of external coexist"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE
default 3
range 0 3
help
Select wire_type for external coexist, the wire_type define in external_coex_wire_t.
config EXTERNAL_COEX_REQUEST_PIN
int "The number of external coexist request pin"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE && (EXTERNAL_COEX_WIRE_TYPE >= 0)
default 0
config EXTERNAL_COEX_GRANT_PIN
int "The number of external coexist grant pin"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE && (EXTERNAL_COEX_WIRE_TYPE >= 1)
default 1
config EXTERNAL_COEX_PRIORITY_PIN
int "The number of external coexist priority pin"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE && (EXTERNAL_COEX_WIRE_TYPE >= 2)
default 2
config EXTERNAL_COEX_TX_LINE_PIN
int "The number of external coexist tx_line pin"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE && (EXTERNAL_COEX_WIRE_TYPE = 3)
default 3
endmenu # External coexist wire type and pin config
endmenu
@@ -0,0 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
cmd_system:
path: ${IDF_PATH}/examples/system/console/advanced/components/cmd_system
@@ -0,0 +1,63 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include "sdkconfig.h"
#if CONFIG_ESP_COEX_EXTERNAL_COEXIST_ENABLE
#if CONFIG_EXTERNAL_COEX_WIRE_TYPE == EXTERNAL_COEXIST_WIRE_1
#define ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG() \
{ \
.request = CONFIG_EXTERNAL_COEX_REQUEST_PIN, \
}
#elif CONFIG_EXTERNAL_COEX_WIRE_TYPE == EXTERNAL_COEXIST_WIRE_2
#define ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG() \
{ \
.request = CONFIG_EXTERNAL_COEX_REQUEST_PIN, \
.grant = CONFIG_EXTERNAL_COEX_GRANT_PIN, \
}
#elif CONFIG_EXTERNAL_COEX_WIRE_TYPE == EXTERNAL_COEXIST_WIRE_3
#define ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG() \
{ \
.request = CONFIG_EXTERNAL_COEX_REQUEST_PIN, \
.priority = CONFIG_EXTERNAL_COEX_PRIORITY_PIN, \
.grant = CONFIG_EXTERNAL_COEX_GRANT_PIN, \
}
#elif CONFIG_EXTERNAL_COEX_WIRE_TYPE == EXTERNAL_COEXIST_WIRE_4
#define ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG() \
{ \
.request = CONFIG_EXTERNAL_COEX_REQUEST_PIN, \
.priority = CONFIG_EXTERNAL_COEX_PRIORITY_PIN, \
.grant = CONFIG_EXTERNAL_COEX_GRANT_PIN, \
.tx_line = CONFIG_EXTERNAL_COEX_TX_LINE_PIN, \
}
#endif
#endif // CONFIG_ESP_COEX_EXTERNAL_COEXIST_ENABLE
/**
* @brief Initializes the external coexistence.
*
*/
void ot_external_coexist_init(void);
/**
* @brief Initializes the console.
*
*/
void ot_console_start(void);
/**
* @brief Form or join the Thread network automatically.
*
*/
void ot_network_auto_start(void);
@@ -0,0 +1,48 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include "ot_examples_common.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_console.h"
#include "cmd_system.h"
void ot_console_start(void)
{
esp_console_repl_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
/* Prompt to be printed before each line.
* This can be customized, made dynamic, etc.
*/
repl_config.prompt = CONFIG_IDF_TARGET ">";
repl_config.max_cmdline_length = 256;
repl_config.max_history_len = 10;
#if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) || defined(CONFIG_ESP_CONSOLE_UART_CUSTOM)
esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
#elif defined(CONFIG_ESP_CONSOLE_USB_CDC)
esp_console_dev_usb_cdc_config_t hw_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&hw_config, &repl_config, &repl));
#elif defined(CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG)
esp_console_dev_usb_serial_jtag_config_t hw_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&hw_config, &repl_config, &repl));
#else
#error Unsupported console type
#endif
ESP_ERROR_CHECK(esp_console_start_repl(repl));
register_system();
}
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include "sdkconfig.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_coexist.h"
#include "ot_examples_common.h"
void ot_external_coexist_init(void)
{
esp_extern_coex_work_mode_t mode =
#if CONFIG_EXTERNAL_COEX_WORK_MODE_LEADER
EXTERNAL_COEX_LEADER_ROLE;
#elif CONFIG_EXTERNAL_COEX_WORK_MODE_FOLLOWER
EXTERNAL_COEX_FOLLOWER_ROLE;
#else
EXTERNAL_COEX_UNKNOWN_ROLE;
#endif
esp_external_coex_gpio_set_t gpio_pin = ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG();
ESP_ERROR_CHECK(esp_external_coex_set_work_mode(mode));
ESP_ERROR_CHECK(esp_enable_extern_coex_gpio_pin(CONFIG_EXTERNAL_COEX_WIRE_TYPE, gpio_pin));
}
@@ -0,0 +1,28 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line Example
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#include "ot_examples_common.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_openthread.h"
#include "esp_openthread_lock.h"
void ot_network_auto_start(void)
{
otOperationalDatasetTlvs dataset;
esp_openthread_lock_acquire(portMAX_DELAY);
otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
ESP_ERROR_CHECK(esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL));
esp_openthread_lock_release();
}