Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

usb_serial.h 14KB

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