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.

HardwareSerial.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 HARDWARE_SERIAL_H_
  23. #define HARDWARE_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 <Stream.h>
  31. extern "C" {
  32. #include <lpc17xx_uart.h>
  33. #include "lpc17xx_pinsel.h"
  34. }
  35. class HardwareSerial : public Stream {
  36. private:
  37. LPC_UART_TypeDef *UARTx;
  38. uint32_t Baudrate;
  39. uint32_t Status;
  40. uint8_t RxBuffer[RX_BUFFER_SIZE];
  41. uint32_t RxQueueWritePos;
  42. uint32_t RxQueueReadPos;
  43. #if TX_BUFFER_SIZE > 0
  44. uint8_t TxBuffer[TX_BUFFER_SIZE];
  45. uint32_t TxQueueWritePos;
  46. uint32_t TxQueueReadPos;
  47. #endif
  48. #if ENABLED(EMERGENCY_PARSER)
  49. EmergencyParser::State emergency_state;
  50. #endif
  51. public:
  52. HardwareSerial(LPC_UART_TypeDef *UARTx)
  53. : UARTx(UARTx)
  54. , Baudrate(0)
  55. , RxQueueWritePos(0)
  56. , RxQueueReadPos(0)
  57. #if TX_BUFFER_SIZE > 0
  58. , TxQueueWritePos(0)
  59. , TxQueueReadPos(0)
  60. #endif
  61. #if ENABLED(EMERGENCY_PARSER)
  62. , emergency_state(EmergencyParser::State::EP_RESET)
  63. #endif
  64. {
  65. }
  66. void begin(uint32_t baudrate);
  67. int peek();
  68. int read();
  69. size_t write(uint8_t send);
  70. #if TX_BUFFER_SIZE > 0
  71. void flushTX();
  72. #endif
  73. int available();
  74. void flush();
  75. void printf(const char *format, ...);
  76. operator bool() { return true; }
  77. void IRQHandler();
  78. #define DEC 10
  79. #define HEX 16
  80. #define OCT 8
  81. #define BIN 2
  82. void print_bin(uint32_t value, uint8_t num_digits) {
  83. uint32_t mask = 1 << (num_digits -1);
  84. for (uint8_t i = 0; i < num_digits; i++) {
  85. if (!(i % 4) && i) printf(" ");
  86. if (!(i % 16) && i) printf(" ");
  87. if (value & mask) printf("1");
  88. else printf("0");
  89. value <<= 1;
  90. }
  91. }
  92. void print(const char value[]) {
  93. printf("%s" , value);
  94. }
  95. void print(char value, int nbase = 0) {
  96. if (nbase == BIN) print_bin(value,8);
  97. else if (nbase == OCT) printf("%3o", value);
  98. else if (nbase == HEX) printf("%2X", value);
  99. else if (nbase == DEC ) printf("%d", value);
  100. else printf("%c" , value);
  101. }
  102. void print(unsigned char value, int nbase = 0) {
  103. if (nbase == BIN) print_bin(value,8);
  104. else if (nbase == OCT) printf("%3o", value);
  105. else if (nbase == HEX) printf("%2X", value);
  106. else printf("%u" , value);
  107. }
  108. void print(int value, int nbase = 0) {
  109. if (nbase == BIN) print_bin(value,16);
  110. else if (nbase == OCT) printf("%6o", value);
  111. else if (nbase == HEX) printf("%4X", value);
  112. else printf("%d", value);
  113. }
  114. void print(unsigned int value, int nbase = 0) {
  115. if (nbase == BIN) print_bin(value,16);
  116. else if (nbase == OCT) printf("%6o", value);
  117. else if (nbase == HEX) printf("%4X", value);
  118. else printf("%u" , value);
  119. }
  120. void print(long value, int nbase = 0) {
  121. if (nbase == BIN) print_bin(value,32);
  122. else if (nbase == OCT) printf("%11o", value);
  123. else if (nbase == HEX) printf("%8X", value);
  124. else printf("%ld" , value);
  125. }
  126. void print(unsigned long value, int nbase = 0) {
  127. if (nbase == BIN) print_bin(value,32);
  128. else if (nbase == OCT) printf("%11o", value);
  129. else if (nbase == HEX) printf("%8X", value);
  130. else printf("%lu" , value);
  131. }
  132. void print(float value, int round = 6) {
  133. printf("%f" , value);
  134. }
  135. void print(double value, int round = 6) {
  136. printf("%f" , value );
  137. }
  138. void println(const char value[]) {
  139. printf("%s\n" , value);
  140. }
  141. void println(char value, int nbase = 0) {
  142. print(value, nbase);
  143. println();
  144. }
  145. void println(unsigned char value, int nbase = 0) {
  146. print(value, nbase);
  147. println();
  148. }
  149. void println(int value, int nbase = 0) {
  150. print(value, nbase);
  151. println();
  152. }
  153. void println(unsigned int value, int nbase = 0) {
  154. print(value, nbase);
  155. println();
  156. }
  157. void println(long value, int nbase = 0) {
  158. print(value, nbase);
  159. println();
  160. }
  161. void println(unsigned long value, int nbase = 0) {
  162. print(value, nbase);
  163. println();
  164. }
  165. void println(float value, int round = 6) {
  166. printf("%f\n" , value );
  167. }
  168. void println(double value, int round = 6) {
  169. printf("%f\n" , value );
  170. }
  171. void println(void) {
  172. print('\n');
  173. }
  174. };
  175. #endif // MARLIN_SRC_HAL_HAL_SERIAL_H_