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.

421 lines
12KB

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