esp_modbus_common.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_SERIAL_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_type_t;
  63. /**
  64. * @brief Event group for parameters notification
  65. */
  66. typedef enum
  67. {
  68. MB_EVENT_NO_EVENTS = 0x00,
  69. MB_EVENT_HOLDING_REG_WR = BIT0, /*!< Modbus Event Write Holding registers. */
  70. MB_EVENT_HOLDING_REG_RD = BIT1, /*!< Modbus Event Read Holding registers. */
  71. MB_EVENT_INPUT_REG_RD = BIT3, /*!< Modbus Event Read Input registers. */
  72. MB_EVENT_COILS_WR = BIT4, /*!< Modbus Event Write Coils. */
  73. MB_EVENT_COILS_RD = BIT5, /*!< Modbus Event Read Coils. */
  74. MB_EVENT_DISCRETE_RD = BIT6, /*!< Modbus Event Read Discrete bits. */
  75. MB_EVENT_STACK_STARTED = BIT7 /*!< Modbus Event Stack started */
  76. } mb_event_group_t;
  77. /**
  78. * @brief Type of Modbus parameter
  79. */
  80. typedef enum {
  81. MB_PARAM_HOLDING = 0x00, /*!< Modbus Holding register. */
  82. MB_PARAM_INPUT, /*!< Modbus Input register. */
  83. MB_PARAM_COIL, /*!< Modbus Coils. */
  84. MB_PARAM_DISCRETE, /*!< Modbus Discrete bits. */
  85. MB_PARAM_COUNT,
  86. MB_PARAM_UNKNOWN = 0xFF
  87. } mb_param_type_t;
  88. /*!
  89. * \brief Modbus serial transmission modes (RTU/ASCII).
  90. */
  91. typedef enum {
  92. MB_MODE_RTU, /*!< RTU transmission mode. */
  93. MB_MODE_ASCII, /*!< ASCII transmission mode. */
  94. MB_MODE_TCP /*!< TCP mode. */
  95. } mb_mode_type_t; // Todo: This is common type leave it here for now
  96. /**
  97. * @brief Device communication structure to setup Modbus controller
  98. */
  99. typedef union {
  100. // Serial communication structure
  101. struct {
  102. mb_mode_type_t mode; /*!< Modbus communication mode */
  103. uint8_t slave_addr; /*!< Modbus slave address field (dummy for master) */
  104. uart_port_t port; /*!< Modbus communication port (UART) number */
  105. uint32_t baudrate; /*!< Modbus baudrate */
  106. uart_parity_t parity; /*!< Modbus UART parity settings */
  107. uint16_t dummy_port; /*!< Dummy field, unused */
  108. };
  109. // Tcp communication structure
  110. struct {
  111. mb_mode_type_t tcp_mode; /*!< Modbus communication mode */
  112. uint8_t dummy_addr; /*!< Modbus slave address field (dummy for master) */
  113. uart_port_t dummy_uart_port; /*!< Modbus communication port (UART) number */
  114. uint32_t dummy_baudrate; /*!< Modbus baudrate */
  115. uart_parity_t dummy_parity; /*!< Modbus UART parity settings */
  116. uint16_t tcp_port; /*!< Modbus TCP port */
  117. };
  118. } mb_communication_info_t;
  119. /**
  120. * common interface method types
  121. */
  122. typedef esp_err_t (*iface_init)(mb_port_type_t, void**); /*!< Interface method init */
  123. typedef esp_err_t (*iface_destroy)(void); /*!< Interface method destroy */
  124. typedef esp_err_t (*iface_setup)(void*); /*!< Interface method setup */
  125. typedef esp_err_t (*iface_start)(void); /*!< Interface method start */
  126. #ifdef __cplusplus
  127. }
  128. #endif
  129. #endif // _MB_IFACE_COMMON_H