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.

309 satır
13KB

  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. #include <stdint.h>
  33. #if (defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)) || defined(USB_DISABLED)
  34. // C language implementation
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. void usb_serial_reset(void);
  39. void usb_serial_configure(void);
  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. constexpr usb_serial_class() {}
  67. void begin(long) {
  68. //uint32_t millis_begin = systick_millis_count;
  69. //disabled for now - causes more trouble than it solves?
  70. //while (!(*this)) {
  71. // wait up to 2.5 seconds for Arduino Serial Monitor
  72. // Yes, this is a long time, but some Windows systems open
  73. // the port very slowly. This wait allows programs for
  74. // Arduino Uno to "just work" (without forcing a reboot when
  75. // the port is opened), and when no PC is connected the user's
  76. // sketch still gets to run normally after this wait time.
  77. //if ((uint32_t)(systick_millis_count - millis_begin) > 2500) break;
  78. //}
  79. }
  80. void end() { /* TODO: flush output and shut down USB port */ };
  81. virtual int available() { return usb_serial_available(); }
  82. virtual int read() { return usb_serial_getchar(); }
  83. virtual int peek() { return usb_serial_peekchar(); }
  84. virtual void flush() { usb_serial_flush_output(); } // TODO: actually wait for data to leave USB...
  85. virtual void clear(void) { usb_serial_flush_input(); }
  86. virtual size_t write(uint8_t c) { return usb_serial_putchar(c); }
  87. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial_write(buffer, size); }
  88. size_t write(unsigned long n) { return write((uint8_t)n); }
  89. size_t write(long n) { return write((uint8_t)n); }
  90. size_t write(unsigned int n) { return write((uint8_t)n); }
  91. size_t write(int n) { return write((uint8_t)n); }
  92. virtual int availableForWrite() { return usb_serial_write_buffer_free(); }
  93. using Print::write;
  94. void send_now(void) { usb_serial_flush_output(); }
  95. uint32_t baud(void) { return usb_cdc_line_coding[0]; }
  96. uint8_t stopbits(void) { uint8_t b = usb_cdc_line_coding[1]; if (!b) b = 1; return b; }
  97. uint8_t paritytype(void) { return usb_cdc_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
  98. uint8_t numbits(void) { return usb_cdc_line_coding[1] >> 16; }
  99. uint8_t dtr(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
  100. uint8_t rts(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
  101. operator bool() { return usb_configuration && (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) &&
  102. ((uint32_t)(systick_millis_count - usb_cdc_line_rtsdtr_millis) >= 15);
  103. }
  104. size_t readBytes(char *buffer, size_t length) {
  105. size_t count=0;
  106. unsigned long startMillis = millis();
  107. do {
  108. count += usb_serial_read(buffer + count, length - count);
  109. if (count >= length) return count;
  110. } while(millis() - startMillis < _timeout);
  111. setReadError();
  112. return count;
  113. }
  114. };
  115. extern usb_serial_class Serial;
  116. extern void serialEvent(void);
  117. #endif // __cplusplus
  118. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE
  119. #if defined(CDC2_STATUS_INTERFACE) && defined(CDC2_DATA_INTERFACE)
  120. // C language implementation
  121. #ifdef __cplusplus
  122. extern "C" {
  123. #endif
  124. void usb_serial2_configure(void);
  125. int usb_serial2_getchar(void);
  126. int usb_serial2_peekchar(void);
  127. int usb_serial2_available(void);
  128. int usb_serial2_read(void *buffer, uint32_t size);
  129. void usb_serial2_flush_input(void);
  130. int usb_serial2_putchar(uint8_t c);
  131. int usb_serial2_write(const void *buffer, uint32_t size);
  132. int usb_serial2_write_buffer_free(void);
  133. void usb_serial2_flush_output(void);
  134. extern uint32_t usb_cdc2_line_coding[2];
  135. extern volatile uint32_t usb_cdc2_line_rtsdtr_millis;
  136. extern volatile uint8_t usb_cdc2_line_rtsdtr;
  137. extern volatile uint8_t usb_cdc2_transmit_flush_timer;
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. // C++ interface
  142. #ifdef __cplusplus
  143. #include "Stream.h"
  144. class usb_serial2_class : public Stream
  145. {
  146. public:
  147. constexpr usb_serial2_class() {}
  148. void begin(long) {
  149. //uint32_t millis_begin = systick_millis_count;
  150. //disabled for now - causes more trouble than it solves?
  151. //while (!(*this)) {
  152. // wait up to 2.5 seconds for Arduino Serial Monitor
  153. // Yes, this is a long time, but some Windows systems open
  154. // the port very slowly. This wait allows programs for
  155. // Arduino Uno to "just work" (without forcing a reboot when
  156. // the port is opened), and when no PC is connected the user's
  157. // sketch still gets to run normally after this wait time.
  158. //if ((uint32_t)(systick_millis_count - millis_begin) > 2500) break;
  159. //}
  160. }
  161. void end() { /* TODO: flush output and shut down USB port */ };
  162. virtual int available() { return usb_serial2_available(); }
  163. virtual int read() { return usb_serial2_getchar(); }
  164. virtual int peek() { return usb_serial2_peekchar(); }
  165. virtual void flush() { usb_serial2_flush_output(); } // TODO: actually wait for data to leave USB...
  166. virtual void clear(void) { usb_serial2_flush_input(); }
  167. virtual size_t write(uint8_t c) { return usb_serial2_putchar(c); }
  168. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial2_write(buffer, size); }
  169. size_t write(unsigned long n) { return write((uint8_t)n); }
  170. size_t write(long n) { return write((uint8_t)n); }
  171. size_t write(unsigned int n) { return write((uint8_t)n); }
  172. size_t write(int n) { return write((uint8_t)n); }
  173. virtual int availableForWrite() { return usb_serial2_write_buffer_free(); }
  174. using Print::write;
  175. void send_now(void) { usb_serial2_flush_output(); }
  176. uint32_t baud(void) { return usb_cdc2_line_coding[0]; }
  177. uint8_t stopbits(void) { uint8_t b = usb_cdc2_line_coding[1]; if (!b) b = 1; return b; }
  178. uint8_t paritytype(void) { return usb_cdc2_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
  179. uint8_t numbits(void) { return usb_cdc2_line_coding[1] >> 16; }
  180. uint8_t dtr(void) { return (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
  181. uint8_t rts(void) { return (usb_cdc2_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
  182. operator bool() { return usb_configuration && (usb_cdc2_line_rtsdtr & USB_SERIAL_DTR) &&
  183. ((uint32_t)(systick_millis_count - usb_cdc2_line_rtsdtr_millis) >= 15);
  184. }
  185. size_t readBytes(char *buffer, size_t length) {
  186. size_t count=0;
  187. unsigned long startMillis = millis();
  188. do {
  189. count += usb_serial2_read(buffer + count, length - count);
  190. if (count >= length) return count;
  191. } while(millis() - startMillis < _timeout);
  192. setReadError();
  193. return count;
  194. }
  195. };
  196. extern usb_serial2_class SerialUSB1;
  197. extern void serialEventUSB1(void);
  198. #endif // __cplusplus
  199. #endif // CDC2_STATUS_INTERFACE && CDC2_DATA_INTERFACE
  200. #if defined(CDC3_STATUS_INTERFACE) && defined(CDC3_DATA_INTERFACE)
  201. // C language implementation
  202. #ifdef __cplusplus
  203. extern "C" {
  204. #endif
  205. void usb_serial3_configure(void);
  206. int usb_serial3_getchar(void);
  207. int usb_serial3_peekchar(void);
  208. int usb_serial3_available(void);
  209. int usb_serial3_read(void *buffer, uint32_t size);
  210. void usb_serial3_flush_input(void);
  211. int usb_serial3_putchar(uint8_t c);
  212. int usb_serial3_write(const void *buffer, uint32_t size);
  213. int usb_serial3_write_buffer_free(void);
  214. void usb_serial3_flush_output(void);
  215. extern uint32_t usb_cdc3_line_coding[2];
  216. extern volatile uint32_t usb_cdc3_line_rtsdtr_millis;
  217. extern volatile uint8_t usb_cdc3_line_rtsdtr;
  218. extern volatile uint8_t usb_cdc3_transmit_flush_timer;
  219. #ifdef __cplusplus
  220. }
  221. #endif
  222. // C++ interface
  223. #ifdef __cplusplus
  224. #include "Stream.h"
  225. class usb_serial3_class : public Stream
  226. {
  227. public:
  228. constexpr usb_serial3_class() {}
  229. void begin(long) {
  230. //uint32_t millis_begin = systick_millis_count;
  231. //disabled for now - causes more trouble than it solves?
  232. //while (!(*this)) {
  233. // wait up to 2.5 seconds for Arduino Serial Monitor
  234. // Yes, this is a long time, but some Windows systems open
  235. // the port very slowly. This wait allows programs for
  236. // Arduino Uno to "just work" (without forcing a reboot when
  237. // the port is opened), and when no PC is connected the user's
  238. // sketch still gets to run normally after this wait time.
  239. //if ((uint32_t)(systick_millis_count - millis_begin) > 2500) break;
  240. //}
  241. }
  242. void end() { /* TODO: flush output and shut down USB port */ };
  243. virtual int available() { return usb_serial3_available(); }
  244. virtual int read() { return usb_serial3_getchar(); }
  245. virtual int peek() { return usb_serial3_peekchar(); }
  246. virtual void flush() { usb_serial3_flush_output(); } // TODO: actually wait for data to leave USB...
  247. virtual void clear(void) { usb_serial3_flush_input(); }
  248. virtual size_t write(uint8_t c) { return usb_serial3_putchar(c); }
  249. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial3_write(buffer, size); }
  250. size_t write(unsigned long n) { return write((uint8_t)n); }
  251. size_t write(long n) { return write((uint8_t)n); }
  252. size_t write(unsigned int n) { return write((uint8_t)n); }
  253. size_t write(int n) { return write((uint8_t)n); }
  254. virtual int availableForWrite() { return usb_serial3_write_buffer_free(); }
  255. using Print::write;
  256. void send_now(void) { usb_serial3_flush_output(); }
  257. uint32_t baud(void) { return usb_cdc3_line_coding[0]; }
  258. uint8_t stopbits(void) { uint8_t b = usb_cdc3_line_coding[1]; if (!b) b = 1; return b; }
  259. uint8_t paritytype(void) { return usb_cdc3_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
  260. uint8_t numbits(void) { return usb_cdc3_line_coding[1] >> 16; }
  261. uint8_t dtr(void) { return (usb_cdc3_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
  262. uint8_t rts(void) { return (usb_cdc3_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
  263. operator bool() { return usb_configuration && (usb_cdc3_line_rtsdtr & USB_SERIAL_DTR) &&
  264. ((uint32_t)(systick_millis_count - usb_cdc3_line_rtsdtr_millis) >= 15);
  265. }
  266. size_t readBytes(char *buffer, size_t length) {
  267. size_t count=0;
  268. unsigned long startMillis = millis();
  269. do {
  270. count += usb_serial3_read(buffer + count, length - count);
  271. if (count >= length) return count;
  272. } while(millis() - startMillis < _timeout);
  273. setReadError();
  274. return count;
  275. }
  276. };
  277. extern usb_serial3_class SerialUSB2;
  278. extern void serialEventUSB2(void);
  279. #endif // __cplusplus
  280. #endif // CDC3_STATUS_INTERFACE && CDC3_DATA_INTERFACE