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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "../../../core/serial_hook.h"
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. /**
  31. * Generic RingBuffer
  32. * T type of the buffer array
  33. * S size of the buffer (must be power of 2)
  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. struct HalSerial {
  69. HalSerial() { host_connected = true; }
  70. void begin(int32_t) {}
  71. void end() {}
  72. int peek() {
  73. uint8_t value;
  74. return receive_buffer.peek(&value) ? value : -1;
  75. }
  76. int read() { return receive_buffer.read(); }
  77. size_t write(char c) {
  78. if (!host_connected) return 0;
  79. while (!transmit_buffer.free());
  80. return transmit_buffer.write(c);
  81. }
  82. bool connected() { return host_connected; }
  83. uint16_t available() {
  84. return (uint16_t)receive_buffer.available();
  85. }
  86. void flush() { receive_buffer.clear(); }
  87. uint8_t availableForWrite() {
  88. return transmit_buffer.free() > 255 ? 255 : (uint8_t)transmit_buffer.free();
  89. }
  90. void flushTX() {
  91. if (host_connected)
  92. while (transmit_buffer.available()) { /* nada */ }
  93. }
  94. volatile RingBuffer<uint8_t, 128> receive_buffer;
  95. volatile RingBuffer<uint8_t, 128> transmit_buffer;
  96. volatile bool host_connected;
  97. };
  98. typedef Serial0Type<HalSerial> MSerialT;