esp_modbus_slave.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_SLAVE_INTERFACE_H
  16. #define _ESP_MB_SLAVE_INTERFACE_H
  17. // Public interface header for slave
  18. #include <stdint.h> // for standard int types definition
  19. #include <stddef.h> // for NULL and std defines
  20. #include "soc/soc.h" // for BITN definitions
  21. #include "freertos/FreeRTOS.h" // for task creation and queues access
  22. #include "freertos/event_groups.h" // for event groups
  23. #include "esp_modbus_common.h" // for common types
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * @brief Parameter access event information type
  29. */
  30. typedef struct {
  31. uint32_t time_stamp; /*!< Timestamp of Modbus Event (uS)*/
  32. uint16_t mb_offset; /*!< Modbus register offset */
  33. mb_event_group_t type; /*!< Modbus event type */
  34. uint8_t* address; /*!< Modbus data storage address */
  35. size_t size; /*!< Modbus event register size (number of registers)*/
  36. } mb_param_info_t;
  37. /**
  38. * @brief Parameter storage area descriptor
  39. */
  40. typedef struct {
  41. uint16_t start_offset; /*!< Modbus start address for area descriptor */
  42. mb_param_type_t type; /*!< Type of storage area descriptor */
  43. void* address; /*!< Instance address for storage area descriptor */
  44. size_t size; /*!< Instance size for area descriptor (bytes) */
  45. } mb_register_area_descriptor_t;
  46. /**
  47. * @brief Initialize Modbus Slave controller and stack for TCP port
  48. *
  49. * @param[out] handler handler(pointer) to master data structure
  50. * @return
  51. * - ESP_OK Success
  52. * - ESP_ERR_NO_MEM Parameter error
  53. * - ESP_ERR_NOT_SUPPORTED Port type not supported
  54. * - ESP_ERR_INVALID_STATE Initialization failure
  55. */
  56. esp_err_t mbc_slave_init_tcp(void** handler);
  57. /**
  58. * @brief Initialize Modbus Slave controller and stack for Serial port
  59. *
  60. * @param[out] handler handler(pointer) to master data structure
  61. * @param[in] port_type the type of port
  62. * @return
  63. * - ESP_OK Success
  64. * - ESP_ERR_NO_MEM Parameter error
  65. * - ESP_ERR_NOT_SUPPORTED Port type not supported
  66. * - ESP_ERR_INVALID_STATE Initialization failure
  67. */
  68. esp_err_t mbc_slave_init(mb_port_type_t port_type, void** handler);
  69. /**
  70. * @brief Initialize Modbus Slave controller interface handle
  71. *
  72. * @param[in] handler - pointer to slave interface data structure
  73. */
  74. void mbc_slave_init_iface(void* handler);
  75. /**
  76. * @brief Destroy Modbus controller and stack
  77. *
  78. * @return
  79. * - ESP_OK Success
  80. * - ESP_ERR_INVALID_STATE Parameter error
  81. */
  82. esp_err_t mbc_slave_destroy(void);
  83. /**
  84. * @brief Start Modbus communication stack
  85. *
  86. * @return
  87. * - ESP_OK Success
  88. * - ESP_ERR_INVALID_ARG Modbus stack start error
  89. */
  90. esp_err_t mbc_slave_start(void);
  91. /**
  92. * @brief Set Modbus communication parameters for the controller
  93. *
  94. * @param comm_info Communication parameters structure.
  95. *
  96. * @return
  97. * - ESP_OK Success
  98. * - ESP_ERR_INVALID_ARG Incorrect parameter data
  99. */
  100. esp_err_t mbc_slave_setup(void* comm_info);
  101. /**
  102. * @brief Wait for specific event on parameter change.
  103. *
  104. * @param group Group event bit mask to wait for change
  105. *
  106. * @return
  107. * - mb_event_group_t event bits triggered
  108. */
  109. mb_event_group_t mbc_slave_check_event(mb_event_group_t group);
  110. /**
  111. * @brief Get parameter information
  112. *
  113. * @param[out] reg_info parameter info structure
  114. * @param timeout Timeout in milliseconds to read information from
  115. * parameter queue
  116. * @return
  117. * - ESP_OK Success
  118. * - ESP_ERR_TIMEOUT Can not get data from parameter queue
  119. * or queue overflow
  120. */
  121. esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout);
  122. /**
  123. * @brief Set Modbus area descriptor
  124. *
  125. * @param descr_data Modbus registers area descriptor structure
  126. *
  127. * @return
  128. * - ESP_OK: The appropriate descriptor is set
  129. * - ESP_ERR_INVALID_ARG: The argument is incorrect
  130. */
  131. esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data);
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif