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.

MarlinSerial.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /**
  24. * MarlinSerial_Due.h - Hardware serial library for Arduino DUE
  25. * Copyright (c) 2017 Eduardo José Tagle. All right reserved
  26. * Based on MarlinSerial for AVR, copyright (c) 2006 Nicholas Zambetti. All right reserved.
  27. */
  28. #include <WString.h>
  29. #include "../../inc/MarlinConfigPre.h"
  30. #include "../../core/serial_hook.h"
  31. // Define constants and variables for buffering incoming serial data. We're
  32. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  33. // location to which to write the next incoming character and rx_buffer_tail
  34. // is the index of the location from which to read.
  35. // 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256)
  36. #ifndef RX_BUFFER_SIZE
  37. #define RX_BUFFER_SIZE 128
  38. #endif
  39. #ifndef TX_BUFFER_SIZE
  40. #define TX_BUFFER_SIZE 32
  41. #endif
  42. //#if ENABLED(SERIAL_XON_XOFF) && RX_BUFFER_SIZE < 1024
  43. // #error "SERIAL_XON_XOFF requires RX_BUFFER_SIZE >= 1024 for reliable transfers without drops."
  44. //#elif RX_BUFFER_SIZE && (RX_BUFFER_SIZE < 2 || !IS_POWER_OF_2(RX_BUFFER_SIZE))
  45. // #error "RX_BUFFER_SIZE must be a power of 2 greater than 1."
  46. //#elif TX_BUFFER_SIZE && (TX_BUFFER_SIZE < 2 || TX_BUFFER_SIZE > 256 || !IS_POWER_OF_2(TX_BUFFER_SIZE))
  47. // #error "TX_BUFFER_SIZE must be 0, a power of 2 greater than 1, and no greater than 256."
  48. //#endif
  49. // Templated type selector
  50. template<bool b, typename T, typename F> struct TypeSelector { typedef T type;} ;
  51. template<typename T, typename F> struct TypeSelector<false, T, F> { typedef F type; };
  52. // Templated structure wrapper
  53. template<typename S, unsigned int addr> struct StructWrapper {
  54. constexpr StructWrapper(int) {}
  55. FORCE_INLINE S* operator->() const { return (S*)addr; }
  56. };
  57. template<typename Cfg>
  58. class MarlinSerial {
  59. protected:
  60. // Information for all supported UARTs
  61. static constexpr uint32_t BASES[] = {0x400E0800U, 0x40098000U, 0x4009C000U, 0x400A0000U, 0x400A4000U};
  62. static constexpr IRQn_Type IRQS[] = { UART_IRQn, USART0_IRQn, USART1_IRQn, USART2_IRQn, USART3_IRQn};
  63. static constexpr int IRQ_IDS[] = { ID_UART, ID_USART0, ID_USART1, ID_USART2, ID_USART3};
  64. // Alias for shorter code
  65. static constexpr StructWrapper<Uart,BASES[Cfg::PORT]> HWUART = 0;
  66. static constexpr IRQn_Type HWUART_IRQ = IRQS[Cfg::PORT];
  67. static constexpr int HWUART_IRQ_ID = IRQ_IDS[Cfg::PORT];
  68. // Base size of type on buffer size
  69. typedef typename TypeSelector<(Cfg::RX_SIZE>256), uint16_t, uint8_t>::type ring_buffer_pos_t;
  70. struct ring_buffer_r {
  71. volatile ring_buffer_pos_t head, tail;
  72. unsigned char buffer[Cfg::RX_SIZE];
  73. };
  74. struct ring_buffer_t {
  75. volatile uint8_t head, tail;
  76. unsigned char buffer[Cfg::TX_SIZE];
  77. };
  78. static ring_buffer_r rx_buffer;
  79. static ring_buffer_t tx_buffer;
  80. static bool _written;
  81. static constexpr uint8_t XON_XOFF_CHAR_SENT = 0x80, // XON / XOFF Character was sent
  82. XON_XOFF_CHAR_MASK = 0x1F; // XON / XOFF character to send
  83. // XON / XOFF character definitions
  84. static constexpr uint8_t XON_CHAR = 17, XOFF_CHAR = 19;
  85. static uint8_t xon_xoff_state,
  86. rx_dropped_bytes,
  87. rx_buffer_overruns,
  88. rx_framing_errors;
  89. static ring_buffer_pos_t rx_max_enqueued;
  90. FORCE_INLINE static void store_rxd_char();
  91. FORCE_INLINE static void _tx_thr_empty_irq();
  92. static void UART_ISR();
  93. public:
  94. MarlinSerial() {};
  95. static void begin(const long);
  96. static void end();
  97. static int peek();
  98. static int read();
  99. static void flush();
  100. static ring_buffer_pos_t available();
  101. static size_t write(const uint8_t c);
  102. static void flushTX();
  103. static inline bool emergency_parser_enabled() { return Cfg::EMERGENCYPARSER; }
  104. FORCE_INLINE static uint8_t dropped() { return Cfg::DROPPED_RX ? rx_dropped_bytes : 0; }
  105. FORCE_INLINE static uint8_t buffer_overruns() { return Cfg::RX_OVERRUNS ? rx_buffer_overruns : 0; }
  106. FORCE_INLINE static uint8_t framing_errors() { return Cfg::RX_FRAMING_ERRORS ? rx_framing_errors : 0; }
  107. FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return Cfg::MAX_RX_QUEUED ? rx_max_enqueued : 0; }
  108. };
  109. // Serial port configuration
  110. template <uint8_t serial>
  111. struct MarlinSerialCfg {
  112. static constexpr int PORT = serial;
  113. static constexpr unsigned int RX_SIZE = RX_BUFFER_SIZE;
  114. static constexpr unsigned int TX_SIZE = TX_BUFFER_SIZE;
  115. static constexpr bool XONOFF = ENABLED(SERIAL_XON_XOFF);
  116. static constexpr bool EMERGENCYPARSER = ENABLED(EMERGENCY_PARSER);
  117. static constexpr bool DROPPED_RX = ENABLED(SERIAL_STATS_DROPPED_RX);
  118. static constexpr bool RX_OVERRUNS = ENABLED(SERIAL_STATS_RX_BUFFER_OVERRUNS);
  119. static constexpr bool RX_FRAMING_ERRORS = ENABLED(SERIAL_STATS_RX_FRAMING_ERRORS);
  120. static constexpr bool MAX_RX_QUEUED = ENABLED(SERIAL_STATS_MAX_RX_QUEUED);
  121. };
  122. #if SERIAL_PORT >= 0
  123. typedef Serial0Type< MarlinSerial< MarlinSerialCfg<SERIAL_PORT> > > MSerialT;
  124. extern MSerialT customizedSerial1;
  125. #endif
  126. #if defined(SERIAL_PORT_2) && SERIAL_PORT_2 >= 0
  127. typedef Serial0Type< MarlinSerial< MarlinSerialCfg<SERIAL_PORT_2> > > MSerialT2;
  128. extern MSerialT2 customizedSerial2;
  129. #endif