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.

275 lines
7.0KB

  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. //Why is this timout dependend on F_CPU ?
  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 == 48000000
  144. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  145. #elif F_CPU == 24000000
  146. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  147. #else
  148. #error #F_CPU
  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. void usb_serial_flush_output(void)
  206. {
  207. if (!usb_configuration) return;
  208. tx_noautoflush = 1;
  209. if (tx_packet) {
  210. usb_cdc_transmit_flush_timer = 0;
  211. tx_packet->len = tx_packet->index;
  212. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  213. tx_packet = NULL;
  214. } else {
  215. usb_packet_t *tx = usb_malloc();
  216. if (tx) {
  217. usb_cdc_transmit_flush_timer = 0;
  218. usb_tx(CDC_TX_ENDPOINT, tx);
  219. } else {
  220. usb_cdc_transmit_flush_timer = 1;
  221. }
  222. }
  223. tx_noautoflush = 0;
  224. }
  225. void usb_serial_flush_callback(void)
  226. {
  227. if (tx_noautoflush) return;
  228. if (tx_packet) {
  229. tx_packet->len = tx_packet->index;
  230. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  231. tx_packet = NULL;
  232. } else {
  233. usb_packet_t *tx = usb_malloc();
  234. if (tx) {
  235. usb_tx(CDC_TX_ENDPOINT, tx);
  236. } else {
  237. usb_cdc_transmit_flush_timer = 1;
  238. }
  239. }
  240. }
  241. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE