esp_modbus_master.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ESP_MB_MASTER_INTERFACE_H
  7. #define _ESP_MB_MASTER_INTERFACE_H
  8. #include <stdint.h> // for standard int types definition
  9. #include <stddef.h> // for NULL and std defines
  10. #include "esp_modbus_common.h" // for common types
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /*!
  15. * \brief Modbus descriptor table parameter type defines.
  16. */
  17. typedef enum {
  18. PARAM_TYPE_U8 = 0x00, /*!< Unsigned 8 */
  19. PARAM_TYPE_U16 = 0x01, /*!< Unsigned 16 */
  20. PARAM_TYPE_U32 = 0x02, /*!< Unsigned 32 */
  21. PARAM_TYPE_FLOAT = 0x03, /*!< Float type */
  22. PARAM_TYPE_ASCII = 0x04 /*!< ASCII type */
  23. } mb_descr_type_t;
  24. /*!
  25. * \brief Modbus descriptor table parameter size in bytes.
  26. */
  27. typedef enum {
  28. PARAM_SIZE_U8 = 0x01, /*!< Unsigned 8 */
  29. PARAM_SIZE_U16 = 0x02, /*!< Unsigned 16 */
  30. PARAM_SIZE_U32 = 0x04, /*!< Unsigned 32 */
  31. PARAM_SIZE_FLOAT = 0x04, /*!< Float size */
  32. PARAM_SIZE_ASCII = 0x08, /*!< ASCII size */
  33. PARAM_SIZE_ASCII24 = 0x18, /*!< ASCII24 size */
  34. PARAM_MAX_SIZE
  35. } mb_descr_size_t;
  36. /*!
  37. * \brief Modbus parameter options for description table
  38. */
  39. typedef union {
  40. struct {
  41. int opt1; /*!< Parameter option1 */
  42. int opt2; /*!< Parameter option2 */
  43. int opt3; /*!< Parameter option3 */
  44. };
  45. struct {
  46. int min; /*!< Parameter minimum value */
  47. int max; /*!< Parameter maximum value */
  48. int step; /*!< Step of parameter change tracking */
  49. };
  50. } mb_parameter_opt_t;
  51. /**
  52. * @brief Permissions for the characteristics
  53. */
  54. typedef enum {
  55. PAR_PERMS_READ = 1 << BIT0, /**< the characteristic of the device are readable */
  56. PAR_PERMS_WRITE = 1 << BIT1, /**< the characteristic of the device are writable*/
  57. PAR_PERMS_TRIGGER = 1 << BIT2, /**< the characteristic of the device are triggerable */
  58. PAR_PERMS_READ_WRITE = PAR_PERMS_READ | PAR_PERMS_WRITE, /**< the characteristic of the device are readable & writable */
  59. PAR_PERMS_READ_TRIGGER = PAR_PERMS_READ | PAR_PERMS_TRIGGER, /**< the characteristic of the device are readable & triggerable */
  60. PAR_PERMS_WRITE_TRIGGER = PAR_PERMS_WRITE | PAR_PERMS_TRIGGER, /**< the characteristic of the device are writable & triggerable */
  61. PAR_PERMS_READ_WRITE_TRIGGER = PAR_PERMS_READ_WRITE | PAR_PERMS_TRIGGER, /**< the characteristic of the device are readable & writable & triggerable */
  62. } mb_param_perms_t;
  63. /**
  64. * @brief Characteristics descriptor type is used to describe characteristic and
  65. * link it with Modbus parameters that reflect its data.
  66. */
  67. typedef struct {
  68. uint16_t cid; /*!< Characteristic cid */
  69. const char* param_key; /*!< The key (name) of the parameter */
  70. const char* param_units; /*!< The physical units of the parameter */
  71. uint8_t mb_slave_addr; /*!< Slave address of device in the Modbus segment */
  72. mb_param_type_t mb_param_type; /*!< Type of modbus parameter */
  73. uint16_t mb_reg_start; /*!< This is the Modbus register address. This is the 0 based value. */
  74. uint16_t mb_size; /*!< Size of mb parameter in registers */
  75. uint16_t param_offset; /*!< Parameter name (OFFSET in the parameter structure) */
  76. mb_descr_type_t param_type; /*!< Float, U8, U16, U32, ASCII, etc. */
  77. mb_descr_size_t param_size; /*!< Number of bytes in the parameter. */
  78. mb_parameter_opt_t param_opts; /*!< Parameter options used to check limits and etc. */
  79. mb_param_perms_t access; /*!< Access permissions based on mode */
  80. } mb_parameter_descriptor_t;
  81. /**
  82. * @brief Modbus register request type structure
  83. */
  84. typedef struct {
  85. uint8_t slave_addr; /*!< Modbus slave address */
  86. uint8_t command; /*!< Modbus command to send */
  87. uint16_t reg_start; /*!< Modbus start register */
  88. uint16_t reg_size; /*!< Modbus number of registers */
  89. } mb_param_request_t;
  90. /**
  91. * @brief Initialize Modbus controller and stack for TCP port
  92. *
  93. * @param[out] handler handler(pointer) to master data structure
  94. * @return
  95. * - ESP_OK Success
  96. * - ESP_ERR_NO_MEM Parameter error
  97. * - ESP_ERR_NOT_SUPPORTED Port type not supported
  98. * - ESP_ERR_INVALID_STATE Initialization failure
  99. */
  100. esp_err_t mbc_master_init_tcp(void** handler);
  101. /**
  102. * @brief Initialize Modbus Master controller and stack for Serial port
  103. *
  104. * @param[out] handler handler(pointer) to master data structure
  105. * @param[in] port_type type of stack
  106. * @return
  107. * - ESP_OK Success
  108. * - ESP_ERR_NO_MEM Parameter error
  109. * - ESP_ERR_NOT_SUPPORTED Port type not supported
  110. * - ESP_ERR_INVALID_STATE Initialization failure
  111. */
  112. esp_err_t mbc_master_init(mb_port_type_t port_type, void** handler);
  113. /**
  114. * @brief Initialize Modbus Master controller interface handle
  115. *
  116. * @param[in] handler - pointer to master data structure
  117. */
  118. void mbc_master_init_iface(void* handler);
  119. /**
  120. * @brief Destroy Modbus controller and stack
  121. *
  122. * @return
  123. * - ESP_OK Success
  124. * - ESP_ERR_INVALID_STATE Parameter error
  125. */
  126. esp_err_t mbc_master_destroy(void);
  127. /**
  128. * @brief Start Modbus communication stack
  129. *
  130. * @return
  131. * - ESP_OK Success
  132. * - ESP_ERR_INVALID_ARG Modbus stack start error
  133. */
  134. esp_err_t mbc_master_start(void);
  135. /**
  136. * @brief Set Modbus communication parameters for the controller
  137. *
  138. * @param comm_info Communication parameters structure.
  139. *
  140. * @return
  141. * - ESP_OK Success
  142. * - ESP_ERR_INVALID_ARG Incorrect parameter data
  143. */
  144. esp_err_t mbc_master_setup(void* comm_info);
  145. /***************************** Specific interface functions ********************************************
  146. * Interface functions below provide basic methods to read/write access to slave devices in Modbus
  147. * segment as well as API to read specific supported characteristics linked to Modbus parameters
  148. * of devices in Modbus network.
  149. *******************************************************************************************************/
  150. /**
  151. * @brief Assign parameter description table for Modbus controller interface.
  152. *
  153. * @param[in] descriptor pointer to parameter description table
  154. * @param num_elements number of elements in the table
  155. *
  156. * @return
  157. * - esp_err_t ESP_OK - set descriptor successfully
  158. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument in function call
  159. */
  160. esp_err_t mbc_master_set_descriptor(const mb_parameter_descriptor_t* descriptor, const uint16_t num_elements);
  161. /**
  162. * @brief Send data request as defined in parameter request, waits response
  163. * from slave and returns status of command execution. This function provides standard way
  164. * for read/write access to Modbus devices in the network.
  165. *
  166. * @param[in] request pointer to request structure of type mb_param_request_t
  167. * @param[in] data_ptr pointer to data buffer to send or received data (dependent of command field in request)
  168. *
  169. * @return
  170. * - esp_err_t ESP_OK - request was successful
  171. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
  172. * - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave
  173. * - esp_err_t ESP_ERR_TIMEOUT - operation timeout or no response from slave
  174. * - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
  175. * - esp_err_t ESP_FAIL - slave returned an exception or other failure
  176. */
  177. esp_err_t mbc_master_send_request(mb_param_request_t* request, void* data_ptr);
  178. /**
  179. * @brief Get information about supported characteristic defined as cid. Uses parameter description table to get
  180. * this information. The function will check if characteristic defined as a cid parameter is supported
  181. * and returns its description in param_info. Returns ESP_ERR_NOT_FOUND if characteristic is not supported.
  182. *
  183. * @param[in] cid characteristic id
  184. * @param param_info pointer to pointer of characteristic data.
  185. *
  186. * @return
  187. * - esp_err_t ESP_OK - request was successful and buffer contains the supported characteristic name
  188. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
  189. * - esp_err_t ESP_ERR_NOT_FOUND - the characteristic (cid) not found
  190. * - esp_err_t ESP_FAIL - unknown error during lookup table processing
  191. */
  192. esp_err_t mbc_master_get_cid_info(uint16_t cid, const mb_parameter_descriptor_t** param_info);
  193. /**
  194. * @brief Read parameter from modbus slave device whose name is defined by name and has cid.
  195. * The additional data for request is taken from parameter description (lookup) table.
  196. *
  197. * @param[in] cid id of the characteristic for parameter
  198. * @param[in] name pointer into string name (key) of parameter (null terminated)
  199. * @param[out] value pointer to data buffer of parameter
  200. * @param[out] type parameter type associated with the name returned from parameter description table.
  201. *
  202. * @return
  203. * - esp_err_t ESP_OK - request was successful and value buffer contains
  204. * representation of actual parameter data from slave
  205. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
  206. * - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave
  207. * - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
  208. * - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
  209. * - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
  210. * - esp_err_t ESP_ERR_NOT_FOUND - the parameter is not found in the parameter description table
  211. * - esp_err_t ESP_FAIL - slave returned an exception or other failure
  212. */
  213. esp_err_t mbc_master_get_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t *type);
  214. /**
  215. * @brief Set characteristic's value defined as a name and cid parameter.
  216. * The additional data for cid parameter request is taken from master parameter lookup table.
  217. *
  218. * @param[in] cid id of the characteristic for parameter
  219. * @param[in] name pointer into string name (key) of parameter (null terminated)
  220. * @param[out] value pointer to data buffer of parameter (actual representation of json value field in binary form)
  221. * @param[out] type pointer to parameter type associated with the name returned from parameter lookup table.
  222. *
  223. * @return
  224. * - esp_err_t ESP_OK - request was successful and value was saved in the slave device registers
  225. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function or parameter descriptor
  226. * - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave during processing of parameter
  227. * - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
  228. * - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
  229. * - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
  230. * - esp_err_t ESP_FAIL - slave returned an exception or other failure
  231. */
  232. esp_err_t mbc_master_set_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t *type);
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif // _ESP_MB_MASTER_INTERFACE_H