modbus: update to support other targets

This commit is contained in:
Alex Lisitsyn
2021-11-08 16:28:55 +00:00
parent c49d9e152c
commit 0586be45d2
17 changed files with 125 additions and 92 deletions

View File

@@ -2,9 +2,9 @@ menu "Echo Example Configuration"
config EXAMPLE_UART_PORT_NUM
int "UART port number"
range 0 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
range 0 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
default 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
range 0 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3
default 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
default 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3
help
UART communication port number for the example.
@@ -22,6 +22,7 @@ menu "Echo Example Configuration"
range 0 34 if IDF_TARGET_ESP32
range 0 46 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
range 0 19 if IDF_TARGET_ESP32C3
range 0 47 if IDF_TARGET_ESP32S3
default 5
help
GPIO number for UART RX pin. See UART documentation for more information
@@ -32,6 +33,7 @@ menu "Echo Example Configuration"
range 0 34 if IDF_TARGET_ESP32
range 0 46 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
range 0 19 if IDF_TARGET_ESP32C3
range 0 47 if IDF_TARGET_ESP32S3
default 4
help
GPIO number for UART TX pin. See UART documentation for more information

View File

@@ -12,6 +12,7 @@
#include "driver/uart.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "esp_log.h"
/**
* This is an example which echos any data it receives on configured UART back to the sender,
@@ -34,6 +35,8 @@
#define ECHO_UART_BAUD_RATE (CONFIG_EXAMPLE_UART_BAUD_RATE)
#define ECHO_TASK_STACK_SIZE (CONFIG_EXAMPLE_TASK_STACK_SIZE)
static const char *TAG = "UART TEST";
#define BUF_SIZE (1024)
static void echo_task(void *arg)
@@ -63,9 +66,13 @@ static void echo_task(void *arg)
while (1) {
// Read data from the UART
int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, BUF_SIZE, 20 / portTICK_RATE_MS);
int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, (BUF_SIZE - 1), 20 / portTICK_RATE_MS);
// Write data back to the UART
uart_write_bytes(ECHO_UART_PORT_NUM, (const char *) data, len);
if (len) {
data[len] = '\0';
ESP_LOGI(TAG, "Recv str: %s", (char *) data);
}
}
}

View File

@@ -1,3 +1,6 @@
| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 |
| ----------------- | ----- | -------- | -------- | -------- |
# UART RS485 Echo Example
(See the README.md file in the upper level 'examples' directory for more information about examples.)
@@ -9,7 +12,7 @@ The approach demonstrated in this example can be used in user application to tra
## How to use example
### Hardware Required
PC + USB Serial adapter connected to USB port + RS485 line drivers + ESP32, ESP32-S or ESP32-C series based board.
PC + USB Serial adapter connected to USB port + RS485 line drivers + Espressif development board.
The MAX485 line driver is used for example below but other similar chips can be used as well.
#### RS485 example connection circuit schematic:
@@ -20,7 +23,7 @@ The MAX485 line driver is used for example below but other similar chips can be
RXD <------| RO | | RO|-----> RXD
| B|---------------|B |
TXD ------>| DI MAX485 | \ / | MAX485 DI|<----- TXD
ESP dev kit | | RS-485 side | | SERIAL ADAPTER SIDE
ESP32 BOARD | | RS-485 side | | SERIAL ADAPTER SIDE
RTS --+--->| DE | / \ | DE|---+
| | A|---------------|A | |
+----| /RE | | /RE|---+-- RTS
@@ -29,19 +32,20 @@ ESP dev kit | | RS-485 side | | SERIAL AD
--- ---
```
#### Connect an external RS485 serial interface to an ESP board
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 the ESP board (See picture above). To view or adjust default pins please see the `Echo RS485 Example Configuration` submenu in `idf.py menuconfig`.
#### 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).
```
--------------------------------------------------------------------------------------------------
| ESP Interface | #define | Default ESP Pin | External RS485 Driver Pin |
| ----------------------|--------------------|-----------------------|---------------------------|
| Transmit Data (TxD) | CONFIG_MB_UART_TXD | CONFIG_ECHO_UART_TXD | DI |
| Receive Data (RxD) | CONFIG_MB_UART_RXD | CONFIG_ECHO_UART_RXD | RO |
| Request To Send (RTS) | CONFIG_MB_UART_RTS | CONFIG_ECHO_UART_RTS | ~RE/DE |
| Ground | n/a | GND | GND |
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
| UART Interface | #define | Default ESP32 Pin | Default pins for | External RS485 Driver Pin |
| | | | ESP32-S2(S3, C3) | |
| ----------------------|--------------------|-----------------------|-----------------------|---------------------------|
| 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: Some GPIOs can not be used with some chip because they are used for flash chip connection. Please refer to UART documentation for selected target.
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
```

View File

@@ -3,8 +3,8 @@ menu "Echo RS485 Example Configuration"
config ECHO_UART_PORT_NUM
int "UART port number"
range 0 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
range 0 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3
default 2 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
range 0 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3
default 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3
help
UART communication port number for the example.
@@ -22,10 +22,9 @@ menu "Echo RS485 Example Configuration"
range 0 34 if IDF_TARGET_ESP32
default 22 if IDF_TARGET_ESP32
range 0 46 if IDF_TARGET_ESP32S2
default 19 if IDF_TARGET_ESP32S2
range 0 48 if IDF_TARGET_ESP32S3
range 0 47 if IDF_TARGET_ESP32S3
range 0 19 if IDF_TARGET_ESP32C3
default 5 if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S3
default 8 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32C3
help
GPIO number for UART RX pin. See UART documentation for more information
about available pin numbers for UART.
@@ -35,10 +34,9 @@ menu "Echo RS485 Example Configuration"
range 0 34 if IDF_TARGET_ESP32
default 23 if IDF_TARGET_ESP32
range 0 46 if IDF_TARGET_ESP32S2
default 20 if IDF_TARGET_ESP32S2
range 0 48 if IDF_TARGET_ESP32S3
range 0 47 if IDF_TARGET_ESP32S3
range 0 19 if IDF_TARGET_ESP32C3
default 4 if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S3
default 9 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32C3
help
GPIO number for UART TX pin. See UART documentation for more information
about available pin numbers for UART.
@@ -46,10 +44,11 @@ menu "Echo RS485 Example Configuration"
config ECHO_UART_RTS
int "UART RTS pin number"
range 0 34 if IDF_TARGET_ESP32
default 18 if IDF_TARGET_ESP32
range 0 46 if IDF_TARGET_ESP32S2
range 0 48 if IDF_TARGET_ESP32S3
range 0 47 if IDF_TARGET_ESP32S3
range 0 19 if IDF_TARGET_ESP32C3
default 18
default 10 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32C3
help
GPIO number for UART RTS pin. This pin is connected to
~RE/DE pin of RS485 transceiver to switch direction.