Teensy 4.1 core updated for C++20
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.

225 lines
7.3KB

  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. #include <inttypes.h>
  31. #include "usb_serial_port.h"
  32. #if F_CPU >= 20000000 && !defined(USB_DISABLED)
  33. #include "core_pins.h" // for millis()
  34. // C language implementation
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. extern struct usb_serial_port usb_serial_instance;
  39. static inline int usb_serial_getchar(void)
  40. {
  41. return __usb_serial_getchar(&usb_serial_instance);
  42. }
  43. static inline int usb_serial_peekchar(void)
  44. {
  45. return __usb_serial_peekchar(&usb_serial_instance);
  46. }
  47. static inline int usb_serial_available(void)
  48. {
  49. return __usb_serial_available(&usb_serial_instance);
  50. }
  51. static inline int usb_serial_read(void *buffer, uint32_t size)
  52. {
  53. return __usb_serial_read(&usb_serial_instance, buffer, size);
  54. }
  55. static inline void usb_serial_flush_input(void)
  56. {
  57. __usb_serial_flush_input(&usb_serial_instance);
  58. }
  59. static inline int usb_serial_putchar(uint8_t c)
  60. {
  61. return __usb_serial_putchar(&usb_serial_instance, c);
  62. }
  63. static inline int usb_serial_write(const void *buffer, uint32_t size)
  64. {
  65. return __usb_serial_write(&usb_serial_instance, buffer, size);
  66. }
  67. static inline int usb_serial_write_buffer_free(void)
  68. {
  69. return __usb_serial_write_buffer_free(&usb_serial_instance);
  70. }
  71. static inline void usb_serial_flush_output(void)
  72. {
  73. __usb_serial_flush_output(&usb_serial_instance);
  74. }
  75. static inline void usb_serial_flush_callback(void)
  76. {
  77. __usb_serial_flush_callback(&usb_serial_instance);
  78. }
  79. #define usb_cdc_line_coding usb_serial_instance.cdc_line_coding
  80. #define usb_cdc_line_rtsdtr_millis usb_serial_instance.cdc_line_rtsdtr_millis
  81. #define usb_cdc_line_rtsdtr usb_serial_instance.cdc_line_rtsdtr
  82. #define usb_cdc_transmit_flush_timer usb_serial_instance.cdc_transmit_flush_timer
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. #define USB_SERIAL_DTR 0x01
  87. #define USB_SERIAL_RTS 0x02
  88. // C++ interface
  89. #ifdef __cplusplus
  90. #include "Stream.h"
  91. class usb_serial_class : public Stream
  92. {
  93. public:
  94. constexpr usb_serial_class() {}
  95. void begin(long) {
  96. //uint32_t millis_begin = systick_millis_count;
  97. //disabled for now - causes more trouble than it solves?
  98. //while (!(*this)) {
  99. // wait up to 2.5 seconds for Arduino Serial Monitor
  100. // Yes, this is a long time, but some Windows systems open
  101. // the port very slowly. This wait allows programs for
  102. // Arduino Uno to "just work" (without forcing a reboot when
  103. // the port is opened), and when no PC is connected the user's
  104. // sketch still gets to run normally after this wait time.
  105. //if ((uint32_t)(systick_millis_count - millis_begin) > 2500) break;
  106. //}
  107. }
  108. void end() { /* TODO: flush output and shut down USB port */ };
  109. virtual int available() { return usb_serial_available(); }
  110. virtual int read() { return usb_serial_getchar(); }
  111. virtual int peek() { return usb_serial_peekchar(); }
  112. virtual void flush() { usb_serial_flush_output(); } // TODO: actually wait for data to leave USB...
  113. virtual void clear(void) { usb_serial_flush_input(); }
  114. virtual size_t write(uint8_t c) { return usb_serial_putchar(c); }
  115. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial_write(buffer, size); }
  116. size_t write(unsigned long n) { return write((uint8_t)n); }
  117. size_t write(long n) { return write((uint8_t)n); }
  118. size_t write(unsigned int n) { return write((uint8_t)n); }
  119. size_t write(int n) { return write((uint8_t)n); }
  120. virtual int availableForWrite() { return usb_serial_write_buffer_free(); }
  121. using Print::write;
  122. void send_now(void) { usb_serial_flush_output(); }
  123. uint32_t baud(void) { return usb_serial_instance.cdc_line_coding[0]; }
  124. uint8_t stopbits(void) {
  125. uint8_t b = usb_serial_instance.cdc_line_coding[1];
  126. if (!b) b = 1;
  127. return b;
  128. }
  129. uint8_t paritytype(void) {
  130. // 0=none, 1=odd, 2=even
  131. return usb_serial_instance.cdc_line_coding[1] >> 8;
  132. }
  133. uint8_t numbits(void) {
  134. return usb_serial_instance.cdc_line_coding[1] >> 16;
  135. }
  136. uint8_t dtr(void) {
  137. return (usb_serial_instance.cdc_line_rtsdtr & USB_SERIAL_DTR) ?
  138. 1 : 0;
  139. }
  140. uint8_t rts(void) {
  141. return (usb_serial_instance.cdc_line_rtsdtr & USB_SERIAL_RTS) ?
  142. 1 : 0;
  143. }
  144. operator bool() {
  145. return usb_configuration &&
  146. (usb_serial_instance.cdc_line_rtsdtr & USB_SERIAL_DTR) &&
  147. ((uint32_t)(systick_millis_count - usb_serial_instance.cdc_line_rtsdtr_millis) >= 15);
  148. }
  149. size_t readBytes(char *buffer, size_t length) {
  150. size_t count=0;
  151. unsigned long startMillis = millis();
  152. do {
  153. count += usb_serial_read(buffer + count, length - count);
  154. if (count >= length) return count;
  155. } while(millis() - startMillis < _timeout);
  156. setReadError();
  157. return count;
  158. }
  159. };
  160. extern usb_serial_class Serial;
  161. extern void serialEvent(void);
  162. #endif // __cplusplus
  163. #else // F_CPU < 20000000
  164. // Allow Arduino programs using Serial to compile, but Serial will do nothing.
  165. #ifdef __cplusplus
  166. #include "Stream.h"
  167. class usb_serial_class : public Stream
  168. {
  169. public:
  170. constexpr usb_serial_class() {}
  171. void begin(long) { };
  172. void end() { };
  173. virtual int available() { return 0; }
  174. virtual int read() { return -1; }
  175. virtual int peek() { return -1; }
  176. virtual void flush() { }
  177. virtual void clear() { }
  178. virtual size_t write(uint8_t c) { return 1; }
  179. virtual size_t write(const uint8_t *buffer, size_t size) { return size; }
  180. size_t write(unsigned long n) { return 1; }
  181. size_t write(long n) { return 1; }
  182. size_t write(unsigned int n) { return 1; }
  183. size_t write(int n) { return 1; }
  184. int availableForWrite() { return 0; }
  185. using Print::write;
  186. void send_now(void) { }
  187. uint32_t baud(void) { return 0; }
  188. uint8_t stopbits(void) { return 1; }
  189. uint8_t paritytype(void) { return 0; }
  190. uint8_t numbits(void) { return 8; }
  191. uint8_t dtr(void) { return 1; }
  192. uint8_t rts(void) { return 1; }
  193. operator bool() { return true; }
  194. };
  195. extern usb_serial_class Serial;
  196. extern void serialEvent(void);
  197. #endif // __cplusplus
  198. #endif // F_CPU