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.

usb_serial.c 8.2KB

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