port.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * FreeModbus Libary: BARE Port
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: port.h ,v 1.60 2013/08/13 15:07:05 Armink add Master Functions $
  20. * port.h ,v 1.60 2022/07/17 quanghona <lyhonquang@gmail.com> CMSIS-RTOS2 port $
  21. */
  22. #ifndef _PORT_H
  23. #define _PORT_H
  24. #include "mbconfig.h"
  25. #include <assert.h>
  26. #include <inttypes.h>
  27. #include "main.h"
  28. #include "cmsis_os2.h"
  29. #define INLINE
  30. #define PR_BEGIN_EXTERN_C extern "C" {
  31. #define PR_END_EXTERN_C }
  32. #define ENTER_CRITICAL_SECTION() EnterCriticalSection()
  33. #define EXIT_CRITICAL_SECTION() ExitCriticalSection()
  34. typedef uint8_t BOOL;
  35. typedef unsigned char UCHAR;
  36. typedef char CHAR;
  37. typedef uint16_t USHORT;
  38. typedef int16_t SHORT;
  39. typedef uint32_t ULONG;
  40. typedef int32_t LONG;
  41. #ifndef TRUE
  42. #define TRUE 1
  43. #endif
  44. #ifndef FALSE
  45. #define FALSE 0
  46. #endif
  47. #define MB_SER_PDU_SIZE_MAX 256 /*!< Maximum size of a Modbus RTU frame. */
  48. /* ----------------------- Type definitions ---------------------------------*/
  49. typedef enum {
  50. STATE_RX_INIT, /*!< Receiver is in initial state. */
  51. STATE_RX_IDLE, /*!< Receiver is in idle state. */
  52. STATE_RX_RCV, /*!< Frame is beeing received. */
  53. STATE_RX_ERROR /*!< If the frame is invalid. */
  54. } eMBRcvState;
  55. typedef enum {
  56. STATE_TX_IDLE, /*!< Transmitter is in idle state. */
  57. STATE_TX_XMIT /*!< Transmitter is in transfer state. */
  58. } eMBSndState;
  59. /*! \ingroup modbus
  60. * \brief Errorcodes used by all function in the protocol stack.
  61. */
  62. typedef enum {
  63. MB_ENOERR, /*!< no error. */
  64. MB_ENOREG, /*!< illegal register address. */
  65. MB_EINVAL, /*!< illegal argument. */
  66. MB_EPORTERR, /*!< porting layer error. */
  67. MB_ENORES, /*!< insufficient resources. */
  68. MB_EIO, /*!< I/O error. */
  69. MB_EILLSTATE, /*!< protocol stack in illegal state. */
  70. MB_ETIMEDOUT /*!< timeout error occurred. */
  71. } eMBErrorCode;
  72. /*! \ingroup modbus
  73. * \brief If register should be written or read.
  74. *
  75. * This value is passed to the callback functions which support either
  76. * reading or writing register values. Writing means that the application
  77. * registers should be updated and reading means that the modbus protocol
  78. * stack needs to know the current register values.
  79. *
  80. * \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and
  81. * eMBRegInputCB( ).
  82. */
  83. typedef enum {
  84. MB_REG_READ, /*!< Read register values and pass to protocol stack. */
  85. MB_REG_WRITE /*!< Update register values. */
  86. } eMBRegisterMode;
  87. typedef eMBErrorCode (*eMBRegInputCB)(UCHAR *pucRegBuffer, USHORT usAddress,
  88. USHORT usNRegs);
  89. typedef eMBErrorCode (*eMBRegHoldingCB)(UCHAR *pucRegBuffer, USHORT usAddress,
  90. USHORT usNRegs, eMBRegisterMode eMode);
  91. typedef eMBErrorCode (*eMBRegCoilsCB)(UCHAR *pucRegBuffer, USHORT usAddress,
  92. USHORT usNCoils, eMBRegisterMode eMode);
  93. typedef eMBErrorCode (*eMBRegDiscreteCB)(UCHAR *pucRegBuffer, USHORT usAddress,
  94. USHORT usNDiscrete);
  95. typedef eMBErrorCode (*eMBWriteFileCB)(UCHAR *header, UCHAR *pucRegBuffer, USHORT *usLen);
  96. typedef eMBErrorCode (*eMBWriteDeviceSerialCB)(UCHAR *header, UCHAR *pucRegBuffer, USHORT *usLen);
  97. /**
  98. * 定义寄存器操作回调函数结构体
  99. */
  100. typedef struct {
  101. eMBRegInputCB eMbRegInputCb;
  102. eMBRegHoldingCB eMbRegHoldingCb;
  103. eMBRegDiscreteCB eMbRegDiscreteCb;
  104. eMBWriteFileCB eMbWriteFileCb;
  105. eMBRegCoilsCB eMbRegCoilsCb;
  106. eMBWriteDeviceSerialCB eMBWriteDeviceSerialCB;
  107. } mb_reg_callback_t;
  108. /**
  109. * 定义从站对象,用于支持多个从站
  110. */
  111. typedef struct {
  112. /**
  113. * 协议类型
  114. */
  115. uint8_t eMode;
  116. /**
  117. * 从站地址
  118. */
  119. uint8_t ucSlaveAddress;
  120. /**
  121. * 波特率
  122. */
  123. uint32_t ulBaudRate;
  124. /**
  125. * 校验位
  126. */
  127. uint8_t eParity;
  128. /**
  129. * UART句柄
  130. */
  131. UART_HandleTypeDef pUartHandle;
  132. /**
  133. * TIM句柄
  134. */
  135. TIM_HandleTypeDef pTimHandle;
  136. /**
  137. * 事件标志ID
  138. */
  139. osEventFlagsId_t xEventFlagsId;
  140. /**
  141. * 发送、接收使能IO端口
  142. */
  143. GPIO_TypeDef *enPort;
  144. /**
  145. * 发送、接收使能IO引脚
  146. */
  147. uint16_t enPin;
  148. volatile eMBSndState eSndState;
  149. volatile eMBRcvState eRcvState;
  150. volatile UCHAR ucRTUBuf[MB_SER_PDU_SIZE_MAX];
  151. volatile UCHAR *pucSndBufferCur;
  152. volatile USHORT usSndBufferCount;
  153. volatile USHORT usRcvBufferPos;
  154. mb_reg_callback_t *callback;
  155. UCHAR *ucMBFrame;
  156. UCHAR id;
  157. USHORT usLength;
  158. } eMBRTUSlaveObj;
  159. void EnterCriticalSection(void);
  160. void ExitCriticalSection(void);
  161. void prvvMBMasterUARTTxReadyISR(void);
  162. void prvvMBMasterUARTRxISR(void);
  163. void prvvMBMasterUARTRxReceiveCharISR(CHAR data);
  164. void prvvUARTTxReadyISR(eMBRTUSlaveObj *pObj);
  165. void prvvUARTRxISR(eMBRTUSlaveObj *pObj);
  166. void prvvUARTRxReceiveCharISR(CHAR data);
  167. #endif