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.

150 lines
5.5KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef USBserial_h_
  31. #define USBserial_h_
  32. #if defined(USB_SERIAL) || defined(USB_SERIAL_HID)
  33. #include <inttypes.h>
  34. #if F_CPU >= 20000000
  35. // C language implementation
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. int usb_serial_getchar(void);
  40. int usb_serial_peekchar(void);
  41. int usb_serial_available(void);
  42. int usb_serial_read(void *buffer, uint32_t size);
  43. void usb_serial_flush_input(void);
  44. int usb_serial_putchar(uint8_t c);
  45. int usb_serial_write(const void *buffer, uint32_t size);
  46. int usb_serial_write_buffer_free(void);
  47. void usb_serial_flush_output(void);
  48. extern uint32_t usb_cdc_line_coding[2];
  49. extern volatile uint8_t usb_cdc_line_rtsdtr;
  50. extern volatile uint8_t usb_cdc_transmit_flush_timer;
  51. extern volatile uint8_t usb_configuration;
  52. #ifdef __cplusplus
  53. }
  54. #endif
  55. #define USB_SERIAL_DTR 0x01
  56. #define USB_SERIAL_RTS 0x02
  57. // C++ interface
  58. #ifdef __cplusplus
  59. #include "Stream.h"
  60. class usb_serial_class : public Stream
  61. {
  62. public:
  63. void begin(long) { /* TODO: call a function that tries to wait for enumeration */ };
  64. void end() { /* TODO: flush output and shut down USB port */ };
  65. virtual int available() { return usb_serial_available(); }
  66. virtual int read() { return usb_serial_getchar(); }
  67. virtual int peek() { return usb_serial_peekchar(); }
  68. virtual void flush() { usb_serial_flush_output(); } // TODO: actually wait for data to leave USB...
  69. virtual size_t write(uint8_t c) { return usb_serial_putchar(c); }
  70. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial_write(buffer, size); }
  71. size_t write(unsigned long n) { return write((uint8_t)n); }
  72. size_t write(long n) { return write((uint8_t)n); }
  73. size_t write(unsigned int n) { return write((uint8_t)n); }
  74. size_t write(int n) { return write((uint8_t)n); }
  75. int availableForWrite() { return usb_serial_write_buffer_free(); }
  76. using Print::write;
  77. void send_now(void) { usb_serial_flush_output(); }
  78. uint32_t baud(void) { return usb_cdc_line_coding[0]; }
  79. uint8_t stopbits(void) { uint8_t b = usb_cdc_line_coding[1]; if (!b) b = 1; return b; }
  80. uint8_t paritytype(void) { return usb_cdc_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
  81. uint8_t numbits(void) { return usb_cdc_line_coding[1] >> 16; }
  82. uint8_t dtr(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
  83. uint8_t rts(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
  84. operator bool() { return usb_configuration && (usb_cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS)); }
  85. size_t readBytes(char *buffer, size_t length) {
  86. size_t count=0;
  87. unsigned long startMillis = millis();
  88. do {
  89. count += usb_serial_read(buffer + count, length - count);
  90. if (count >= length) return count;
  91. } while(millis() - startMillis < _timeout);
  92. setReadError();
  93. return count;
  94. }
  95. };
  96. extern usb_serial_class Serial;
  97. extern void serialEvent(void);
  98. #endif // __cplusplus
  99. #else // F_CPU < 20 MHz
  100. // Allow Arduino programs using Serial to compile, but Serial will do nothing.
  101. #ifdef __cplusplus
  102. #include "Stream.h"
  103. class usb_serial_class : public Stream
  104. {
  105. public:
  106. void begin(long) { };
  107. void end() { };
  108. virtual int available() { return 0; }
  109. virtual int read() { return -1; }
  110. virtual int peek() { return -1; }
  111. virtual void flush() { }
  112. virtual size_t write(uint8_t c) { return 1; }
  113. virtual size_t write(const uint8_t *buffer, size_t size) { return size; }
  114. size_t write(unsigned long n) { return 1; }
  115. size_t write(long n) { return 1; }
  116. size_t write(unsigned int n) { return 1; }
  117. size_t write(int n) { return 1; }
  118. int availableForWrite() { return 0; }
  119. using Print::write;
  120. void send_now(void) { }
  121. uint32_t baud(void) { return 0; }
  122. uint8_t stopbits(void) { return 1; }
  123. uint8_t paritytype(void) { return 0; }
  124. uint8_t numbits(void) { return 8; }
  125. uint8_t dtr(void) { return 1; }
  126. uint8_t rts(void) { return 1; }
  127. operator bool() { return true; }
  128. };
  129. extern usb_serial_class Serial;
  130. extern void serialEvent(void);
  131. #endif // __cplusplus
  132. #endif // F_CPU
  133. #endif // USB_SERIAL || USB_SERIAL_HID
  134. #endif // USBserial_h_