Teensy 4.1 core updated for C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

usb_serial.h 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "usb_desc.h"
  33. #if (defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)) || defined(USB_DISABLED)
  34. #include <inttypes.h>
  35. #if F_CPU >= 20000000 && !defined(USB_DISABLED)
  36. // C language implementation
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. int usb_serial_getchar(void);
  41. int usb_serial_peekchar(void);
  42. int usb_serial_available(void);
  43. int usb_serial_read(void *buffer, uint32_t size);
  44. void usb_serial_flush_input(void);
  45. int usb_serial_putchar(uint8_t c);
  46. int usb_serial_write(const void *buffer, uint32_t size);
  47. int usb_serial_write_buffer_free(void);
  48. void usb_serial_flush_output(void);
  49. extern uint32_t usb_cdc_line_coding[2];
  50. extern volatile uint32_t usb_cdc_line_rtsdtr_millis;
  51. extern volatile uint32_t systick_millis_count;
  52. extern volatile uint8_t usb_cdc_line_rtsdtr;
  53. extern volatile uint8_t usb_cdc_transmit_flush_timer;
  54. extern volatile uint8_t usb_configuration;
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. #define USB_SERIAL_DTR 0x01
  59. #define USB_SERIAL_RTS 0x02
  60. // C++ interface
  61. #ifdef __cplusplus
  62. #include "Stream.h"
  63. class usb_serial_class : public Stream
  64. {
  65. public:
  66. void begin(long) {
  67. uint32_t millis_begin = systick_millis_count;
  68. while (!(*this)) {
  69. // wait up to 1 second for Arduino Serial Monitor
  70. if ((uint32_t)(systick_millis_count - millis_begin) > 1000) break;
  71. }
  72. }
  73. void end() { /* TODO: flush output and shut down USB port */ };
  74. virtual int available() { return usb_serial_available(); }
  75. virtual int read() { return usb_serial_getchar(); }
  76. virtual int peek() { return usb_serial_peekchar(); }
  77. virtual void flush() { usb_serial_flush_output(); } // TODO: actually wait for data to leave USB...
  78. virtual void clear(void) { usb_serial_flush_input(); }
  79. virtual size_t write(uint8_t c) { return usb_serial_putchar(c); }
  80. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial_write(buffer, size); }
  81. size_t write(unsigned long n) { return write((uint8_t)n); }
  82. size_t write(long n) { return write((uint8_t)n); }
  83. size_t write(unsigned int n) { return write((uint8_t)n); }
  84. size_t write(int n) { return write((uint8_t)n); }
  85. virtual int availableForWrite() { return usb_serial_write_buffer_free(); }
  86. using Print::write;
  87. void send_now(void) { usb_serial_flush_output(); }
  88. uint32_t baud(void) { return usb_cdc_line_coding[0]; }
  89. uint8_t stopbits(void) { uint8_t b = usb_cdc_line_coding[1]; if (!b) b = 1; return b; }
  90. uint8_t paritytype(void) { return usb_cdc_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
  91. uint8_t numbits(void) { return usb_cdc_line_coding[1] >> 16; }
  92. uint8_t dtr(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
  93. uint8_t rts(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
  94. operator bool() { return usb_configuration &&
  95. (usb_cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS)) &&
  96. ((uint32_t)(systick_millis_count - usb_cdc_line_rtsdtr_millis) >= 25);
  97. }
  98. size_t readBytes(char *buffer, size_t length) {
  99. size_t count=0;
  100. unsigned long startMillis = millis();
  101. do {
  102. count += usb_serial_read(buffer + count, length - count);
  103. if (count >= length) return count;
  104. } while(millis() - startMillis < _timeout);
  105. setReadError();
  106. return count;
  107. }
  108. };
  109. extern usb_serial_class Serial;
  110. extern void serialEvent(void);
  111. #endif // __cplusplus
  112. #else // F_CPU < 20000000
  113. // Allow Arduino programs using Serial to compile, but Serial will do nothing.
  114. #ifdef __cplusplus
  115. #include "Stream.h"
  116. class usb_serial_class : public Stream
  117. {
  118. public:
  119. void begin(long) { };
  120. void end() { };
  121. virtual int available() { return 0; }
  122. virtual int read() { return -1; }
  123. virtual int peek() { return -1; }
  124. virtual void flush() { }
  125. virtual void clear() { }
  126. virtual size_t write(uint8_t c) { return 1; }
  127. virtual size_t write(const uint8_t *buffer, size_t size) { return size; }
  128. size_t write(unsigned long n) { return 1; }
  129. size_t write(long n) { return 1; }
  130. size_t write(unsigned int n) { return 1; }
  131. size_t write(int n) { return 1; }
  132. int availableForWrite() { return 0; }
  133. using Print::write;
  134. void send_now(void) { }
  135. uint32_t baud(void) { return 0; }
  136. uint8_t stopbits(void) { return 1; }
  137. uint8_t paritytype(void) { return 0; }
  138. uint8_t numbits(void) { return 8; }
  139. uint8_t dtr(void) { return 1; }
  140. uint8_t rts(void) { return 1; }
  141. operator bool() { return true; }
  142. };
  143. extern usb_serial_class Serial;
  144. extern void serialEvent(void);
  145. #endif // __cplusplus
  146. #endif // F_CPU
  147. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE
  148. #endif // USBserial_h_