mbc_slave.h 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 _MB_CONTROLLER_SLAVE_H
  16. #define _MB_CONTROLLER_SLAVE_H
  17. #include "driver/uart.h" // for uart defines
  18. #include "errno.h" // for errno
  19. #include "esp_log.h" // for log write
  20. #include "string.h" // for strerror()
  21. #include "esp_modbus_slave.h" // for public type defines
  22. #include "esp_modbus_callbacks.h" // for callback functions
  23. /* ----------------------- Defines ------------------------------------------*/
  24. #define MB_INST_MIN_SIZE (2) // The minimal size of Modbus registers area in bytes
  25. #define MB_INST_MAX_SIZE (65535 * 2) // The maximum size of Modbus area in bytes
  26. #define MB_CONTROLLER_NOTIFY_QUEUE_SIZE (CONFIG_FMB_CONTROLLER_NOTIFY_QUEUE_SIZE) // Number of messages in parameter notification queue
  27. #define MB_CONTROLLER_NOTIFY_TIMEOUT (pdMS_TO_TICKS(CONFIG_FMB_CONTROLLER_NOTIFY_TIMEOUT)) // notification timeout
  28. #define MB_SLAVE_TAG "MB_CONTROLLER_SLAVE"
  29. #define MB_SLAVE_CHECK(a, ret_val, str, ...) \
  30. if (!(a)) { \
  31. ESP_LOGE(MB_SLAVE_TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  32. return (ret_val); \
  33. }
  34. #define MB_SLAVE_ASSERT(con) do { \
  35. if (!(con)) { ESP_LOGE(MB_SLAVE_TAG, "assert errno:%d, errno_str: !(%s)", errno, strerror(errno)); assert(0 && #con); } \
  36. } while (0)
  37. /**
  38. * @brief Device communication parameters for master
  39. */
  40. typedef struct {
  41. mb_mode_type_t mode; /*!< Modbus communication mode */
  42. uint8_t slave_addr; /*!< Slave address field */
  43. uart_port_t port; /*!< Modbus communication port (UART) number */
  44. uint32_t baudrate; /*!< Modbus baudrate */
  45. uart_parity_t parity; /*!< Modbus UART parity settings */
  46. } mb_slave_comm_info_t;
  47. /**
  48. * @brief Modbus controller handler structure
  49. */
  50. typedef struct {
  51. mb_port_type_t port_type; /*!< port type */
  52. mb_communication_info_t mbs_comm; /*!< communication info */
  53. TaskHandle_t mbs_task_handle; /*!< task handle */
  54. EventGroupHandle_t mbs_event_group; /*!< controller event group */
  55. QueueHandle_t mbs_notification_queue_handle; /*!< controller notification queue */
  56. mb_register_area_descriptor_t mbs_area_descriptors[MB_PARAM_COUNT]; /*!< register area descriptors */
  57. } mb_slave_options_t;
  58. typedef mb_event_group_t (*iface_check_event)(mb_event_group_t); /*!< Interface method check_event */
  59. typedef esp_err_t (*iface_get_param_info)(mb_param_info_t*, uint32_t); /*!< Interface method get_param_info */
  60. typedef esp_err_t (*iface_set_descriptor)(mb_register_area_descriptor_t); /*!< Interface method set_descriptor */
  61. /**
  62. * @brief Request mode for parameter to use in data dictionary
  63. */
  64. typedef struct
  65. {
  66. mb_slave_options_t opts; /*!< Modbus slave options */
  67. // Functional pointers to internal static functions of the implementation (public interface methods)
  68. iface_init init; /*!< Interface method init */
  69. iface_destroy destroy; /*!< Interface method destroy */
  70. iface_setup setup; /*!< Interface method setup */
  71. iface_start start; /*!< Interface method start */
  72. iface_check_event check_event; /*!< Interface method check_event */
  73. iface_get_param_info get_param_info; /*!< Interface method get_param_info */
  74. iface_set_descriptor set_descriptor; /*!< Interface method set_descriptor */
  75. // Modbus register calback function pointers
  76. reg_discrete_cb slave_reg_cb_discrete; /*!< Stack callback discrete rw method */
  77. reg_input_cb slave_reg_cb_input; /*!< Stack callback input rw method */
  78. reg_holding_cb slave_reg_cb_holding; /*!< Stack callback holding rw method */
  79. reg_coils_cb slave_reg_cb_coils; /*!< Stack callback coils rw method */
  80. } mb_slave_interface_t;
  81. #endif