esp_modbus_common.h 5.6 KB

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