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.

301 line
8.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. #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 == 192000000
  137. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  138. #elif F_CPU == 180000000
  139. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  140. #elif F_CPU == 168000000
  141. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  142. #elif F_CPU == 144000000
  143. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  144. #elif F_CPU == 120000000
  145. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  146. #elif F_CPU == 96000000
  147. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  148. #elif F_CPU == 72000000
  149. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  150. #elif F_CPU == 48000000
  151. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  152. #elif F_CPU == 24000000
  153. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  154. #endif
  155. // When we've suffered the transmit timeout, don't wait again until the computer
  156. // begins accepting data. If no software is running to receive, we'll just discard
  157. // data as rapidly as Serial.print() can generate it, until there's something to
  158. // actually receive it.
  159. static uint8_t transmit_previous_timeout=0;
  160. // transmit a character. 0 returned on success, -1 on error
  161. int usb_serial_putchar(uint8_t c)
  162. {
  163. return usb_serial_write(&c, 1);
  164. }
  165. int usb_serial_write(const void *buffer, uint32_t size)
  166. {
  167. uint32_t len;
  168. uint32_t wait_count;
  169. const uint8_t *src = (const uint8_t *)buffer;
  170. uint8_t *dest;
  171. tx_noautoflush = 1;
  172. while (size > 0) {
  173. if (!tx_packet) {
  174. wait_count = 0;
  175. while (1) {
  176. if (!usb_configuration) {
  177. tx_noautoflush = 0;
  178. return -1;
  179. }
  180. if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  181. tx_noautoflush = 1;
  182. tx_packet = usb_malloc();
  183. if (tx_packet) break;
  184. tx_noautoflush = 0;
  185. }
  186. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  187. transmit_previous_timeout = 1;
  188. return -1;
  189. }
  190. yield();
  191. }
  192. }
  193. transmit_previous_timeout = 0;
  194. len = CDC_TX_SIZE - tx_packet->index;
  195. if (len > size) len = size;
  196. dest = tx_packet->buf + tx_packet->index;
  197. tx_packet->index += len;
  198. size -= len;
  199. while (len-- > 0) *dest++ = *src++;
  200. if (tx_packet->index >= CDC_TX_SIZE) {
  201. tx_packet->len = CDC_TX_SIZE;
  202. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  203. tx_packet = NULL;
  204. }
  205. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  206. }
  207. tx_noautoflush = 0;
  208. return 0;
  209. }
  210. int usb_serial_write_buffer_free(void)
  211. {
  212. uint32_t len;
  213. tx_noautoflush = 1;
  214. if (!tx_packet) {
  215. if (!usb_configuration ||
  216. usb_tx_packet_count(CDC_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
  217. (tx_packet = usb_malloc()) == NULL) {
  218. tx_noautoflush = 0;
  219. return 0;
  220. }
  221. }
  222. len = CDC_TX_SIZE - tx_packet->index;
  223. // TODO: Perhaps we need "usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT"
  224. // added here, so the SOF interrupt can't take away the available buffer
  225. // space we just promised the user could write without blocking?
  226. // But does this come with other performance downsides? Could it lead to
  227. // buffer data never actually transmitting in some usage cases? More
  228. // investigation is needed.
  229. // https://github.com/PaulStoffregen/cores/issues/10#issuecomment-61514955
  230. tx_noautoflush = 0;
  231. return len;
  232. }
  233. void usb_serial_flush_output(void)
  234. {
  235. if (!usb_configuration) return;
  236. tx_noautoflush = 1;
  237. if (tx_packet) {
  238. usb_cdc_transmit_flush_timer = 0;
  239. tx_packet->len = tx_packet->index;
  240. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  241. tx_packet = NULL;
  242. } else {
  243. usb_packet_t *tx = usb_malloc();
  244. if (tx) {
  245. usb_cdc_transmit_flush_timer = 0;
  246. usb_tx(CDC_TX_ENDPOINT, tx);
  247. } else {
  248. usb_cdc_transmit_flush_timer = 1;
  249. }
  250. }
  251. tx_noautoflush = 0;
  252. }
  253. void usb_serial_flush_callback(void)
  254. {
  255. if (tx_noautoflush) return;
  256. if (tx_packet) {
  257. tx_packet->len = tx_packet->index;
  258. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  259. tx_packet = NULL;
  260. } else {
  261. usb_packet_t *tx = usb_malloc();
  262. if (tx) {
  263. usb_tx(CDC_TX_ENDPOINT, tx);
  264. } else {
  265. usb_cdc_transmit_flush_timer = 1;
  266. }
  267. }
  268. }
  269. #endif // F_CPU
  270. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE