esp_modbus_common.h 6.6 KB

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