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.

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