esp_modbus_master.h 11 KB

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