2018-10-19 09:51:27 -04:00
|
|
|
/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "esp_err.h" // for esp_err_t
|
|
|
|
#include "mbc_master.h" // for master interface define
|
|
|
|
#include "esp_modbus_master.h" // for public interface defines
|
|
|
|
#include "esp_modbus_callbacks.h" // for callback functions
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
// This file implements public API for Modbus master controller.
|
2018-10-19 09:51:27 -04:00
|
|
|
// These functions are wrappers for interface functions of the controller
|
|
|
|
static mb_master_interface_t* master_interface_ptr = NULL;
|
|
|
|
|
2020-07-21 12:34:04 -04:00
|
|
|
void mbc_master_init_iface(void* handler)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
2020-07-21 12:34:04 -04:00
|
|
|
master_interface_ptr = (mb_master_interface_t*) handler;
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modbus controller destroy function
|
|
|
|
*/
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t mbc_master_destroy(void)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->destroy != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->destroy();
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_MASTER_CHECK((error == ESP_OK),
|
|
|
|
error,
|
2020-07-21 12:34:04 -04:00
|
|
|
"Master destroy failure, error=(0x%x).",
|
2019-11-26 00:16:25 -05:00
|
|
|
error);
|
2018-10-19 09:51:27 -04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t mbc_master_get_cid_info(uint16_t cid, const mb_parameter_descriptor_t** param_info)
|
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->get_cid_info != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->get_cid_info(cid, param_info);
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_MASTER_CHECK((error == ESP_OK),
|
|
|
|
error,
|
2020-07-21 12:34:04 -04:00
|
|
|
"Master get cid info failure, error=(0x%x).",
|
2019-11-26 00:16:25 -05:00
|
|
|
error);
|
2018-10-19 09:51:27 -04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parameter data for corresponding characteristic
|
|
|
|
*/
|
|
|
|
esp_err_t mbc_master_get_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t* type)
|
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->get_parameter != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->get_parameter(cid, name, value, type);
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_MASTER_CHECK((error == ESP_OK),
|
2019-11-08 03:55:42 -05:00
|
|
|
error,
|
2020-07-21 12:34:04 -04:00
|
|
|
"Master get parameter failure, error=(0x%x) (%s).",
|
2019-11-08 03:55:42 -05:00
|
|
|
error, esp_err_to_name(error));
|
2018-10-19 09:51:27 -04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send custom Modbus request defined as mb_param_request_t structure
|
|
|
|
*/
|
|
|
|
esp_err_t mbc_master_send_request(mb_param_request_t* request, void* data_ptr)
|
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->send_request != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->send_request(request, data_ptr);
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_MASTER_CHECK((error == ESP_OK),
|
2019-11-08 03:55:42 -05:00
|
|
|
error,
|
2020-07-21 12:34:04 -04:00
|
|
|
"Master send request failure error=(0x%x) (%s).",
|
2019-11-08 03:55:42 -05:00
|
|
|
error, esp_err_to_name(error));
|
|
|
|
return ESP_OK;
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Modbus parameter description table
|
|
|
|
*/
|
2020-07-21 12:34:04 -04:00
|
|
|
esp_err_t mbc_master_set_descriptor(const mb_parameter_descriptor_t* descriptor,
|
|
|
|
const uint16_t num_elements)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->set_descriptor != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->set_descriptor(descriptor, num_elements);
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_MASTER_CHECK((error == ESP_OK),
|
2019-11-08 03:55:42 -05:00
|
|
|
error,
|
2020-07-21 12:34:04 -04:00
|
|
|
"Master set descriptor failure, error=(0x%x) (%s).",
|
2019-11-08 03:55:42 -05:00
|
|
|
error, esp_err_to_name(error));
|
|
|
|
return ESP_OK;
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set parameter value for characteristic selected by name and cid
|
|
|
|
*/
|
|
|
|
esp_err_t mbc_master_set_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t* type)
|
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->set_parameter != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->set_parameter(cid, name, value, type);
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_MASTER_CHECK((error == ESP_OK),
|
2019-11-08 03:55:42 -05:00
|
|
|
error,
|
2020-07-21 12:34:04 -04:00
|
|
|
"Master set parameter failure, error=(0x%x) (%s).",
|
2019-11-08 03:55:42 -05:00
|
|
|
error, esp_err_to_name(error));
|
|
|
|
return ESP_OK;
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup Modbus controller parameters
|
|
|
|
*/
|
|
|
|
esp_err_t mbc_master_setup(void* comm_info)
|
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->setup != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->setup(comm_info);
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_MASTER_CHECK((error == ESP_OK),
|
2019-11-08 03:55:42 -05:00
|
|
|
error,
|
2020-07-21 12:34:04 -04:00
|
|
|
"Master setup failure, error=(0x%x) (%s).",
|
2019-11-08 03:55:42 -05:00
|
|
|
error, esp_err_to_name(error));
|
|
|
|
return ESP_OK;
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modbus controller stack start function
|
|
|
|
*/
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t mbc_master_start(void)
|
2018-10-19 09:51:27 -04:00
|
|
|
{
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->start != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->start();
|
2020-11-10 02:40:01 -05:00
|
|
|
MB_MASTER_CHECK((error == ESP_OK),
|
2019-11-08 03:55:42 -05:00
|
|
|
error,
|
2020-07-21 12:34:04 -04:00
|
|
|
"Master start failure, error=(0x%x) (%s).",
|
2019-11-08 03:55:42 -05:00
|
|
|
error, esp_err_to_name(error));
|
|
|
|
return ESP_OK;
|
2018-10-19 09:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode eMBMasterRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
|
USHORT usNDiscrete)
|
|
|
|
{
|
|
|
|
eMBErrorCode error = MB_ENOERR;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_discrete != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->master_reg_cb_discrete(pucRegBuffer, usAddress, usNDiscrete);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode eMBMasterRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
|
|
|
|
USHORT usNCoils, eMBRegisterMode eMode)
|
|
|
|
{
|
|
|
|
eMBErrorCode error = MB_ENOERR;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_coils != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->master_reg_cb_coils(pucRegBuffer, usAddress,
|
|
|
|
usNCoils, eMode);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode eMBMasterRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
|
USHORT usNRegs, eMBRegisterMode eMode)
|
|
|
|
{
|
|
|
|
eMBErrorCode error = MB_ENOERR;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_holding != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->master_reg_cb_holding(pucRegBuffer, usAddress,
|
|
|
|
usNRegs, eMode);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode eMBMasterRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
|
USHORT usNRegs)
|
|
|
|
{
|
|
|
|
eMBErrorCode error = MB_ENOERR;
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_input != NULL),
|
|
|
|
ESP_ERR_INVALID_STATE,
|
|
|
|
"Master interface is not correctly initialized.");
|
|
|
|
error = master_interface_ptr->master_reg_cb_input(pucRegBuffer, usAddress, usNRegs);
|
|
|
|
return error;
|
2019-11-26 00:16:25 -05:00
|
|
|
}
|