Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

usb_seremu.c 7.5KB

6 lat temu
8 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #if F_CPU >= 20000000
  31. #include "usb_dev.h"
  32. #include "usb_seremu.h"
  33. #include "core_pins.h" // for yield()
  34. //#include "HardwareSerial.h"
  35. #ifdef SEREMU_INTERFACE // defined by usb_dev.h -> usb_desc.h
  36. volatile uint8_t usb_seremu_transmit_flush_timer=0;
  37. static usb_packet_t *rx_packet=NULL;
  38. static usb_packet_t *tx_packet=NULL;
  39. static volatile uint8_t tx_noautoflush=0;
  40. volatile uint8_t usb_seremu_online=0;
  41. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  42. // get the next character, or -1 if nothing received
  43. int usb_seremu_getchar(void)
  44. {
  45. unsigned int i;
  46. int c;
  47. while (1) {
  48. if (!usb_configuration) return -1;
  49. if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
  50. if (!rx_packet) return -1;
  51. i = rx_packet->index;
  52. c = rx_packet->buf[i++];
  53. if (c) {
  54. if (i >= rx_packet->len) {
  55. usb_free(rx_packet);
  56. rx_packet = NULL;
  57. } else {
  58. rx_packet->index = i;
  59. }
  60. return c;
  61. }
  62. usb_free(rx_packet);
  63. rx_packet = NULL;
  64. }
  65. }
  66. // peek at the next character, or -1 if nothing received
  67. int usb_seremu_peekchar(void)
  68. {
  69. int c;
  70. while (1) {
  71. if (!usb_configuration) return -1;
  72. if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
  73. if (!rx_packet) return -1;
  74. c = rx_packet->buf[rx_packet->index];
  75. if (c) return c;
  76. usb_free(rx_packet);
  77. rx_packet = NULL;
  78. }
  79. }
  80. // number of bytes available in the receive buffer
  81. int usb_seremu_available(void)
  82. {
  83. int i, len, count;
  84. if (!rx_packet) {
  85. if (usb_configuration) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
  86. if (!rx_packet) return 0;
  87. }
  88. len = rx_packet->len;
  89. i = rx_packet->index;
  90. count = 0;
  91. for (i = rx_packet->index; i < len; i++) {
  92. if (rx_packet->buf[i] == 0) break;
  93. count++;
  94. }
  95. if (count == 0) {
  96. usb_free(rx_packet);
  97. rx_packet = NULL;
  98. }
  99. return count;
  100. }
  101. // discard any buffered input
  102. void usb_seremu_flush_input(void)
  103. {
  104. usb_packet_t *rx;
  105. if (!usb_configuration) return;
  106. if (rx_packet) {
  107. usb_free(rx_packet);
  108. rx_packet = NULL;
  109. }
  110. while (1) {
  111. rx = usb_rx(SEREMU_RX_ENDPOINT);
  112. if (!rx) break;
  113. usb_free(rx);
  114. }
  115. }
  116. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  117. #define TX_PACKET_LIMIT 6
  118. // When the PC isn't listening, how long do we wait before discarding data? If this is
  119. // too short, we risk losing data during the stalls that are common with ordinary desktop
  120. // software. If it's too long, we stall the user's program when no software is running.
  121. #define TX_TIMEOUT_MSEC 30
  122. #if F_CPU == 256000000
  123. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706)
  124. #elif F_CPU == 240000000
  125. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
  126. #elif F_CPU == 216000000
  127. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
  128. #elif F_CPU == 192000000
  129. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  130. #elif F_CPU == 180000000
  131. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  132. #elif F_CPU == 168000000
  133. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  134. #elif F_CPU == 144000000
  135. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  136. #elif F_CPU == 120000000
  137. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  138. #elif F_CPU == 96000000
  139. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  140. #elif F_CPU == 72000000
  141. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  142. #elif F_CPU == 48000000
  143. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  144. #elif F_CPU == 24000000
  145. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  146. #endif
  147. // When we've suffered the transmit timeout, don't wait again until the computer
  148. // begins accepting data. If no software is running to receive, we'll just discard
  149. // data as rapidly as Serial.print() can generate it, until there's something to
  150. // actually receive it.
  151. static uint8_t transmit_previous_timeout=0;
  152. // transmit a character. 0 returned on success, -1 on error
  153. int usb_seremu_putchar(uint8_t c)
  154. {
  155. return usb_seremu_write(&c, 1);
  156. }
  157. int usb_seremu_write(const void *buffer, uint32_t size)
  158. {
  159. #if 1
  160. uint32_t len;
  161. uint32_t wait_count;
  162. const uint8_t *src = (const uint8_t *)buffer;
  163. uint8_t *dest;
  164. tx_noautoflush = 1;
  165. while (size > 0) {
  166. if (!tx_packet) {
  167. wait_count = 0;
  168. while (1) {
  169. if (!usb_configuration) {
  170. tx_noautoflush = 0;
  171. return -1;
  172. }
  173. if (usb_tx_packet_count(SEREMU_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  174. tx_noautoflush = 1;
  175. tx_packet = usb_malloc();
  176. if (tx_packet) break;
  177. }
  178. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  179. transmit_previous_timeout = 1;
  180. tx_noautoflush = 0;
  181. return -1;
  182. }
  183. tx_noautoflush = 0;
  184. yield();
  185. tx_noautoflush = 1;
  186. }
  187. }
  188. transmit_previous_timeout = 0;
  189. len = SEREMU_TX_SIZE - tx_packet->index;
  190. if (len > size) len = size;
  191. dest = tx_packet->buf + tx_packet->index;
  192. tx_packet->index += len;
  193. size -= len;
  194. while (len-- > 0) *dest++ = *src++;
  195. if (tx_packet->index < SEREMU_TX_SIZE) {
  196. usb_seremu_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  197. } else {
  198. tx_packet->len = SEREMU_TX_SIZE;
  199. usb_seremu_transmit_flush_timer = 0;
  200. usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
  201. tx_packet = NULL;
  202. }
  203. }
  204. tx_noautoflush = 0;
  205. return 0;
  206. #endif
  207. }
  208. int usb_seremu_write_buffer_free(void)
  209. {
  210. uint32_t len;
  211. tx_noautoflush = 1;
  212. if (!tx_packet) {
  213. if (!usb_configuration ||
  214. usb_tx_packet_count(SEREMU_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
  215. (tx_packet = usb_malloc()) == NULL) {
  216. tx_noautoflush = 0;
  217. return 0;
  218. }
  219. }
  220. len = SEREMU_TX_SIZE - tx_packet->index;
  221. tx_noautoflush = 0;
  222. return len;
  223. }
  224. void usb_seremu_flush_output(void)
  225. {
  226. int i;
  227. if (!usb_configuration) return;
  228. //serial_print("usb_serial_flush_output\n");
  229. if (tx_packet && tx_packet->index > 0) {
  230. usb_seremu_transmit_flush_timer = 0;
  231. for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
  232. tx_packet->buf[i] = 0;
  233. }
  234. tx_packet->len = SEREMU_TX_SIZE;
  235. usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
  236. tx_packet = NULL;
  237. }
  238. // while (usb_tx_byte_count(SEREMU_TX_ENDPOINT) > 0) ; // wait
  239. }
  240. void usb_seremu_flush_callback(void)
  241. {
  242. int i;
  243. //serial_print("C");
  244. if (tx_noautoflush) return;
  245. //serial_print("usb_flush_callback \n");
  246. for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
  247. tx_packet->buf[i] = 0;
  248. }
  249. tx_packet->len = SEREMU_TX_SIZE;
  250. usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
  251. tx_packet = NULL;
  252. //serial_print("usb_flush_callback end\n");
  253. }
  254. #endif // SEREMU_INTERFACE
  255. #endif // F_CPU >= 20 MHz