first commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
|
||||
idf_build_set_property(MINIMAL_BUILD ON)
|
||||
project(echo_rs485)
|
||||
@@ -0,0 +1,82 @@
|
||||
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H21 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
|
||||
| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | --------- | -------- | -------- | -------- | -------- |
|
||||
|
||||
# UART RS485 Echo Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This is an example which echoes any data it receives on UART port 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.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware Required
|
||||
PC + USB Serial adapter connected to USB port + RS485 line drivers + Espressif development board.
|
||||
The MAX483 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 MAX483 | \ / | MAX483 DI|<----- TXD
|
||||
ESP32 BOARD | | RS-485 side | | SERIAL ADAPTER SIDE
|
||||
RTS --+--->| DE | / \ | DE|---+
|
||||
| | A|---------------|A | |
|
||||
+----| /RE | | /RE|---+-- RTS
|
||||
+-------x-------+ +-------x-------+
|
||||
| |
|
||||
--- ---
|
||||
```
|
||||
|
||||
#### Connect an external RS485 serial interface to an ESP32 board
|
||||
Connect a USB-to-RS485 adapter to a computer, then connect the adapter's A/B output lines with the corresponding A/B output lines of the RS485 line driver connected to the ESP32 chip (see figure above).
|
||||
```
|
||||
------------------------------------------------------------------------------------------------------------------------------
|
||||
| UART Interface | #define | Default pin for ESP32 | Default pins for others | External RS485 Driver Pin |
|
||||
| ----------------------|--------------------|-----------------------|---------------------------|---------------------------|
|
||||
| Transmit Data (TxD) | CONFIG_MB_UART_TXD | GPIO23 | GPIO9 | DI |
|
||||
| Receive Data (RxD) | CONFIG_MB_UART_RXD | GPIO22 | GPIO8 | RO |
|
||||
| Request To Send (RTS) | CONFIG_MB_UART_RTS | GPIO18 | GPIO10 | ~RE/DE |
|
||||
| Ground | n/a | GND | GND | GND |
|
||||
------------------------------------------------------------------------------------------------------------------------------
|
||||
```
|
||||
Note: Each target chip has different GPIO pins available for UART connection. Please refer to UART documentation for selected target for more information.
|
||||
|
||||
### Configure the project
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
### Build and Flash
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
```
|
||||
idf.py -p PORT 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 are 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 your board by disconnecting either `TxD` or `RxD` pin. Once done there should be no any `.` displayed.
|
||||
|
||||
## 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 hexadecimal form in the brackets.
|
||||
|
||||
## Troubleshooting
|
||||
When example software does not show the `.` symbol, the issue is most likely related to connection errors of the 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 the appropriate serial port.
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "rs485_example.c"
|
||||
REQUIRES nvs_flash esp_driver_uart
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,61 @@
|
||||
menu "Echo RS485 Example Configuration"
|
||||
|
||||
orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
|
||||
|
||||
config ECHO_UART_PORT_NUM
|
||||
int "UART port number"
|
||||
range 0 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
|
||||
default 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
|
||||
range 0 1
|
||||
default 1
|
||||
help
|
||||
UART communication port number for the example.
|
||||
See UART documentation for available port numbers.
|
||||
|
||||
config ECHO_UART_BAUD_RATE
|
||||
int "UART communication speed"
|
||||
range 1200 115200
|
||||
default 115200
|
||||
help
|
||||
UART communication speed for Modbus example.
|
||||
|
||||
config ECHO_UART_RXD
|
||||
int "UART RXD pin number"
|
||||
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
|
||||
default 27 if IDF_TARGET_ESP32P4
|
||||
default 22 if IDF_TARGET_ESP32
|
||||
default 8 if !IDF_TARGET_ESP32
|
||||
help
|
||||
GPIO number for UART RX pin. See UART documentation for more information
|
||||
about available pin numbers for UART.
|
||||
|
||||
config ECHO_UART_TXD
|
||||
int "UART TXD pin number"
|
||||
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
|
||||
default 26 if IDF_TARGET_ESP32P4
|
||||
default 23 if IDF_TARGET_ESP32
|
||||
default 9 if !IDF_TARGET_ESP32
|
||||
help
|
||||
GPIO number for UART TX pin. See UART documentation for more information
|
||||
about available pin numbers for UART.
|
||||
|
||||
# config ECHO_UART_RTS
|
||||
# int "UART RTS pin number"
|
||||
# range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
|
||||
# default -1 if IDF_TARGET_ESP32P4
|
||||
# default 18 if IDF_TARGET_ESP32
|
||||
# default 10 if !IDF_TARGET_ESP32
|
||||
# help
|
||||
# GPIO number for UART RTS pin. This pin is connected to
|
||||
# ~RE/DE pin of RS485 transceiver to switch direction.
|
||||
# See UART documentation for more information about available pin
|
||||
# numbers for UART.
|
||||
|
||||
config ECHO_TASK_STACK_SIZE
|
||||
int "UART echo RS485 example task stack size"
|
||||
range 1024 16384
|
||||
default 3072
|
||||
help
|
||||
Defines stack size for UART echo RS485 example. Insufficient stack size can cause crash.
|
||||
|
||||
endmenu
|
||||
@@ -0,0 +1,138 @@
|
||||
/* Uart Events Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/uart.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_log.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
/**
|
||||
* This is a example which echos any data it receives on UART back to the sender using RS485 interface in half duplex mode.
|
||||
*/
|
||||
#define TAG "RS485_ECHO_APP"
|
||||
|
||||
// Note: Some pins on target chip cannot be assigned for UART communication.
|
||||
// Please refer to documentation for selected board and target to configure pins using Kconfig.
|
||||
#define ECHO_TEST_TXD (CONFIG_ECHO_UART_TXD)
|
||||
#define ECHO_TEST_RXD (CONFIG_ECHO_UART_RXD)
|
||||
|
||||
// RTS for RS485 Half-Duplex Mode manages DE/~RE
|
||||
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
|
||||
|
||||
// CTS is not used in RS485 Half-Duplex Mode
|
||||
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
|
||||
|
||||
#define BUF_SIZE (127)
|
||||
#define BAUD_RATE (CONFIG_ECHO_UART_BAUD_RATE)
|
||||
|
||||
// Read packet timeout
|
||||
#define PACKET_READ_TICS (100 / portTICK_PERIOD_MS)
|
||||
#define ECHO_TASK_STACK_SIZE (CONFIG_ECHO_TASK_STACK_SIZE)
|
||||
#define ECHO_TASK_PRIO (10)
|
||||
#define ECHO_UART_PORT (CONFIG_ECHO_UART_PORT_NUM)
|
||||
|
||||
// Timeout threshold for UART = number of symbols (~10 tics) with unchanged state on receive pin
|
||||
#define ECHO_READ_TOUT (3) // 3.5T * 8 = 28 ticks, TOUT=3 -> ~24..33 ticks
|
||||
|
||||
static void echo_send(const int port, const char* str, uint8_t length)
|
||||
{
|
||||
if (uart_write_bytes(port, str, length) != length) {
|
||||
ESP_LOGE(TAG, "Send data critical failure.");
|
||||
// add your code to handle sending failure here
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
// An example of echo test with hardware flow control on UART
|
||||
static void echo_task(void *arg)
|
||||
{
|
||||
const int uart_num = ECHO_UART_PORT;
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = BAUD_RATE,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.rx_flow_ctrl_thresh = 122,
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
};
|
||||
|
||||
// Set UART log level
|
||||
esp_log_level_set(TAG, ESP_LOG_INFO);
|
||||
|
||||
ESP_LOGI(TAG, "Start RS485 application test and configure UART.");
|
||||
|
||||
// Install UART driver (we don't need an event queue here)
|
||||
// In this example we don't even use a buffer for sending data.
|
||||
ESP_ERROR_CHECK(uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0));
|
||||
|
||||
// Configure UART parameters
|
||||
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
|
||||
|
||||
ESP_LOGI(TAG, "UART set pins, mode and install driver.");
|
||||
|
||||
// Set UART pins as per KConfig settings
|
||||
ESP_ERROR_CHECK(uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));
|
||||
|
||||
// Set RS485 half duplex mode
|
||||
ESP_ERROR_CHECK(uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX));
|
||||
|
||||
// Set read timeout of UART TOUT feature
|
||||
ESP_ERROR_CHECK(uart_set_rx_timeout(uart_num, ECHO_READ_TOUT));
|
||||
|
||||
// Allocate buffers for UART
|
||||
uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
|
||||
assert(data);
|
||||
|
||||
ESP_LOGI(TAG, "UART start receive loop.\r");
|
||||
echo_send(uart_num, "Start RS485 UART test.\r\n", 24);
|
||||
|
||||
while (1) {
|
||||
// //Read data from UART
|
||||
int len = uart_read_bytes(uart_num, data, BUF_SIZE, PACKET_READ_TICS);
|
||||
|
||||
// //Write data back to UART
|
||||
// if (len > 0) {
|
||||
// echo_send(uart_num, "\r\n", 2);
|
||||
// char prefix[] = "RS485 Received: [";
|
||||
// echo_send(uart_num, prefix, (sizeof(prefix) - 1));
|
||||
// ESP_LOGI(TAG, "Received %u bytes:", len);
|
||||
// printf("[ ");
|
||||
// for (int i = 0; i < len; i++) {
|
||||
// printf("0x%.2X ", (uint8_t)data[i]);
|
||||
// echo_send(uart_num, (const char*)&data[i], 1);
|
||||
// // Add a Newline character if you get a return character from paste (Paste tests multibyte receipt/buffer)
|
||||
// if (data[i] == '\r') {
|
||||
// echo_send(uart_num, "\n", 1);
|
||||
// }
|
||||
// }
|
||||
// printf("] \n");
|
||||
// echo_send(uart_num, "]\r\n", 3);
|
||||
// } else {
|
||||
// Echo a "." to show we are alive while we wait for input
|
||||
echo_send(uart_num, "RS485 Test\r\n", 13);
|
||||
ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 10));
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
// }
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
//A uart read/write example without event queue;
|
||||
xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, ECHO_TASK_PRIO, NULL);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user