2018-10-19 13:51:27 +00:00
|
|
|
/*
|
|
|
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
|
|
|
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* File: $Id: mbrtu_m.c,v 1.60 2013/08/20 11:18:10 Armink Add Master Functions $
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------------- System includes ----------------------------------*/
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
/* ----------------------- Platform includes --------------------------------*/
|
|
|
|
#include "port.h"
|
|
|
|
|
|
|
|
/* ----------------------- Modbus includes ----------------------------------*/
|
|
|
|
|
|
|
|
#include "mb_m.h"
|
|
|
|
#include "mbconfig.h"
|
|
|
|
#include "mbframe.h"
|
|
|
|
#include "mbproto.h"
|
|
|
|
#include "mbfunc.h"
|
|
|
|
|
|
|
|
#include "mbport.h"
|
2019-11-11 10:58:56 +00:00
|
|
|
#if MB_MASTER_RTU_ENABLED
|
2018-10-19 13:51:27 +00:00
|
|
|
#include "mbrtu.h"
|
|
|
|
#endif
|
2019-11-11 10:58:56 +00:00
|
|
|
#if MB_MASTER_ASCII_ENABLED
|
2019-11-26 05:16:25 +00:00
|
|
|
#include "mbascii.h"
|
2018-10-19 13:51:27 +00:00
|
|
|
#endif
|
2019-11-11 10:58:56 +00:00
|
|
|
#if MB_MASTER_TCP_ENABLED
|
2018-10-19 13:51:27 +00:00
|
|
|
#include "mbtcp.h"
|
|
|
|
#endif
|
|
|
|
|
2019-11-11 10:58:56 +00:00
|
|
|
#if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
|
2018-10-19 13:51:27 +00:00
|
|
|
|
|
|
|
#ifndef MB_PORT_HAS_CLOSE
|
|
|
|
#define MB_PORT_HAS_CLOSE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ----------------------- Static variables ---------------------------------*/
|
|
|
|
|
|
|
|
static UCHAR ucMBMasterDestAddress;
|
|
|
|
static BOOL xMBRunInMasterMode = FALSE;
|
|
|
|
static volatile eMBMasterErrorEventType eMBMasterCurErrorType;
|
2019-11-26 05:16:25 +00:00
|
|
|
static volatile USHORT usMasterSendPDULength;
|
|
|
|
|
|
|
|
/*------------------------ Shared variables ---------------------------------*/
|
|
|
|
|
2020-03-30 14:05:48 +00:00
|
|
|
volatile UCHAR ucMasterSndBuf[MB_SERIAL_BUF_SIZE];
|
|
|
|
volatile UCHAR ucMasterRcvBuf[MB_SERIAL_BUF_SIZE];
|
2019-11-26 05:16:25 +00:00
|
|
|
volatile eMBMasterTimerMode eMasterCurTimerMode;
|
|
|
|
volatile BOOL xFrameIsBroadcast = FALSE;
|
2018-10-19 13:51:27 +00:00
|
|
|
|
|
|
|
static enum
|
|
|
|
{
|
|
|
|
STATE_ENABLED,
|
|
|
|
STATE_DISABLED,
|
|
|
|
STATE_NOT_INITIALIZED
|
|
|
|
} eMBState = STATE_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
/* Functions pointer which are initialized in eMBInit( ). Depending on the
|
|
|
|
* mode (RTU or ASCII) the are set to the correct implementations.
|
|
|
|
* Using for Modbus Master,Add by Armink 20130813
|
|
|
|
*/
|
|
|
|
static peMBFrameSend peMBMasterFrameSendCur;
|
|
|
|
static pvMBFrameStart pvMBMasterFrameStartCur;
|
|
|
|
static pvMBFrameStop pvMBMasterFrameStopCur;
|
|
|
|
static peMBFrameReceive peMBMasterFrameReceiveCur;
|
|
|
|
static pvMBFrameClose pvMBMasterFrameCloseCur;
|
|
|
|
|
|
|
|
/* Callback functions required by the porting layer. They are called when
|
|
|
|
* an external event has happend which includes a timeout or the reception
|
|
|
|
* or transmission of a character.
|
|
|
|
* Using for Modbus Master,Add by Armink 20130813
|
|
|
|
*/
|
|
|
|
BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
|
|
|
|
|
|
|
|
BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
|
|
|
|
|
|
|
|
BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
|
|
|
|
|
|
|
|
BOOL( *pxMBMasterFrameCBReceiveFSMCur ) ( void );
|
|
|
|
|
|
|
|
BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void );
|
|
|
|
|
|
|
|
/* An array of Modbus functions handlers which associates Modbus function
|
|
|
|
* codes with implementing functions.
|
|
|
|
*/
|
|
|
|
static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
|
|
|
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
|
|
|
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
|
|
|
{MB_FUNC_READ_INPUT_REGISTER, eMBMasterFuncReadInputRegister},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
|
|
|
{MB_FUNC_READ_HOLDING_REGISTER, eMBMasterFuncReadHoldingRegister},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
|
|
|
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBMasterFuncWriteMultipleHoldingRegister},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
|
|
|
{MB_FUNC_WRITE_REGISTER, eMBMasterFuncWriteHoldingRegister},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
|
|
|
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBMasterFuncReadWriteMultipleHoldingRegister},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_READ_COILS_ENABLED > 0
|
|
|
|
{MB_FUNC_READ_COILS, eMBMasterFuncReadCoils},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
|
|
|
{MB_FUNC_WRITE_SINGLE_COIL, eMBMasterFuncWriteCoil},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
|
|
|
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBMasterFuncWriteMultipleCoils},
|
|
|
|
#endif
|
|
|
|
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
|
|
|
{MB_FUNC_READ_DISCRETE_INPUTS, eMBMasterFuncReadDiscreteInputs},
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ----------------------- Start implementation -----------------------------*/
|
|
|
|
eMBErrorCode
|
|
|
|
eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
|
|
|
{
|
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
|
|
|
|
switch (eMode)
|
|
|
|
{
|
|
|
|
#if MB_MASTER_RTU_ENABLED > 0
|
|
|
|
case MB_RTU:
|
|
|
|
pvMBMasterFrameStartCur = eMBMasterRTUStart;
|
|
|
|
pvMBMasterFrameStopCur = eMBMasterRTUStop;
|
|
|
|
peMBMasterFrameSendCur = eMBMasterRTUSend;
|
|
|
|
peMBMasterFrameReceiveCur = eMBMasterRTUReceive;
|
|
|
|
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
|
|
|
|
pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM;
|
|
|
|
pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM;
|
|
|
|
pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired;
|
|
|
|
|
|
|
|
eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if MB_MASTER_ASCII_ENABLED > 0
|
2019-11-26 05:16:25 +00:00
|
|
|
case MB_ASCII:
|
2018-10-19 13:51:27 +00:00
|
|
|
pvMBMasterFrameStartCur = eMBMasterASCIIStart;
|
|
|
|
pvMBMasterFrameStopCur = eMBMasterASCIIStop;
|
|
|
|
peMBMasterFrameSendCur = eMBMasterASCIISend;
|
|
|
|
peMBMasterFrameReceiveCur = eMBMasterASCIIReceive;
|
|
|
|
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
|
|
|
|
pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM;
|
|
|
|
pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM;
|
|
|
|
pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired;
|
|
|
|
|
|
|
|
eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity );
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
eStatus = MB_EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eStatus == MB_ENOERR)
|
|
|
|
{
|
|
|
|
if (!xMBMasterPortEventInit())
|
|
|
|
{
|
|
|
|
/* port dependent event module initalization failed. */
|
|
|
|
eStatus = MB_EPORTERR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eMBState = STATE_DISABLED;
|
|
|
|
}
|
|
|
|
/* initialize the OS resource for modbus master. */
|
|
|
|
vMBMasterOsResInit();
|
|
|
|
}
|
|
|
|
return eStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode
|
|
|
|
eMBMasterClose( void )
|
|
|
|
{
|
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
|
|
|
|
if( eMBState == STATE_DISABLED )
|
|
|
|
{
|
|
|
|
if( pvMBMasterFrameCloseCur != NULL )
|
|
|
|
{
|
|
|
|
pvMBMasterFrameCloseCur( );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eStatus = MB_EILLSTATE;
|
|
|
|
}
|
|
|
|
return eStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode
|
|
|
|
eMBMasterEnable( void )
|
|
|
|
{
|
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
|
|
|
|
if( eMBState == STATE_DISABLED )
|
|
|
|
{
|
|
|
|
/* Activate the protocol stack. */
|
|
|
|
pvMBMasterFrameStartCur( );
|
|
|
|
/* Release the resource, because it created in busy state */
|
|
|
|
vMBMasterRunResRelease( );
|
|
|
|
eMBState = STATE_ENABLED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eStatus = MB_EILLSTATE;
|
|
|
|
}
|
|
|
|
return eStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode
|
|
|
|
eMBMasterDisable( void )
|
|
|
|
{
|
|
|
|
eMBErrorCode eStatus;
|
|
|
|
|
|
|
|
if( eMBState == STATE_ENABLED )
|
|
|
|
{
|
|
|
|
pvMBMasterFrameStopCur( );
|
|
|
|
eMBState = STATE_DISABLED;
|
|
|
|
eStatus = MB_ENOERR;
|
|
|
|
}
|
|
|
|
else if( eMBState == STATE_DISABLED )
|
|
|
|
{
|
|
|
|
eStatus = MB_ENOERR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eStatus = MB_EILLSTATE;
|
|
|
|
}
|
|
|
|
return eStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
eMBErrorCode
|
|
|
|
eMBMasterPoll( void )
|
|
|
|
{
|
2019-11-11 10:58:56 +00:00
|
|
|
static UCHAR *ucMBFrame = NULL;
|
2018-10-19 13:51:27 +00:00
|
|
|
static UCHAR ucRcvAddress;
|
|
|
|
static UCHAR ucFunctionCode;
|
|
|
|
static USHORT usLength;
|
|
|
|
static eMBException eException;
|
2019-11-11 10:58:56 +00:00
|
|
|
int i;
|
|
|
|
int j;
|
2018-10-19 13:51:27 +00:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
eMBMasterEventType eEvent;
|
|
|
|
eMBMasterErrorEventType errorType;
|
|
|
|
|
|
|
|
/* Check if the protocol stack is ready. */
|
2019-11-11 10:58:56 +00:00
|
|
|
if( eMBState != STATE_ENABLED )
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
return MB_EILLSTATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there is a event available. If not return control to caller.
|
|
|
|
* Otherwise we will handle the event. */
|
2019-12-10 06:27:09 +00:00
|
|
|
if ( xMBMasterPortEventGet( &eEvent ) == TRUE )
|
2019-11-11 10:58:56 +00:00
|
|
|
{
|
2019-12-10 06:27:09 +00:00
|
|
|
// In some cases it is possible that more than one event set
|
|
|
|
// together (even from one subset mask) than process them consistently
|
|
|
|
if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_READY ) ) {
|
2018-10-19 13:51:27 +00:00
|
|
|
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_READY", __func__);
|
2019-12-10 06:27:09 +00:00
|
|
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_READY );
|
|
|
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_FRAME_RECEIVED ) ) {
|
|
|
|
eStatus = peMBMasterFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength);
|
2018-10-19 13:51:27 +00:00
|
|
|
// Check if the frame is for us. If not ,send an error process event.
|
2019-11-11 10:58:56 +00:00
|
|
|
if ( ( eStatus == MB_ENOERR ) && ( ucRcvAddress == ucMBMasterGetDestAddress() ) )
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
( void ) xMBMasterPortEventPost( EV_MASTER_EXECUTE );
|
2020-06-15 07:20:42 +00:00
|
|
|
ESP_LOGD(MB_PORT_TAG, "%s: Packet data received successfully (%u).", __func__, eStatus);
|
|
|
|
ESP_LOG_BUFFER_HEX_LEVEL("POLL RCV buffer", (void*)ucMBFrame, (uint16_t)usLength, ESP_LOG_DEBUG);
|
2019-12-10 06:27:09 +00:00
|
|
|
}
|
2019-11-11 10:58:56 +00:00
|
|
|
else
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
|
2020-06-15 07:20:42 +00:00
|
|
|
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
2019-12-10 06:27:09 +00:00
|
|
|
ESP_LOGD( MB_PORT_TAG, "%s: Packet data receive failed (addr=%u)(%u).",
|
|
|
|
__func__, ucRcvAddress, eStatus);
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
2019-12-10 06:27:09 +00:00
|
|
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_FRAME_RECEIVED );
|
|
|
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_EXECUTE ) ) {
|
2019-11-11 10:58:56 +00:00
|
|
|
if ( !ucMBFrame )
|
|
|
|
{
|
|
|
|
return MB_EILLSTATE;
|
|
|
|
}
|
2018-10-19 13:51:27 +00:00
|
|
|
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.*/
|
2019-11-11 10:58:56 +00:00
|
|
|
if (ucFunctionCode & MB_FUNC_ERROR)
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
|
2019-12-10 06:27:09 +00:00
|
|
|
} else {
|
|
|
|
for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
2019-11-11 10:58:56 +00:00
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
/* No more function handlers registered. Abort. */
|
2019-11-11 10:58:56 +00:00
|
|
|
if (xMasterFuncHandlers[i].ucFunctionCode == 0)
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-11-11 10:58:56 +00:00
|
|
|
if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode)
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
vMBMasterSetCBRunInMasterMode(TRUE);
|
|
|
|
/* If master request is broadcast,
|
|
|
|
* the master need execute function for all slave.
|
|
|
|
*/
|
2019-11-11 10:58:56 +00:00
|
|
|
if ( xMBMasterRequestIsBroadcast() )
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
usLength = usMBMasterGetPDUSndLength();
|
2019-11-11 10:58:56 +00:00
|
|
|
for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++)
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
vMBMasterSetDestAddress(j);
|
|
|
|
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
|
|
|
|
}
|
2019-11-11 10:58:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-10 06:27:09 +00:00
|
|
|
eException = xMasterFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
2019-12-10 06:27:09 +00:00
|
|
|
vMBMasterSetCBRunInMasterMode( FALSE );
|
2018-10-19 13:51:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
|
2019-11-11 10:58:56 +00:00
|
|
|
if (eException != MB_EX_NONE)
|
|
|
|
{
|
2018-10-19 13:51:27 +00:00
|
|
|
vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
|
|
|
|
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
2019-11-11 10:58:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-30 14:05:48 +00:00
|
|
|
vMBMasterSetErrorType(EV_ERROR_OK);
|
|
|
|
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
2019-12-10 06:27:09 +00:00
|
|
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_EXECUTE );
|
|
|
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_FRAME_TRANSMIT ) ) {
|
2019-11-26 05:16:25 +00:00
|
|
|
ESP_LOGD(MB_PORT_TAG, "%s:EV_MASTER_FRAME_TRANSMIT", __func__);
|
2018-10-19 13:51:27 +00:00
|
|
|
/* Master is busy now. */
|
|
|
|
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
|
|
|
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
|
2019-11-11 10:58:56 +00:00
|
|
|
if (eStatus != MB_ENOERR)
|
|
|
|
{
|
2019-12-10 06:27:09 +00:00
|
|
|
ESP_LOGE( MB_PORT_TAG, "%s:Frame send error. %d", __func__, eStatus );
|
2020-06-15 07:20:42 +00:00
|
|
|
} else {
|
|
|
|
ESP_LOG_BUFFER_HEX_LEVEL("Sent buffer", (void*)ucMBFrame, usMBMasterGetPDUSndLength(), ESP_LOG_DEBUG);
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
2019-12-10 06:27:09 +00:00
|
|
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_FRAME_TRANSMIT );
|
|
|
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_FRAME_SENT ) ) {
|
|
|
|
ESP_LOGD( MB_PORT_TAG, "%s:EV_MASTER_FRAME_SENT", __func__ );
|
|
|
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_FRAME_SENT );
|
|
|
|
} else if ( MB_PORT_CHECK_EVENT( eEvent, EV_MASTER_ERROR_PROCESS ) ) {
|
|
|
|
ESP_LOGD( MB_PORT_TAG, "%s:EV_MASTER_ERROR_PROCESS", __func__ );
|
2018-10-19 13:51:27 +00:00
|
|
|
/* Execute specified error process callback function. */
|
2019-12-10 06:27:09 +00:00
|
|
|
errorType = eMBMasterGetErrorType( );
|
2018-10-19 13:51:27 +00:00
|
|
|
vMBMasterGetPDUSndBuf( &ucMBFrame );
|
2019-12-10 06:27:09 +00:00
|
|
|
switch ( errorType )
|
2019-11-26 05:16:25 +00:00
|
|
|
{
|
2019-12-10 06:27:09 +00:00
|
|
|
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;
|
2020-03-30 14:05:48 +00:00
|
|
|
case EV_ERROR_OK:
|
|
|
|
vMBMasterCBRequestSuccess( );
|
|
|
|
break;
|
2019-12-10 06:27:09 +00:00
|
|
|
default:
|
|
|
|
ESP_LOGE( MB_PORT_TAG, "%s: incorrect error type = %d.", __func__, errorType);
|
|
|
|
break;
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
2019-12-10 06:27:09 +00:00
|
|
|
vMBMasterRunResRelease( );
|
|
|
|
MB_PORT_CLEAR_EVENT( eEvent, EV_MASTER_ERROR_PROCESS );
|
|
|
|
}
|
|
|
|
if ( eEvent ) {
|
|
|
|
// Event processing is done, but some poll events still set then
|
|
|
|
// postpone its processing for next poll cycle (rare case).
|
|
|
|
ESP_LOGW( MB_PORT_TAG, "%s: Unprocessed event %d.", __func__, eEvent );
|
|
|
|
( void ) xMBMasterPortEventPost( eEvent );
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-10 06:27:09 +00:00
|
|
|
// Something went wrong and task unblocked but there are no any correct events set
|
|
|
|
ESP_LOGE( MB_PORT_TAG, "%s: Unexpected event triggered %d.", __func__, eEvent );
|
|
|
|
eStatus = MB_EILLSTATE;
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
2019-12-10 06:27:09 +00:00
|
|
|
return eStatus;
|
2018-10-19 13:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get whether the Modbus Master is run in master mode.
|
|
|
|
BOOL xMBMasterGetCBRunInMasterMode( void )
|
|
|
|
{
|
|
|
|
return xMBRunInMasterMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set whether the Modbus Master is run in master mode.
|
|
|
|
void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode )
|
|
|
|
{
|
|
|
|
xMBRunInMasterMode = IsMasterMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Modbus Master send destination address.
|
|
|
|
UCHAR ucMBMasterGetDestAddress( void )
|
|
|
|
{
|
|
|
|
return ucMBMasterDestAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Modbus Master send destination address.
|
|
|
|
void vMBMasterSetDestAddress( UCHAR Address )
|
|
|
|
{
|
|
|
|
ucMBMasterDestAddress = Address;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Modbus Master current error event type.
|
2019-11-26 05:16:25 +00:00
|
|
|
eMBMasterErrorEventType inline eMBMasterGetErrorType( void )
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
|
|
|
return eMBMasterCurErrorType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Modbus Master current error event type.
|
2019-11-08 08:55:42 +00:00
|
|
|
void IRAM_ATTR vMBMasterSetErrorType( eMBMasterErrorEventType errorType )
|
2018-10-19 13:51:27 +00:00
|
|
|
{
|
|
|
|
eMBMasterCurErrorType = errorType;
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:16:25 +00:00
|
|
|
/* Get Modbus Master send PDU's buffer address pointer.*/
|
|
|
|
void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame )
|
|
|
|
{
|
|
|
|
*pucFrame = ( UCHAR * ) &ucMasterSndBuf[MB_SER_PDU_PDU_OFF];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set Modbus Master send PDU's buffer length.*/
|
|
|
|
void vMBMasterSetPDUSndLength( USHORT SendPDULength )
|
|
|
|
{
|
|
|
|
usMasterSendPDULength = SendPDULength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get Modbus Master send PDU's buffer length.*/
|
|
|
|
USHORT usMBMasterGetPDUSndLength( void )
|
|
|
|
{
|
|
|
|
return usMasterSendPDULength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set Modbus Master current timer mode.*/
|
|
|
|
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode )
|
|
|
|
{
|
|
|
|
eMasterCurTimerMode = eMBTimerMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get Modbus Master current timer mode.*/
|
2019-11-11 10:58:56 +00:00
|
|
|
eMBMasterTimerMode MB_PORT_ISR_ATTR xMBMasterGetCurTimerMode( void )
|
2019-11-26 05:16:25 +00:00
|
|
|
{
|
|
|
|
return eMasterCurTimerMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The master request is broadcast? */
|
2019-12-10 06:27:09 +00:00
|
|
|
BOOL MB_PORT_ISR_ATTR xMBMasterRequestIsBroadcast( void )
|
|
|
|
{
|
2019-11-26 05:16:25 +00:00
|
|
|
return xFrameIsBroadcast;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The master request is broadcast? */
|
|
|
|
void vMBMasterRequestSetType( BOOL xIsBroadcast ){
|
|
|
|
xFrameIsBroadcast = xIsBroadcast;
|
|
|
|
}
|
|
|
|
|
2019-11-11 10:58:56 +00:00
|
|
|
#endif // MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED
|