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_port.c 8.8KB

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