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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. * Modified by Jacob Alexander 2013-2014
  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 == 48000000
  50. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  51. #elif F_CPU == 24000000
  52. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  53. #endif
  54. // ----- Variables -----
  55. // serial port settings (baud rate, control signals, etc) set
  56. // by the PC. These are ignored, but kept in RAM.
  57. volatile uint8_t usb_cdc_line_coding[7] = { 0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08 };
  58. volatile uint8_t usb_cdc_line_rtsdtr = 0;
  59. volatile uint8_t usb_cdc_transmit_flush_timer = 0;
  60. static usb_packet_t *rx_packet = NULL;
  61. static usb_packet_t *tx_packet = NULL;
  62. static volatile uint8_t tx_noautoflush = 0;
  63. // When we've suffered the transmit timeout, don't wait again until the computer
  64. // begins accepting data. If no software is running to receive, we'll just discard
  65. // data as rapidly as Serial.print() can generate it, until there's something to
  66. // actually receive it.
  67. static uint8_t transmit_previous_timeout = 0;
  68. // ----- Functions -----
  69. // get the next character, or -1 if nothing received
  70. int usb_serial_getchar()
  71. {
  72. unsigned int i;
  73. int c;
  74. if ( !rx_packet )
  75. {
  76. if ( !usb_configuration )
  77. return -1;
  78. rx_packet = usb_rx( CDC_RX_ENDPOINT );
  79. if ( !rx_packet )
  80. return -1;
  81. }
  82. i = rx_packet->index;
  83. c = rx_packet->buf[i++];
  84. if ( i >= rx_packet->len )
  85. {
  86. usb_free( rx_packet );
  87. rx_packet = NULL;
  88. }
  89. else
  90. {
  91. rx_packet->index = i;
  92. }
  93. return c;
  94. }
  95. // peek at the next character, or -1 if nothing received
  96. int usb_serial_peekchar()
  97. {
  98. if ( !rx_packet )
  99. {
  100. if ( !usb_configuration )
  101. return -1;
  102. rx_packet = usb_rx( CDC_RX_ENDPOINT );
  103. if ( !rx_packet )
  104. return -1;
  105. }
  106. if ( !rx_packet )
  107. return -1;
  108. return rx_packet->buf[ rx_packet->index ];
  109. }
  110. // number of bytes available in the receive buffer
  111. int usb_serial_available()
  112. {
  113. int count = usb_rx_byte_count( CDC_RX_ENDPOINT );
  114. if ( rx_packet )
  115. count += rx_packet->len - rx_packet->index;
  116. return count;
  117. }
  118. // read a block of bytes to a buffer
  119. int usb_serial_read( void *buffer, uint32_t size )
  120. {
  121. uint8_t *p = (uint8_t *)buffer;
  122. uint32_t qty, count=0;
  123. while ( size )
  124. {
  125. if ( !usb_configuration )
  126. break;
  127. if ( !rx_packet )
  128. {
  129. rx:
  130. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  131. if ( !rx_packet )
  132. break;
  133. if ( rx_packet->len == 0 )
  134. {
  135. usb_free(rx_packet);
  136. goto rx;
  137. }
  138. }
  139. qty = rx_packet->len - rx_packet->index;
  140. if ( qty > size )
  141. qty = size;
  142. memcpy( p, rx_packet->buf + rx_packet->index, qty );
  143. p += qty;
  144. count += qty;
  145. size -= qty;
  146. rx_packet->index += qty;
  147. if ( rx_packet->index >= rx_packet->len )
  148. {
  149. usb_free( rx_packet );
  150. rx_packet = NULL;
  151. }
  152. }
  153. return count;
  154. }
  155. // discard any buffered input
  156. void usb_serial_flush_input()
  157. {
  158. usb_packet_t *rx;
  159. if ( !usb_configuration )
  160. return;
  161. if ( rx_packet )
  162. {
  163. usb_free( rx_packet );
  164. rx_packet = NULL;
  165. }
  166. while (1)
  167. {
  168. rx = usb_rx( CDC_RX_ENDPOINT );
  169. if ( !rx )
  170. break;
  171. usb_free( rx );
  172. }
  173. }
  174. // transmit a character. 0 returned on success, -1 on error
  175. int usb_serial_putchar( uint8_t c )
  176. {
  177. return usb_serial_write( &c, 1 );
  178. }
  179. int usb_serial_write( const void *buffer, uint32_t size )
  180. {
  181. uint32_t len;
  182. uint32_t wait_count;
  183. const uint8_t *src = (const uint8_t *)buffer;
  184. uint8_t *dest;
  185. tx_noautoflush = 1;
  186. while ( size > 0 )
  187. {
  188. if ( !tx_packet )
  189. {
  190. wait_count = 0;
  191. while ( 1 )
  192. {
  193. if ( !usb_configuration )
  194. {
  195. tx_noautoflush = 0;
  196. return -1;
  197. }
  198. if ( usb_tx_packet_count( CDC_TX_ENDPOINT ) < TX_PACKET_LIMIT )
  199. {
  200. tx_noautoflush = 1;
  201. tx_packet = usb_malloc();
  202. if ( tx_packet )
  203. break;
  204. tx_noautoflush = 0;
  205. }
  206. if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout )
  207. {
  208. transmit_previous_timeout = 1;
  209. return -1;
  210. }
  211. yield();
  212. }
  213. }
  214. transmit_previous_timeout = 0;
  215. len = CDC_TX_SIZE - tx_packet->index;
  216. if ( len > size )
  217. len = size;
  218. dest = tx_packet->buf + tx_packet->index;
  219. tx_packet->index += len;
  220. size -= len;
  221. while ( len-- > 0 )
  222. *dest++ = *src++;
  223. if ( tx_packet->index >= CDC_TX_SIZE )
  224. {
  225. tx_packet->len = CDC_TX_SIZE;
  226. usb_tx( CDC_TX_ENDPOINT, tx_packet );
  227. tx_packet = NULL;
  228. }
  229. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  230. }
  231. tx_noautoflush = 0;
  232. return 0;
  233. }
  234. void usb_serial_flush_output()
  235. {
  236. if ( !usb_configuration )
  237. return;
  238. tx_noautoflush = 1;
  239. if ( tx_packet )
  240. {
  241. usb_cdc_transmit_flush_timer = 0;
  242. tx_packet->len = tx_packet->index;
  243. usb_tx( CDC_TX_ENDPOINT, tx_packet );
  244. tx_packet = NULL;
  245. }
  246. else
  247. {
  248. usb_packet_t *tx = usb_malloc();
  249. if ( tx )
  250. {
  251. usb_cdc_transmit_flush_timer = 0;
  252. usb_tx( CDC_TX_ENDPOINT, tx );
  253. }
  254. else
  255. {
  256. usb_cdc_transmit_flush_timer = 1;
  257. }
  258. }
  259. tx_noautoflush = 0;
  260. }
  261. void usb_serial_flush_callback()
  262. {
  263. if ( tx_noautoflush )
  264. return;
  265. if ( tx_packet )
  266. {
  267. tx_packet->len = tx_packet->index;
  268. usb_tx( CDC_TX_ENDPOINT, tx_packet );
  269. tx_packet = NULL;
  270. } else {
  271. usb_packet_t *tx = usb_malloc();
  272. if ( tx )
  273. {
  274. usb_tx( CDC_TX_ENDPOINT, tx );
  275. }
  276. else
  277. {
  278. usb_cdc_transmit_flush_timer = 1;
  279. }
  280. }
  281. }