Teensy 4.1 core updated for C++20
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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 == 168000000
  135. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  136. #elif F_CPU == 144000000
  137. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  138. #elif F_CPU == 120000000
  139. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  140. #elif F_CPU == 96000000
  141. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  142. #elif F_CPU == 72000000
  143. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  144. #elif F_CPU == 48000000
  145. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  146. #elif F_CPU == 24000000
  147. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  148. #endif
  149. // When we've suffered the transmit timeout, don't wait again until the computer
  150. // begins accepting data. If no software is running to receive, we'll just discard
  151. // data as rapidly as Serial.print() can generate it, until there's something to
  152. // actually receive it.
  153. static uint8_t transmit_previous_timeout=0;
  154. // transmit a character. 0 returned on success, -1 on error
  155. int usb_serial_putchar(uint8_t c)
  156. {
  157. return usb_serial_write(&c, 1);
  158. }
  159. int usb_serial_write(const void *buffer, uint32_t size)
  160. {
  161. uint32_t len;
  162. uint32_t wait_count;
  163. const uint8_t *src = (const uint8_t *)buffer;
  164. uint8_t *dest;
  165. tx_noautoflush = 1;
  166. while (size > 0) {
  167. if (!tx_packet) {
  168. wait_count = 0;
  169. while (1) {
  170. if (!usb_configuration) {
  171. tx_noautoflush = 0;
  172. return -1;
  173. }
  174. if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  175. tx_noautoflush = 1;
  176. tx_packet = usb_malloc();
  177. if (tx_packet) break;
  178. tx_noautoflush = 0;
  179. }
  180. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  181. transmit_previous_timeout = 1;
  182. return -1;
  183. }
  184. yield();
  185. }
  186. }
  187. transmit_previous_timeout = 0;
  188. len = CDC_TX_SIZE - tx_packet->index;
  189. if (len > size) len = size;
  190. dest = tx_packet->buf + tx_packet->index;
  191. tx_packet->index += len;
  192. size -= len;
  193. while (len-- > 0) *dest++ = *src++;
  194. if (tx_packet->index >= CDC_TX_SIZE) {
  195. tx_packet->len = CDC_TX_SIZE;
  196. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  197. tx_packet = NULL;
  198. }
  199. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  200. }
  201. tx_noautoflush = 0;
  202. return 0;
  203. }
  204. void usb_serial_flush_output(void)
  205. {
  206. if (!usb_configuration) return;
  207. tx_noautoflush = 1;
  208. if (tx_packet) {
  209. usb_cdc_transmit_flush_timer = 0;
  210. tx_packet->len = tx_packet->index;
  211. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  212. tx_packet = NULL;
  213. } else {
  214. usb_packet_t *tx = usb_malloc();
  215. if (tx) {
  216. usb_cdc_transmit_flush_timer = 0;
  217. usb_tx(CDC_TX_ENDPOINT, tx);
  218. } else {
  219. usb_cdc_transmit_flush_timer = 1;
  220. }
  221. }
  222. tx_noautoflush = 0;
  223. }
  224. void usb_serial_flush_callback(void)
  225. {
  226. if (tx_noautoflush) return;
  227. if (tx_packet) {
  228. tx_packet->len = tx_packet->index;
  229. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  230. tx_packet = NULL;
  231. } else {
  232. usb_packet_t *tx = usb_malloc();
  233. if (tx) {
  234. usb_tx(CDC_TX_ENDPOINT, tx);
  235. } else {
  236. usb_cdc_transmit_flush_timer = 1;
  237. }
  238. }
  239. }
  240. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE