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.

usb_serial.c 13KB

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