mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-30 10:47:19 +02:00
freemodbuss: fix event processing after merge
This commit is contained in:
@ -103,7 +103,7 @@ eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG
|
|||||||
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF ] = usNCoils >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF ] = usNCoils >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils;
|
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils;
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -210,7 +210,7 @@ eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LO
|
|||||||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF ] = usCoilData >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF ] = usCoilData >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData;
|
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData;
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
@ -320,7 +320,7 @@ eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
|
|||||||
*ucMBFrame++ = pucDataBuffer[usRegIndex++];
|
*ucMBFrame++ = pucDataBuffer[usRegIndex++];
|
||||||
}
|
}
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
|
@ -78,16 +78,16 @@ eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT u
|
|||||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr;
|
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn;
|
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn;
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
@ -106,11 +106,11 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||||
if ( xMBMasterRequestIsBroadcast() )
|
if ( xMBMasterRequestIsBroadcast() )
|
||||||
{
|
{
|
||||||
eStatus = MB_EX_NONE;
|
eStatus = MB_EX_NONE;
|
||||||
}
|
}
|
||||||
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
@ -122,26 +122,26 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
* byte is only partially field with unused coils set to zero. */
|
* byte is only partially field with unused coils set to zero. */
|
||||||
if( ( usDiscreteCnt & 0x0007 ) != 0 )
|
if( ( usDiscreteCnt & 0x0007 ) != 0 )
|
||||||
{
|
{
|
||||||
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 );
|
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 );
|
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if the number of registers to read is valid. If not
|
/* Check if the number of registers to read is valid. If not
|
||||||
* return Modbus illegal data value exception.
|
* return Modbus illegal data value exception.
|
||||||
*/
|
*/
|
||||||
if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF])
|
if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF])
|
||||||
{
|
{
|
||||||
/* Make callback to fill the buffer. */
|
/* Make callback to fill the buffer. */
|
||||||
eRegStatus = eMBMasterRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt );
|
eRegStatus = eMBMasterRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt );
|
||||||
|
|
||||||
/* If an error occured convert it into a Modbus exception. */
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
if( eRegStatus != MB_ENOERR )
|
if( eRegStatus != MB_ENOERR )
|
||||||
{
|
{
|
||||||
eStatus = prveMBError2Exception( eRegStatus );
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -106,16 +106,16 @@ eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRe
|
|||||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr;
|
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = usRegData >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = usRegData >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ;
|
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ;
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
@ -167,7 +167,7 @@ eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
*/
|
*/
|
||||||
eMBMasterReqErrCode
|
eMBMasterReqErrCode
|
||||||
eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
||||||
USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut )
|
USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut )
|
||||||
{
|
{
|
||||||
UCHAR *ucMBFrame;
|
UCHAR *ucMBFrame;
|
||||||
USHORT usRegIndex = 0;
|
USHORT usRegIndex = 0;
|
||||||
@ -177,23 +177,23 @@ eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
|||||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ;
|
||||||
ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2;
|
||||||
ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
|
ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
|
||||||
while( usNRegs > usRegIndex)
|
while( usNRegs > usRegIndex)
|
||||||
{
|
{
|
||||||
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
||||||
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
||||||
}
|
}
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
@ -212,7 +212,7 @@ eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
/* If this request is broadcast, the *usLen is not need check. */
|
/* If this request is broadcast, the *usLen is not need check. */
|
||||||
if( ( *usLen == MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() )
|
if( ( *usLen == MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() )
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] << 8 );
|
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] );
|
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
@ -270,16 +270,16 @@ eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRe
|
|||||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
|
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
@ -297,11 +297,11 @@ eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||||
if ( xMBMasterRequestIsBroadcast() )
|
if ( xMBMasterRequestIsBroadcast() )
|
||||||
{
|
{
|
||||||
eStatus = MB_EX_NONE;
|
eStatus = MB_EX_NONE;
|
||||||
}
|
}
|
||||||
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
@ -354,8 +354,8 @@ eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
*/
|
*/
|
||||||
eMBMasterReqErrCode
|
eMBMasterReqErrCode
|
||||||
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
||||||
USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
|
USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
|
||||||
USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut )
|
USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut )
|
||||||
{
|
{
|
||||||
UCHAR *ucMBFrame;
|
UCHAR *ucMBFrame;
|
||||||
USHORT usRegIndex = 0;
|
USHORT usRegIndex = 0;
|
||||||
@ -365,27 +365,27 @@ eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
|||||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr;
|
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ;
|
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ;
|
||||||
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2;
|
||||||
ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF;
|
ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF;
|
||||||
while( usNWriteRegs > usRegIndex)
|
while( usNWriteRegs > usRegIndex)
|
||||||
{
|
{
|
||||||
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
||||||
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
||||||
}
|
}
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
@ -405,11 +405,11 @@ eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen
|
|||||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||||
if ( xMBMasterRequestIsBroadcast() )
|
if ( xMBMasterRequestIsBroadcast() )
|
||||||
{
|
{
|
||||||
eStatus = MB_EX_NONE;
|
eStatus = MB_EX_NONE;
|
||||||
}
|
}
|
||||||
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN )
|
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN )
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U );
|
usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U );
|
||||||
usRegReadAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] );
|
usRegReadAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] );
|
||||||
usRegReadAddress++;
|
usRegReadAddress++;
|
||||||
@ -433,8 +433,8 @@ eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen
|
|||||||
if( eRegStatus == MB_ENOERR )
|
if( eRegStatus == MB_ENOERR )
|
||||||
{
|
{
|
||||||
/* Make the read callback. */
|
/* Make the read callback. */
|
||||||
eRegStatus = eMBMasterRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF],
|
eRegStatus = eMBMasterRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF],
|
||||||
usRegReadAddress, usRegReadCount, MB_REG_READ);
|
usRegReadAddress, usRegReadCount, MB_REG_READ);
|
||||||
}
|
}
|
||||||
if( eRegStatus != MB_ENOERR )
|
if( eRegStatus != MB_ENOERR )
|
||||||
{
|
{
|
||||||
|
@ -78,16 +78,16 @@ eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs
|
|||||||
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
|
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
|
||||||
eErrStatus = eMBMasterWaitRequestFinish( );
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
@ -103,13 +103,13 @@ eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
|
|||||||
eMBErrorCode eRegStatus;
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
/* If this request is broadcast, and it's read mode. This request don't need execute. */
|
||||||
if ( xMBMasterRequestIsBroadcast() )
|
if ( xMBMasterRequestIsBroadcast() )
|
||||||
{
|
|
||||||
eStatus = MB_EX_NONE;
|
|
||||||
}
|
|
||||||
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
eStatus = MB_EX_NONE;
|
||||||
|
}
|
||||||
|
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
|
||||||
|
{
|
||||||
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
||||||
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
||||||
usRegAddress++;
|
usRegAddress++;
|
||||||
|
@ -47,14 +47,14 @@ typedef enum
|
|||||||
EV_FRAME_SENT = 0x08 /*!< Frame sent. */
|
EV_FRAME_SENT = 0x08 /*!< Frame sent. */
|
||||||
} eMBEventType;
|
} eMBEventType;
|
||||||
|
|
||||||
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
|
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
|
||||||
typedef enum {
|
typedef enum {
|
||||||
EV_MASTER_NO_EVENT = 0x0000,
|
EV_MASTER_NO_EVENT = 0x0000,
|
||||||
EV_MASTER_READY = 0x0001, /*!< Startup finished. */
|
EV_MASTER_READY = 0x0001, /*!< Startup finished. */
|
||||||
EV_MASTER_FRAME_RECEIVED = 0x0002, /*!< Frame received. */
|
EV_MASTER_FRAME_RECEIVED = 0x0002, /*!< Frame received. */
|
||||||
EV_MASTER_EXECUTE = 0x0004, /*!< Execute function. */
|
EV_MASTER_EXECUTE = 0x0004, /*!< Execute function. */
|
||||||
EV_MASTER_FRAME_SENT = 0x0008, /*!< Frame sent. */
|
EV_MASTER_FRAME_SENT = 0x0008, /*!< Frame sent. */
|
||||||
EV_MASTER_FRAME_TRANSMITTED = 0x0010, /*!< Request execute function error. */
|
EV_MASTER_FRAME_TRANSMIT = 0x0010, /*!< Frame transmission. */
|
||||||
EV_MASTER_ERROR_PROCESS = 0x0020, /*!< Frame error process. */
|
EV_MASTER_ERROR_PROCESS = 0x0020, /*!< Frame error process. */
|
||||||
EV_MASTER_PROCESS_SUCCESS = 0x0040, /*!< Request process success. */
|
EV_MASTER_PROCESS_SUCCESS = 0x0040, /*!< Request process success. */
|
||||||
EV_MASTER_ERROR_RESPOND_TIMEOUT = 0x0080, /*!< Request respond timeout. */
|
EV_MASTER_ERROR_RESPOND_TIMEOUT = 0x0080, /*!< Request respond timeout. */
|
||||||
@ -63,9 +63,11 @@ typedef enum {
|
|||||||
} eMBMasterEventType;
|
} eMBMasterEventType;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
EV_ERROR_INIT, /*!< No error, initial state. */
|
||||||
EV_ERROR_RESPOND_TIMEOUT, /*!< Slave respond timeout. */
|
EV_ERROR_RESPOND_TIMEOUT, /*!< Slave respond timeout. */
|
||||||
EV_ERROR_RECEIVE_DATA, /*!< Receive frame data erroe. */
|
EV_ERROR_RECEIVE_DATA, /*!< Receive frame data error. */
|
||||||
EV_ERROR_EXECUTE_FUNCTION, /*!< Execute function error. */
|
EV_ERROR_EXECUTE_FUNCTION, /*!< Execute function error. */
|
||||||
|
EV_ERROR_OK /*!< No error, processing completed. */
|
||||||
} eMBMasterErrorEventType;
|
} eMBMasterErrorEventType;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -156,7 +158,7 @@ void vMBMasterPortTimersConvertDelayEnable( void );
|
|||||||
void vMBMasterPortTimersRespondTimeoutEnable( void );
|
void vMBMasterPortTimersRespondTimeoutEnable( void );
|
||||||
|
|
||||||
void vMBMasterPortTimersDisable( void );
|
void vMBMasterPortTimersDisable( void );
|
||||||
#endif
|
|
||||||
|
|
||||||
/* ----------------- Callback for the master error process ------------------*/
|
/* ----------------- Callback for the master error process ------------------*/
|
||||||
void vMBMasterErrorCBRespondTimeout( UCHAR ucDestAddress, const UCHAR* pucPDUData,
|
void vMBMasterErrorCBRespondTimeout( UCHAR ucDestAddress, const UCHAR* pucPDUData,
|
||||||
@ -169,7 +171,7 @@ void vMBMasterErrorCBExecuteFunction( UCHAR ucDestAddress, const UCHA
|
|||||||
USHORT ucPDULength );
|
USHORT ucPDULength );
|
||||||
|
|
||||||
void vMBMasterCBRequestSuccess( void );
|
void vMBMasterCBRequestSuccess( void );
|
||||||
|
#endif
|
||||||
/* ----------------------- Callback for the protocol stack ------------------*/
|
/* ----------------------- Callback for the protocol stack ------------------*/
|
||||||
/*!
|
/*!
|
||||||
* \brief Callback function for the porting layer when a new byte is
|
* \brief Callback function for the porting layer when a new byte is
|
||||||
@ -206,8 +208,8 @@ void vMBTCPPortDisable( void );
|
|||||||
BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
||||||
|
|
||||||
BOOL xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
|
BOOL xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#endif
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
|
@ -257,13 +257,13 @@ eMBMasterDisable( void )
|
|||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBMasterPoll( void )
|
eMBMasterPoll( void )
|
||||||
{
|
{
|
||||||
static UCHAR *ucMBFrame;
|
static UCHAR *ucMBFrame = NULL;
|
||||||
static UCHAR ucRcvAddress;
|
static UCHAR ucRcvAddress;
|
||||||
static UCHAR ucFunctionCode;
|
static UCHAR ucFunctionCode;
|
||||||
static USHORT usLength;
|
static USHORT usLength;
|
||||||
static eMBException eException;
|
static eMBException eException;
|
||||||
|
int i;
|
||||||
int i , j;
|
int j;
|
||||||
eMBErrorCode eStatus = MB_ENOERR;
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
eMBMasterEventType eEvent;
|
eMBMasterEventType eEvent;
|
||||||
eMBMasterErrorEventType errorType;
|
eMBMasterErrorEventType errorType;
|
||||||
@ -278,133 +278,141 @@ eMBMasterPoll( void )
|
|||||||
* Otherwise we will handle the event. */
|
* Otherwise we will handle the event. */
|
||||||
if( xMBMasterPortEventGet( &eEvent ) == TRUE )
|
if( xMBMasterPortEventGet( &eEvent ) == TRUE )
|
||||||
{
|
{
|
||||||
switch ( eEvent )
|
while( eEvent ) {
|
||||||
{
|
// In some cases it is possible that more than one event set
|
||||||
case EV_MASTER_NO_EVENT:
|
// together (even from one subset mask) than process them consistently
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_NO_EVENT", __func__);
|
if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_READY ) ) {
|
||||||
// Something went wrong and task unblocked but there are no any events set
|
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_READY", __func__);
|
||||||
assert(0);
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_READY );
|
||||||
break;
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_FRAME_TRANSMIT ) ) {
|
||||||
case EV_MASTER_PROCESS_SUCCESS:
|
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_TRANSMIT", __func__);
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_PROCESS_SUCCESS", __func__);
|
/* Master is busy now. */
|
||||||
break;
|
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
||||||
case EV_MASTER_ERROR_RESPOND_TIMEOUT:
|
ESP_LOG_BUFFER_HEX_LEVEL("POLL transmit buffer", (void*)ucMBFrame, usMBMasterGetPDUSndLength(), ESP_LOG_DEBUG);
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_ERROR_RESPOND_TIMEOUT", __func__);
|
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
|
||||||
break;
|
if (eStatus != MB_ENOERR)
|
||||||
case EV_MASTER_ERROR_RECEIVE_DATA:
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_ERROR_RECEIVE_DATA", __func__);
|
|
||||||
break;
|
|
||||||
case EV_MASTER_ERROR_EXECUTE_FUNCTION:
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_ERROR_EXECUTE_FUNCTION", __func__);
|
|
||||||
break;
|
|
||||||
case EV_MASTER_READY:
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_READY", __func__);
|
|
||||||
break;
|
|
||||||
case EV_MASTER_FRAME_RECEIVED:
|
|
||||||
eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
|
|
||||||
// Check if the frame is for us. If not ,send an error process event.
|
|
||||||
if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
|
|
||||||
{
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, eStatus);
|
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).", __func__, ucRcvAddress, eStatus);
|
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case EV_MASTER_EXECUTE:
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_EXECUTE", __func__);
|
|
||||||
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
|
|
||||||
eException = MB_EX_ILLEGAL_FUNCTION;
|
|
||||||
/* If receive frame has exception. The receive function code highest bit is 1.*/
|
|
||||||
if(ucFunctionCode >> 7) {
|
|
||||||
eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++)
|
|
||||||
{
|
{
|
||||||
/* No more function handlers registered. Abort. */
|
ESP_LOGE( MB_PORT_TAG, "%s:Frame send error. %d", __func__, eStatus );
|
||||||
if (xMasterFuncHandlers[i].ucFunctionCode == 0) {
|
}
|
||||||
break;
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_FRAME_TRANSMIT );
|
||||||
}
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_FRAME_SENT ) ) {
|
||||||
else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
|
ESP_LOGD( MB_PORT_TAG, "%s:EV_MASTER_FRAME_SENT", __func__ );
|
||||||
vMBMasterSetCBRunInMasterMode(TRUE);
|
ESP_LOG_BUFFER_HEX_LEVEL("POLL sent buffer", (void*)ucMBFrame, usMBMasterGetPDUSndLength(), ESP_LOG_DEBUG);
|
||||||
/* If master request is broadcast,
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_FRAME_SENT );
|
||||||
* the master need execute function for all slave.
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_FRAME_RECEIVED ) ) {
|
||||||
*/
|
eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength);
|
||||||
if ( xMBMasterRequestIsBroadcast() ) {
|
|
||||||
usLength = usMBMasterGetPDUSndLength();
|
// Check if the frame is for us. If not ,send an error process event.
|
||||||
for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){
|
if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
|
||||||
vMBMasterSetDestAddress(j);
|
{
|
||||||
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
|
( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
|
||||||
|
ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, eStatus);
|
||||||
|
ESP_LOG_BUFFER_HEX_LEVEL("POLL receive buffer", (void*)ucMBFrame, (uint16_t)usLength, ESP_LOG_DEBUG);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||||
|
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||||
|
ESP_LOGD( MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).",
|
||||||
|
__func__, ucRcvAddress, eStatus);
|
||||||
|
}
|
||||||
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_FRAME_RECEIVED );
|
||||||
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_EXECUTE ) ) {
|
||||||
|
if ( !ucMBFrame )
|
||||||
|
{
|
||||||
|
return MB_EILLSTATE;
|
||||||
|
}
|
||||||
|
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_EXECUTE", __func__);
|
||||||
|
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
|
||||||
|
eException = MB_EX_ILLEGAL_FUNCTION;
|
||||||
|
/* If receive frame has exception. The receive function code highest bit is 1.*/
|
||||||
|
if (ucFunctionCode & MB_FUNC_ERROR)
|
||||||
|
{
|
||||||
|
eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
|
||||||
|
} else {
|
||||||
|
for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
||||||
|
{
|
||||||
|
/* No more function handlers registered. Abort. */
|
||||||
|
if (xMasterFuncHandlers[i].ucFunctionCode == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode)
|
||||||
|
{
|
||||||
|
vMBMasterSetCBRunInMasterMode(TRUE);
|
||||||
|
/* If master request is broadcast,
|
||||||
|
* the master need execute function for all slave.
|
||||||
|
*/
|
||||||
|
if ( xMBMasterRequestIsBroadcast() )
|
||||||
|
{
|
||||||
|
usLength = usMBMasterGetPDUSndLength();
|
||||||
|
for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++)
|
||||||
|
{
|
||||||
|
vMBMasterSetDestAddress(j);
|
||||||
|
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eException = xMasterFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
|
||||||
|
}
|
||||||
|
vMBMasterSetCBRunInMasterMode( FALSE );
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
|
|
||||||
}
|
|
||||||
vMBMasterSetCBRunInMasterMode(FALSE);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
/* If master has exception, will send error process event. Otherwise the master is idle.*/
|
||||||
/* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
|
if ( eException != MB_EX_NONE )
|
||||||
if (eException != MB_EX_NONE) {
|
{
|
||||||
vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
|
vMBMasterSetErrorType( EV_ERROR_EXECUTE_FUNCTION );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
vMBMasterCBRequestSuccess( );
|
{
|
||||||
|
if ( eMBMasterGetErrorType( ) == EV_ERROR_INIT ) {
|
||||||
|
vMBMasterSetErrorType(EV_ERROR_OK);
|
||||||
|
ESP_LOGD( MB_PORT_TAG, "%s: set event EV_ERROR_OK", __func__ );
|
||||||
|
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_EXECUTE );
|
||||||
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_ERROR_PROCESS ) ) {
|
||||||
|
ESP_LOGD( MB_PORT_TAG, "%s:EV_MASTER_ERROR_PROCESS", __func__ );
|
||||||
|
/* Execute specified error process callback function. */
|
||||||
|
errorType = eMBMasterGetErrorType( );
|
||||||
|
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
||||||
|
switch ( errorType )
|
||||||
|
{
|
||||||
|
case EV_ERROR_RESPOND_TIMEOUT:
|
||||||
|
vMBMasterErrorCBRespondTimeout( ucMBMasterGetDestAddress( ),
|
||||||
|
ucMBFrame, usMBMasterGetPDUSndLength( ) );
|
||||||
|
break;
|
||||||
|
case EV_ERROR_RECEIVE_DATA:
|
||||||
|
vMBMasterErrorCBReceiveData( ucMBMasterGetDestAddress( ),
|
||||||
|
ucMBFrame, usMBMasterGetPDUSndLength( ) );
|
||||||
|
break;
|
||||||
|
case EV_ERROR_EXECUTE_FUNCTION:
|
||||||
|
vMBMasterErrorCBExecuteFunction( ucMBMasterGetDestAddress( ),
|
||||||
|
ucMBFrame, usMBMasterGetPDUSndLength( ) );
|
||||||
|
break;
|
||||||
|
case EV_ERROR_OK:
|
||||||
|
vMBMasterCBRequestSuccess( );
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGE( MB_PORT_TAG, "%s: incorrect error type = %d.", __func__, errorType);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
vMBMasterSetErrorType( EV_ERROR_INIT );
|
||||||
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_ERROR_PROCESS );
|
||||||
vMBMasterRunResRelease( );
|
vMBMasterRunResRelease( );
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case EV_MASTER_FRAME_SENT:
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_SENT", __func__);
|
|
||||||
/* Master is busy now. */
|
|
||||||
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
|
||||||
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
|
|
||||||
if (eStatus != MB_ENOERR)
|
|
||||||
{
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:Frame send error. %d", __func__, eStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
case EV_MASTER_FRAME_TRANSMITTED:
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_TRANSMITTED", __func__);
|
|
||||||
break;
|
|
||||||
case EV_MASTER_ERROR_PROCESS:
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_ERROR_PROCESS", __func__);
|
|
||||||
/* Execute specified error process callback function. */
|
|
||||||
errorType = eMBMasterGetErrorType();
|
|
||||||
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
|
||||||
switch (errorType) {
|
|
||||||
case EV_ERROR_RESPOND_TIMEOUT:
|
|
||||||
vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(),
|
|
||||||
ucMBFrame, usMBMasterGetPDUSndLength());
|
|
||||||
break;
|
|
||||||
case EV_ERROR_RECEIVE_DATA:
|
|
||||||
vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(),
|
|
||||||
ucMBFrame, usMBMasterGetPDUSndLength());
|
|
||||||
break;
|
|
||||||
case EV_ERROR_EXECUTE_FUNCTION:
|
|
||||||
vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(),
|
|
||||||
ucMBFrame, usMBMasterGetPDUSndLength());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s: incorrect error type.", __func__);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
vMBMasterRunResRelease();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// xMBMasterPortEventGet has unbloked the task but the event bits are not set
|
// Something went wrong and task unblocked but there are no any correct events set
|
||||||
ESP_LOGD(MB_PORT_TAG, "%s: task event wait failure.", __func__);
|
ESP_LOGE( MB_PORT_TAG, "%s: Unexpected event triggered 0x%02x.", __func__, eEvent );
|
||||||
|
eStatus = MB_EILLSTATE;
|
||||||
}
|
}
|
||||||
return MB_ENOERR;
|
return eStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get whether the Modbus Master is run in master mode.
|
// Get whether the Modbus Master is run in master mode.
|
||||||
|
@ -342,8 +342,7 @@ xMBRTUTimerT35Expired( void )
|
|||||||
|
|
||||||
/* Function called in an illegal state. */
|
/* Function called in an illegal state. */
|
||||||
default:
|
default:
|
||||||
assert( ( eRcvState == STATE_RX_INIT ) ||
|
assert( ( eRcvState == STATE_RX_IDLE ) || ( eRcvState == STATE_RX_ERROR ) );
|
||||||
( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_ERROR ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vMBPortTimersDisable( );
|
vMBPortTimersDisable( );
|
||||||
|
@ -264,11 +264,11 @@ xMBMasterRTUReceiveFSM( void )
|
|||||||
* the timer of respond timeout .
|
* the timer of respond timeout .
|
||||||
*/
|
*/
|
||||||
case STATE_M_RX_IDLE:
|
case STATE_M_RX_IDLE:
|
||||||
/* In time of respond timeout,the receiver receive a frame.
|
/* In time of respond timeout,the receiver receive a frame.
|
||||||
* Disable timer of respond timeout and change the transmiter state to idle.
|
* Disable timer of respond timeout and change the transmiter state to idle.
|
||||||
*/
|
*/
|
||||||
vMBMasterPortTimersDisable( );
|
vMBMasterPortTimersDisable( );
|
||||||
eSndState = STATE_M_TX_IDLE;
|
eSndState = STATE_M_TX_IDLE;
|
||||||
|
|
||||||
usMasterRcvBufferPos = 0;
|
usMasterRcvBufferPos = 0;
|
||||||
ucMasterRTURcvBuf[usMasterRcvBufferPos++] = ucByte;
|
ucMasterRTURcvBuf[usMasterRcvBufferPos++] = ucByte;
|
||||||
@ -336,11 +336,11 @@ xMBMasterRTUTransmitFSM( void )
|
|||||||
* else master will enable timer of respond timeout. */
|
* else master will enable timer of respond timeout. */
|
||||||
if ( xFrameIsBroadcast == TRUE )
|
if ( xFrameIsBroadcast == TRUE )
|
||||||
{
|
{
|
||||||
vMBMasterPortTimersConvertDelayEnable( );
|
vMBMasterPortTimersConvertDelayEnable( );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vMBMasterPortTimersRespondTimeoutEnable( );
|
vMBMasterPortTimersRespondTimeoutEnable( );
|
||||||
}
|
}
|
||||||
xNeedPoll = TRUE;
|
xNeedPoll = TRUE;
|
||||||
}
|
}
|
||||||
@ -353,96 +353,95 @@ xMBMasterRTUTransmitFSM( void )
|
|||||||
BOOL
|
BOOL
|
||||||
xMBMasterRTUTimerExpired(void)
|
xMBMasterRTUTimerExpired(void)
|
||||||
{
|
{
|
||||||
BOOL xNeedPoll = FALSE;
|
BOOL xNeedPoll = FALSE;
|
||||||
|
|
||||||
switch (eRcvState)
|
switch (eRcvState)
|
||||||
{
|
{
|
||||||
/* Timer t35 expired. Startup phase is finished. */
|
/* Timer t35 expired. Startup phase is finished. */
|
||||||
case STATE_M_RX_INIT:
|
case STATE_M_RX_INIT:
|
||||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_READY);
|
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_READY);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* A frame was received and t35 expired. Notify the listener that
|
/* A frame was received and t35 expired. Notify the listener that
|
||||||
* a new frame was received. */
|
* a new frame was received. */
|
||||||
case STATE_M_RX_RCV:
|
case STATE_M_RX_RCV:
|
||||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_FRAME_RECEIVED);
|
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_FRAME_RECEIVED);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* An error occured while receiving the frame. */
|
/* An error occured while receiving the frame. */
|
||||||
case STATE_M_RX_ERROR:
|
case STATE_M_RX_ERROR:
|
||||||
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
||||||
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Function called in an illegal state. */
|
/* Function called in an illegal state. */
|
||||||
default:
|
default:
|
||||||
assert((eRcvState == STATE_M_RX_INIT) || (eRcvState == STATE_M_RX_RCV)
|
assert((eRcvState == STATE_M_RX_INIT) || (eRcvState == STATE_M_RX_RCV)
|
||||||
|| (eRcvState == STATE_M_RX_ERROR) || (eRcvState == STATE_M_RX_IDLE));
|
|| (eRcvState == STATE_M_RX_ERROR) || (eRcvState == STATE_M_RX_IDLE));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
eRcvState = STATE_M_RX_IDLE;
|
eRcvState = STATE_M_RX_IDLE;
|
||||||
|
|
||||||
switch (eSndState)
|
switch (eSndState)
|
||||||
{
|
{
|
||||||
/* A frame was send finish and convert delay or respond timeout expired.
|
/* A frame was send finish and convert delay or respond timeout expired.
|
||||||
* If the frame is broadcast,The master will idle,and if the frame is not
|
* If the frame is broadcast,The master will idle,and if the frame is not
|
||||||
* broadcast.Notify the listener process error.*/
|
* broadcast.Notify the listener process error.*/
|
||||||
case STATE_M_TX_XFWR:
|
case STATE_M_TX_XFWR:
|
||||||
if ( xFrameIsBroadcast == FALSE ) {
|
if ( xFrameIsBroadcast == FALSE ) {
|
||||||
vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT);
|
vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT);
|
||||||
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
|
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/* Function called in an illegal state. */
|
/* Function called in an illegal state. */
|
||||||
default:
|
default:
|
||||||
assert(
|
assert( ( eSndState == STATE_M_TX_XMIT ) || ( eSndState == STATE_M_TX_IDLE ));
|
||||||
( eSndState == STATE_M_TX_XFWR ) || ( eSndState == STATE_M_TX_IDLE ));
|
break;
|
||||||
break;
|
}
|
||||||
}
|
eSndState = STATE_M_TX_IDLE;
|
||||||
eSndState = STATE_M_TX_IDLE;
|
|
||||||
|
|
||||||
vMBMasterPortTimersDisable( );
|
vMBMasterPortTimersDisable( );
|
||||||
/* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */
|
/* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */
|
||||||
if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) {
|
if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) {
|
||||||
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE );
|
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE );
|
||||||
}
|
}
|
||||||
|
|
||||||
return xNeedPoll;
|
return xNeedPoll;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get Modbus Master send RTU's buffer address pointer.*/
|
/* Get Modbus Master send RTU's buffer address pointer.*/
|
||||||
void vMBMasterGetRTUSndBuf( UCHAR ** pucFrame )
|
void vMBMasterGetRTUSndBuf( UCHAR ** pucFrame )
|
||||||
{
|
{
|
||||||
*pucFrame = ( UCHAR * ) ucMasterRTUSndBuf;
|
*pucFrame = ( UCHAR * ) ucMasterRTUSndBuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get Modbus Master send PDU's buffer address pointer.*/
|
/* Get Modbus Master send PDU's buffer address pointer.*/
|
||||||
void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame )
|
void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame )
|
||||||
{
|
{
|
||||||
*pucFrame = ( UCHAR * ) &ucMasterRTUSndBuf[MB_SER_PDU_PDU_OFF];
|
*pucFrame = ( UCHAR * ) &ucMasterRTUSndBuf[MB_SER_PDU_PDU_OFF];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set Modbus Master send PDU's buffer length.*/
|
/* Set Modbus Master send PDU's buffer length.*/
|
||||||
void vMBMasterSetPDUSndLength( USHORT SendPDULength )
|
void vMBMasterSetPDUSndLength( USHORT SendPDULength )
|
||||||
{
|
{
|
||||||
usMasterSendPDULength = SendPDULength;
|
usMasterSendPDULength = SendPDULength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get Modbus Master send PDU's buffer length.*/
|
/* Get Modbus Master send PDU's buffer length.*/
|
||||||
USHORT usMBMasterGetPDUSndLength( void )
|
USHORT usMBMasterGetPDUSndLength( void )
|
||||||
{
|
{
|
||||||
return usMasterSendPDULength;
|
return usMasterSendPDULength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set Modbus Master current timer mode.*/
|
/* Set Modbus Master current timer mode.*/
|
||||||
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode )
|
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode )
|
||||||
{
|
{
|
||||||
eMasterCurTimerMode = eMBTimerMode;
|
eMasterCurTimerMode = eMBTimerMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The master request is broadcast? */
|
/* The master request is broadcast? */
|
||||||
BOOL xMBMasterRequestIsBroadcast( void ){
|
BOOL xMBMasterRequestIsBroadcast( void ){
|
||||||
return xFrameIsBroadcast;
|
return xFrameIsBroadcast;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
#define MB_PORT_CHECK(a, ret_val, str, ...) \
|
#define MB_PORT_CHECK(a, ret_val, str, ...) \
|
||||||
if (!(a)) { \
|
if (!(a)) { \
|
||||||
ESP_LOGE(MB_PORT_TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
ESP_LOGE(MB_PORT_TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||||
return (ret_val); \
|
return ret_val; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@ -68,6 +68,9 @@ void vMBPortExitCritical( );
|
|||||||
#define MB_PORT_PARITY_GET(parity) ((parity != UART_PARITY_DISABLE) ? \
|
#define MB_PORT_PARITY_GET(parity) ((parity != UART_PARITY_DISABLE) ? \
|
||||||
((parity == UART_PARITY_ODD) ? MB_PAR_ODD : MB_PAR_EVEN) : MB_PAR_NONE)
|
((parity == UART_PARITY_ODD) ? MB_PAR_ODD : MB_PAR_EVEN) : MB_PAR_NONE)
|
||||||
|
|
||||||
|
#define MB_PORT_CHECK_EVENT( event, mask ) ( event & mask )
|
||||||
|
#define MB_PORT_CLEAR_EVENT( event, mask ) do { event &= ~mask; } while(0)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
@ -53,7 +53,7 @@
|
|||||||
EV_MASTER_FRAME_RECEIVED | \
|
EV_MASTER_FRAME_RECEIVED | \
|
||||||
EV_MASTER_EXECUTE | \
|
EV_MASTER_EXECUTE | \
|
||||||
EV_MASTER_FRAME_SENT | \
|
EV_MASTER_FRAME_SENT | \
|
||||||
EV_MASTER_FRAME_TRANSMITTED | \
|
EV_MASTER_FRAME_TRANSMIT | \
|
||||||
EV_MASTER_ERROR_PROCESS )
|
EV_MASTER_ERROR_PROCESS )
|
||||||
|
|
||||||
// Event bit mask for eMBMasterWaitRequestFinish()
|
// Event bit mask for eMBMasterWaitRequestFinish()
|
||||||
@ -62,7 +62,6 @@
|
|||||||
EV_MASTER_ERROR_RECEIVE_DATA | \
|
EV_MASTER_ERROR_RECEIVE_DATA | \
|
||||||
EV_MASTER_ERROR_EXECUTE_FUNCTION )
|
EV_MASTER_ERROR_EXECUTE_FUNCTION )
|
||||||
|
|
||||||
#define MB_CHECK_EVENT(event, mask) (event & mask)
|
|
||||||
|
|
||||||
/* ----------------------- Variables ----------------------------------------*/
|
/* ----------------------- Variables ----------------------------------------*/
|
||||||
static SemaphoreHandle_t xSemaphorMasterHdl;
|
static SemaphoreHandle_t xSemaphorMasterHdl;
|
||||||
@ -123,14 +122,15 @@ xMBMasterPortEventGet( eMBMasterEventType * eEvent)
|
|||||||
MB_EVENT_POLL_MASK, // The bits within the event group to wait for.
|
MB_EVENT_POLL_MASK, // The bits within the event group to wait for.
|
||||||
pdTRUE, // Masked bits should be cleared before returning.
|
pdTRUE, // Masked bits should be cleared before returning.
|
||||||
pdFALSE, // Don't wait for both bits, either bit will do.
|
pdFALSE, // Don't wait for both bits, either bit will do.
|
||||||
portMAX_DELAY); // Wait forever for either bit to be set.
|
portMAX_DELAY); // Wait forever for either bit to be set.
|
||||||
|
|
||||||
// Check if poll event is correct
|
// Check if poll event is correct
|
||||||
if (uxBits & MB_EVENT_POLL_MASK) {
|
if (MB_PORT_CHECK_EVENT(uxBits, MB_EVENT_POLL_MASK)) {
|
||||||
*eEvent = (eMBMasterEventType)(uxBits);
|
*eEvent = (eMBMasterEventType)(uxBits & MB_EVENT_POLL_MASK);
|
||||||
xEventHappened = TRUE;
|
xEventHappened = TRUE;
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event triggered.", __func__);
|
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event triggered = %d.", __func__, uxBits);
|
||||||
|
*eEvent = (eMBMasterEventType)uxBits;
|
||||||
xEventHappened = FALSE;
|
xEventHappened = FALSE;
|
||||||
}
|
}
|
||||||
return xEventHappened;
|
return xEventHappened;
|
||||||
@ -140,10 +140,7 @@ xMBMasterPortEventGet( eMBMasterEventType * eEvent)
|
|||||||
void vMBMasterOsResInit( void )
|
void vMBMasterOsResInit( void )
|
||||||
{
|
{
|
||||||
xSemaphorMasterHdl = xSemaphoreCreateBinary();
|
xSemaphorMasterHdl = xSemaphoreCreateBinary();
|
||||||
if (xSemaphorMasterHdl == NULL)
|
MB_PORT_CHECK((xSemaphorMasterHdl != NULL), ; , "%s: OS semaphore create error.", __func__);
|
||||||
{
|
|
||||||
ESP_LOGE(MB_PORT_TAG,"%s: OS semaphore create error.", __func__);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,15 +153,13 @@ void vMBMasterOsResInit( void )
|
|||||||
*/
|
*/
|
||||||
BOOL xMBMasterRunResTake( LONG lTimeOut )
|
BOOL xMBMasterRunResTake( LONG lTimeOut )
|
||||||
{
|
{
|
||||||
BOOL xResult = FALSE;
|
|
||||||
BaseType_t xStatus = pdTRUE;
|
BaseType_t xStatus = pdTRUE;
|
||||||
|
|
||||||
// If waiting time is -1. It will wait forever
|
// If waiting time is -1. It will wait forever
|
||||||
xStatus = xSemaphoreTake(xSemaphorMasterHdl, lTimeOut );
|
xStatus = xSemaphoreTake(xSemaphorMasterHdl, lTimeOut );
|
||||||
if (xStatus == pdTRUE) {
|
MB_PORT_CHECK((xStatus == pdTRUE), FALSE , "%s:Take resource failure.", __func__);
|
||||||
xResult = TRUE;
|
ESP_LOGV(MB_PORT_TAG,"%s:Take resource (%lu ticks).", __func__, lTimeOut);
|
||||||
}
|
return TRUE;
|
||||||
return xResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -173,11 +168,9 @@ BOOL xMBMasterRunResTake( LONG lTimeOut )
|
|||||||
*/
|
*/
|
||||||
void vMBMasterRunResRelease( void )
|
void vMBMasterRunResRelease( void )
|
||||||
{
|
{
|
||||||
BaseType_t xStatus = pdTRUE;
|
BaseType_t xStatus = pdFALSE;
|
||||||
xStatus = xSemaphoreGive(xSemaphorMasterHdl);
|
xStatus = xSemaphoreGive(xSemaphorMasterHdl);
|
||||||
if (xStatus != pdTRUE) {
|
MB_PORT_CHECK((xStatus == pdTRUE), ; , "%s: resource release failure.", __func__);
|
||||||
ESP_LOGE(MB_PORT_TAG,"%s: resource release failure.", __func__);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -192,9 +185,7 @@ void vMBMasterRunResRelease( void )
|
|||||||
void vMBMasterErrorCBRespondTimeout(UCHAR ucDestAddress, const UCHAR* pucPDUData, USHORT ucPDULength)
|
void vMBMasterErrorCBRespondTimeout(UCHAR ucDestAddress, const UCHAR* pucPDUData, USHORT ucPDULength)
|
||||||
{
|
{
|
||||||
BOOL ret = xMBMasterPortEventPost(EV_MASTER_ERROR_RESPOND_TIMEOUT);
|
BOOL ret = xMBMasterPortEventPost(EV_MASTER_ERROR_RESPOND_TIMEOUT);
|
||||||
if(ret != TRUE) {
|
MB_PORT_CHECK((ret == TRUE), ; , "%s: Post event 'EV_MASTER_ERROR_RESPOND_TIMEOUT' failed!", __func__);
|
||||||
ESP_LOGE(MB_PORT_TAG, "Post event 'EV_MASTER_ERROR_RESPOND_TIMEOUT' failed!!!");
|
|
||||||
}
|
|
||||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback respond timeout.", __func__);
|
ESP_LOGD(MB_PORT_TAG,"%s:Callback respond timeout.", __func__);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,10 +200,9 @@ void vMBMasterErrorCBRespondTimeout(UCHAR ucDestAddress, const UCHAR* pucPDUData
|
|||||||
void vMBMasterErrorCBReceiveData(UCHAR ucDestAddress, const UCHAR* pucPDUData, USHORT ucPDULength)
|
void vMBMasterErrorCBReceiveData(UCHAR ucDestAddress, const UCHAR* pucPDUData, USHORT ucPDULength)
|
||||||
{
|
{
|
||||||
BOOL ret = xMBMasterPortEventPost(EV_MASTER_ERROR_RECEIVE_DATA);
|
BOOL ret = xMBMasterPortEventPost(EV_MASTER_ERROR_RECEIVE_DATA);
|
||||||
if(ret != TRUE) {
|
MB_PORT_CHECK((ret == TRUE), ; , "%s: Post event 'EV_MASTER_ERROR_RECEIVE_DATA' failed!", __func__);
|
||||||
ESP_LOGE(MB_PORT_TAG,"xMBMasterPortEventPost event 'EV_MASTER_ERROR_RECEIVE_DATA' failed!!!");
|
|
||||||
}
|
|
||||||
ESP_LOGD(MB_PORT_TAG,"%s:Callback receive data timeout failure.", __func__);
|
ESP_LOGD(MB_PORT_TAG,"%s:Callback receive data timeout failure.", __func__);
|
||||||
|
ESP_LOG_BUFFER_HEX_LEVEL("Err rcv buf", (void*)pucPDUData, (uint16_t)ucPDULength, ESP_LOG_DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -228,9 +218,9 @@ void vMBMasterErrorCBReceiveData(UCHAR ucDestAddress, const UCHAR* pucPDUData, U
|
|||||||
void vMBMasterErrorCBExecuteFunction(UCHAR ucDestAddress, const UCHAR* pucPDUData, USHORT ucPDULength)
|
void vMBMasterErrorCBExecuteFunction(UCHAR ucDestAddress, const UCHAR* pucPDUData, USHORT ucPDULength)
|
||||||
{
|
{
|
||||||
BOOL ret = xMBMasterPortEventPost(EV_MASTER_ERROR_EXECUTE_FUNCTION);
|
BOOL ret = xMBMasterPortEventPost(EV_MASTER_ERROR_EXECUTE_FUNCTION);
|
||||||
if(ret != TRUE) {
|
MB_PORT_CHECK((ret == TRUE), ; , "%s: Post event 'EV_MASTER_ERROR_EXECUTE_FUNCTION' failed!", __func__);
|
||||||
ESP_LOGE(MB_PORT_TAG,"xMBMasterPortEventPost event 'EV_MASTER_ERROR_EXECUTE_FUNCTION' failed!!!");
|
ESP_LOGD(MB_PORT_TAG,"%s:Callback execute data handler failure.", __func__);
|
||||||
}
|
ESP_LOG_BUFFER_HEX_LEVEL("Exec func buf", (void*)pucPDUData, (uint16_t)ucPDULength, ESP_LOG_DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -244,9 +234,8 @@ void vMBMasterCBRequestSuccess( void ) {
|
|||||||
* If you don't use OS, you can change it.
|
* If you don't use OS, you can change it.
|
||||||
*/
|
*/
|
||||||
BOOL ret = xMBMasterPortEventPost(EV_MASTER_PROCESS_SUCCESS);
|
BOOL ret = xMBMasterPortEventPost(EV_MASTER_PROCESS_SUCCESS);
|
||||||
if (ret != TRUE) {
|
MB_PORT_CHECK((ret == TRUE), ; , "%s: Post event 'EV_MASTER_PROCESS_SUCCESS' failed!", __func__);
|
||||||
ESP_LOGE(MB_PORT_TAG,"xMBMasterPortEventPost event 'EV_MASTER_PROCESS_SUCCESS' failed!!!");
|
ESP_LOGD(MB_PORT_TAG,"%s: Callback request success.", __func__);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -271,20 +260,20 @@ eMBMasterReqErrCode eMBMasterWaitRequestFinish( void ) {
|
|||||||
if (xRecvedEvent) {
|
if (xRecvedEvent) {
|
||||||
ESP_LOGD(MB_PORT_TAG,"%s: returned event = 0x%x", __func__, xRecvedEvent);
|
ESP_LOGD(MB_PORT_TAG,"%s: returned event = 0x%x", __func__, xRecvedEvent);
|
||||||
if (!(xRecvedEvent & MB_EVENT_REQ_MASK)) {
|
if (!(xRecvedEvent & MB_EVENT_REQ_MASK)) {
|
||||||
// if we wait for certain event bits but get other then set in mask
|
// if we wait for certain event bits but get from poll subset
|
||||||
ESP_LOGE(MB_PORT_TAG,"%s: incorrect event set = 0x%x", __func__, xRecvedEvent);
|
ESP_LOGE(MB_PORT_TAG,"%s: incorrect event set = 0x%x", __func__, xRecvedEvent);
|
||||||
}
|
}
|
||||||
if MB_CHECK_EVENT(xRecvedEvent, EV_MASTER_PROCESS_SUCCESS) {
|
if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_PROCESS_SUCCESS)) {
|
||||||
eErrStatus = MB_MRE_NO_ERR;
|
eErrStatus = MB_MRE_NO_ERR;
|
||||||
} else if MB_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_RESPOND_TIMEOUT){
|
} else if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_RESPOND_TIMEOUT)) {
|
||||||
eErrStatus = MB_MRE_TIMEDOUT;
|
eErrStatus = MB_MRE_TIMEDOUT;
|
||||||
} else if MB_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_RECEIVE_DATA){
|
} else if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_RECEIVE_DATA)) {
|
||||||
eErrStatus = MB_MRE_REV_DATA;
|
eErrStatus = MB_MRE_REV_DATA;
|
||||||
} else if MB_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_EXECUTE_FUNCTION){
|
} else if (MB_PORT_CHECK_EVENT(xRecvedEvent, EV_MASTER_ERROR_EXECUTE_FUNCTION)) {
|
||||||
eErrStatus = MB_MRE_EXE_FUN;
|
eErrStatus = MB_MRE_EXE_FUN;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event or timeout xRecvedEvent = 0x%x", __func__, xRecvedEvent);
|
ESP_LOGE(MB_PORT_TAG,"%s: Incorrect event or timeout xRecvedEvent = 0x%x", __func__, uxBits);
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
|
@ -64,7 +64,7 @@ static void modbus_master_task(void *pvParameters)
|
|||||||
BOOL xSentState = xMBMasterPortSerialTxPoll();
|
BOOL xSentState = xMBMasterPortSerialTxPoll();
|
||||||
if (xSentState) {
|
if (xSentState) {
|
||||||
// Let state machine know that response was transmitted out
|
// Let state machine know that response was transmitted out
|
||||||
(void)xMBMasterPortEventPost(EV_MASTER_FRAME_TRANSMITTED);
|
(void)xMBMasterPortEventPost(EV_MASTER_FRAME_SENT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,7 +279,13 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
|
|||||||
case MB_MRE_REV_DATA:
|
case MB_MRE_REV_DATA:
|
||||||
error = ESP_ERR_INVALID_RESPONSE;
|
error = ESP_ERR_INVALID_RESPONSE;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case MB_MRE_MASTER_BUSY:
|
||||||
|
error = ESP_ERR_INVALID_STATE; // Master is busy (previous request is pending)
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
ESP_LOGE(MB_MASTER_TAG, "%s: Incorrect return code (%x) ", __FUNCTION__, mb_error);
|
||||||
error = ESP_FAIL;
|
error = ESP_FAIL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user