esp_modbus_master.h 12 KB

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