Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 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 "usb_dev.h"
  31. #include "usb_serial.h"
  32. #include "core_pins.h" // for yield()
  33. //#include "HardwareSerial.h"
  34. #include <string.h> // for memcpy()
  35. // defined by usb_dev.h -> usb_desc.h
  36. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  37. uint32_t usb_cdc_line_coding[2];
  38. volatile uint8_t usb_cdc_line_rtsdtr=0;
  39. volatile uint8_t usb_cdc_transmit_flush_timer=0;
  40. static usb_packet_t *rx_packet=NULL;
  41. static usb_packet_t *tx_packet=NULL;
  42. static volatile uint8_t tx_noautoflush=0;
  43. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  44. // get the next character, or -1 if nothing received
  45. int usb_serial_getchar(void)
  46. {
  47. unsigned int i;
  48. int c;
  49. if (!rx_packet) {
  50. if (!usb_configuration) return -1;
  51. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  52. if (!rx_packet) return -1;
  53. }
  54. i = rx_packet->index;
  55. c = rx_packet->buf[i++];
  56. if (i >= rx_packet->len) {
  57. usb_free(rx_packet);
  58. rx_packet = NULL;
  59. } else {
  60. rx_packet->index = i;
  61. }
  62. return c;
  63. }
  64. // peek at the next character, or -1 if nothing received
  65. int usb_serial_peekchar(void)
  66. {
  67. if (!rx_packet) {
  68. if (!usb_configuration) return -1;
  69. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  70. if (!rx_packet) return -1;
  71. }
  72. if (!rx_packet) return -1;
  73. return rx_packet->buf[rx_packet->index];
  74. }
  75. // number of bytes available in the receive buffer
  76. int usb_serial_available(void)
  77. {
  78. int count;
  79. count = usb_rx_byte_count(CDC_RX_ENDPOINT);
  80. if (rx_packet) count += rx_packet->len - rx_packet->index;
  81. return count;
  82. }
  83. // read a block of bytes to a buffer
  84. int usb_serial_read(void *buffer, uint32_t size)
  85. {
  86. uint8_t *p = (uint8_t *)buffer;
  87. uint32_t qty, count=0;
  88. while (size) {
  89. if (!usb_configuration) break;
  90. if (!rx_packet) {
  91. rx:
  92. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  93. if (!rx_packet) break;
  94. if (rx_packet->len == 0) {
  95. usb_free(rx_packet);
  96. goto rx;
  97. }
  98. }
  99. qty = rx_packet->len - rx_packet->index;
  100. if (qty > size) qty = size;
  101. memcpy(p, rx_packet->buf + rx_packet->index, qty);
  102. p += qty;
  103. count += qty;
  104. size -= qty;
  105. rx_packet->index += qty;
  106. if (rx_packet->index >= rx_packet->len) {
  107. usb_free(rx_packet);
  108. rx_packet = NULL;
  109. }
  110. }
  111. return count;
  112. }
  113. // discard any buffered input
  114. void usb_serial_flush_input(void)
  115. {
  116. usb_packet_t *rx;
  117. if (!usb_configuration) return;
  118. if (rx_packet) {
  119. usb_free(rx_packet);
  120. rx_packet = NULL;
  121. }
  122. while (1) {
  123. rx = usb_rx(CDC_RX_ENDPOINT);
  124. if (!rx) break;
  125. usb_free(rx);
  126. }
  127. }
  128. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  129. #define TX_PACKET_LIMIT 8
  130. // When the PC isn't listening, how long do we wait before discarding data? If this is
  131. // too short, we risk losing data during the stalls that are common with ordinary desktop
  132. // software. If it's too long, we stall the user's program when no software is running.
  133. #define TX_TIMEOUT_MSEC 70
  134. #if F_CPU == 96000000
  135. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  136. #elif F_CPU == 48000000
  137. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  138. #elif F_CPU == 24000000
  139. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  140. #endif
  141. // When we've suffered the transmit timeout, don't wait again until the computer
  142. // begins accepting data. If no software is running to receive, we'll just discard
  143. // data as rapidly as Serial.print() can generate it, until there's something to
  144. // actually receive it.
  145. static uint8_t transmit_previous_timeout=0;
  146. // transmit a character. 0 returned on success, -1 on error
  147. int usb_serial_putchar(uint8_t c)
  148. {
  149. return usb_serial_write(&c, 1);
  150. }
  151. int usb_serial_write(const void *buffer, uint32_t size)
  152. {
  153. uint32_t len;
  154. uint32_t wait_count;
  155. const uint8_t *src = (const uint8_t *)buffer;
  156. uint8_t *dest;
  157. tx_noautoflush = 1;
  158. while (size > 0) {
  159. if (!tx_packet) {
  160. wait_count = 0;
  161. while (1) {
  162. if (!usb_configuration) {
  163. tx_noautoflush = 0;
  164. return -1;
  165. }
  166. if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  167. tx_noautoflush = 1;
  168. tx_packet = usb_malloc();
  169. if (tx_packet) break;
  170. tx_noautoflush = 0;
  171. }
  172. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  173. transmit_previous_timeout = 1;
  174. return -1;
  175. }
  176. yield();
  177. }
  178. }
  179. transmit_previous_timeout = 0;
  180. len = CDC_TX_SIZE - tx_packet->index;
  181. if (len > size) len = size;
  182. dest = tx_packet->buf + tx_packet->index;
  183. tx_packet->index += len;
  184. size -= len;
  185. while (len-- > 0) *dest++ = *src++;
  186. if (tx_packet->index >= CDC_TX_SIZE) {
  187. tx_packet->len = CDC_TX_SIZE;
  188. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  189. tx_packet = NULL;
  190. }
  191. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  192. }
  193. tx_noautoflush = 0;
  194. return 0;
  195. }
  196. void usb_serial_flush_output(void)
  197. {
  198. if (!usb_configuration) return;
  199. tx_noautoflush = 1;
  200. if (tx_packet) {
  201. usb_cdc_transmit_flush_timer = 0;
  202. tx_packet->len = tx_packet->index;
  203. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  204. tx_packet = NULL;
  205. } else {
  206. usb_packet_t *tx = usb_malloc();
  207. if (tx) {
  208. usb_cdc_transmit_flush_timer = 0;
  209. usb_tx(CDC_TX_ENDPOINT, tx);
  210. } else {
  211. usb_cdc_transmit_flush_timer = 1;
  212. }
  213. }
  214. tx_noautoflush = 0;
  215. }
  216. void usb_serial_flush_callback(void)
  217. {
  218. if (tx_noautoflush) return;
  219. if (tx_packet) {
  220. tx_packet->len = tx_packet->index;
  221. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  222. tx_packet = NULL;
  223. } else {
  224. usb_packet_t *tx = usb_malloc();
  225. if (tx) {
  226. usb_tx(CDC_TX_ENDPOINT, tx);
  227. } else {
  228. usb_cdc_transmit_flush_timer = 1;
  229. }
  230. }
  231. }
  232. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE