gfambrtuslv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*! \file gfambrtuslv.h
  2. \brief Header file of the \b gfambrtuslv library.
  3. */
  4. #if !defined(AGD_MBRTUSLV_H__84F27BE0_71CA_4910_8631_B0EEA21DC84A__INCLUDED_)
  5. #define AGD_MBRTUSLV_H__84F27BE0_71CA_4910_8631_B0EEA21DC84A__INCLUDED_
  6. #include <gfautils.h>
  7. #include <gfambrtucom.h>
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif // __cplusplus
  11. /////////////////////////////////////////////////////////////////////////////
  12. // gfambrtuslv.h - Declarations:
  13. /*! \typedef HMBRTUSLV
  14. \brief Defines a Handle to a Modbus RTU Slave.
  15. */
  16. typedef void *HMBRTUSLV;
  17. /////////////////////////////////////////////////////////////////////////////
  18. /////////////////////////////////////////////////////////////////////////////
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Modbus Slave
  21. /*! \typedef GFA_MODBUS_RTU_SLV_STATES
  22. \brief Defines the Slave's states used in the state machine.
  23. \verbatim
  24. typedef enum _GFA_MODBUS_RTU_SLV_STATES
  25. {
  26. MB_RTU_SLV_Void = -1,
  27. MB_RTU_SLV_Idle,
  28. MB_RTU_SLV_RxSlvID,
  29. MB_RTU_SLV_RxFunc,
  30. MB_RTU_SLV_RxDataInfo,
  31. MB_RTU_SLV_RxDataPayload,
  32. MB_RTU_SLV_RxCRC,
  33. MB_RTU_SLV_RxComplete,
  34. MB_RTU_SLV_TxStart,
  35. MB_RTU_SLV_TxWaitEnd
  36. }GFA_MODBUS_RTU_SLV_STATES;
  37. \endverbatim
  38. */
  39. typedef enum _GFA_MODBUS_RTU_SLV_STATES
  40. {
  41. MB_RTU_SLV_Void = -1,
  42. MB_RTU_SLV_Idle,
  43. MB_RTU_SLV_RxSlvID,
  44. MB_RTU_SLV_RxFunc,
  45. MB_RTU_SLV_RxDataInfo,
  46. MB_RTU_SLV_RxDataPayload,
  47. MB_RTU_SLV_RxCRC,
  48. MB_RTU_SLV_RxComplete,
  49. MB_RTU_SLV_TxStart,
  50. MB_RTU_SLV_TxWaitEnd
  51. }GFA_MODBUS_RTU_SLV_STATES;
  52. /////////////////////////////////////////////////////////////////////////////
  53. /*! \typedef typedef void (*PFN_GFA_SLAVE_PRE_READ_REGISTERS)(uint8_t func, uint16_t start, uint16_t count)
  54. \brief Defines the pointer to a callback function, that is called before the slave attempts to read from registers.
  55. \param func The modbus function code.
  56. \param start The physical start address of the registers to be read.
  57. \param count The number of registers to be read.
  58. */
  59. typedef void (*PFN_GFA_SLAVE_PRE_READ_REGISTERS) (uint8_t func, uint16_t start, uint16_t count);
  60. /*! \typedef typedef void (*PFN_GFA_SLAVE_POST_READ_REGISTERS)(uint8_t func, uint16_t start, uint16_t count)
  61. \brief Defines the pointer to a callback function, that is called after the slave has read read from registers.
  62. \param func The modbus function code.
  63. \param start The physical start address of the registers to be read.
  64. \param count The number of registers to be read.
  65. */
  66. typedef void (*PFN_GFA_SLAVE_POST_READ_REGISTERS) (uint8_t func, uint16_t start, uint16_t count);
  67. /*! \typedef typedef void (*PFN_GFA_SLAVE_PRE_WRITE_REGISTERS)(uint8_t func, uint16_t start, uint16_t count)
  68. \brief Defines the pointer to a callback function, that is called before the slave attempts to write to registers.
  69. \param func The modbus function code.
  70. \param start The physical start address of the registers to be written to.
  71. \param count The number of registers to be written to.
  72. */
  73. typedef void (*PFN_GFA_SLAVE_PRE_WRITE_REGISTERS) (uint8_t func, uint16_t start, uint16_t count);
  74. /*! \typedef typedef void (*PFN_POST_WRITE_REGISTERS)(uint8_t func, uint16_t start, uint16_t count)
  75. \brief Defines the pointer to a callback function, that is called after the slave has written to registers.
  76. \param func The modbus function code.
  77. \param start The physical start address of the registers to be written to.
  78. \param count The number of registers to be written to.
  79. */
  80. typedef void (*PFN_POST_WRITE_REGISTERS) (uint8_t func, uint16_t start, uint16_t count);
  81. /*! \typedef typedef void (*PFN_GFA_SLAVE_PRE_TRANSMIT)(LPMODBUS_RTU_ADU padu)
  82. \brief Defines the pointer to a callback function, that is called before the slave sends the response to a previous request.
  83. \param padu A pointer to a MODBUS_RTU_ADU (Application Data Unit) structure to be sent. MODBUS_RTU_ADU is defined in \b gfambrtucom.h.
  84. */
  85. typedef void (*PFN_GFA_SLAVE_PRE_TRANSMIT) (LPMODBUS_RTU_ADU padu);
  86. /*! \typedef typedef void (*PFN_GFA_SLAVE_POST_TRANSMIT)(LPMODBUS_RTU_ADU padu)
  87. \brief Defines the pointer to a callback function, that is called after the slave has sent the response to a previous request.
  88. \param padu A pointer to a MODBUS_RTU_ADU (Application Data Unit) structure that has been sent. MODBUS_RTU_ADU is defined in \b gfambrtucom.h.
  89. */
  90. typedef void (*PFN_GFA_SLAVE_POST_TRANSMIT) (LPMODBUS_RTU_ADU padu);
  91. /*! \typedef typedef uint16_t (*PFN_GFA_SLAVE_MAP_REG_ADDR)(uint16_t addr)
  92. \brief Defines the pointer to a callback function, that is called to map a modbus register address to a physical register address (index). If not implemented, the modbus address is interpreted as physical address
  93. \param addr Modbus address to be mapped.
  94. \return The mapped physical address.
  95. */
  96. typedef uint16_t (*PFN_GFA_SLAVE_MAP_REG_ADDR) (uint16_t addr);
  97. /*! \typedef typedef void (*PFN_GFA_SLAVE_STATE_CHANGED)(GFA_MODBUS_RTU_SLV_STATES newState, GFA_MODBUS_RTU_SLV_STATES oldState)
  98. \brief Defines the pointer to a callback function, that is called after the slave's state has changed.
  99. \param newState The new state of the slave.
  100. \param oldState The previous state of the slave.
  101. */
  102. typedef void (*PFN_GFA_SLAVE_STATE_CHANGED) (GFA_MODBUS_RTU_SLV_STATES newState, GFA_MODBUS_RTU_SLV_STATES oldState);
  103. /////////////////////////////////////////////////////////////////////////////
  104. /*! \typedef GFA_MODBUS_REGISTER
  105. \brief Struct that holds information about the slave's registers.
  106. \verbatim
  107. typedef struct _GFA_MODBUS_REGISTER
  108. {
  109. volatile uint16_t *pRegs; // pointer to an array of registers
  110. size_t nCountRegs; // number of registers
  111. }GFA_MODBUS_REGISTER, *LPGFA_MODBUS_REGISTER;
  112. typedef const GFA_MODBUS_REGISTER *LPCGFA_MODBUS_REGISTER;
  113. \endverbatim
  114. */
  115. typedef struct _GFA_MODBUS_REGISTER
  116. {
  117. volatile uint16_t *pRegs;
  118. size_t nCountRegs;
  119. }GFA_MODBUS_REGISTER, *LPGFA_MODBUS_REGISTER;
  120. typedef const GFA_MODBUS_REGISTER *LPCGFA_MODBUS_REGISTER;
  121. /////////////////////////////////////////////////////////////////////////////
  122. /*! \typedef GFA_MODBUS_SLAVE_APP_INTERFACE
  123. \brief Defines a set of functions to interact with a Modbus application. Any of these function pointers may be NULL, if not needed.
  124. \verbatim
  125. typedef struct _GFA_MODBUS_SLAVE_APP_INTERFACE
  126. {
  127. PFN_GFA_SLAVE_MAP_REG_ADDR pfnMapRegAddr; // maps a modbus register address to a hardware address.
  128. PFN_GFA_SLAVE_PRE_READ_REGISTERS pfnPreRead; // called before the slave attempts to read from registers
  129. PFN_GFA_SLAVE_POST_READ_REGISTERS pfnPostRead; // called after the slave has read from registers
  130. PFN_GFA_SLAVE_PRE_WRITE_REGISTERS pfnPreWrite; // called before the slave attempts to write to registers
  131. PFN_POST_WRITE_REGISTERS pfnPostWrite; // called after the slave has written to registers
  132. PFN_GFA_SLAVE_PRE_TRANSMIT pfnPreTransmit; // called before the slave sends the response to a request
  133. PFN_GFA_SLAVE_POST_TRANSMIT pfnPostTransmit; // called after the slave has sent the response to a request
  134. PFN_GFA_SLAVE_STATE_CHANGED pfnStateChanged; // called if the slave's state has changed
  135. }GFA_MODBUS_SLAVE_APP_INTERFACE, *LPGFA_MODBUS_SLAVE_APP_INTERFACE;
  136. typedef const GFA_MODBUS_SLAVE_APP_INTERFACE *LPCGFA_MODBUS_SLAVE_APP_INTERFACE;
  137. \endverbatim
  138. */
  139. typedef struct _GFA_MODBUS_SLAVE_APP_INTERFACE
  140. {
  141. PFN_GFA_SLAVE_MAP_REG_ADDR pfnMapRegAddr;
  142. PFN_GFA_SLAVE_PRE_READ_REGISTERS pfnPreRead;
  143. PFN_GFA_SLAVE_POST_READ_REGISTERS pfnPostRead;
  144. PFN_GFA_SLAVE_PRE_WRITE_REGISTERS pfnPreWrite;
  145. PFN_POST_WRITE_REGISTERS pfnPostWrite;
  146. PFN_GFA_SLAVE_PRE_TRANSMIT pfnPreTransmit;
  147. PFN_GFA_SLAVE_POST_TRANSMIT pfnPostTransmit;
  148. PFN_GFA_SLAVE_STATE_CHANGED pfnStateChanged;
  149. }GFA_MODBUS_SLAVE_APP_INTERFACE, *LPGFA_MODBUS_SLAVE_APP_INTERFACE;
  150. typedef const GFA_MODBUS_SLAVE_APP_INTERFACE *LPCGFA_MODBUS_SLAVE_APP_INTERFACE;
  151. /////////////////////////////////////////////////////////////////////////////
  152. /*! \typedef GFA_MODBUS_RTU_SLAVE_PARAMETERS
  153. \brief Used to create a Modbus RTU Slave
  154. \verbatim
  155. typedef struct _GFA_MODBUS_RTU_SLAVE_PARAMETERS
  156. {
  157. uint8_t slaveID; // the slave id
  158. HFIFO hFifoRX; // handle to the receive fifo buffer
  159. HFIFO hFifoTX; // handle to the transmit fifo buffer
  160. GFA_MODBUS_REGISTER regMap; // register information
  161. GFA_MODBUS_SLAVE_APP_INTERFACE appItf; // app interface
  162. }GFA_MODBUS_RTU_SLAVE_PARAMETERS, *LPGFA_MODBUS_RTU_SLAVE_PARAMETERS;
  163. typedef const GFA_MODBUS_RTU_SLAVE_PARAMETERS *LPCGFA_MODBUS_RTU_SLAVE_PARAMETERS;
  164. \endverbatim
  165. */
  166. typedef struct _GFA_MODBUS_RTU_SLAVE_PARAMETERS
  167. {
  168. uint8_t slaveID;
  169. HFIFO hFifoRX;
  170. HFIFO hFifoTX;
  171. GFA_MODBUS_REGISTER regMap;
  172. GFA_MODBUS_SLAVE_APP_INTERFACE appItf;
  173. }GFA_MODBUS_RTU_SLAVE_PARAMETERS, *LPGFA_MODBUS_RTU_SLAVE_PARAMETERS;
  174. typedef const GFA_MODBUS_RTU_SLAVE_PARAMETERS *LPCGFA_MODBUS_RTU_SLAVE_PARAMETERS;
  175. /////////////////////////////////////////////////////////////////////////////
  176. /*! \fn HMBRTUSLV GfaModbusRTUSlvCreate(LPCGFA_MODBUS_RTU_SLAVE_PARAMETERS pslp)
  177. \brief Creates and initializes a Modbus RTU Slave.
  178. \param pslp A const pointer to a \ref GFA_MODBUS_RTU_SLAVE_PARAMETERS structure.
  179. \return If successful, returns a handle to the newly created slave, or NULL in the case of an error.
  180. */
  181. HMBRTUSLV GfaModbusRTUSlvCreate(LPCGFA_MODBUS_RTU_SLAVE_PARAMETERS pslp);
  182. /*! \fn void GfaModbusRTUSlvRelease(HMBRTUSLV hMbSlv)
  183. \brief Releases a modbus slave, that was previously created with \ref GfaModbusRTUSlvCreate.
  184. \param hMbSlv Handle to the modbus slave that has been acquired by a call to \ref GfaModbusRTUSlvCreate.
  185. */
  186. void GfaModbusRTUSlvRelease(HMBRTUSLV hMbSlv);
  187. /*! \fn bool GfaModbusRTUSlvStateMachine(HMBRTUSLV hMbSlv)
  188. \brief The modbus slave's state machine. This function is the heart beat of the slave and must be called repeatedly.
  189. \param hMbSlv Handle to the modbus slave that has been acquired by a call to \ref GfaModbusRTUSlvCreate.
  190. \return \b true if successful, or \b false only if hMbSlv is NULL or an invalid state was detected.
  191. */
  192. bool GfaModbusRTUSlvStateMachine(HMBRTUSLV hMbSlv);
  193. /*! \fn bool GfaModbusRTUSlvSetID(HMBRTUSLV hMbSlv, uint8_t newID)
  194. \brief Assigns the slave a new slave id.
  195. \param hMbSlv Handle to the modbus slave that has been acquired by a call to \ref GfaModbusRTUSlvCreate.
  196. \param newID The new Slave ID.
  197. \return \b true if successful, or \b false if \b hMbSlv is \b NULL or \b newID is not a valid slave id.
  198. \note A new slave id can be assigned at any time, however, the changes take effect only when the slave is in idle state (between two requests)!
  199. */
  200. bool GfaModbusRTUSlvSetID(HMBRTUSLV hMbSlv, uint8_t newID);
  201. /////////////////////////////////////////////////////////////////////////////
  202. #ifdef __cplusplus
  203. }
  204. #endif // __cplusplus
  205. #endif // !defined(AGD_MBRTUSLV_H__84F27BE0_71CA_4910_8631_B0EEA21DC84A__INCLUDED_)