Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

440 Zeilen
13KB

  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(CDC3_STATUS_INTERFACE) && defined(CDC3_DATA_INTERFACE)
  40. uint32_t usb_cdc3_line_coding[2];
  41. volatile uint32_t usb_cdc3_line_rtsdtr_millis;
  42. volatile uint8_t usb_cdc3_line_rtsdtr=0;
  43. volatile uint8_t usb_cdc3_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_serial3_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_serial3_configure(void)
  72. {
  73. int i;
  74. printf("usb_serial3_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(CDC3_ACM_ENDPOINT, CDC_ACM_SIZE, 0, NULL); // size same 12 & 480
  92. usb_config_rx(CDC3_RX_ENDPOINT, rx_packet_size, 0, rx_event);
  93. usb_config_tx(CDC3_TX_ENDPOINT, tx_packet_size, 1, NULL);
  94. for (i=0; i < RX_NUM; i++) rx_queue_transfer(i);
  95. timer_config(usb_serial3_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(CDC3_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_serial3_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_serial3_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_serial3_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_serial3_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_serial3_available(void)
  194. {
  195. return rx_available;
  196. }
  197. // discard any buffered input
  198. void usb_serial3_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_serial3_getchar(void)
  211. {
  212. uint8_t c;
  213. if (usb_serial3_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_serial3_putchar(uint8_t c)
  230. {
  231. return usb_serial3_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 quadtimer_isr(void)
  238. {
  239. TMR1_SCTRL3 = 0;
  240. usb_serial3_flush_callback();
  241. }
  242. static void timer_config(void (*callback)(void), uint32_t microseconds)
  243. {
  244. // Both USB timers are already used with USB_TRIPLE_SERIAL
  245. // Ideally we should have a software layer to extend the timers
  246. // to any many USB interfaces as required. But until that
  247. // glorious future, as a quick hack we will commandeer Quad
  248. // Timer #1 channel 3 (which isn't used by any PWM pin) and
  249. // hope no other software needs it or it's interrupt. The
  250. // other 3 channels of Quad Timer #1 are normally used for PWM
  251. // which doesn't use interrupts, so this is (probably) the
  252. // safest Quad Timer channel to commandeer for an interrupt
  253. // that the other 3 channels (hopefully) won't use.
  254. TMR1_CTRL3 = 0;
  255. TMR1_SCTRL3 = 0;
  256. attachInterruptVector(IRQ_QTIMER1, quadtimer_isr);
  257. NVIC_ENABLE_IRQ(IRQ_QTIMER1);
  258. // kludge - both inputs ignored and hard-coded into other functions
  259. }
  260. static void timer_start_oneshot(void)
  261. {
  262. TMR1_CTRL3 = 0;
  263. TMR1_CNTR3 = 0;
  264. TMR1_COMP13 = TRANSMIT_FLUSH_TIMEOUT * (F_BUS_ACTUAL >> 10) / (16000000 >> 10);
  265. TMR1_SCTRL3 = TMR_SCTRL_TCFIE;
  266. TMR1_CTRL3 = TMR_CTRL_CM(1) | TMR_CTRL_PCS(12) | TMR_CTRL_ONCE;
  267. }
  268. static void timer_stop(void)
  269. {
  270. TMR1_CTRL3 = 0;
  271. }
  272. int usb_serial3_write(const void *buffer, uint32_t size)
  273. {
  274. uint32_t sent=0;
  275. const uint8_t *data = (const uint8_t *)buffer;
  276. if (!usb_configuration) return 0;
  277. while (size > 0) {
  278. transfer_t *xfer = tx_transfer + tx_head;
  279. int waiting=0;
  280. uint32_t wait_begin_at=0;
  281. while (!tx_available) {
  282. //digitalWriteFast(3, HIGH);
  283. uint32_t status = usb_transfer_status(xfer);
  284. if (!(status & 0x80)) {
  285. if (status & 0x68) {
  286. // TODO: what if status has errors???
  287. printf("ERROR status = %x, i=%d, ms=%u\n",
  288. status, tx_head, systick_millis_count);
  289. }
  290. tx_available = TX_SIZE;
  291. transmit_previous_timeout = 0;
  292. break;
  293. }
  294. if (!waiting) {
  295. wait_begin_at = systick_millis_count;
  296. waiting = 1;
  297. }
  298. if (transmit_previous_timeout) return sent;
  299. if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
  300. // waited too long, assume the USB host isn't listening
  301. transmit_previous_timeout = 1;
  302. return sent;
  303. //printf("\nstop, waited too long\n");
  304. //printf("status = %x\n", status);
  305. //printf("tx head=%d\n", tx_head);
  306. //printf("TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
  307. //usb_print_transfer_log();
  308. //while (1) ;
  309. }
  310. if (!usb_configuration) return sent;
  311. yield();
  312. }
  313. //digitalWriteFast(3, LOW);
  314. uint8_t *txdata = txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available);
  315. if (size >= tx_available) {
  316. memcpy(txdata, data, tx_available);
  317. //*(txbuffer + (tx_head * TX_SIZE)) = 'A' + tx_head; // to see which buffer
  318. //*(txbuffer + (tx_head * TX_SIZE) + 1) = ' '; // really see it
  319. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  320. usb_prepare_transfer(xfer, txbuf, TX_SIZE, 0);
  321. arm_dcache_flush_delete(txbuf, TX_SIZE);
  322. usb_transmit(CDC3_TX_ENDPOINT, xfer);
  323. if (++tx_head >= TX_NUM) tx_head = 0;
  324. size -= tx_available;
  325. sent += tx_available;
  326. data += tx_available;
  327. tx_available = 0;
  328. timer_stop();
  329. } else {
  330. memcpy(txdata, data, size);
  331. tx_available -= size;
  332. sent += size;
  333. size = 0;
  334. timer_start_oneshot();
  335. }
  336. }
  337. return sent;
  338. }
  339. int usb_serial3_write_buffer_free(void)
  340. {
  341. uint32_t sum = 0;
  342. tx_noautoflush = 1;
  343. for (uint32_t i=0; i < TX_NUM; i++) {
  344. if (i == tx_head) continue;
  345. if (!(usb_transfer_status(tx_transfer + i) & 0x80)) sum += TX_SIZE;
  346. }
  347. tx_noautoflush = 0;
  348. return sum;
  349. }
  350. void usb_serial3_flush_output(void)
  351. {
  352. if (!usb_configuration) return;
  353. if (tx_available == 0) return;
  354. tx_noautoflush = 1;
  355. transfer_t *xfer = tx_transfer + tx_head;
  356. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  357. uint32_t txnum = TX_SIZE - tx_available;
  358. usb_prepare_transfer(xfer, txbuf, txnum, 0);
  359. arm_dcache_flush_delete(txbuf, txnum);
  360. usb_transmit(CDC3_TX_ENDPOINT, xfer);
  361. if (++tx_head >= TX_NUM) tx_head = 0;
  362. tx_available = 0;
  363. tx_noautoflush = 0;
  364. }
  365. static void usb_serial3_flush_callback(void)
  366. {
  367. if (tx_noautoflush) return;
  368. if (!usb_configuration) return;
  369. if (tx_available == 0) return;
  370. //printf("flush callback, %d bytes\n", TX_SIZE - tx_available);
  371. transfer_t *xfer = tx_transfer + tx_head;
  372. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  373. uint32_t txnum = TX_SIZE - tx_available;
  374. usb_prepare_transfer(xfer, txbuf, txnum, 0);
  375. arm_dcache_flush_delete(txbuf, txnum);
  376. usb_transmit(CDC3_TX_ENDPOINT, xfer);
  377. if (++tx_head >= TX_NUM) tx_head = 0;
  378. tx_available = 0;
  379. }
  380. #endif // CDC3_STATUS_INTERFACE && CDC3_DATA_INTERFACE