Skip to content

Commit fd2df5e

Browse files
committed
Merge branch 'bugfix/uart_async_rxtxtasks_example_stack_overflow' into 'master'
fix(uart): enlarged task stack size for uart_async_rxtxtasks example Closes IDFGH-14607, IDF-12230, and IDFGH-12126 See merge request espressif/esp-idf!36903
2 parents 1160a86 + c465b75 commit fd2df5e

File tree

7 files changed

+138
-73
lines changed

7 files changed

+138
-73
lines changed

docs/en/api-reference/peripherals/uart.rst

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ Functional Overview
2323

2424
The overview describes how to establish communication between an {IDF_TARGET_NAME} and other UART devices using the functions and data types of the UART driver. A typical programming workflow is broken down into the sections provided below:
2525

26-
1. :ref:`uart-api-setting-communication-parameters` - Setting baud rate, data bits, stop bits, etc.
27-
2. :ref:`uart-api-setting-communication-pins` - Assigning pins for connection to a device
28-
3. :ref:`uart-api-driver-installation` - Allocating {IDF_TARGET_NAME}'s resources for the UART driver
26+
1. :ref:`uart-api-driver-installation` - Allocating {IDF_TARGET_NAME}'s resources for the UART driver
27+
2. :ref:`uart-api-setting-communication-parameters` - Setting baud rate, data bits, stop bits, etc.
28+
3. :ref:`uart-api-setting-communication-pins` - Assigning pins for connection to a device
2929
4. :ref:`uart-api-running-uart-communication` - Sending/receiving data
3030
5. :ref:`uart-api-using-interrupts` - Triggering interrupts on specific communication events
3131
6. :ref:`uart-api-deleting-driver` - Freeing allocated resources if a UART communication is no longer required
@@ -39,13 +39,39 @@ Steps 1 to 3 comprise the configuration stage. Step 4 is where the UART starts o
3939
The UART driver's functions identify each of the UART controllers using :cpp:type:`uart_port_t`. This identification is needed for all the following function calls.
4040

4141

42+
.. _uart-api-driver-installation:
43+
44+
Install Drivers
45+
^^^^^^^^^^^^^^^^^^^
46+
47+
First of all, install the driver by calling :cpp:func:`uart_driver_install` and specify the following parameters:
48+
49+
- UART port number
50+
- Size of RX ring buffer
51+
- Size of TX ring buffer
52+
- Event queue size
53+
- Pointer to store the event queue handle
54+
- Flags to allocate an interrupt
55+
56+
.. _driver-code-snippet:
57+
58+
The function allocates the required internal resources for the UART driver.
59+
60+
.. code-block:: c
61+
62+
// Setup UART buffered IO with event queue
63+
const int uart_buffer_size = (1024 * 2);
64+
QueueHandle_t uart_queue;
65+
// Install UART driver using an event queue here
66+
ESP_ERROR_CHECK(uart_driver_install({IDF_TARGET_UART_EXAMPLE_PORT}, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
67+
68+
4269
.. _uart-api-setting-communication-parameters:
4370

4471
Set Communication Parameters
4572
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4673

47-
UART communication parameters can be configured all in a single step or individually in multiple steps.
48-
74+
As the next step, UART communication parameters can be configured all in a single step or individually in multiple steps.
4975

5076
Single Step
5177
"""""""""""
@@ -113,35 +139,6 @@ The same macro :c:macro:`UART_PIN_NO_CHANGE` should be specified for pins that w
113139
// Set UART pins(TX: IO4, RX: IO5, RTS: IO18, CTS: IO19)
114140
ESP_ERROR_CHECK(uart_set_pin({IDF_TARGET_UART_EXAMPLE_PORT}, 4, 5, 18, 19));
115141
116-
.. _uart-api-driver-installation:
117-
118-
Install Drivers
119-
^^^^^^^^^^^^^^^^^^^
120-
121-
Once the communication pins are set, install the driver by calling :cpp:func:`uart_driver_install` and specify the following parameters:
122-
123-
- UART port number
124-
- Size of TX ring buffer
125-
- Size of RX ring buffer
126-
- Pointer to store the event queue handle
127-
- Event queue size
128-
- Flags to allocate an interrupt
129-
130-
.. _driver-code-snippet:
131-
132-
The function allocates the required internal resources for the UART driver.
133-
134-
.. code-block:: c
135-
136-
// Setup UART buffered IO with event queue
137-
const int uart_buffer_size = (1024 * 2);
138-
QueueHandle_t uart_queue;
139-
// Install UART driver using an event queue here
140-
ESP_ERROR_CHECK(uart_driver_install({IDF_TARGET_UART_EXAMPLE_PORT}, uart_buffer_size, \
141-
uart_buffer_size, 10, &uart_queue, 0));
142-
143-
Once this step is complete, you can connect the external UART device and check the communication.
144-
145142
146143
.. _uart-api-running-uart-communication:
147144

docs/zh_CN/api-reference/peripherals/uart.rst

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
下文介绍了如何使用 UART 驱动程序的函数和数据类型在 {IDF_TARGET_NAME} 和其他 UART 设备之间建立通信。基本编程流程分为以下几个步骤:
2525

26-
1. :ref:`uart-api-setting-communication-parameters` - 设置波特率、数据位、停止位等
27-
2. :ref:`uart-api-setting-communication-pins` - 分配连接设备的管脚
28-
3. :ref:`uart-api-driver-installation` - 为 UART 驱动程序分配 {IDF_TARGET_NAME} 资源
26+
1. :ref:`uart-api-driver-installation` - 为 UART 驱动程序分配 {IDF_TARGET_NAME} 资源
27+
2. :ref:`uart-api-setting-communication-parameters` - 设置波特率、数据位、停止位等
28+
3. :ref:`uart-api-setting-communication-pins` - 分配连接设备的管脚
2929
4. :ref:`uart-api-running-uart-communication` - 发送/接收数据
3030
5. :ref:`uart-api-using-interrupts` - 触发特定通信事件的中断
3131
6. :ref:`uart-api-deleting-driver` - 如无需 UART 通信,则释放已分配的资源
@@ -39,13 +39,39 @@
3939
UART 驱动程序函数通过 :cpp:type:`uart_port_t` 识别不同的 UART 控制器。调用以下所有函数均需此标识。
4040

4141

42+
.. _uart-api-driver-installation:
43+
44+
安装驱动程序
45+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
47+
首先,请调用 :cpp:func:`uart_driver_install` 安装驱动程序并指定以下参数:
48+
49+
- UART 控制器编号
50+
- Rx 环形缓冲区的大小
51+
- Tx 环形缓冲区的大小
52+
- 事件队列大小
53+
- 指向事件队列句柄的指针
54+
- 分配中断的标志
55+
56+
.. _driver-code-snippet:
57+
58+
该函数将为 UART 驱动程序分配所需的内部资源。
59+
60+
.. code-block:: c
61+
62+
// Setup UART buffered IO with event queue
63+
const int uart_buffer_size = (1024 * 2);
64+
QueueHandle_t uart_queue;
65+
// Install UART driver using an event queue here
66+
ESP_ERROR_CHECK(uart_driver_install({IDF_TARGET_UART_EXAMPLE_PORT}, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
67+
68+
4269
.. _uart-api-setting-communication-parameters:
4370

4471
设置通信参数
4572
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4673

47-
UART 通信参数可以在一个步骤中完成全部配置,也可以在多个步骤中单独配置。
48-
74+
其次,UART 通信参数可以在一个步骤中完成全部配置,也可以在多个步骤中单独配置。
4975

5076
一次性配置所有参数
5177
""""""""""""""""""""""""""""""""
@@ -113,35 +139,6 @@ UART 通信参数可以在一个步骤中完成全部配置,也可以在多个
113139
// Set UART pins(TX: IO4, RX: IO5, RTS: IO18, CTS: IO19)
114140
ESP_ERROR_CHECK(uart_set_pin({IDF_TARGET_UART_EXAMPLE_PORT}, 4, 5, 18, 19));
115141
116-
.. _uart-api-driver-installation:
117-
118-
安装驱动程序
119-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120-
121-
通信管脚设置完成后,请调用 :cpp:func:`uart_driver_install` 安装驱动程序并指定以下参数:
122-
123-
- UART 控制器编号
124-
- Tx 环形缓冲区的大小
125-
- Rx 环形缓冲区的大小
126-
- 指向事件队列句柄的指针
127-
- 事件队列大小
128-
- 分配中断的标志
129-
130-
.. _driver-code-snippet:
131-
132-
该函数将为 UART 驱动程序分配所需的内部资源。
133-
134-
.. code-block:: c
135-
136-
// Setup UART buffered IO with event queue
137-
const int uart_buffer_size = (1024 * 2);
138-
QueueHandle_t uart_queue;
139-
// Install UART driver using an event queue here
140-
ESP_ERROR_CHECK(uart_driver_install({IDF_TARGET_UART_EXAMPLE_PORT}, uart_buffer_size, \
141-
uart_buffer_size, 10, &uart_queue, 0));
142-
143-
此步骤完成后,可连接外部 UART 设备检查通信。
144-
145142
146143
.. _uart-api-running-uart-communication:
147144

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
menu "Example Configuration"
2+
3+
orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
4+
5+
config EXAMPLE_UART_BAUD_RATE
6+
int "UART communication speed"
7+
range 1200 115200
8+
default 115200
9+
help
10+
UART communication speed for the example.
11+
12+
config EXAMPLE_UART_RXD
13+
int "UART RXD pin number"
14+
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
15+
default 5
16+
help
17+
GPIO number for UART RX pin. See UART documentation for more information
18+
about available pin numbers for UART.
19+
20+
config EXAMPLE_UART_TXD
21+
int "UART TXD pin number"
22+
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
23+
default 4
24+
help
25+
GPIO number for UART TX pin. See UART documentation for more information
26+
about available pin numbers for UART.
27+
28+
config EXAMPLE_TASK_STACK_SIZE
29+
int "Example task stack size"
30+
range 1024 16384
31+
default 3072
32+
help
33+
Defines stack size for UART TX and RX tasks. Insufficient stack size can cause crash.
34+
35+
endmenu

examples/peripherals/uart/uart_async_rxtxtasks/main/uart_async_rxtxtasks_main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
static const int RX_BUF_SIZE = 1024;
1818

19-
#define TXD_PIN (GPIO_NUM_4)
20-
#define RXD_PIN (GPIO_NUM_5)
19+
#define TXD_PIN (CONFIG_EXAMPLE_UART_TXD)
20+
#define RXD_PIN (CONFIG_EXAMPLE_UART_RXD)
2121

2222
void init(void)
2323
{
2424
const uart_config_t uart_config = {
25-
.baud_rate = 115200,
25+
.baud_rate = CONFIG_EXAMPLE_UART_BAUD_RATE,
2626
.data_bits = UART_DATA_8_BITS,
2727
.parity = UART_PARITY_DISABLE,
2828
.stop_bits = UART_STOP_BITS_1,
@@ -72,6 +72,6 @@ static void rx_task(void *arg)
7272
void app_main(void)
7373
{
7474
init();
75-
xTaskCreate(rx_task, "uart_rx_task", 1024 * 2, NULL, configMAX_PRIORITIES - 1, NULL);
76-
xTaskCreate(tx_task, "uart_tx_task", 1024 * 2, NULL, configMAX_PRIORITIES - 2, NULL);
75+
xTaskCreate(rx_task, "uart_rx_task", CONFIG_EXAMPLE_TASK_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
76+
xTaskCreate(tx_task, "uart_tx_task", CONFIG_EXAMPLE_TASK_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, NULL);
7777
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: CC0-1.0
3+
import pytest
4+
from pytest_embedded import Dut
5+
6+
7+
@pytest.mark.supported_targets
8+
@pytest.mark.generic
9+
def test_uart_async_rxtxtasks_example(dut: Dut) -> None:
10+
dut.expect_exact('TX_TASK: Wrote 11 bytes')
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: CC0-1.0
3+
import pytest
4+
from pytest_embedded import Dut
5+
6+
7+
@pytest.mark.supported_targets
8+
@pytest.mark.generic
9+
def test_uart_events_example(dut: Dut) -> None:
10+
dut.expect_exact('Returned from app_main()')
11+
dut.write('a')
12+
dut.expect_exact('uart_events: [UART DATA]: 2') # dut.write will add an extra '\n'
13+
dut.write('HA')
14+
dut.expect_exact('uart_events: [UART DATA]: 3') # dut.write will add an extra '\n'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: CC0-1.0
3+
import pytest
4+
from pytest_embedded import Dut
5+
6+
7+
@pytest.mark.supported_targets
8+
@pytest.mark.generic
9+
def test_uart_select_example(dut: Dut) -> None:
10+
dut.expect_exact('uart_select_example: Timeout has been reached and nothing has been received')
11+
dut.write('a')
12+
dut.expect_exact('uart_select_example: Received: a')

0 commit comments

Comments
 (0)