Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usb_serial.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <Lib/OutputLib.h>
  33. #include <string.h> // For memcpy
  34. // defined by usb_dev.h -> usb_desc.h
  35. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  36. uint32_t usb_cdc_line_coding[2];
  37. volatile uint8_t usb_cdc_line_rtsdtr=0;
  38. volatile uint8_t usb_cdc_transmit_flush_timer=0;
  39. static usb_packet_t *rx_packet=NULL;
  40. static usb_packet_t *tx_packet=NULL;
  41. static volatile uint8_t tx_noautoflush=0;
  42. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  43. // get the next character, or -1 if nothing received
  44. int usb_serial_getchar(void)
  45. {
  46. unsigned int i;
  47. int c;
  48. if (!rx_packet) {
  49. if (!usb_configuration) return -1;
  50. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  51. if (!rx_packet) return -1;
  52. }
  53. i = rx_packet->index;
  54. c = rx_packet->buf[i++];
  55. if (i >= rx_packet->len) {
  56. usb_free(rx_packet);
  57. rx_packet = NULL;
  58. } else {
  59. rx_packet->index = i;
  60. }
  61. return c;
  62. }
  63. // peek at the next character, or -1 if nothing received
  64. int usb_serial_peekchar(void)
  65. {
  66. if (!rx_packet) {
  67. if (!usb_configuration) return -1;
  68. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  69. if (!rx_packet) return -1;
  70. }
  71. if (!rx_packet) return -1;
  72. return rx_packet->buf[rx_packet->index];
  73. }
  74. // number of bytes available in the receive buffer
  75. int usb_serial_available(void)
  76. {
  77. int count;
  78. count = usb_rx_byte_count(CDC_RX_ENDPOINT);
  79. if (rx_packet) count += rx_packet->len - rx_packet->index;
  80. return count;
  81. }
  82. // read a block of bytes to a buffer
  83. int usb_serial_read(void *buffer, uint32_t size)
  84. {
  85. uint8_t *p = (uint8_t *)buffer;
  86. uint32_t qty, count=0;
  87. while (size) {
  88. if (!usb_configuration) break;
  89. if (!rx_packet) {
  90. rx:
  91. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  92. if (!rx_packet) break;
  93. if (rx_packet->len == 0) {
  94. usb_free(rx_packet);
  95. goto rx;
  96. }
  97. }
  98. qty = rx_packet->len - rx_packet->index;
  99. if (qty > size) qty = size;
  100. memcpy(p, rx_packet->buf + rx_packet->index, qty);
  101. p += qty;
  102. count += qty;
  103. size -= qty;
  104. rx_packet->index += qty;
  105. if (rx_packet->index >= rx_packet->len) {
  106. usb_free(rx_packet);
  107. rx_packet = NULL;
  108. }
  109. }
  110. return count;
  111. }
  112. // discard any buffered input
  113. void usb_serial_flush_input(void)
  114. {
  115. usb_packet_t *rx;
  116. if (!usb_configuration) return;
  117. if (rx_packet) {
  118. usb_free(rx_packet);
  119. rx_packet = NULL;
  120. }
  121. while (1) {
  122. rx = usb_rx(CDC_RX_ENDPOINT);
  123. if (!rx) break;
  124. usb_free(rx);
  125. }
  126. }
  127. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  128. #define TX_PACKET_LIMIT 8
  129. // When the PC isn't listening, how long do we wait before discarding data? If this is
  130. // too short, we risk losing data during the stalls that are common with ordinary desktop
  131. // software. If it's too long, we stall the user's program when no software is running.
  132. #define TX_TIMEOUT_MSEC 70
  133. #if F_CPU == 96000000
  134. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  135. #elif F_CPU == 48000000
  136. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  137. #elif F_CPU == 24000000
  138. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  139. #endif
  140. // When we've suffered the transmit timeout, don't wait again until the computer
  141. // begins accepting data. If no software is running to receive, we'll just discard
  142. // data as rapidly as Serial.print() can generate it, until there's something to
  143. // actually receive it.
  144. static uint8_t transmit_previous_timeout=0;
  145. // transmit a character. 0 returned on success, -1 on error
  146. int usb_serial_putchar(uint8_t c)
  147. {
  148. return usb_serial_write(&c, 1);
  149. }
  150. int usb_serial_write(const void *buffer, uint32_t size)
  151. {
  152. uint32_t len;
  153. uint32_t wait_count;
  154. const uint8_t *src = (const uint8_t *)buffer;
  155. uint8_t *dest;
  156. tx_noautoflush = 1;
  157. while (size > 0) {
  158. if (!tx_packet) {
  159. wait_count = 0;
  160. while (1) {
  161. if (!usb_configuration) {
  162. tx_noautoflush = 0;
  163. return -1;
  164. }
  165. if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  166. tx_noautoflush = 1;
  167. tx_packet = usb_malloc();
  168. if (tx_packet) break;
  169. tx_noautoflush = 0;
  170. }
  171. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  172. transmit_previous_timeout = 1;
  173. return -1;
  174. }
  175. yield();
  176. }
  177. }
  178. transmit_previous_timeout = 0;
  179. len = CDC_TX_SIZE - tx_packet->index;
  180. if (len > size) len = size;
  181. dest = tx_packet->buf + tx_packet->index;
  182. tx_packet->index += len;
  183. size -= len;
  184. while (len-- > 0) *dest++ = *src++;
  185. if (tx_packet->index >= CDC_TX_SIZE) {
  186. tx_packet->len = CDC_TX_SIZE;
  187. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  188. tx_packet = NULL;
  189. }
  190. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  191. }
  192. tx_noautoflush = 0;
  193. return 0;
  194. }
  195. void usb_serial_flush_output(void)
  196. {
  197. if (!usb_configuration) return;
  198. tx_noautoflush = 1;
  199. if (tx_packet) {
  200. usb_cdc_transmit_flush_timer = 0;
  201. tx_packet->len = tx_packet->index;
  202. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  203. tx_packet = NULL;
  204. } else {
  205. usb_packet_t *tx = usb_malloc();
  206. if (tx) {
  207. usb_cdc_transmit_flush_timer = 0;
  208. usb_tx(CDC_TX_ENDPOINT, tx);
  209. } else {
  210. usb_cdc_transmit_flush_timer = 1;
  211. }
  212. }
  213. tx_noautoflush = 0;
  214. }
  215. void usb_serial_flush_callback(void)
  216. {
  217. if (tx_noautoflush) return;
  218. if (tx_packet) {
  219. tx_packet->len = tx_packet->index;
  220. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  221. tx_packet = NULL;
  222. } else {
  223. usb_packet_t *tx = usb_malloc();
  224. if (tx) {
  225. usb_tx(CDC_TX_ENDPOINT, tx);
  226. } else {
  227. usb_cdc_transmit_flush_timer = 1;
  228. }
  229. }
  230. }
  231. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE