summaryrefslogtreecommitdiff
path: root/apps/modbus/rtu
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-07-21 13:35:35 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-07-21 13:35:35 +0000
commit1dc0e8b8c5b6e03fcfcdcbc89efd512f934eaca5 (patch)
tree646977318abbfe8b9b08885db68bda1103428686 /apps/modbus/rtu
parentec9c693a8fbabde333c2a022c12476a12ca5f9b8 (diff)
downloadnuttx-1dc0e8b8c5b6e03fcfcdcbc89efd512f934eaca5.tar.gz
nuttx-1dc0e8b8c5b6e03fcfcdcbc89efd512f934eaca5.tar.bz2
nuttx-1dc0e8b8c5b6e03fcfcdcbc89efd512f934eaca5.zip
Check-in of initial, unmodified freemodbus-v1.5.0
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4960 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'apps/modbus/rtu')
-rw-r--r--apps/modbus/rtu/mbcrc.c98
-rw-r--r--apps/modbus/rtu/mbcrc.h36
-rw-r--r--apps/modbus/rtu/mbrtu.c355
-rw-r--r--apps/modbus/rtu/mbrtu.h51
4 files changed, 540 insertions, 0 deletions
diff --git a/apps/modbus/rtu/mbcrc.c b/apps/modbus/rtu/mbcrc.c
new file mode 100644
index 000000000..29b9ea765
--- /dev/null
+++ b/apps/modbus/rtu/mbcrc.c
@@ -0,0 +1,98 @@
+/*
+ * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
+ * 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.
+ *
+ * File: $Id: mbcrc.c,v 1.7 2007/02/18 23:50:27 wolti Exp $
+ */
+
+/* ----------------------- Platform includes --------------------------------*/
+#include "port.h"
+
+static const UCHAR aucCRCHi[] = {
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+ 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+ 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+ 0x00, 0xC1, 0x81, 0x40
+};
+
+static const UCHAR aucCRCLo[] = {
+ 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
+ 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
+ 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
+ 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
+ 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
+ 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
+ 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
+ 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
+ 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
+ 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
+ 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
+ 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
+ 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
+ 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
+ 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
+ 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
+ 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
+ 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
+ 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
+ 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
+ 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
+ 0x41, 0x81, 0x80, 0x40
+};
+
+USHORT
+usMBCRC16( UCHAR * pucFrame, USHORT usLen )
+{
+ UCHAR ucCRCHi = 0xFF;
+ UCHAR ucCRCLo = 0xFF;
+ int iIndex;
+
+ while( usLen-- )
+ {
+ iIndex = ucCRCLo ^ *( pucFrame++ );
+ ucCRCLo = ( UCHAR )( ucCRCHi ^ aucCRCHi[iIndex] );
+ ucCRCHi = aucCRCLo[iIndex];
+ }
+ return ( USHORT )( ucCRCHi << 8 | ucCRCLo );
+}
diff --git a/apps/modbus/rtu/mbcrc.h b/apps/modbus/rtu/mbcrc.h
new file mode 100644
index 000000000..db227763f
--- /dev/null
+++ b/apps/modbus/rtu/mbcrc.h
@@ -0,0 +1,36 @@
+/*
+ * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
+ * 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.
+ *
+ * File: $Id: mbcrc.h,v 1.5 2006/12/07 22:10:34 wolti Exp $
+ */
+
+#ifndef _MB_CRC_H
+#define _MB_CRC_H
+
+USHORT usMBCRC16( UCHAR * pucFrame, USHORT usLen );
+
+#endif
diff --git a/apps/modbus/rtu/mbrtu.c b/apps/modbus/rtu/mbrtu.c
new file mode 100644
index 000000000..fc9b854ff
--- /dev/null
+++ b/apps/modbus/rtu/mbrtu.c
@@ -0,0 +1,355 @@
+/*
+ * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
+ * 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.
+ *
+ * File: $Id: mbrtu.c,v 1.18 2007/09/12 10:15:56 wolti Exp $
+ */
+
+/* ----------------------- System includes ----------------------------------*/
+#include "stdlib.h"
+#include "string.h"
+
+/* ----------------------- Platform includes --------------------------------*/
+#include "port.h"
+
+/* ----------------------- Modbus includes ----------------------------------*/
+#include "mb.h"
+#include "mbrtu.h"
+#include "mbframe.h"
+
+#include "mbcrc.h"
+#include "mbport.h"
+
+/* ----------------------- Defines ------------------------------------------*/
+#define MB_SER_PDU_SIZE_MIN 4 /*!< Minimum size of a Modbus RTU frame. */
+#define MB_SER_PDU_SIZE_MAX 256 /*!< Maximum size of a Modbus RTU frame. */
+#define MB_SER_PDU_SIZE_CRC 2 /*!< Size of CRC field in PDU. */
+#define MB_SER_PDU_ADDR_OFF 0 /*!< Offset of slave address in Ser-PDU. */
+#define MB_SER_PDU_PDU_OFF 1 /*!< Offset of Modbus-PDU in Ser-PDU. */
+
+/* ----------------------- Type definitions ---------------------------------*/
+typedef enum
+{
+ STATE_RX_INIT, /*!< Receiver is in initial state. */
+ STATE_RX_IDLE, /*!< Receiver is in idle state. */
+ STATE_RX_RCV, /*!< Frame is beeing received. */
+ STATE_RX_ERROR /*!< If the frame is invalid. */
+} eMBRcvState;
+
+typedef enum
+{
+ STATE_TX_IDLE, /*!< Transmitter is in idle state. */
+ STATE_TX_XMIT /*!< Transmitter is in transfer state. */
+} eMBSndState;
+
+/* ----------------------- Static variables ---------------------------------*/
+static volatile eMBSndState eSndState;
+static volatile eMBRcvState eRcvState;
+
+volatile UCHAR ucRTUBuf[MB_SER_PDU_SIZE_MAX];
+
+static volatile UCHAR *pucSndBufferCur;
+static volatile USHORT usSndBufferCount;
+
+static volatile USHORT usRcvBufferPos;
+
+/* ----------------------- Start implementation -----------------------------*/
+eMBErrorCode
+eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
+{
+ eMBErrorCode eStatus = MB_ENOERR;
+ ULONG usTimerT35_50us;
+
+ ( void )ucSlaveAddress;
+ ENTER_CRITICAL_SECTION( );
+
+ /* Modbus RTU uses 8 Databits. */
+ if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
+ {
+ eStatus = MB_EPORTERR;
+ }
+ else
+ {
+ /* If baudrate > 19200 then we should use the fixed timer values
+ * t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
+ */
+ if( ulBaudRate > 19200 )
+ {
+ usTimerT35_50us = 35; /* 1800us. */
+ }
+ else
+ {
+ /* The timer reload value for a character is given by:
+ *
+ * ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
+ * = 11 * Ticks_per_1s / Baudrate
+ * = 220000 / Baudrate
+ * The reload for t3.5 is 1.5 times this value and similary
+ * for t3.5.
+ */
+ usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
+ }
+ if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
+ {
+ eStatus = MB_EPORTERR;
+ }
+ }
+ EXIT_CRITICAL_SECTION( );
+
+ return eStatus;
+}
+
+void
+eMBRTUStart( void )
+{
+ ENTER_CRITICAL_SECTION( );
+ /* Initially the receiver is in the state STATE_RX_INIT. we start
+ * the timer and if no character is received within t3.5 we change
+ * to STATE_RX_IDLE. This makes sure that we delay startup of the
+ * modbus protocol stack until the bus is free.
+ */
+ eRcvState = STATE_RX_INIT;
+ vMBPortSerialEnable( TRUE, FALSE );
+ vMBPortTimersEnable( );
+
+ EXIT_CRITICAL_SECTION( );
+}
+
+void
+eMBRTUStop( void )
+{
+ ENTER_CRITICAL_SECTION( );
+ vMBPortSerialEnable( FALSE, FALSE );
+ vMBPortTimersDisable( );
+ EXIT_CRITICAL_SECTION( );
+}
+
+eMBErrorCode
+eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
+{
+ BOOL xFrameReceived = FALSE;
+ eMBErrorCode eStatus = MB_ENOERR;
+
+ ENTER_CRITICAL_SECTION( );
+ assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );
+
+ /* Length and CRC check */
+ if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
+ && ( usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )
+ {
+ /* Save the address field. All frames are passed to the upper layed
+ * and the decision if a frame is used is done there.
+ */
+ *pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];
+
+ /* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
+ * size of address field and CRC checksum.
+ */
+ *pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC );
+
+ /* Return the start of the Modbus PDU to the caller. */
+ *pucFrame = ( UCHAR * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
+ xFrameReceived = TRUE;
+ }
+ else
+ {
+ eStatus = MB_EIO;
+ }
+
+ EXIT_CRITICAL_SECTION( );
+ return eStatus;
+}
+
+eMBErrorCode
+eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
+{
+ eMBErrorCode eStatus = MB_ENOERR;
+ USHORT usCRC16;
+
+ ENTER_CRITICAL_SECTION( );
+
+ /* Check if the receiver is still in idle state. If not we where to
+ * slow with processing the received frame and the master sent another
+ * frame on the network. We have to abort sending the frame.
+ */
+ if( eRcvState == STATE_RX_IDLE )
+ {
+ /* First byte before the Modbus-PDU is the slave address. */
+ pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
+ usSndBufferCount = 1;
+
+ /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
+ pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
+ usSndBufferCount += usLength;
+
+ /* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
+ usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
+ ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
+ ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
+
+ /* Activate the transmitter. */
+ eSndState = STATE_TX_XMIT;
+ vMBPortSerialEnable( FALSE, TRUE );
+ }
+ else
+ {
+ eStatus = MB_EIO;
+ }
+ EXIT_CRITICAL_SECTION( );
+ return eStatus;
+}
+
+BOOL
+xMBRTUReceiveFSM( void )
+{
+ BOOL xTaskNeedSwitch = FALSE;
+ UCHAR ucByte;
+
+ assert( eSndState == STATE_TX_IDLE );
+
+ /* Always read the character. */
+ ( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
+
+ switch ( eRcvState )
+ {
+ /* If we have received a character in the init state we have to
+ * wait until the frame is finished.
+ */
+ case STATE_RX_INIT:
+ vMBPortTimersEnable( );
+ break;
+
+ /* In the error state we wait until all characters in the
+ * damaged frame are transmitted.
+ */
+ case STATE_RX_ERROR:
+ vMBPortTimersEnable( );
+ break;
+
+ /* In the idle state we wait for a new character. If a character
+ * is received the t1.5 and t3.5 timers are started and the
+ * receiver is in the state STATE_RX_RECEIVCE.
+ */
+ case STATE_RX_IDLE:
+ usRcvBufferPos = 0;
+ ucRTUBuf[usRcvBufferPos++] = ucByte;
+ eRcvState = STATE_RX_RCV;
+
+ /* Enable t3.5 timers. */
+ vMBPortTimersEnable( );
+ break;
+
+ /* We are currently receiving a frame. Reset the timer after
+ * every character received. If more than the maximum possible
+ * number of bytes in a modbus frame is received the frame is
+ * ignored.
+ */
+ case STATE_RX_RCV:
+ if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
+ {
+ ucRTUBuf[usRcvBufferPos++] = ucByte;
+ }
+ else
+ {
+ eRcvState = STATE_RX_ERROR;
+ }
+ vMBPortTimersEnable( );
+ break;
+ }
+ return xTaskNeedSwitch;
+}
+
+BOOL
+xMBRTUTransmitFSM( void )
+{
+ BOOL xNeedPoll = FALSE;
+
+ assert( eRcvState == STATE_RX_IDLE );
+
+ switch ( eSndState )
+ {
+ /* We should not get a transmitter event if the transmitter is in
+ * idle state. */
+ case STATE_TX_IDLE:
+ /* enable receiver/disable transmitter. */
+ vMBPortSerialEnable( TRUE, FALSE );
+ break;
+
+ case STATE_TX_XMIT:
+ /* check if we are finished. */
+ if( usSndBufferCount != 0 )
+ {
+ xMBPortSerialPutByte( ( CHAR )*pucSndBufferCur );
+ pucSndBufferCur++; /* next byte in sendbuffer. */
+ usSndBufferCount--;
+ }
+ else
+ {
+ xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
+ /* Disable transmitter. This prevents another transmit buffer
+ * empty interrupt. */
+ vMBPortSerialEnable( TRUE, FALSE );
+ eSndState = STATE_TX_IDLE;
+ }
+ break;
+ }
+
+ return xNeedPoll;
+}
+
+BOOL
+xMBRTUTimerT35Expired( void )
+{
+ BOOL xNeedPoll = FALSE;
+
+ switch ( eRcvState )
+ {
+ /* Timer t35 expired. Startup phase is finished. */
+ case STATE_RX_INIT:
+ xNeedPoll = xMBPortEventPost( EV_READY );
+ break;
+
+ /* A frame was received and t35 expired. Notify the listener that
+ * a new frame was received. */
+ case STATE_RX_RCV:
+ xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED );
+ break;
+
+ /* An error occured while receiving the frame. */
+ case STATE_RX_ERROR:
+ break;
+
+ /* Function called in an illegal state. */
+ default:
+ assert( ( eRcvState == STATE_RX_INIT ) ||
+ ( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_ERROR ) );
+ }
+
+ vMBPortTimersDisable( );
+ eRcvState = STATE_RX_IDLE;
+
+ return xNeedPoll;
+}
diff --git a/apps/modbus/rtu/mbrtu.h b/apps/modbus/rtu/mbrtu.h
new file mode 100644
index 000000000..8d8bd1f0a
--- /dev/null
+++ b/apps/modbus/rtu/mbrtu.h
@@ -0,0 +1,51 @@
+/*
+ * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
+ * 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.
+ *
+ * File: $Id: mbrtu.h,v 1.9 2006/12/07 22:10:34 wolti Exp $
+ */
+
+#ifndef _MB_RTU_H
+#define _MB_RTU_H
+
+#ifdef __cplusplus
+PR_BEGIN_EXTERN_C
+#endif
+ eMBErrorCode eMBRTUInit( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate,
+ eMBParity eParity );
+void eMBRTUStart( void );
+void eMBRTUStop( void );
+eMBErrorCode eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );
+eMBErrorCode eMBRTUSend( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength );
+BOOL xMBRTUReceiveFSM( void );
+BOOL xMBRTUTransmitFSM( void );
+BOOL xMBRTUTimerT15Expired( void );
+BOOL xMBRTUTimerT35Expired( void );
+
+#ifdef __cplusplus
+PR_END_EXTERN_C
+#endif
+#endif