|
1 | 1 | Universal Asynchronous Receiver/Transmitter (UART) |
2 | 2 | ================================================== |
3 | 3 |
|
| 4 | +:link_to_translation:`zh_CN:[中文]` |
| 5 | + |
4 | 6 | {IDF_TARGET_UART_EXAMPLE_PORT:default = "UART_NUM_1", esp32 = "UART_NUM_2", esp32s3 = "UART_NUM_2"} |
5 | 7 |
|
6 | 8 | Introduction |
@@ -114,11 +116,15 @@ Install Drivers |
114 | 116 |
|
115 | 117 | Once the communication pins are set, install the driver by calling :cpp:func:`uart_driver_install` and specify the following parameters: |
116 | 118 |
|
| 119 | +- UART port number |
117 | 120 | - Size of TX ring buffer |
118 | 121 | - Size of RX ring buffer |
119 | | -- Event queue handle and size |
| 122 | +- Pointer to store the event queue handle |
| 123 | +- Event queue size |
120 | 124 | - Flags to allocate an interrupt |
121 | 125 |
|
| 126 | +.. _driver-code-snippet: |
| 127 | + |
122 | 128 | The function allocates the required internal resources for the UART driver. |
123 | 129 |
|
124 | 130 | .. code-block:: c |
@@ -225,23 +231,45 @@ Use Interrupts |
225 | 231 |
|
226 | 232 | There are many interrupts that can be generated depending on specific UART states or detected errors. The full list of available interrupts is provided in *{IDF_TARGET_NAME} Technical Reference Manual* > *UART Controller (UART)* > *UART Interrupts* and *UHCI Interrupts* [`PDF <{IDF_TARGET_TRM_EN_URL}#uart>`__]. You can enable or disable specific interrupts by calling :cpp:func:`uart_enable_intr_mask` or :cpp:func:`uart_disable_intr_mask` respectively. |
227 | 233 |
|
228 | | -The :cpp:func:`uart_driver_install` function installs the driver's internal interrupt handler to manage the TX and RX ring buffers and provides high-level API functions like events (see below). |
| 234 | +The UART driver provides a convenient way to handle specific interrupts by wrapping them into corresponding events. Events defined in :cpp:type:`uart_event_type_t` can be reported to a user application using the FreeRTOS queue functionality. |
| 235 | + |
| 236 | +To receive the events that have happened, call :cpp:func:`uart_driver_install` and get the event queue handle returned from the function. Please see the above :ref:`code snippet <driver-code-snippet>` as an example. |
| 237 | + |
| 238 | +The processed events include the following: |
229 | 239 |
|
230 | | -The API provides a convenient way to handle specific interrupts discussed in this document by wrapping them into dedicated functions: |
| 240 | +- **FIFO overflow** (:cpp:enumerator:`UART_FIFO_OVF`): The RX FIFO can trigger an interrupt when it receives more data than the FIFO can store. |
231 | 241 |
|
232 | | -- **Event detection**: There are several events defined in :cpp:type:`uart_event_type_t` that may be reported to a user application using the FreeRTOS queue functionality. You can enable this functionality when calling :cpp:func:`uart_driver_install` described in :ref:`uart-api-driver-installation`. An example of using Event detection can be found in :example:`peripherals/uart/uart_events`. |
| 242 | + - (Optional) Configure the full threshold of the FIFO space by entering it in the structure :cpp:type:`uart_intr_config_t` and call :cpp:func:`uart_intr_config` to set the configuration. This can help the data stored in the RX FIFO can be processed timely in the driver to avoid FIFO overflow. |
| 243 | + - Enable the interrupts using the functions :cpp:func:`uart_enable_rx_intr`. |
| 244 | + - Disable these interrupts using the corresponding functions :cpp:func:`uart_disable_rx_intr`. |
233 | 245 |
|
234 | | -- **FIFO space threshold or transmission timeout reached**: The TX and RX FIFO buffers can trigger an interrupt when they are filled with a specific number of characters, or on a timeout of sending or receiving data. To use these interrupts, do the following: |
| 246 | + .. code-block:: c |
235 | 247 |
|
236 | | - - Configure respective threshold values of the buffer length and timeout by entering them in the structure :cpp:type:`uart_intr_config_t` and calling :cpp:func:`uart_intr_config` |
237 | | - - Enable the interrupts using the functions :cpp:func:`uart_enable_tx_intr` and :cpp:func:`uart_enable_rx_intr` |
238 | | - - Disable these interrupts using the corresponding functions :cpp:func:`uart_disable_tx_intr` or :cpp:func:`uart_disable_rx_intr` |
| 248 | + const uart_port_t uart_num = {IDF_TARGET_UART_EXAMPLE_PORT}; |
| 249 | + // Configure a UART interrupt threshold and timeout |
| 250 | + uart_intr_config_t uart_intr = { |
| 251 | + .intr_enable_mask = UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT, |
| 252 | + .rxfifo_full_thresh = 100, |
| 253 | + .rx_timeout_thresh = 10, |
| 254 | + }; |
| 255 | + ESP_ERROR_CHECK(uart_intr_config(uart_num, &uart_intr)); |
239 | 256 |
|
240 | | -- **Pattern detection**: An interrupt triggered on detecting a 'pattern' of the same character being received/sent repeatedly. This functionality is demonstrated in the example :example:`peripherals/uart/uart_events`. It can be used, e.g., to detect a command string with a specific number of identical characters (the 'pattern') at the end. The following functions are available: |
| 257 | + // Enable UART RX FIFO full threshold and timeout interrupts |
| 258 | + ESP_ERROR_CHECK(uart_enable_rx_intr(uart_num)); |
| 259 | +
|
| 260 | +- **Pattern detection** (:cpp:enumerator:`UART_PATTERN_DET`): An interrupt triggered on detecting a 'pattern' of the same character being received/sent repeatedly. It can be used, e.g., to detect a command string with a specific number of identical characters (the 'pattern') at the end. The following functions are available: |
241 | 261 |
|
242 | 262 | - Configure and enable this interrupt using :cpp:func:`uart_enable_pattern_det_baud_intr` |
243 | 263 | - Disable the interrupt using :cpp:func:`uart_disable_pattern_det_intr` |
244 | 264 |
|
| 265 | + .. code-block:: c |
| 266 | +
|
| 267 | + //Set UART pattern detect function |
| 268 | + uart_enable_pattern_det_baud_intr(EX_UART_NUM, '+', PATTERN_CHR_NUM, 9, 0, 0); |
| 269 | +
|
| 270 | +- **Other events**: The UART driver can report other events such as data receiving (:cpp:enumerator:`UART_DATA`), ring buffer full (:cpp:enumerator:`UART_BUFFER_FULL`), detecting NULL after the stop bit (:cpp:enumerator:`UART_BREAK`), parity check error (:cpp:enumerator:`UART_PARITY_ERR`), and frame error (:cpp:enumerator:`UART_FRAME_ERR`). |
| 271 | + |
| 272 | +The strings inside of brackets indicate corresponding event names. An example of how to handle various UART events can be found in :example:`peripherals/uart/uart_events`. |
245 | 273 |
|
246 | 274 | .. _uart-api-deleting-driver: |
247 | 275 |
|
|
0 commit comments