mbcontroller.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2015-2016 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. // mbcontroller.h
  15. // Implementation of the MbController
  16. #ifndef _MODBUS_CONTROLLER
  17. #define _MODBUS_CONTROLLER
  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 "sdkconfig.h" // for KConfig options
  22. #include "driver/uart.h" // for uart port number defines
  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_STACK_SIZE (CONFIG_MB_CONTROLLER_STACK_SIZE) // Stack size for Modbus controller
  27. #define MB_CONTROLLER_PRIORITY (CONFIG_MB_SERIAL_TASK_PRIO - 1) // priority of MB controller task
  28. #define MB_CONTROLLER_NOTIFY_QUEUE_SIZE (CONFIG_MB_CONTROLLER_NOTIFY_QUEUE_SIZE) // Number of messages in parameter notification queue
  29. #define MB_CONTROLLER_NOTIFY_TIMEOUT (pdMS_TO_TICKS(CONFIG_MB_CONTROLLER_NOTIFY_TIMEOUT)) // notification timeout
  30. // Default port defines
  31. #define MB_DEVICE_ADDRESS (1) // Default slave device address in Modbus
  32. #define MB_DEVICE_SPEED (115200) // Default Modbus speed for now hard defined
  33. #define MB_UART_PORT (UART_NUM_2) // Default UART port number
  34. #define MB_PAR_INFO_TOUT (10) // Timeout for get parameter info
  35. #define MB_PARITY_NONE (UART_PARITY_DISABLE)
  36. /**
  37. * @brief Event group for parameters notification
  38. */
  39. typedef enum
  40. {
  41. MB_EVENT_NO_EVENTS = 0x00,
  42. MB_EVENT_HOLDING_REG_WR = BIT0, /*!< Modbus Event Write Holding registers. */
  43. MB_EVENT_HOLDING_REG_RD = BIT1, /*!< Modbus Event Read Holding registers. */
  44. MB_EVENT_INPUT_REG_RD = BIT3, /*!< Modbus Event Read Input registers. */
  45. MB_EVENT_COILS_WR = BIT4, /*!< Modbus Event Write Coils. */
  46. MB_EVENT_COILS_RD = BIT5, /*!< Modbus Event Read Coils. */
  47. MB_EVENT_DISCRETE_RD = BIT6, /*!< Modbus Event Read Discrete bits. */
  48. MB_EVENT_STACK_STARTED = BIT7 /*!< Modbus Event Stack started */
  49. } mb_event_group_t;
  50. /**
  51. * @brief Type of Modbus parameter
  52. */
  53. typedef enum
  54. {
  55. MB_PARAM_HOLDING, /*!< Modbus Holding register. */
  56. MB_PARAM_INPUT, /*!< Modbus Input register. */
  57. MB_PARAM_COIL, /*!< Modbus Coils. */
  58. MB_PARAM_DISCRETE, /*!< Modbus Discrete bits. */
  59. MB_PARAM_COUNT,
  60. MB_PARAM_UNKNOWN = 0xFF
  61. } mb_param_type_t;
  62. /*!
  63. * \brief Modbus serial transmission modes (RTU/ASCII).
  64. */
  65. typedef enum
  66. {
  67. MB_MODE_RTU, /*!< RTU transmission mode. */
  68. MB_MODE_ASCII, /*!< ASCII transmission mode. */
  69. MB_MODE_TCP /*!< TCP mode. */
  70. } mb_mode_type_t;
  71. /**
  72. * @brief Parameter access event information type
  73. */
  74. typedef struct {
  75. uint32_t time_stamp; /*!< Timestamp of Modbus Event (uS)*/
  76. uint16_t mb_offset; /*!< Modbus register offset */
  77. mb_event_group_t type; /*!< Modbus event type */
  78. uint8_t* address; /*!< Modbus data storage address */
  79. size_t size; /*!< Modbus event register size (number of registers)*/
  80. } mb_param_info_t;
  81. /**
  82. * @brief Parameter storage area descriptor
  83. */
  84. typedef struct {
  85. uint16_t start_offset; /*!< Modbus start address for area descriptor */
  86. mb_param_type_t type; /*!< Type of storage area descriptor */
  87. void* address; /*!< Instance address for storage area descriptor */
  88. size_t size; /*!< Instance size for area descriptor (bytes) */
  89. } mb_register_area_descriptor_t;
  90. /**
  91. * @brief Device communication parameters
  92. */
  93. typedef struct {
  94. mb_mode_type_t mode; /*!< Modbus communication mode */
  95. uint8_t slave_addr; /*!< Modbus Slave Address */
  96. uart_port_t port; /*!< Modbus communication port (UART) number */
  97. uint32_t baudrate; /*!< Modbus baudrate */
  98. uart_parity_t parity; /*!< Modbus UART parity settings */
  99. } mb_communication_info_t;
  100. /**
  101. * @brief Initialize modbus controller and stack
  102. *
  103. * @return
  104. * - ESP_OK Success
  105. * - ESP_FAIL Parameter error
  106. */
  107. esp_err_t mbcontroller_init(void);
  108. /**
  109. * @brief Destroy Modbus controller and stack
  110. *
  111. * @return
  112. * - ESP_OK Success
  113. * - ESP_FAIL Parameter error
  114. */
  115. esp_err_t mbcontroller_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 mbcontroller_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 mbcontroller_setup(mb_communication_info_t comm_info);
  134. /**
  135. * @brief Wait for specific event on parameter change.
  136. *
  137. * @param group Group event bit mask to wait for change
  138. *
  139. * @return
  140. * - mb_event_group_t event bits triggered
  141. */
  142. mb_event_group_t mbcontroller_check_event(mb_event_group_t group);
  143. /**
  144. * @brief Get parameter information
  145. *
  146. * @param[out] reg_info parameter info structure
  147. * @param timeout Timeout in milliseconds to read information from
  148. * parameter queue
  149. * @return
  150. * - ESP_OK Success
  151. * - ESP_ERR_TIMEOUT Can not get data from parameter queue
  152. * or queue overflow
  153. */
  154. esp_err_t mbcontroller_get_param_info(mb_param_info_t* reg_info, uint32_t timeout);
  155. /**
  156. * @brief Set Modbus area descriptor
  157. *
  158. * @param descr_data Modbus registers area descriptor structure
  159. *
  160. * @return
  161. * - ESP_OK: The appropriate descriptor is set
  162. * - ESP_ERR_INVALID_ARG: The argument is incorrect
  163. */
  164. esp_err_t mbcontroller_set_descriptor(mb_register_area_descriptor_t descr_data);
  165. #endif