modified for nuc472

Dependents:   modbus-over-rs485-sample NTOUEE-mbed-modbus-RTU NuMaker_NuWicam_Lite

Fork of Modbus by Matthew Waddilove

Committer:
wclin
Date:
Thu Aug 18 08:24:37 2016 +0000
Revision:
2:6ee56c002f64
Parent:
0:274eb57e1df3
Child:
3:419ee4c5e10f
Fix buffer overflow issue.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giryan 0:274eb57e1df3 1 /*
giryan 0:274eb57e1df3 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
giryan 0:274eb57e1df3 3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
giryan 0:274eb57e1df3 4 * All rights reserved.
giryan 0:274eb57e1df3 5 *
giryan 0:274eb57e1df3 6 * Redistribution and use in source and binary forms, with or without
giryan 0:274eb57e1df3 7 * modification, are permitted provided that the following conditions
giryan 0:274eb57e1df3 8 * are met:
giryan 0:274eb57e1df3 9 * 1. Redistributions of source code must retain the above copyright
giryan 0:274eb57e1df3 10 * notice, this list of conditions and the following disclaimer.
giryan 0:274eb57e1df3 11 * 2. Redistributions in binary form must reproduce the above copyright
giryan 0:274eb57e1df3 12 * notice, this list of conditions and the following disclaimer in the
giryan 0:274eb57e1df3 13 * documentation and/or other materials provided with the distribution.
giryan 0:274eb57e1df3 14 * 3. The name of the author may not be used to endorse or promote products
giryan 0:274eb57e1df3 15 * derived from this software without specific prior written permission.
giryan 0:274eb57e1df3 16 *
giryan 0:274eb57e1df3 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
giryan 0:274eb57e1df3 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
giryan 0:274eb57e1df3 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
giryan 0:274eb57e1df3 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
giryan 0:274eb57e1df3 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
giryan 0:274eb57e1df3 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
giryan 0:274eb57e1df3 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
giryan 0:274eb57e1df3 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
giryan 0:274eb57e1df3 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
giryan 0:274eb57e1df3 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giryan 0:274eb57e1df3 27 *
giryan 0:274eb57e1df3 28 * File: $Id: mbrtu.c,v 1.18 2007/09/12 10:15:56 wolti Exp $
giryan 0:274eb57e1df3 29 */
wclin 2:6ee56c002f64 30 #include "mbed.h"
giryan 0:274eb57e1df3 31
giryan 0:274eb57e1df3 32 /* ----------------------- System includes ----------------------------------*/
giryan 0:274eb57e1df3 33 #include "stdlib.h"
giryan 0:274eb57e1df3 34 #include "string.h"
giryan 0:274eb57e1df3 35
giryan 0:274eb57e1df3 36 /* ----------------------- Platform includes --------------------------------*/
giryan 0:274eb57e1df3 37 #include "port.h"
giryan 0:274eb57e1df3 38
giryan 0:274eb57e1df3 39 /* ----------------------- Modbus includes ----------------------------------*/
giryan 0:274eb57e1df3 40 #include "mb.h"
giryan 0:274eb57e1df3 41 #include "mbrtu.h"
giryan 0:274eb57e1df3 42 #include "mbframe.h"
giryan 0:274eb57e1df3 43
giryan 0:274eb57e1df3 44 #include "mbcrc.h"
giryan 0:274eb57e1df3 45 #include "mbport.h"
giryan 0:274eb57e1df3 46
giryan 0:274eb57e1df3 47 /* ----------------------- Defines ------------------------------------------*/
giryan 0:274eb57e1df3 48 #define MB_SER_PDU_SIZE_MIN 4 /*!< Minimum size of a Modbus RTU frame. */
giryan 0:274eb57e1df3 49 #define MB_SER_PDU_SIZE_MAX 256 /*!< Maximum size of a Modbus RTU frame. */
giryan 0:274eb57e1df3 50 #define MB_SER_PDU_SIZE_CRC 2 /*!< Size of CRC field in PDU. */
giryan 0:274eb57e1df3 51 #define MB_SER_PDU_ADDR_OFF 0 /*!< Offset of slave address in Ser-PDU. */
giryan 0:274eb57e1df3 52 #define MB_SER_PDU_PDU_OFF 1 /*!< Offset of Modbus-PDU in Ser-PDU. */
giryan 0:274eb57e1df3 53
giryan 0:274eb57e1df3 54 /* ----------------------- Type definitions ---------------------------------*/
giryan 0:274eb57e1df3 55 typedef enum
giryan 0:274eb57e1df3 56 {
giryan 0:274eb57e1df3 57 STATE_RX_INIT, /*!< Receiver is in initial state. */
giryan 0:274eb57e1df3 58 STATE_RX_IDLE, /*!< Receiver is in idle state. */
giryan 0:274eb57e1df3 59 STATE_RX_RCV, /*!< Frame is beeing received. */
giryan 0:274eb57e1df3 60 STATE_RX_ERROR /*!< If the frame is invalid. */
giryan 0:274eb57e1df3 61 } eMBRcvState;
giryan 0:274eb57e1df3 62
giryan 0:274eb57e1df3 63 typedef enum
giryan 0:274eb57e1df3 64 {
giryan 0:274eb57e1df3 65 STATE_TX_IDLE, /*!< Transmitter is in idle state. */
giryan 0:274eb57e1df3 66 STATE_TX_XMIT /*!< Transmitter is in transfer state. */
giryan 0:274eb57e1df3 67 } eMBSndState;
giryan 0:274eb57e1df3 68
giryan 0:274eb57e1df3 69 /* ----------------------- Static variables ---------------------------------*/
giryan 0:274eb57e1df3 70 static volatile eMBSndState eSndState;
giryan 0:274eb57e1df3 71 static volatile eMBRcvState eRcvState;
giryan 0:274eb57e1df3 72
giryan 0:274eb57e1df3 73 volatile UCHAR ucRTUBuf[MB_SER_PDU_SIZE_MAX];
giryan 0:274eb57e1df3 74
giryan 0:274eb57e1df3 75 static volatile UCHAR *pucSndBufferCur;
giryan 0:274eb57e1df3 76 static volatile USHORT usSndBufferCount;
giryan 0:274eb57e1df3 77
giryan 0:274eb57e1df3 78 static volatile USHORT usRcvBufferPos;
giryan 0:274eb57e1df3 79
giryan 0:274eb57e1df3 80 /* ----------------------- Start implementation -----------------------------*/
giryan 0:274eb57e1df3 81 eMBErrorCode
giryan 0:274eb57e1df3 82 eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
giryan 0:274eb57e1df3 83 {
giryan 0:274eb57e1df3 84 eMBErrorCode eStatus = MB_ENOERR;
giryan 0:274eb57e1df3 85 ULONG usTimerT35_50us;
giryan 0:274eb57e1df3 86
giryan 0:274eb57e1df3 87 ( void )ucSlaveAddress;
giryan 0:274eb57e1df3 88 ENTER_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 89
giryan 0:274eb57e1df3 90 /* Modbus RTU uses 8 Databits. */
giryan 0:274eb57e1df3 91 if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
giryan 0:274eb57e1df3 92 {
giryan 0:274eb57e1df3 93 eStatus = MB_EPORTERR;
giryan 0:274eb57e1df3 94 }
giryan 0:274eb57e1df3 95 else
giryan 0:274eb57e1df3 96 {
giryan 0:274eb57e1df3 97 /* If baudrate > 19200 then we should use the fixed timer values
giryan 0:274eb57e1df3 98 * t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
giryan 0:274eb57e1df3 99 */
giryan 0:274eb57e1df3 100 if( ulBaudRate > 19200 )
giryan 0:274eb57e1df3 101 {
giryan 0:274eb57e1df3 102 usTimerT35_50us = 35; /* 1800us. */
giryan 0:274eb57e1df3 103 }
giryan 0:274eb57e1df3 104 else
giryan 0:274eb57e1df3 105 {
giryan 0:274eb57e1df3 106 /* The timer reload value for a character is given by:
giryan 0:274eb57e1df3 107 *
giryan 0:274eb57e1df3 108 * ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
giryan 0:274eb57e1df3 109 * = 11 * Ticks_per_1s / Baudrate
giryan 0:274eb57e1df3 110 * = 220000 / Baudrate
giryan 0:274eb57e1df3 111 * The reload for t3.5 is 1.5 times this value and similary
giryan 0:274eb57e1df3 112 * for t3.5.
giryan 0:274eb57e1df3 113 */
giryan 0:274eb57e1df3 114 usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
giryan 0:274eb57e1df3 115 }
giryan 0:274eb57e1df3 116 if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
giryan 0:274eb57e1df3 117 {
giryan 0:274eb57e1df3 118 eStatus = MB_EPORTERR;
giryan 0:274eb57e1df3 119 }
giryan 0:274eb57e1df3 120 }
giryan 0:274eb57e1df3 121 EXIT_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 122
giryan 0:274eb57e1df3 123 return eStatus;
giryan 0:274eb57e1df3 124 }
giryan 0:274eb57e1df3 125
giryan 0:274eb57e1df3 126 void
giryan 0:274eb57e1df3 127 eMBRTUStart( void )
giryan 0:274eb57e1df3 128 {
giryan 0:274eb57e1df3 129 ENTER_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 130 /* Initially the receiver is in the state STATE_RX_INIT. we start
giryan 0:274eb57e1df3 131 * the timer and if no character is received within t3.5 we change
giryan 0:274eb57e1df3 132 * to STATE_RX_IDLE. This makes sure that we delay startup of the
giryan 0:274eb57e1df3 133 * modbus protocol stack until the bus is free.
giryan 0:274eb57e1df3 134 */
giryan 0:274eb57e1df3 135 eRcvState = STATE_RX_INIT;
giryan 0:274eb57e1df3 136 vMBPortSerialEnable( TRUE, FALSE );
giryan 0:274eb57e1df3 137 vMBPortTimersEnable( );
giryan 0:274eb57e1df3 138 EXIT_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 139 }
giryan 0:274eb57e1df3 140
giryan 0:274eb57e1df3 141 void
giryan 0:274eb57e1df3 142 eMBRTUStop( void )
giryan 0:274eb57e1df3 143 {
giryan 0:274eb57e1df3 144 ENTER_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 145 vMBPortSerialEnable( FALSE, FALSE );
giryan 0:274eb57e1df3 146 vMBPortTimersDisable( );
giryan 0:274eb57e1df3 147 EXIT_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 148 }
giryan 0:274eb57e1df3 149
giryan 0:274eb57e1df3 150 eMBErrorCode
giryan 0:274eb57e1df3 151 eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
giryan 0:274eb57e1df3 152 {
giryan 0:274eb57e1df3 153 BOOL xFrameReceived = FALSE;
giryan 0:274eb57e1df3 154 eMBErrorCode eStatus = MB_ENOERR;
giryan 0:274eb57e1df3 155
giryan 0:274eb57e1df3 156 ENTER_CRITICAL_SECTION( );
wclin 2:6ee56c002f64 157 //assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX ); //Wayne's workaround
wclin 2:6ee56c002f64 158 if ( usRcvBufferPos >= MB_SER_PDU_SIZE_MAX )
wclin 2:6ee56c002f64 159 {
wclin 2:6ee56c002f64 160 //printf("usRcvBufferPos < MB_SER_PDU_SIZE_MAX - %d %d \r\n", usRcvBufferPos, MB_SER_PDU_SIZE_MAX );
wclin 2:6ee56c002f64 161 eStatus = MB_EIO;
wclin 2:6ee56c002f64 162 usRcvBufferPos = 0;
wclin 2:6ee56c002f64 163 return eStatus;
wclin 2:6ee56c002f64 164 }
giryan 0:274eb57e1df3 165
giryan 0:274eb57e1df3 166 /* Length and CRC check */
giryan 0:274eb57e1df3 167 if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
giryan 0:274eb57e1df3 168 && ( usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )
giryan 0:274eb57e1df3 169 {
giryan 0:274eb57e1df3 170 /* Save the address field. All frames are passed to the upper layer
giryan 0:274eb57e1df3 171 * and the decision if a frame is used is done there.
giryan 0:274eb57e1df3 172 */
giryan 0:274eb57e1df3 173 *pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];
giryan 0:274eb57e1df3 174
giryan 0:274eb57e1df3 175 /* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
giryan 0:274eb57e1df3 176 * size of address field and CRC checksum.
giryan 0:274eb57e1df3 177 */
giryan 0:274eb57e1df3 178 *pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC );
giryan 0:274eb57e1df3 179
giryan 0:274eb57e1df3 180 /* Return the start of the Modbus PDU to the caller. */
giryan 0:274eb57e1df3 181 *pucFrame = ( UCHAR * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
giryan 0:274eb57e1df3 182 xFrameReceived = TRUE;
giryan 0:274eb57e1df3 183
giryan 0:274eb57e1df3 184 // Added by Cam
giryan 0:274eb57e1df3 185 // Now that the poll routine knows about the received frame,
giryan 0:274eb57e1df3 186 // clear the receive buffer position ready for the next frame received
giryan 0:274eb57e1df3 187 usRcvBufferPos = 0;
giryan 0:274eb57e1df3 188
giryan 0:274eb57e1df3 189 }
giryan 0:274eb57e1df3 190 else
giryan 0:274eb57e1df3 191 {
giryan 0:274eb57e1df3 192 eStatus = MB_EIO;
giryan 0:274eb57e1df3 193 }
giryan 0:274eb57e1df3 194
giryan 0:274eb57e1df3 195 EXIT_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 196 return eStatus;
giryan 0:274eb57e1df3 197 }
giryan 0:274eb57e1df3 198
giryan 0:274eb57e1df3 199 eMBErrorCode
giryan 0:274eb57e1df3 200 eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
giryan 0:274eb57e1df3 201 {
giryan 0:274eb57e1df3 202 eMBErrorCode eStatus = MB_ENOERR;
giryan 0:274eb57e1df3 203 USHORT usCRC16;
giryan 0:274eb57e1df3 204
giryan 0:274eb57e1df3 205 ENTER_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 206
giryan 0:274eb57e1df3 207 /* Check if the receiver is still in idle state. If not we where to
giryan 0:274eb57e1df3 208 * slow with processing the received frame and the master sent another
giryan 0:274eb57e1df3 209 * frame on the network. We have to abort sending the frame.
giryan 0:274eb57e1df3 210 */
giryan 0:274eb57e1df3 211 if( eRcvState == STATE_RX_IDLE )
giryan 0:274eb57e1df3 212 {
giryan 0:274eb57e1df3 213 /* First byte before the Modbus-PDU is the slave address. */
giryan 0:274eb57e1df3 214 pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
giryan 0:274eb57e1df3 215 usSndBufferCount = 1;
giryan 0:274eb57e1df3 216
giryan 0:274eb57e1df3 217 /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
giryan 0:274eb57e1df3 218 pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
giryan 0:274eb57e1df3 219 usSndBufferCount += usLength;
giryan 0:274eb57e1df3 220
giryan 0:274eb57e1df3 221 /* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
giryan 0:274eb57e1df3 222 usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
giryan 0:274eb57e1df3 223 ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
giryan 0:274eb57e1df3 224 ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
giryan 0:274eb57e1df3 225
giryan 0:274eb57e1df3 226 /* Activate the transmitter. */
giryan 0:274eb57e1df3 227 eSndState = STATE_TX_XMIT;
giryan 0:274eb57e1df3 228 vMBPortSerialEnable( FALSE, TRUE );
giryan 0:274eb57e1df3 229 }
giryan 0:274eb57e1df3 230 else
giryan 0:274eb57e1df3 231 {
giryan 0:274eb57e1df3 232 eStatus = MB_EIO;
giryan 0:274eb57e1df3 233 }
giryan 0:274eb57e1df3 234 EXIT_CRITICAL_SECTION( );
giryan 0:274eb57e1df3 235 return eStatus;
giryan 0:274eb57e1df3 236 }
giryan 0:274eb57e1df3 237
giryan 0:274eb57e1df3 238 BOOL
giryan 0:274eb57e1df3 239 xMBRTUReceiveFSM( void )
giryan 0:274eb57e1df3 240 {
giryan 0:274eb57e1df3 241 BOOL xTaskNeedSwitch = FALSE;
giryan 0:274eb57e1df3 242 UCHAR ucByte;
giryan 0:274eb57e1df3 243
giryan 0:274eb57e1df3 244 assert( eSndState == STATE_TX_IDLE );
giryan 0:274eb57e1df3 245
giryan 0:274eb57e1df3 246 /* Always read the character. */
giryan 0:274eb57e1df3 247 ( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
giryan 0:274eb57e1df3 248
giryan 0:274eb57e1df3 249 switch ( eRcvState )
giryan 0:274eb57e1df3 250 {
giryan 0:274eb57e1df3 251 /* If we have received a character in the init state we have to
giryan 0:274eb57e1df3 252 * wait until the frame is finished.
giryan 0:274eb57e1df3 253 */
giryan 0:274eb57e1df3 254 case STATE_RX_INIT:
giryan 0:274eb57e1df3 255 vMBPortTimersEnable( );
giryan 0:274eb57e1df3 256 break;
giryan 0:274eb57e1df3 257
giryan 0:274eb57e1df3 258 /* In the error state we wait until all characters in the
giryan 0:274eb57e1df3 259 * damaged frame are transmitted.
giryan 0:274eb57e1df3 260 */
giryan 0:274eb57e1df3 261 case STATE_RX_ERROR:
giryan 0:274eb57e1df3 262 vMBPortTimersEnable( );
giryan 0:274eb57e1df3 263 break;
giryan 0:274eb57e1df3 264
giryan 0:274eb57e1df3 265 /* In the idle state we wait for a new character. If a character
giryan 0:274eb57e1df3 266 * is received the t1.5 and t3.5 timers are started and the
giryan 0:274eb57e1df3 267 * receiver is in the state STATE_RX_RECEIVE.
giryan 0:274eb57e1df3 268 */
giryan 0:274eb57e1df3 269 case STATE_RX_IDLE:
giryan 0:274eb57e1df3 270 ucRTUBuf[usRcvBufferPos++] = ucByte;
giryan 0:274eb57e1df3 271 eRcvState = STATE_RX_RCV;
giryan 0:274eb57e1df3 272
giryan 0:274eb57e1df3 273 /* Enable t3.5 timers. */
giryan 0:274eb57e1df3 274 vMBPortTimersEnable( );
giryan 0:274eb57e1df3 275 break;
giryan 0:274eb57e1df3 276
giryan 0:274eb57e1df3 277 /* We are currently receiving a frame. Reset the timer after
giryan 0:274eb57e1df3 278 * every character received. If more than the maximum possible
giryan 0:274eb57e1df3 279 * number of bytes in a modbus frame is received the frame is
giryan 0:274eb57e1df3 280 * ignored.
giryan 0:274eb57e1df3 281 */
giryan 0:274eb57e1df3 282 case STATE_RX_RCV:
giryan 0:274eb57e1df3 283 if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
giryan 0:274eb57e1df3 284 {
giryan 0:274eb57e1df3 285 ucRTUBuf[usRcvBufferPos++] = ucByte;
giryan 0:274eb57e1df3 286 }
giryan 0:274eb57e1df3 287 else
giryan 0:274eb57e1df3 288 {
giryan 0:274eb57e1df3 289 eRcvState = STATE_RX_ERROR;
giryan 0:274eb57e1df3 290 }
giryan 0:274eb57e1df3 291 vMBPortTimersEnable( );
giryan 0:274eb57e1df3 292 break;
giryan 0:274eb57e1df3 293 }
giryan 0:274eb57e1df3 294 return xTaskNeedSwitch;
giryan 0:274eb57e1df3 295 }
giryan 0:274eb57e1df3 296
giryan 0:274eb57e1df3 297 BOOL
giryan 0:274eb57e1df3 298 xMBRTUTransmitFSM( void )
giryan 0:274eb57e1df3 299 {
giryan 0:274eb57e1df3 300 BOOL xNeedPoll = FALSE;
giryan 0:274eb57e1df3 301
giryan 0:274eb57e1df3 302 assert( eRcvState == STATE_RX_IDLE );
giryan 0:274eb57e1df3 303 switch ( eSndState )
giryan 0:274eb57e1df3 304 {
giryan 0:274eb57e1df3 305 /* We should not get a transmitter event if the transmitter is in
giryan 0:274eb57e1df3 306 * idle state. */
giryan 0:274eb57e1df3 307 case STATE_TX_IDLE:
giryan 0:274eb57e1df3 308 /* enable receiver/disable transmitter. */
giryan 0:274eb57e1df3 309 vMBPortSerialEnable( TRUE, FALSE );
giryan 0:274eb57e1df3 310 break;
giryan 0:274eb57e1df3 311
giryan 0:274eb57e1df3 312 case STATE_TX_XMIT:
giryan 0:274eb57e1df3 313 /* check if we are finished. */
giryan 0:274eb57e1df3 314 if( usSndBufferCount != 0 )
giryan 0:274eb57e1df3 315 {
giryan 0:274eb57e1df3 316 xMBPortSerialPutByte( ( CHAR )*pucSndBufferCur );
giryan 0:274eb57e1df3 317 pucSndBufferCur++; /* next byte in sendbuffer. */
giryan 0:274eb57e1df3 318 usSndBufferCount--;
giryan 0:274eb57e1df3 319 }
giryan 0:274eb57e1df3 320 else
giryan 0:274eb57e1df3 321 {
giryan 0:274eb57e1df3 322 xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
giryan 0:274eb57e1df3 323 /* Disable transmitter. This prevents another transmit buffer
giryan 0:274eb57e1df3 324 * empty interrupt. */
giryan 0:274eb57e1df3 325 vMBPortSerialEnable( TRUE, FALSE );
giryan 0:274eb57e1df3 326 eSndState = STATE_TX_IDLE;
giryan 0:274eb57e1df3 327 }
giryan 0:274eb57e1df3 328 break;
giryan 0:274eb57e1df3 329 }
giryan 0:274eb57e1df3 330
giryan 0:274eb57e1df3 331 return xNeedPoll;
giryan 0:274eb57e1df3 332 }
giryan 0:274eb57e1df3 333
giryan 0:274eb57e1df3 334 BOOL
giryan 0:274eb57e1df3 335 xMBRTUTimerT35Expired( void )
giryan 0:274eb57e1df3 336 {
giryan 0:274eb57e1df3 337 BOOL xNeedPoll = FALSE;
giryan 0:274eb57e1df3 338
giryan 0:274eb57e1df3 339 switch ( eRcvState )
giryan 0:274eb57e1df3 340 {
giryan 0:274eb57e1df3 341 /* Timer t35 expired. Startup phase is finished. */
giryan 0:274eb57e1df3 342 case STATE_RX_INIT:
giryan 0:274eb57e1df3 343 xNeedPoll = xMBPortEventPost( EV_READY );
giryan 0:274eb57e1df3 344 break;
giryan 0:274eb57e1df3 345
giryan 0:274eb57e1df3 346 /* A frame was received and t35 expired. Notify the listener that
giryan 0:274eb57e1df3 347 * a new frame was received. */
giryan 0:274eb57e1df3 348 case STATE_RX_RCV:
giryan 0:274eb57e1df3 349 xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED );
giryan 0:274eb57e1df3 350 break;
giryan 0:274eb57e1df3 351
giryan 0:274eb57e1df3 352 /* An error occured while receiving the frame. */
giryan 0:274eb57e1df3 353 case STATE_RX_ERROR:
giryan 0:274eb57e1df3 354 break;
giryan 0:274eb57e1df3 355
giryan 0:274eb57e1df3 356 /* Function called in an illegal state. */
giryan 0:274eb57e1df3 357 default:
giryan 0:274eb57e1df3 358 assert( ( eRcvState == STATE_RX_INIT ) ||
giryan 0:274eb57e1df3 359 ( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_ERROR ) );
giryan 0:274eb57e1df3 360 }
giryan 0:274eb57e1df3 361
giryan 0:274eb57e1df3 362 vMBPortTimersDisable( );
giryan 0:274eb57e1df3 363 eRcvState = STATE_RX_IDLE;
giryan 0:274eb57e1df3 364
giryan 0:274eb57e1df3 365 return xNeedPoll;
giryan 0:274eb57e1df3 366 }