- 在工程的
main
目录下添加Kconfig.projbuild
文件 - 对需要添加自定义设置的参数进行定义
- 在应用代码中使用自定义设置的参数
- 编写
Kconfig.projbuild
文件
在工程的 main
目录下添加 Kconfig.projbuild
文件
接下来会基于 esp-idf/examples/peripherals/uart/uart_async_rxtxtasks/main 例程来进行测试
- 可参考 esp-idf/examples/peripherals/uart/uart_echo/main/Kconfig.projbuild
- 可以直接复制 esp-idf/examples/peripherals/uart/uart_echo/main/Kconfig.projbuild 文件到 esp-idf/examples/peripherals/uart/uart_async_rxtxtasks/main 目录下
对需要添加自定义设置的参数进行定义
我们以 esp-idf/examples/peripherals/uart/uart_async_rxtxtasks 例程为例,来进行编写测试
- 需要先在
main.c
文件中对待设置的参数进行定义,例如
#define UART_TXD_PIN (CONFIG_EXAMPLE_UART_TXD)
#define UART_RXD_PIN (CONFIG_EXAMPLE_UART_RXD)
#define UART_RTS_PIN (CONFIG_EXAMPLE_UART_RTS)
#define UART_CTS_PIN (CONFIG_EXAMPLE_UART_CTS)
#define UART_PORT_NUM (CONFIG_EXAMPLE_UART_PORT_NUM)
#define UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE)
#define TASK_STACK_SIZE (CONFIG_EXAMPLE_TASK_STACK_SIZE)
UART_TXD_PIN
、UART_RXD_PIN
、UART_RXD_PIN
、UART_RTS_PIN
、UART_CTS_PIN
、UART_PORT_NUM
、UART_BAUD_RATE
、TASK_STACK_SIZE
为对应定义的参数
EXAMPLE_UART_TXD
、EXAMPLE_UART_RXD
、EXAMPLE_UART_RTS
、EXAMPLE_UART_CTS
、EXAMPLE_UART_PORT_NUM
、EXAMPLE_UART_BAUD_RATE
、EXAMPLE_TASK_STACK_SIZE
需要与Kconfig.projbuild
文件中参数名称对应
在应用代码中使用自定义设置的参数
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "sdkconfig.h"
#include "driver/gpio.h"
//static const int RX_BUF_SIZE = 1024;
#define UART_TXD_PIN (CONFIG_EXAMPLE_UART_TXD)
#define UART_RXD_PIN (CONFIG_EXAMPLE_UART_RXD)
#define UART_RTS_PIN (CONFIG_EXAMPLE_UART_RTS)
#define UART_CTS_PIN (CONFIG_EXAMPLE_UART_CTS)
#define UART_PORT_NUM (CONFIG_EXAMPLE_UART_PORT_NUM)
#define UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE)
#define TASK_STACK_SIZE (CONFIG_EXAMPLE_TASK_STACK_SIZE)
void init(void) {
const uart_config_t uart_config = {
.baud_rate = UART_BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
.source_clk = UART_SCLK_DEFAULT,
};
// We won't use a buffer for sending data.
uart_driver_install(UART_PORT_NUM, TASK_STACK_SIZE * 2, 0, 0, NULL, 0);
uart_param_config(UART_PORT_NUM, &uart_config);
uart_set_pin(UART_PORT_NUM, UART_TXD_PIN, UART_RXD_PIN, UART_RTS_PIN, UART_CTS_PIN);
}
int sendData(const char* logName, const char* data)
{
const int len = strlen(data);
const int txBytes = uart_write_bytes(UART_PORT_NUM, data, len);
ESP_LOGI(logName, "Wrote %d bytes", txBytes);
return txBytes;
}
static void tx_task(void *arg)
{
static const char *TX_TASK_TAG = "TX_TASK";
esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);
while (1) {
sendData(TX_TASK_TAG, "Hello world");
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
static void rx_task(void *arg)
{
static const char *RX_TASK_TAG = "RX_TASK";
esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
uint8_t* data = (uint8_t*) malloc(TASK_STACK_SIZE+1);
while (1) {
const int rxBytes = uart_read_bytes(UART_PORT_NUM, data, TASK_STACK_SIZE, 1000 / portTICK_PERIOD_MS);
if (rxBytes > 0) {
data[rxBytes] = 0;
ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);
}
}
free(data);
}
void app_main(void)
{
init();
xTaskCreate(rx_task, "uart_rx_task", TASK_STACK_SIZE * 2 , NULL, configMAX_PRIORITIES, NULL);
xTaskCreate(tx_task, "uart_tx_task", TASK_STACK_SIZE * 2 , NULL, configMAX_PRIORITIES-1, NULL);
}
编写 Kconfig.projbuild
文件
- 参考 esp-idf/examples/peripherals/uart/uart_echo/main/Kconfig.projbuild 文件,我们对当前
Kconfig.projbuild
文件内容进行修改
menu "Example Configuration"
orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
config EXAMPLE_UART_PORT_NUM
int "UART port number"
range 0 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
default 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
range 0 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32H2
default 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32H2
help
UART communication port number for the example.
See UART documentation for available port numbers.
config EXAMPLE_UART_BAUD_RATE
int "UART communication speed"
range 1200 115200
default 115200
help
UART communication speed for Modbus example.
config EXAMPLE_UART_RXD
int "UART RXD pin number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
default 22
help
GPIO number for UART RX pin. See UART documentation for more information
about available pin numbers for UART.
config EXAMPLE_UART_TXD
int "UART TXD pin number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 23
help
GPIO number for UART TX pin. See UART documentation for more information
about available pin numbers for UART.
config EXAMPLE_UART_CTS
int "UART CTS pin number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 18
help
GPIO number for UART CTS pin. See UART documentation for more information
about available pin numbers for UART.
config EXAMPLE_UART_RTS
int "UART RTS pin number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 19
help
GPIO number for UART RTS pin. See UART documentation for more information
about available pin numbers for UART.
config EXAMPLE_TASK_STACK_SIZE
int "UART example task stack size"
range 1024 16384
default 2048
help
Defines stack size for UART echo example. Insufficient stack size can cause crash.
endmenu
config EXAMPLE_UART_PORT_NUM
对应的是#define UART_PORT_NUM (CONFIG_EXAMPLE_UART_PORT_NUM)
的CONFIG_EXAMPLE_UART_PORT_NUM
内容int "UART port number"
中的UART port number
为menuconfig
中对应的配置选项名称,可任意自定义