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.

UHS_printf_HELPER.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (C) 2015-2016 Andrew J. Kroll
  2. and
  3. Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Contact information
  16. -------------------
  17. Circuits At Home, LTD
  18. Web : https://www.circuitsathome.com
  19. e-mail : support@circuitsathome.com
  20. */
  21. #ifndef UHS_PRINTF_HELPER_H
  22. #define UHS_PRINTF_HELPER_H
  23. #ifdef LOAD_UHS_PRINTF_HELPER
  24. #include <Arduino.h>
  25. #ifdef true
  26. #undef true
  27. #endif
  28. #ifdef false
  29. #undef false
  30. #endif
  31. #ifndef STDIO_IS_OK_TO_USE_AS_IS
  32. #if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAM_DUE) || defined(ARDUINO_spresense_ast)
  33. // STDIO patching not required.
  34. #define STDIO_IS_OK_TO_USE_AS_IS
  35. #endif
  36. #endif
  37. #ifndef STDIO_IS_OK_TO_USE_AS_IS
  38. // We need to patch STDIO so it can be used.
  39. #ifndef SERIAL_PORT_MONITOR
  40. // Some don't define this.
  41. #define SERIAL_PORT_MONITOR Serial
  42. #endif
  43. #ifndef SERIAL_PORT_HARDWARE
  44. // Some don't define this.
  45. #define SERIAL_PORT_HARDWARE SERIAL_PORT_MONITOR
  46. #endif
  47. #ifndef USB_HOST_SERIAL
  48. #if defined(SERIAL_PORT_USBVIRTUAL) && defined(LOAD_UHS_KINETIS_FS_HOST)
  49. #define USB_HOST_SERIAL SERIAL_PORT_HARDWARE
  50. #else
  51. #define USB_HOST_SERIAL SERIAL_PORT_MONITOR
  52. #endif
  53. #endif
  54. #ifndef NOTUSED
  55. #define NOTUSED(...) __VA_ARGS__ __attribute__((unused))
  56. #endif
  57. #ifndef __AVR__
  58. #ifndef printf_P
  59. #define printf_P(...) printf(__VA_ARGS__)
  60. #endif
  61. #endif
  62. #ifdef ARDUINO_ARCH_PIC32
  63. /*
  64. * For printf() output with pic32 Arduino
  65. */
  66. extern "C" {
  67. void _mon_putc(char s) {
  68. USB_HOST_SERIAL.write(s);
  69. }
  70. int _mon_getc() {
  71. while(!USB_HOST_SERIAL.available());
  72. return USB_HOST_SERIAL.read();
  73. }
  74. }
  75. #elif defined(__AVR__)
  76. extern "C" {
  77. static FILE tty_stdio;
  78. static FILE tty_stderr;
  79. static int NOTUSED(tty_stderr_putc(char c, NOTUSED(FILE *t)));
  80. static int NOTUSED(tty_stderr_flush(NOTUSED(FILE *t)));
  81. static int NOTUSED(tty_std_putc(char c, NOTUSED(FILE *t)));
  82. static int NOTUSED(tty_std_getc(NOTUSED(FILE *t)));
  83. static int NOTUSED(tty_std_flush(NOTUSED(FILE *t)));
  84. static int tty_stderr_putc(char c, NOTUSED(FILE *t)) {
  85. USB_HOST_SERIAL.write(c);
  86. return 0;
  87. }
  88. static int tty_stderr_flush(NOTUSED(FILE *t)) {
  89. USB_HOST_SERIAL.flush();
  90. return 0;
  91. }
  92. static int tty_std_putc(char c, NOTUSED(FILE *t)) {
  93. USB_HOST_SERIAL.write(c);
  94. return 0;
  95. }
  96. static int tty_std_getc(NOTUSED(FILE *t)) {
  97. while(!USB_HOST_SERIAL.available());
  98. return USB_HOST_SERIAL.read();
  99. }
  100. static int tty_std_flush(NOTUSED(FILE *t)) {
  101. USB_HOST_SERIAL.flush();
  102. return 0;
  103. }
  104. }
  105. #elif defined(CORE_TEENSY)
  106. extern "C" {
  107. int _write(int fd, const char *ptr, int len) {
  108. int j;
  109. for(j = 0; j < len; j++) {
  110. if(fd == 1)
  111. USB_HOST_SERIAL.write(*ptr++);
  112. else if(fd == 2)
  113. USB_HOST_SERIAL.write(*ptr++);
  114. }
  115. return len;
  116. }
  117. int _read(int fd, char *ptr, int len) {
  118. if(len > 0 && fd == 0) {
  119. while(!USB_HOST_SERIAL.available());
  120. *ptr = USB_HOST_SERIAL.read();
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. #include <sys/stat.h>
  126. int _fstat(int fd, struct stat *st) {
  127. memset(st, 0, sizeof (*st));
  128. st->st_mode = S_IFCHR;
  129. st->st_blksize = 1024;
  130. return 0;
  131. }
  132. int _isatty(int fd) {
  133. return (fd < 3) ? 1 : 0;
  134. }
  135. }
  136. #else
  137. #error no STDIO
  138. #endif // defined(ARDUINO_ARCH_PIC32)
  139. #ifdef __AVR__
  140. // The only wierdo in the bunch...
  141. void UHS_AVR_printf_HELPER_init() {
  142. // Set up stdio/stderr
  143. tty_stdio.put = tty_std_putc;
  144. tty_stdio.get = tty_std_getc;
  145. tty_stdio.flags = _FDEV_SETUP_RW;
  146. tty_stdio.udata = 0;
  147. tty_stderr.put = tty_stderr_putc;
  148. tty_stderr.get = NULL;
  149. tty_stderr.flags = _FDEV_SETUP_WRITE;
  150. tty_stderr.udata = 0;
  151. stdout = &tty_stdio;
  152. stdin = &tty_stdio;
  153. stderr = &tty_stderr;
  154. }
  155. #define UHS_printf_HELPER_init() UHS_AVR_printf_HELPER_init()
  156. #endif
  157. #endif /* STDIO_IS_OK_TO_USE_AS_IS */
  158. #endif /* load.... */
  159. #ifndef UHS_printf_HELPER_init
  160. #define UHS_printf_HELPER_init() (void(0))
  161. #endif
  162. #endif /* UHS_PRINTF_HELPER_H */