Merge branch 'bugfix/freemodbus_fix_parity_propagation_issue_v41' into 'release/v4.1'

Bugfix/freemodbus fix parity propagation issue (Backport v4.1)

See merge request espressif/esp-idf!12388
This commit is contained in:
Michael (XIAO Xufeng) 2021-02-22 05:26:31 +00:00
commit 52496189d9
5 changed files with 32 additions and 21 deletions

View File

@ -74,6 +74,8 @@ void vMBPortExitCritical(void);
#define MB_PORT_CHECK_EVENT( event, mask ) ( event & mask )
#define MB_PORT_CLEAR_EVENT( event, mask ) do { event &= ~mask; } while(0)
#define MB_PORT_PARITY_GET(parity) ((parity != UART_PARITY_DISABLE) ? \
((parity == UART_PARITY_ODD) ? MB_PAR_ODD : MB_PAR_EVEN) : MB_PAR_NONE)
#ifdef __cplusplus
PR_END_EXTERN_C
#endif /* __cplusplus */

View File

@ -197,7 +197,6 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
UCHAR ucDataBits, eMBParity eParity)
{
esp_err_t xErr = ESP_OK;
MB_PORT_CHECK((eParity <= MB_PAR_EVEN), FALSE, "mb serial set parity failure.");
// Set communication port number
ucUartNumber = ucPORT;
// Configure serial communication parameters
@ -213,6 +212,9 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
case MB_PAR_EVEN:
ucParity = UART_PARITY_EVEN;
break;
default:
ESP_LOGE(TAG, "Incorrect parity option: %d", eParity);
return FALSE;
}
switch(ucDataBits){
case 5:

View File

@ -191,7 +191,6 @@ static void vUartTask(void* pvParameters)
BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{
esp_err_t xErr = ESP_OK;
MB_PORT_CHECK((eParity <= MB_PAR_EVEN), FALSE, "mb serial set parity failure.");
// Set communication port number
ucUartNumber = ucPORT;
// Configure serial communication parameters
@ -207,6 +206,9 @@ BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
case MB_PAR_EVEN:
ucParity = UART_PARITY_EVEN;
break;
default:
ESP_LOGE(TAG, "Incorrect parity option: %d", eParity);
return FALSE;
}
switch(ucDataBits){
case 5:

View File

@ -83,7 +83,7 @@ static esp_err_t mbc_serial_master_setup(void* comm_info)
(uint32_t)comm_info_ptr->mode);
MB_MASTER_CHECK((comm_info_ptr->port < UART_NUM_MAX), ESP_ERR_INVALID_ARG,
"mb wrong port to set = (0x%x).", (uint32_t)comm_info_ptr->port);
MB_MASTER_CHECK((comm_info_ptr->parity <= UART_PARITY_EVEN), ESP_ERR_INVALID_ARG,
MB_MASTER_CHECK((comm_info_ptr->parity <= UART_PARITY_ODD), ESP_ERR_INVALID_ARG,
"mb wrong parity option = (0x%x).", (uint32_t)comm_info_ptr->parity);
// Save the communication options
mbm_opts->mbm_comm = *(mb_communication_info_t*)comm_info_ptr;
@ -102,7 +102,9 @@ static esp_err_t mbc_serial_master_start(void)
// Initialize Modbus stack using mbcontroller parameters
status = eMBMasterInit((eMBMode)comm_info->mode, (UCHAR)comm_info->port,
(ULONG)comm_info->baudrate, (eMBParity)comm_info->parity);
(ULONG)comm_info->baudrate,
MB_PORT_PARITY_GET(comm_info->parity));
MB_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
status = eMBMasterEnable();
@ -145,9 +147,9 @@ static esp_err_t mbc_serial_master_destroy(void)
// Set Modbus parameter description table
static esp_err_t mbc_serial_master_set_descriptor(const mb_parameter_descriptor_t* descriptor, const uint16_t num_elements)
{
MB_MASTER_CHECK((descriptor != NULL),
MB_MASTER_CHECK((descriptor != NULL),
ESP_ERR_INVALID_ARG, "mb incorrect descriptor.");
MB_MASTER_CHECK((num_elements >= 1),
MB_MASTER_CHECK((num_elements >= 1),
ESP_ERR_INVALID_ARG, "mb table size is incorrect.");
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts;
const mb_parameter_descriptor_t *reg_ptr = descriptor;
@ -159,7 +161,7 @@ static esp_err_t mbc_serial_master_set_descriptor(const mb_parameter_descriptor_
ESP_ERR_INVALID_ARG, "mb descriptor cid field is incorrect.");
MB_MASTER_CHECK((reg_ptr->param_key != NULL),
ESP_ERR_INVALID_ARG, "mb descriptor param key is incorrect.");
MB_MASTER_CHECK((reg_ptr->mb_size > 0),
MB_MASTER_CHECK((reg_ptr->mb_size > 0),
ESP_ERR_INVALID_ARG, "mb descriptor param size is incorrect.");
}
mbm_opts->mbm_param_descriptor_table = descriptor;
@ -174,9 +176,9 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
ESP_ERR_INVALID_STATE,
"Master interface uninitialized.");
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts;
MB_MASTER_CHECK((request != NULL),
MB_MASTER_CHECK((request != NULL),
ESP_ERR_INVALID_ARG, "mb request structure.");
MB_MASTER_CHECK((data_ptr != NULL),
MB_MASTER_CHECK((data_ptr != NULL),
ESP_ERR_INVALID_ARG, "mb incorrect data pointer.");
eMBMasterReqErrCode mb_error = MB_MRE_NO_REG;
@ -284,15 +286,15 @@ static esp_err_t mbc_serial_master_get_cid_info(uint16_t cid, const mb_parameter
MB_MASTER_CHECK((param_buffer != NULL),
ESP_ERR_INVALID_ARG, "mb incorrect data buffer pointer.");
MB_MASTER_CHECK((mbm_opts->mbm_param_descriptor_table != NULL),
MB_MASTER_CHECK((mbm_opts->mbm_param_descriptor_table != NULL),
ESP_ERR_INVALID_ARG, "mb incorrect descriptor table or not set.");
MB_MASTER_CHECK((cid < mbm_opts->mbm_param_descriptor_size),
MB_MASTER_CHECK((cid < mbm_opts->mbm_param_descriptor_size),
ESP_ERR_NOT_FOUND, "mb incorrect cid of characteristic.");
// It is assumed that characteristics cid increased in the table
const mb_parameter_descriptor_t* reg_info = &mbm_opts->mbm_param_descriptor_table[cid];
MB_MASTER_CHECK((reg_info->param_key != NULL),
MB_MASTER_CHECK((reg_info->param_key != NULL),
ESP_ERR_INVALID_ARG, "mb incorrect characteristic key.");
*param_buffer = reg_info;
return ESP_OK;
@ -396,15 +398,15 @@ static esp_err_t mbc_serial_master_set_request(char* name, mb_param_mode_t mode,
continue; // The length of strings is different then check next record in the table
}
// Compare the name of parameter with parameter key from table
uint8_t comp_result = memcmp((const char*)name, (const char*)reg_ptr->param_key, (size_t)param_key_len);
int comp_result = memcmp((const void*)name, (const void*)reg_ptr->param_key, (size_t)param_key_len);
if (comp_result == 0) {
// The correct line is found in the table and reg_ptr points to the found parameter description
request->slave_addr = reg_ptr->mb_slave_addr;
request->reg_start = reg_ptr->mb_reg_start;
request->reg_size = reg_ptr->mb_size;
request->command = mbc_serial_master_get_command(reg_ptr->mb_param_type, mode);
MB_MASTER_CHECK((request->command > 0),
ESP_ERR_INVALID_ARG,
MB_MASTER_CHECK((request->command > 0),
ESP_ERR_INVALID_ARG,
"mb incorrect command or parameter type.");
if (reg_data != NULL) {
*reg_data = *reg_ptr; // Set the cid registered parameter data

View File

@ -75,7 +75,7 @@ static esp_err_t mbc_serial_slave_setup(void* comm_info)
(uint32_t)comm_settings->slave_addr);
MB_SLAVE_CHECK((comm_settings->port < UART_NUM_MAX), ESP_ERR_INVALID_ARG,
"mb wrong port to set = (0x%x).", (uint32_t)comm_settings->port);
MB_SLAVE_CHECK((comm_settings->parity <= UART_PARITY_EVEN), ESP_ERR_INVALID_ARG,
MB_SLAVE_CHECK((comm_settings->parity <= UART_PARITY_ODD), ESP_ERR_INVALID_ARG,
"mb wrong parity option = (0x%x).", (uint32_t)comm_settings->parity);
// Set communication options of the controller
@ -91,12 +91,15 @@ static esp_err_t mbc_serial_slave_start(void)
"Slave interface is not correctly initialized.");
mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
eMBErrorCode status = MB_EIO;
const mb_communication_info_t* comm_info = (mb_communication_info_t*)&mbs_opts->mbs_comm;
// Initialize Modbus stack using mbcontroller parameters
status = eMBInit((eMBMode)mbs_opts->mbs_comm.mode,
(UCHAR)mbs_opts->mbs_comm.slave_addr,
(UCHAR)mbs_opts->mbs_comm.port,
(ULONG)mbs_opts->mbs_comm.baudrate,
(eMBParity)mbs_opts->mbs_comm.parity);
status = eMBInit((eMBMode)comm_info->mode,
(UCHAR)comm_info->slave_addr,
(UCHAR)comm_info->port,
(ULONG)comm_info->baudrate,
MB_PORT_PARITY_GET(comm_info->parity));
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
status = eMBEnable();