Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

6 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #include "usb_dev.h"
  31. #include "usb_serial.h"
  32. #include "core_pins.h"// for delay()
  33. //#include "HardwareSerial.h"
  34. #include <string.h> // for memcpy()
  35. #include "debug/printf.h"
  36. #include "core_pins.h"
  37. // defined by usb_dev.h -> usb_desc.h
  38. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  39. //#if F_CPU >= 20000000
  40. uint32_t usb_cdc_line_coding[2];
  41. volatile uint32_t usb_cdc_line_rtsdtr_millis;
  42. volatile uint8_t usb_cdc_line_rtsdtr=0;
  43. volatile uint8_t usb_cdc_transmit_flush_timer=0;
  44. //static usb_packet_t *rx_packet=NULL;
  45. //static usb_packet_t *tx_packet=NULL;
  46. static volatile uint8_t tx_noautoflush=0;
  47. // TODO: should be 2 different timeouts, high speed (480) vs full speed (12)
  48. #define TRANSMIT_FLUSH_TIMEOUT 75 /* in microseconds */
  49. static void timer_config(void (*callback)(void), uint32_t microseconds);
  50. static void timer_start_oneshot();
  51. static void timer_stop();
  52. static void usb_serial_flush_callback(void);
  53. #define TX_NUM 7
  54. #define TX_SIZE 256 /* should be a multiple of CDC_TX_SIZE */
  55. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  56. static uint8_t txbuffer[TX_SIZE * TX_NUM];
  57. static uint8_t tx_head=0;
  58. static uint16_t tx_available=0;
  59. #define RX_NUM 3
  60. static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
  61. static uint8_t rx_buffer[RX_NUM * CDC_RX_SIZE];
  62. static uint16_t rx_count[RX_NUM];
  63. static uint16_t rx_index[RX_NUM];
  64. static void rx_event(transfer_t *t)
  65. {
  66. int len = CDC_RX_SIZE - ((t->status >> 16) & 0x7FFF);
  67. int index = t->callback_param;
  68. //printf("rx event, len=%d, i=%d\n", len, index);
  69. rx_count[index] = len;
  70. rx_index[index] = 0;
  71. }
  72. void usb_serial_reset(void)
  73. {
  74. printf("usb_serial_reset\n");
  75. // deallocate all transfer descriptors
  76. }
  77. void usb_serial_configure(void)
  78. {
  79. printf("usb_serial_configure\n");
  80. memset(tx_transfer, 0, sizeof(tx_transfer));
  81. tx_head = 0;
  82. tx_available = 0;
  83. memset(rx_transfer, 0, sizeof(rx_transfer));
  84. memset(rx_count, 0, sizeof(rx_count));
  85. memset(rx_index, 0, sizeof(rx_index));
  86. usb_config_tx(CDC_ACM_ENDPOINT, CDC_ACM_SIZE, 0, NULL);
  87. usb_config_rx(CDC_RX_ENDPOINT, CDC_RX_SIZE, 0, rx_event);
  88. usb_config_tx(CDC_TX_ENDPOINT, CDC_TX_SIZE, 0, NULL);
  89. usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, CDC_RX_SIZE, 0);
  90. usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
  91. timer_config(usb_serial_flush_callback, TRANSMIT_FLUSH_TIMEOUT);
  92. }
  93. // get the next character, or -1 if nothing received
  94. int usb_serial_getchar(void)
  95. {
  96. if (rx_index[0] < rx_count[0]) {
  97. int c = rx_buffer[rx_index[0]++];
  98. if (rx_index[0] >= rx_count[0]) {
  99. // reschedule transfer
  100. usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, CDC_RX_SIZE, 0);
  101. usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
  102. }
  103. return c;
  104. }
  105. return -1;
  106. }
  107. // peek at the next character, or -1 if nothing received
  108. int usb_serial_peekchar(void)
  109. {
  110. if (rx_index[0] < rx_count[0]) {
  111. return rx_buffer[rx_index[0]];
  112. }
  113. return -1;
  114. }
  115. // number of bytes available in the receive buffer
  116. int usb_serial_available(void)
  117. {
  118. return rx_count[0] - rx_index[0];
  119. }
  120. // read a block of bytes to a buffer
  121. int usb_serial_read(void *buffer, uint32_t size)
  122. {
  123. // Quick and dirty to make it at least limp...
  124. uint8_t *p = (uint8_t *)buffer;
  125. uint32_t count=0;
  126. while (size) {
  127. int ch = usb_serial_getchar();
  128. if (ch == -1) break;
  129. *p++ = (uint8_t)ch;
  130. size--;
  131. count++;
  132. }
  133. return count;
  134. }
  135. // discard any buffered input
  136. void usb_serial_flush_input(void)
  137. {
  138. if (rx_index[0] < rx_count[0]) {
  139. rx_index[0] = rx_count[0];
  140. usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, CDC_RX_SIZE, 0);
  141. usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
  142. }
  143. }
  144. // When the PC isn't listening, how long do we wait before discarding data? If this is
  145. // too short, we risk losing data during the stalls that are common with ordinary desktop
  146. // software. If it's too long, we stall the user's program when no software is running.
  147. #define TX_TIMEOUT_MSEC 120
  148. // When we've suffered the transmit timeout, don't wait again until the computer
  149. // begins accepting data. If no software is running to receive, we'll just discard
  150. // data as rapidly as Serial.print() can generate it, until there's something to
  151. // actually receive it.
  152. static uint8_t transmit_previous_timeout=0;
  153. // transmit a character. 0 returned on success, -1 on error
  154. int usb_serial_putchar(uint8_t c)
  155. {
  156. return usb_serial_write(&c, 1);
  157. }
  158. extern volatile uint32_t systick_millis_count;
  159. static void timer_config(void (*callback)(void), uint32_t microseconds);
  160. static void timer_start_oneshot();
  161. static void timer_stop();
  162. static void timer_config(void (*callback)(void), uint32_t microseconds)
  163. {
  164. usb_timer0_callback = callback;
  165. USB1_GPTIMER0CTRL = 0;
  166. USB1_GPTIMER0LD = microseconds - 1;
  167. USB1_USBINTR |= USB_USBINTR_TIE0;
  168. }
  169. static void timer_start_oneshot(void)
  170. {
  171. // restarts timer if already running (retriggerable one-shot)
  172. USB1_GPTIMER0CTRL = USB_GPTIMERCTRL_GPTRUN | USB_GPTIMERCTRL_GPTRST;
  173. }
  174. static void timer_stop(void)
  175. {
  176. USB1_GPTIMER0CTRL = 0;
  177. }
  178. int usb_serial_write(const void *buffer, uint32_t size)
  179. {
  180. uint32_t sent=0;
  181. const uint8_t *data = (const uint8_t *)buffer;
  182. if (!usb_configuration) return 0;
  183. while (size > 0) {
  184. transfer_t *xfer = tx_transfer + tx_head;
  185. int waiting=0;
  186. uint32_t wait_begin_at=0;
  187. while (!tx_available) {
  188. //digitalWriteFast(3, HIGH);
  189. uint32_t status = usb_transfer_status(xfer);
  190. if (!(status & 0x80)) {
  191. if (status & 0x68) {
  192. // TODO: what if status has errors???
  193. printf("ERROR status = %x, i=%d, ms=%u\n",
  194. status, tx_head, systick_millis_count);
  195. }
  196. tx_available = TX_SIZE;
  197. transmit_previous_timeout = 0;
  198. break;
  199. }
  200. if (!waiting) {
  201. wait_begin_at = systick_millis_count;
  202. waiting = 1;
  203. }
  204. if (transmit_previous_timeout) return sent;
  205. if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
  206. // waited too long, assume the USB host isn't listening
  207. transmit_previous_timeout = 1;
  208. return sent;
  209. //printf("\nstop, waited too long\n");
  210. //printf("status = %x\n", status);
  211. //printf("tx head=%d\n", tx_head);
  212. //printf("TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
  213. //usb_print_transfer_log();
  214. //while (1) ;
  215. }
  216. if (!usb_configuration) return sent;
  217. yield();
  218. }
  219. //digitalWriteFast(3, LOW);
  220. uint8_t *txdata = txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available);
  221. if (size >= tx_available) {
  222. memcpy(txdata, data, tx_available);
  223. //*(txbuffer + (tx_head * TX_SIZE)) = 'A' + tx_head; // to see which buffer
  224. //*(txbuffer + (tx_head * TX_SIZE) + 1) = ' '; // really see it
  225. usb_prepare_transfer(xfer, txbuffer + (tx_head * TX_SIZE), TX_SIZE, 0);
  226. usb_transmit(CDC_TX_ENDPOINT, xfer);
  227. if (++tx_head >= TX_NUM) tx_head = 0;
  228. size -= tx_available;
  229. sent += tx_available;
  230. data += tx_available;
  231. tx_available = 0;
  232. timer_stop();
  233. } else {
  234. memcpy(txdata, data, size);
  235. tx_available -= size;
  236. sent += size;
  237. size = 0;
  238. timer_start_oneshot();
  239. }
  240. }
  241. return sent;
  242. }
  243. int usb_serial_write_buffer_free(void)
  244. {
  245. uint32_t sum = 0;
  246. tx_noautoflush = 1;
  247. for (uint32_t i=0; i < TX_NUM; i++) {
  248. if (i == tx_head) continue;
  249. if (!(usb_transfer_status(tx_transfer + i) & 0x80)) sum += TX_SIZE;
  250. }
  251. tx_noautoflush = 0;
  252. return sum;
  253. }
  254. void usb_serial_flush_output(void)
  255. {
  256. if (!usb_configuration) return;
  257. if (tx_available == 0) return;
  258. tx_noautoflush = 1;
  259. transfer_t *xfer = tx_transfer + tx_head;
  260. usb_prepare_transfer(xfer, txbuffer + (tx_head * TX_SIZE), TX_SIZE - tx_available, 0);
  261. usb_transmit(CDC_TX_ENDPOINT, xfer);
  262. if (++tx_head >= TX_NUM) tx_head = 0;
  263. tx_available = 0;
  264. tx_noautoflush = 0;
  265. }
  266. static void usb_serial_flush_callback(void)
  267. {
  268. if (tx_noautoflush) return;
  269. if (!usb_configuration) return;
  270. if (tx_available == 0) return;
  271. //printf("flush callback, %d bytes\n", TX_SIZE - tx_available);
  272. transfer_t *xfer = tx_transfer + tx_head;
  273. usb_prepare_transfer(xfer, txbuffer + (tx_head * TX_SIZE), TX_SIZE - tx_available, 0);
  274. usb_transmit(CDC_TX_ENDPOINT, xfer);
  275. if (++tx_head >= TX_NUM) tx_head = 0;
  276. tx_available = 0;
  277. }
  278. //#endif // F_CPU
  279. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE