Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

388 lines
11KB

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