From 2a4a72101e6cfd6cbd7d0d8835b56d6573595672 Mon Sep 17 00:00:00 2001 From: Alex Lisitsyn Date: Fri, 27 Mar 2020 16:20:21 +0800 Subject: [PATCH] driver: fix driver set rx timeout feature of uart tout_thr - move calculation and masking into hal layer update driver and uart_ll (add uart_ll_set_rx_tout) move tout calculation into uart_ll move calculation of time out in bit time for esp32s2 into low level uart_ll.h file move uart_hal_get_symb_len() into hal update set_rx_timeout() to warn user about incorrect value update HAL, LL 1 fix uart_xx_set_rx_tout() to convert symbol time into bit time update param description update tout calculation in LL update uart_hal_get_max_rx_timeout_thrd() and uart_ll_get_max_rx_timeout_thrd() * Original commit: espressif/esp-idf@16e6e636945b093d3cb51d5795a8d0c6ad2329ab --- .../modbus/serial/mb_master/CMakeLists.txt | 2 -- .../modbus/serial/mb_master/README.md | 24 ++++++++++--------- .../serial/mb_master/main/Kconfig.projbuild | 23 ++++++++++++------ .../modbus/serial/mb_master/main/master.c | 3 +++ .../modbus/serial/mb_slave/CMakeLists.txt | 1 - .../modbus/serial/mb_slave/README.md | 18 +++++++------- .../serial/mb_slave/main/Kconfig.projbuild | 24 +++++++++++++------ .../modbus/serial/mb_slave/main/slave.c | 3 +++ 8 files changed, 62 insertions(+), 36 deletions(-) diff --git a/examples/protocols/modbus/serial/mb_master/CMakeLists.txt b/examples/protocols/modbus/serial/mb_master/CMakeLists.txt index f97cc2e..a6b6887 100644 --- a/examples/protocols/modbus/serial/mb_master/CMakeLists.txt +++ b/examples/protocols/modbus/serial/mb_master/CMakeLists.txt @@ -2,8 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -set(SUPPORTED_TARGETS esp32) - set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/protocols/modbus/serial/mb_example_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/examples/protocols/modbus/serial/mb_master/README.md b/examples/protocols/modbus/serial/mb_master/README.md index 5968f41..82b4977 100644 --- a/examples/protocols/modbus/serial/mb_master/README.md +++ b/examples/protocols/modbus/serial/mb_master/README.md @@ -91,18 +91,20 @@ idf.py menuconfig Configure the UART pins used for modbus communication using and table below. Define the communication mode parameter for master and slave in Kconfig - CONFIG_MB_COMM_MODE (must be the same for master and slave devices in one segment). 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 | + | ----------------------|--------------------|-----------------------|-----------------------|---------------------------| + | 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 | + | 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. + +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). -``` - ------------------------------------------------------------------------------------------------ - | ESP32 Interface | #define | Default ESP32 Pin | External RS485 Pin| - | ----------------------|------------------------------|-------------------|-------------------| - | Transmit Data (TxD) | CONFIG_MB_UART_TXD | GPIO23 | DI | - | Receive Data (RxD) | CONFIG_MB_UART_RXD | GPIO22 | RO | - | Request To Send (RTS) | CONFIG_MB_UART_RTS | GPIO18 | ~RE/DE | - | | | | | - | Ground | n/a | GND | GND | - ------------------------------------------------------------------------------------------------ -``` 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. diff --git a/examples/protocols/modbus/serial/mb_master/main/Kconfig.projbuild b/examples/protocols/modbus/serial/mb_master/main/Kconfig.projbuild index 6adb91d..b5b2f7a 100644 --- a/examples/protocols/modbus/serial/mb_master/main/Kconfig.projbuild +++ b/examples/protocols/modbus/serial/mb_master/main/Kconfig.projbuild @@ -2,8 +2,10 @@ menu "Modbus Example Configuration" config MB_UART_PORT_NUM int "UART port number" - range 0 2 - default 2 + 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 help UART communication port number for Modbus example. @@ -16,27 +18,34 @@ menu "Modbus Example Configuration" config MB_UART_RXD int "UART RXD pin number" - range 0 34 - default 22 + 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 help GPIO number for UART RX pin. See UART documentation for more information about available pin numbers for UART. config MB_UART_TXD int "UART TXD pin number" - range 0 34 - default 23 + 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 help GPIO number for UART TX pin. See UART documentation for more information about available pin numbers for UART. config MB_UART_RTS int "UART RTS pin number" - range 0 34 + range 0 34 if IDF_TARGET_ESP32 + range 0 46 if IDF_TARGET_ESP32S2 default 18 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. choice MB_COMM_MODE prompt "Modbus communication mode" diff --git a/examples/protocols/modbus/serial/mb_master/main/master.c b/examples/protocols/modbus/serial/mb_master/main/master.c index da9c58c..82e0a4c 100644 --- a/examples/protocols/modbus/serial/mb_master/main/master.c +++ b/examples/protocols/modbus/serial/mb_master/main/master.c @@ -21,6 +21,9 @@ #define MB_PORT_NUM (CONFIG_MB_UART_PORT_NUM) // Number of UART port used for Modbus connection #define MB_DEV_SPEED (CONFIG_MB_UART_BAUD_RATE) // The communication speed of the UART +// Note: Some pins on target chip cannot be assigned for UART communication. +// See UART documentation for selected board and target to configure pins using Kconfig. + // The number of parameters that intended to be used in the particular control process #define MASTER_MAX_CIDS num_device_parameters diff --git a/examples/protocols/modbus/serial/mb_slave/CMakeLists.txt b/examples/protocols/modbus/serial/mb_slave/CMakeLists.txt index 13fa630..a941d4e 100644 --- a/examples/protocols/modbus/serial/mb_slave/CMakeLists.txt +++ b/examples/protocols/modbus/serial/mb_slave/CMakeLists.txt @@ -1,7 +1,6 @@ # 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.5) -set(SUPPORTED_TARGETS esp32) set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/protocols/modbus/serial/mb_example_common) diff --git a/examples/protocols/modbus/serial/mb_slave/README.md b/examples/protocols/modbus/serial/mb_slave/README.md index 8d09f1a..f93d684 100644 --- a/examples/protocols/modbus/serial/mb_slave/README.md +++ b/examples/protocols/modbus/serial/mb_slave/README.md @@ -42,15 +42,17 @@ idf.py menuconfig Select Modbus Example Configuration menu item. Configure the UART pins used for modbus communication using command and table below. ``` - ----------------------------------------------------------------------------------- - | ESP32 Interface | #define | Default ESP32 Pin | External RS485 | - | ----------------------|--------------------|-------------------| Driver Pin | - | Transmit Data (TxD) | CONFIG_MB_UART_TXD | GPIO23 | DI | - | Receive Data (RxD) | CONFIG_MB_UART_RXD | GPIO22 | RO | - | Request To Send (RTS) | CONFIG_MB_UART_RTS | GPIO18 | ~RE/DE | - | Ground | n/a | GND | GND | - ----------------------------------------------------------------------------------- + -------------------------------------------------------------------------------------------------------------------------- + | ESP32 Interface | #define | Default ESP32 Pin | Default ESP32-S2 Pins | External RS485 Driver Pin | + | ----------------------|--------------------|-----------------------|-----------------------|---------------------------| + | 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 | + | 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. + 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). The communication parameters of freemodbus stack (Component config->Modbus configuration) allow to configure it appropriately but usually it is enough to use default settings. diff --git a/examples/protocols/modbus/serial/mb_slave/main/Kconfig.projbuild b/examples/protocols/modbus/serial/mb_slave/main/Kconfig.projbuild index 12f0e01..e722f86 100644 --- a/examples/protocols/modbus/serial/mb_slave/main/Kconfig.projbuild +++ b/examples/protocols/modbus/serial/mb_slave/main/Kconfig.projbuild @@ -2,8 +2,10 @@ menu "Modbus Example Configuration" config MB_UART_PORT_NUM int "UART port number" - range 0 2 - default 2 + 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 help UART communication port number for Modbus example. @@ -16,27 +18,34 @@ menu "Modbus Example Configuration" config MB_UART_RXD int "UART RXD pin number" - range 0 34 - default 22 + 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 help GPIO number for UART RX pin. See UART documentation for more information about available pin numbers for UART. config MB_UART_TXD int "UART TXD pin number" - range 0 34 - default 23 + 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 help GPIO number for UART TX pin. See UART documentation for more information about available pin numbers for UART. config MB_UART_RTS int "UART RTS pin number" - range 0 34 + range 0 34 if IDF_TARGET_ESP32 + range 0 46 if IDF_TARGET_ESP32S2 default 18 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. choice MB_COMM_MODE prompt "Modbus communication mode" @@ -62,4 +71,5 @@ menu "Modbus Example Configuration" This is the Modbus slave address in the network. It is used to organize Modbus network with several slaves connected into the same segment. + endmenu diff --git a/examples/protocols/modbus/serial/mb_slave/main/slave.c b/examples/protocols/modbus/serial/mb_slave/main/slave.c index 98808cc..313d45a 100644 --- a/examples/protocols/modbus/serial/mb_slave/main/slave.c +++ b/examples/protocols/modbus/serial/mb_slave/main/slave.c @@ -16,6 +16,9 @@ #define MB_SLAVE_ADDR (CONFIG_MB_SLAVE_ADDR) // The address of device in Modbus network #define MB_DEV_SPEED (CONFIG_MB_UART_BAUD_RATE) // The communication speed of the UART +// 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. + // Defines below are used to define register start address for each type of Modbus registers #define MB_REG_DISCRETE_INPUT_START (0x0000) #define MB_REG_INPUT_START (0x0000)