Merge branch 'feature/esp32s2beta_merge_fix_modbus_examples' into 'master'

freemodbus: feature esp32s2beta fix modbus examples

Closes IDF-1028

See merge request espressif/esp-idf!6365
This commit is contained in:
Michael (XIAO Xufeng)
2020-03-27 16:20:22 +08:00
16 changed files with 304 additions and 141 deletions
+13 -9
View File
@@ -52,13 +52,13 @@ static const char* UART_TAG = "uart";
return (ret_val); \
}
#define UART_EMPTY_THRESH_DEFAULT (10)
#define UART_FULL_THRESH_DEFAULT (120)
#define UART_TOUT_THRESH_DEFAULT (10)
#define UART_CLKDIV_FRAG_BIT_WIDTH (3)
#define UART_TX_IDLE_NUM_DEFAULT (0)
#define UART_PATTERN_DET_QLEN_DEFAULT (10)
#define UART_MIN_WAKEUP_THRESH (SOC_UART_MIN_WAKEUP_THRESH)
#define UART_EMPTY_THRESH_DEFAULT (10)
#define UART_FULL_THRESH_DEFAULT (120)
#define UART_TOUT_THRESH_DEFAULT (10)
#define UART_CLKDIV_FRAG_BIT_WIDTH (3)
#define UART_TX_IDLE_NUM_DEFAULT (0)
#define UART_PATTERN_DET_QLEN_DEFAULT (10)
#define UART_MIN_WAKEUP_THRESH (SOC_UART_MIN_WAKEUP_THRESH)
#define UART_INTR_CONFIG_FLAG ((UART_INTR_RXFIFO_FULL) \
| (UART_INTR_RXFIFO_TOUT) \
@@ -1492,8 +1492,12 @@ esp_err_t uart_set_tx_empty_threshold(uart_port_t uart_num, int threshold)
esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh)
{
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_ERR_INVALID_ARG);
UART_CHECK((tout_thresh < 127), "tout_thresh max value is 126", ESP_ERR_INVALID_ARG);
// get maximum timeout threshold
uint16_t tout_max_thresh = uart_hal_get_max_rx_timeout_thrd(&(uart_context[uart_num].hal));
if (tout_thresh > tout_max_thresh) {
ESP_LOGE(UART_TAG, "tout_thresh = %d > maximum value = %d", tout_thresh, tout_max_thresh);
return ESP_ERR_INVALID_ARG;
}
UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
uart_hal_set_rx_timeout(&(uart_context[uart_num].hal), tout_thresh);
UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
+19 -1
View File
@@ -430,6 +430,24 @@ void uart_hal_get_sclk(uart_hal_context_t *hal, uart_sclk_t *sclk);
*/
void uart_hal_set_loop_back(uart_hal_context_t *hal, bool loop_back_en);
/**
* @brief Calculate uart symbol bit length, as defined in configuration.
*
* @param hw Beginning address of the peripheral registers.
*
* @return number of bits per UART symbol.
*/
uint8_t uart_hal_get_symb_len(uart_hal_context_t *hal);
/**
* @brief Get UART maximum timeout threshold.
*
* @param hw Beginning address of the peripheral registers.
*
* @return maximum timeout threshold value for target.
*/
uint16_t uart_hal_get_max_rx_timeout_thrd(uart_hal_context_t *hal);
#ifdef __cplusplus
}
#endif
#endif
+45 -26
View File
@@ -379,31 +379,6 @@ static inline void uart_ll_set_tx_idle_num(uart_dev_t *hw, uint32_t idle_num)
hw->idle_conf.tx_idle_num = idle_num;
}
/**
* @brief Configure the timeout value for receiver receiving a byte, and enable rx timeout function.
*
* @param hw Beginning address of the peripheral registers.
* @param tout_thr The timeout value. The rx timeout function will be disabled if `tout_thr == 0`.
*
* @return None.
*/
static inline void uart_ll_set_rx_tout(uart_dev_t *hw, uint8_t tout_thr)
{
// The tout_thresh = 1, defines TOUT interrupt timeout equal to
// transmission time of one symbol (~11 bit) on current baudrate
if (hw->conf0.tick_ref_always_on == 0) {
//Hardware issue workaround: when using ref_tick, the rx timeout threshold needs increase to 10 times.
//T_ref = T_apb * APB_CLK/(REF_TICK << CLKDIV_FRAG_BIT_WIDTH)
tout_thr = tout_thr * UART_LL_TOUT_REF_FACTOR_DEFAULT;
}
if(tout_thr > 0) {
hw->conf1.rx_tout_thrhd = tout_thr;
hw->conf1.rx_tout_en = 1;
} else {
hw->conf1.rx_tout_en = 0;
}
}
/**
* @brief Configure the transmiter to send break chars.
*
@@ -806,8 +781,52 @@ static inline void uart_ll_inverse_signal(uart_dev_t *hw, uint32_t inv_mask)
hw->conf0.val = conf0_reg.val;
}
/**
* @brief Configure the timeout value for receiver receiving a byte, and enable rx timeout function.
*
* @param hw Beginning address of the peripheral registers.
* @param tout_thr The timeout value as a bit time. The rx timeout function will be disabled if `tout_thr == 0`.
*
* @return None.
*/
static inline void uart_ll_set_rx_tout(uart_dev_t *hw, uint16_t tout_thr)
{
if (hw->conf0.tick_ref_always_on == 0) {
//Hardware issue workaround: when using ref_tick, the rx timeout threshold needs increase to 10 times.
//T_ref = T_apb * APB_CLK/(REF_TICK << CLKDIV_FRAG_BIT_WIDTH)
tout_thr = tout_thr * UART_LL_TOUT_REF_FACTOR_DEFAULT;
} else {
//If APB_CLK is used: counting rate is BAUD tick rate / 8
tout_thr = (tout_thr + 7) / 8;
}
if (tout_thr > 0) {
hw->conf1.rx_tout_thrhd = tout_thr;
hw->conf1.rx_tout_en = 1;
} else {
hw->conf1.rx_tout_en = 0;
}
}
/**
* @brief Get UART maximum timeout threshold.
*
* @param hw Beginning address of the peripheral registers.
*
* @return maximum timeout threshold.
*/
static inline uint16_t uart_ll_max_tout_thrd(uart_dev_t *hw)
{
uint16_t tout_sym = 0;
if (hw->conf0.tick_ref_always_on == 0) {
tout_sym = (uint16_t)(UART_RX_TOUT_THRHD_V / UART_LL_TOUT_REF_FACTOR_DEFAULT);
} else {
tout_sym = (uint16_t)(UART_RX_TOUT_THRHD_V << 3);
}
return tout_sym;
}
#undef UART_LL_TOUT_REF_FACTOR_DEFAULT
#ifdef __cplusplus
}
#endif
#endif
@@ -29,7 +29,6 @@ extern "C" {
// Get UART hardware instance with giving uart num
#define UART_LL_GET_HW(num) (((num) == 0) ? (&UART0) : (&UART1))
// Define UART interrupts
typedef enum {
UART_INTR_RXFIFO_FULL = (0x1<<0),
@@ -353,24 +352,6 @@ static inline void uart_ll_set_tx_idle_num(uart_dev_t *hw, uint32_t idle_num)
hw->idle_conf.tx_idle_num = idle_num;
}
/**
* @brief Configure the timeout value for receiver receiving a byte, and enable rx timeout function.
*
* @param hw Beginning address of the peripheral registers.
* @param tout_thr The timeout value. The rx timeout function will be disabled if `tout_thr == 0`.
*
* @return None.
*/
static inline void uart_ll_set_rx_tout(uart_dev_t *hw, uint8_t tout_thr)
{
if(tout_thr > 0) {
hw->mem_conf.rx_tout_thrhd = tout_thr;
hw->conf1.rx_tout_en = 1;
} else {
hw->conf1.rx_tout_en = 0;
}
}
/**
* @brief Configure the transmiter to send break chars.
*
@@ -772,6 +753,37 @@ static inline void uart_ll_inverse_signal(uart_dev_t *hw, uint32_t inv_mask)
hw->conf0.val = conf0_reg.val;
}
/**
* @brief Configure the timeout value for receiver receiving a byte, and enable rx timeout function.
*
* @param hw Beginning address of the peripheral registers.
* @param tout_thrd The timeout value as UART bit time. The rx timeout function will be disabled if `tout_thrd == 0`.
*
* @return None.
*/
static inline void uart_ll_set_rx_tout(uart_dev_t *hw, uint16_t tout_thrd)
{
uint16_t tout_val = tout_thrd;
if(tout_thrd > 0) {
hw->mem_conf.rx_tout_thrhd = tout_val;
hw->conf1.rx_tout_en = 1;
} else {
hw->conf1.rx_tout_en = 0;
}
}
/**
* @brief Get UART maximum timeout threshold.
*
* @param hw Beginning address of the peripheral registers.
*
* @return maximum timeout threshold.
*/
static inline uint16_t uart_ll_max_tout_thrd(uart_dev_t *hw)
{
return UART_RX_TOUT_THRHD_V;
}
#ifdef __cplusplus
}
#endif
#endif
+29 -6
View File
@@ -76,11 +76,6 @@ void uart_hal_set_at_cmd_char(uart_hal_context_t *hal, uart_at_cmd_t *at_cmd)
uart_ll_set_at_cmd_char(hal->dev, at_cmd);
}
void uart_hal_set_rx_timeout(uart_hal_context_t *hal, const uint8_t tout)
{
uart_ll_set_rx_tout(hal->dev, tout);
}
void uart_hal_get_sclk(uart_hal_context_t *hal, uart_sclk_t *sclk)
{
uart_ll_get_sclk(hal->dev, sclk);
@@ -153,4 +148,32 @@ void uart_hal_init(uart_hal_context_t *hal, int uart_num)
uart_ll_set_tx_idle_num(hal->dev, 0);
// Disable hw-flow control
uart_ll_set_hw_flow_ctrl(hal->dev, UART_HW_FLOWCTRL_DISABLE, 100);
}
}
uint8_t uart_hal_get_symb_len(uart_hal_context_t *hal)
{
uint8_t symbol_len = 1; // number of bits per symbol including start
uart_parity_t parity_mode;
uart_stop_bits_t stop_bit;
uart_word_length_t data_bit;
uart_ll_get_data_bit_num(hal->dev, &data_bit);
uart_ll_get_stop_bits(hal->dev, &stop_bit);
uart_ll_get_parity(hal->dev, &parity_mode);
symbol_len += (data_bit < UART_DATA_BITS_MAX) ? (uint8_t)data_bit + 5 : 8;
symbol_len += (stop_bit > UART_STOP_BITS_1) ? 2 : 1;
symbol_len += (parity_mode > UART_PARITY_DISABLE) ? 1 : 0;
return symbol_len;
}
void uart_hal_set_rx_timeout(uart_hal_context_t *hal, const uint8_t tout)
{
uint8_t symb_len = uart_hal_get_symb_len(hal);
uart_ll_set_rx_tout(hal->dev, symb_len * tout);
}
uint16_t uart_hal_get_max_rx_timeout_thrd(uart_hal_context_t *hal)
{
uint8_t symb_len = uart_hal_get_symb_len(hal);
uint16_t max_tout_thresh = uart_ll_max_tout_thrd(hal->dev);
return (max_tout_thresh / symb_len);
}
@@ -30,18 +30,18 @@ ESP32-WROVER-KIT | | RS-485 side | | SERIAL AD
```
#### Connect an external RS485 serial interface to an ESP32 board
```
----------------------------------------------------------------------
| ESP32 Interface | #define | ESP32 Pin | External RS485 |
| | | | Driver Pin |
| ----------------------|---------------|-----------|----------------|
| Transmit Data (TxD) | ECHO_TEST_TXD | GPIO23 | DI |
| Receive Data (RxD) | ECHO_TEST_RXD | GPIO22 | RO |
| Request To Send (RTS) | ECHO_TEST_RTS | GPIO18 | ~RE/DE |
| Ground | n/a | GND | GND |
----------------------------------------------------------------------
```
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 | 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.
### Configure the project
```
@@ -0,0 +1,51 @@
menu "Echo RS485 Example Configuration"
config ECHO_UART_PORT_NUM
int "UART port number"
range 0 2 if IDF_TARGET_ESP32
range 0 1 if IDF_TARGET_ESP32S2
default 2 if IDF_TARGET_ESP32
default 1 if IDF_TARGET_ESP32S2
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 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 ECHO_UART_TXD
int "UART TXD pin number"
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 ECHO_UART_RTS
int "UART RTS pin number"
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.
endmenu
@@ -16,40 +16,44 @@
#include "driver/uart.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "sdkconfig.h"
/**
* This is a example example which echos any data it receives on UART back to the sender.
*
* - port: UART2
* - rx buffer: on
* - tx buffer: off
* - flow control: off
*
* This example has been tested on a 3 node RS485 Serial Bus
*
*/
* 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: UART2 default pins IO16, IO17 do not work on ESP32-WROVER module
// because these pins connected to PSRAM
#define ECHO_TEST_TXD (23)
#define ECHO_TEST_RXD (22)
// 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 (18)
#define ECHO_TEST_RTS (CONFIG_ECHO_UART_RTS)
// CTS is not used in RS485 Half-Duplex Mode
#define ECHO_TEST_CTS UART_PIN_NO_CHANGE
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
#define BUF_SIZE (127)
#define BAUD_RATE (115200)
#define BAUD_RATE (CONFIG_ECHO_UART_BAUD_RATE)
// Read packet timeout
#define PACKET_READ_TICS (100 / portTICK_RATE_MS)
#define ECHO_TASK_STACK_SIZE (2048)
#define ECHO_TASK_PRIO (10)
#define ECHO_UART_PORT (UART_NUM_MAX - 1)
#define ECHO_UART_PORT (CONFIG_ECHO_UART_PORT_NUM)
static const char *TAG = "RS485_ECHO_APP";
// 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)
@@ -72,22 +76,27 @@ static void echo_task(void *arg)
// Install UART driver (we don't need an event queue here)
// In this example we don't even use a buffer for sending data.
uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
ESP_ERROR_CHECK(uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0));
// Configure UART parameters
uart_param_config(uart_num, &uart_config);
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
ESP_LOGI(TAG, "UART set pins, mode and install driver.");
// Set UART1 pins(TX: IO23, RX: I022, RTS: IO18, CTS: IO19)
uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
// 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
uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX);
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);
ESP_LOGI(TAG, "UART start recieve loop.\r\n");
uart_write_bytes(uart_num, "Start RS485 UART test.\r\n", 24);
echo_send(uart_num, "Start RS485 UART test.\r\n", 24);
while(1) {
//Read data from UART
@@ -95,27 +104,28 @@ static void echo_task(void *arg)
//Write data back to UART
if (len > 0) {
uart_write_bytes(uart_num, "\r\n", 2);
echo_send(uart_num, "\r\n", 2);
char prefix[] = "RS485 Received: [";
uart_write_bytes(uart_num, prefix, (sizeof(prefix) - 1));
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]);
uart_write_bytes(uart_num, (const char*)&data[i], 1);
echo_send(uart_num, (const char*)&data[i], 1);
// Add a Newline character if you get a return charater from paste (Paste tests multibyte receipt/buffer)
if (data[i] == '\r') {
uart_write_bytes(uart_num, "\n", 1);
echo_send(uart_num, "\n", 1);
}
}
printf("] \n");
uart_write_bytes(uart_num, "]\r\n", 3);
echo_send(uart_num, "]\r\n", 3);
} else {
// Echo a "." to show we are alive while we wait for input
uart_write_bytes(uart_num, ".", 1);
echo_send(uart_num, ".", 1);
ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 10));
}
}
vTaskDelete(NULL);
}
void app_main(void)
@@ -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)
@@ -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.
@@ -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"
@@ -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
@@ -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)
@@ -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.
@@ -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
@@ -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)