ESP32 IDF – Serial Communication Tutorial

Hi all… Now we will see ESP32 IDF Serial Communication Tutorial. ESP32 has three Serial ports. UART0, UART1 and UART2..

ESP32 IDF Serial Communication Tutorial

Suggest to Read

First we will take the example Program from example directory. Before coding i will explain the API and Structures which we are using in our code.

ESP32 IDF Serial Communication

APIs Used

  • uart_param_config()
  • uart_set_pin()
  • uart_driver_install()
  • uart_read_bytes()
  • uart_write_bytes()

uart_param_config()

This API is used to set the UART configuration like baudrate, stop bits etc.  This API contains Two arguments. Have a look at this API below.

esp_err_t uart_param_config(uart_port_t uart_numconst uart_config_t *uart_config)

Return

  • ESP_OK Success
  • ESP_FAIL Parameter error

Parameters

  • uart_num: UART_NUM_0, UART_NUM_1 or UART_NUM_2
  • uart_config: UART parameter settings

Here Second argument is a structure’s address. That structure is used to set the configuration settings. 

structuart_config_t

MembersDescription
int baud_rateUART baudrate
uart_word_length_t data_bitsUART byte size
uart_parity_t parityUART parity mode
uart_stop_bits_t stop_bitsUART stop bits
uart_hw_flowcontrol_t flow_ctrlUART HW flow control mode(cts/rts)
uint8_t rx_flow_ctrl_threshUART HW RTS threshold

uart_set_pin()

This function is used to configure the UART pin in ESP32. Using this function we can select any pin as a TX, RX, CTS and RTS. This function contains five arguments. Internal signal can be output to multiple GPIO pads. Only one GPIO pad can connect with input signal.

esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num)

Return

  • ESP_OK Success
  • ESP_FAIL Parameter error

Parameters

  • uart_num: UART_NUM_0, UART_NUM_1 or UART_NUM_2
  • tx_io_num: UART TX pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin.
  • rx_io_num: UART RX pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin.
  • rts_io_num: UART RTS pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin.
  • cts_io_num: UART CTS pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin.

uart_driver_install()

This API is used to install the uart_driver. This API contains six arguments. UART ISR handler will be attached to the same CPU core that this function is running on. Users should know that which CPU is running and then pick a INUM that is not used by system. We can find the information of INUM and interrupt level in soc.h.

esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, int uart_intr_num, void *uart_queue) 

Return

  • ESP_OK Success
  • ESP_FAIL Parameter error

Parameters

  • uart_num: UART_NUM_0, UART_NUM_1 or UART_NUM_2
  • rx_buffer_size: UART RX ring buffer size
  • tx_buffer_size: UART TX ring buffer size. If set to zero, driver will not use TX buffer, TX function will block task until all data have been sent out.
  • queue_size: UART event queue size/depth.
  • uart_intr_num: UART interrupt number,check the info in soc.h, and please refer to core-isa.h for more details
  • uart_queue: UART event queue handle, if set NULL, driver will not use an event queue.

uart_read_bytes()

This API is used to read the data from UART buffer. This contains four arguments.

int uart_read_bytes(uart_port_t uart_num, uint8_t *buf, uint32_t length, TickType_t ticks_to_wait)

Return

  • (-1) Error
  • Others return a char data from uart fifo.

Parameters

  • uart_num: UART_NUM_0, UART_NUM_1 or UART_NUM_2
  • buf: pointer to the buffer.
  • length: data length
  • ticks_to_wait: sTimeout, count in RTOS ticks

uart_write_bytes()

This API is used to write the data into the UART port from a given buffer and length. This has three arguments. 

int uart_write_bytes(uart_port_t uart_numconst char *src, size_t size)

If parameter tx_buffer_size is set to zero: This function will not return until all the data have been sent out, or at least pushed into TX FIFO.

Otherwise, if tx_buffer_size > 0, this function will return after copying all the data to tx ringbuffer, then, UART ISR will move data from ring buffer to TX FIFO gradually.

Return

  • (-1) Parameter error
  • OTHERS(>=0) The number of data that pushed to the TX FIFO

Parameters

  • uart_num: UART_NUM_0, UART_NUM_1 or UART_NUM_2
  • src: data buffer address
  • size: data length to send

Example Code 1

This is a example example which echos any data it receives on UART1 back to the sender, with hardware flow control turned off. It does not use UART driver event queue. 

  • port: UART1
  • rx buffer: on
  • tx buffer: off
  • flow control: off
  • event queue: off
  • pin assignment: txd(io4), rxd(io5)

Output

ESP32 IDF Serial Communication

Example Code 2

In this example I’m going to use two UARTs. Whatever i’m typing in UART 0 it sends to UART1 and UART0.

UART 0:

  • port: UART0
  • rx buffer: on
  • tx buffer: off
  • flow control: off
  • event queue: off
  • pin assignment: txd(default), rxd(default)

UART 1:

  • port: UART1
  • rx buffer: on
  • tx buffer: off
  • flow control: off
  • event queue: off
  • pin assignment: txd(io4), rxd(io5)

Output

0 0 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: