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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 _HAL_SERIAL_H_
  23. #define _HAL_SERIAL_H_
  24. #include "../../../inc/MarlinConfigPre.h"
  25. #if ENABLED(EMERGENCY_PARSER)
  26. #include "../../../feature/emergency_parser.h"
  27. #endif
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <Print.h>
  31. /**
  32. * Generic RingBuffer
  33. * T type of the buffer array
  34. * S size of the buffer (must be power of 2)
  35. */
  36. template <typename T, uint32_t S> class RingBuffer {
  37. public:
  38. RingBuffer() {index_read = index_write = 0;}
  39. uint32_t available() {return mask(index_write - index_read);}
  40. uint32_t free() {return buffer_size - available();}
  41. bool empty() {return index_read == index_write;}
  42. bool full() {return next(index_write) == index_read;}
  43. void clear() {index_read = index_write = 0;}
  44. bool peek(T *const value) {
  45. if (value == nullptr || empty()) return false;
  46. *value = buffer[index_read];
  47. return true;
  48. }
  49. uint32_t read(T *const value) {
  50. if (value == nullptr || empty()) return 0;
  51. *value = buffer[index_read];
  52. index_read = next(index_read);
  53. return 1;
  54. }
  55. uint32_t write(T value) {
  56. uint32_t next_head = next(index_write);
  57. if (next_head == index_read) return 0; // buffer full
  58. buffer[index_write] = value;
  59. index_write = next_head;
  60. return 1;
  61. }
  62. private:
  63. inline uint32_t mask(uint32_t val) {
  64. return val & buffer_mask;
  65. }
  66. inline uint32_t next(uint32_t val) {
  67. return mask(val + 1);
  68. }
  69. static const uint32_t buffer_size = S;
  70. static const uint32_t buffer_mask = buffer_size - 1;
  71. T buffer[buffer_size];
  72. volatile uint32_t index_write;
  73. volatile uint32_t index_read;
  74. };
  75. /**
  76. * Serial Interface Class
  77. * Data is injected directly into, and consumed from, the fifo buffers
  78. */
  79. class HalSerial: public Print {
  80. public:
  81. #if ENABLED(EMERGENCY_PARSER)
  82. EmergencyParser::State emergency_state;
  83. #endif
  84. HalSerial() : host_connected(false) { }
  85. virtual ~HalSerial() { }
  86. operator bool() { return host_connected; }
  87. void begin(int32_t baud) { }
  88. int16_t peek() {
  89. uint8_t value;
  90. return receive_buffer.peek(&value) ? value : -1;
  91. }
  92. int16_t read() {
  93. uint8_t value;
  94. return receive_buffer.read(&value) ? value : -1;
  95. }
  96. size_t write(const uint8_t c) {
  97. if (!host_connected) return 0; // Do not fill buffer when host disconnected
  98. while (transmit_buffer.write(c) == 0) { // Block until there is free room in buffer
  99. if (!host_connected) return 0; // Break infinite loop on host disconect
  100. }
  101. return 1;
  102. }
  103. size_t available() {
  104. return (size_t)receive_buffer.available();
  105. }
  106. void flush() {
  107. receive_buffer.clear();
  108. }
  109. uint8_t availableForWrite(void) {
  110. return transmit_buffer.free() > 255 ? 255 : (uint8_t)transmit_buffer.free();
  111. }
  112. void flushTX(void) {
  113. while (transmit_buffer.available() && host_connected) { /* nada */}
  114. }
  115. size_t printf(const char *format, ...) {
  116. static char buffer[256];
  117. va_list vArgs;
  118. va_start(vArgs, format);
  119. int length = vsnprintf((char *) buffer, 256, (char const *) format, vArgs);
  120. va_end(vArgs);
  121. size_t i = 0;
  122. if (length > 0 && length < 256) {
  123. while (i < (size_t)length && host_connected) {
  124. i += transmit_buffer.write(buffer[i]);
  125. }
  126. }
  127. return i;
  128. }
  129. RingBuffer<uint8_t, 128> receive_buffer;
  130. RingBuffer<uint8_t, 128> transmit_buffer;
  131. volatile bool host_connected;
  132. };
  133. #endif // _HAL_SERIAL_H_