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 12KB

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