2021-10-22 17:23:03 +00:00
|
|
|
/*
|
2022-01-21 05:18:00 +00:00
|
|
|
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
|
2018-10-19 13:51:27 +00:00
|
|
|
*
|
2021-10-22 17:23:03 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-10-19 13:51:27 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-28 04:29:32 +00:00
|
|
|
#include "esp_err.h" // for esp_err_t
|
2021-11-08 16:28:55 +00:00
|
|
|
#include "esp_timer.h" // for esp_timer_get_time()
|
2021-01-28 04:29:32 +00:00
|
|
|
#include "sdkconfig.h" // for KConfig defines
|
|
|
|
|
2018-10-19 13:51:27 +00:00
|
|
|
#include "mbc_slave.h" // for slave private type definitions
|
2021-01-28 04:29:32 +00:00
|
|
|
#include "mbutils.h" // for stack bit setting utilities
|
2018-10-19 13:51:27 +00:00
|
|
|
#include "esp_modbus_common.h" // for common defines
|
|
|
|
#include "esp_modbus_slave.h" // for public slave defines
|
|
|
|
#include "esp_modbus_callbacks.h" // for modbus callbacks function pointers declaration
|
|
|
|
|
2019-04-26 16:47:21 +00:00
|
|
|
#ifdef CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
|
2018-10-19 13:51:27 +00:00
|
|
|
|
|
|
|
#define MB_ID_BYTE0(id) ((uint8_t)(id))
|
|
|
|
#define MB_ID_BYTE1(id) ((uint8_t)(((uint16_t)(id) >> 8) & 0xFF))
|
|
|
|
#define MB_ID_BYTE2(id) ((uint8_t)(((uint32_t)(id) >> 16) & 0xFF))
|
|
|
|
#define MB_ID_BYTE3(id) ((uint8_t)(((uint32_t)(id) >> 24) & 0xFF))
|
|
|
|
|
2019-04-26 16:47:21 +00:00
|
|
|
#define MB_CONTROLLER_SLAVE_ID (CONFIG_FMB_CONTROLLER_SLAVE_ID)
|
2020-07-21 16:34:04 +00:00
|
|
|
#define MB_SLAVE_ID_SHORT (MB_ID_BYTE3(MB_CONTROLLER_SLAVE_ID))
|
2018-10-19 13:51:27 +00:00
|
|
|
|
|
|
|
// Slave ID constant
|
|
|
|
static uint8_t mb_slave_id[] = { MB_ID_BYTE0(MB_CONTROLLER_SLAVE_ID),
|
|
|
|
MB_ID_BYTE1(MB_CONTROLLER_SLAVE_ID),
|
|
|
|
MB_ID_BYTE2(MB_CONTROLLER_SLAVE_ID) };
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-01-28 04:29:32 +00:00
|
|
|
#define REG_SIZE(type, nregs) ((type == MB_PARAM_INPUT) || (type == MB_PARAM_HOLDING)) ? (nregs >> 1) : (nregs << 3)
|
|
|
|
|
2018-10-19 13:51:27 +00:00
|
|
|
// Common interface pointer for slave port
|
|
|
|
static mb_slave_interface_t* slave_interface_ptr = NULL;
|
2022-01-21 05:18:00 +00:00
|
|
|
static const char TAG[] __attribute__((unused)) = "MB_CONTROLLER_SLAVE";
|
2018-10-19 13:51:27 +00:00
|
|
|
|
2021-01-28 04:29:32 +00:00
|
|
|
// Searches the register in the area specified by type, returns descriptor if found, else NULL
|
|
|
|
static mb_descr_entry_t* mbc_slave_find_reg_descriptor(mb_param_type_t type, uint16_t addr, size_t regs)
|
|
|
|
{
|
|
|
|
mb_descr_entry_t* it;
|
|
|
|
uint16_t reg_size = 0;
|
|
|
|
|
|
|
|
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
|
|
|
|
|
|
|
if (LIST_EMPTY(&mbs_opts->mbs_area_descriptors[type])) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// search for the register in each area
|
|
|
|
for (it = LIST_FIRST(&mbs_opts->mbs_area_descriptors[type]); it != NULL; it = LIST_NEXT(it, entries)) {
|
|
|
|
reg_size = REG_SIZE(type, it->size);
|
|
|
|
if ((addr >= it->start_offset)
|
|
|
|
&& (it->p_data)
|
|
|
|
&& (regs >= 1)
|
2021-02-19 14:39:08 +00:00
|
|
|
&& ((addr + regs) <= (it->start_offset + reg_size))
|
2021-01-28 04:29:32 +00:00
|
|
|
&& (reg_size >= 1)) {
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mbc_slave_free_descriptors(void) {
|
|
|
|
|
|
|
|
mb_descr_entry_t* it;
|
|
|
|
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
|
|
|
|
|
|
|
for (int descr_type = 0; descr_type < MB_PARAM_COUNT; descr_type++) {
|
|
|
|
for (it = LIST_FIRST(&mbs_opts->mbs_area_descriptors[descr_type]); it != NULL; it = LIST_NEXT(it, entries)) {
|
|
|
|
LIST_REMOVE(it, entries);
|
|
|
|
free(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:34:04 +00:00
|
|
|
void mbc_slave_init_iface(void* handler)
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
2020-07-21 16:34:04 +00:00
|
|
|
slave_interface_ptr = (mb_slave_interface_t*) handler;
|
2021-01-28 04:29:32 +00:00
|
|
|
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
|
|
|
// Initialize list head for register areas
|
|
|
|
LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_INPUT]);
|
|
|
|
LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_HOLDING]);
|
|
|
|
LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_COIL]);
|
|
|
|
LIST_INIT(&mbs_opts->mbs_area_descriptors[MB_PARAM_DISCRETE]);
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modbus controller destroy function
|
|
|
|
*/
|
2019-07-16 09:33:30 +00:00
|
|
|
esp_err_t mbc_slave_destroy(void)
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
// Is initialization done?
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
|
|
|
// Check if interface has been initialized
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr->destroy != NULL),
|
2018-10-19 13:51:27 +00:00
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
|
|
|
// Call the slave port destroy function
|
|
|
|
error = slave_interface_ptr->destroy();
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((error == ESP_OK),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
2020-07-21 16:34:04 +00:00
|
|
|
"Slave destroy failure error=(0x%x).",
|
|
|
|
error);
|
2021-01-28 04:29:32 +00:00
|
|
|
// Destroy all opened descriptors
|
|
|
|
mbc_slave_free_descriptors();
|
|
|
|
free(slave_interface_ptr);
|
|
|
|
slave_interface_ptr = NULL;
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup Modbus controller parameters
|
|
|
|
*/
|
|
|
|
esp_err_t mbc_slave_setup(void* comm_info)
|
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr->setup != NULL),
|
2018-10-19 13:51:27 +00:00
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
|
|
|
error = slave_interface_ptr->setup(comm_info);
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((error == ESP_OK),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
2020-07-21 16:34:04 +00:00
|
|
|
"Slave setup failure error=(0x%x).",
|
|
|
|
error);
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start Modbus controller start function
|
|
|
|
*/
|
2019-07-16 09:33:30 +00:00
|
|
|
esp_err_t mbc_slave_start(void)
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr->start != NULL),
|
2018-10-19 13:51:27 +00:00
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2019-04-26 16:47:21 +00:00
|
|
|
#ifdef CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
|
2018-10-19 13:51:27 +00:00
|
|
|
// Set the slave ID if the KConfig option is selected
|
|
|
|
eMBErrorCode status = eMBSetSlaveID(MB_SLAVE_ID_SHORT, TRUE, (UCHAR*)mb_slave_id, sizeof(mb_slave_id));
|
|
|
|
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack set slave ID failure.");
|
|
|
|
#endif
|
|
|
|
error = slave_interface_ptr->start();
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((error == ESP_OK),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
2020-07-21 16:34:04 +00:00
|
|
|
"Slave start failure error=(0x%x).",
|
|
|
|
error);
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
2020-11-10 07:40:01 +00:00
|
|
|
}
|
2018-10-19 13:51:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocking function to get event on parameter group change for application task
|
|
|
|
*/
|
|
|
|
mb_event_group_t mbc_slave_check_event(mb_event_group_t group)
|
|
|
|
{
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
MB_EVENT_NO_EVENTS,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr->check_event != NULL),
|
2018-10-19 13:51:27 +00:00
|
|
|
MB_EVENT_NO_EVENTS,
|
|
|
|
"Slave interface is not correctly initialized.");
|
|
|
|
mb_event_group_t event = slave_interface_ptr->check_event(group);
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to get notification about parameter change from application task
|
|
|
|
*/
|
|
|
|
esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout)
|
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr->get_param_info != NULL),
|
2018-10-19 13:51:27 +00:00
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
|
|
|
error = slave_interface_ptr->get_param_info(reg_info, timeout);
|
2020-11-10 07:40:01 +00:00
|
|
|
MB_SLAVE_CHECK((error == ESP_OK),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
2020-07-21 16:34:04 +00:00
|
|
|
"Slave get parameter info failure error=(0x%x).",
|
|
|
|
error);
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to set area descriptors for modbus parameters
|
|
|
|
*/
|
|
|
|
esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data)
|
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2021-01-28 04:29:32 +00:00
|
|
|
|
|
|
|
if (slave_interface_ptr->set_descriptor != NULL) {
|
|
|
|
error = slave_interface_ptr->set_descriptor(descr_data);
|
|
|
|
MB_SLAVE_CHECK((error == ESP_OK),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave set descriptor failure error=(0x%x).",
|
|
|
|
(uint16_t)error);
|
|
|
|
} else {
|
|
|
|
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
|
|
|
// Check if the address is already in the descriptor list
|
|
|
|
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(descr_data.type, descr_data.start_offset, 1);
|
|
|
|
MB_SLAVE_CHECK((it == NULL), ESP_ERR_INVALID_ARG, "mb incorrect descriptor or already defined.");
|
|
|
|
|
|
|
|
mb_descr_entry_t* new_descr = (mb_descr_entry_t*) heap_caps_malloc(sizeof(mb_descr_entry_t),
|
|
|
|
MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
|
|
|
|
MB_SLAVE_CHECK((new_descr != NULL), ESP_ERR_NO_MEM, "mb can not allocate memory for descriptor.");
|
|
|
|
new_descr->start_offset = descr_data.start_offset;
|
|
|
|
new_descr->type = descr_data.type;
|
|
|
|
new_descr->p_data = descr_data.address;
|
|
|
|
new_descr->size = descr_data.size;
|
|
|
|
LIST_INSERT_HEAD(&mbs_opts->mbs_area_descriptors[descr_data.type], new_descr, entries);
|
|
|
|
error = ESP_OK;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The helper function to get time stamp in microseconds
|
|
|
|
static uint64_t mbc_slave_get_time_stamp(void)
|
|
|
|
{
|
|
|
|
uint64_t time_stamp = esp_timer_get_time();
|
|
|
|
return time_stamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to send parameter information to application task
|
|
|
|
static esp_err_t mbc_slave_send_param_info(mb_event_group_t par_type, uint16_t mb_offset,
|
|
|
|
uint8_t* par_address, uint16_t par_size)
|
|
|
|
{
|
|
|
|
MB_SLAVE_ASSERT(slave_interface_ptr != NULL);
|
|
|
|
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
|
|
|
esp_err_t error = ESP_FAIL;
|
|
|
|
mb_param_info_t par_info;
|
|
|
|
// Check if queue is not full the send parameter information
|
|
|
|
par_info.type = par_type;
|
|
|
|
par_info.size = par_size;
|
|
|
|
par_info.address = par_address;
|
|
|
|
par_info.time_stamp = mbc_slave_get_time_stamp();
|
|
|
|
par_info.mb_offset = mb_offset;
|
|
|
|
BaseType_t status = xQueueSend(mbs_opts->mbs_notification_queue_handle, &par_info, MB_PAR_INFO_TOUT);
|
|
|
|
if (pdTRUE == status) {
|
2022-01-21 05:18:00 +00:00
|
|
|
ESP_LOGD(TAG, "Queue send parameter info (type, address, size): %d, 0x%.4x, %d",
|
2021-01-28 04:29:32 +00:00
|
|
|
par_type, (uint32_t)par_address, par_size);
|
|
|
|
error = ESP_OK;
|
|
|
|
} else if (errQUEUE_FULL == status) {
|
2022-01-21 05:18:00 +00:00
|
|
|
ESP_LOGD(TAG, "Parameter queue is overflowed.");
|
2021-01-28 04:29:32 +00:00
|
|
|
}
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-01-28 04:29:32 +00:00
|
|
|
// Helper function to send notification
|
|
|
|
static esp_err_t mbc_slave_send_param_access_notification(mb_event_group_t event)
|
|
|
|
{
|
|
|
|
MB_SLAVE_ASSERT(slave_interface_ptr != NULL);
|
|
|
|
mb_slave_options_t* mbs_opts = &slave_interface_ptr->opts;
|
|
|
|
esp_err_t err = ESP_FAIL;
|
|
|
|
mb_event_group_t bits = (mb_event_group_t)xEventGroupSetBits(mbs_opts->mbs_event_group, (EventBits_t)event);
|
|
|
|
if (bits & event) {
|
2022-01-21 05:18:00 +00:00
|
|
|
ESP_LOGD(TAG, "The MB_REG_CHANGE_EVENT = 0x%.2x is set.", (uint8_t)event);
|
2021-01-28 04:29:32 +00:00
|
|
|
err = ESP_OK;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Below are the common slave read/write register callback functions
|
|
|
|
* The concrete slave port can override them using interface function pointers
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Callback function for reading of MB Input Registers
|
|
|
|
eMBErrorCode mbc_reg_input_slave_cb(UCHAR * reg_buffer, USHORT address, USHORT n_regs)
|
|
|
|
{
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
MB_EILLSTATE, "Slave stack uninitialized.");
|
|
|
|
MB_SLAVE_CHECK((reg_buffer != NULL),
|
|
|
|
MB_EINVAL, "Slave stack call failed.");
|
|
|
|
eMBErrorCode status = MB_ENOERR;
|
|
|
|
address--; // address of register is already +1
|
|
|
|
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_INPUT, address, n_regs);
|
|
|
|
if (it != NULL) {
|
|
|
|
uint16_t input_reg_start = (uint16_t)it->start_offset; // Get Modbus start address
|
|
|
|
uint8_t* input_buffer = (uint8_t*)it->p_data; // Get instance address
|
|
|
|
uint16_t regs = n_regs;
|
|
|
|
uint16_t reg_index;
|
|
|
|
// If input or configuration parameters are incorrect then return an error to stack layer
|
|
|
|
reg_index = (uint16_t)(address - input_reg_start);
|
|
|
|
reg_index <<= 1; // register Address to byte address
|
|
|
|
input_buffer += reg_index;
|
|
|
|
uint8_t* buffer_start = input_buffer;
|
|
|
|
while (regs > 0) {
|
|
|
|
_XFER_2_RD(reg_buffer, input_buffer);
|
|
|
|
reg_index += 2;
|
|
|
|
regs -= 1;
|
|
|
|
}
|
|
|
|
// Send access notification
|
|
|
|
(void)mbc_slave_send_param_access_notification(MB_EVENT_INPUT_REG_RD);
|
|
|
|
// Send parameter info to application task
|
|
|
|
(void)mbc_slave_send_param_info(MB_EVENT_INPUT_REG_RD, (uint16_t)address,
|
|
|
|
(uint8_t*)buffer_start, (uint16_t)n_regs);
|
|
|
|
} else {
|
|
|
|
status = MB_ENOREG;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback function for reading of MB Holding Registers
|
|
|
|
// Executed by stack when request to read/write holding registers is received
|
|
|
|
eMBErrorCode mbc_reg_holding_slave_cb(UCHAR * reg_buffer, USHORT address, USHORT n_regs, eMBRegisterMode mode)
|
|
|
|
{
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
MB_EILLSTATE, "Slave stack uninitialized.");
|
|
|
|
MB_SLAVE_CHECK((reg_buffer != NULL),
|
|
|
|
MB_EINVAL, "Slave stack call failed.");
|
|
|
|
eMBErrorCode status = MB_ENOERR;
|
|
|
|
uint16_t reg_index;
|
|
|
|
address--; // address of register is already +1
|
|
|
|
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_HOLDING, address, n_regs);
|
|
|
|
if (it != NULL) {
|
|
|
|
uint16_t reg_holding_start = (uint16_t)it->start_offset; // Get Modbus start address
|
|
|
|
uint8_t* holding_buffer = (uint8_t*)it->p_data; // Get instance address
|
|
|
|
uint16_t regs = n_regs;
|
|
|
|
reg_index = (uint16_t) (address - reg_holding_start);
|
|
|
|
reg_index <<= 1; // register Address to byte address
|
|
|
|
holding_buffer += reg_index;
|
|
|
|
uint8_t* buffer_start = holding_buffer;
|
|
|
|
switch (mode) {
|
|
|
|
case MB_REG_READ:
|
|
|
|
while (regs > 0) {
|
|
|
|
_XFER_2_RD(reg_buffer, holding_buffer);
|
|
|
|
reg_index += 2;
|
|
|
|
regs -= 1;
|
|
|
|
};
|
|
|
|
// Send access notification
|
|
|
|
(void)mbc_slave_send_param_access_notification(MB_EVENT_HOLDING_REG_RD);
|
|
|
|
// Send parameter info
|
|
|
|
(void)mbc_slave_send_param_info(MB_EVENT_HOLDING_REG_RD, (uint16_t)address,
|
|
|
|
(uint8_t*)buffer_start, (uint16_t)n_regs);
|
|
|
|
break;
|
|
|
|
case MB_REG_WRITE:
|
|
|
|
while (regs > 0) {
|
|
|
|
_XFER_2_WR(holding_buffer, reg_buffer);
|
|
|
|
holding_buffer += 2;
|
|
|
|
reg_index += 2;
|
|
|
|
regs -= 1;
|
|
|
|
};
|
|
|
|
// Send access notification
|
|
|
|
(void)mbc_slave_send_param_access_notification(MB_EVENT_HOLDING_REG_WR);
|
|
|
|
// Send parameter info
|
|
|
|
(void)mbc_slave_send_param_info(MB_EVENT_HOLDING_REG_WR, (uint16_t)address,
|
|
|
|
(uint8_t*)buffer_start, (uint16_t)n_regs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = MB_ENOREG;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback function for reading of MB Coils Registers
|
|
|
|
eMBErrorCode mbc_reg_coils_slave_cb(UCHAR* reg_buffer, USHORT address, USHORT n_coils, eMBRegisterMode mode)
|
|
|
|
{
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
MB_EILLSTATE, "Slave stack uninitialized.");
|
|
|
|
MB_SLAVE_CHECK((reg_buffer != NULL),
|
|
|
|
MB_EINVAL, "Slave stack call failed.");
|
|
|
|
eMBErrorCode status = MB_ENOERR;
|
|
|
|
uint16_t reg_index;
|
|
|
|
uint16_t coils = n_coils;
|
|
|
|
address--; // The address is already +1
|
|
|
|
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_COIL, address, n_coils);
|
|
|
|
if (it != NULL) {
|
|
|
|
uint16_t reg_coils_start = (uint16_t)it->start_offset; // MB offset of coils
|
|
|
|
uint8_t* reg_coils_buf = (uint8_t*)it->p_data;
|
|
|
|
reg_index = (uint16_t) (address - it->start_offset);
|
|
|
|
CHAR* coils_data_buf = (CHAR*)(reg_coils_buf + (reg_index >> 3));
|
|
|
|
switch (mode) {
|
|
|
|
case MB_REG_READ:
|
|
|
|
while (coils > 0) {
|
|
|
|
uint8_t result = xMBUtilGetBits((uint8_t*)reg_coils_buf, reg_index, 1);
|
|
|
|
xMBUtilSetBits(reg_buffer, reg_index - (address - reg_coils_start), 1, result);
|
|
|
|
reg_index++;
|
|
|
|
coils--;
|
|
|
|
}
|
|
|
|
// Send an event to notify application task about event
|
|
|
|
(void)mbc_slave_send_param_access_notification(MB_EVENT_COILS_RD);
|
|
|
|
(void)mbc_slave_send_param_info(MB_EVENT_COILS_RD, (uint16_t)address,
|
|
|
|
(uint8_t*)(coils_data_buf), (uint16_t)n_coils);
|
|
|
|
break;
|
|
|
|
case MB_REG_WRITE:
|
|
|
|
while (coils > 0) {
|
|
|
|
uint8_t result = xMBUtilGetBits(reg_buffer,
|
|
|
|
reg_index - (address - reg_coils_start), 1);
|
|
|
|
xMBUtilSetBits((uint8_t*)reg_coils_buf, reg_index, 1, result);
|
|
|
|
reg_index++;
|
|
|
|
coils--;
|
|
|
|
}
|
|
|
|
// Send an event to notify application task about event
|
|
|
|
(void)mbc_slave_send_param_access_notification(MB_EVENT_COILS_WR);
|
|
|
|
(void)mbc_slave_send_param_info(MB_EVENT_COILS_WR, (uint16_t)address,
|
|
|
|
(uint8_t*)coils_data_buf, (uint16_t)n_coils);
|
|
|
|
break;
|
|
|
|
} // switch ( eMode )
|
|
|
|
} else {
|
|
|
|
// If the configuration or input parameters are incorrect then return error to stack
|
|
|
|
status = MB_ENOREG;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback function for reading of MB Discrete Input Registers
|
|
|
|
eMBErrorCode mbc_reg_discrete_slave_cb(UCHAR* reg_buffer, USHORT address, USHORT n_discrete)
|
|
|
|
{
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
MB_EILLSTATE, "Slave stack uninitialized.");
|
|
|
|
MB_SLAVE_CHECK((reg_buffer != NULL),
|
|
|
|
MB_EINVAL, "Slave stack call failed.");
|
|
|
|
|
|
|
|
eMBErrorCode status = MB_ENOERR;
|
|
|
|
uint16_t reg_index;
|
|
|
|
uint16_t reg_bit_index;
|
|
|
|
uint16_t n_reg;
|
|
|
|
uint8_t* discrete_input_buf;
|
|
|
|
// It already plus one in modbus function method.
|
|
|
|
address--;
|
|
|
|
mb_descr_entry_t* it = mbc_slave_find_reg_descriptor(MB_PARAM_DISCRETE, address, n_discrete);
|
|
|
|
if (it != NULL) {
|
|
|
|
uint16_t reg_discrete_start = (uint16_t)it->start_offset; // MB offset of registers
|
|
|
|
n_reg = (n_discrete >> 3) + 1;
|
|
|
|
discrete_input_buf = (uint8_t*)it->p_data; // the storage address
|
|
|
|
reg_index = (uint16_t) (address - reg_discrete_start) / 8; // Get register index in the buffer for bit number
|
|
|
|
reg_bit_index = (uint16_t)(address - reg_discrete_start) % 8; // Get bit index
|
|
|
|
uint8_t* temp_buf = &discrete_input_buf[reg_index];
|
|
|
|
while (n_reg > 0) {
|
|
|
|
*reg_buffer++ = xMBUtilGetBits(&discrete_input_buf[reg_index++], reg_bit_index, 8);
|
|
|
|
n_reg--;
|
|
|
|
}
|
|
|
|
reg_buffer--;
|
|
|
|
// Last discrete
|
|
|
|
n_discrete = n_discrete % 8;
|
|
|
|
// Filling zero to high bit
|
|
|
|
*reg_buffer = *reg_buffer << (8 - n_discrete);
|
|
|
|
*reg_buffer = *reg_buffer >> (8 - n_discrete);
|
|
|
|
// Send an event to notify application task about event
|
|
|
|
(void)mbc_slave_send_param_access_notification(MB_EVENT_DISCRETE_RD);
|
|
|
|
(void)mbc_slave_send_param_info(MB_EVENT_DISCRETE_RD, (uint16_t)address,
|
|
|
|
(uint8_t*)temp_buf, (uint16_t)n_discrete);
|
|
|
|
} else {
|
|
|
|
status = MB_ENOREG;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-10-19 13:51:27 +00:00
|
|
|
/**
|
2021-01-28 04:29:32 +00:00
|
|
|
* Below are the stack callback functions to read/write registers
|
2018-10-19 13:51:27 +00:00
|
|
|
*/
|
2021-01-28 04:29:32 +00:00
|
|
|
eMBErrorCode eMBRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
|
|
|
eMBErrorCode error = MB_ENOERR;
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2021-01-28 04:29:32 +00:00
|
|
|
// Check if the callback is overridden in concrete port
|
|
|
|
if (slave_interface_ptr->slave_reg_cb_discrete) {
|
|
|
|
error = slave_interface_ptr->slave_reg_cb_discrete(pucRegBuffer, usAddress, usNDiscrete);
|
|
|
|
} else {
|
|
|
|
error = mbc_reg_discrete_slave_cb(pucRegBuffer, usAddress, usNDiscrete);
|
|
|
|
}
|
2020-11-10 07:40:01 +00:00
|
|
|
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode eMBRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
|
2021-01-28 04:29:32 +00:00
|
|
|
USHORT usNCoils, eMBRegisterMode eMode)
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
|
|
|
eMBErrorCode error = MB_ENOERR;
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2021-01-28 04:29:32 +00:00
|
|
|
|
|
|
|
if (slave_interface_ptr->slave_reg_cb_coils) {
|
|
|
|
error = slave_interface_ptr->slave_reg_cb_coils(pucRegBuffer, usAddress, usNCoils, eMode);
|
|
|
|
} else {
|
|
|
|
error = mbc_reg_coils_slave_cb(pucRegBuffer, usAddress, usNCoils, eMode);
|
|
|
|
}
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode eMBRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
2021-01-28 04:29:32 +00:00
|
|
|
USHORT usNRegs, eMBRegisterMode eMode)
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
|
|
|
eMBErrorCode error = MB_ENOERR;
|
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2021-01-28 04:29:32 +00:00
|
|
|
|
|
|
|
if (slave_interface_ptr->slave_reg_cb_holding) {
|
|
|
|
error = slave_interface_ptr->slave_reg_cb_holding(pucRegBuffer, usAddress, usNRegs, eMode);
|
|
|
|
} else {
|
|
|
|
error = mbc_reg_holding_slave_cb(pucRegBuffer, usAddress, usNRegs, eMode);
|
|
|
|
}
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-01-28 04:29:32 +00:00
|
|
|
eMBErrorCode eMBRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs)
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
2021-01-28 04:29:32 +00:00
|
|
|
eMBErrorCode error = ESP_ERR_INVALID_STATE;
|
2018-10-19 13:51:27 +00:00
|
|
|
MB_SLAVE_CHECK((slave_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Slave interface is not correctly initialized.");
|
2021-01-28 04:29:32 +00:00
|
|
|
|
|
|
|
if (slave_interface_ptr->slave_reg_cb_input) {
|
|
|
|
error = slave_interface_ptr->slave_reg_cb_input(pucRegBuffer, usAddress, usNRegs);
|
|
|
|
} else {
|
|
|
|
error = mbc_reg_input_slave_cb(pucRegBuffer, usAddress, usNRegs);
|
|
|
|
}
|
2018-10-19 13:51:27 +00:00
|
|
|
return error;
|
2019-04-26 16:47:21 +00:00
|
|
|
}
|