選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

307 行
8.3KB

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