xref: /nrf52832-nimble/rt-thread/components/net/freemodbus/modbus/include/mbport.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
3  * Copyright (c) 2006 Christian Walter <[email protected]>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * File: $Id: mbport.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
29  *            mbport.h,v 1.60 2013/08/17 11:42:56 Armink Add Master Functions  $
30  */
31 
32 #ifndef _MB_PORT_H
33 #define _MB_PORT_H
34 
35 #ifdef __cplusplus
36 PR_BEGIN_EXTERN_C
37 #endif
38 
39 /* ----------------------- Defines ------------------------------------------*/
40 
41 /* ----------------------- Type definitions ---------------------------------*/
42 
43 typedef enum
44 {
45     EV_READY            = 1<<0,         /*!< Startup finished. */
46     EV_FRAME_RECEIVED   = 1<<1,         /*!< Frame received. */
47     EV_EXECUTE          = 1<<2,         /*!< Execute function. */
48     EV_FRAME_SENT       = 1<<3          /*!< Frame sent. */
49 } eMBEventType;
50 
51 typedef enum
52 {
53     EV_MASTER_READY                    = 1<<0,  /*!< Startup finished. */
54     EV_MASTER_FRAME_RECEIVED           = 1<<1,  /*!< Frame received. */
55     EV_MASTER_EXECUTE                  = 1<<2,  /*!< Execute function. */
56     EV_MASTER_FRAME_SENT               = 1<<3,  /*!< Frame sent. */
57     EV_MASTER_ERROR_PROCESS            = 1<<4,  /*!< Frame error process. */
58     EV_MASTER_PROCESS_SUCESS           = 1<<5,  /*!< Request process success. */
59     EV_MASTER_ERROR_RESPOND_TIMEOUT    = 1<<6,  /*!< Request respond timeout. */
60     EV_MASTER_ERROR_RECEIVE_DATA       = 1<<7,  /*!< Request receive data error. */
61     EV_MASTER_ERROR_EXECUTE_FUNCTION   = 1<<8,  /*!< Request execute function error. */
62 } eMBMasterEventType;
63 
64 typedef enum
65 {
66     EV_ERROR_RESPOND_TIMEOUT,         /*!< Slave respond timeout. */
67     EV_ERROR_RECEIVE_DATA,            /*!< Receive frame data erroe. */
68     EV_ERROR_EXECUTE_FUNCTION,        /*!< Execute function error. */
69 } eMBMasterErrorEventType;
70 
71 /*! \ingroup modbus
72  * \brief Parity used for characters in serial mode.
73  *
74  * The parity which should be applied to the characters sent over the serial
75  * link. Please note that this values are actually passed to the porting
76  * layer and therefore not all parity modes might be available.
77  */
78 typedef enum
79 {
80     MB_PAR_NONE,                /*!< No parity. */
81     MB_PAR_ODD,                 /*!< Odd parity. */
82     MB_PAR_EVEN                 /*!< Even parity. */
83 } eMBParity;
84 
85 /* ----------------------- Supporting functions -----------------------------*/
86 BOOL            xMBPortEventInit( void );
87 
88 BOOL            xMBPortEventPost( eMBEventType eEvent );
89 
90 BOOL            xMBPortEventGet(  /*@out@ */ eMBEventType * eEvent );
91 
92 BOOL            xMBMasterPortEventInit( void );
93 
94 BOOL            xMBMasterPortEventPost( eMBMasterEventType eEvent );
95 
96 BOOL            xMBMasterPortEventGet(  /*@out@ */ eMBMasterEventType * eEvent );
97 
98 void            vMBMasterOsResInit( void );
99 
100 BOOL            xMBMasterRunResTake( int32_t time );
101 
102 void            vMBMasterRunResRelease( void );
103 
104 /* ----------------------- Serial port functions ----------------------------*/
105 
106 BOOL            xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
107                                    UCHAR ucDataBits, eMBParity eParity );
108 
109 void            vMBPortClose( void );
110 
111 void            xMBPortSerialClose( void );
112 
113 void            vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
114 
115 INLINE BOOL     xMBPortSerialGetByte( CHAR * pucByte );
116 
117 INLINE BOOL     xMBPortSerialPutByte( CHAR ucByte );
118 
119 BOOL            xMBMasterPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
120                                    UCHAR ucDataBits, eMBParity eParity );
121 
122 void            vMBMasterPortClose( void );
123 
124 void            xMBMasterPortSerialClose( void );
125 
126 void            vMBMasterPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
127 
128 INLINE BOOL     xMBMasterPortSerialGetByte( CHAR * pucByte );
129 
130 INLINE BOOL     xMBMasterPortSerialPutByte( CHAR ucByte );
131 
132 /* ----------------------- Timers functions ---------------------------------*/
133 BOOL            xMBPortTimersInit( USHORT usTimeOut50us );
134 
135 void            xMBPortTimersClose( void );
136 
137 INLINE void     vMBPortTimersEnable( void );
138 
139 INLINE void     vMBPortTimersDisable( void );
140 
141 BOOL            xMBMasterPortTimersInit( USHORT usTimeOut50us );
142 
143 void            xMBMasterPortTimersClose( void );
144 
145 INLINE void     vMBMasterPortTimersT35Enable( void );
146 
147 INLINE void     vMBMasterPortTimersConvertDelayEnable( void );
148 
149 INLINE void     vMBMasterPortTimersRespondTimeoutEnable( void );
150 
151 INLINE void     vMBMasterPortTimersDisable( void );
152 
153 /* ----------------- Callback for the master error process ------------------*/
154 void            vMBMasterErrorCBRespondTimeout( UCHAR ucDestAddress, const UCHAR* pucPDUData,
155                                                 USHORT ucPDULength );
156 
157 void            vMBMasterErrorCBReceiveData( UCHAR ucDestAddress, const UCHAR* pucPDUData,
158                                              USHORT ucPDULength );
159 
160 void            vMBMasterErrorCBExecuteFunction( UCHAR ucDestAddress, const UCHAR* pucPDUData,
161                                                  USHORT ucPDULength );
162 
163 void            vMBMasterCBRequestScuuess( void );
164 
165 /* ----------------------- Callback for the protocol stack ------------------*/
166 
167 /*!
168  * \brief Callback function for the porting layer when a new byte is
169  *   available.
170  *
171  * Depending upon the mode this callback function is used by the RTU or
172  * ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
173  * must immediately return a new character.
174  *
175  * \return <code>TRUE</code> if a event was posted to the queue because
176  *   a new byte was received. The port implementation should wake up the
177  *   tasks which are currently blocked on the eventqueue.
178  */
179 extern          BOOL( *pxMBFrameCBByteReceived ) ( void );
180 
181 extern          BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
182 
183 extern          BOOL( *pxMBPortCBTimerExpired ) ( void );
184 
185 extern          BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
186 
187 extern          BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
188 
189 extern          BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
190 
191 /* ----------------------- TCP port functions -------------------------------*/
192 BOOL            xMBTCPPortInit( USHORT usTCPPort );
193 
194 void            vMBTCPPortClose( void );
195 
196 void            vMBTCPPortDisable( void );
197 
198 BOOL            xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
199 
200 BOOL            xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
201 
202 #ifdef __cplusplus
203 PR_END_EXTERN_C
204 #endif
205 #endif
206