您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
  36. #include "debug/printf.h"
  37. #include "core_pins.h"
  38. // defined by usb_dev.h -> usb_desc.h
  39. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  40. //#if F_CPU >= 20000000
  41. uint32_t usb_cdc_line_coding[2];
  42. volatile uint32_t usb_cdc_line_rtsdtr_millis;
  43. volatile uint8_t usb_cdc_line_rtsdtr=0;
  44. volatile uint8_t usb_cdc_transmit_flush_timer=0;
  45. static volatile uint8_t tx_noautoflush=0;
  46. extern volatile uint8_t usb_high_speed;
  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 4
  54. #define TX_SIZE 2048 /* should be a multiple of CDC_TX_SIZE */
  55. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  56. DMAMEM static uint8_t txbuffer[TX_SIZE * TX_NUM];
  57. static uint8_t tx_head=0;
  58. static uint16_t tx_available=0;
  59. static uint16_t tx_packet_size=0;
  60. #define RX_NUM 3
  61. static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
  62. static uint8_t rx_buffer[RX_NUM * CDC_RX_SIZE_480];
  63. static uint16_t rx_count[RX_NUM];
  64. static uint16_t rx_index[RX_NUM];
  65. static uint16_t rx_packet_size=0;
  66. static volatile uint8_t rx_head;
  67. static volatile uint8_t rx_tail;
  68. static uint8_t rx_list[RX_NUM + 1];
  69. static volatile uint32_t rx_available;
  70. static void rx_queue_transfer(int i);
  71. static void rx_event(transfer_t *t);
  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. int i;
  80. printf("usb_serial_configure\n");
  81. if (usb_high_speed) {
  82. tx_packet_size = CDC_TX_SIZE_480;
  83. rx_packet_size = CDC_RX_SIZE_480;
  84. } else {
  85. tx_packet_size = CDC_TX_SIZE_12;
  86. rx_packet_size = CDC_RX_SIZE_12;
  87. }
  88. memset(tx_transfer, 0, sizeof(tx_transfer));
  89. tx_head = 0;
  90. tx_available = 0;
  91. memset(rx_transfer, 0, sizeof(rx_transfer));
  92. memset(rx_count, 0, sizeof(rx_count));
  93. memset(rx_index, 0, sizeof(rx_index));
  94. rx_head = 0;
  95. rx_tail = 0;
  96. rx_available = 0;
  97. usb_config_tx(CDC_ACM_ENDPOINT, CDC_ACM_SIZE, 0, NULL); // size same 12 & 480
  98. usb_config_rx(CDC_RX_ENDPOINT, rx_packet_size, 0, rx_event);
  99. usb_config_tx(CDC_TX_ENDPOINT, tx_packet_size, 1, NULL);
  100. for (i=0; i < RX_NUM; i++) rx_queue_transfer(i);
  101. timer_config(usb_serial_flush_callback, TRANSMIT_FLUSH_TIMEOUT);
  102. }
  103. /*************************************************************************/
  104. /** Receive **/
  105. /*************************************************************************/
  106. static void rx_queue_transfer(int i)
  107. {
  108. NVIC_DISABLE_IRQ(IRQ_USB1);
  109. printf("rx queue i=%d\n", i);
  110. usb_prepare_transfer(rx_transfer + i, rx_buffer + i * CDC_RX_SIZE_480, rx_packet_size, i);
  111. usb_receive(CDC_RX_ENDPOINT, rx_transfer + i);
  112. NVIC_ENABLE_IRQ(IRQ_USB1);
  113. }
  114. // called by USB interrupt when any packet is received
  115. static void rx_event(transfer_t *t)
  116. {
  117. int len = rx_packet_size - ((t->status >> 16) & 0x7FFF);
  118. int i = t->callback_param;
  119. printf("rx event, len=%d, i=%d\n", len, i);
  120. if (len > 0) {
  121. // received a packet with data
  122. uint32_t head = rx_head;
  123. if (head != rx_tail) {
  124. // a previous packet is still buffered
  125. uint32_t ii = rx_list[head];
  126. uint32_t count = rx_count[ii];
  127. if (len <= CDC_RX_SIZE_480 - count) {
  128. // previous buffer has enough free space for this packet's data
  129. memcpy(rx_buffer + ii * CDC_RX_SIZE_480 + count,
  130. rx_buffer + i * CDC_RX_SIZE_480, len);
  131. rx_count[ii] = count + len;
  132. rx_available += len;
  133. rx_queue_transfer(i);
  134. // TODO: trigger serialEvent
  135. return;
  136. }
  137. }
  138. // add this packet to rx_list
  139. rx_count[i] = len;
  140. rx_index[i] = 0;
  141. if (++head > RX_NUM) head = 0;
  142. rx_list[head] = i;
  143. rx_head = head;
  144. rx_available += len;
  145. // TODO: trigger serialEvent
  146. } else {
  147. // received a zero length packet
  148. rx_queue_transfer(i);
  149. }
  150. }
  151. //static int maxtimes=0;
  152. // read a block of bytes to a buffer
  153. int usb_serial_read(void *buffer, uint32_t size)
  154. {
  155. uint8_t *p = (uint8_t *)buffer;
  156. uint32_t count=0;
  157. NVIC_DISABLE_IRQ(IRQ_USB1);
  158. //if (++maxtimes > 15) while (1) ;
  159. uint32_t tail = rx_tail;
  160. //printf("usb_serial_read, size=%d, tail=%d, head=%d\n", size, tail, rx_head);
  161. while (count < size && tail != rx_head) {
  162. if (++tail > RX_NUM) tail = 0;
  163. uint32_t i = rx_list[tail];
  164. uint32_t len = size - count;
  165. uint32_t avail = rx_count[i] - rx_index[i];
  166. //printf("usb_serial_read, count=%d, size=%d, i=%d, index=%d, len=%d, avail=%d, c=%c\n",
  167. //count, size, i, rx_index[i], len, avail, rx_buffer[i * CDC_RX_SIZE_480]);
  168. if (avail > len) {
  169. // partially consume this packet
  170. memcpy(p, rx_buffer + i * CDC_RX_SIZE_480 + rx_index[i], len);
  171. rx_available -= len;
  172. rx_index[i] += len;
  173. count += len;
  174. } else {
  175. // fully consume this packet
  176. memcpy(p, rx_buffer + i * CDC_RX_SIZE_480 + rx_index[i], avail);
  177. p += avail;
  178. rx_available -= avail;
  179. count += avail;
  180. rx_tail = tail;
  181. rx_queue_transfer(i);
  182. }
  183. }
  184. NVIC_ENABLE_IRQ(IRQ_USB1);
  185. return count;
  186. }
  187. // peek at the next character, or -1 if nothing received
  188. int usb_serial_peekchar(void)
  189. {
  190. uint32_t tail = rx_tail;
  191. if (tail == rx_head) return -1;
  192. if (++tail > RX_NUM) tail = 0;
  193. uint32_t i = rx_list[tail];
  194. return rx_buffer[i * CDC_RX_SIZE_480 + rx_index[i]];
  195. }
  196. // number of bytes available in the receive buffer
  197. int usb_serial_available(void)
  198. {
  199. return rx_available;
  200. }
  201. // discard any buffered input
  202. void usb_serial_flush_input(void)
  203. {
  204. uint32_t tail = rx_tail;
  205. while (tail != rx_head) {
  206. if (++tail > RX_NUM) tail = 0;
  207. uint32_t i = rx_list[tail];
  208. rx_available -= rx_count[i] - rx_index[i];
  209. rx_queue_transfer(i);
  210. rx_tail = tail;
  211. }
  212. }
  213. // get the next character, or -1 if nothing received
  214. int usb_serial_getchar(void)
  215. {
  216. uint8_t c;
  217. if (usb_serial_read(&c, 1)) return c;
  218. return -1;
  219. }
  220. /*************************************************************************/
  221. /** Transmit **/
  222. /*************************************************************************/
  223. // When the PC isn't listening, how long do we wait before discarding data? If this is
  224. // too short, we risk losing data during the stalls that are common with ordinary desktop
  225. // software. If it's too long, we stall the user's program when no software is running.
  226. #define TX_TIMEOUT_MSEC 120
  227. // When we've suffered the transmit timeout, don't wait again until the computer
  228. // begins accepting data. If no software is running to receive, we'll just discard
  229. // data as rapidly as Serial.print() can generate it, until there's something to
  230. // actually receive it.
  231. static uint8_t transmit_previous_timeout=0;
  232. // transmit a character. 0 returned on success, -1 on error
  233. int usb_serial_putchar(uint8_t c)
  234. {
  235. return usb_serial_write(&c, 1);
  236. }
  237. extern volatile uint32_t systick_millis_count;
  238. static void timer_config(void (*callback)(void), uint32_t microseconds);
  239. static void timer_start_oneshot();
  240. static void timer_stop();
  241. static void timer_config(void (*callback)(void), uint32_t microseconds)
  242. {
  243. usb_timer0_callback = callback;
  244. USB1_GPTIMER0CTRL = 0;
  245. USB1_GPTIMER0LD = microseconds - 1;
  246. USB1_USBINTR |= USB_USBINTR_TIE0;
  247. }
  248. static void timer_start_oneshot(void)
  249. {
  250. // restarts timer if already running (retriggerable one-shot)
  251. USB1_GPTIMER0CTRL = USB_GPTIMERCTRL_GPTRUN | USB_GPTIMERCTRL_GPTRST;
  252. }
  253. static void timer_stop(void)
  254. {
  255. USB1_GPTIMER0CTRL = 0;
  256. }
  257. int usb_serial_write(const void *buffer, uint32_t size)
  258. {
  259. uint32_t sent=0;
  260. const uint8_t *data = (const uint8_t *)buffer;
  261. if (!usb_configuration) return 0;
  262. while (size > 0) {
  263. transfer_t *xfer = tx_transfer + tx_head;
  264. int waiting=0;
  265. uint32_t wait_begin_at=0;
  266. while (!tx_available) {
  267. //digitalWriteFast(3, HIGH);
  268. uint32_t status = usb_transfer_status(xfer);
  269. if (!(status & 0x80)) {
  270. if (status & 0x68) {
  271. // TODO: what if status has errors???
  272. printf("ERROR status = %x, i=%d, ms=%u\n",
  273. status, tx_head, systick_millis_count);
  274. }
  275. tx_available = TX_SIZE;
  276. transmit_previous_timeout = 0;
  277. break;
  278. }
  279. if (!waiting) {
  280. wait_begin_at = systick_millis_count;
  281. waiting = 1;
  282. }
  283. if (transmit_previous_timeout) return sent;
  284. if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
  285. // waited too long, assume the USB host isn't listening
  286. transmit_previous_timeout = 1;
  287. return sent;
  288. //printf("\nstop, waited too long\n");
  289. //printf("status = %x\n", status);
  290. //printf("tx head=%d\n", tx_head);
  291. //printf("TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
  292. //usb_print_transfer_log();
  293. //while (1) ;
  294. }
  295. if (!usb_configuration) return sent;
  296. yield();
  297. }
  298. //digitalWriteFast(3, LOW);
  299. uint8_t *txdata = txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available);
  300. if (size >= tx_available) {
  301. memcpy(txdata, data, tx_available);
  302. //*(txbuffer + (tx_head * TX_SIZE)) = 'A' + tx_head; // to see which buffer
  303. //*(txbuffer + (tx_head * TX_SIZE) + 1) = ' '; // really see it
  304. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  305. usb_prepare_transfer(xfer, txbuf, TX_SIZE, 0);
  306. arm_dcache_flush_delete(txbuf, TX_SIZE);
  307. usb_transmit(CDC_TX_ENDPOINT, xfer);
  308. if (++tx_head >= TX_NUM) tx_head = 0;
  309. size -= tx_available;
  310. sent += tx_available;
  311. data += tx_available;
  312. tx_available = 0;
  313. timer_stop();
  314. } else {
  315. memcpy(txdata, data, size);
  316. tx_available -= size;
  317. sent += size;
  318. size = 0;
  319. timer_start_oneshot();
  320. }
  321. }
  322. return sent;
  323. }
  324. int usb_serial_write_buffer_free(void)
  325. {
  326. uint32_t sum = 0;
  327. tx_noautoflush = 1;
  328. for (uint32_t i=0; i < TX_NUM; i++) {
  329. if (i == tx_head) continue;
  330. if (!(usb_transfer_status(tx_transfer + i) & 0x80)) sum += TX_SIZE;
  331. }
  332. tx_noautoflush = 0;
  333. return sum;
  334. }
  335. void usb_serial_flush_output(void)
  336. {
  337. if (!usb_configuration) return;
  338. if (tx_available == 0) return;
  339. tx_noautoflush = 1;
  340. transfer_t *xfer = tx_transfer + tx_head;
  341. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  342. uint32_t txnum = TX_SIZE - tx_available;
  343. usb_prepare_transfer(xfer, txbuf, txnum, 0);
  344. arm_dcache_flush_delete(txbuf, txnum);
  345. usb_transmit(CDC_TX_ENDPOINT, xfer);
  346. if (++tx_head >= TX_NUM) tx_head = 0;
  347. tx_available = 0;
  348. tx_noautoflush = 0;
  349. }
  350. static void usb_serial_flush_callback(void)
  351. {
  352. if (tx_noautoflush) return;
  353. if (!usb_configuration) return;
  354. if (tx_available == 0) return;
  355. //printf("flush callback, %d bytes\n", TX_SIZE - tx_available);
  356. transfer_t *xfer = tx_transfer + tx_head;
  357. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  358. uint32_t txnum = TX_SIZE - tx_available;
  359. usb_prepare_transfer(xfer, txbuf, txnum, 0);
  360. arm_dcache_flush_delete(txbuf, txnum);
  361. usb_transmit(CDC_TX_ENDPOINT, xfer);
  362. if (++tx_head >= TX_NUM) tx_head = 0;
  363. tx_available = 0;
  364. }
  365. //#endif // F_CPU
  366. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE