Port of the FreeModbus Libary for mbed, copied from https://developer.mbed.org/users/cam/code/Modbus/ and upgraded to mbed 5

Dependents:   NUCLEO-F401-printf

Committer:
Rajit Singh
Date:
Wed Aug 16 17:31:26 2017 +0100
Revision:
1:3e360cf155b6
Parent:
0:9db3bed8fffd
Remove main.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rajit Singh 0:9db3bed8fffd 1 /*
Rajit Singh 0:9db3bed8fffd 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
Rajit Singh 0:9db3bed8fffd 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
Rajit Singh 0:9db3bed8fffd 4 * All rights reserved.
Rajit Singh 0:9db3bed8fffd 5 *
Rajit Singh 0:9db3bed8fffd 6 * Redistribution and use in source and binary forms, with or without
Rajit Singh 0:9db3bed8fffd 7 * modification, are permitted provided that the following conditions
Rajit Singh 0:9db3bed8fffd 8 * are met:
Rajit Singh 0:9db3bed8fffd 9 * 1. Redistributions of source code must retain the above copyright
Rajit Singh 0:9db3bed8fffd 10 * notice, this list of conditions and the following disclaimer.
Rajit Singh 0:9db3bed8fffd 11 * 2. Redistributions in binary form must reproduce the above copyright
Rajit Singh 0:9db3bed8fffd 12 * notice, this list of conditions and the following disclaimer in the
Rajit Singh 0:9db3bed8fffd 13 * documentation and/or other materials provided with the distribution.
Rajit Singh 0:9db3bed8fffd 14 * 3. The name of the author may not be used to endorse or promote products
Rajit Singh 0:9db3bed8fffd 15 * derived from this software without specific prior written permission.
Rajit Singh 0:9db3bed8fffd 16 *
Rajit Singh 0:9db3bed8fffd 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Rajit Singh 0:9db3bed8fffd 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Rajit Singh 0:9db3bed8fffd 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Rajit Singh 0:9db3bed8fffd 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Rajit Singh 0:9db3bed8fffd 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Rajit Singh 0:9db3bed8fffd 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Rajit Singh 0:9db3bed8fffd 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Rajit Singh 0:9db3bed8fffd 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Rajit Singh 0:9db3bed8fffd 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Rajit Singh 0:9db3bed8fffd 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Rajit Singh 0:9db3bed8fffd 27 *
Rajit Singh 0:9db3bed8fffd 28 * File: $Id: mb.c,v 1.27 2007/02/18 23:45:41 wolti Exp $
Rajit Singh 0:9db3bed8fffd 29 */
Rajit Singh 0:9db3bed8fffd 30
Rajit Singh 0:9db3bed8fffd 31 /* ----------------------- System includes ----------------------------------*/
Rajit Singh 0:9db3bed8fffd 32 #include "stdlib.h"
Rajit Singh 0:9db3bed8fffd 33 #include "string.h"
Rajit Singh 0:9db3bed8fffd 34
Rajit Singh 0:9db3bed8fffd 35 /* ----------------------- Platform includes --------------------------------*/
Rajit Singh 0:9db3bed8fffd 36 #include "port.h"
Rajit Singh 0:9db3bed8fffd 37
Rajit Singh 0:9db3bed8fffd 38 /* ----------------------- Modbus includes ----------------------------------*/
Rajit Singh 0:9db3bed8fffd 39 #include "mb.h"
Rajit Singh 0:9db3bed8fffd 40 #include "mbconfig.h"
Rajit Singh 0:9db3bed8fffd 41 #include "mbframe.h"
Rajit Singh 0:9db3bed8fffd 42 #include "mbproto.h"
Rajit Singh 0:9db3bed8fffd 43 #include "mbfunc.h"
Rajit Singh 0:9db3bed8fffd 44 #include "mbport.h"
Rajit Singh 0:9db3bed8fffd 45 #if MB_RTU_ENABLED == 1
Rajit Singh 0:9db3bed8fffd 46 #include "mbrtu.h"
Rajit Singh 0:9db3bed8fffd 47 #endif
Rajit Singh 0:9db3bed8fffd 48 #if MB_ASCII_ENABLED == 1
Rajit Singh 0:9db3bed8fffd 49 #include "mbascii.h"
Rajit Singh 0:9db3bed8fffd 50 #endif
Rajit Singh 0:9db3bed8fffd 51 #if MB_TCP_ENABLED == 1
Rajit Singh 0:9db3bed8fffd 52 #include "mbtcp.h"
Rajit Singh 0:9db3bed8fffd 53 #endif
Rajit Singh 0:9db3bed8fffd 54
Rajit Singh 0:9db3bed8fffd 55 #ifndef MB_PORT_HAS_CLOSE
Rajit Singh 0:9db3bed8fffd 56 #define MB_PORT_HAS_CLOSE 0
Rajit Singh 0:9db3bed8fffd 57 #endif
Rajit Singh 0:9db3bed8fffd 58
Rajit Singh 0:9db3bed8fffd 59 /* ----------------------- Static variables ---------------------------------*/
Rajit Singh 0:9db3bed8fffd 60
Rajit Singh 0:9db3bed8fffd 61
Rajit Singh 0:9db3bed8fffd 62 static UCHAR ucMBAddress;
Rajit Singh 0:9db3bed8fffd 63 static eMBMode eMBCurrentMode;
Rajit Singh 0:9db3bed8fffd 64
Rajit Singh 0:9db3bed8fffd 65 static enum {
Rajit Singh 0:9db3bed8fffd 66 STATE_ENABLED,
Rajit Singh 0:9db3bed8fffd 67 STATE_DISABLED,
Rajit Singh 0:9db3bed8fffd 68 STATE_NOT_INITIALIZED
Rajit Singh 0:9db3bed8fffd 69 } eMBState = STATE_NOT_INITIALIZED;
Rajit Singh 0:9db3bed8fffd 70
Rajit Singh 0:9db3bed8fffd 71 /* Functions pointer which are initialized in eMBInit( ). Depending on the
Rajit Singh 0:9db3bed8fffd 72 * mode (RTU or ASCII) the are set to the correct implementations.
Rajit Singh 0:9db3bed8fffd 73 */
Rajit Singh 0:9db3bed8fffd 74 static peMBFrameSend peMBFrameSendCur;
Rajit Singh 0:9db3bed8fffd 75 static pvMBFrameStart pvMBFrameStartCur;
Rajit Singh 0:9db3bed8fffd 76 static pvMBFrameStop pvMBFrameStopCur;
Rajit Singh 0:9db3bed8fffd 77 static peMBFrameReceive peMBFrameReceiveCur;
Rajit Singh 0:9db3bed8fffd 78 static pvMBFrameClose pvMBFrameCloseCur;
Rajit Singh 0:9db3bed8fffd 79
Rajit Singh 0:9db3bed8fffd 80 /* Callback functions required by the porting layer. They are called when
Rajit Singh 0:9db3bed8fffd 81 * an external event has happend which includes a timeout or the reception
Rajit Singh 0:9db3bed8fffd 82 * or transmission of a character.
Rajit Singh 0:9db3bed8fffd 83 */
Rajit Singh 0:9db3bed8fffd 84 BOOL( *pxMBFrameCBByteReceived ) ( void );
Rajit Singh 0:9db3bed8fffd 85 BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
Rajit Singh 0:9db3bed8fffd 86 BOOL( *pxMBPortCBTimerExpired ) ( void );
Rajit Singh 0:9db3bed8fffd 87
Rajit Singh 0:9db3bed8fffd 88 BOOL( *pxMBFrameCBReceiveFSMCur ) ( void );
Rajit Singh 0:9db3bed8fffd 89 BOOL( *pxMBFrameCBTransmitFSMCur ) ( void );
Rajit Singh 0:9db3bed8fffd 90
Rajit Singh 0:9db3bed8fffd 91 /* An array of Modbus functions handlers which associates Modbus function
Rajit Singh 0:9db3bed8fffd 92 * codes with implementing functions.
Rajit Singh 0:9db3bed8fffd 93 */
Rajit Singh 0:9db3bed8fffd 94 static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
Rajit Singh 0:9db3bed8fffd 95 #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 96 {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
Rajit Singh 0:9db3bed8fffd 97 #endif
Rajit Singh 0:9db3bed8fffd 98 #if MB_FUNC_READ_INPUT_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 99 {MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
Rajit Singh 0:9db3bed8fffd 100 #endif
Rajit Singh 0:9db3bed8fffd 101 #if MB_FUNC_READ_HOLDING_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 102 {MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
Rajit Singh 0:9db3bed8fffd 103 #endif
Rajit Singh 0:9db3bed8fffd 104 #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 105 {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
Rajit Singh 0:9db3bed8fffd 106 #endif
Rajit Singh 0:9db3bed8fffd 107 #if MB_FUNC_WRITE_HOLDING_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 108 {MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
Rajit Singh 0:9db3bed8fffd 109 #endif
Rajit Singh 0:9db3bed8fffd 110 #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 111 {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
Rajit Singh 0:9db3bed8fffd 112 #endif
Rajit Singh 0:9db3bed8fffd 113 #if MB_FUNC_READ_COILS_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 114 {MB_FUNC_READ_COILS, eMBFuncReadCoils},
Rajit Singh 0:9db3bed8fffd 115 #endif
Rajit Singh 0:9db3bed8fffd 116 #if MB_FUNC_WRITE_COIL_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 117 {MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
Rajit Singh 0:9db3bed8fffd 118 #endif
Rajit Singh 0:9db3bed8fffd 119 #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 120 {MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
Rajit Singh 0:9db3bed8fffd 121 #endif
Rajit Singh 0:9db3bed8fffd 122 #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 123 {MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
Rajit Singh 0:9db3bed8fffd 124 #endif
Rajit Singh 0:9db3bed8fffd 125 };
Rajit Singh 0:9db3bed8fffd 126
Rajit Singh 0:9db3bed8fffd 127 /* ----------------------- Start implementation -----------------------------*/
Rajit Singh 0:9db3bed8fffd 128 eMBErrorCode
Rajit Singh 0:9db3bed8fffd 129 eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity ) {
Rajit Singh 0:9db3bed8fffd 130 eMBErrorCode eStatus = MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 131
Rajit Singh 0:9db3bed8fffd 132 /* check preconditions */
Rajit Singh 0:9db3bed8fffd 133 if ( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
Rajit Singh 0:9db3bed8fffd 134 ( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) ) {
Rajit Singh 0:9db3bed8fffd 135 eStatus = MB_EINVAL;
Rajit Singh 0:9db3bed8fffd 136 } else {
Rajit Singh 0:9db3bed8fffd 137 ucMBAddress = ucSlaveAddress;
Rajit Singh 0:9db3bed8fffd 138
Rajit Singh 0:9db3bed8fffd 139 switch ( eMode ) {
Rajit Singh 0:9db3bed8fffd 140 #if MB_RTU_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 141 case MB_RTU:
Rajit Singh 0:9db3bed8fffd 142 pvMBFrameStartCur = eMBRTUStart;
Rajit Singh 0:9db3bed8fffd 143 pvMBFrameStopCur = eMBRTUStop;
Rajit Singh 0:9db3bed8fffd 144 peMBFrameSendCur = eMBRTUSend;
Rajit Singh 0:9db3bed8fffd 145 peMBFrameReceiveCur = eMBRTUReceive;
Rajit Singh 0:9db3bed8fffd 146 pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
Rajit Singh 0:9db3bed8fffd 147 pxMBFrameCBByteReceived = xMBRTUReceiveFSM;
Rajit Singh 0:9db3bed8fffd 148 pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM;
Rajit Singh 0:9db3bed8fffd 149 pxMBPortCBTimerExpired = xMBRTUTimerT35Expired;
Rajit Singh 0:9db3bed8fffd 150
Rajit Singh 0:9db3bed8fffd 151 eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
Rajit Singh 0:9db3bed8fffd 152 break;
Rajit Singh 0:9db3bed8fffd 153 #endif
Rajit Singh 0:9db3bed8fffd 154 #if MB_ASCII_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 155 case MB_ASCII:
Rajit Singh 0:9db3bed8fffd 156 pvMBFrameStartCur = eMBASCIIStart;
Rajit Singh 0:9db3bed8fffd 157 pvMBFrameStopCur = eMBASCIIStop;
Rajit Singh 0:9db3bed8fffd 158 peMBFrameSendCur = eMBASCIISend;
Rajit Singh 0:9db3bed8fffd 159 peMBFrameReceiveCur = eMBASCIIReceive;
Rajit Singh 0:9db3bed8fffd 160 pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
Rajit Singh 0:9db3bed8fffd 161 pxMBFrameCBByteReceived = xMBASCIIReceiveFSM;
Rajit Singh 0:9db3bed8fffd 162 pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM;
Rajit Singh 0:9db3bed8fffd 163 pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired;
Rajit Singh 0:9db3bed8fffd 164
Rajit Singh 0:9db3bed8fffd 165 eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity );
Rajit Singh 0:9db3bed8fffd 166 break;
Rajit Singh 0:9db3bed8fffd 167 #endif
Rajit Singh 0:9db3bed8fffd 168 default:
Rajit Singh 0:9db3bed8fffd 169 eStatus = MB_EINVAL;
Rajit Singh 0:9db3bed8fffd 170 }
Rajit Singh 0:9db3bed8fffd 171
Rajit Singh 0:9db3bed8fffd 172 if ( eStatus == MB_ENOERR ) {
Rajit Singh 0:9db3bed8fffd 173 if ( !xMBPortEventInit( ) ) {
Rajit Singh 0:9db3bed8fffd 174 /* port dependent event module initalization failed. */
Rajit Singh 0:9db3bed8fffd 175 eStatus = MB_EPORTERR;
Rajit Singh 0:9db3bed8fffd 176 } else {
Rajit Singh 0:9db3bed8fffd 177 eMBCurrentMode = eMode;
Rajit Singh 0:9db3bed8fffd 178 eMBState = STATE_DISABLED;
Rajit Singh 0:9db3bed8fffd 179 }
Rajit Singh 0:9db3bed8fffd 180 }
Rajit Singh 0:9db3bed8fffd 181 }
Rajit Singh 0:9db3bed8fffd 182 return eStatus;
Rajit Singh 0:9db3bed8fffd 183 }
Rajit Singh 0:9db3bed8fffd 184
Rajit Singh 0:9db3bed8fffd 185 #if MB_TCP_ENABLED > 0
Rajit Singh 0:9db3bed8fffd 186 eMBErrorCode
Rajit Singh 0:9db3bed8fffd 187 eMBTCPInit( USHORT ucTCPPort ) {
Rajit Singh 0:9db3bed8fffd 188 eMBErrorCode eStatus = MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 189
Rajit Singh 0:9db3bed8fffd 190 if ( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR ) {
Rajit Singh 0:9db3bed8fffd 191 eMBState = STATE_DISABLED;
Rajit Singh 0:9db3bed8fffd 192 } else if ( !xMBPortEventInit( ) ) {
Rajit Singh 0:9db3bed8fffd 193 /* Port dependent event module initalization failed. */
Rajit Singh 0:9db3bed8fffd 194 eStatus = MB_EPORTERR;
Rajit Singh 0:9db3bed8fffd 195 } else {
Rajit Singh 0:9db3bed8fffd 196 pvMBFrameStartCur = eMBTCPStart;
Rajit Singh 0:9db3bed8fffd 197 pvMBFrameStopCur = eMBTCPStop;
Rajit Singh 0:9db3bed8fffd 198 peMBFrameReceiveCur = eMBTCPReceive;
Rajit Singh 0:9db3bed8fffd 199 peMBFrameSendCur = eMBTCPSend;
Rajit Singh 0:9db3bed8fffd 200 pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL;
Rajit Singh 0:9db3bed8fffd 201 ucMBAddress = MB_TCP_PSEUDO_ADDRESS;
Rajit Singh 0:9db3bed8fffd 202 eMBCurrentMode = MB_TCP;
Rajit Singh 0:9db3bed8fffd 203 eMBState = STATE_DISABLED;
Rajit Singh 0:9db3bed8fffd 204 }
Rajit Singh 0:9db3bed8fffd 205 return eStatus;
Rajit Singh 0:9db3bed8fffd 206 }
Rajit Singh 0:9db3bed8fffd 207 #endif
Rajit Singh 0:9db3bed8fffd 208
Rajit Singh 0:9db3bed8fffd 209
Rajit Singh 0:9db3bed8fffd 210 eMBErrorCode
Rajit Singh 0:9db3bed8fffd 211 eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler ) {
Rajit Singh 0:9db3bed8fffd 212 int i;
Rajit Singh 0:9db3bed8fffd 213 eMBErrorCode eStatus;
Rajit Singh 0:9db3bed8fffd 214
Rajit Singh 0:9db3bed8fffd 215 if ( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) ) {
Rajit Singh 0:9db3bed8fffd 216 ENTER_CRITICAL_SECTION( );
Rajit Singh 0:9db3bed8fffd 217 if ( pxHandler != NULL ) {
Rajit Singh 0:9db3bed8fffd 218 for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) {
Rajit Singh 0:9db3bed8fffd 219 if ( ( xFuncHandlers[i].pxHandler == NULL ) ||
Rajit Singh 0:9db3bed8fffd 220 ( xFuncHandlers[i].pxHandler == pxHandler ) ) {
Rajit Singh 0:9db3bed8fffd 221 xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
Rajit Singh 0:9db3bed8fffd 222 xFuncHandlers[i].pxHandler = pxHandler;
Rajit Singh 0:9db3bed8fffd 223 break;
Rajit Singh 0:9db3bed8fffd 224 }
Rajit Singh 0:9db3bed8fffd 225 }
Rajit Singh 0:9db3bed8fffd 226 eStatus = ( i != MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES;
Rajit Singh 0:9db3bed8fffd 227 } else {
Rajit Singh 0:9db3bed8fffd 228 for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) {
Rajit Singh 0:9db3bed8fffd 229 if ( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) {
Rajit Singh 0:9db3bed8fffd 230 xFuncHandlers[i].ucFunctionCode = 0;
Rajit Singh 0:9db3bed8fffd 231 xFuncHandlers[i].pxHandler = NULL;
Rajit Singh 0:9db3bed8fffd 232 break;
Rajit Singh 0:9db3bed8fffd 233 }
Rajit Singh 0:9db3bed8fffd 234 }
Rajit Singh 0:9db3bed8fffd 235 /* Remove can't fail. */
Rajit Singh 0:9db3bed8fffd 236 eStatus = MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 237 }
Rajit Singh 0:9db3bed8fffd 238 EXIT_CRITICAL_SECTION( );
Rajit Singh 0:9db3bed8fffd 239 } else {
Rajit Singh 0:9db3bed8fffd 240 eStatus = MB_EINVAL;
Rajit Singh 0:9db3bed8fffd 241 }
Rajit Singh 0:9db3bed8fffd 242 return eStatus;
Rajit Singh 0:9db3bed8fffd 243 }
Rajit Singh 0:9db3bed8fffd 244
Rajit Singh 0:9db3bed8fffd 245
Rajit Singh 0:9db3bed8fffd 246 eMBErrorCode
Rajit Singh 0:9db3bed8fffd 247 eMBClose( void ) {
Rajit Singh 0:9db3bed8fffd 248 eMBErrorCode eStatus = MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 249
Rajit Singh 0:9db3bed8fffd 250 if ( eMBState == STATE_DISABLED ) {
Rajit Singh 0:9db3bed8fffd 251 if ( pvMBFrameCloseCur != NULL ) {
Rajit Singh 0:9db3bed8fffd 252 pvMBFrameCloseCur( );
Rajit Singh 0:9db3bed8fffd 253 }
Rajit Singh 0:9db3bed8fffd 254 } else {
Rajit Singh 0:9db3bed8fffd 255 eStatus = MB_EILLSTATE;
Rajit Singh 0:9db3bed8fffd 256 }
Rajit Singh 0:9db3bed8fffd 257 return eStatus;
Rajit Singh 0:9db3bed8fffd 258 }
Rajit Singh 0:9db3bed8fffd 259
Rajit Singh 0:9db3bed8fffd 260 eMBErrorCode
Rajit Singh 0:9db3bed8fffd 261 eMBEnable( void ) {
Rajit Singh 0:9db3bed8fffd 262 eMBErrorCode eStatus = MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 263
Rajit Singh 0:9db3bed8fffd 264 if ( eMBState == STATE_DISABLED ) {
Rajit Singh 0:9db3bed8fffd 265 /* Activate the protocol stack. */
Rajit Singh 0:9db3bed8fffd 266 pvMBFrameStartCur( );
Rajit Singh 0:9db3bed8fffd 267 eMBState = STATE_ENABLED;
Rajit Singh 0:9db3bed8fffd 268 } else {
Rajit Singh 0:9db3bed8fffd 269 eStatus = MB_EILLSTATE;
Rajit Singh 0:9db3bed8fffd 270 }
Rajit Singh 0:9db3bed8fffd 271 return eStatus;
Rajit Singh 0:9db3bed8fffd 272 }
Rajit Singh 0:9db3bed8fffd 273
Rajit Singh 0:9db3bed8fffd 274 eMBErrorCode
Rajit Singh 0:9db3bed8fffd 275 eMBDisable( void ) {
Rajit Singh 0:9db3bed8fffd 276 eMBErrorCode eStatus;
Rajit Singh 0:9db3bed8fffd 277
Rajit Singh 0:9db3bed8fffd 278 if ( eMBState == STATE_ENABLED ) {
Rajit Singh 0:9db3bed8fffd 279 pvMBFrameStopCur( );
Rajit Singh 0:9db3bed8fffd 280 eMBState = STATE_DISABLED;
Rajit Singh 0:9db3bed8fffd 281 eStatus = MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 282 } else if ( eMBState == STATE_DISABLED ) {
Rajit Singh 0:9db3bed8fffd 283 eStatus = MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 284 } else {
Rajit Singh 0:9db3bed8fffd 285 eStatus = MB_EILLSTATE;
Rajit Singh 0:9db3bed8fffd 286 }
Rajit Singh 0:9db3bed8fffd 287 return eStatus;
Rajit Singh 0:9db3bed8fffd 288 }
Rajit Singh 0:9db3bed8fffd 289
Rajit Singh 0:9db3bed8fffd 290 eMBErrorCode
Rajit Singh 0:9db3bed8fffd 291 eMBPoll( void ) {
Rajit Singh 0:9db3bed8fffd 292 static UCHAR *ucMBFrame;
Rajit Singh 0:9db3bed8fffd 293 static UCHAR ucRcvAddress;
Rajit Singh 0:9db3bed8fffd 294 static UCHAR ucFunctionCode;
Rajit Singh 0:9db3bed8fffd 295 static USHORT usLength;
Rajit Singh 0:9db3bed8fffd 296 static eMBException eException;
Rajit Singh 0:9db3bed8fffd 297
Rajit Singh 0:9db3bed8fffd 298 int i;
Rajit Singh 0:9db3bed8fffd 299 eMBErrorCode eStatus = MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 300 eMBEventType eEvent;
Rajit Singh 0:9db3bed8fffd 301
Rajit Singh 0:9db3bed8fffd 302 /* Check if the protocol stack is ready. */
Rajit Singh 0:9db3bed8fffd 303 if ( eMBState != STATE_ENABLED ) {
Rajit Singh 0:9db3bed8fffd 304 return MB_EILLSTATE;
Rajit Singh 0:9db3bed8fffd 305 }
Rajit Singh 0:9db3bed8fffd 306
Rajit Singh 0:9db3bed8fffd 307 /* Check if there is a event available. If not return control to caller.
Rajit Singh 0:9db3bed8fffd 308 * Otherwise we will handle the event. */
Rajit Singh 0:9db3bed8fffd 309 if ( xMBPortEventGet( &eEvent ) == TRUE ) {
Rajit Singh 0:9db3bed8fffd 310 switch ( eEvent ) {
Rajit Singh 0:9db3bed8fffd 311 case EV_READY:
Rajit Singh 0:9db3bed8fffd 312 break;
Rajit Singh 0:9db3bed8fffd 313
Rajit Singh 0:9db3bed8fffd 314 case EV_FRAME_RECEIVED:
Rajit Singh 0:9db3bed8fffd 315 eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
Rajit Singh 0:9db3bed8fffd 316 if ( eStatus == MB_ENOERR ) {
Rajit Singh 0:9db3bed8fffd 317 /* Check if the frame is for us. If not ignore the frame. */
Rajit Singh 0:9db3bed8fffd 318 if ( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) ) {
Rajit Singh 0:9db3bed8fffd 319 ( void )xMBPortEventPost( EV_EXECUTE );
Rajit Singh 0:9db3bed8fffd 320 }
Rajit Singh 0:9db3bed8fffd 321 }
Rajit Singh 0:9db3bed8fffd 322 break;
Rajit Singh 0:9db3bed8fffd 323
Rajit Singh 0:9db3bed8fffd 324 case EV_EXECUTE:
Rajit Singh 0:9db3bed8fffd 325 ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
Rajit Singh 0:9db3bed8fffd 326 eException = MB_EX_ILLEGAL_FUNCTION;
Rajit Singh 0:9db3bed8fffd 327
Rajit Singh 0:9db3bed8fffd 328
Rajit Singh 0:9db3bed8fffd 329 for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) {
Rajit Singh 0:9db3bed8fffd 330 /* No more function handlers registered. Abort. */
Rajit Singh 0:9db3bed8fffd 331 if ( xFuncHandlers[i].ucFunctionCode == 0 ) {
Rajit Singh 0:9db3bed8fffd 332 break;
Rajit Singh 0:9db3bed8fffd 333 } else if ( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ) {
Rajit Singh 0:9db3bed8fffd 334 eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
Rajit Singh 0:9db3bed8fffd 335 break;
Rajit Singh 0:9db3bed8fffd 336 }
Rajit Singh 0:9db3bed8fffd 337 }
Rajit Singh 0:9db3bed8fffd 338
Rajit Singh 0:9db3bed8fffd 339 /* If the request was not sent to the broadcast address we
Rajit Singh 0:9db3bed8fffd 340 * return a reply. */
Rajit Singh 0:9db3bed8fffd 341 if ( ucRcvAddress != MB_ADDRESS_BROADCAST ) {
Rajit Singh 0:9db3bed8fffd 342 if ( eException != MB_EX_NONE ) {
Rajit Singh 0:9db3bed8fffd 343 /* An exception occured. Build an error frame. */
Rajit Singh 0:9db3bed8fffd 344 usLength = 0;
Rajit Singh 0:9db3bed8fffd 345 ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
Rajit Singh 0:9db3bed8fffd 346 ucMBFrame[usLength++] = eException;
Rajit Singh 0:9db3bed8fffd 347 }
Rajit Singh 0:9db3bed8fffd 348 eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
Rajit Singh 0:9db3bed8fffd 349 }
Rajit Singh 0:9db3bed8fffd 350 break;
Rajit Singh 0:9db3bed8fffd 351
Rajit Singh 0:9db3bed8fffd 352 case EV_FRAME_SENT:
Rajit Singh 0:9db3bed8fffd 353 break;
Rajit Singh 0:9db3bed8fffd 354 }
Rajit Singh 0:9db3bed8fffd 355 }
Rajit Singh 0:9db3bed8fffd 356 return MB_ENOERR;
Rajit Singh 0:9db3bed8fffd 357 }