Teensy 4.1 core updated for C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

268 Zeilen
6.8KB

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