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.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "../../../inc/MarlinConfigPre.h"
  24. #if ENABLED(EMERGENCY_PARSER)
  25. #include "../../../feature/e_parser.h"
  26. #endif
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. /**
  30. * Generic RingBuffer
  31. * T type of the buffer array
  32. * S size of the buffer (must be power of 2)
  33. *
  34. */
  35. template <typename T, uint32_t S> class RingBuffer {
  36. public:
  37. RingBuffer() { index_read = index_write = 0; }
  38. uint32_t available() volatile { return index_write - index_read; }
  39. uint32_t free() volatile { return buffer_size - available(); }
  40. bool empty() volatile { return index_read == index_write; }
  41. bool full() volatile { return available() == buffer_size; }
  42. void clear() volatile { index_read = index_write = 0; }
  43. bool peek(T *value) volatile {
  44. if (value == 0 || available() == 0)
  45. return false;
  46. *value = buffer[mask(index_read)];
  47. return true;
  48. }
  49. int read() volatile {
  50. if (empty()) return -1;
  51. return buffer[mask(index_read++)];
  52. }
  53. bool write(T value) volatile {
  54. if (full()) return false;
  55. buffer[mask(index_write++)] = value;
  56. return true;
  57. }
  58. private:
  59. uint32_t mask(uint32_t val) volatile {
  60. return buffer_mask & val;
  61. }
  62. static const uint32_t buffer_size = S;
  63. static const uint32_t buffer_mask = buffer_size - 1;
  64. volatile T buffer[buffer_size];
  65. volatile uint32_t index_write;
  66. volatile uint32_t index_read;
  67. };
  68. class HalSerial {
  69. public:
  70. #if ENABLED(EMERGENCY_PARSER)
  71. EmergencyParser::State emergency_state;
  72. static inline bool emergency_parser_enabled() { return true; }
  73. #endif
  74. HalSerial() { host_connected = true; }
  75. void begin(int32_t) {}
  76. void end() {}
  77. int peek() {
  78. uint8_t value;
  79. return receive_buffer.peek(&value) ? value : -1;
  80. }
  81. int read() { return receive_buffer.read(); }
  82. size_t write(char c) {
  83. if (!host_connected) return 0;
  84. while (!transmit_buffer.free());
  85. return transmit_buffer.write(c);
  86. }
  87. operator bool() { return host_connected; }
  88. uint16_t available() {
  89. return (uint16_t)receive_buffer.available();
  90. }
  91. void flush() { receive_buffer.clear(); }
  92. uint8_t availableForWrite() {
  93. return transmit_buffer.free() > 255 ? 255 : (uint8_t)transmit_buffer.free();
  94. }
  95. void flushTX() {
  96. if (host_connected)
  97. while (transmit_buffer.available()) { /* nada */ }
  98. }
  99. void printf(const char *format, ...) {
  100. static char buffer[256];
  101. va_list vArgs;
  102. va_start(vArgs, format);
  103. int length = vsnprintf((char *) buffer, 256, (char const *) format, vArgs);
  104. va_end(vArgs);
  105. if (length > 0 && length < 256) {
  106. if (host_connected) {
  107. for (int i = 0; i < length;) {
  108. if (transmit_buffer.write(buffer[i])) {
  109. ++i;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. #define DEC 10
  116. #define HEX 16
  117. #define OCT 8
  118. #define BIN 2
  119. void print_bin(uint32_t value, uint8_t num_digits) {
  120. uint32_t mask = 1 << (num_digits -1);
  121. for (uint8_t i = 0; i < num_digits; i++) {
  122. if (!(i % 4) && i) write(' ');
  123. if (!(i % 16) && i) write(' ');
  124. if (value & mask) write('1');
  125. else write('0');
  126. value <<= 1;
  127. }
  128. }
  129. void print(const char value[]) { printf("%s" , value); }
  130. void print(char value, int nbase = 0) {
  131. if (nbase == BIN) print_bin(value, 8);
  132. else if (nbase == OCT) printf("%3o", value);
  133. else if (nbase == HEX) printf("%2X", value);
  134. else if (nbase == DEC ) printf("%d", value);
  135. else printf("%c" , value);
  136. }
  137. void print(unsigned char value, int nbase = 0) {
  138. if (nbase == BIN) print_bin(value, 8);
  139. else if (nbase == OCT) printf("%3o", value);
  140. else if (nbase == HEX) printf("%2X", value);
  141. else printf("%u" , value);
  142. }
  143. void print(int value, int nbase = 0) {
  144. if (nbase == BIN) print_bin(value, 16);
  145. else if (nbase == OCT) printf("%6o", value);
  146. else if (nbase == HEX) printf("%4X", value);
  147. else printf("%d", value);
  148. }
  149. void print(unsigned int value, int nbase = 0) {
  150. if (nbase == BIN) print_bin(value, 16);
  151. else if (nbase == OCT) printf("%6o", value);
  152. else if (nbase == HEX) printf("%4X", value);
  153. else printf("%u" , value);
  154. }
  155. void print(long value, int nbase = 0) {
  156. if (nbase == BIN) print_bin(value, 32);
  157. else if (nbase == OCT) printf("%11o", value);
  158. else if (nbase == HEX) printf("%8X", value);
  159. else printf("%ld" , value);
  160. }
  161. void print(unsigned long value, int nbase = 0) {
  162. if (nbase == BIN) print_bin(value, 32);
  163. else if (nbase == OCT) printf("%11o", value);
  164. else if (nbase == HEX) printf("%8X", value);
  165. else printf("%lu" , value);
  166. }
  167. void print(float value, int round = 6) { printf("%f" , value); }
  168. void print(double value, int round = 6) { printf("%f" , value); }
  169. void println(const char value[]) { printf("%s\n" , value); }
  170. void println(char value, int nbase = 0) { print(value, nbase); println(); }
  171. void println(unsigned char value, int nbase = 0) { print(value, nbase); println(); }
  172. void println(int value, int nbase = 0) { print(value, nbase); println(); }
  173. void println(unsigned int value, int nbase = 0) { print(value, nbase); println(); }
  174. void println(long value, int nbase = 0) { print(value, nbase); println(); }
  175. void println(unsigned long value, int nbase = 0) { print(value, nbase); println(); }
  176. void println(float value, int round = 6) { printf("%f\n" , value); }
  177. void println(double value, int round = 6) { printf("%f\n" , value); }
  178. void println() { print('\n'); }
  179. volatile RingBuffer<uint8_t, 128> receive_buffer;
  180. volatile RingBuffer<uint8_t, 128> transmit_buffer;
  181. volatile bool host_connected;
  182. };