2018-06-28 11:45:41 -04:00
|
|
|
/* FreeModbus Slave Example ESP32
|
|
|
|
|
|
|
|
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>
|
2019-11-26 00:16:25 -05:00
|
|
|
#include <stdint.h>
|
2018-06-28 11:45:41 -04:00
|
|
|
#include "esp_err.h"
|
|
|
|
#include "mbcontroller.h" // for mbcontroller defines and api
|
2019-11-26 00:16:25 -05:00
|
|
|
#include "modbus_params.h" // for modbus parameters structures
|
2018-06-28 11:45:41 -04:00
|
|
|
#include "esp_log.h" // for log_write
|
2019-11-26 00:16:25 -05:00
|
|
|
#include "sdkconfig.h"
|
2018-06-28 11:45:41 -04:00
|
|
|
|
2019-11-26 00:16:25 -05:00
|
|
|
#define MB_PORT_NUM (CONFIG_MB_UART_PORT_NUM) // Number of UART port used for Modbus connection
|
|
|
|
#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
|
2018-06-28 11:45:41 -04:00
|
|
|
|
2020-03-27 04:20:21 -04:00
|
|
|
// 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.
|
|
|
|
|
2018-06-28 11:45:41 -04:00
|
|
|
// 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)
|
|
|
|
#define MB_REG_HOLDING_START (0x0000)
|
|
|
|
#define MB_REG_COILS_START (0x0000)
|
|
|
|
|
|
|
|
#define MB_PAR_INFO_GET_TOUT (10) // Timeout for get parameter info
|
2019-11-26 00:16:25 -05:00
|
|
|
#define MB_CHAN_DATA_MAX_VAL (6)
|
|
|
|
#define MB_CHAN_DATA_OFFSET (0.2f)
|
2019-04-10 11:45:22 -04:00
|
|
|
#define MB_READ_MASK (MB_EVENT_INPUT_REG_RD \
|
|
|
|
| MB_EVENT_HOLDING_REG_RD \
|
|
|
|
| MB_EVENT_DISCRETE_RD \
|
|
|
|
| MB_EVENT_COILS_RD)
|
|
|
|
#define MB_WRITE_MASK (MB_EVENT_HOLDING_REG_WR \
|
|
|
|
| MB_EVENT_COILS_WR)
|
|
|
|
#define MB_READ_WRITE_MASK (MB_READ_MASK | MB_WRITE_MASK)
|
2018-06-28 11:45:41 -04:00
|
|
|
|
2019-11-26 00:16:25 -05:00
|
|
|
static const char *SLAVE_TAG = "SLAVE_TEST";
|
|
|
|
|
|
|
|
static portMUX_TYPE param_lock = portMUX_INITIALIZER_UNLOCKED;
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
// Set register values into known state
|
2019-07-16 05:33:30 -04:00
|
|
|
static void setup_reg_data(void)
|
2018-06-28 11:45:41 -04:00
|
|
|
{
|
|
|
|
// Define initial state of parameters
|
|
|
|
discrete_reg_params.discrete_input1 = 1;
|
|
|
|
discrete_reg_params.discrete_input3 = 1;
|
|
|
|
discrete_reg_params.discrete_input5 = 1;
|
|
|
|
discrete_reg_params.discrete_input7 = 1;
|
|
|
|
|
2019-11-26 00:16:25 -05:00
|
|
|
holding_reg_params.holding_data0 = 1.34;
|
|
|
|
holding_reg_params.holding_data1 = 2.56;
|
|
|
|
holding_reg_params.holding_data2 = 3.78;
|
|
|
|
holding_reg_params.holding_data3 = 4.90;
|
|
|
|
|
|
|
|
coil_reg_params.coils_port0 = 0x55;
|
|
|
|
coil_reg_params.coils_port1 = 0xAA;
|
|
|
|
|
|
|
|
input_reg_params.input_data0 = 1.12;
|
|
|
|
input_reg_params.input_data1 = 2.34;
|
|
|
|
input_reg_params.input_data2 = 3.56;
|
|
|
|
input_reg_params.input_data3 = 4.78;
|
2018-06-28 11:45:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// An example application of Modbus slave. It is based on freemodbus stack.
|
|
|
|
// See deviceparams.h file for more information about assigned Modbus parameters.
|
|
|
|
// These parameters can be accessed from main application and also can be changed
|
|
|
|
// by external Modbus master host.
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-06-28 11:45:41 -04:00
|
|
|
{
|
|
|
|
mb_param_info_t reg_info; // keeps the Modbus registers access information
|
|
|
|
mb_communication_info_t comm_info; // Modbus communication parameters
|
|
|
|
mb_register_area_descriptor_t reg_area; // Modbus register area descriptor structure
|
|
|
|
|
|
|
|
// Set UART log level
|
2019-11-26 00:16:25 -05:00
|
|
|
esp_log_level_set(SLAVE_TAG, ESP_LOG_INFO);
|
2018-10-19 09:51:27 -04:00
|
|
|
void* mbc_slave_handler = NULL;
|
2018-06-28 11:45:41 -04:00
|
|
|
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_init(MB_PORT_SERIAL_SLAVE, &mbc_slave_handler)); // Initialization of Modbus controller
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
// Setup communication parameters and start stack
|
2019-11-26 00:16:25 -05:00
|
|
|
#if CONFIG_MB_COMM_MODE_ASCII
|
|
|
|
comm_info.mode = MB_MODE_ASCII,
|
|
|
|
#elif CONFIG_MB_COMM_MODE_RTU
|
|
|
|
comm_info.mode = MB_MODE_RTU,
|
|
|
|
#endif
|
|
|
|
comm_info.slave_addr = MB_SLAVE_ADDR;
|
2018-06-28 11:45:41 -04:00
|
|
|
comm_info.port = MB_PORT_NUM;
|
|
|
|
comm_info.baudrate = MB_DEV_SPEED;
|
|
|
|
comm_info.parity = MB_PARITY_NONE;
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_setup((void*)&comm_info));
|
2019-11-26 00:16:25 -05:00
|
|
|
|
2018-06-28 11:45:41 -04:00
|
|
|
// The code below initializes Modbus register area descriptors
|
|
|
|
// for Modbus Holding Registers, Input Registers, Coils and Discrete Inputs
|
|
|
|
// Initialization should be done for each supported Modbus register area according to register map.
|
|
|
|
// When external master trying to access the register in the area that is not initialized
|
2018-10-19 09:51:27 -04:00
|
|
|
// by mbc_slave_set_descriptor() API call then Modbus stack
|
2018-06-28 11:45:41 -04:00
|
|
|
// will send exception response for this register area.
|
|
|
|
reg_area.type = MB_PARAM_HOLDING; // Set type of register area
|
|
|
|
reg_area.start_offset = MB_REG_HOLDING_START; // Offset of register area in Modbus protocol
|
|
|
|
reg_area.address = (void*)&holding_reg_params; // Set pointer to storage instance
|
|
|
|
reg_area.size = sizeof(holding_reg_params); // Set the size of register storage instance
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_set_descriptor(reg_area));
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
// Initialization of Input Registers area
|
|
|
|
reg_area.type = MB_PARAM_INPUT;
|
|
|
|
reg_area.start_offset = MB_REG_INPUT_START;
|
|
|
|
reg_area.address = (void*)&input_reg_params;
|
|
|
|
reg_area.size = sizeof(input_reg_params);
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_set_descriptor(reg_area));
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
// Initialization of Coils register area
|
|
|
|
reg_area.type = MB_PARAM_COIL;
|
|
|
|
reg_area.start_offset = MB_REG_COILS_START;
|
|
|
|
reg_area.address = (void*)&coil_reg_params;
|
|
|
|
reg_area.size = sizeof(coil_reg_params);
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_set_descriptor(reg_area));
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
// Initialization of Discrete Inputs register area
|
|
|
|
reg_area.type = MB_PARAM_DISCRETE;
|
|
|
|
reg_area.start_offset = MB_REG_DISCRETE_INPUT_START;
|
|
|
|
reg_area.address = (void*)&discrete_reg_params;
|
|
|
|
reg_area.size = sizeof(discrete_reg_params);
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_set_descriptor(reg_area));
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
setup_reg_data(); // Set values into known state
|
|
|
|
|
|
|
|
// Starts of modbus controller and stack
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_start());
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2019-11-26 00:16:25 -05:00
|
|
|
// Set UART pin numbers
|
|
|
|
ESP_ERROR_CHECK(uart_set_pin(MB_PORT_NUM, CONFIG_MB_UART_TXD,
|
|
|
|
CONFIG_MB_UART_RXD, CONFIG_MB_UART_RTS,
|
|
|
|
UART_PIN_NO_CHANGE));
|
|
|
|
|
2018-12-03 08:16:36 -05:00
|
|
|
// Set UART driver mode to Half Duplex
|
2020-03-30 10:05:48 -04:00
|
|
|
ESP_ERROR_CHECK(uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2019-11-26 00:16:25 -05:00
|
|
|
ESP_LOGI(SLAVE_TAG, "Modbus slave stack initialized.");
|
|
|
|
ESP_LOGI(SLAVE_TAG, "Start modbus test...");
|
2018-06-28 11:45:41 -04:00
|
|
|
|
|
|
|
// The cycle below will be terminated when parameter holdingRegParams.dataChan0
|
|
|
|
// incremented each access cycle reaches the CHAN_DATA_MAX_VAL value.
|
2019-11-26 00:16:25 -05:00
|
|
|
for(;holding_reg_params.holding_data0 < MB_CHAN_DATA_MAX_VAL;) {
|
2018-06-28 11:45:41 -04:00
|
|
|
// Check for read/write events of Modbus master for certain events
|
2018-10-19 09:51:27 -04:00
|
|
|
mb_event_group_t event = mbc_slave_check_event(MB_READ_WRITE_MASK);
|
2019-04-10 11:45:22 -04:00
|
|
|
const char* rw_str = (event & MB_READ_MASK) ? "READ" : "WRITE";
|
2020-07-21 12:34:04 -04:00
|
|
|
|
2018-06-28 11:45:41 -04:00
|
|
|
// Filter events and process them accordingly
|
2019-04-10 11:45:22 -04:00
|
|
|
if(event & (MB_EVENT_HOLDING_REG_WR | MB_EVENT_HOLDING_REG_RD)) {
|
2018-06-28 11:45:41 -04:00
|
|
|
// Get parameter information from parameter queue
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
2019-11-26 00:16:25 -05:00
|
|
|
ESP_LOGI(SLAVE_TAG, "HOLDING %s (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
2019-04-10 11:45:22 -04:00
|
|
|
rw_str,
|
2018-06-28 11:45:41 -04:00
|
|
|
(uint32_t)reg_info.time_stamp,
|
|
|
|
(uint32_t)reg_info.mb_offset,
|
|
|
|
(uint32_t)reg_info.type,
|
|
|
|
(uint32_t)reg_info.address,
|
|
|
|
(uint32_t)reg_info.size);
|
2019-11-26 00:16:25 -05:00
|
|
|
if (reg_info.address == (uint8_t*)&holding_reg_params.holding_data0)
|
2018-06-28 11:45:41 -04:00
|
|
|
{
|
2019-11-26 00:16:25 -05:00
|
|
|
portENTER_CRITICAL(¶m_lock);
|
|
|
|
holding_reg_params.holding_data0 += MB_CHAN_DATA_OFFSET;
|
|
|
|
if (holding_reg_params.holding_data0 >= (MB_CHAN_DATA_MAX_VAL - MB_CHAN_DATA_OFFSET)) {
|
|
|
|
coil_reg_params.coils_port1 = 0xFF;
|
|
|
|
}
|
|
|
|
portEXIT_CRITICAL(¶m_lock);
|
2018-06-28 11:45:41 -04:00
|
|
|
}
|
|
|
|
} else if (event & MB_EVENT_INPUT_REG_RD) {
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
2019-11-26 00:16:25 -05:00
|
|
|
ESP_LOGI(SLAVE_TAG, "INPUT READ (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
2018-06-28 11:45:41 -04:00
|
|
|
(uint32_t)reg_info.time_stamp,
|
|
|
|
(uint32_t)reg_info.mb_offset,
|
|
|
|
(uint32_t)reg_info.type,
|
|
|
|
(uint32_t)reg_info.address,
|
|
|
|
(uint32_t)reg_info.size);
|
|
|
|
} else if (event & MB_EVENT_DISCRETE_RD) {
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
2019-11-26 00:16:25 -05:00
|
|
|
ESP_LOGI(SLAVE_TAG, "DISCRETE READ (%u us): ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
2018-06-28 11:45:41 -04:00
|
|
|
(uint32_t)reg_info.time_stamp,
|
|
|
|
(uint32_t)reg_info.mb_offset,
|
|
|
|
(uint32_t)reg_info.type,
|
|
|
|
(uint32_t)reg_info.address,
|
|
|
|
(uint32_t)reg_info.size);
|
2019-04-10 11:45:22 -04:00
|
|
|
} else if (event & (MB_EVENT_COILS_RD | MB_EVENT_COILS_WR)) {
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_get_param_info(®_info, MB_PAR_INFO_GET_TOUT));
|
2019-11-26 00:16:25 -05:00
|
|
|
ESP_LOGI(SLAVE_TAG, "COILS %s (%u us), ADDR:%u, TYPE:%u, INST_ADDR:0x%.4x, SIZE:%u",
|
2019-04-10 11:45:22 -04:00
|
|
|
rw_str,
|
2018-06-28 11:45:41 -04:00
|
|
|
(uint32_t)reg_info.time_stamp,
|
|
|
|
(uint32_t)reg_info.mb_offset,
|
|
|
|
(uint32_t)reg_info.type,
|
|
|
|
(uint32_t)reg_info.address,
|
|
|
|
(uint32_t)reg_info.size);
|
2019-11-26 00:16:25 -05:00
|
|
|
if (coil_reg_params.coils_port1 == 0xFF) break;
|
2018-06-28 11:45:41 -04:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 00:16:25 -05:00
|
|
|
// Destroy of Modbus controller on alarm
|
|
|
|
ESP_LOGI(SLAVE_TAG,"Modbus controller destroyed.");
|
|
|
|
vTaskDelay(100);
|
2018-10-19 09:51:27 -04:00
|
|
|
ESP_ERROR_CHECK(mbc_slave_destroy());
|
2018-06-28 11:45:41 -04:00
|
|
|
}
|