1、【修改】“UserModbusSlaver”相关文件前缀为“user_app”,主机为“user_app_m”,从机为“user_app_s”,主从机公用一个头文件,同时将相关文件移动到/FreeModbus/port下;
2、【修改】FreeModbus的port文件夹下相关文件的命名方式,从机保留FreeModbus原有的文件,主机则在文件名后面增加“_m”以区分,尽量保证FreeModbus源码的统一性; 3、【增加】FreeModbus主机初始化、使能、失能及定时器状态机相关方法,定时器超时状态机还需要后期完善,发送状态机及接收状态机还未实现; Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
parent
7c952c0623
commit
a5528950d0
|
@ -12,7 +12,7 @@
|
||||||
#include "bsp.h"
|
#include "bsp.h"
|
||||||
#include "delay_conf.h"
|
#include "delay_conf.h"
|
||||||
#include "cpuusage.h"
|
#include "cpuusage.h"
|
||||||
#include "UserModbusSlaver.h"
|
#include "user_app.h"
|
||||||
|
|
||||||
/*******************************************************************************************************/
|
/*******************************************************************************************************/
|
||||||
// DEFINES
|
// DEFINES
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
#include <bsp.h>
|
#include <bsp.h>
|
||||||
#include <rthw.h>
|
#include <rthw.h>
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include "UserModbusSlaver.h"
|
#include "user_app.h"
|
||||||
/*
|
/*
|
||||||
*********************************************************************************************************
|
*********************************************************************************************************
|
||||||
* LOCAL TABLES
|
* LOCAL TABLES
|
||||||
|
@ -125,13 +125,8 @@ static void GPIO_Configuration(void)
|
||||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_6;
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_6;
|
||||||
GPIO_Init(GPIOG, &GPIO_InitStructure);
|
GPIO_Init(GPIOG, &GPIO_InitStructure);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Function Name : USART1_Configuration
|
* Function Name : USART1_Configuration
|
||||||
* Description : NUSART1ÉèÖÃ
|
* Description : NUSART1ÉèÖÃ
|
||||||
|
|
Binary file not shown.
|
@ -26,7 +26,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* File: $Id: mbconfig.h,v 1.14 2006/12/07 22:10:34 wolti Exp $
|
* File: $Id: mbconfig.h,v 1.14 2006/12/07 22:10:34 wolti Exp $
|
||||||
* $Id: mbconfig.h,v 1.15 2013/08/13 21:19:55 Armink Add Master Functions $
|
* $Id: mbconfig.h,v 1.60 2013/08/13 21:19:55 Armink Add Master Functions $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _MB_CONFIG_H
|
#ifndef _MB_CONFIG_H
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* File: $Id: mbport.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
|
* File: $Id: mbport.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
* mbport.h,v 1.60 2013/08/17 11:42:56 Armink Add Master Functions $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _MB_PORT_H
|
#ifndef _MB_PORT_H
|
||||||
|
@ -66,6 +67,12 @@ BOOL xMBPortEventPost( eMBEventType eEvent );
|
||||||
|
|
||||||
BOOL xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
|
BOOL xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
|
||||||
|
|
||||||
|
BOOL xMBMasterPortEventInit( void );
|
||||||
|
|
||||||
|
BOOL xMBMasterPortEventPost( eMBEventType eEvent );
|
||||||
|
|
||||||
|
BOOL xMBMasterPortEventGet( /*@out@ */ eMBEventType * eEvent );
|
||||||
|
|
||||||
/* ----------------------- Serial port functions ----------------------------*/
|
/* ----------------------- Serial port functions ----------------------------*/
|
||||||
|
|
||||||
BOOL xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
|
BOOL xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
|
||||||
|
@ -81,6 +88,20 @@ INLINE BOOL xMBPortSerialGetByte( CHAR * pucByte );
|
||||||
|
|
||||||
INLINE BOOL xMBPortSerialPutByte( CHAR ucByte );
|
INLINE BOOL xMBPortSerialPutByte( CHAR ucByte );
|
||||||
|
|
||||||
|
|
||||||
|
BOOL xMBMasterPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
|
||||||
|
UCHAR ucDataBits, eMBParity eParity );
|
||||||
|
|
||||||
|
void vMBMasterPortClose( void );
|
||||||
|
|
||||||
|
void xMBMasterPortSerialClose( void );
|
||||||
|
|
||||||
|
void vMBMasterPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
|
||||||
|
|
||||||
|
INLINE BOOL xMBMasterPortSerialGetByte( CHAR * pucByte );
|
||||||
|
|
||||||
|
INLINE BOOL xMBPortSerialPutByte( CHAR ucByte );
|
||||||
|
|
||||||
/* ----------------------- Timers functions ---------------------------------*/
|
/* ----------------------- Timers functions ---------------------------------*/
|
||||||
BOOL xMBPortTimersInit( USHORT usTimeOut50us );
|
BOOL xMBPortTimersInit( USHORT usTimeOut50us );
|
||||||
|
|
||||||
|
@ -90,6 +111,14 @@ INLINE void vMBPortTimersEnable( void );
|
||||||
|
|
||||||
INLINE void vMBPortTimersDisable( void );
|
INLINE void vMBPortTimersDisable( void );
|
||||||
|
|
||||||
|
BOOL xMBMasterPortTimersInit( USHORT usTimeOut50us );
|
||||||
|
|
||||||
|
void xMBMasterPortTimersClose( void );
|
||||||
|
|
||||||
|
INLINE void vMBMasterPortTimersEnable( void );
|
||||||
|
|
||||||
|
INLINE void vMBMasterPortTimersDisable( void );
|
||||||
|
|
||||||
/* ----------------------- Callback for the protocol stack ------------------*/
|
/* ----------------------- Callback for the protocol stack ------------------*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
|
@ -198,6 +198,7 @@ eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eM
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
eStatus = MB_EINVAL;
|
eStatus = MB_EINVAL;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( eStatus == MB_ENOERR )
|
if( eStatus == MB_ENOERR )
|
||||||
|
@ -247,6 +248,64 @@ eMBTCPInit( USHORT ucTCPPort )
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if MB_MASTER_RTU_ENABLED > 0
|
||||||
|
eMBErrorCode
|
||||||
|
eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
switch (eMode)
|
||||||
|
{
|
||||||
|
#if MB_MASTER_RTU_ENABLED > 0
|
||||||
|
case MB_RTU:
|
||||||
|
pvMBMasterFrameStartCur = eMBMasterRTUStart;
|
||||||
|
pvMBMasterFrameStopCur = eMBMasterRTUStop;
|
||||||
|
peMBMasterFrameSendCur = eMBMasterRTUSend;
|
||||||
|
peMBMasterFrameReceiveCur = eMBMasterRTUReceive;
|
||||||
|
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
|
||||||
|
pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM;
|
||||||
|
pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM;
|
||||||
|
pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerT35Expired;
|
||||||
|
|
||||||
|
eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#if MB_MASTER_ASCII_ENABLED > 0
|
||||||
|
case MB_ASCII:
|
||||||
|
pvMBMasterFrameStartCur = eMBMasterASCIIStart;
|
||||||
|
pvMBMasterFrameStopCur = eMBMasterASCIIStop;
|
||||||
|
peMBMasterFrameSendCur = eMBMasterASCIISend;
|
||||||
|
peMBMasterFrameReceiveCur = eMBMasterASCIIReceive;
|
||||||
|
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
|
||||||
|
pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM;
|
||||||
|
pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM;
|
||||||
|
pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired;
|
||||||
|
|
||||||
|
eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity );
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
eStatus = MB_EINVAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eStatus == MB_ENOERR)
|
||||||
|
{
|
||||||
|
if (!xMBMasterPortEventInit())
|
||||||
|
{
|
||||||
|
/* port dependent event module initalization failed. */
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eMBCurrentMode = eMode;
|
||||||
|
eMBState = STATE_DISABLED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
|
eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
|
||||||
{
|
{
|
||||||
|
@ -313,6 +372,27 @@ eMBClose( void )
|
||||||
return eStatus;
|
return eStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if MB_MASTER_RTU_ENABLED > 0
|
||||||
|
eMBErrorCode
|
||||||
|
eMBMasterClose( void )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
if( eMBState == STATE_DISABLED )
|
||||||
|
{
|
||||||
|
if( pvMBMasterFrameCloseCur != NULL )
|
||||||
|
{
|
||||||
|
pvMBMasterFrameCloseCur( );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EILLSTATE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBEnable( void )
|
eMBEnable( void )
|
||||||
{
|
{
|
||||||
|
@ -331,6 +411,26 @@ eMBEnable( void )
|
||||||
return eStatus;
|
return eStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if MB_MASTER_RTU_ENABLED > 0
|
||||||
|
eMBErrorCode
|
||||||
|
eMBMasterEnable( void )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
if( eMBState == STATE_DISABLED )
|
||||||
|
{
|
||||||
|
/* Activate the protocol stack. */
|
||||||
|
pvMBMasterFrameStartCur( );
|
||||||
|
eMBState = STATE_ENABLED;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EILLSTATE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBDisable( void )
|
eMBDisable( void )
|
||||||
{
|
{
|
||||||
|
@ -353,6 +453,30 @@ eMBDisable( void )
|
||||||
return eStatus;
|
return eStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if MB_MASTER_RTU_ENABLED > 0
|
||||||
|
eMBErrorCode
|
||||||
|
eMBMasterDisable( void )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus;
|
||||||
|
|
||||||
|
if( eMBState == STATE_ENABLED )
|
||||||
|
{
|
||||||
|
pvMBMasterFrameStopCur( );
|
||||||
|
eMBState = STATE_DISABLED;
|
||||||
|
eStatus = MB_ENOERR;
|
||||||
|
}
|
||||||
|
else if( eMBState == STATE_DISABLED )
|
||||||
|
{
|
||||||
|
eStatus = MB_ENOERR;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EILLSTATE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
eMBErrorCode eMBPoll( void )
|
eMBErrorCode eMBPoll( void )
|
||||||
{
|
{
|
||||||
static UCHAR *ucMBFrame;
|
static UCHAR *ucMBFrame;
|
||||||
|
|
|
@ -26,7 +26,9 @@
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* 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 $
|
* File: $Id: mbrtu.h,v 1.9 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
* File: $Id: mbrtu.h,v 1.60 2013/08/17 13:11:42 Armink Add Master Functions $
|
||||||
*/
|
*/
|
||||||
|
#include "mbconfig.h"
|
||||||
|
|
||||||
#ifndef _MB_RTU_H
|
#ifndef _MB_RTU_H
|
||||||
#define _MB_RTU_H
|
#define _MB_RTU_H
|
||||||
|
@ -45,6 +47,18 @@ BOOL xMBRTUTransmitFSM( void );
|
||||||
BOOL xMBRTUTimerT15Expired( void );
|
BOOL xMBRTUTimerT15Expired( void );
|
||||||
BOOL xMBRTUTimerT35Expired( void );
|
BOOL xMBRTUTimerT35Expired( void );
|
||||||
|
|
||||||
|
#if MB_MASTER_RTU_ENABLED > 0
|
||||||
|
eMBErrorCode eMBMasterRTUInit( UCHAR ucPort, ULONG ulBaudRate,eMBParity eParity );
|
||||||
|
void eMBMasterRTUStart( void );
|
||||||
|
void eMBMasterRTUStop( void );
|
||||||
|
eMBErrorCode eMBMasterRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );
|
||||||
|
eMBErrorCode eMBMasterRTUSend( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength );
|
||||||
|
BOOL xMBMasterRTUReceiveFSM( void );
|
||||||
|
BOOL xMBMasterRTUTransmitFSM( void );
|
||||||
|
BOOL xMBMasterRTUTimerT15Expired( void );
|
||||||
|
BOOL xMBMasterRTUTimerT35Expired( void );
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
PR_END_EXTERN_C
|
PR_END_EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,355 @@
|
||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2013 China Beijing Armink <armink.ztl@gmail.com>
|
||||||
|
* 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_m.c,v 1.60 2013/08/17 11:42:56 Armink Add Master Functions , $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- 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"
|
||||||
|
|
||||||
|
#if MB_MASTER_RTU_ENABLED > 0
|
||||||
|
/* ----------------------- 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_M_RX_INIT, /*!< Receiver is in initial state. */
|
||||||
|
STATE_M_RX_IDLE, /*!< Receiver is in idle state. */
|
||||||
|
STATE_M_RX_RCV /*!< Frame is beeing received. */
|
||||||
|
} eMBMasterRcvState;
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
STATE_M_TX_IDLE, /*!< Transmitter is in idle state. */
|
||||||
|
STATE_M_TX_XMIT, /*!< Transmitter is in transfer state. */
|
||||||
|
STATE_M_TX_ERROR /*!< If the frame is invalid. */
|
||||||
|
} eMBMasterSndState;
|
||||||
|
|
||||||
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
|
static volatile eMBMasterSndState eSndState;
|
||||||
|
static volatile eMBMasterRcvState eRcvState;
|
||||||
|
|
||||||
|
volatile UCHAR ucMasterRTUBuf[MB_SER_PDU_SIZE_MAX];
|
||||||
|
|
||||||
|
static volatile UCHAR *pucSndBufferCur;
|
||||||
|
static volatile USHORT usSndBufferCount;
|
||||||
|
|
||||||
|
static volatile USHORT usRcvBufferPos;
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
eMBErrorCode
|
||||||
|
eMBMasterRTUInit(UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
ULONG usTimerT35_50us;
|
||||||
|
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
|
||||||
|
/* Modbus RTU uses 8 Databits. */
|
||||||
|
if( xMBMasterPortSerialInit( 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( xMBMasterPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
|
||||||
|
{
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
eMBMasterRTUStart( void )
|
||||||
|
{
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
/* Initially the receiver is in the state STATE_M_RX_INIT. we start
|
||||||
|
* the timer and if no character is received within t3.5 we change
|
||||||
|
* to STATE_M_RX_IDLE. This makes sure that we delay startup of the
|
||||||
|
* modbus protocol stack until the bus is free.
|
||||||
|
*/
|
||||||
|
eRcvState = STATE_M_RX_INIT;
|
||||||
|
vMBMasterPortSerialEnable( TRUE, FALSE );
|
||||||
|
vMBMasterPortTimersEnable( );
|
||||||
|
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
eMBMasterRTUStop( void )
|
||||||
|
{
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
vMBMasterPortSerialEnable( FALSE, FALSE );
|
||||||
|
vMBMasterPortTimersDisable( );
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
}
|
||||||
|
|
||||||
|
//eMBErrorCode
|
||||||
|
//eMBMasterRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
||||||
|
//{
|
||||||
|
// BOOL xFrameReceived = FALSE;
|
||||||
|
// eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
//
|
||||||
|
// ENTER_CRITICAL_SECTION( );
|
||||||
|
// assert_param( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );
|
||||||
|
//
|
||||||
|
// /* Length and CRC check */
|
||||||
|
// if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
|
||||||
|
// && ( usMBCRC16( ( UCHAR * ) ucMasterRTUBuf, 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 = ucMasterRTUBuf[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 * ) & ucMasterRTUBuf[MB_SER_PDU_PDU_OFF];
|
||||||
|
// xFrameReceived = TRUE;
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// eStatus = MB_EIO;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// EXIT_CRITICAL_SECTION( );
|
||||||
|
// return eStatus;
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//eMBErrorCode
|
||||||
|
//eMBMasterRTUSend( 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_M_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 );
|
||||||
|
// ucMasterRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
|
||||||
|
// ucMasterRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
|
||||||
|
//
|
||||||
|
// /* Activate the transmitter. */
|
||||||
|
// eSndState = STATE_M_TX_XMIT;
|
||||||
|
// vMBMasterPortSerialEnable( FALSE, TRUE );
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// eStatus = MB_EIO;
|
||||||
|
// }
|
||||||
|
// EXIT_CRITICAL_SECTION( );
|
||||||
|
// return eStatus;
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//BOOL
|
||||||
|
//xMBMasterRTUReceiveFSM( void )
|
||||||
|
//{
|
||||||
|
// BOOL xTaskNeedSwitch = FALSE;
|
||||||
|
// UCHAR ucByte;
|
||||||
|
//
|
||||||
|
// assert_param( eSndState == STATE_TX_IDLE );
|
||||||
|
//
|
||||||
|
// /* Always read the character. */
|
||||||
|
// ( void )xMBMasterPortSerialGetByte( ( 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:
|
||||||
|
// vMBMasterPortTimersEnable( );
|
||||||
|
// break;
|
||||||
|
//
|
||||||
|
// /* In the error state we wait until all characters in the
|
||||||
|
// * damaged frame are transmitted.
|
||||||
|
// */
|
||||||
|
// case STATE_RX_ERROR:
|
||||||
|
// vMBMasterPortTimersEnable( );
|
||||||
|
// 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_M_RX_IDLE:
|
||||||
|
// usRcvBufferPos = 0;
|
||||||
|
// ucMasterRTUBuf[usRcvBufferPos++] = ucByte;
|
||||||
|
// eRcvState = STATE_M_RX_RCV;
|
||||||
|
//
|
||||||
|
// /* Enable t3.5 timers. */
|
||||||
|
// vMBMasterPortTimersEnable( );
|
||||||
|
// 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_M_RX_RCV:
|
||||||
|
// if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
||||||
|
// {
|
||||||
|
// ucMasterRTUBuf[usRcvBufferPos++] = ucByte;
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// eRcvState = STATE_RX_ERROR;
|
||||||
|
// }
|
||||||
|
// vMBMasterPortTimersEnable();
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// return xTaskNeedSwitch;
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//BOOL
|
||||||
|
//xMBMasterRTUTransmitFSM( void )
|
||||||
|
//{
|
||||||
|
// BOOL xNeedPoll = FALSE;
|
||||||
|
//
|
||||||
|
// assert_param( eRcvState == STATE_RX_IDLE );
|
||||||
|
//
|
||||||
|
// switch ( eSndState )
|
||||||
|
// {
|
||||||
|
// /* We should not get a transmitter event if the transmitter is in
|
||||||
|
// * idle state. */
|
||||||
|
// case STATE_M_TX_IDLE:
|
||||||
|
// /* enable receiver/disable transmitter. */
|
||||||
|
// vMBMasterPortSerialEnable( TRUE, FALSE );
|
||||||
|
// break;
|
||||||
|
//
|
||||||
|
// case STATE_M_TX_XMIT:
|
||||||
|
// /* check if we are finished. */
|
||||||
|
// if( usSndBufferCount != 0 )
|
||||||
|
// {
|
||||||
|
// xMBMasterPortSerialPutByte( ( CHAR )*pucSndBufferCur );
|
||||||
|
// pucSndBufferCur++; /* next byte in sendbuffer. */
|
||||||
|
// usSndBufferCount--;
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// xNeedPoll = xMBMasterPortEventPost( EV_FRAME_SENT );
|
||||||
|
// /* Disable transmitter. This prevents another transmit buffer
|
||||||
|
// * empty interrupt. */
|
||||||
|
// vMBMasterPortSerialEnable( TRUE, FALSE );
|
||||||
|
// eSndState = STATE_M_TX_IDLE;
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return xNeedPoll;
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
BOOL
|
||||||
|
xMBMasterRTUTimerT35Expired( void )
|
||||||
|
{
|
||||||
|
BOOL xNeedPoll = FALSE;
|
||||||
|
|
||||||
|
switch (eRcvState)
|
||||||
|
{
|
||||||
|
/* Timer t35 expired. Startup phase is finished. */
|
||||||
|
case STATE_M_RX_INIT:
|
||||||
|
xNeedPoll = xMBMasterPortEventPost(EV_READY);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* A frame was received and t35 expired. Notify the listener that
|
||||||
|
* a new frame was received. */
|
||||||
|
case STATE_M_RX_RCV:
|
||||||
|
xNeedPoll = xMBMasterPortEventPost(EV_FRAME_RECEIVED);
|
||||||
|
break;
|
||||||
|
/* Function called in an illegal state. */
|
||||||
|
default:
|
||||||
|
assert_param(
|
||||||
|
( eRcvState == STATE_M_RX_INIT ) || ( eRcvState == STATE_M_RX_RCV ));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
vMBMasterPortTimersDisable();
|
||||||
|
eRcvState = STATE_M_RX_IDLE;
|
||||||
|
|
||||||
|
//TODO 发送状态在超时后的状态转换需要添加
|
||||||
|
|
||||||
|
return xNeedPoll;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*
|
*
|
||||||
* File: $Id: port.h add Master Functions,v 1.1 2013/08/13 15:07:05 Armink Exp $
|
* File: $Id: port.h ,v 1.60 2013/08/13 15:07:05 Armink add Master Functions $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _PORT_H
|
#ifndef _PORT_H
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*
|
*
|
||||||
* File: $Id: port_s_event.c,v 1.1 2013/08/13 15:07:05 Armink Exp $
|
* File: $Id: portevent.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions, $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* ----------------------- Modbus includes ----------------------------------*/
|
/* ----------------------- Modbus includes ----------------------------------*/
|
|
@ -16,7 +16,7 @@
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*
|
*
|
||||||
* File: $Id: port_m_event.c add Master Functions,v 1.1 2013/08/13 15:07:05 Armink Exp $
|
* File: $Id: portevent_m.c v 1.60 2013/08/13 15:07:05 Armink add Master Functions, $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* ----------------------- Modbus includes ----------------------------------*/
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
@ -29,14 +29,14 @@ static BOOL xMasterEventInQueue;
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
BOOL
|
BOOL
|
||||||
xMBPortMasterEventInit( void )
|
xMBMasterPortEventInit( void )
|
||||||
{
|
{
|
||||||
xMasterEventInQueue = FALSE;
|
xMasterEventInQueue = FALSE;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
BOOL
|
||||||
xMBPortMasterEventPost( eMBEventType eEvent )
|
xMBMasterPortEventPost( eMBEventType eEvent )
|
||||||
{
|
{
|
||||||
xMasterEventInQueue = TRUE;
|
xMasterEventInQueue = TRUE;
|
||||||
eMasterQueuedEvent = eEvent;
|
eMasterQueuedEvent = eEvent;
|
||||||
|
@ -44,7 +44,7 @@ xMBPortMasterEventPost( eMBEventType eEvent )
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL
|
BOOL
|
||||||
xMBPortMasterEventGet( eMBEventType * eEvent )
|
xMBMasterPortEventGet( eMBEventType * eEvent )
|
||||||
{
|
{
|
||||||
BOOL xEventHappened = FALSE;
|
BOOL xEventHappened = FALSE;
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*
|
*
|
||||||
* File: $Id: port_s_serial.c,v 1.1 2013/08/13 15:07:05 Armink Exp $
|
* File: $Id: portserial.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "port.h"
|
#include "port.h"
|
|
@ -16,7 +16,7 @@
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*
|
*
|
||||||
* File: $Id: port_m_serial.c add Master Functions,v 1.1 2013/08/13 15:07:05 Armink Exp $
|
* File: $Id: portserial_m.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "port.h"
|
#include "port.h"
|
|
@ -16,7 +16,7 @@
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*
|
*
|
||||||
* File: $Id: port_s_timer.c,v 1.1 2013/08/13 15:07:05 Armink Exp $
|
* File: $Id: porttimer.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* ----------------------- Platform includes --------------------------------*/
|
/* ----------------------- Platform includes --------------------------------*/
|
|
@ -16,7 +16,7 @@
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*
|
*
|
||||||
* File: $Id: port_m_timer.c add Master Functions,v 1.1 2013/08/13 15:07:05 Armink Exp $
|
* File: $Id: porttimer_m.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* ----------------------- Platform includes --------------------------------*/
|
/* ----------------------- Platform includes --------------------------------*/
|
|
@ -1,4 +1,4 @@
|
||||||
#include "UserModbusSlaver.h"
|
#include "user_app.h"
|
||||||
/* ----------------------- Variables ---------------------------------*/
|
/* ----------------------- Variables ---------------------------------*/
|
||||||
USHORT usDiscreteInputStart = DISCRETE_INPUT_START;
|
USHORT usDiscreteInputStart = DISCRETE_INPUT_START;
|
||||||
UCHAR usDiscreteInputBuf[DISCRETE_INPUT_NDISCRETES/8] ;
|
UCHAR usDiscreteInputBuf[DISCRETE_INPUT_NDISCRETES/8] ;
|
Loading…
Reference in New Issue