2015-04-07 20:16:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/modbus/mb.c
|
|
|
|
*
|
2015-04-08 00:48:09 +02:00
|
|
|
* FreeModbus Library: A portable Modbus implementation for Modbus ASCII/RTU.
|
2012-07-21 15:35:35 +02:00
|
|
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
|
|
|
* 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.
|
|
|
|
*
|
2015-04-07 20:16:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 16:56:21 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
#include "port.h"
|
|
|
|
|
2012-07-21 18:18:16 +02:00
|
|
|
#include <apps/modbus/mb.h>
|
|
|
|
#include <apps/modbus/mbframe.h>
|
|
|
|
#include <apps/modbus/mbproto.h>
|
|
|
|
#include <apps/modbus/mbfunc.h>
|
|
|
|
|
|
|
|
#include <apps/modbus/mbport.h>
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_RTU_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
# include "mbrtu.h"
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 18:18:16 +02:00
|
|
|
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_ASCII_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
# include "mbascii.h"
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 18:18:16 +02:00
|
|
|
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_TCP_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
# include "mbtcp.h"
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
static uint8_t ucMBAddress;
|
2012-07-21 15:35:35 +02:00
|
|
|
static eMBMode eMBCurrentMode;
|
|
|
|
|
|
|
|
static enum
|
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
STATE_ENABLED,
|
|
|
|
STATE_DISABLED,
|
|
|
|
STATE_NOT_INITIALIZED
|
2012-07-21 15:35:35 +02:00
|
|
|
} eMBState = STATE_NOT_INITIALIZED;
|
|
|
|
|
2014-11-25 22:14:28 +01:00
|
|
|
/* Functions pointer which are initialized in eMBInit(). Depending on the
|
2012-07-21 15:35:35 +02:00
|
|
|
* mode (RTU or ASCII) the are set to the correct implementations.
|
|
|
|
*/
|
2015-04-07 20:16:15 +02:00
|
|
|
|
2012-07-21 15:35:35 +02:00
|
|
|
static peMBFrameSend peMBFrameSendCur;
|
|
|
|
static pvMBFrameStart pvMBFrameStartCur;
|
|
|
|
static pvMBFrameStop pvMBFrameStopCur;
|
|
|
|
static peMBFrameReceive peMBFrameReceiveCur;
|
|
|
|
static pvMBFrameClose pvMBFrameCloseCur;
|
|
|
|
|
|
|
|
/* Callback functions required by the porting layer. They are called when
|
2015-04-07 20:16:15 +02:00
|
|
|
* an external event has happened which includes a timeout or the reception
|
2012-07-21 15:35:35 +02:00
|
|
|
* or transmission of a character.
|
|
|
|
*/
|
2015-04-07 20:16:15 +02:00
|
|
|
|
2014-11-25 22:14:28 +01:00
|
|
|
bool(*pxMBFrameCBByteReceived)(void);
|
|
|
|
bool(*pxMBFrameCBTransmitterEmpty)(void);
|
|
|
|
bool(*pxMBPortCBTimerExpired)(void);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2014-11-25 22:14:28 +01:00
|
|
|
bool(*pxMBFrameCBReceiveFSMCur)(void);
|
|
|
|
bool(*pxMBFrameCBTransmitFSMCur)(void);
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
/* An array of Modbus functions handlers which associates Modbus function
|
|
|
|
* codes with implementing functions.
|
|
|
|
*/
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
static xMBFunctionHandler xFuncHandlers[CONFIG_MB_FUNC_HANDLERS_MAX] =
|
|
|
|
{
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-11-20 14:36:07 +01:00
|
|
|
#ifdef CONFIG_MB_FUNC_READ_INPUT_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_READ_HOLDING_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_WRITE_HOLDING_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_READWRITE_HOLDING_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_READ_COILS_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_READ_COILS, eMBFuncReadCoils},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_WRITE_COIL_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_FUNC_READ_DISCRETE_INPUTS_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
{MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
eMBErrorCode eMBInit(eMBMode eMode, uint8_t ucSlaveAddress, uint8_t ucPort,
|
|
|
|
speed_t ulBaudRate, eMBParity eParity)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
/* check preconditions */
|
2014-11-25 22:14:28 +01:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
if ((ucSlaveAddress == MB_ADDRESS_BROADCAST) ||
|
|
|
|
(ucSlaveAddress < MB_ADDRESS_MIN) || (ucSlaveAddress > MB_ADDRESS_MAX))
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = MB_EINVAL;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
ucMBAddress = ucSlaveAddress;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
switch (eMode)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_RTU_ENABLED
|
2012-07-21 15:35:35 +02:00
|
|
|
case MB_RTU:
|
2015-04-07 20:16:15 +02:00
|
|
|
pvMBFrameStartCur = eMBRTUStart;
|
|
|
|
pvMBFrameStopCur = eMBRTUStop;
|
|
|
|
peMBFrameSendCur = eMBRTUSend;
|
|
|
|
peMBFrameReceiveCur = eMBRTUReceive;
|
|
|
|
pvMBFrameCloseCur = vMBPortClose;
|
|
|
|
pxMBFrameCBByteReceived = xMBRTUReceiveFSM;
|
|
|
|
pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM;
|
|
|
|
pxMBPortCBTimerExpired = xMBRTUTimerT35Expired;
|
|
|
|
|
|
|
|
eStatus = eMBRTUInit(ucMBAddress, ucPort, ulBaudRate, eParity);
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
2014-04-14 00:24:28 +02:00
|
|
|
#ifdef CONFIG_MB_ASCII_ENABLED
|
2012-07-21 15:35:35 +02:00
|
|
|
case MB_ASCII:
|
2015-04-07 20:16:15 +02:00
|
|
|
pvMBFrameStartCur = eMBASCIIStart;
|
|
|
|
pvMBFrameStopCur = eMBASCIIStop;
|
|
|
|
peMBFrameSendCur = eMBASCIISend;
|
|
|
|
peMBFrameReceiveCur = eMBASCIIReceive;
|
|
|
|
pvMBFrameCloseCur = vMBPortClose;
|
|
|
|
pxMBFrameCBByteReceived = xMBASCIIReceiveFSM;
|
|
|
|
pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM;
|
|
|
|
pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired;
|
|
|
|
|
|
|
|
eStatus = eMBASCIIInit(ucMBAddress, ucPort, ulBaudRate, eParity);
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
#endif
|
|
|
|
default:
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = MB_EINVAL;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
if (eStatus == MB_ENOERR)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
if (!xMBPortEventInit())
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
/* port dependent event module initialization failed. */
|
|
|
|
|
|
|
|
eStatus = MB_EPORTERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBCurrentMode = eMode;
|
|
|
|
eMBState = STATE_DISABLED;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2012-07-21 16:56:21 +02:00
|
|
|
#ifdef CONFIG_MB_TCP_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eMBTCPInit(uint16_t ucTCPPort)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
if ((eStatus = eMBTCPDoInit(ucTCPPort)) != MB_ENOERR)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBState = STATE_DISABLED;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else if (!xMBPortEventInit())
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
/* Port dependent event module initialization failed. */
|
|
|
|
|
|
|
|
eStatus = MB_EPORTERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
pvMBFrameStartCur = eMBTCPStart;
|
|
|
|
pvMBFrameStopCur = eMBTCPStop;
|
|
|
|
peMBFrameReceiveCur = eMBTCPReceive;
|
|
|
|
peMBFrameSendCur = eMBTCPSend;
|
|
|
|
#ifdef CONFIG_MB_HAVE_CLOSE
|
|
|
|
pvMBFrameCloseCur = vMBTCPPortClose;
|
|
|
|
#else
|
|
|
|
pvMBFrameCloseCur = NULL;
|
|
|
|
#endif
|
|
|
|
ucMBAddress = MB_TCP_PSEUDO_ADDRESS;
|
|
|
|
eMBCurrentMode = MB_TCP;
|
|
|
|
eMBState = STATE_DISABLED;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eMBRegisterCB(uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eStatus;
|
|
|
|
int i;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
if ((0 < ucFunctionCode) && (ucFunctionCode <= 127))
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
ENTER_CRITICAL_SECTION();
|
|
|
|
if (pxHandler != NULL)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
for (i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
if ((xFuncHandlers[i].pxHandler == NULL) ||
|
|
|
|
(xFuncHandlers[i].pxHandler == pxHandler))
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
|
|
|
|
xFuncHandlers[i].pxHandler = pxHandler;
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = (i != CONFIG_MB_FUNC_HANDLERS_MAX) ? MB_ENOERR : MB_ENORES;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
for (i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
xFuncHandlers[i].ucFunctionCode = 0;
|
|
|
|
xFuncHandlers[i].pxHandler = NULL;
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
/* Remove can't fail. */
|
|
|
|
|
|
|
|
eStatus = MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
EXIT_CRITICAL_SECTION();
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = MB_EINVAL;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
return eStatus;
|
|
|
|
}
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eMBClose(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
if (eMBState == STATE_DISABLED)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
if (pvMBFrameCloseCur != NULL)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
pvMBFrameCloseCur();
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = MB_EILLSTATE;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eMBEnable(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
if (eMBState == STATE_DISABLED)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
/* Activate the protocol stack. */
|
|
|
|
|
|
|
|
pvMBFrameStartCur();
|
|
|
|
eMBState = STATE_ENABLED;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = MB_EILLSTATE;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eMBDisable(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
if (eMBState == STATE_ENABLED)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
pvMBFrameStopCur();
|
|
|
|
eMBState = STATE_DISABLED;
|
|
|
|
eStatus = MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else if (eMBState == STATE_DISABLED)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = MB_EILLSTATE;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
return eStatus;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
eMBErrorCode eMBPoll(void)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
static uint8_t *ucMBFrame;
|
|
|
|
static uint8_t ucRcvAddress;
|
|
|
|
static uint8_t ucFunctionCode;
|
|
|
|
static uint16_t usLength;
|
|
|
|
static eMBException eException;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
|
|
eMBEventType eEvent;
|
|
|
|
|
|
|
|
/* Check if the protocol stack is ready. */
|
|
|
|
|
|
|
|
if (eMBState != STATE_ENABLED)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
return MB_EILLSTATE;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
/* Check if there is a event available. If not return control to caller.
|
|
|
|
* Otherwise we will handle the event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (xMBPortEventGet(&eEvent) == true)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
switch (eEvent)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
|
|
|
case EV_READY:
|
2015-04-07 20:16:15 +02:00
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
|
|
|
|
case EV_FRAME_RECEIVED:
|
2015-04-07 20:16:15 +02:00
|
|
|
eStatus = peMBFrameReceiveCur(&ucRcvAddress, &ucMBFrame, &usLength);
|
|
|
|
if (eStatus == MB_ENOERR)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
/* Check if the frame is for us. If not ignore the frame. */
|
|
|
|
|
|
|
|
if ((ucRcvAddress == ucMBAddress) || (ucRcvAddress == MB_ADDRESS_BROADCAST))
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
(void)xMBPortEventPost(EV_EXECUTE);
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EV_EXECUTE:
|
2015-04-07 20:16:15 +02:00
|
|
|
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
|
|
|
|
eException = MB_EX_ILLEGAL_FUNCTION;
|
|
|
|
for( i = 0; i < CONFIG_MB_FUNC_HANDLERS_MAX; i++)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
/* No more function handlers registered. Abort. */
|
|
|
|
|
|
|
|
if (xFuncHandlers[i].ucFunctionCode == 0)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
else if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
eException = xFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
|
|
|
|
break;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 20:16:15 +02:00
|
|
|
/* If the request was not sent to the broadcast address we
|
|
|
|
* return a reply.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ucRcvAddress != MB_ADDRESS_BROADCAST)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
if (eException != MB_EX_NONE)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
/* An exception occured. Build an error frame. */
|
|
|
|
|
|
|
|
usLength = 0;
|
|
|
|
ucMBFrame[usLength++] = (uint8_t)(ucFunctionCode | MB_FUNC_ERROR);
|
|
|
|
ucMBFrame[usLength++] = eException;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
2012-11-14 15:29:01 +01:00
|
|
|
#ifdef CONFIG_MB_ASCII_ENABLED
|
2015-04-07 20:16:15 +02:00
|
|
|
if ((eMBCurrentMode == MB_ASCII) && CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS)
|
2012-07-21 15:35:35 +02:00
|
|
|
{
|
2015-04-07 20:16:15 +02:00
|
|
|
vMBPortTimersDelay(CONFIG_MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS);
|
2012-11-14 15:29:01 +01:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-07 20:16:15 +02:00
|
|
|
(void)peMBFrameSendCur(ucMBAddress, ucMBFrame, usLength);
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EV_FRAME_SENT:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 20:16:15 +02:00
|
|
|
|
|
|
|
return MB_ENOERR;
|
2012-07-21 15:35:35 +02:00
|
|
|
}
|