mbport.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (c) 2006-2018 Christian Walter <cwalter@embedded-solutions.at>
  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. * mbport.h,v 1.60 2022/07/17 quanghona <lyhonquang@gmail.com> Add send multiple bytes in one call $
  31. */
  32. #ifndef _MB_PORT_H
  33. #define _MB_PORT_H
  34. #ifdef __cplusplus
  35. PR_BEGIN_EXTERN_C
  36. #endif
  37. /* ----------------------- Defines ------------------------------------------*/
  38. #include "port.h"
  39. #define MASTER_SEND_ALL_BYTES_IN_ONE_CALL (1)
  40. #define SLAVE_SEND_ALL_BYTES_IN_ONE_CALL (0)
  41. /* ----------------------- Type definitions ---------------------------------*/
  42. typedef enum
  43. {
  44. EV_READY = 1<<0, /*!< Startup finished. */
  45. EV_FRAME_RECEIVED = 1<<1, /*!< Frame received. */
  46. EV_EXECUTE = 1<<2, /*!< Execute function. */
  47. EV_FRAME_SENT = 1<<3 /*!< Frame sent. */
  48. } eMBEventType;
  49. typedef enum
  50. {
  51. EV_MASTER_READY = 1<<0, /*!< Startup finished. */
  52. EV_MASTER_FRAME_RECEIVED = 1<<1, /*!< Frame received. */
  53. EV_MASTER_EXECUTE = 1<<2, /*!< Execute function. */
  54. EV_MASTER_FRAME_SENT = 1<<3, /*!< Frame sent. */
  55. EV_MASTER_ERROR_PROCESS = 1<<4, /*!< Frame error process. */
  56. EV_MASTER_PROCESS_SUCESS = 1<<5, /*!< Request process success. */
  57. EV_MASTER_ERROR_RESPOND_TIMEOUT = 1<<6, /*!< Request respond timeout. */
  58. EV_MASTER_ERROR_RECEIVE_DATA = 1<<7, /*!< Request receive data error. */
  59. EV_MASTER_ERROR_EXECUTE_FUNCTION = 1<<8, /*!< Request execute function error. */
  60. } eMBMasterEventType;
  61. typedef enum
  62. {
  63. EV_ERROR_RESPOND_TIMEOUT, /*!< Slave respond timeout. */
  64. EV_ERROR_RECEIVE_DATA, /*!< Receive frame data erroe. */
  65. EV_ERROR_EXECUTE_FUNCTION, /*!< Execute function error. */
  66. } eMBMasterErrorEventType;
  67. /*! \ingroup modbus
  68. * \brief Parity used for characters in serial mode.
  69. *
  70. * The parity which should be applied to the characters sent over the serial
  71. * link. Please note that this values are actually passed to the porting
  72. * layer and therefore not all parity modes might be available.
  73. */
  74. typedef enum
  75. {
  76. MB_PAR_NONE, /*!< No parity. */
  77. MB_PAR_ODD, /*!< Odd parity. */
  78. MB_PAR_EVEN /*!< Even parity. */
  79. } eMBParity;
  80. /* ----------------------- Supporting functions -----------------------------*/
  81. BOOL xMBPortEventInit( void );
  82. BOOL xMBPortEventPost( eMBEventType eEvent );
  83. BOOL xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
  84. BOOL xMBMasterPortEventInit( void );
  85. BOOL xMBMasterPortEventPost( eMBMasterEventType eEvent );
  86. BOOL xMBMasterPortEventGet( /*@out@ */ eMBMasterEventType * eEvent );
  87. void vMBMasterOsResInit( void );
  88. BOOL xMBMasterRunResTake( int32_t time );
  89. void vMBMasterRunResRelease( void );
  90. /* ----------------------- Serial port functions ----------------------------*/
  91. BOOL xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
  92. UCHAR ucDataBits, eMBParity eParity );
  93. void vMBPortClose( void );
  94. void xMBPortSerialClose( void );
  95. void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
  96. INLINE BOOL xMBPortSerialGetByte( CHAR * pucByte );
  97. INLINE BOOL xMBPortSerialPutByte( CHAR ucByte );
  98. INLINE BOOL xMBPortSerialPutBytes( volatile UCHAR *ucByte, USHORT usSize );
  99. BOOL xMBMasterPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
  100. UCHAR ucDataBits, eMBParity eParity );
  101. void vMBMasterPortClose( void );
  102. void xMBMasterPortSerialClose( void );
  103. void vMBMasterPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
  104. INLINE BOOL xMBMasterPortSerialGetByte( CHAR * pucByte );
  105. INLINE BOOL xMBMasterPortSerialPutByte( CHAR ucByte );
  106. INLINE BOOL xMBMasterPortSerialPutBytes( volatile UCHAR *ucByte, USHORT usSize );
  107. /* ----------------------- Timers functions ---------------------------------*/
  108. BOOL xMBPortTimersInit( USHORT usTimeOut50us );
  109. void xMBPortTimersClose( void );
  110. INLINE void vMBPortTimersEnable( void );
  111. INLINE void vMBPortTimersDisable( void );
  112. BOOL xMBMasterPortTimersInit( USHORT usTimeOut50us );
  113. void xMBMasterPortTimersClose( void );
  114. INLINE void vMBMasterPortTimersT35Enable( void );
  115. INLINE void vMBMasterPortTimersConvertDelayEnable( void );
  116. INLINE void vMBMasterPortTimersRespondTimeoutEnable( void );
  117. INLINE void vMBMasterPortTimersDisable( void );
  118. /* ----------------- Callback for the master error process ------------------*/
  119. void vMBMasterErrorCBRespondTimeout( UCHAR ucDestAddress, const UCHAR* pucPDUData,
  120. USHORT ucPDULength );
  121. void vMBMasterErrorCBReceiveData( UCHAR ucDestAddress, const UCHAR* pucPDUData,
  122. USHORT ucPDULength );
  123. void vMBMasterErrorCBExecuteFunction( UCHAR ucDestAddress, const UCHAR* pucPDUData,
  124. USHORT ucPDULength );
  125. void vMBMasterCBRequestSuccess( void );
  126. /* ----------------------- Callback for the protocol stack ------------------*/
  127. /*!
  128. * \brief Callback function for the porting layer when a new byte is
  129. * available.
  130. *
  131. * Depending upon the mode this callback function is used by the RTU or
  132. * ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
  133. * must immediately return a new character.
  134. *
  135. * \return <code>TRUE</code> if a event was posted to the queue because
  136. * a new byte was received. The port implementation should wake up the
  137. * tasks which are currently blocked on the eventqueue.
  138. */
  139. extern BOOL( *pxMBFrameCBByteReceived ) ( void );
  140. extern BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
  141. extern BOOL( *pxMBPortCBTimerExpired ) ( void );
  142. extern BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
  143. extern BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
  144. extern BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
  145. /* ----------------------- TCP port functions -------------------------------*/
  146. BOOL xMBTCPPortInit( USHORT usTCPPort );
  147. void vMBTCPPortClose( void );
  148. void vMBTCPPortDisable( void );
  149. BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
  150. BOOL xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
  151. #ifdef __cplusplus
  152. PR_END_EXTERN_C
  153. #endif
  154. #endif