esp_modbus_master.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // Master interface public functions
  101. /**
  102. * @brief Initialize Modbus controller and stack
  103. *
  104. * @param[out] handler handler(pointer) to master data structure
  105. * @param[in] port_type the type of port
  106. * @return
  107. * - ESP_OK Success
  108. * - ESP_ERR_NO_MEM Parameter error
  109. */
  110. esp_err_t mbc_master_init(mb_port_type_t port_type, void** handler);
  111. /**
  112. * @brief Destroy Modbus controller and stack
  113. *
  114. * @return
  115. * - ESP_OK Success
  116. * - ESP_ERR_INVALID_STATE Parameter error
  117. */
  118. esp_err_t mbc_master_destroy(void);
  119. /**
  120. * @brief Start Modbus communication stack
  121. *
  122. * @return
  123. * - ESP_OK Success
  124. * - ESP_ERR_INVALID_ARG Modbus stack start error
  125. */
  126. esp_err_t mbc_master_start(void);
  127. /**
  128. * @brief Set Modbus communication parameters for the controller
  129. *
  130. * @param comm_info Communication parameters structure.
  131. *
  132. * @return
  133. * - ESP_OK Success
  134. * - ESP_ERR_INVALID_ARG Incorrect parameter data
  135. */
  136. esp_err_t mbc_master_setup(void* comm_info);
  137. /***************************** Specific interface functions ********************************************
  138. * Interface functions below provide basic methods to read/write access to slave devices in Modbus
  139. * segment as well as API to read specific supported characteristics linked to Modbus parameters
  140. * of devices in Modbus network.
  141. *******************************************************************************************************/
  142. /**
  143. * @brief Assign parameter description table for Modbus controller interface.
  144. *
  145. * @param[in] descriptor pointer to parameter description table
  146. * @param num_elements number of elements in the table
  147. *
  148. * @return
  149. * - esp_err_t ESP_OK - set descriptor successfully
  150. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument in function call
  151. */
  152. esp_err_t mbc_master_set_descriptor(const mb_parameter_descriptor_t* descriptor, const uint16_t num_elements);
  153. /**
  154. * @brief Send data request as defined in parameter request, waits response
  155. * from slave and returns status of command execution. This function provides standard way
  156. * for read/write access to Modbus devices in the network.
  157. *
  158. * @param[in] request pointer to request structure of type mb_param_request_t
  159. * @param[in] data_ptr pointer to data buffer to send or received data (dependent of command field in request)
  160. *
  161. * @return
  162. * - esp_err_t ESP_OK - request was successful
  163. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
  164. * - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave
  165. * - esp_err_t ESP_ERR_TIMEOUT - operation timeout or no response from slave
  166. * - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
  167. * - esp_err_t ESP_FAIL - slave returned an exception or other failure
  168. */
  169. esp_err_t mbc_master_send_request(mb_param_request_t* request, void* data_ptr);
  170. /**
  171. * @brief Get information about supported characteristic defined as cid. Uses parameter description table to get
  172. * this information. The function will check if characteristic defined as a cid parameter is supported
  173. * and returns its description in param_info. Returns ESP_ERR_NOT_FOUND if characteristic is not supported.
  174. *
  175. * @param[in] cid characteristic id
  176. * @param param_info pointer to pointer of characteristic data.
  177. *
  178. * @return
  179. * - esp_err_t ESP_OK - request was successful and buffer contains the supported characteristic name
  180. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
  181. * - esp_err_t ESP_ERR_NOT_FOUND - the characteristic (cid) not found
  182. * - esp_err_t ESP_FAIL - unknown error during lookup table processing
  183. */
  184. esp_err_t mbc_master_get_cid_info(uint16_t cid, const mb_parameter_descriptor_t** param_info);
  185. /**
  186. * @brief Read parameter from modbus slave device whose name is defined by name and has cid.
  187. * The additional data for request is taken from parameter description (lookup) table.
  188. *
  189. * @param[in] cid id of the characteristic for parameter
  190. * @param[in] name pointer into string name (key) of parameter (null terminated)
  191. * @param[out] value pointer to data buffer of parameter
  192. * @param[out] type parameter type associated with the name returned from parameter description table.
  193. *
  194. * @return
  195. * - esp_err_t ESP_OK - request was successful and value buffer contains
  196. * representation of actual parameter data from slave
  197. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
  198. * - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave
  199. * - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
  200. * - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
  201. * - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
  202. * - esp_err_t ESP_ERR_NOT_FOUND - the parameter is not found in the parameter description table
  203. * - esp_err_t ESP_FAIL - slave returned an exception or other failure
  204. */
  205. esp_err_t mbc_master_get_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t *type);
  206. /**
  207. * @brief Set characteristic's value defined as a name and cid parameter.
  208. * The additional data for cid parameter request is taken from master parameter lookup table.
  209. *
  210. * @param[in] cid id of the characteristic for parameter
  211. * @param[in] name pointer into string name (key) of parameter (null terminated)
  212. * @param[out] value pointer to data buffer of parameter (actual representation of json value field in binary form)
  213. * @param[out] type pointer to parameter type associated with the name returned from parameter lookup table.
  214. *
  215. * @return
  216. * - esp_err_t ESP_OK - request was successful and value was saved in the slave device registers
  217. * - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
  218. * - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave during processing of parameter
  219. * - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
  220. * - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
  221. * - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
  222. * - esp_err_t ESP_FAIL - slave returned an exception or other failure
  223. */
  224. esp_err_t mbc_master_set_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t *type);
  225. #ifdef __cplusplus
  226. }
  227. #endif
  228. #endif // _ESP_MB_MASTER_INTERFACE_H