Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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