No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

usb_serial.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #if 0
  106. unsigned int i;
  107. int c;
  108. if (!rx_packet) {
  109. if (!usb_configuration) return -1;
  110. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  111. if (!rx_packet) return -1;
  112. }
  113. i = rx_packet->index;
  114. c = rx_packet->buf[i++];
  115. if (i >= rx_packet->len) {
  116. usb_free(rx_packet);
  117. rx_packet = NULL;
  118. } else {
  119. rx_packet->index = i;
  120. }
  121. return c;
  122. #endif
  123. return -1;
  124. }
  125. // peek at the next character, or -1 if nothing received
  126. int usb_serial_peekchar(void)
  127. {
  128. #if 0
  129. if (!rx_packet) {
  130. if (!usb_configuration) return -1;
  131. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  132. if (!rx_packet) return -1;
  133. }
  134. if (!rx_packet) return -1;
  135. return rx_packet->buf[rx_packet->index];
  136. #endif
  137. return -1;
  138. }
  139. // number of bytes available in the receive buffer
  140. int usb_serial_available(void)
  141. {
  142. return rx_count[0] - rx_index[0];
  143. #if 0
  144. int count;
  145. count = usb_rx_byte_count(CDC_RX_ENDPOINT);
  146. if (rx_packet) count += rx_packet->len - rx_packet->index;
  147. return count;
  148. #endif
  149. return 0;
  150. }
  151. // read a block of bytes to a buffer
  152. int usb_serial_read(void *buffer, uint32_t size)
  153. {
  154. #if 0
  155. uint8_t *p = (uint8_t *)buffer;
  156. uint32_t qty, count=0;
  157. while (size) {
  158. if (!usb_configuration) break;
  159. if (!rx_packet) {
  160. rx:
  161. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  162. if (!rx_packet) break;
  163. if (rx_packet->len == 0) {
  164. usb_free(rx_packet);
  165. goto rx;
  166. }
  167. }
  168. qty = rx_packet->len - rx_packet->index;
  169. if (qty > size) qty = size;
  170. memcpy(p, rx_packet->buf + rx_packet->index, qty);
  171. p += qty;
  172. count += qty;
  173. size -= qty;
  174. rx_packet->index += qty;
  175. if (rx_packet->index >= rx_packet->len) {
  176. usb_free(rx_packet);
  177. rx_packet = NULL;
  178. }
  179. }
  180. return count;
  181. #endif
  182. return 0;
  183. }
  184. // discard any buffered input
  185. void usb_serial_flush_input(void)
  186. {
  187. #if 0
  188. usb_packet_t *rx;
  189. if (!usb_configuration) return;
  190. if (rx_packet) {
  191. usb_free(rx_packet);
  192. rx_packet = NULL;
  193. }
  194. while (1) {
  195. rx = usb_rx(CDC_RX_ENDPOINT);
  196. if (!rx) break;
  197. usb_free(rx);
  198. }
  199. #endif
  200. }
  201. // When the PC isn't listening, how long do we wait before discarding data? If this is
  202. // too short, we risk losing data during the stalls that are common with ordinary desktop
  203. // software. If it's too long, we stall the user's program when no software is running.
  204. #define TX_TIMEOUT_MSEC 120
  205. // When we've suffered the transmit timeout, don't wait again until the computer
  206. // begins accepting data. If no software is running to receive, we'll just discard
  207. // data as rapidly as Serial.print() can generate it, until there's something to
  208. // actually receive it.
  209. static uint8_t transmit_previous_timeout=0;
  210. // transmit a character. 0 returned on success, -1 on error
  211. int usb_serial_putchar(uint8_t c)
  212. {
  213. return usb_serial_write(&c, 1);
  214. }
  215. extern volatile uint32_t systick_millis_count;
  216. static void timer_config(void (*callback)(void), uint32_t microseconds);
  217. static void timer_start_oneshot();
  218. static void timer_stop();
  219. static void timer_config(void (*callback)(void), uint32_t microseconds)
  220. {
  221. usb_timer0_callback = callback;
  222. USB1_GPTIMER0CTRL = 0;
  223. USB1_GPTIMER0LD = microseconds - 1;
  224. USB1_USBINTR |= USB_USBINTR_TIE0;
  225. }
  226. static void timer_start_oneshot(void)
  227. {
  228. // restarts timer if already running (retriggerable one-shot)
  229. USB1_GPTIMER0CTRL = USB_GPTIMERCTRL_GPTRUN | USB_GPTIMERCTRL_GPTRST;
  230. }
  231. static void timer_stop(void)
  232. {
  233. USB1_GPTIMER0CTRL = 0;
  234. }
  235. int usb_serial_write(const void *buffer, uint32_t size)
  236. {
  237. uint32_t sent=0;
  238. const uint8_t *data = (const uint8_t *)buffer;
  239. if (!usb_configuration) return 0;
  240. while (size > 0) {
  241. transfer_t *xfer = tx_transfer + tx_head;
  242. int waiting=0;
  243. uint32_t wait_begin_at=0;
  244. while (!tx_available) {
  245. //digitalWriteFast(3, HIGH);
  246. uint32_t status = usb_transfer_status(xfer);
  247. if (!(status & 0x80)) {
  248. if (status & 0x68) {
  249. // TODO: what if status has errors???
  250. printf("ERROR status = %x, i=%d, ms=%u\n",
  251. status, tx_head, systick_millis_count);
  252. }
  253. tx_available = TX_SIZE;
  254. transmit_previous_timeout = 0;
  255. break;
  256. }
  257. if (!waiting) {
  258. wait_begin_at = systick_millis_count;
  259. waiting = 1;
  260. }
  261. if (transmit_previous_timeout) return sent;
  262. if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
  263. // waited too long, assume the USB host isn't listening
  264. transmit_previous_timeout = 1;
  265. return sent;
  266. //printf("\nstop, waited too long\n");
  267. //printf("status = %x\n", status);
  268. //printf("tx head=%d\n", tx_head);
  269. //printf("TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
  270. //usb_print_transfer_log();
  271. //while (1) ;
  272. }
  273. if (!usb_configuration) return sent;
  274. yield();
  275. }
  276. //digitalWriteFast(3, LOW);
  277. uint8_t *txdata = txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available);
  278. if (size >= tx_available) {
  279. memcpy(txdata, data, tx_available);
  280. //*(txbuffer + (tx_head * TX_SIZE)) = 'A' + tx_head; // to see which buffer
  281. //*(txbuffer + (tx_head * TX_SIZE) + 1) = ' '; // really see it
  282. usb_prepare_transfer(xfer, txbuffer + (tx_head * TX_SIZE), TX_SIZE, 0);
  283. usb_transmit(CDC_TX_ENDPOINT, xfer);
  284. if (++tx_head >= TX_NUM) tx_head = 0;
  285. size -= tx_available;
  286. sent += tx_available;
  287. data += tx_available;
  288. tx_available = 0;
  289. timer_stop();
  290. } else {
  291. memcpy(txdata, data, size);
  292. tx_available -= size;
  293. sent += size;
  294. size = 0;
  295. timer_start_oneshot();
  296. }
  297. }
  298. return sent;
  299. }
  300. int usb_serial_write_buffer_free(void)
  301. {
  302. #if 0
  303. uint32_t len;
  304. tx_noautoflush = 1;
  305. if (!tx_packet) {
  306. if (!usb_configuration ||
  307. usb_tx_packet_count(CDC_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
  308. (tx_packet = usb_malloc()) == NULL) {
  309. tx_noautoflush = 0;
  310. return 0;
  311. }
  312. }
  313. len = CDC_TX_SIZE - tx_packet->index;
  314. // TODO: Perhaps we need "usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT"
  315. // added here, so the SOF interrupt can't take away the available buffer
  316. // space we just promised the user could write without blocking?
  317. // But does this come with other performance downsides? Could it lead to
  318. // buffer data never actually transmitting in some usage cases? More
  319. // investigation is needed.
  320. // https://github.com/PaulStoffregen/cores/issues/10#issuecomment-61514955
  321. tx_noautoflush = 0;
  322. return len;
  323. #endif
  324. return 0;
  325. }
  326. void usb_serial_flush_output(void)
  327. {
  328. if (!usb_configuration) return;
  329. if (tx_available == 0) return;
  330. tx_noautoflush = 1;
  331. transfer_t *xfer = tx_transfer + tx_head;
  332. usb_prepare_transfer(xfer, txbuffer + (tx_head * TX_SIZE), TX_SIZE - tx_available, 0);
  333. usb_transmit(CDC_TX_ENDPOINT, xfer);
  334. if (++tx_head >= TX_NUM) tx_head = 0;
  335. tx_available = 0;
  336. tx_noautoflush = 0;
  337. }
  338. static void usb_serial_flush_callback(void)
  339. {
  340. if (tx_noautoflush) return;
  341. if (!usb_configuration) return;
  342. if (tx_available == 0) return;
  343. //printf("flush callback, %d bytes\n", TX_SIZE - tx_available);
  344. transfer_t *xfer = tx_transfer + tx_head;
  345. usb_prepare_transfer(xfer, txbuffer + (tx_head * TX_SIZE), TX_SIZE - tx_available, 0);
  346. usb_transmit(CDC_TX_ENDPOINT, xfer);
  347. if (++tx_head >= TX_NUM) tx_head = 0;
  348. tx_available = 0;
  349. }
  350. //#endif // F_CPU
  351. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE