Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

297 lines
7.9KB

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