esp_modbus_common.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_IFACE_COMMON_H
  16. #define _MB_IFACE_COMMON_H
  17. #include "driver/uart.h" // for UART types
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define MB_CONTROLLER_STACK_SIZE (CONFIG_FMB_CONTROLLER_STACK_SIZE) // Stack size for Modbus controller
  22. #define MB_CONTROLLER_PRIORITY (CONFIG_FMB_PORT_TASK_PRIO - 1) // priority of MB controller task
  23. // Default port defines
  24. #define MB_DEVICE_ADDRESS (1) // Default slave device address in Modbus
  25. #define MB_DEVICE_SPEED (115200) // Default Modbus speed for now hard defined
  26. #define MB_UART_PORT (UART_NUM_MAX - 1) // Default UART port number
  27. #define MB_PAR_INFO_TOUT (10) // Timeout for get parameter info
  28. #define MB_PARITY_NONE (UART_PARITY_DISABLE)
  29. // The Macros below handle the endianness while transfer N byte data into buffer
  30. #define _XFER_4_RD(dst, src) { \
  31. *(uint8_t *)(dst)++ = *(uint8_t*)(src + 1); \
  32. *(uint8_t *)(dst)++ = *(uint8_t*)(src + 0); \
  33. *(uint8_t *)(dst)++ = *(uint8_t*)(src + 3); \
  34. *(uint8_t *)(dst)++ = *(uint8_t*)(src + 2); \
  35. (src) += 4; \
  36. }
  37. #define _XFER_2_RD(dst, src) { \
  38. *(uint8_t *)(dst)++ = *(uint8_t *)(src + 1); \
  39. *(uint8_t *)(dst)++ = *(uint8_t *)(src + 0); \
  40. (src) += 2; \
  41. }
  42. #define _XFER_4_WR(dst, src) { \
  43. *(uint8_t *)(dst + 1) = *(uint8_t *)(src)++; \
  44. *(uint8_t *)(dst + 0) = *(uint8_t *)(src)++; \
  45. *(uint8_t *)(dst + 3) = *(uint8_t *)(src)++; \
  46. *(uint8_t *)(dst + 2) = *(uint8_t *)(src)++ ; \
  47. }
  48. #define _XFER_2_WR(dst, src) { \
  49. *(uint8_t *)(dst + 1) = *(uint8_t *)(src)++; \
  50. *(uint8_t *)(dst + 0) = *(uint8_t *)(src)++; \
  51. }
  52. /**
  53. * @brief Types of actual Modbus implementation
  54. */
  55. typedef enum
  56. {
  57. MB_PORT_SERIAL_MASTER = 0x00, /*!< Modbus port type serial master. */
  58. MB_PORT_SERIAL_SLAVE, /*!< Modbus port type serial slave. */
  59. MB_PORT_TCP_MASTER, /*!< Modbus port type TCP master. */
  60. MB_PORT_TCP_SLAVE, /*!< Modbus port type TCP slave. */
  61. MB_PORT_COUNT, /*!< Modbus port count. */
  62. MB_PORT_INACTIVE = 0xFF
  63. } mb_port_type_t;
  64. /**
  65. * @brief Event group for parameters notification
  66. */
  67. typedef enum
  68. {
  69. MB_EVENT_NO_EVENTS = 0x00,
  70. MB_EVENT_HOLDING_REG_WR = BIT0, /*!< Modbus Event Write Holding registers. */
  71. MB_EVENT_HOLDING_REG_RD = BIT1, /*!< Modbus Event Read Holding registers. */
  72. MB_EVENT_INPUT_REG_RD = BIT3, /*!< Modbus Event Read Input registers. */
  73. MB_EVENT_COILS_WR = BIT4, /*!< Modbus Event Write Coils. */
  74. MB_EVENT_COILS_RD = BIT5, /*!< Modbus Event Read Coils. */
  75. MB_EVENT_DISCRETE_RD = BIT6, /*!< Modbus Event Read Discrete bits. */
  76. MB_EVENT_STACK_STARTED = BIT7 /*!< Modbus Event Stack started */
  77. } mb_event_group_t;
  78. /**
  79. * @brief Type of Modbus parameter
  80. */
  81. typedef enum {
  82. MB_PARAM_HOLDING = 0x00, /*!< Modbus Holding register. */
  83. MB_PARAM_INPUT, /*!< Modbus Input register. */
  84. MB_PARAM_COIL, /*!< Modbus Coils. */
  85. MB_PARAM_DISCRETE, /*!< Modbus Discrete bits. */
  86. MB_PARAM_COUNT,
  87. MB_PARAM_UNKNOWN = 0xFF
  88. } mb_param_type_t;
  89. /*!
  90. * \brief Modbus serial transmission modes (RTU/ASCII).
  91. */
  92. typedef enum {
  93. MB_MODE_RTU, /*!< RTU transmission mode. */
  94. MB_MODE_ASCII, /*!< ASCII transmission mode. */
  95. MB_MODE_TCP, /*!< TCP communication mode. */
  96. MB_MODE_UDP /*!< UDP communication mode. */
  97. } mb_mode_type_t;
  98. /*!
  99. * \brief Modbus TCP type of address.
  100. */
  101. typedef enum {
  102. MB_IPV4 = 0, /*!< TCP IPV4 addressing */
  103. MB_IPV6 = 1 /*!< TCP IPV6 addressing */
  104. } mb_tcp_addr_type_t;
  105. /**
  106. * @brief Device communication structure to setup Modbus controller
  107. */
  108. typedef union {
  109. // Serial communication structure
  110. struct {
  111. mb_mode_type_t mode; /*!< Modbus communication mode */
  112. uint8_t slave_addr; /*!< Modbus slave address field (dummy for master) */
  113. uart_port_t port; /*!< Modbus communication port (UART) number */
  114. uint32_t baudrate; /*!< Modbus baudrate */
  115. uart_parity_t parity; /*!< Modbus UART parity settings */
  116. uint16_t dummy_port; /*!< Dummy field, unused */
  117. };
  118. // TCP/UDP communication structure
  119. struct {
  120. mb_mode_type_t ip_mode; /*!< Modbus communication mode */
  121. uint16_t ip_port; /*!< Modbus port */
  122. mb_tcp_addr_type_t ip_addr_type; /*!< Modbus address type */
  123. void* ip_addr; /*!< Modbus address table for connection */
  124. void* ip_netif_ptr; /*!< Modbus network interface */
  125. };
  126. } mb_communication_info_t;
  127. /**
  128. * common interface method types
  129. */
  130. typedef esp_err_t (*iface_init)(void**); /*!< Interface method init */
  131. typedef esp_err_t (*iface_destroy)(void); /*!< Interface method destroy */
  132. typedef esp_err_t (*iface_setup)(void*); /*!< Interface method setup */
  133. typedef esp_err_t (*iface_start)(void); /*!< Interface method start */
  134. #ifdef __cplusplus
  135. }
  136. #endif
  137. #endif // _MB_IFACE_COMMON_H