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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. * Modified by Jacob Alexander 2013-2015
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * 1. The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * 2. If the Software is incorporated into a build system that allows
  18. * selection among a list of target devices, then similar target
  19. * devices manufactured by PJRC.COM must be included in the list of
  20. * target devices and selectable in the same manner.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. // ----- Includes -----
  32. // Compiler Includes
  33. #include <string.h> // For memcpy
  34. // Project Includes
  35. #include <Lib/OutputLib.h>
  36. // Local Includes
  37. #include "usb_dev.h"
  38. #include "usb_serial.h"
  39. // ----- Defines -----
  40. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  41. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  42. #define TX_PACKET_LIMIT 8
  43. // When the PC isn't listening, how long do we wait before discarding data? If this is
  44. // too short, we risk losing data during the stalls that are common with ordinary desktop
  45. // software. If it's too long, we stall the user's program when no software is running.
  46. #define TX_TIMEOUT_MSEC 70
  47. #if F_CPU == 96000000
  48. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  49. #elif F_CPU == 72000000
  50. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512) // XXX Correct?
  51. #elif F_CPU == 48000000
  52. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  53. #elif F_CPU == 24000000
  54. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  55. #endif
  56. // ----- Variables -----
  57. // serial port settings (baud rate, control signals, etc) set
  58. // by the PC. These are ignored, but kept in RAM.
  59. volatile uint8_t usb_cdc_line_coding[7] = { 0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08 };
  60. volatile uint8_t usb_cdc_line_rtsdtr = 0;
  61. volatile uint8_t usb_cdc_transmit_flush_timer = 0;
  62. static usb_packet_t *rx_packet = NULL;
  63. static usb_packet_t *tx_packet = NULL;
  64. static volatile uint8_t tx_noautoflush = 0;
  65. // When we've suffered the transmit timeout, don't wait again until the computer
  66. // begins accepting data. If no software is running to receive, we'll just discard
  67. // data as rapidly as Serial.print() can generate it, until there's something to
  68. // actually receive it.
  69. static uint8_t transmit_previous_timeout = 0;
  70. // ----- Functions -----
  71. // get the next character, or -1 if nothing received
  72. int usb_serial_getchar()
  73. {
  74. unsigned int i;
  75. int c;
  76. if ( !rx_packet )
  77. {
  78. if ( !usb_configuration )
  79. return -1;
  80. rx_packet = usb_rx( CDC_RX_ENDPOINT );
  81. if ( !rx_packet )
  82. return -1;
  83. }
  84. i = rx_packet->index;
  85. c = rx_packet->buf[i++];
  86. if ( i >= rx_packet->len )
  87. {
  88. usb_free( rx_packet );
  89. rx_packet = NULL;
  90. }
  91. else
  92. {
  93. rx_packet->index = i;
  94. }
  95. return c;
  96. }
  97. // peek at the next character, or -1 if nothing received
  98. int usb_serial_peekchar()
  99. {
  100. if ( !rx_packet )
  101. {
  102. if ( !usb_configuration )
  103. return -1;
  104. rx_packet = usb_rx( CDC_RX_ENDPOINT );
  105. if ( !rx_packet )
  106. return -1;
  107. }
  108. if ( !rx_packet )
  109. return -1;
  110. return rx_packet->buf[ rx_packet->index ];
  111. }
  112. // number of bytes available in the receive buffer
  113. int usb_serial_available()
  114. {
  115. int count = usb_rx_byte_count( CDC_RX_ENDPOINT );
  116. if ( rx_packet )
  117. count += rx_packet->len - rx_packet->index;
  118. return count;
  119. }
  120. // read a block of bytes to a buffer
  121. int usb_serial_read( void *buffer, uint32_t size )
  122. {
  123. uint8_t *p = (uint8_t *)buffer;
  124. uint32_t qty, count=0;
  125. while ( size )
  126. {
  127. if ( !usb_configuration )
  128. break;
  129. if ( !rx_packet )
  130. {
  131. rx:
  132. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  133. if ( !rx_packet )
  134. break;
  135. if ( rx_packet->len == 0 )
  136. {
  137. usb_free(rx_packet);
  138. goto rx;
  139. }
  140. }
  141. qty = rx_packet->len - rx_packet->index;
  142. if ( qty > size )
  143. qty = size;
  144. memcpy( p, rx_packet->buf + rx_packet->index, qty );
  145. p += qty;
  146. count += qty;
  147. size -= qty;
  148. rx_packet->index += qty;
  149. if ( rx_packet->index >= rx_packet->len )
  150. {
  151. usb_free( rx_packet );
  152. rx_packet = NULL;
  153. }
  154. }
  155. return count;
  156. }
  157. // discard any buffered input
  158. void usb_serial_flush_input()
  159. {
  160. usb_packet_t *rx;
  161. if ( !usb_configuration )
  162. return;
  163. if ( rx_packet )
  164. {
  165. usb_free( rx_packet );
  166. rx_packet = NULL;
  167. }
  168. while (1)
  169. {
  170. rx = usb_rx( CDC_RX_ENDPOINT );
  171. if ( !rx )
  172. break;
  173. usb_free( rx );
  174. }
  175. }
  176. // transmit a character. 0 returned on success, -1 on error
  177. int usb_serial_putchar( uint8_t c )
  178. {
  179. return usb_serial_write( &c, 1 );
  180. }
  181. int usb_serial_write( const void *buffer, uint32_t size )
  182. {
  183. uint32_t len;
  184. uint32_t wait_count;
  185. const uint8_t *src = (const uint8_t *)buffer;
  186. uint8_t *dest;
  187. tx_noautoflush = 1;
  188. while ( size > 0 )
  189. {
  190. if ( !tx_packet )
  191. {
  192. wait_count = 0;
  193. while ( 1 )
  194. {
  195. if ( !usb_configuration )
  196. {
  197. tx_noautoflush = 0;
  198. return -1;
  199. }
  200. if ( usb_tx_packet_count( CDC_TX_ENDPOINT ) < TX_PACKET_LIMIT )
  201. {
  202. tx_noautoflush = 1;
  203. tx_packet = usb_malloc();
  204. if ( tx_packet )
  205. break;
  206. tx_noautoflush = 0;
  207. }
  208. if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout )
  209. {
  210. transmit_previous_timeout = 1;
  211. return -1;
  212. }
  213. yield();
  214. }
  215. }
  216. transmit_previous_timeout = 0;
  217. len = CDC_TX_SIZE - tx_packet->index;
  218. if ( len > size )
  219. len = size;
  220. dest = tx_packet->buf + tx_packet->index;
  221. tx_packet->index += len;
  222. size -= len;
  223. while ( len-- > 0 )
  224. *dest++ = *src++;
  225. if ( tx_packet->index >= CDC_TX_SIZE )
  226. {
  227. tx_packet->len = CDC_TX_SIZE;
  228. usb_tx( CDC_TX_ENDPOINT, tx_packet );
  229. tx_packet = NULL;
  230. }
  231. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  232. }
  233. tx_noautoflush = 0;
  234. return 0;
  235. }
  236. void usb_serial_flush_output()
  237. {
  238. if ( !usb_configuration )
  239. return;
  240. tx_noautoflush = 1;
  241. if ( tx_packet )
  242. {
  243. usb_cdc_transmit_flush_timer = 0;
  244. tx_packet->len = tx_packet->index;
  245. usb_tx( CDC_TX_ENDPOINT, tx_packet );
  246. tx_packet = NULL;
  247. }
  248. else
  249. {
  250. usb_packet_t *tx = usb_malloc();
  251. if ( tx )
  252. {
  253. usb_cdc_transmit_flush_timer = 0;
  254. usb_tx( CDC_TX_ENDPOINT, tx );
  255. }
  256. else
  257. {
  258. usb_cdc_transmit_flush_timer = 1;
  259. }
  260. }
  261. tx_noautoflush = 0;
  262. }
  263. void usb_serial_flush_callback()
  264. {
  265. if ( tx_noautoflush )
  266. return;
  267. if ( tx_packet )
  268. {
  269. tx_packet->len = tx_packet->index;
  270. usb_tx( CDC_TX_ENDPOINT, tx_packet );
  271. tx_packet = NULL;
  272. } else {
  273. usb_packet_t *tx = usb_malloc();
  274. if ( tx )
  275. {
  276. usb_tx( CDC_TX_ENDPOINT, tx );
  277. }
  278. else
  279. {
  280. usb_cdc_transmit_flush_timer = 1;
  281. }
  282. }
  283. }