Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

305 lines
8.2KB

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