mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-02 20:24:32 +02:00
freemodbus: fix mb controller parity propagation issues
Closes https://github.com/espressif/esp-idf/issues/6377
This commit is contained in:
@@ -74,6 +74,8 @@ void vMBPortExitCritical(void);
|
|||||||
#define MB_PORT_CHECK_EVENT( event, mask ) ( event & mask )
|
#define MB_PORT_CHECK_EVENT( event, mask ) ( event & mask )
|
||||||
#define MB_PORT_CLEAR_EVENT( event, mask ) do { event &= ~mask; } while(0)
|
#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
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
@@ -197,7 +197,6 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
|
|||||||
UCHAR ucDataBits, eMBParity eParity)
|
UCHAR ucDataBits, eMBParity eParity)
|
||||||
{
|
{
|
||||||
esp_err_t xErr = ESP_OK;
|
esp_err_t xErr = ESP_OK;
|
||||||
MB_PORT_CHECK((eParity <= MB_PAR_EVEN), FALSE, "mb serial set parity failure.");
|
|
||||||
// Set communication port number
|
// Set communication port number
|
||||||
ucUartNumber = ucPORT;
|
ucUartNumber = ucPORT;
|
||||||
// Configure serial communication parameters
|
// Configure serial communication parameters
|
||||||
@@ -213,6 +212,9 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
|
|||||||
case MB_PAR_EVEN:
|
case MB_PAR_EVEN:
|
||||||
ucParity = UART_PARITY_EVEN;
|
ucParity = UART_PARITY_EVEN;
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGE(TAG, "Incorrect parity option: %d", eParity);
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
switch(ucDataBits){
|
switch(ucDataBits){
|
||||||
case 5:
|
case 5:
|
||||||
|
@@ -191,7 +191,6 @@ static void vUartTask(void* pvParameters)
|
|||||||
BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
|
BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
|
||||||
{
|
{
|
||||||
esp_err_t xErr = ESP_OK;
|
esp_err_t xErr = ESP_OK;
|
||||||
MB_PORT_CHECK((eParity <= MB_PAR_EVEN), FALSE, "mb serial set parity failure.");
|
|
||||||
// Set communication port number
|
// Set communication port number
|
||||||
ucUartNumber = ucPORT;
|
ucUartNumber = ucPORT;
|
||||||
// Configure serial communication parameters
|
// Configure serial communication parameters
|
||||||
@@ -207,6 +206,9 @@ BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
|||||||
case MB_PAR_EVEN:
|
case MB_PAR_EVEN:
|
||||||
ucParity = UART_PARITY_EVEN;
|
ucParity = UART_PARITY_EVEN;
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGE(TAG, "Incorrect parity option: %d", eParity);
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
switch(ucDataBits){
|
switch(ucDataBits){
|
||||||
case 5:
|
case 5:
|
||||||
|
@@ -83,7 +83,7 @@ static esp_err_t mbc_serial_master_setup(void* comm_info)
|
|||||||
(uint32_t)comm_info_ptr->mode);
|
(uint32_t)comm_info_ptr->mode);
|
||||||
MB_MASTER_CHECK((comm_info_ptr->port < UART_NUM_MAX), ESP_ERR_INVALID_ARG,
|
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 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);
|
"mb wrong parity option = (0x%x).", (uint32_t)comm_info_ptr->parity);
|
||||||
// Save the communication options
|
// Save the communication options
|
||||||
mbm_opts->mbm_comm = *(mb_communication_info_t*)comm_info_ptr;
|
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
|
// Initialize Modbus stack using mbcontroller parameters
|
||||||
status = eMBMasterInit((eMBMode)comm_info->mode, (UCHAR)comm_info->port,
|
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_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||||
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
||||||
status = eMBMasterEnable();
|
status = eMBMasterEnable();
|
||||||
@@ -396,7 +398,7 @@ 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
|
continue; // The length of strings is different then check next record in the table
|
||||||
}
|
}
|
||||||
// Compare the name of parameter with parameter key from 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) {
|
if (comp_result == 0) {
|
||||||
// The correct line is found in the table and reg_ptr points to the found parameter description
|
// 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->slave_addr = reg_ptr->mb_slave_addr;
|
||||||
|
@@ -75,7 +75,7 @@ static esp_err_t mbc_serial_slave_setup(void* comm_info)
|
|||||||
(uint32_t)comm_settings->slave_addr);
|
(uint32_t)comm_settings->slave_addr);
|
||||||
MB_SLAVE_CHECK((comm_settings->port < UART_NUM_MAX), ESP_ERR_INVALID_ARG,
|
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 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);
|
"mb wrong parity option = (0x%x).", (uint32_t)comm_settings->parity);
|
||||||
|
|
||||||
// Set communication options of the controller
|
// 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.");
|
"Slave interface is not correctly initialized.");
|
||||||
mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts;
|
||||||
eMBErrorCode status = MB_EIO;
|
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
|
// Initialize Modbus stack using mbcontroller parameters
|
||||||
status = eMBInit((eMBMode)mbs_opts->mbs_comm.mode,
|
status = eMBInit((eMBMode)comm_info->mode,
|
||||||
(UCHAR)mbs_opts->mbs_comm.slave_addr,
|
(UCHAR)comm_info->slave_addr,
|
||||||
(UCHAR)mbs_opts->mbs_comm.port,
|
(UCHAR)comm_info->port,
|
||||||
(ULONG)mbs_opts->mbs_comm.baudrate,
|
(ULONG)comm_info->baudrate,
|
||||||
(eMBParity)mbs_opts->mbs_comm.parity);
|
MB_PORT_PARITY_GET(comm_info->parity));
|
||||||
|
|
||||||
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||||
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
"mb stack initialization failure, eMBInit() returns (0x%x).", status);
|
||||||
status = eMBEnable();
|
status = eMBEnable();
|
||||||
|
Reference in New Issue
Block a user