gfambrtucom.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // gfambrtucom.h :
  2. //
  3. #if !defined(AGD_GFAMODBUS_H__C0642473_3513_4FB4_9D7B_F81E8ACC2C34__INCLUDED_)
  4. #define AGD_GFAMODBUS_H__C0642473_3513_4FB4_9D7B_F81E8ACC2C34__INCLUDED_
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #include <stdlib.h>
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif // __cplusplus
  11. /////////////////////////////////////////////////////////////////////////////
  12. // gfambrtucom.h - Declarations:
  13. #ifndef __BYTE_ORDER__
  14. #define __ORDER_LITTLE_ENDIAN__ 1234
  15. #define __ORDER_BIG_ENDIAN__ 4321
  16. #define __ORDER_PDP_ENDIAN__ 3412
  17. #if defined(__little_endian__)
  18. #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
  19. #elif defined(__big_endian__)
  20. #define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
  21. #else
  22. #error No endianess defined!
  23. #endif // __LITTLE_ENDIAN__
  24. #endif // __BYTE_ORDER__
  25. /////////////////////////////////////////////////////////////////////////////
  26. #ifndef _countof
  27. #define _countof(a) (sizeof(a) / sizeof(*a))
  28. #endif // _countof
  29. /////////////////////////////////////////////////////////////////////////////
  30. /////////////////////////////////////////////////////////////////////////////
  31. /////////////////////////////////////////////////////////////////////////////
  32. // Modbus RTU common defines
  33. #define MB_FUNC_READ_COIL_STATUS ((uint8_t)0x01)
  34. #define MB_FUNC_READ_INPUT_STATUS ((uint8_t)0x02)
  35. #define MB_FUNC_READ_HOLDING_REGISTERS ((uint8_t)0x03)
  36. #define MB_FUNC_READ_INPUT_REGISTERS ((uint8_t)0x04)
  37. #define MB_FUNC_FORCE_SINGLE_COIL ((uint8_t)0x05)
  38. #define MB_FUNC_PRESET_SINGLE_REGISTER ((uint8_t)0x06)
  39. #define MB_FUNC_READ_EXCEPTION_STATUS ((uint8_t)0x07)
  40. #define MB_FUNC_DIAGNOSTIC ((uint8_t)0x08)
  41. #define MB_FUNC_FETCH_COMM_EVENT_CTR ((uint8_t)0x0B)
  42. #define MB_FUNC_FETCH_COMM_EVENT_LOG ((uint8_t)0x0C)
  43. #define MB_FUNC_FORCE_MULTIPLE_COILS ((uint8_t)0x0F)
  44. #define MB_FUNC_WRITE_MULTIPLE_REGISTERS ((uint8_t)0x10)
  45. #define MB_FUNC_REPORT_SLAVE_ID ((uint8_t)0x11)
  46. #define MB_FUNC_READ_GENERAL_REFERENCE ((uint8_t)0x14)
  47. #define MB_FUNC_WRITE_GENERAL_REFERENCE ((uint8_t)0x15)
  48. #define MB_FUNC_MASK_WRITE_4X_REGISTER ((uint8_t)0x16)
  49. #define MB_FUNC_READ_WRITE_4X_REGISTERS ((uint8_t)0x17)
  50. #define MB_FUNC_READ_FIFO_QUEUE ((uint8_t)0x18)
  51. #define MB_FUNC_ERROR_FLAG ((uint8_t)0x80)
  52. /////////////////////////////////////////////////////////////////////////////
  53. #define MB_SUBFUNC_RETURN_QUERY_DATA ((uint16_t)0x00)
  54. #define MB_SUBFUNC_RESTART_COMM_OPTION ((uint16_t)0x01)
  55. #define MB_SUBFUNC_RETURN_DIAGNOSTIC_REGISTER ((uint16_t)0x02)
  56. #define MB_SUBFUNC_FORCE_LISTEN_ONLY_MODE ((uint16_t)0x04)
  57. #define MB_SUBFUNC_CLEAR_CTRS_AND_DIAGNOSTIC_REG ((uint16_t)0x0A)
  58. #define MB_SUBFUNC_RETURN_BUS_MESSAGE_COUNT ((uint16_t)0x0B)
  59. #define MB_SUBFUNC_RETURN_BUS_COMM_ERROR_COUNT ((uint16_t)0x0C)
  60. #define MB_SUBFUNC_RETURN_BUS_EXCEPTION_ERROR_COUNT ((uint16_t)0x0D)
  61. #define MB_SUBFUNC_RETURN_SLAVE_MESSAGE_COUNT ((uint16_t)0x0E)
  62. #define MB_SUBFUNC_RETURN_SLAVE_NO_RESPONSE_COUNT ((uint16_t)0x0F)
  63. #define MB_SUBFUNC_RETURN_SLAVE_NAK_COUNT ((uint16_t)0x10)
  64. #define MB_SUBFUNC_RETURN_SLAVE_BUSY_COUNT ((uint16_t)0x11)
  65. #define MB_SUBFUNC_RETURN_BUS_CHAR_OVERRUN_COUNT ((uint16_t)0x12)
  66. #define MB_SUBFUNC_RETURN_OVERRUN_ERROR_COUNT ((uint16_t)0x13)
  67. #define MB_SUBFUNC_CLEAR_OVERRUN_COUNTER_AND_FLAG ((uint16_t)0x14)
  68. /////////////////////////////////////////////////////////////////////////////
  69. #define MB_ERROR_ILLEGAL_FUNCTION ((uint8_t)0x01)
  70. #define MB_ERROR_ILLEGAL_DATA_ADDRESS ((uint8_t)0x02)
  71. #define MB_ERROR_ILLEGAL_DATA_VALUE ((uint8_t)0x03)
  72. #define MB_ERROR_SLAVE_DEVICE_FAILURE ((uint8_t)0x04)
  73. #define MB_ERROR_ACKNOWLEDGE ((uint8_t)0x05)
  74. #define MB_ERROR_SLAVE_DEVICE_BUSY ((uint8_t)0x06)
  75. #define MB_ERROR_NEGATIVE_ACKNOWLEDGE ((uint8_t)0x07)
  76. #define MB_ERROR_MEMORY_PARITY_ERROR ((uint8_t)0x08)
  77. /////////////////////////////////////////////////////////////////////////////
  78. #define MODBUS_BROADCAST_ADDRESS 0
  79. #define MODBUS_MAX_SLAVE_ID 247
  80. #define MODBUS_MAX_READ_BITS 2000
  81. #define MODBUS_MAX_WRITE_BITS 1968
  82. #define MODBUS_MAX_READ_REGISTERS 125
  83. #define MODBUS_MAX_WRITE_REGISTERS 123
  84. #define MODBUS_MAX_RW_WRITE_REGISTERS 121
  85. #define MODBUS_MAX_DATA_PAYLOAD 252
  86. /////////////////////////////////////////////////////////////////////////////
  87. typedef struct _MODBUS_RTU_PDU
  88. {
  89. uint8_t func;
  90. union
  91. {
  92. uint8_t b[254];
  93. uint16_t w[127];
  94. };
  95. }__attribute__ ((__packed__)) MODBUS_RTU_PDU, *LPMODBUS_RTU_PDU;
  96. typedef const MODBUS_RTU_PDU *LPCMODBUS_RTU_PDU;
  97. typedef struct _MODBUS_RTU_ADU
  98. {
  99. uint8_t slaveID;
  100. MODBUS_RTU_PDU pdu;
  101. }__attribute__ ((__aligned__(2),__packed__)) MODBUS_RTU_ADU, *LPMODBUS_RTU_ADU;
  102. typedef const MODBUS_RTU_ADU *LPCMODBUS_RTU_ADU;
  103. /////////////////////////////////////////////////////////////////////////////
  104. /////////////////////////////////////////////////////////////////////////////
  105. /////////////////////////////////////////////////////////////////////////////
  106. // Fifo
  107. typedef void *HFIFO;
  108. typedef bool (*PFN_GFA_INT_ENABLE) (bool);
  109. typedef bool (*PFN_GFA_RX_PREPARE) (void);
  110. typedef void (*PFN_GFA_RX_START) (void);
  111. typedef bool (*PFN_GFA_RX_FINALIZE) (void);
  112. typedef bool (*PFN_GFA_TX_PREPARE) (void);
  113. typedef void (*PFN_GFA_TX_START) (void);
  114. typedef bool (*PFN_GFA_TX_FINALIZE) (void);
  115. /////////////////////////////////////////////////////////////////////////////
  116. typedef struct _GFA_FIFO_BACKEND
  117. {
  118. PFN_GFA_INT_ENABLE pfnLockBackend;
  119. PFN_GFA_RX_PREPARE pfnRxPrepare;
  120. PFN_GFA_RX_START pfnRxStart;
  121. PFN_GFA_RX_FINALIZE pfnRxFinalize;
  122. PFN_GFA_TX_PREPARE pfnTxPrepare;
  123. PFN_GFA_TX_START pfnTxStart;
  124. PFN_GFA_TX_FINALIZE pfnTxFinalize;
  125. }GFA_FIFO_BACKEND, *LPGFA_FIFO_BACKEND;
  126. typedef const GFA_FIFO_BACKEND *LPCGFA_FIFO_BACKEND;
  127. /////////////////////////////////////////////////////////////////////////////
  128. HFIFO GfaMbFifoCreate(int nFifoIndex, LPCGFA_FIFO_BACKEND pBackend);
  129. void GfaMbFifoRelease(HFIFO hFifo);
  130. void GfaMbFifoReset(HFIFO hFifo, bool bLock);
  131. size_t GfaMbFifoRead(HFIFO hFifo, void *pData, size_t nCbData, bool bLock);
  132. size_t GfaMbFifoWrite(HFIFO hFifo, const void *pData, size_t nCbData, bool bLock);
  133. bool GfaMbFifoPop(HFIFO hFifo, uint8_t *b, bool bLock);
  134. bool GfaMbFifoPush(HFIFO hFifo, uint8_t b, bool bLock);
  135. size_t GfaMbFifoPeek(HFIFO hFifo, bool bLock);
  136. bool GfaMbFifoCanWrite(HFIFO hFifo, bool bLock);
  137. bool GfaMbFifoEmpty(HFIFO hFifo, bool bLock);
  138. void GfaMbFifoSetFlags(HFIFO hFifo, uint32_t nFlags, bool bLock);
  139. void GfaMbFifoClearFlags(HFIFO hFifo, uint32_t nFlags, bool bLock);
  140. uint32_t GfaMbFifoGetFlags(HFIFO hFifo, bool bLock);
  141. bool GfaMbFifoMatchFlags(HFIFO hFifo, uint32_t nFlags, bool bLock);
  142. bool GfaMbFifoRxPrepare(HFIFO hFifo, bool bLock);
  143. void GfaMbFifoRxStart(HFIFO hFifo, bool bLock);
  144. bool GfaMbFifoRxFinalize(HFIFO hFifo, bool bLock);
  145. bool GfaMbFifoTxPrepare(HFIFO hFifo, bool bLock);
  146. void GfaMbFifoTxStart(HFIFO hFifo, bool bLock);
  147. bool GfaMbFifoTxFinalize(HFIFO hFifo, bool bLock);
  148. uint16_t GfaMbFifoCalcCRC(HFIFO hFifo, bool bLock);
  149. /////////////////////////////////////////////////////////////////////////////
  150. /////////////////////////////////////////////////////////////////////////////
  151. /////////////////////////////////////////////////////////////////////////////
  152. // UART
  153. typedef enum _GfA_UART_Parity
  154. {
  155. P_None,
  156. P_Even,
  157. P_Odd,
  158. P_Zero,
  159. P_One
  160. }GfA_UART_Parity;
  161. /////////////////////////////////////////////////////////////////////////////
  162. typedef struct _GFA_MODBUS_PROTOCOL_TIMEOUTS
  163. {
  164. uint64_t nCharTimeoutUs;
  165. uint64_t nFrameTimeoutUs;
  166. }GFA_MODBUS_PROTOCOL_TIMEOUTS, *LPGFA_MODBUS_PROTOCOL_TIMEOUTS;
  167. typedef const GFA_MODBUS_PROTOCOL_TIMEOUTS *LPCGFA_MODBUS_PROTOCOL_TIMEOUTS;
  168. /////////////////////////////////////////////////////////////////////////////
  169. inline bool GfaMbCalcProtocolTimeouts(uint32_t nBaud, LPGFA_MODBUS_PROTOCOL_TIMEOUTS pmpt) {
  170. if(nBaud && pmpt) {
  171. if(nBaud <= 19200) {
  172. pmpt->nFrameTimeoutUs = (350000000 / nBaud + 5) / 10;
  173. pmpt->nCharTimeoutUs = (150000000 / nBaud + 5) / 10;
  174. }
  175. else {
  176. // For baud rates greater than 19200 the t1.5 and t3.5 timers are implemented
  177. // using fixed values, for t1.5 that is 750μs, for t3.5 1.750ms
  178. pmpt->nFrameTimeoutUs = 1750;
  179. pmpt->nCharTimeoutUs = 750;
  180. }
  181. return true;
  182. }
  183. return false;
  184. }
  185. /////////////////////////////////////////////////////////////////////////////
  186. typedef struct _GFA_UART_CONFIG
  187. {
  188. uint32_t P_UART_BASE;
  189. uint32_t P_UART_BASE_SYSCTL;
  190. uint32_t P_UART_PORT;
  191. uint32_t P_UART_PORT_SYSCTL;
  192. uint32_t P_UART_RX_PIN;
  193. uint32_t P_UART_RX_PIN_MUX;
  194. uint32_t P_UART_TX_PIN;
  195. uint32_t P_UART_TX_PIN_MUX;
  196. uint32_t P_UART_INT;
  197. uint8_t P_UART_INT_PRIORITY;
  198. uint32_t P_EN_485_PORT_SYSCTL;
  199. uint32_t P_EN_485_PORT;
  200. uint32_t P_EN_485_PIN;
  201. uint32_t nBaud;
  202. int nDatabits;
  203. int nStopbits;
  204. GfA_UART_Parity parity;
  205. int nFifoIndexRx;
  206. int nFifoIndexTx;
  207. }GFA_UART_CONFIG, *LPGFA_UART_CONFIG;
  208. typedef const GFA_UART_CONFIG *LPCGFA_UART_CONFIG;
  209. /////////////////////////////////////////////////////////////////////////////
  210. typedef struct _GFA_UART_STATUS_COUNTERS
  211. {
  212. uint64_t nCharsReceived;
  213. uint64_t nCharsTransmitted;
  214. uint32_t nOverrunErrors;
  215. uint32_t nBreakErrors;
  216. uint32_t nParityErrors;
  217. uint32_t nFramingErrors;
  218. }GFA_UART_STATUS_COUNTERS, *LPGFA_UART_STATUS_COUNTERS;
  219. typedef const GFA_UART_STATUS_COUNTERS *LPCGFA_UART_STATUS_COUNTERS;
  220. /////////////////////////////////////////////////////////////////////////////
  221. #define MB_RTU_FLAG_FRAME_GAP_DETECT 0x00000001
  222. #define MB_RTU_FLAG_TRANSMIT_IN_PROGRESS 0x00000002
  223. #define MB_RTU_FLAG_TRANSMIT_END 0x00000004
  224. #define MB_RTU_FLAG_IGNORE_FRAME 0x00000008
  225. #define MB_RTU_FLAG_OVERRUN_ERROR 0x00000010
  226. #define MB_RTU_FLAG_BREAK_ERROR 0x00000020
  227. #define MB_RTU_FLAG_PARITY_ERROR 0x00000040
  228. #define MB_RTU_FLAG_FRAMING_ERROR 0x00000080
  229. /////////////////////////////////////////////////////////////////////////////
  230. bool GfaMbUartInit(LPCGFA_UART_CONFIG pCfg);
  231. void GfaMbUartRelease(void);
  232. HFIFO GfaMbUartGetRxFifo(void);
  233. HFIFO GfaMbUartGetTxFifo(void);
  234. void GfaMbUartGetProtocolTimeouts(LPGFA_MODBUS_PROTOCOL_TIMEOUTS pmpt);
  235. void GfaMbUartGetStatusCounters(LPGFA_UART_STATUS_COUNTERS pSc);
  236. void GfaMbUartResetStatusCounters(void);
  237. /////////////////////////////////////////////////////////////////////////////
  238. /////////////////////////////////////////////////////////////////////////////
  239. /////////////////////////////////////////////////////////////////////////////
  240. // Buffer helper functions
  241. void GfaBufCpyUnaligned_uint16_LE(void *pTo, const void *pFrom, size_t nCntWords);
  242. void GfaBufCpyUnaligned_uint16_BE(void *pTo, const void *pFrom, size_t nCntWords);
  243. void GfaBufCpyUnaligned_uint32_LE(void *pTo, const void *pFrom, size_t nCntDWords);
  244. void GfaBufCpyUnaligned_uint32_BE(void *pTo, const void *pFrom, size_t nCntDWords);
  245. uint16_t GfaBufGetUnaligned_uint16_LE(const void *pData);
  246. uint16_t GfaBufGetUnaligned_uint16_BE(const void *pData);
  247. void GfaBufSetUnaligned_uint16_LE(void *pData, uint16_t nVal);
  248. void GfaBufSetUnaligned_uint16_BE(void *pData, uint16_t nVal);
  249. uint32_t GfaBufGetUnaligned_uint32_LE(const void *pData);
  250. uint32_t GfaBufGetUnaligned_uint32_BE(const void *pData);
  251. void GfaBufSetUnaligned_uint32_LE(void *pData, uint16_t nVal);
  252. void GfaBufSetUnaligned_uint32_BE(void *pData, uint16_t nVal);
  253. float GfaBufGetUnaligned_float(const void *pData);
  254. void GfaBufSetUnaligned_float(void *pData, float fVal);
  255. double GfaBufGetUnaligned_double(const void *pData);
  256. void GfaBufSetUnaligned_double(void *pData, double fVal);
  257. bool GfaBufVerifyCRC(const void *pData, size_t nCbData, const void *pCRC);
  258. void GfaBufSetCRC(const void *pData, size_t nCbData, void *pCRC);
  259. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  260. #define GfaBufCpyUnaligned_uint16 GfaBufCpyUnaligned_uint16_LE
  261. #define GfaBufCpyUnaligned_uint32 GfaBufCpyUnaligned_uint32_LE
  262. #define GfaBufGetUnaligned_uint16 GfaBufGetUnaligned_uint16_LE
  263. #define GfaBufSetUnaligned_uint16 GfaBufSetUnaligned_uint16_LE
  264. #define GfaBufGetUnaligned_uint32 GfaBufGetUnaligned_uint32_LE
  265. #define GfaBufSetUnaligned_uint32 GfaBufSetUnaligned_uint32_LE
  266. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  267. #define GfaBufCpyUnaligned_uint16 GfaBufCpyUnaligned_uint16_BE
  268. #define GfaBufCpyUnaligned_uint32 GfaBufCpyUnaligned_uint32_BE
  269. #define GfaBufGetUnaligned_uint16 GfaBufGetUnaligned_uint16_BE
  270. #define GfaBufSetUnaligned_uint16 GfaBufSetUnaligned_uint16_BE
  271. #define GfaBufGetUnaligned_uint32 GfaBufGetUnaligned_uint32_BE
  272. #define GfaBufSetUnaligned_uint32 GfaBufSetUnaligned_uint32_BE
  273. #endif // __BYTE_ORDER__
  274. /////////////////////////////////////////////////////////////////////////////
  275. #ifdef __cplusplus
  276. }
  277. #endif // __cplusplus
  278. #endif // !defined(AGD_GFAMODBUS_H__C0642473_3513_4FB4_9D7B_F81E8ACC2C34__INCLUDED_)