esp_modbus_master.h 12 KB

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