Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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