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