Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

140 lines
5.4KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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 USBserial2_h_
  31. #define USBserial2_h_
  32. #include "usb_desc.h"
  33. #if defined(CDC2_STATUS_INTERFACE) && defined(CDC2_DATA_INTERFACE)
  34. #include <inttypes.h>
  35. #if F_CPU >= 20000000
  36. #include "core_pins.h" // for millis()
  37. // C language implementation
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. int usb_serial2_getchar(void);
  42. int usb_serial2_peekchar(void);
  43. int usb_serial2_available(void);
  44. int usb_serial2_read(void *buffer, uint32_t size);
  45. void usb_serial2_flush_input(void);
  46. int usb_serial2_putchar(uint8_t c);
  47. int usb_serial2_write(const void *buffer, uint32_t size);
  48. int usb_serial2_write_buffer_free(void);
  49. void usb_serial2_flush_output(void);
  50. void usb_serial2_flush_callback(void);
  51. extern uint32_t usb_cdc2_line_coding[2];
  52. extern volatile uint32_t usb_cdc2_line_rtsdtr_millis;
  53. extern volatile uint32_t systick_millis_count;
  54. extern volatile uint8_t usb_cdc2_line_rtsdtr;
  55. extern volatile uint8_t usb_cdc2_transmit_flush_timer;
  56. extern volatile uint8_t usb_configuration;
  57. #ifdef __cplusplus
  58. }
  59. #endif
  60. #ifndef USB_SERIAL_DTR
  61. #define USB_SERIAL_DTR 0x01
  62. #endif
  63. #ifndef USB_SERIAL_RTS
  64. #define USB_SERIAL_RTS 0x02
  65. #endif
  66. // C++ interface
  67. #ifdef __cplusplus
  68. #include "Stream.h"
  69. class usb_serial2_class : public Stream
  70. {
  71. public:
  72. constexpr usb_serial2_class() {}
  73. void begin(long) {
  74. //uint32_t millis_begin = systick_millis_count;
  75. //disabled for now - causes more trouble than it solves?
  76. //while (!(*this)) {
  77. // wait up to 2.5 seconds for Arduino Serial Monitor
  78. // Yes, this is a long time, but some Windows systems open
  79. // the port very slowly. This wait allows programs for
  80. // Arduino Uno to "just work" (without forcing a reboot when
  81. // the port is opened), and when no PC is connected the user's
  82. // sketch still gets to run normally after this wait time.
  83. //if ((uint32_t)(systick_millis_count - millis_begin) > 2500) break;
  84. //}
  85. }
  86. void end() { /* TODO: flush output and shut down USB port */ };
  87. virtual int available() { return usb_serial2_available(); }
  88. virtual int read() { return usb_serial2_getchar(); }
  89. virtual int peek() { return usb_serial2_peekchar(); }
  90. virtual void flush() { usb_serial2_flush_output(); } // TODO: actually wait for data to leave USB...
  91. virtual void clear(void) { usb_serial2_flush_input(); }
  92. virtual size_t write(uint8_t c) { return usb_serial2_putchar(c); }
  93. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial2_write(buffer, size); }
  94. size_t write(unsigned long n) { return write((uint8_t)n); }
  95. size_t write(long n) { return write((uint8_t)n); }
  96. size_t write(unsigned int n) { return write((uint8_t)n); }
  97. size_t write(int n) { return write((uint8_t)n); }
  98. virtual int availableForWrite() { return usb_serial2_write_buffer_free(); }
  99. using Print::write;
  100. void send_now(void) { usb_serial2_flush_output(); }
  101. uint32_t baud(void) { return usb_cdc2_line_coding[0]; }
  102. uint8_t stopbits(void) { uint8_t b = usb_cdc2_line_coding[1]; if (!b) b = 1; return b; }
  103. uint8_t paritytype(void) { return usb_cdc2_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
  104. uint8_t numbits(void) { return usb_cdc2_line_coding[1] >> 16; }
  105. uint8_t dtr(void) { return (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
  106. uint8_t rts(void) { return (usb_cdc2_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
  107. operator bool() { return usb_configuration && (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR) &&
  108. ((uint32_t)(systick_millis_count - usb_cdc2_line_rtsdtr_millis) >= 15);
  109. }
  110. size_t readBytes(char *buffer, size_t length) {
  111. size_t count=0;
  112. unsigned long startMillis = millis();
  113. do {
  114. count += usb_serial2_read(buffer + count, length - count);
  115. if (count >= length) return count;
  116. } while(millis() - startMillis < _timeout);
  117. setReadError();
  118. return count;
  119. }
  120. };
  121. extern usb_serial2_class SerialUSB1;
  122. extern void serialEventUSB1(void);
  123. #endif // __cplusplus
  124. #endif // F_CPU
  125. #endif // CDC2_STATUS_INTERFACE && CDC2_DATA_INTERFACE
  126. #endif // USBserial2_h_