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