Files
board-mate/esp32-thread/open-thread-rcp/main/esp_ot_rcp.c
2025-12-26 13:02:17 +01:00

119 lines
3.3 KiB
C

/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Radio Co-Processor (RCP) Example
*
* This example code is in the Public Domain (or CC0-1.0 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 <stdio.h>
#include <unistd.h>
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_openthread.h"
#include "esp_ot_config.h"
#include "esp_vfs_eventfd.h"
#include "driver/uart.h"
#include "driver/rmt.h"
#include "led_strip.h"
#include "esp_err.h"
#include "driver/rmt_tx.h"
#if CONFIG_EXTERNAL_COEX_ENABLE
#include "esp_coexist.h"
#endif
#if !SOC_IEEE802154_SUPPORTED
#error "RCP is only supported for the SoCs which have IEEE 802.15.4 module"
#endif
#define TAG "ot_esp_rcp"
extern void otAppNcpInit(otInstance *instance);
#if CONFIG_EXTERNAL_COEX_ENABLE
#if SOC_EXTERNAL_COEX_ADVANCE
static void ot_external_coexist_init(void)
{
esp_external_coex_gpio_set_t gpio_pin = ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG();
esp_external_coex_set_work_mode(EXTERNAL_COEX_FOLLOWER_ROLE);
ESP_ERROR_CHECK(esp_enable_extern_coex_gpio_pin(CONFIG_EXTERNAL_COEX_WIRE_TYPE, gpio_pin));
}
#endif // SOC_EXTERNAL_COEX_ADVANCE
#endif // CONFIG_EXTERNAL_COEX_ENABLE
static void ot_task_worker(void *aContext)
{
esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
// Initialize the OpenThread stack
ESP_ERROR_CHECK(esp_openthread_init(&config));
#if CONFIG_EXTERNAL_COEX_ENABLE
ot_external_coexist_init();
#endif // CONFIG_EXTERNAL_COEX_ENABLE
// Initialize the OpenThread ncp
otAppNcpInit(esp_openthread_get_instance());
// Run the main loop
esp_openthread_launch_mainloop();
// Clean up
esp_vfs_eventfd_unregister();
vTaskDelete(NULL);
}
#define LED_GPIO 18
#define LED_COUNT 1
static led_strip_handle_t led_strip = NULL;
void init_led_strip(void)
{
led_strip_config_t strip_config = {
.strip_gpio_num = LED_GPIO,
.max_leds = LED_COUNT,
};
led_strip_rmt_config_t rmt_cfg = {
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = 10 * 1000 * 1000, // 10MHz is typical
.mem_block_symbols = 0, // 0 to use default
.flags.with_dma = false, // DMA optional
};
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_cfg, &led_strip));
ESP_ERROR_CHECK(led_strip_clear(led_strip));
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, 0, 255, 0, 255));
ESP_ERROR_CHECK(led_strip_refresh(led_strip));
}
void app_main(void)
{
// Used eventfds:
// * ot task queue
// * radio driver
esp_vfs_eventfd_config_t eventfd_config = {
.max_fds = 2,
};
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
xTaskCreate(ot_task_worker, "ot_rcp_main", 3072, xTaskGetCurrentTaskHandle(), 5, NULL);
}