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

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