From a9fca0c77510b9111df0eeba81dd6b20913ae202 Mon Sep 17 00:00:00 2001 From: Alex Lisitsyn Date: Mon, 2 Jul 2018 15:21:02 +0200 Subject: [PATCH] driver: add rs485 half duplex interface support to uart driver (fix after review) An existing UART driver does not support RS485 half duplex mode. This task adds this functionality to ESP_IDF UART driver. driver/uart.c/h: updated to add support of RS485 half duplex mode examples/peripherals/uart_echo_rs485/main/rs485_example.c: added test example components/driver/test/test_uart.c: added test of RS485 half duplex mode docs/en/api-reference/peripherals/uart.rst: updated documentation test_uart.c: suppress GCC warnings about discarded const qualifiers uart.rst: remove sphinx warning - "Duplicate explicit target name" simple change in uart.h file update (test_uart.c) after rebase from master update uart.rst, uart.c, rs485_example.c Update example description in file Readme.md update uart.c/h, uart.rst, test_uart.c according to review results update uart.h (uart_set_rx_timeout() description test_uart.c remove ignore tag uart.c/h: fix param errors test_uart.c: Remove GCC warning supress uart.rst: fix the notes rs485_example.c: fix output uart.c: remove reset using UART_RXFIFO_RST from driver. readme.md: Update example description The tests are completed using RS485 adapters hardware connected to two ESP32 WROVER KITs. TW#13812 Closes https://github.com/espressif/esp-idf/pull/667 Closes https://github.com/espressif/esp-idf/pull/1006 --- components/driver/uart.c | 2 - .../peripherals/uart_echo_rs485/README.md | 64 +++++++++++++++---- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/components/driver/uart.c b/components/driver/uart.c index 7fe6045dd..828b22805 100644 --- a/components/driver/uart.c +++ b/components/driver/uart.c @@ -984,8 +984,6 @@ static void uart_rx_intr_handler_default(void *param) // reset RTS pin to start receiver driver if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX)) { UART_ENTER_CRITICAL_ISR(&uart_spinlock[uart_num]); - uart_reg->conf0.rxfifo_rst = 1; // Workaround to clear phantom 00 characters - uart_reg->conf0.rxfifo_rst = 0; // received after transmission. uart_reset_rx_fifo(uart_num); // Allows to avoid hardware issue with the RXFIFO reset uart_reg->conf0.sw_rts = 1; UART_EXIT_CRITICAL_ISR(&uart_spinlock[uart_num]); diff --git a/examples/peripherals/uart_echo_rs485/README.md b/examples/peripherals/uart_echo_rs485/README.md index a71dc5597..ddba04c69 100644 --- a/examples/peripherals/uart_echo_rs485/README.md +++ b/examples/peripherals/uart_echo_rs485/README.md @@ -1,27 +1,34 @@ # UART RS485 Echo Example -This is an example which echoes any data it receives on UART2 back to the sender. +This is an example which echoes any data it receives on UART2 back to the sender in the RS485 network. +It uses ESP-IDF UART software driver in RS485 half duplex transmission mode and requires external connection of bus drivers. +The approach demonstrated in this example can be used in user application to transmit/receive data in RS485 networks. -## Setup -This example uses external RS485 interface. The MAX485 line driver can be used for example. - -RS485 example connection circuit schematic: +## Hardware required : +PC + USB Serial adapter connected to USB port + RS485 line drivers + ESP32 WROVER-KIT board. +The MAX485 line driver is used for example below but other similar chips can be used as well. +### RS485 example connection circuit schematic: +``` VCC ---------------+ +--------------- VCC | | +-------x-------+ +-------x-------+ RXD <------| RO | | RO|-----> RXD | B|---------------|B | TXD ------>| DI MAX485 | \ / | MAX485 DI|<----- TXD -ESP32 WROVER KIT | | RS-485 side | | SERIAL ADAPTER +ESP32 WROVER-KIT | | RS-485 side | | SERIAL ADAPTER SIDE RTS --+--->| DE | / \ | DE|---+ | | A|---------------|A | | +----| /RE | | /RE|---+-- RTS +-------x-------+ +-------x-------+ | | --- --- +``` -1. Connect an external RS485 serial interface to an ESP32 board. +## How to setup and use an example: + +### Connect an external RS485 serial interface to an ESP32 board. +``` ---------------------------------------------------------------------- | ESP32 Interface | #define | ESP32 Pin | External RS485 | | ----------------------|---------------|-----------| Driver Pin | @@ -30,10 +37,41 @@ ESP32 WROVER KIT | | RS-485 side | | SERIAL AD | Request To Send (RTS) | ECHO_TEST_RTS | GPIO18 | ~RE/DE | | Ground | n/a | GND | GND | ---------------------------------------------------------------------- -2. Connect USB to RS485 adapter to computer and connect its D+, D- output lines with the D+, D- lines of RS485 driver connected to ESP32. -3. Compile and load the example to the ESP32 board -4. Refer to the example and set up a serial terminal program to the same settings as of UART in ESP32 -5. Open the external serial interface in the terminal. -6. By default if no any symbols received the application sends character "." to check transmission side. When typing message and push send button in the terminal you should see the message "RS485 Received: [ your message ], where your message is the message you sent from terminal. -7. Verify if echo indeed comes from ESP32 by disconnecting either 'TxD' or 'RxD' pin. There should be no any "." once TxD pin is disconnected. +``` +Connect USB to RS485 adapter to computer and connect its D+, D- output lines with the D+, D- lines of RS485 line driver connected to ESP32 (See picture above). + +### Configure the application +``` +make menuconfig +``` +* Set serial port under Serial Flasher Options to the serial port of ESP32 WROVER-KIT board. + +### Build and flash software +Build the project and flash it to the board, then run monitor tool to view serial output: +``` +make -j4 flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +### Setup external terminal software +Refer to the example and set up a serial terminal program to the same settings as of UART in ESP32 WROVER-KIT BOARD. +Open the external serial interface in the terminal. By default if no any symbols received the application sends character "." to check transmission side. +When typing message and push send button in the terminal you should see the message "RS485 Received: [ your message ], where your message is the message you sent from terminal. +Verify if echo indeed comes from ESP32 by disconnecting either 'TxD' or 'RxD' pin. There should be no any "." once TxD pin is disconnected. + +## Example Output +Example output of the application: +``` +I (655020) RS485_ECHO_APP: Received 12 bytes: +[ 0x79 0x6F 0x75 0x72 0x20 0x6D 0x65 0x73 0x73 0x61 0x67 0x65 ] +``` +The received message is showed in HEX form in the brackets. + +## Troubleshooting +Most of the issues when example software does not show the '.' symbol related to connection errors of external RS485 interface. +Check the RS485 interface connection with the environment according to schematic above and restart the application. +Then start terminal software and open appropriate serial port.