Skip to content

Commit df9ca68

Browse files
alisitsynginkgm
authored andcommitted
examples: fix echo example crash when UART interrupt handler placed in IRAM
1 parent bd920d2 commit df9ca68

File tree

3 files changed

+98
-31
lines changed

3 files changed

+98
-31
lines changed

examples/peripherals/uart/uart_echo/README.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,51 @@
22

33
(See the README.md file in the upper level 'examples' directory for more information about examples.)
44

5-
This example demonstrates how to utilize UART interfaces of ESP32 by echoing back to the sender any data received on
6-
UART1.
5+
This example demonstrates how to utilize UART interfaces by echoing back to the sender any data received on
6+
configured UART.
77

88
## How to use example
99

1010
### Hardware Required
1111

12-
The example can be run on any ESP32 development board connected to a PC with a single USB cable for flashing and
12+
The example can be run on any ESP32 or ESP32-S2 based development board connected to a PC with a single USB cable for flashing and
1313
monitoring. The external interface should have 3.3V outputs. You may use e.g. 3.3V compatible USB-to-Serial dongle.
1414

1515
### Setup the Hardware
1616

17-
Connect the external serial interface to the ESP32 board as follows.
18-
19-
| ESP32 Interface | #define | ESP32 Pin | External UART Pin |
20-
| --- | --- | --- | --- |
21-
| Transmit Data (TxD) | ECHO_TEST_TXD | GPIO4 | RxD |
22-
| Receive Data (RxD) | ECHO_TEST_RXD | GPIO5 | TxD |
23-
| Ground | n/a | GND | GND |
17+
Connect the external serial interface to the ESP32(S2) board as follows.
18+
19+
```
20+
---------------------------------------------------------------------------------------
21+
| Target chip Interface | #define | Default ESP32(S2) Pin| External UART Pin |
22+
| ----------------------|------------------|----------------------|--------------------
23+
| Transmit Data (TxD) | EXAMPLE_UART_TXD | GPIO4 | RxD |
24+
| Receive Data (RxD) | EXAMPLE_UART_RXD | GPIO5 | TxD |
25+
| Ground | n/a | GND | GND |
26+
---------------------------------------------------------------------------------------
27+
```
28+
Note: The GPIO22 - GPIO25 can not be used with ESP32-S2 chip because they are reserved for internal use. Please refer to UART documentation for selected target.
2429

2530
Optionally, you can set-up and use a serial interface that has RTS and CTS signals in order to verify that the
2631
hardware control flow works. Connect the extra signals according to the following table, configure both extra pins in
2732
the example code by replacing existing `UART_PIN_NO_CHANGE` macros with the appropriate pin numbers and configure
2833
UART1 driver to use the hardware flow control by setting `.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS` and adding
2934
`.rx_flow_ctrl_thresh = 122`.
3035

31-
| ESP32 Interface | #define | ESP32 Pin | External UART Pin |
32-
| --- | --- | --- | --- |
33-
| Request to Send (RTS) | ECHO_TEST_RTS | GPIO18 | CTS |
34-
| Clear to Send (CTS) | ECHO_TEST_CTS | GPIO19 | RTS |
36+
```
37+
--------------------------------------------------------------------------------------
38+
| Target chip Interface | #define | Default ESP32(S2) Pin| External UART Pin |
39+
| ----------------------|-----------------|----------------------|--------------------
40+
| Transmit Data (TxD) | ECHO_TEST_RTS | GPIO18 | CTS |
41+
| Receive Data (RxD) | ECHO_TEST_CTS | GPIO19 | RTS |
42+
| Ground | n/a | GND | GND |
43+
--------------------------------------------------------------------------------------
44+
```
3545

3646
### Configure the project
3747

48+
Use the command below to configure project using Kconfig menu as showed in the table above.
49+
The default Kconfig values can be changed such as: EXAMPLE_TASK_STACK_SIZE, EXAMPLE_UART_BAUD_RATE, EXAMPLE_UART_PORT_NUM (Refer to Kconfig file).
3850
```
3951
idf.py menuconfig
4052
```
@@ -54,10 +66,9 @@ See the Getting Started Guide for full steps to configure and use ESP-IDF to bui
5466
## Example Output
5567

5668
Type some characters in the terminal connected to the external serial interface. As result you should see echo in the
57-
terminal which is used for flashing and monitoring. You can verify if the echo indeed comes from ESP32 by
69+
terminal which is used for flashing and monitoring. You can verify if the echo indeed comes from ESP32(S2) board by
5870
disconnecting either `TxD` or `RxD` pin: no characters will appear when typing.
5971

6072
## Troubleshooting
6173

62-
You are not supposed to see the echo in the terminal which is used for flashing and monitoring, but in the other one
63-
which is connected to UART1.
74+
You are not supposed to see the echo in the terminal which is used for flashing and monitoring, but in the other UART configured through Kconfig can be used.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
menu "Echo Example Configuration"
2+
3+
config EXAMPLE_UART_PORT_NUM
4+
int "UART port number"
5+
range 0 2 if IDF_TARGET_ESP32
6+
range 0 1 if IDF_TARGET_ESP32S2
7+
default 2 if IDF_TARGET_ESP32
8+
default 1 if IDF_TARGET_ESP32S2
9+
help
10+
UART communication port number for the example.
11+
See UART documentation for available port numbers.
12+
13+
config EXAMPLE_UART_BAUD_RATE
14+
int "UART communication speed"
15+
range 1200 115200
16+
default 115200
17+
help
18+
UART communication speed for Modbus example.
19+
20+
config EXAMPLE_UART_RXD
21+
int "UART RXD pin number"
22+
range 0 34 if IDF_TARGET_ESP32
23+
range 0 46 if IDF_TARGET_ESP32S2
24+
default 5
25+
help
26+
GPIO number for UART RX pin. See UART documentation for more information
27+
about available pin numbers for UART.
28+
29+
config EXAMPLE_UART_TXD
30+
int "UART TXD pin number"
31+
range 0 34 if IDF_TARGET_ESP32
32+
range 0 46 if IDF_TARGET_ESP32S2
33+
default 4
34+
help
35+
GPIO number for UART TX pin. See UART documentation for more information
36+
about available pin numbers for UART.
37+
38+
config EXAMPLE_TASK_STACK_SIZE
39+
int "UART echo example task stack size"
40+
range 1024 16384
41+
default 2048
42+
help
43+
Defines stack size for UART echo example. Insufficient stack size can cause crash.
44+
45+
endmenu

examples/peripherals/uart/uart_echo/main/uart_echo_example_main.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,28 @@
1111
#include "freertos/task.h"
1212
#include "driver/uart.h"
1313
#include "driver/gpio.h"
14+
#include "sdkconfig.h"
1415

1516
/**
16-
* This is an example which echos any data it receives on UART1 back to the sender,
17+
* This is an example which echos any data it receives on configured UART back to the sender,
1718
* with hardware flow control turned off. It does not use UART driver event queue.
1819
*
19-
* - Port: UART1
20+
* - Port: configured UART
2021
* - Receive (Rx) buffer: on
2122
* - Transmit (Tx) buffer: off
2223
* - Flow control: off
2324
* - Event queue: off
24-
* - Pin assignment: see defines below
25+
* - Pin assignment: see defines below (See Kconfig)
2526
*/
2627

27-
#define ECHO_TEST_TXD (GPIO_NUM_4)
28-
#define ECHO_TEST_RXD (GPIO_NUM_5)
29-
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
30-
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
28+
#define ECHO_TEST_TXD (CONFIG_EXAMPLE_UART_TXD)
29+
#define ECHO_TEST_RXD (CONFIG_EXAMPLE_UART_RXD)
30+
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
31+
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
32+
33+
#define ECHO_UART_PORT_NUM (CONFIG_EXAMPLE_UART_PORT_NUM)
34+
#define ECHO_UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE)
35+
#define ECHO_TASK_STACK_SIZE (CONFIG_EXAMPLE_TASK_STACK_SIZE)
3136

3237
#define BUF_SIZE (1024)
3338

@@ -36,29 +41,35 @@ static void echo_task(void *arg)
3641
/* Configure parameters of an UART driver,
3742
* communication pins and install the driver */
3843
uart_config_t uart_config = {
39-
.baud_rate = 115200,
44+
.baud_rate = ECHO_UART_BAUD_RATE,
4045
.data_bits = UART_DATA_8_BITS,
4146
.parity = UART_PARITY_DISABLE,
4247
.stop_bits = UART_STOP_BITS_1,
4348
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
4449
.source_clk = UART_SCLK_APB,
4550
};
46-
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
47-
uart_param_config(UART_NUM_1, &uart_config);
48-
uart_set_pin(UART_NUM_1, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
51+
int intr_alloc_flags = 0;
52+
53+
#if CONFIG_UART_ISR_IN_IRAM
54+
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
55+
#endif
56+
57+
ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
58+
ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
59+
ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));
4960

5061
// Configure a temporary buffer for the incoming data
5162
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
5263

5364
while (1) {
5465
// Read data from the UART
55-
int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 20 / portTICK_RATE_MS);
66+
int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, BUF_SIZE, 20 / portTICK_RATE_MS);
5667
// Write data back to the UART
57-
uart_write_bytes(UART_NUM_1, (const char *) data, len);
68+
uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);
5869
}
5970
}
6071

6172
void app_main(void)
6273
{
63-
xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
74+
xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
6475
}

0 commit comments

Comments
 (0)