mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-31 19:24:33 +02:00
freemodbus: fix port enable disable sequence for tcp master and slave
This commit is contained in:
@@ -222,6 +222,8 @@ BOOL xMBTCPPortInit( USHORT usTCPPort );
|
|||||||
|
|
||||||
void vMBTCPPortClose( void );
|
void vMBTCPPortClose( void );
|
||||||
|
|
||||||
|
void vMBTCPPortEnable( void );
|
||||||
|
|
||||||
void vMBTCPPortDisable( void );
|
void vMBTCPPortDisable( void );
|
||||||
|
|
||||||
BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
||||||
@@ -235,6 +237,8 @@ BOOL xMBMasterTCPPortInit( USHORT usTCPPort );
|
|||||||
|
|
||||||
void vMBMasterTCPPortClose( void );
|
void vMBMasterTCPPortClose( void );
|
||||||
|
|
||||||
|
void vMBMasterTCPPortEnable( void );
|
||||||
|
|
||||||
void vMBMasterTCPPortDisable( void );
|
void vMBMasterTCPPortDisable( void );
|
||||||
|
|
||||||
BOOL xMBMasterTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
BOOL xMBMasterTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
||||||
|
@@ -86,12 +86,15 @@ eMBTCPDoInit( USHORT ucTCPPort )
|
|||||||
void
|
void
|
||||||
eMBTCPStart( void )
|
eMBTCPStart( void )
|
||||||
{
|
{
|
||||||
|
ESP_LOGD(MB_PORT_TAG, "TCP Slave port enable.");
|
||||||
|
vMBTCPPortEnable( );
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
eMBTCPStop( void )
|
eMBTCPStop( void )
|
||||||
{
|
{
|
||||||
/* Make sure that no more clients are connected. */
|
/* Make sure that no more clients are connected. */
|
||||||
|
ESP_LOGD(MB_PORT_TAG, "TCP Slave port disable.");
|
||||||
vMBTCPPortDisable( );
|
vMBTCPPortDisable( );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -86,11 +86,15 @@ eMBMasterTCPDoInit( USHORT ucTCPPort )
|
|||||||
void
|
void
|
||||||
eMBMasterTCPStart( void )
|
eMBMasterTCPStart( void )
|
||||||
{
|
{
|
||||||
|
ESP_LOGD(MB_PORT_TAG, "TCP Master port enable.");
|
||||||
|
vMBMasterTCPPortEnable( );
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
eMBMasterTCPStop( void )
|
eMBMasterTCPStop( void )
|
||||||
{
|
{
|
||||||
|
ESP_LOGD(MB_PORT_TAG, "TCP Master port disable.");
|
||||||
|
vMBMasterTCPPortDisable( );
|
||||||
}
|
}
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
|
@@ -165,7 +165,10 @@ static esp_err_t mbc_tcp_master_start(void)
|
|||||||
eMBPortProto proto = (comm_info->ip_mode == MB_MODE_TCP) ? MB_PROTO_TCP : MB_PROTO_UDP;
|
eMBPortProto proto = (comm_info->ip_mode == MB_MODE_TCP) ? MB_PROTO_TCP : MB_PROTO_UDP;
|
||||||
eMBPortIpVer ip_ver = (comm_info->ip_addr_type == MB_IPV4) ? MB_PORT_IPV4 : MB_PORT_IPV6;
|
eMBPortIpVer ip_ver = (comm_info->ip_addr_type == MB_IPV4) ? MB_PORT_IPV4 : MB_PORT_IPV6;
|
||||||
vMBTCPPortMasterSetNetOpt(comm_info->ip_netif_ptr, ip_ver, proto);
|
vMBTCPPortMasterSetNetOpt(comm_info->ip_netif_ptr, ip_ver, proto);
|
||||||
vMBTCPPortMasterTaskStart();
|
|
||||||
|
status = eMBMasterEnable();
|
||||||
|
MB_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||||
|
"mb stack set slave ID failure, eMBMasterEnable() returned (0x%x).", (uint32_t)status);
|
||||||
|
|
||||||
// Add slave IP address for each slave to initialize connection
|
// Add slave IP address for each slave to initialize connection
|
||||||
mb_slave_addr_entry_t *p_slave_info;
|
mb_slave_addr_entry_t *p_slave_info;
|
||||||
@@ -178,10 +181,6 @@ static esp_err_t mbc_tcp_master_start(void)
|
|||||||
// Add end of list condition
|
// Add end of list condition
|
||||||
(void)xMBTCPPortMasterAddSlaveIp(0xFF, NULL, 0xFF);
|
(void)xMBTCPPortMasterAddSlaveIp(0xFF, NULL, 0xFF);
|
||||||
|
|
||||||
status = eMBMasterEnable();
|
|
||||||
MB_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
|
||||||
"mb stack set slave ID failure, eMBMasterEnable() returned (0x%x).", (uint32_t)status);
|
|
||||||
|
|
||||||
|
|
||||||
// Wait for connection done event
|
// Wait for connection done event
|
||||||
bool start = (bool)xMBTCPPortMasterWaitEvent(mbm_opts->mbm_event_group,
|
bool start = (bool)xMBTCPPortMasterWaitEvent(mbm_opts->mbm_event_group,
|
||||||
@@ -189,7 +188,6 @@ static esp_err_t mbc_tcp_master_start(void)
|
|||||||
MB_MASTER_CHECK((start), ESP_ERR_INVALID_STATE,
|
MB_MASTER_CHECK((start), ESP_ERR_INVALID_STATE,
|
||||||
"mb stack could not connect to slaves for %d seconds.",
|
"mb stack could not connect to slaves for %d seconds.",
|
||||||
CONFIG_FMB_TCP_CONNECTION_TOUT_SEC);
|
CONFIG_FMB_TCP_CONNECTION_TOUT_SEC);
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -588,8 +586,7 @@ static esp_err_t mbc_tcp_master_set_parameter(uint16_t cid, char* name, uint8_t*
|
|||||||
* @return result
|
* @return result
|
||||||
*/
|
*/
|
||||||
// Callback function for reading of MB Input Registers
|
// Callback function for reading of MB Input Registers
|
||||||
eMBErrorCode eMBRegInputCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress,
|
eMBErrorCode eMBRegInputCBTcpMaster(UCHAR* pucRegBuffer, USHORT usAddress, USHORT usNRegs)
|
||||||
USHORT usNRegs)
|
|
||||||
{
|
{
|
||||||
MB_MASTER_ASSERT(mbm_interface_ptr != NULL);
|
MB_MASTER_ASSERT(mbm_interface_ptr != NULL);
|
||||||
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts;
|
mb_master_options_t* mbm_opts = &mbm_interface_ptr->opts;
|
||||||
@@ -635,25 +632,29 @@ eMBErrorCode eMBRegHoldingCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress,
|
|||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
USHORT usRegs = usNRegs;
|
USHORT usRegs = usNRegs;
|
||||||
// Check input and configuration parameters for correctness
|
// Check input and configuration parameters for correctness
|
||||||
if ((pucHoldingBuffer != NULL)
|
if ((pucHoldingBuffer != NULL) && (usRegHoldingNregs == usNRegs) && (usNRegs >= 1))
|
||||||
&& (usRegHoldingNregs == usNRegs)
|
{
|
||||||
&& (usNRegs >= 1)) {
|
switch (eMode)
|
||||||
switch (eMode) {
|
{
|
||||||
case MB_REG_WRITE:
|
case MB_REG_WRITE:
|
||||||
while (usRegs > 0) {
|
while (usRegs > 0)
|
||||||
|
{
|
||||||
_XFER_2_RD(pucRegBuffer, pucHoldingBuffer);
|
_XFER_2_RD(pucRegBuffer, pucHoldingBuffer);
|
||||||
usRegs -= 1;
|
usRegs -= 1;
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case MB_REG_READ:
|
case MB_REG_READ:
|
||||||
while (usRegs > 0) {
|
while (usRegs > 0)
|
||||||
|
{
|
||||||
_XFER_2_WR(pucHoldingBuffer, pucRegBuffer);
|
_XFER_2_WR(pucHoldingBuffer, pucRegBuffer);
|
||||||
pucHoldingBuffer += 2;
|
pucHoldingBuffer += 2;
|
||||||
usRegs -= 1;
|
usRegs -= 1;
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
eStatus = MB_ENOREG;
|
eStatus = MB_ENOREG;
|
||||||
}
|
}
|
||||||
return eStatus;
|
return eStatus;
|
||||||
|
@@ -248,11 +248,6 @@ void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortPr
|
|||||||
xMbPortConfig.eMbIpVer = xIpVersion;
|
xMbPortConfig.eMbIpVer = xIpVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vMBTCPPortMasterTaskStart(void)
|
|
||||||
{
|
|
||||||
vTaskResume(xMbPortConfig.xMbTcpTaskHandle);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function returns time left for response processing according to response timeout
|
// Function returns time left for response processing according to response timeout
|
||||||
static int64_t xMBTCPPortMasterGetRespTimeLeft(MbSlaveInfo_t* pxInfo)
|
static int64_t xMBTCPPortMasterGetRespTimeLeft(MbSlaveInfo_t* pxInfo)
|
||||||
{
|
{
|
||||||
@@ -890,23 +885,13 @@ extern void vMBMasterPortEventClose(void);
|
|||||||
extern void vMBMasterPortTimerClose(void);
|
extern void vMBMasterPortTimerClose(void);
|
||||||
|
|
||||||
void
|
void
|
||||||
vMBMasterTCPPortDisable(void)
|
vMBMasterTCPPortEnable(void)
|
||||||
{
|
{
|
||||||
for (USHORT ucCnt = 0; ucCnt < MB_TCP_PORT_MAX_CONN; ucCnt++) {
|
vTaskResume(xMbPortConfig.xMbTcpTaskHandle);
|
||||||
MbSlaveInfo_t* pxInfo = xMbPortConfig.pxMbSlaveInfo[ucCnt];
|
|
||||||
if (pxInfo) {
|
|
||||||
xMBTCPPortMasterCloseConnection(pxInfo);
|
|
||||||
if (pxInfo->pucRcvBuf) {
|
|
||||||
free(pxInfo->pucRcvBuf);
|
|
||||||
}
|
|
||||||
free(pxInfo);
|
|
||||||
xMbPortConfig.pxMbSlaveInfo[ucCnt] = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
vMBMasterTCPPortClose(void)
|
vMBMasterTCPPortDisable(void)
|
||||||
{
|
{
|
||||||
// Try to exit the task gracefully, so select could release its internal callbacks
|
// Try to exit the task gracefully, so select could release its internal callbacks
|
||||||
// that were allocated on the stack of the task we're going to delete
|
// that were allocated on the stack of the task we're going to delete
|
||||||
@@ -920,8 +905,23 @@ vMBMasterTCPPortClose(void)
|
|||||||
vSemaphoreDelete(xShutdownSemaphore);
|
vSemaphoreDelete(xShutdownSemaphore);
|
||||||
xShutdownSemaphore = NULL;
|
xShutdownSemaphore = NULL;
|
||||||
}
|
}
|
||||||
vMBMasterTCPPortDisable();
|
for (USHORT ucCnt = 0; ucCnt < MB_TCP_PORT_MAX_CONN; ucCnt++) {
|
||||||
|
MbSlaveInfo_t* pxInfo = xMbPortConfig.pxMbSlaveInfo[ucCnt];
|
||||||
|
if (pxInfo) {
|
||||||
|
xMBTCPPortMasterCloseConnection(pxInfo);
|
||||||
|
if (pxInfo->pucRcvBuf) {
|
||||||
|
free(pxInfo->pucRcvBuf);
|
||||||
|
}
|
||||||
|
free(pxInfo);
|
||||||
|
xMbPortConfig.pxMbSlaveInfo[ucCnt] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
free(xMbPortConfig.pxMbSlaveInfo);
|
free(xMbPortConfig.pxMbSlaveInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
vMBMasterTCPPortClose(void)
|
||||||
|
{
|
||||||
vQueueDelete(xMbPortConfig.xConnectQueue);
|
vQueueDelete(xMbPortConfig.xConnectQueue);
|
||||||
vMBMasterPortTimerClose();
|
vMBMasterPortTimerClose();
|
||||||
// Release resources for the event queue.
|
// Release resources for the event queue.
|
||||||
|
@@ -132,13 +132,6 @@ BOOL xMBTCPPortMasterWaitEvent(EventGroupHandle_t xEventHandle, EventBits_t xEve
|
|||||||
*/
|
*/
|
||||||
void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto);
|
void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto);
|
||||||
|
|
||||||
/**
|
|
||||||
* Resume TCP/UDP Master processing task
|
|
||||||
*
|
|
||||||
* @return None
|
|
||||||
*/
|
|
||||||
void vMBTCPPortMasterTaskStart(void);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
|
@@ -88,7 +88,6 @@ static esp_err_t mbc_tcp_slave_start(void)
|
|||||||
eMBPortProto proto = (mbs_opts->mbs_comm.ip_mode == MB_MODE_TCP) ? MB_PROTO_TCP : MB_PROTO_UDP;
|
eMBPortProto proto = (mbs_opts->mbs_comm.ip_mode == MB_MODE_TCP) ? MB_PROTO_TCP : MB_PROTO_UDP;
|
||||||
eMBPortIpVer ip_ver = (mbs_opts->mbs_comm.ip_addr_type == MB_IPV4) ? MB_PORT_IPV4 : MB_PORT_IPV6;
|
eMBPortIpVer ip_ver = (mbs_opts->mbs_comm.ip_addr_type == MB_IPV4) ? MB_PORT_IPV4 : MB_PORT_IPV6;
|
||||||
vMBTCPPortSlaveSetNetOpt(mbs_opts->mbs_comm.ip_netif_ptr, ip_ver, proto, (char*)mbs_opts->mbs_comm.ip_addr);
|
vMBTCPPortSlaveSetNetOpt(mbs_opts->mbs_comm.ip_netif_ptr, ip_ver, proto, (char*)mbs_opts->mbs_comm.ip_addr);
|
||||||
vMBTCPPortSlaveStartServerTask();
|
|
||||||
|
|
||||||
status = eMBEnable();
|
status = eMBEnable();
|
||||||
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
|
||||||
|
@@ -176,11 +176,6 @@ void vMBTCPPortSlaveSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortPro
|
|||||||
xConfig.pcBindAddr = pcBindAddrStr;
|
xConfig.pcBindAddr = pcBindAddrStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vMBTCPPortSlaveStartServerTask(void)
|
|
||||||
{
|
|
||||||
vTaskResume(xConfig.xMbTcpTaskHandle);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr)
|
static int xMBTCPPortAcceptConnection(int xListenSockId, char** pcIPAddr)
|
||||||
{
|
{
|
||||||
MB_PORT_CHECK(pcIPAddr, -1, "Wrong IP address pointer.");
|
MB_PORT_CHECK(pcIPAddr, -1, "Wrong IP address pointer.");
|
||||||
@@ -655,6 +650,11 @@ vMBTCPPortClose( )
|
|||||||
vMBPortEventClose( );
|
vMBPortEventClose( );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void vMBTCPPortEnable( void )
|
||||||
|
{
|
||||||
|
vTaskResume(xConfig.xMbTcpTaskHandle);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
vMBTCPPortDisable( void )
|
vMBTCPPortDisable( void )
|
||||||
{
|
{
|
||||||
|
@@ -101,13 +101,6 @@ typedef struct {
|
|||||||
*/
|
*/
|
||||||
void vMBTCPPortSlaveSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto, CHAR* pcBindAddr);
|
void vMBTCPPortSlaveSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto, CHAR* pcBindAddr);
|
||||||
|
|
||||||
/**
|
|
||||||
* Resume TCP Slave processing task
|
|
||||||
*
|
|
||||||
* @return None
|
|
||||||
*/
|
|
||||||
void vMBTCPPortSlaveStartServerTask(void);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
|
@@ -98,7 +98,7 @@ static void start_mdns_service(void)
|
|||||||
ESP_ERROR_CHECK(mdns_init());
|
ESP_ERROR_CHECK(mdns_init());
|
||||||
//set mDNS hostname (required if you want to advertise services)
|
//set mDNS hostname (required if you want to advertise services)
|
||||||
ESP_ERROR_CHECK(mdns_hostname_set(hostname));
|
ESP_ERROR_CHECK(mdns_hostname_set(hostname));
|
||||||
ESP_LOGI(SLAVE_TAG, "mdns hostname set to: [%s]", hostname);
|
ESP_LOGI(TAG, "mdns hostname set to: [%s]", hostname);
|
||||||
|
|
||||||
//set default mDNS instance name
|
//set default mDNS instance name
|
||||||
ESP_ERROR_CHECK(mdns_instance_name_set(MB_MDNS_INSTANCE("esp32_")));
|
ESP_ERROR_CHECK(mdns_instance_name_set(MB_MDNS_INSTANCE("esp32_")));
|
||||||
|
Reference in New Issue
Block a user