My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

twibus.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef TWIBUS_H
  23. #define TWIBUS_H
  24. /**
  25. * TWIBUS class
  26. *
  27. * This class implements a wrapper around the two wire (I2C) bus, it allows
  28. * Marlin to send and request data from any slave device on the bus. This is
  29. * an experimental feature and it's inner workings as well as public facing
  30. * interface are prune to change in the future.
  31. *
  32. * The two main consumers of this class are M155 and M156, where M155 allows
  33. * Marlin to send a I2C packet to a device (please be aware that no repeated
  34. * starts are possible), this can be done in caching method by calling multiple
  35. * times M155 B<byte-1 value in base 10> or a one liner M155, have a look at
  36. * the gcode_M155() function for more information. M156 allows Marlin to
  37. * request data from a device, the received data is then relayed into the serial
  38. * line for host interpretation.
  39. *
  40. */
  41. class TWIBus {
  42. private:
  43. /**
  44. * @brief Timeout value in milliseconds
  45. * @details For blocking operations this constant value will set the max
  46. * amount of time Marlin will keep waiting for a reply. Useful is something
  47. * goes wrong on the bus and the SDA/SCL lines are held up by another device.
  48. */
  49. const int timeout = 5;
  50. /**
  51. * @brief Target device address
  52. * @description This stores, until the buffer is flushed, the target device
  53. * address, take not we do follow Arduino 7bit addressing.
  54. */
  55. uint8_t addr = 0;
  56. /**
  57. * @brief Number of bytes on buffer
  58. * @description This var holds the total number of bytes on our buffer
  59. * waiting to be flushed to the bus.
  60. */
  61. uint8_t buffer_s = 0;
  62. /**
  63. * @brief Internal buffer
  64. * @details This is a fixed buffer, TWI command cannot be longer than this
  65. */
  66. char buffer[30];
  67. public:
  68. /**
  69. * @brief Class constructor
  70. * @details Initialized the TWI bus and clears the buffer
  71. */
  72. TWIBus();
  73. /**
  74. * @brief Reset the buffer
  75. * @details Brings the internal buffer to a known-empty state
  76. */
  77. void reset();
  78. /**
  79. * @brief Send the buffer data to the bus
  80. * @details Flushed the buffer into the bus targeting the cached slave device
  81. * address.
  82. */
  83. void send();
  84. /**
  85. * @brief Add one byte to the buffer
  86. * @details Adds the byte to the buffer in a sequential way, if buffer is full
  87. * the request is silently ignored.
  88. *
  89. * @param c a data byte
  90. */
  91. void addbyte(char c);
  92. /**
  93. * @brief Sets the target slave address
  94. * @details The target slave address is stored so it can be later used when
  95. * the complete packet needs to be sent over the bus.
  96. *
  97. * @param addr 7-bit integer address
  98. */
  99. void address(uint8_t addr);
  100. /**
  101. * @brief Request data from slave device
  102. * @details Requests data from a slave device, when the data is received it will
  103. * be relayed to the serial line using a parser-friendly formatting.
  104. *
  105. * @param bytes the number of bytes to request
  106. */
  107. void reqbytes(uint8_t bytes);
  108. };
  109. #endif //TWIBUS_H