mbc_master.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_MASTER_H
  16. #define _MB_CONTROLLER_MASTER_H
  17. #include "freertos/FreeRTOS.h" // for task creation and queue access
  18. #include "freertos/task.h" // for task api access
  19. #include "freertos/event_groups.h" // for event groups
  20. #include "driver/uart.h" // for UART types
  21. #include "errno.h" // for errno
  22. #include "esp_log.h" // for log write
  23. #include "string.h" // for strerror()
  24. #include "esp_modbus_common.h" // for common types
  25. #include "esp_modbus_master.h" // for public master types
  26. #include "esp_modbus_callbacks.h"
  27. /* ----------------------- Defines ------------------------------------------*/
  28. #define MB_MASTER_TAG "MB_CONTROLLER_MASTER"
  29. #define MB_MASTER_CHECK(a, ret_val, str, ...) \
  30. if (!(a)) { \
  31. ESP_LOGE(MB_MASTER_TAG, "%s(%u): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  32. return (ret_val); \
  33. }
  34. #define MB_MASTER_ASSERT(con) do { \
  35. if (!(con)) { ESP_LOGE(MB_MASTER_TAG, "assert errno:%d, errno_str: !(%s)", errno, strerror(errno)); assert(0 && #con); } \
  36. } while (0)
  37. /**
  38. * @brief Request mode for parameter to use in data dictionary
  39. */
  40. typedef enum {
  41. MB_PARAM_READ, /*!< Read parameter values. */
  42. MB_PARAM_WRITE /*!< Write parameter values. */
  43. } mb_param_mode_t;
  44. /**
  45. * @brief Device communication parameters for master
  46. */
  47. typedef struct {
  48. mb_mode_type_t mode; /*!< Modbus communication mode */
  49. uint8_t dummy; /*!< Dummy field */
  50. uart_port_t port; /*!< Modbus communication port (UART) number */
  51. uint32_t baudrate; /*!< Modbus baudrate */
  52. uart_parity_t parity; /*!< Modbus UART parity settings */
  53. } mb_master_comm_info_t;
  54. /**
  55. * @brief Modbus controller handler structure
  56. */
  57. typedef struct {
  58. mb_port_type_t port_type; /*!< Modbus port type */
  59. mb_communication_info_t mbm_comm; /*!< Modbus communication info */
  60. uint8_t* mbm_reg_buffer_ptr; /*!< Modbus data buffer pointer */
  61. uint16_t mbm_reg_buffer_size; /*!< Modbus data buffer size */
  62. TaskHandle_t mbm_task_handle; /*!< Modbus task handle */
  63. EventGroupHandle_t mbm_event_group; /*!< Modbus controller event group */
  64. const mb_parameter_descriptor_t* mbm_param_descriptor_table; /*!< Modbus controller parameter description table */
  65. size_t mbm_param_descriptor_size; /*!< Modbus controller parameter description table size*/
  66. } mb_master_options_t;
  67. typedef esp_err_t (*iface_get_cid_info)(uint16_t, const mb_parameter_descriptor_t**); /*!< Interface get_cid_info method */
  68. typedef esp_err_t (*iface_get_parameter)(uint16_t, char*, uint8_t*, uint8_t*); /*!< Interface get_parameter method */
  69. typedef esp_err_t (*iface_send_request)(mb_param_request_t*, void*); /*!< Interface send_request method */
  70. typedef esp_err_t (*iface_set_descriptor)(const mb_parameter_descriptor_t*, const uint16_t); /*!< Interface set_descriptor method */
  71. typedef esp_err_t (*iface_set_parameter)(uint16_t, char*, uint8_t*, uint8_t*); /*!< Interface set_parameter method */
  72. /**
  73. * @brief Modbus controller interface structure
  74. */
  75. typedef struct {
  76. // Master object interface options
  77. mb_master_options_t opts;
  78. // Public interface methods
  79. iface_init init; /*!< Interface method init */
  80. iface_destroy destroy; /*!< Interface method destroy */
  81. iface_setup setup; /*!< Interface method setup */
  82. iface_start start; /*!< Interface method start */
  83. iface_get_cid_info get_cid_info; /*!< Interface get_cid_info method */
  84. iface_get_parameter get_parameter; /*!< Interface get_parameter method */
  85. iface_send_request send_request; /*!< Interface send_request method */
  86. iface_set_descriptor set_descriptor; /*!< Interface set_descriptor method */
  87. iface_set_parameter set_parameter; /*!< Interface set_parameter method */
  88. // Modbus register calback function pointers
  89. reg_discrete_cb master_reg_cb_discrete; /*!< Stack callback discrete rw method */
  90. reg_input_cb master_reg_cb_input; /*!< Stack callback input rw method */
  91. reg_holding_cb master_reg_cb_holding; /*!< Stack callback holding rw method */
  92. reg_coils_cb master_reg_cb_coils; /*!< Stack callback coils rw method */
  93. } mb_master_interface_t;
  94. #endif //_MB_CONTROLLER_MASTER_H