From 00a091a84077b60dddb5746934a0c6277245ba33 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 7 Apr 2015 16:12:49 -0600 Subject: [PATCH] Modbus: changes to more C file to make them more compatible with NuttX coding style --- modbus/rtu/mbcrc.c | 140 ++++++++------- modbus/rtu/mbrtu.c | 418 +++++++++++++++++++++++++-------------------- modbus/tcp/mbtcp.c | 2 +- modbus/tcp/mbtcp.h | 2 +- 4 files changed, 308 insertions(+), 254 deletions(-) diff --git a/modbus/rtu/mbcrc.c b/modbus/rtu/mbcrc.c index 3b34edd77..2281b8b6a 100644 --- a/modbus/rtu/mbcrc.c +++ b/modbus/rtu/mbcrc.c @@ -1,4 +1,6 @@ -/* +/**************************************************************************** + * apps/modbus/rtu/mbcrc.c + * * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006 Christian Walter * All rights reserved. @@ -25,74 +27,86 @@ * (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 $ - */ + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ -/* ----------------------- Platform includes --------------------------------*/ #include "port.h" -static const uint8_t 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 -}; +/**************************************************************************** + * Private Data + ****************************************************************************/ -static const uint8_t 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 -}; - -uint16_t -usMBCRC16( uint8_t * pucFrame, uint16_t usLen ) +static const uint8_t aucCRCHi[] = { - uint8_t ucCRCHi = 0xFF; - uint8_t ucCRCLo = 0xFF; - int iIndex; + 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 +}; - while( usLen-- ) +static const uint8_t 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 +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +uint16_t usMBCRC16(uint8_t * pucFrame, uint16_t usLen) +{ + uint8_t ucCRCHi = 0xff; + uint8_t ucCRCLo = 0xff; + int iIndex; + + while(usLen--) { - iIndex = ucCRCLo ^ *( pucFrame++ ); - ucCRCLo = ( uint8_t )( ucCRCHi ^ aucCRCHi[iIndex] ); - ucCRCHi = aucCRCLo[iIndex]; + iIndex = ucCRCLo ^ *(pucFrame++); + ucCRCLo = (uint8_t)(ucCRCHi ^ aucCRCHi[iIndex]); + ucCRCHi = aucCRCLo[iIndex]; } - return ( uint16_t )( ucCRCHi << 8 | ucCRCLo ); + + return (uint16_t)(ucCRCHi << 8 | ucCRCLo); } diff --git a/modbus/rtu/mbrtu.c b/modbus/rtu/mbrtu.c index 5bbf8c23d..29e60815d 100644 --- a/modbus/rtu/mbrtu.c +++ b/modbus/rtu/mbrtu.c @@ -1,4 +1,6 @@ -/* +/**************************************************************************** + * apps/modbus/rtu/mbrtu.c + * * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * Copyright (c) 2006 Christian Walter * All rights reserved. @@ -25,19 +27,19 @@ * (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 $ - */ + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ -/* ----------------------- System includes ----------------------------------*/ #include #include #include #include -/* ----------------------- Platform includes --------------------------------*/ #include "port.h" -/* ----------------------- Modbus includes ----------------------------------*/ #include #include #include @@ -45,29 +47,38 @@ #include "mbrtu.h" #include "mbcrc.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. */ +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#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. */ + +/**************************************************************************** + * Private Type Definitions + ****************************************************************************/ -/* ----------------------- 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. */ + STATE_RX_INIT, /* Receiver is in initial state. */ + STATE_RX_IDLE, /* Receiver is in idle state. */ + STATE_RX_RCV, /* Frame is being 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. */ + STATE_TX_IDLE, /* Transmitter is in idle state. */ + STATE_TX_XMIT /* Transmitter is in transfer state. */ } eMBSndState; -/* ----------------------- Static variables ---------------------------------*/ +/**************************************************************************** + * Private Data + ****************************************************************************/ + static volatile eMBSndState eSndState; static volatile eMBRcvState eRcvState; @@ -78,283 +89,312 @@ static volatile uint16_t usSndBufferCount; static volatile uint16_t usRcvBufferPos; -/* ----------------------- Start implementation -----------------------------*/ -eMBErrorCode -eMBRTUInit(uint8_t ucSlaveAddress, uint8_t ucPort, speed_t ulBaudRate, eMBParity eParity) +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +eMBErrorCode eMBRTUInit(uint8_t ucSlaveAddress, uint8_t ucPort, + speed_t ulBaudRate, eMBParity eParity) { - eMBErrorCode eStatus = MB_ENOERR; - uint32_t usTimerT35_50us; + eMBErrorCode eStatus = MB_ENOERR; + uint32_t usTimerT35_50us; - (void)ucSlaveAddress; - ENTER_CRITICAL_SECTION(); + (void)ucSlaveAddress; + ENTER_CRITICAL_SECTION(); - /* Modbus RTU uses 8 Databits. */ + /* Modbus RTU uses 8 Databits. */ - if (xMBPortSerialInit(ucPort, ulBaudRate, 8, eParity) != true) + if (xMBPortSerialInit(ucPort, ulBaudRate, 8, eParity) != true) { - eStatus = MB_EPORTERR; + eStatus = MB_EPORTERR; } - else + 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) + /* 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. */ + usTimerT35_50us = 35; /* 1800us. */ } - else + 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); + /* 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((uint16_t) usTimerT35_50us) != true) + + if (xMBPortTimersInit((uint16_t) usTimerT35_50us) != true) { - eStatus = MB_EPORTERR; + eStatus = MB_EPORTERR; } } - EXIT_CRITICAL_SECTION(); - return eStatus; + EXIT_CRITICAL_SECTION(); + return eStatus; } -void -eMBRTUStart(void) +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(); + ENTER_CRITICAL_SECTION(); - EXIT_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) +void eMBRTUStop(void) { - ENTER_CRITICAL_SECTION(); - vMBPortSerialEnable(false, false); - vMBPortTimersDisable(); - EXIT_CRITICAL_SECTION(); + ENTER_CRITICAL_SECTION(); + vMBPortSerialEnable(false, false); + vMBPortTimersDisable(); + EXIT_CRITICAL_SECTION(); } -eMBErrorCode -eMBRTUReceive(uint8_t * pucRcvAddress, uint8_t ** pucFrame, uint16_t * pusLength) +eMBErrorCode eMBRTUReceive(uint8_t *pucRcvAddress, uint8_t **pucFrame, + uint16_t *pusLength) { - eMBErrorCode eStatus = MB_ENOERR; + eMBErrorCode eStatus = MB_ENOERR; - ENTER_CRITICAL_SECTION(); - ASSERT(usRcvBufferPos < MB_SER_PDU_SIZE_MAX); + ENTER_CRITICAL_SECTION(); + ASSERT(usRcvBufferPos < MB_SER_PDU_SIZE_MAX); - /* Length and CRC check */ + /* Length and CRC check */ - if ((usRcvBufferPos >= MB_SER_PDU_SIZE_MIN) - && (usMBCRC16((uint8_t *) ucRTUBuf, usRcvBufferPos) == 0)) + if ((usRcvBufferPos >= MB_SER_PDU_SIZE_MIN) && + (usMBCRC16((uint8_t *) 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. - */ + /* 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]; + *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. - */ + /* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus + * size of address field and CRC checksum. + */ - *pusLength = (uint16_t)(usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC); + *pusLength = (uint16_t)(usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC); - /* Return the start of the Modbus PDU to the caller. */ + /* Return the start of the Modbus PDU to the caller. */ - *pucFrame = (uint8_t *) & ucRTUBuf[MB_SER_PDU_PDU_OFF]; + *pucFrame = (uint8_t *) & ucRTUBuf[MB_SER_PDU_PDU_OFF]; } - else + else { - eStatus = MB_EIO; + eStatus = MB_EIO; } - EXIT_CRITICAL_SECTION(); - return eStatus; + EXIT_CRITICAL_SECTION(); + return eStatus; } -eMBErrorCode -eMBRTUSend(uint8_t ucSlaveAddress, const uint8_t * pucFrame, uint16_t usLength) +eMBErrorCode eMBRTUSend(uint8_t ucSlaveAddress, const uint8_t *pucFrame, uint16_t usLength) { - eMBErrorCode eStatus = MB_ENOERR; - uint16_t usCRC16; + eMBErrorCode eStatus = MB_ENOERR; + uint16_t usCRC16; - ENTER_CRITICAL_SECTION(); + 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) + /* 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 = (uint8_t *) pucFrame - 1; - usSndBufferCount = 1; + /* First byte before the Modbus-PDU is the slave address. */ - /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */ - pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress; - usSndBufferCount += usLength; + pucSndBufferCur = (uint8_t *) pucFrame - 1; + usSndBufferCount = 1; - /* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */ - usCRC16 = usMBCRC16((uint8_t *) pucSndBufferCur, usSndBufferCount); - ucRTUBuf[usSndBufferCount++] = (uint8_t)(usCRC16 & 0xFF); - ucRTUBuf[usSndBufferCount++] = (uint8_t)(usCRC16 >> 8); + /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */ - /* Activate the transmitter. */ - eSndState = STATE_TX_XMIT; - vMBPortSerialEnable(false, true); + pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress; + usSndBufferCount += usLength; + + /* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */ + + usCRC16 = usMBCRC16((uint8_t *) pucSndBufferCur, usSndBufferCount); + ucRTUBuf[usSndBufferCount++] = (uint8_t)(usCRC16 & 0xFF); + ucRTUBuf[usSndBufferCount++] = (uint8_t)(usCRC16 >> 8); + + /* Activate the transmitter. */ + + eSndState = STATE_TX_XMIT; + vMBPortSerialEnable(false, true); } - else + else { - eStatus = MB_EIO; + eStatus = MB_EIO; } - EXIT_CRITICAL_SECTION(); - return eStatus; + + EXIT_CRITICAL_SECTION(); + return eStatus; } -bool -xMBRTUReceiveFSM(void) +bool xMBRTUReceiveFSM(void) { - bool xTaskNeedSwitch = false; - uint8_t ucByte; + bool xTaskNeedSwitch = false; + uint8_t ucByte; - ASSERT(eSndState == STATE_TX_IDLE); + ASSERT(eSndState == STATE_TX_IDLE); - /* Always read the character. */ - (void)xMBPortSerialGetByte((int8_t *) & ucByte); + /* Always read the character. */ - switch (eRcvState) + (void)xMBPortSerialGetByte((int8_t *) & 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: + /* 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: + /* 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: + /* 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: + /* 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; + + return xTaskNeedSwitch; } -bool -xMBRTUTransmitFSM(void) +bool xMBRTUTransmitFSM(void) { - bool xNeedPoll = false; + bool xNeedPoll = false; - ASSERT(eRcvState == STATE_RX_IDLE); + ASSERT(eRcvState == STATE_RX_IDLE); - switch (eSndState) + switch (eSndState) { - /* We should not get a transmitter event if the transmitter is in - * idle state. */ + /* 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; + /* enable receiver/disable transmitter. */ + + vMBPortSerialEnable(true, false); + break; case STATE_TX_XMIT: - /* check if we are finished. */ - if (usSndBufferCount != 0) + /* check if we are finished. */ + + if (usSndBufferCount != 0) { - xMBPortSerialPutByte((int8_t)*pucSndBufferCur); - pucSndBufferCur++; /* next byte in sendbuffer. */ - usSndBufferCount--; + xMBPortSerialPutByte((int8_t)*pucSndBufferCur); + pucSndBufferCur++; /* next byte in sendbuffer. */ + usSndBufferCount--; } - else + else { - xNeedPoll = xMBPortEventPost(EV_FRAME_SENT); - /* Disable transmitter. This prevents another transmit buffer - * empty interrupt. */ - vMBPortSerialEnable(true, false); - eSndState = STATE_TX_IDLE; + xNeedPoll = xMBPortEventPost(EV_FRAME_SENT); + + /* Disable transmitter. This prevents another transmit buffer + * empty interrupt. + */ + + vMBPortSerialEnable(true, false); + eSndState = STATE_TX_IDLE; } - break; + break; } - return xNeedPoll; + return xNeedPoll; } -bool -xMBRTUTimerT35Expired(void) +bool xMBRTUTimerT35Expired(void) { - bool xNeedPoll = false; + bool xNeedPoll = false; - switch (eRcvState) + switch (eRcvState) { - /* Timer t35 expired. Startup phase is finished. */ - case STATE_RX_INIT: + /* Timer t35 expired. Start-up 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: + /* 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: + /* An error occurred while receiving the frame. */ + + case STATE_RX_ERROR: break; - /* Function called in an illegal state. */ - default: + /* Function called in an illegal state. */ + + default: ASSERT((eRcvState == STATE_RX_INIT) || - (eRcvState == STATE_RX_RCV) || (eRcvState == STATE_RX_ERROR)); + (eRcvState == STATE_RX_RCV) || + (eRcvState == STATE_RX_ERROR)); } - vMBPortTimersDisable(); - eRcvState = STATE_RX_IDLE; + vMBPortTimersDisable(); + eRcvState = STATE_RX_IDLE; - return xNeedPoll; + return xNeedPoll; } diff --git a/modbus/tcp/mbtcp.c b/modbus/tcp/mbtcp.c index c71fdc34a..d47da0a12 100644 --- a/modbus/tcp/mbtcp.c +++ b/modbus/tcp/mbtcp.c @@ -160,4 +160,4 @@ eMBErrorCode eMBTCPSend(uint8_t _unused, const uint8_t * pucFrame, uint16_t usLe return eStatus; } -#endif +#endif /* CONFIG_MB_TCP_ENABLED */ diff --git a/modbus/tcp/mbtcp.h b/modbus/tcp/mbtcp.h index 9a133f29c..c87e19326 100644 --- a/modbus/tcp/mbtcp.h +++ b/modbus/tcp/mbtcp.h @@ -43,7 +43,7 @@ PR_BEGIN_EXTERN_C #define MB_TCP_PSEUDO_ADDRESS 255 /**************************************************************************** - * Public Function Protototypes + * Public Function Prototypes ****************************************************************************/ eMBErrorCode eMBTCPDoInit(uint16_t ucTCPPort);