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.

пре 11 година
пре 10 година
пре 11 година
пре 10 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 10 година
пре 10 година
пре 10 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #if F_CPU >= 20000000
  38. uint32_t usb_cdc_line_coding[2];
  39. volatile uint8_t usb_cdc_line_rtsdtr=0;
  40. volatile uint8_t usb_cdc_transmit_flush_timer=0;
  41. static usb_packet_t *rx_packet=NULL;
  42. static usb_packet_t *tx_packet=NULL;
  43. static volatile uint8_t tx_noautoflush=0;
  44. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  45. // get the next character, or -1 if nothing received
  46. int usb_serial_getchar(void)
  47. {
  48. unsigned int i;
  49. int c;
  50. if (!rx_packet) {
  51. if (!usb_configuration) return -1;
  52. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  53. if (!rx_packet) return -1;
  54. }
  55. i = rx_packet->index;
  56. c = rx_packet->buf[i++];
  57. if (i >= rx_packet->len) {
  58. usb_free(rx_packet);
  59. rx_packet = NULL;
  60. } else {
  61. rx_packet->index = i;
  62. }
  63. return c;
  64. }
  65. // peek at the next character, or -1 if nothing received
  66. int usb_serial_peekchar(void)
  67. {
  68. if (!rx_packet) {
  69. if (!usb_configuration) return -1;
  70. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  71. if (!rx_packet) return -1;
  72. }
  73. if (!rx_packet) return -1;
  74. return rx_packet->buf[rx_packet->index];
  75. }
  76. // number of bytes available in the receive buffer
  77. int usb_serial_available(void)
  78. {
  79. int count;
  80. count = usb_rx_byte_count(CDC_RX_ENDPOINT);
  81. if (rx_packet) count += rx_packet->len - rx_packet->index;
  82. return count;
  83. }
  84. // read a block of bytes to a buffer
  85. int usb_serial_read(void *buffer, uint32_t size)
  86. {
  87. uint8_t *p = (uint8_t *)buffer;
  88. uint32_t qty, count=0;
  89. while (size) {
  90. if (!usb_configuration) break;
  91. if (!rx_packet) {
  92. rx:
  93. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  94. if (!rx_packet) break;
  95. if (rx_packet->len == 0) {
  96. usb_free(rx_packet);
  97. goto rx;
  98. }
  99. }
  100. qty = rx_packet->len - rx_packet->index;
  101. if (qty > size) qty = size;
  102. memcpy(p, rx_packet->buf + rx_packet->index, qty);
  103. p += qty;
  104. count += qty;
  105. size -= qty;
  106. rx_packet->index += qty;
  107. if (rx_packet->index >= rx_packet->len) {
  108. usb_free(rx_packet);
  109. rx_packet = NULL;
  110. }
  111. }
  112. return count;
  113. }
  114. // discard any buffered input
  115. void usb_serial_flush_input(void)
  116. {
  117. usb_packet_t *rx;
  118. if (!usb_configuration) return;
  119. if (rx_packet) {
  120. usb_free(rx_packet);
  121. rx_packet = NULL;
  122. }
  123. while (1) {
  124. rx = usb_rx(CDC_RX_ENDPOINT);
  125. if (!rx) break;
  126. usb_free(rx);
  127. }
  128. }
  129. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  130. #define TX_PACKET_LIMIT 8
  131. // When the PC isn't listening, how long do we wait before discarding data? If this is
  132. // too short, we risk losing data during the stalls that are common with ordinary desktop
  133. // software. If it's too long, we stall the user's program when no software is running.
  134. #define TX_TIMEOUT_MSEC 70
  135. #if F_CPU == 168000000
  136. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  137. #elif F_CPU == 144000000
  138. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  139. #elif F_CPU == 120000000
  140. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  141. #elif F_CPU == 96000000
  142. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  143. #elif F_CPU == 72000000
  144. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  145. #elif F_CPU == 48000000
  146. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  147. #elif F_CPU == 24000000
  148. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  149. #endif
  150. // When we've suffered the transmit timeout, don't wait again until the computer
  151. // begins accepting data. If no software is running to receive, we'll just discard
  152. // data as rapidly as Serial.print() can generate it, until there's something to
  153. // actually receive it.
  154. static uint8_t transmit_previous_timeout=0;
  155. // transmit a character. 0 returned on success, -1 on error
  156. int usb_serial_putchar(uint8_t c)
  157. {
  158. return usb_serial_write(&c, 1);
  159. }
  160. int usb_serial_write(const void *buffer, uint32_t size)
  161. {
  162. uint32_t len;
  163. uint32_t wait_count;
  164. const uint8_t *src = (const uint8_t *)buffer;
  165. uint8_t *dest;
  166. tx_noautoflush = 1;
  167. while (size > 0) {
  168. if (!tx_packet) {
  169. wait_count = 0;
  170. while (1) {
  171. if (!usb_configuration) {
  172. tx_noautoflush = 0;
  173. return -1;
  174. }
  175. if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  176. tx_noautoflush = 1;
  177. tx_packet = usb_malloc();
  178. if (tx_packet) break;
  179. tx_noautoflush = 0;
  180. }
  181. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  182. transmit_previous_timeout = 1;
  183. return -1;
  184. }
  185. yield();
  186. }
  187. }
  188. transmit_previous_timeout = 0;
  189. len = CDC_TX_SIZE - tx_packet->index;
  190. if (len > size) len = size;
  191. dest = tx_packet->buf + tx_packet->index;
  192. tx_packet->index += len;
  193. size -= len;
  194. while (len-- > 0) *dest++ = *src++;
  195. if (tx_packet->index >= CDC_TX_SIZE) {
  196. tx_packet->len = CDC_TX_SIZE;
  197. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  198. tx_packet = NULL;
  199. }
  200. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  201. }
  202. tx_noautoflush = 0;
  203. return 0;
  204. }
  205. int usb_serial_write_buffer_free(void)
  206. {
  207. uint32_t len;
  208. tx_noautoflush = 1;
  209. if (!tx_packet) {
  210. if (!usb_configuration ||
  211. usb_tx_packet_count(CDC_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
  212. (tx_packet = usb_malloc()) == NULL) {
  213. tx_noautoflush = 0;
  214. return 0;
  215. }
  216. }
  217. len = CDC_TX_SIZE - tx_packet->index;
  218. // TODO: Perhaps we need "usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT"
  219. // added here, so the SOF interrupt can't take away the available buffer
  220. // space we just promised the user could write without blocking?
  221. // But does this come with other performance downsides? Could it lead to
  222. // buffer data never actually transmitting in some usage cases? More
  223. // investigation is needed.
  224. // https://github.com/PaulStoffregen/cores/issues/10#issuecomment-61514955
  225. tx_noautoflush = 0;
  226. return len;
  227. }
  228. void usb_serial_flush_output(void)
  229. {
  230. if (!usb_configuration) return;
  231. tx_noautoflush = 1;
  232. if (tx_packet) {
  233. usb_cdc_transmit_flush_timer = 0;
  234. tx_packet->len = tx_packet->index;
  235. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  236. tx_packet = NULL;
  237. } else {
  238. usb_packet_t *tx = usb_malloc();
  239. if (tx) {
  240. usb_cdc_transmit_flush_timer = 0;
  241. usb_tx(CDC_TX_ENDPOINT, tx);
  242. } else {
  243. usb_cdc_transmit_flush_timer = 1;
  244. }
  245. }
  246. tx_noautoflush = 0;
  247. }
  248. void usb_serial_flush_callback(void)
  249. {
  250. if (tx_noautoflush) return;
  251. if (tx_packet) {
  252. tx_packet->len = tx_packet->index;
  253. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  254. tx_packet = NULL;
  255. } else {
  256. usb_packet_t *tx = usb_malloc();
  257. if (tx) {
  258. usb_tx(CDC_TX_ENDPOINT, tx);
  259. } else {
  260. usb_cdc_transmit_flush_timer = 1;
  261. }
  262. }
  263. }
  264. #endif // F_CPU
  265. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE