From 76bde2df7253df92e8f9911be848afc5ca571e42 Mon Sep 17 00:00:00 2001 From: Alex Lisitsyn Date: Mon, 20 Jun 2022 15:34:06 +0800 Subject: [PATCH] modbus: update to support other targets (Backport v4.4) --- components/freemodbus/CMakeLists.txt | 4 -- .../freemodbus/common/esp_modbus_slave.c | 1 + components/freemodbus/port/portserial.c | 2 +- .../tcp_master/port/port_tcp_master.c | 1 + .../tcp_slave/port/port_tcp_slave.c | 1 + .../uart/uart_echo/main/Kconfig.projbuild | 6 ++- .../uart_echo/main/uart_echo_example_main.c | 9 ++++- .../uart/uart_echo_rs485/README.md | 30 ++++++++------- .../uart_echo_rs485/main/Kconfig.projbuild | 17 ++++----- .../protocols/modbus/serial/example_test.py | 3 +- .../modbus/serial/mb_master/README.md | 29 ++++++++------- .../serial/mb_master/main/Kconfig.projbuild | 21 +++++++---- .../modbus/serial/mb_slave/README.md | 37 ++++++++++--------- .../serial/mb_slave/main/Kconfig.projbuild | 21 +++++++---- .../modbus/tcp/mb_tcp_master/README.md | 17 +++++---- .../modbus/tcp/mb_tcp_slave/README.md | 17 +++++---- 16 files changed, 124 insertions(+), 92 deletions(-) diff --git a/components/freemodbus/CMakeLists.txt b/components/freemodbus/CMakeLists.txt index 482616eb1b..aea9e8d900 100644 --- a/components/freemodbus/CMakeLists.txt +++ b/components/freemodbus/CMakeLists.txt @@ -1,7 +1,3 @@ -idf_build_get_property(target IDF_TARGET) - -# The following five lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly set(srcs "common/esp_modbus_master.c" "common/esp_modbus_slave.c" diff --git a/components/freemodbus/common/esp_modbus_slave.c b/components/freemodbus/common/esp_modbus_slave.c index 601a5fa1fe..1faff76d90 100644 --- a/components/freemodbus/common/esp_modbus_slave.c +++ b/components/freemodbus/common/esp_modbus_slave.c @@ -14,6 +14,7 @@ */ #include "esp_err.h" // for esp_err_t +#include "esp_timer.h" // for esp_timer_get_time() #include "sdkconfig.h" // for KConfig defines #include "mbc_slave.h" // for slave private type definitions diff --git a/components/freemodbus/port/portserial.c b/components/freemodbus/port/portserial.c index ccbb4bd284..67b56bf6fc 100644 --- a/components/freemodbus/port/portserial.c +++ b/components/freemodbus/port/portserial.c @@ -226,7 +226,7 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, .rx_flow_ctrl_thresh = 2, - .use_ref_tick = UART_SCLK_APB, + .source_clk = UART_SCLK_APB, }; // Set UART config xErr = uart_param_config(ucUartNumber, &xUartConfig); diff --git a/components/freemodbus/tcp_master/port/port_tcp_master.c b/components/freemodbus/tcp_master/port/port_tcp_master.c index 16ad2bcbc3..ec0210bb2b 100644 --- a/components/freemodbus/tcp_master/port/port_tcp_master.c +++ b/components/freemodbus/tcp_master/port/port_tcp_master.c @@ -39,6 +39,7 @@ #include #include #include "esp_err.h" +#include "esp_timer.h" /* ----------------------- lwIP includes ------------------------------------*/ #include "lwip/err.h" diff --git a/components/freemodbus/tcp_slave/port/port_tcp_slave.c b/components/freemodbus/tcp_slave/port/port_tcp_slave.c index d46e8857b8..b64ad8efd9 100644 --- a/components/freemodbus/tcp_slave/port/port_tcp_slave.c +++ b/components/freemodbus/tcp_slave/port/port_tcp_slave.c @@ -39,6 +39,7 @@ #include #include #include "esp_err.h" +#include "esp_timer.h" #include "sys/time.h" #include "esp_netif.h" diff --git a/examples/peripherals/uart/uart_echo/main/Kconfig.projbuild b/examples/peripherals/uart/uart_echo/main/Kconfig.projbuild index a875c35529..74d23c5bd8 100644 --- a/examples/peripherals/uart/uart_echo/main/Kconfig.projbuild +++ b/examples/peripherals/uart/uart_echo/main/Kconfig.projbuild @@ -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 diff --git a/examples/peripherals/uart/uart_echo/main/uart_echo_example_main.c b/examples/peripherals/uart/uart_echo/main/uart_echo_example_main.c index e2c4a56249..52ffb94932 100644 --- a/examples/peripherals/uart/uart_echo/main/uart_echo_example_main.c +++ b/examples/peripherals/uart/uart_echo/main/uart_echo_example_main.c @@ -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); + } } } diff --git a/examples/peripherals/uart/uart_echo_rs485/README.md b/examples/peripherals/uart/uart_echo_rs485/README.md index 6d58987af5..83750a7efb 100644 --- a/examples/peripherals/uart/uart_echo_rs485/README.md +++ b/examples/peripherals/uart/uart_echo_rs485/README.md @@ -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 ``` diff --git a/examples/peripherals/uart/uart_echo_rs485/main/Kconfig.projbuild b/examples/peripherals/uart/uart_echo_rs485/main/Kconfig.projbuild index 3daec6909e..237c12dbb7 100644 --- a/examples/peripherals/uart/uart_echo_rs485/main/Kconfig.projbuild +++ b/examples/peripherals/uart/uart_echo_rs485/main/Kconfig.projbuild @@ -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. diff --git a/examples/protocols/modbus/serial/example_test.py b/examples/protocols/modbus/serial/example_test.py index c15018262f..f8737ff1d3 100644 --- a/examples/protocols/modbus/serial/example_test.py +++ b/examples/protocols/modbus/serial/example_test.py @@ -165,7 +165,7 @@ def test_check_mode(dut=None, mode_str=None, value=None): return False -@ttfw_idf.idf_example_test(env_tag='Example_T2_RS485') +@ttfw_idf.idf_example_test(env_tag='Example_T2_RS485', target=['esp32']) def test_modbus_communication(env, comm_mode): global logger @@ -283,6 +283,5 @@ if __name__ == '__main__': logger.addHandler(fh) logger.addHandler(ch) logger.info('Start script %s.' % os.path.basename(__file__)) - print('Logging file name: %s' % logger.handlers[0].baseFilename) test_modbus_communication() logging.shutdown() diff --git a/examples/protocols/modbus/serial/mb_master/README.md b/examples/protocols/modbus/serial/mb_master/README.md index 543d152be9..107d72aa17 100644 --- a/examples/protocols/modbus/serial/mb_master/README.md +++ b/examples/protocols/modbus/serial/mb_master/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | -| ----------------- | ----- | +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | # Modbus Master Example @@ -58,11 +58,11 @@ Modbus multi slave segment connection schematic: ## Hardware required : Option 1: -PC (Modbus Slave app) + USB Serial adapter connected to USB port + RS485 line drivers + ESP32 WROVER-KIT board. +PC (Modbus Slave app) + USB Serial adapter connected to USB port + RS485 line drivers + ESP32 based board Option 2: -Several ESP32 WROVER-KIT board flashed with modbus_slave example software to represent slave device with specific slave address (See CONFIG_MB_SLAVE_ADDR). The slave addresses for each board have to be configured as defined in "connection schematic" above. -One ESP32 WROVER-KIT board flashed with modbus_master example. All the boards require connection of RS485 line drivers (see below). +Several ESP32 boards flashed with modbus_slave example software to represent slave device with specific slave address (See CONFIG_MB_SLAVE_ADDR). The slave addresses for each board have to be configured as defined in "connection schematic" above. +One ESP32 board flashed with modbus_master example. All the boards require connection of RS485 line drivers (see below). The MAX485 line driver is used as an example below but other similar chips can be used as well. RS485 example circuit schematic for connection of master and slave devices into segment: @@ -73,8 +73,8 @@ RS485 example circuit schematic for connection of master and slave devices into RXD <------| RO | DIFFERENTIAL | RO|-----> RXD | B|---------------|B | TXD ------>| DI MAX485 | \ / | MAX485 DI|<----- TXD -ESP32 WROVER KIT 1 | | RS-485 side | | External PC (emulator) with USB to serial or - RTS --+--->| DE | / \ | DE|---+ ESP32 WROVER KIT 2 (slave) +ESP32 BOARD | | RS-485 side | | External PC (emulator) with USB to serial or + RTS --+--->| DE | / \ | DE|---+ ESP32 BOARD (slave) | | A|---------------|A | | +----| /RE | PAIR | /RE|---+-- RTS +-------x-------+ +-------x-------+ @@ -96,17 +96,18 @@ Define the communication mode parameter for master and slave in Kconfig - CONFIG Configure the slave address for each slave in the Modbus segment (the CONFIG_MB_SLAVE_ADDR in Kconfig). ``` -------------------------------------------------------------------------------------------------------------------------- - | ESP32 Interface | #define | Default ESP32 Pin | Default ESP32-S2 Pins | External RS485 Driver Pin | + | 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 | GPIO20 | DI | - | Receive Data (RxD) | CONFIG_MB_UART_RXD | GPIO22 | GPIO19 | RO | - | Request To Send (RTS) | CONFIG_MB_UART_RTS | GPIO18 | GPIO18 | ~RE/DE | + | 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: The GPIO22 - GPIO25 can not be used with ESP32-S2 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. -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). +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). The communication parameters of Modbus stack allow to configure it appropriately but usually it is enough to use default settings. See the help string of parameters for more information. @@ -116,7 +117,7 @@ Option 1: Configure the external Modbus master software according to port configuration parameters used in the example. The Modbus Slave application can be used with this example to emulate slave devices with its parameters. Use official documentation for software to setup emulation of slave devices. Option 2: -Other option is to have the modbus_slave example application flashed into ESP32 WROVER KIT board and connect boards together as showed on the Modbus connection schematic above. See the Modbus slave API documentation to configure communication parameters and slave addresses as defined in "Example parameters definition" table above. +Other option is to have the modbus_slave example application flashed into ESP32 based board and connect boards together as showed on the Modbus connection schematic above. See the Modbus slave API documentation to configure communication parameters and slave addresses as defined in "Example parameters definition" table above. ### Build and flash software of master device Build the project and flash it to the board, then run monitor tool to view serial output: diff --git a/examples/protocols/modbus/serial/mb_master/main/Kconfig.projbuild b/examples/protocols/modbus/serial/mb_master/main/Kconfig.projbuild index b5b2f7a7ea..b923e15ca4 100644 --- a/examples/protocols/modbus/serial/mb_master/main/Kconfig.projbuild +++ b/examples/protocols/modbus/serial/mb_master/main/Kconfig.projbuild @@ -2,10 +2,10 @@ menu "Modbus Example Configuration" config MB_UART_PORT_NUM int "UART port number" - range 0 2 if IDF_TARGET_ESP32 - default 2 if IDF_TARGET_ESP32 - range 0 1 if IDF_TARGET_ESP32S2 - default 1 if IDF_TARGET_ESP32S2 + 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 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3 help UART communication port number for Modbus example. @@ -21,7 +21,9 @@ menu "Modbus 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 47 if IDF_TARGET_ESP32S3 + range 0 19 if IDF_TARGET_ESP32C3 + 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. @@ -31,7 +33,9 @@ menu "Modbus 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 47 if IDF_TARGET_ESP32S3 + range 0 19 if IDF_TARGET_ESP32C3 + 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. @@ -39,8 +43,11 @@ menu "Modbus Example Configuration" config MB_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 - default 18 + range 0 47 if IDF_TARGET_ESP32S3 + range 0 19 if IDF_TARGET_ESP32C3 + 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. diff --git a/examples/protocols/modbus/serial/mb_slave/README.md b/examples/protocols/modbus/serial/mb_slave/README.md index 427c0cb6bd..be327faa3a 100644 --- a/examples/protocols/modbus/serial/mb_slave/README.md +++ b/examples/protocols/modbus/serial/mb_slave/README.md @@ -1,21 +1,21 @@ -| Supported Targets | ESP32 | -| ----------------- | ----- | +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | # Modbus Slave Example -This example demonstrates using of FreeModbus stack port implementation for ESP32. The external Modbus host is able to read/write device parameters using Modbus protocol transport. The parameters accessible thorough Modbus are located in `mb_example_common/modbus_params.h\c` files and can be updated by user. -These are represented in structures holding_reg_params, input_reg_params, coil_reg_params, discrete_reg_params for holding registers, input parameters, coils and discrete inputs accordingly. The app_main application demonstrates how to setup Modbus stack and use notifications about parameters change from host system. -The FreeModbus stack located in `components/freemodbus` folder and contains the `/port` folder inside with FreeModbus stack port for ESP32. There are some parameters that can be configured in KConfig file to start stack correctly (See description below for more information). +This example demonstrates using the port of the FreeModbus stack on an ESP32 target where the ESP32 target is operating as a network slave. The example allows an external Modbus host to read/write device parameters on the ESP32 target using the Modbus protocol. The parameters accessible through Modbus are located in `mb_example_common/modbus_params.h\c` source/header files that users can update to add/remove their own custom parameters. +These are represented in structures `holding_reg_params`, `input_reg_params`, `coil_reg_params`, `discrete_reg_params` for holding registers, input parameters, coils and discrete inputs accordingly. The app_main application demonstrates how to setup Modbus stack and use notifications about parameters change from host system. +The FreeModbus stack located in `components/freemodbus` folder and contains the `/port` folder where the stack's port to the ESP32 is situated. There are some parameters of the port that can be configured in KConfig file to start stack correctly (See description below for more information). The slave example uses shared parameter structures defined in `examples/protocols/modbus/mb_example_common` folder. ## Hardware required : Option 1: -PC + USB Serial adapter connected to USB port + RS485 line drivers + ESP32 WROVER-KIT board. +PC + USB Serial adapter connected to USB port + RS485 line drivers + ESP32 based board. The MAX485 line driver is used as an example below but other similar chips can be used as well. Option 2: -The modbus_master example application configured as described in its README.md file and flashed into ESP32 WROVER-KIT board. +The modbus_master example application configured as described in its README.md file and flashed into ESP32 based board. Note: The ```Example Data (Object) Dictionary``` in the modbus_master example can be edited to address parameters from other slaves connected into Modbus segment. RS485 example circuit schematic: @@ -26,13 +26,13 @@ RS485 example circuit schematic: RXD <------| RO | DIFFERENTIAL | RO|-----> RXD | B|---------------|B | TXD ------>| DI MAX485 | \ / | MAX485 DI|<----- TXD -ESP32 WROVER KIT 1 | | RS-485 side | | Modbus master - RTS --+--->| DE | / \ | DE|---+ +ESP32 board | | RS-485 side | | Modbus master + RTS --+--->| DE | / \ | DE|---+ | | A|---------------|A | | +----| /RE | PAIR | /RE|---+-- RTS +-------x--------+ +-------x-------+ | | - --- --- + --- --- ``` ## How to setup and use an example: @@ -43,18 +43,19 @@ Start the command below to show the configuration menu: idf.py menuconfig ``` Select Modbus Example Configuration menu item. -Configure the UART pins used for modbus communication using command and table below. +Configure the UART pins used for modbus communication using the command and table below. ``` -------------------------------------------------------------------------------------------------------------------------- - | ESP32 Interface | #define | Default ESP32 Pin | Default ESP32-S2 Pins | External RS485 Driver Pin | + | 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 | GPIO20 | DI | - | Receive Data (RxD) | CONFIG_MB_UART_RXD | GPIO22 | GPIO19 | RO | - | Request To Send (RTS) | CONFIG_MB_UART_RTS | GPIO18 | GPIO18 | ~RE/DE | + | 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: The GPIO22 - GPIO25 can not be used with ESP32-S2 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. Define the ```Modbus communiction mode``` for slave in Kconfig - CONFIG_MB_COMM_MODE (must be the same for master and slave application). Set ```Modbus slave address``` for the example application (by default for example script is set to 1). @@ -66,8 +67,8 @@ Option 1: Configure the external Modbus master software according to port configuration parameters used in application. As an example the Modbus Poll application can be used with this example. Option 2: -Setup ESP32 WROVER-KIT board and set modbus_master example configuration as described in its README.md file. -Setup one or more slave boards with different slave addresses and connect them into the same Modbus segment (See configuration above). +Setup ESP32 based board and set modbus_master example configuration as described in its README.md file. +Setup one or more slave boards with different slave addresses and connect them into the same Modbus segment (See configuration above). Note: The ```Modbus communiction mode``` parameter must be the same for master and slave example application to be able to communicate with each other. ### Build and flash software diff --git a/examples/protocols/modbus/serial/mb_slave/main/Kconfig.projbuild b/examples/protocols/modbus/serial/mb_slave/main/Kconfig.projbuild index e722f86421..884a1b8b32 100644 --- a/examples/protocols/modbus/serial/mb_slave/main/Kconfig.projbuild +++ b/examples/protocols/modbus/serial/mb_slave/main/Kconfig.projbuild @@ -2,10 +2,10 @@ menu "Modbus Example Configuration" config MB_UART_PORT_NUM int "UART port number" - range 0 2 if IDF_TARGET_ESP32 - default 2 if IDF_TARGET_ESP32 - range 0 1 if IDF_TARGET_ESP32S2 - default 1 if IDF_TARGET_ESP32S2 + 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 1 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3 help UART communication port number for Modbus example. @@ -21,7 +21,9 @@ menu "Modbus 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 47 if IDF_TARGET_ESP32S3 + range 0 19 if IDF_TARGET_ESP32C3 + 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. @@ -31,7 +33,9 @@ menu "Modbus 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 47 if IDF_TARGET_ESP32S3 + range 0 19 if IDF_TARGET_ESP32C3 + 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. @@ -39,8 +43,11 @@ menu "Modbus Example Configuration" config MB_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 - default 18 + range 0 47 if IDF_TARGET_ESP32S3 + range 0 19 if IDF_TARGET_ESP32C3 + 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. diff --git a/examples/protocols/modbus/tcp/mb_tcp_master/README.md b/examples/protocols/modbus/tcp/mb_tcp_master/README.md index 0f66c72dfe..8673580ead 100644 --- a/examples/protocols/modbus/tcp/mb_tcp_master/README.md +++ b/examples/protocols/modbus/tcp/mb_tcp_master/README.md @@ -1,8 +1,11 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + # Modbus TCP Master Example -This example demonstrates using of FreeModbus stack port implementation for ESP32 as a TCP master device. +This example demonstrates using of FreeModbus stack port implementation for ESP32 targets as a TCP master device. This implementation is able to read/write values of slave devices connected into Modbus segment. All parameters to be accessed are defined in data dictionary of the modbus master example source file. -The values represented as characteristics with its name and characteristic CID which are linked into registers of slave devices connected into Modbus segment. +The values represented as characteristics with its name and characteristic CID which are linked into registers of slave devices connected into Modbus segment. The example implements simple control algorithm and checks parameters from slave device and gets alarm (relay in the slave device) when value of parameter exceeded limit. The instances for the modbus parameters are common for master and slave examples and located in `examples/protocols/modbus/mb_example_common` folder. @@ -35,7 +38,7 @@ Modbus multi slave segment connection schematic: ``` MB_DEVICE_ADDR1 ------------- - | | + | | | Slave 1 |---<>--+ | | | ------------- | @@ -55,11 +58,11 @@ Modbus multi slave segment connection schematic: ## Hardware required : Option 1: -PC (Modbus TCP Slave application) + ESP32(-S2) development board with modbus_tcp_slave example. +PC (Modbus TCP Slave application) + ESP32 based development board with modbus_tcp_slave example. Option 2: -Several ESP32(-S2) boards flashed with modbus_tcp_slave example software to represent slave devices. The IP slave addresses for each board have to be configured in `Modbus Example Configuration` menu according to the communication table of example. -One ESP32(-S2) development board should be flashed with modbus_master example and connected to the same network. All the boards require configuration of network settings as described in `examples/common_components/protocol_examples_common`. +Several ESP32 based boards flashed with modbus_tcp_slave example software to represent slave devices. The IP slave addresses for each board have to be configured in `Modbus Example Configuration` menu according to the communication table of example. +One ESP32 based development board should be flashed with modbus_master example and connected to the same network. All the boards require configuration of network settings as described in `examples/common_components/protocol_examples_common`. ## How to setup and use an example: @@ -88,7 +91,7 @@ Option 1: Configure the external Modbus master software according to port configuration parameters used in the example. The Modbus Slave application can be used with this example to emulate slave devices with its parameters. Use official documentation for software to setup emulation of slave devices. Option 2: -Other option is to have the modbus_slave example application flashed into ESP32 WROVER KIT board and connect boards together as showed on the Modbus connection schematic above. See the Modbus slave API documentation to configure communication parameters and slave addresses as defined in "Example parameters definition" table above. +Other option is to have the modbus_slave example application flashed into ESP32 based board and connect boards together as showed on the Modbus connection schematic above. See the Modbus slave API documentation to configure communication parameters and slave addresses as defined in "Example parameters definition" table above. ### Build and flash software of master device Build the project and flash it to the board, then run monitor tool to view serial output: diff --git a/examples/protocols/modbus/tcp/mb_tcp_slave/README.md b/examples/protocols/modbus/tcp/mb_tcp_slave/README.md index 641c999c39..a69b916e7c 100644 --- a/examples/protocols/modbus/tcp/mb_tcp_slave/README.md +++ b/examples/protocols/modbus/tcp/mb_tcp_slave/README.md @@ -1,17 +1,20 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + # Modbus Slave Example -This example demonstrates using of FreeModbus TCP slave stack port implementation for ESP32(-S2). The external Modbus host is able to read/write device parameters using Modbus protocol transport. The parameters accessible thorough Modbus are located in `mb_example_common/modbus_params.h\c` files and can be updated by user. -These are represented in structures holding_reg_params, input_reg_params, coil_reg_params, discrete_reg_params for holding registers, input parameters, coils and discrete inputs accordingly. The app_main application demonstrates how to setup Modbus stack and use notifications about parameters change from host system. -The FreeModbus stack located in `components/freemodbus` folder and contain `/port` folder inside which contains FreeModbus stack port for ESP32. There are some parameters that can be configured in KConfig file to start stack correctly (See description below for more information). +This example demonstrates using of FreeModbus TCP slave stack port implementation for supported ESP32 target chips. The external Modbus host is able to read/write device parameters using Modbus protocol transport. The parameters accessible thorough Modbus are located in `mb_example_common/modbus_params.h\c` files and can be updated by user. +These are represented in structures holding_reg_params, input_reg_params, coil_reg_params, discrete_reg_params for holding registers, input parameters, coils and discrete inputs accordingly. The app_main application demonstrates how to setup Modbus stack and use notifications about parameters change from host system. +The FreeModbus stack located in `components/freemodbus` folder and contain `/port` folder inside which contains FreeModbus stack port for ESP32 target chips. There are some parameters that can be configured in KConfig file to start stack correctly (See description below for more information). The slave example uses shared parameter structures defined in ```examples/protocols/modbus/mb_example_common``` folder. ## Hardware required : Option 1: -The ESP32(-S2) development board flashed with modbus_tcp_slave example + external Modbus master host software. +The ESP32 based development board flashed with modbus_tcp_slave example + external Modbus master host software. Option 2: -The modbus_tcp_master example application configured as described in its README.md file and flashed into ESP32(-S2) board. +The modbus_tcp_master example application configured as described in its README.md file and flashed into ESP32 based board. Note: The ```Example Data (Object) Dictionary``` in the modbus_tcp_master example can be edited to address parameters from other slaves connected into Modbus segment. ## How to setup and use an example: @@ -33,8 +36,8 @@ Option 1: Configure the external Modbus master software according to port configuration parameters used in application. As an example the Modbus Poll application can be used with this example. Option 2: -Setup ESP32(-S2) development board and set modbus_tcp_master example configuration as described in its README.md file. -Setup one or more slave boards and connect them into the same Modbus segment (See configuration above). +Setup ESP32 based development board and set modbus_tcp_master example configuration as described in its README.md file. +Setup one or more slave boards and connect them into the same Modbus segment (See configuration above). ### Build and flash software Build the project and flash it to the board, then run monitor tool to view serial output: