My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serial_hook.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "serial_base.h"
  24. // A mask containing a bitmap of the serial port to act upon
  25. // This is written to ensure a serial index is never used as a serial mask
  26. class SerialMask {
  27. uint8_t mask;
  28. // This constructor is private to ensure you can't convert an index to a mask
  29. // The compiler will stop here if you are mixing index and mask in your code.
  30. // If you need to, you'll have to use the explicit static "from" method here
  31. SerialMask(const serial_index_t);
  32. public:
  33. inline constexpr bool enabled(const SerialMask PortMask) const { return mask & PortMask.mask; }
  34. inline constexpr SerialMask combine(const SerialMask other) const { return SerialMask(mask | other.mask); }
  35. inline constexpr SerialMask operator<< (const int offset) const { return SerialMask(mask << offset); }
  36. static inline SerialMask from(const serial_index_t index) {
  37. if (index.valid()) return SerialMask(_BV(index.index));
  38. return SerialMask(0); // A invalid index mean no output
  39. }
  40. constexpr SerialMask(const uint8_t mask) : mask(mask) {}
  41. constexpr SerialMask(const SerialMask & other) : mask(other.mask) {} // Can't use = default here since not all framework support this
  42. static constexpr uint8_t All = 0xFF;
  43. };
  44. // The most basic serial class: it dispatch to the base serial class with no hook whatsoever. This will compile to nothing but the base serial class
  45. template <class SerialT>
  46. struct BaseSerial : public SerialBase< BaseSerial<SerialT> >, public SerialT {
  47. typedef SerialBase< BaseSerial<SerialT> > BaseClassT;
  48. // It's required to implement a write method here to help compiler disambiguate what method to call
  49. using SerialT::write;
  50. using SerialT::flush;
  51. void msgDone() {}
  52. // We don't care about indices here, since if one can call us, it's the right index anyway
  53. int available(serial_index_t) { return (int)SerialT::available(); }
  54. int read(serial_index_t) { return (int)SerialT::read(); }
  55. bool connected() { return CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected);; }
  56. void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
  57. // We have 2 implementation of the same method in both base class, let's say which one we want
  58. using SerialT::available;
  59. using SerialT::read;
  60. using SerialT::begin;
  61. using SerialT::end;
  62. using BaseClassT::print;
  63. using BaseClassT::println;
  64. BaseSerial(const bool e) : BaseClassT(e) {}
  65. // Forward constructor
  66. template <typename... Args>
  67. BaseSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...) {}
  68. };
  69. // A serial with a condition checked at runtime for its output
  70. // A bit less efficient than static dispatching but since it's only used for ethernet's serial output right now, it's ok.
  71. template <class SerialT>
  72. struct ConditionalSerial : public SerialBase< ConditionalSerial<SerialT> > {
  73. typedef SerialBase< ConditionalSerial<SerialT> > BaseClassT;
  74. bool & condition;
  75. SerialT & out;
  76. NO_INLINE size_t write(uint8_t c) { if (condition) return out.write(c); return 0; }
  77. void flush() { if (condition) out.flush(); }
  78. void begin(long br) { out.begin(br); }
  79. void end() { out.end(); }
  80. void msgDone() {}
  81. bool connected() { return CALL_IF_EXISTS(bool, &out, connected); }
  82. void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
  83. int available(serial_index_t ) { return (int)out.available(); }
  84. int read(serial_index_t ) { return (int)out.read(); }
  85. int available() { return (int)out.available(); }
  86. int read() { return (int)out.read(); }
  87. ConditionalSerial(bool & conditionVariable, SerialT & out, const bool e) : BaseClassT(e), condition(conditionVariable), out(out) {}
  88. };
  89. // A simple foward class that taking a reference to an existing serial instance (likely created in their respective framework)
  90. template <class SerialT>
  91. struct ForwardSerial : public SerialBase< ForwardSerial<SerialT> > {
  92. typedef SerialBase< ForwardSerial<SerialT> > BaseClassT;
  93. SerialT & out;
  94. NO_INLINE size_t write(uint8_t c) { return out.write(c); }
  95. void flush() { out.flush(); }
  96. void begin(long br) { out.begin(br); }
  97. void end() { out.end(); }
  98. void msgDone() {}
  99. // Existing instances implement Arduino's operator bool, so use that if it's available
  100. bool connected() { return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, &out, connected) : (bool)out; }
  101. void flushTX() { CALL_IF_EXISTS(void, &out, flushTX); }
  102. int available(serial_index_t) { return (int)out.available(); }
  103. int read(serial_index_t) { return (int)out.read(); }
  104. int available() { return (int)out.available(); }
  105. int read() { return (int)out.read(); }
  106. ForwardSerial(const bool e, SerialT & out) : BaseClassT(e), out(out) {}
  107. };
  108. // A class that's can be hooked and unhooked at runtime, useful to capturing the output of the serial interface
  109. template <class SerialT>
  110. struct RuntimeSerial : public SerialBase< RuntimeSerial<SerialT> >, public SerialT {
  111. typedef SerialBase< RuntimeSerial<SerialT> > BaseClassT;
  112. typedef void (*WriteHook)(void * userPointer, uint8_t c);
  113. typedef void (*EndOfMessageHook)(void * userPointer);
  114. WriteHook writeHook;
  115. EndOfMessageHook eofHook;
  116. void * userPointer;
  117. NO_INLINE size_t write(uint8_t c) {
  118. if (writeHook) writeHook(userPointer, c);
  119. return SerialT::write(c);
  120. }
  121. NO_INLINE void msgDone() {
  122. if (eofHook) eofHook(userPointer);
  123. }
  124. int available(serial_index_t) { return (int)SerialT::available(); }
  125. int read(serial_index_t) { return (int)SerialT::read(); }
  126. using SerialT::available;
  127. using SerialT::read;
  128. using SerialT::flush;
  129. using SerialT::begin;
  130. using SerialT::end;
  131. using BaseClassT::print;
  132. using BaseClassT::println;
  133. // Underlying implementation might use Arduino's bool operator
  134. bool connected() {
  135. return Private::HasMember_connected<SerialT>::value ? CALL_IF_EXISTS(bool, static_cast<SerialT*>(this), connected) : static_cast<SerialT*>(this)->operator bool();
  136. }
  137. void flushTX() { CALL_IF_EXISTS(void, static_cast<SerialT*>(this), flushTX); }
  138. void setHook(WriteHook writeHook = 0, EndOfMessageHook eofHook = 0, void * userPointer = 0) {
  139. // Order is important here as serial code can be called inside interrupts
  140. // When setting a hook, the user pointer must be set first so if writeHook is called as soon as it's set, it'll be valid
  141. if (userPointer) this->userPointer = userPointer;
  142. this->writeHook = writeHook;
  143. this->eofHook = eofHook;
  144. // Order is important here because of asynchronous access here
  145. // When unsetting a hook, the user pointer must be unset last so that any pending writeHook is still using the old pointer
  146. if (!userPointer) this->userPointer = 0;
  147. }
  148. RuntimeSerial(const bool e) : BaseClassT(e), writeHook(0), eofHook(0), userPointer(0) {}
  149. // Forward constructor
  150. template <typename... Args>
  151. RuntimeSerial(const bool e, Args... args) : BaseClassT(e), SerialT(args...), writeHook(0), eofHook(0), userPointer(0) {}
  152. };
  153. // A class that duplicates its output conditionally to 2 serial interfaces
  154. template <class Serial0T, class Serial1T, const uint8_t offset = 0, const uint8_t step = 1>
  155. struct MultiSerial : public SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > {
  156. typedef SerialBase< MultiSerial<Serial0T, Serial1T, offset, step> > BaseClassT;
  157. SerialMask portMask;
  158. Serial0T & serial0;
  159. Serial1T & serial1;
  160. static constexpr uint8_t Usage = ((1 << step) - 1); // A bit mask containing as many bits as step
  161. static constexpr uint8_t FirstOutput = (Usage << offset);
  162. static constexpr uint8_t SecondOutput = (Usage << (offset + step));
  163. static constexpr uint8_t Both = FirstOutput | SecondOutput;
  164. NO_INLINE size_t write(uint8_t c) {
  165. size_t ret = 0;
  166. if (portMask.enabled(FirstOutput)) ret = serial0.write(c);
  167. if (portMask.enabled(SecondOutput)) ret = serial1.write(c) | ret;
  168. return ret;
  169. }
  170. NO_INLINE void msgDone() {
  171. if (portMask.enabled(FirstOutput)) serial0.msgDone();
  172. if (portMask.enabled(SecondOutput)) serial1.msgDone();
  173. }
  174. int available(serial_index_t index) {
  175. if (index.within(0 + offset, step + offset - 1))
  176. return serial0.available(index);
  177. else if (index.within(step + offset, 2 * step + offset - 1))
  178. return serial1.available(index);
  179. return false;
  180. }
  181. int read(serial_index_t index) {
  182. if (index.within(0 + offset, step + offset - 1))
  183. return serial0.read(index);
  184. else if (index.within(step + offset, 2 * step + offset - 1))
  185. return serial1.read(index);
  186. return -1;
  187. }
  188. void begin(const long br) {
  189. if (portMask.enabled(FirstOutput)) serial0.begin(br);
  190. if (portMask.enabled(SecondOutput)) serial1.begin(br);
  191. }
  192. void end() {
  193. if (portMask.enabled(FirstOutput)) serial0.end();
  194. if (portMask.enabled(SecondOutput)) serial1.end();
  195. }
  196. bool connected() {
  197. bool ret = true;
  198. if (portMask.enabled(FirstOutput)) ret = CALL_IF_EXISTS(bool, &serial0, connected);
  199. if (portMask.enabled(SecondOutput)) ret = ret && CALL_IF_EXISTS(bool, &serial1, connected);
  200. return ret;
  201. }
  202. using BaseClassT::available;
  203. using BaseClassT::read;
  204. // Redirect flush
  205. NO_INLINE void flush() {
  206. if (portMask.enabled(FirstOutput)) serial0.flush();
  207. if (portMask.enabled(SecondOutput)) serial1.flush();
  208. }
  209. NO_INLINE void flushTX() {
  210. if (portMask.enabled(FirstOutput)) CALL_IF_EXISTS(void, &serial0, flushTX);
  211. if (portMask.enabled(SecondOutput)) CALL_IF_EXISTS(void, &serial1, flushTX);
  212. }
  213. MultiSerial(Serial0T & serial0, Serial1T & serial1, const SerialMask mask = Both, const bool e = false) :
  214. BaseClassT(e),
  215. portMask(mask), serial0(serial0), serial1(serial1) {}
  216. };
  217. // Build the actual serial object depending on current configuration
  218. #define Serial1Class TERN(SERIAL_RUNTIME_HOOK, RuntimeSerial, BaseSerial)
  219. #define ForwardSerial1Class TERN(SERIAL_RUNTIME_HOOK, RuntimeSerial, ForwardSerial)
  220. #ifdef HAS_MULTI_SERIAL
  221. #define Serial2Class ConditionalSerial
  222. #endif