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

128 行
5.1KB

  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. #pragma once
  31. #include "usb_desc.h"
  32. #if (defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)) || defined(USB_DISABLED)
  33. #include <stdint.h>
  34. // C language implementation
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. int usb_serial_getchar(void);
  39. int usb_serial_peekchar(void);
  40. int usb_serial_available(void);
  41. int usb_serial_read(void *buffer, uint32_t size);
  42. void usb_serial_flush_input(void);
  43. int usb_serial_putchar(uint8_t c);
  44. int usb_serial_write(const void *buffer, uint32_t size);
  45. int usb_serial_write_buffer_free(void);
  46. void usb_serial_flush_output(void);
  47. extern uint32_t usb_cdc_line_coding[2];
  48. extern volatile uint32_t usb_cdc_line_rtsdtr_millis;
  49. extern volatile uint32_t systick_millis_count;
  50. extern volatile uint8_t usb_cdc_line_rtsdtr;
  51. extern volatile uint8_t usb_cdc_transmit_flush_timer;
  52. extern volatile uint8_t usb_configuration;
  53. #ifdef __cplusplus
  54. }
  55. #endif
  56. #define USB_SERIAL_DTR 0x01
  57. #define USB_SERIAL_RTS 0x02
  58. // C++ interface
  59. #ifdef __cplusplus
  60. #include "Stream.h"
  61. class usb_serial_class : public Stream
  62. {
  63. public:
  64. constexpr usb_serial_class() {}
  65. void begin(long) {
  66. //uint32_t millis_begin = systick_millis_count;
  67. //disabled for now - causes more trouble than it solves?
  68. //while (!(*this)) {
  69. // wait up to 2.5 seconds for Arduino Serial Monitor
  70. // Yes, this is a long time, but some Windows systems open
  71. // the port very slowly. This wait allows programs for
  72. // Arduino Uno to "just work" (without forcing a reboot when
  73. // the port is opened), and when no PC is connected the user's
  74. // sketch still gets to run normally after this wait time.
  75. //if ((uint32_t)(systick_millis_count - millis_begin) > 2500) break;
  76. //}
  77. }
  78. void end() { /* TODO: flush output and shut down USB port */ };
  79. virtual int available() { return usb_serial_available(); }
  80. virtual int read() { return usb_serial_getchar(); }
  81. virtual int peek() { return usb_serial_peekchar(); }
  82. virtual void flush() { usb_serial_flush_output(); } // TODO: actually wait for data to leave USB...
  83. virtual void clear(void) { usb_serial_flush_input(); }
  84. virtual size_t write(uint8_t c) { return usb_serial_putchar(c); }
  85. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial_write(buffer, size); }
  86. size_t write(unsigned long n) { return write((uint8_t)n); }
  87. size_t write(long n) { return write((uint8_t)n); }
  88. size_t write(unsigned int n) { return write((uint8_t)n); }
  89. size_t write(int n) { return write((uint8_t)n); }
  90. virtual int availableForWrite() { return usb_serial_write_buffer_free(); }
  91. using Print::write;
  92. void send_now(void) { usb_serial_flush_output(); }
  93. uint32_t baud(void) { return usb_cdc_line_coding[0]; }
  94. uint8_t stopbits(void) { uint8_t b = usb_cdc_line_coding[1]; if (!b) b = 1; return b; }
  95. uint8_t paritytype(void) { return usb_cdc_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
  96. uint8_t numbits(void) { return usb_cdc_line_coding[1] >> 16; }
  97. uint8_t dtr(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
  98. uint8_t rts(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
  99. operator bool() { return usb_configuration && (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) &&
  100. ((uint32_t)(systick_millis_count - usb_cdc_line_rtsdtr_millis) >= 15);
  101. }
  102. size_t readBytes(char *buffer, size_t length) {
  103. size_t count=0;
  104. unsigned long startMillis = millis();
  105. do {
  106. count += usb_serial_read(buffer + count, length - count);
  107. if (count >= length) return count;
  108. } while(millis() - startMillis < _timeout);
  109. setReadError();
  110. return count;
  111. }
  112. };
  113. extern usb_serial_class Serial;
  114. extern void serialEvent(void);
  115. #endif // __cplusplus
  116. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE