mirror of
https://github.com/armink/FreeModbus_Slave-Master-RTT-STM32.git
synced 2025-02-27 19:27:26 +08:00
1、【修改】FreeModbu分析图使其内容更加完整
2、【增加】FreeModbus源码中关于主机模式的宏定义 3、【优化】FreeModbus中port文件的命名格式,在MB后面直接增加Master字样 4、【增加】FreeModbus中与主机相关的帧处理函数指针及相关回调函数,具体实现及调用逻辑后期实现 Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
parent
d4ee095afa
commit
7c952c0623
Binary file not shown.
@ -44,7 +44,7 @@
|
|||||||
#include "mbcrc.h"
|
#include "mbcrc.h"
|
||||||
#include "mbport.h"
|
#include "mbport.h"
|
||||||
|
|
||||||
#if MB_ASCII_ENABLED > 0
|
#if MB_SLAVER_ASCII_ENABLED > 0
|
||||||
|
|
||||||
/* ----------------------- Defines ------------------------------------------*/
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
#define MB_ASCII_DEFAULT_CR '\r' /*!< Default CR character for Modbus ASCII. */
|
#define MB_ASCII_DEFAULT_CR '\r' /*!< Default CR character for Modbus ASCII. */
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
PR_BEGIN_EXTERN_C
|
PR_BEGIN_EXTERN_C
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MB_ASCII_ENABLED > 0
|
#if MB_SLAVER_ASCII_ENABLED > 0
|
||||||
eMBErrorCode eMBASCIIInit( UCHAR slaveAddress, UCHAR ucPort,
|
eMBErrorCode eMBASCIIInit( UCHAR slaveAddress, UCHAR ucPort,
|
||||||
ULONG ulBaudRate, eMBParity eParity );
|
ULONG ulBaudRate, eMBParity eParity );
|
||||||
void eMBASCIIStart( void );
|
void eMBASCIIStart( void );
|
||||||
|
@ -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: 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 $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _MB_CONFIG_H
|
#ifndef _MB_CONFIG_H
|
||||||
@ -46,12 +47,18 @@ PR_BEGIN_EXTERN_C
|
|||||||
/*! \addtogroup modbus_cfg
|
/*! \addtogroup modbus_cfg
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
/*! \brief If Modbus ASCII support is enabled. */
|
/*! \brief If Modbus Master ASCII support is enabled. */
|
||||||
#define MB_ASCII_ENABLED ( 0 )
|
#define MB_MASTER_ASCII_ENABLED ( 0 )
|
||||||
/*! \brief If Modbus RTU support is enabled. */
|
/*! \brief If Modbus Master RTU support is enabled. */
|
||||||
#define MB_RTU_ENABLED ( 1 )
|
#define MB_MASTER_RTU_ENABLED ( 1 )
|
||||||
/*! \brief If Modbus TCP support is enabled. */
|
/*! \brief If Modbus Master TCP support is enabled. */
|
||||||
#define MB_TCP_ENABLED ( 0 )
|
#define MB_MASTER_TCP_ENABLED ( 0 )
|
||||||
|
/*! \brief If Modbus Slaver ASCII support is enabled. */
|
||||||
|
#define MB_SLAVER_ASCII_ENABLED ( 0 )
|
||||||
|
/*! \brief If Modbus Slaver RTU support is enabled. */
|
||||||
|
#define MB_SLAVER_RTU_ENABLED ( 1 )
|
||||||
|
/*! \brief If Modbus Slaver TCP support is enabled. */
|
||||||
|
#define MB_SLAVER_TCP_ENABLED ( 0 )
|
||||||
/*! \brief The character timeout value for Modbus ASCII.
|
/*! \brief The character timeout value for Modbus ASCII.
|
||||||
*
|
*
|
||||||
* The character timeout value is not fixed for Modbus ASCII and is therefore
|
* The character timeout value is not fixed for Modbus ASCII and is therefore
|
||||||
|
@ -44,13 +44,13 @@
|
|||||||
#include "mbfunc.h"
|
#include "mbfunc.h"
|
||||||
|
|
||||||
#include "mbport.h"
|
#include "mbport.h"
|
||||||
#if MB_RTU_ENABLED == 1
|
#if MB_SLAVER_RTU_ENABLED == 1
|
||||||
#include "mbrtu.h"
|
#include "mbrtu.h"
|
||||||
#endif
|
#endif
|
||||||
#if MB_ASCII_ENABLED == 1
|
#if MB_SLAVER_ASCII_ENABLED == 1
|
||||||
#include "mbascii.h"
|
#include "mbascii.h"
|
||||||
#endif
|
#endif
|
||||||
#if MB_TCP_ENABLED == 1
|
#if MB_SLAVER_TCP_ENABLED == 1
|
||||||
#include "mbtcp.h"
|
#include "mbtcp.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -72,6 +72,7 @@ static enum
|
|||||||
|
|
||||||
/* Functions pointer which are initialized in eMBInit( ). Depending on the
|
/* Functions pointer which are initialized in eMBInit( ). Depending on the
|
||||||
* mode (RTU or ASCII) the are set to the correct implementations.
|
* mode (RTU or ASCII) the are set to the correct implementations.
|
||||||
|
* Using for Modbus Slaver
|
||||||
*/
|
*/
|
||||||
static peMBFrameSend peMBFrameSendCur;
|
static peMBFrameSend peMBFrameSendCur;
|
||||||
static pvMBFrameStart pvMBFrameStartCur;
|
static pvMBFrameStart pvMBFrameStartCur;
|
||||||
@ -79,9 +80,20 @@ static pvMBFrameStop pvMBFrameStopCur;
|
|||||||
static peMBFrameReceive peMBFrameReceiveCur;
|
static peMBFrameReceive peMBFrameReceiveCur;
|
||||||
static pvMBFrameClose pvMBFrameCloseCur;
|
static pvMBFrameClose pvMBFrameCloseCur;
|
||||||
|
|
||||||
|
/* Functions pointer which are initialized in eMBInit( ). Depending on the
|
||||||
|
* mode (RTU or ASCII) the are set to the correct implementations.
|
||||||
|
* Using for Modbus Master,Add by Armink 20130813
|
||||||
|
*/
|
||||||
|
static peMBFrameSend peMBMasterFrameSendCur;
|
||||||
|
static pvMBFrameStart pvMBMasterFrameStartCur;
|
||||||
|
static pvMBFrameStop pvMBMasterFrameStopCur;
|
||||||
|
static peMBFrameReceive peMBMasterFrameReceiveCur;
|
||||||
|
static pvMBFrameClose pvMBMasterFrameCloseCur;
|
||||||
|
|
||||||
/* Callback functions required by the porting layer. They are called when
|
/* Callback functions required by the porting layer. They are called when
|
||||||
* an external event has happend which includes a timeout or the reception
|
* an external event has happend which includes a timeout or the reception
|
||||||
* or transmission of a character.
|
* or transmission of a character.
|
||||||
|
* Using for Modbus Slaver
|
||||||
*/
|
*/
|
||||||
BOOL( *pxMBFrameCBByteReceived ) ( void );
|
BOOL( *pxMBFrameCBByteReceived ) ( void );
|
||||||
BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
|
BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
|
||||||
@ -90,6 +102,18 @@ BOOL( *pxMBPortCBTimerExpired ) ( void );
|
|||||||
BOOL( *pxMBFrameCBReceiveFSMCur ) ( void );
|
BOOL( *pxMBFrameCBReceiveFSMCur ) ( void );
|
||||||
BOOL( *pxMBFrameCBTransmitFSMCur ) ( void );
|
BOOL( *pxMBFrameCBTransmitFSMCur ) ( void );
|
||||||
|
|
||||||
|
/* Callback functions required by the porting layer. They are called when
|
||||||
|
* an external event has happend which includes a timeout or the reception
|
||||||
|
* or transmission of a character.
|
||||||
|
* Using for Modbus Master,Add by Armink 20130813
|
||||||
|
*/
|
||||||
|
BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
|
||||||
|
BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
|
||||||
|
BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
|
||||||
|
|
||||||
|
BOOL( *pxMBMasterFrameCBReceiveFSMCur ) ( void );
|
||||||
|
BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void );
|
||||||
|
|
||||||
/* An array of Modbus functions handlers which associates Modbus function
|
/* An array of Modbus functions handlers which associates Modbus function
|
||||||
* codes with implementing functions.
|
* codes with implementing functions.
|
||||||
*/
|
*/
|
||||||
@ -144,7 +168,7 @@ eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eM
|
|||||||
|
|
||||||
switch ( eMode )
|
switch ( eMode )
|
||||||
{
|
{
|
||||||
#if MB_RTU_ENABLED > 0
|
#if MB_SLAVER_RTU_ENABLED > 0
|
||||||
case MB_RTU:
|
case MB_RTU:
|
||||||
pvMBFrameStartCur = eMBRTUStart;
|
pvMBFrameStartCur = eMBRTUStart;
|
||||||
pvMBFrameStopCur = eMBRTUStop;
|
pvMBFrameStopCur = eMBRTUStop;
|
||||||
@ -158,7 +182,7 @@ eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eM
|
|||||||
eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
|
eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
#if MB_ASCII_ENABLED > 0
|
#if MB_SLAVER_ASCII_ENABLED > 0
|
||||||
case MB_ASCII:
|
case MB_ASCII:
|
||||||
pvMBFrameStartCur = eMBASCIIStart;
|
pvMBFrameStartCur = eMBASCIIStart;
|
||||||
pvMBFrameStopCur = eMBASCIIStop;
|
pvMBFrameStopCur = eMBASCIIStop;
|
||||||
@ -193,7 +217,7 @@ eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eM
|
|||||||
return eStatus;
|
return eStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MB_TCP_ENABLED > 0
|
#if MB_SLAVER_TCP_ENABLED > 0
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBTCPInit( USHORT ucTCPPort )
|
eMBTCPInit( USHORT ucTCPPort )
|
||||||
{
|
{
|
||||||
|
@ -42,7 +42,7 @@
|
|||||||
#include "mbframe.h"
|
#include "mbframe.h"
|
||||||
#include "mbport.h"
|
#include "mbport.h"
|
||||||
|
|
||||||
#if MB_TCP_ENABLED > 0
|
#if MB_SLAVER_TCP_ENABLED > 0
|
||||||
|
|
||||||
/* ----------------------- Defines ------------------------------------------*/
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
|
||||||
|
@ -39,9 +39,6 @@
|
|||||||
#define MASTER_RS485_SEND_MODE GPIO_SetBits(GPIOB,GPIO_Pin_13)
|
#define MASTER_RS485_SEND_MODE GPIO_SetBits(GPIOB,GPIO_Pin_13)
|
||||||
#define MASTER_RS485_RECEIVE_MODE GPIO_ResetBits(GPIOB,GPIO_Pin_13)
|
#define MASTER_RS485_RECEIVE_MODE GPIO_ResetBits(GPIOB,GPIO_Pin_13)
|
||||||
|
|
||||||
//void USART1_IRQHandler(void);
|
|
||||||
//void TIM3_IRQHandler(void);
|
|
||||||
|
|
||||||
#define ENTER_CRITICAL_SECTION() EnterCriticalSection()
|
#define ENTER_CRITICAL_SECTION() EnterCriticalSection()
|
||||||
#define EXIT_CRITICAL_SECTION() ExitCriticalSection()
|
#define EXIT_CRITICAL_SECTION() ExitCriticalSection()
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ static void prvvUARTTxReadyISR(void);
|
|||||||
static void prvvUARTRxISR(void);
|
static void prvvUARTRxISR(void);
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
|
||||||
void vMBPortMasterSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
|
void vMBMasterPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
|
||||||
{
|
{
|
||||||
if (xRxEnable)
|
if (xRxEnable)
|
||||||
{
|
{
|
||||||
@ -51,13 +51,13 @@ void vMBPortMasterSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void vMBPortMasterClose(void)
|
void vMBMasterPortClose(void)
|
||||||
{
|
{
|
||||||
USART_ITConfig(USART2, USART_IT_TXE | USART_IT_RXNE, DISABLE);
|
USART_ITConfig(USART2, USART_IT_TXE | USART_IT_RXNE, DISABLE);
|
||||||
USART_Cmd(USART2, DISABLE);
|
USART_Cmd(USART2, DISABLE);
|
||||||
}
|
}
|
||||||
//默认一个从机 串口2 波特率可设置 奇偶检验可设置
|
//默认一个从机 串口2 波特率可设置 奇偶检验可设置
|
||||||
BOOL xMBPortMasterSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
||||||
eMBParity eParity)
|
eMBParity eParity)
|
||||||
{
|
{
|
||||||
GPIO_InitTypeDef GPIO_InitStructure;
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||||||
@ -130,13 +130,13 @@ BOOL xMBPortMasterSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL xMBPortMasterSerialPutByte(CHAR ucByte)
|
BOOL xMBMasterPortSerialPutByte(CHAR ucByte)
|
||||||
{
|
{
|
||||||
USART_SendData(USART2, ucByte);
|
USART_SendData(USART2, ucByte);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL xMBPortMasterSerialGetByte(CHAR * pucByte)
|
BOOL xMBMasterPortSerialGetByte(CHAR * pucByte)
|
||||||
{
|
{
|
||||||
*pucByte = USART_ReceiveData(USART2);
|
*pucByte = USART_ReceiveData(USART2);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@ -151,7 +151,7 @@ BOOL xMBPortMasterSerialGetByte(CHAR * pucByte)
|
|||||||
*/
|
*/
|
||||||
void prvvUARTTxReadyISR(void)
|
void prvvUARTTxReadyISR(void)
|
||||||
{
|
{
|
||||||
pxMBFrameCBTransmitterEmpty();
|
pxMBMasterFrameCBTransmitterEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -162,7 +162,7 @@ void prvvUARTTxReadyISR(void)
|
|||||||
*/
|
*/
|
||||||
void prvvUARTRxISR(void)
|
void prvvUARTRxISR(void)
|
||||||
{
|
{
|
||||||
pxMBFrameCBByteReceived();
|
pxMBMasterFrameCBByteReceived();
|
||||||
}
|
}
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Function Name : USART2_IRQHandler
|
* Function Name : USART2_IRQHandler
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
static void prvvTIMERExpiredISR(void);
|
static void prvvTIMERExpiredISR(void);
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
BOOL xMBPortMasterTimersInit(USHORT usTim1Timerout50us)
|
BOOL xMBMasterPortTimersInit(USHORT usTim1Timerout50us)
|
||||||
{
|
{
|
||||||
|
|
||||||
uint16_t PrescalerValue = 0;
|
uint16_t PrescalerValue = 0;
|
||||||
@ -72,7 +72,7 @@ BOOL xMBPortMasterTimersInit(USHORT usTim1Timerout50us)
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vMBPortMasterTimersEnable()
|
void vMBMasterPortTimersEnable()
|
||||||
{
|
{
|
||||||
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
||||||
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
||||||
@ -80,7 +80,7 @@ void vMBPortMasterTimersEnable()
|
|||||||
TIM_Cmd(TIM2, ENABLE);
|
TIM_Cmd(TIM2, ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vMBPortMasterTimersDisable()
|
void vMBMasterPortTimersDisable()
|
||||||
{
|
{
|
||||||
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
||||||
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
|
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
|
||||||
@ -90,8 +90,7 @@ void vMBPortMasterTimersDisable()
|
|||||||
|
|
||||||
void prvvTIMERExpiredISR(void)
|
void prvvTIMERExpiredISR(void)
|
||||||
{
|
{
|
||||||
//TODO 修改以适合主机
|
(void) pxMBMasterPortCBTimerExpired();
|
||||||
(void) pxMBPortCBTimerExpired();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TIM2_IRQHandler(void)
|
void TIM2_IRQHandler(void)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user