You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 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. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  190. //#define TX_PACKET_LIMIT 8
  191. // When the PC isn't listening, how long do we wait before discarding data? If this is
  192. // too short, we risk losing data during the stalls that are common with ordinary desktop
  193. // software. If it's too long, we stall the user's program when no software is running.
  194. #define TX_TIMEOUT_MSEC 70
  195. /*#if F_CPU == 240000000
  196. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
  197. #elif F_CPU == 216000000
  198. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
  199. #elif F_CPU == 192000000
  200. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  201. #elif F_CPU == 180000000
  202. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  203. #elif F_CPU == 168000000
  204. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  205. #elif F_CPU == 144000000
  206. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  207. #elif F_CPU == 120000000
  208. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  209. #elif F_CPU == 96000000
  210. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  211. #elif F_CPU == 72000000
  212. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  213. #elif F_CPU == 48000000
  214. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  215. #elif F_CPU == 24000000
  216. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  217. #endif */
  218. // When we've suffered the transmit timeout, don't wait again until the computer
  219. // begins accepting data. If no software is running to receive, we'll just discard
  220. // data as rapidly as Serial.print() can generate it, until there's something to
  221. // actually receive it.
  222. //static uint8_t transmit_previous_timeout=0;
  223. // transmit a character. 0 returned on success, -1 on error
  224. int usb_serial_putchar(uint8_t c)
  225. {
  226. return usb_serial_write(&c, 1);
  227. }
  228. #define TX_NUM 7
  229. #define TX_SIZE 256 /* should be a multiple of CDC_TX_SIZE */
  230. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  231. static uint8_t txbuffer[TX_SIZE * TX_NUM];
  232. static uint8_t tx_head=0;
  233. static uint16_t tx_available=0;
  234. extern volatile uint32_t systick_millis_count;
  235. static void timer_config(void (*callback)(void), uint32_t microseconds);
  236. static void timer_start_oneshot();
  237. static void timer_stop();
  238. static void timer_config(void (*callback)(void), uint32_t microseconds)
  239. {
  240. usb_timer0_callback = callback;
  241. USB1_GPTIMER0CTRL = 0;
  242. USB1_GPTIMER0LD = microseconds - 1;
  243. USB1_USBINTR |= USB_USBINTR_TIE0;
  244. }
  245. static void timer_start_oneshot(void)
  246. {
  247. // restarts timer if already running (retriggerable one-shot)
  248. USB1_GPTIMER0CTRL = USB_GPTIMERCTRL_GPTRUN | USB_GPTIMERCTRL_GPTRST;
  249. }
  250. static void timer_stop(void)
  251. {
  252. USB1_GPTIMER0CTRL = 0;
  253. }
  254. int usb_serial_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 & 0x7F) {
  267. printf("ERROR status = %x, i=%d, ms=%u\n", status, tx_head, systick_millis_count);
  268. }
  269. if (!(status & 0x80)) {
  270. // TODO: what if status has errors???
  271. tx_available = TX_SIZE;
  272. break;
  273. }/* else {
  274. if (!waiting) {
  275. wait_begin_at = systick_millis_count;
  276. waiting = 1;
  277. } else {
  278. if (systick_millis_count - wait_begin_at > 250) {
  279. printf("\nstop, waited too long\n");
  280. printf("status = %x\n", status);
  281. printf("tx head=%d\n", tx_head);
  282. printf("TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
  283. usb_print_transfer_log();
  284. while (1) ;
  285. }
  286. }
  287. } */
  288. // TODO: proper timout?
  289. // TODO: check for USB offline
  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