Teensy 4.1 core updated for C++20
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.

337 lines
9.9KB

  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(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  40. //#if F_CPU >= 20000000
  41. uint32_t usb_cdc_line_coding[2];
  42. volatile uint32_t usb_cdc_line_rtsdtr_millis;
  43. volatile uint8_t usb_cdc_line_rtsdtr=0;
  44. volatile uint8_t usb_cdc_transmit_flush_timer=0;
  45. static volatile uint8_t tx_noautoflush=0;
  46. extern volatile uint8_t usb_high_speed;
  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 4
  54. #define TX_SIZE 2048 /* should be a multiple of CDC_TX_SIZE */
  55. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  56. DMAMEM static uint8_t txbuffer[TX_SIZE * TX_NUM];
  57. static uint8_t tx_head=0;
  58. static uint16_t tx_available=0;
  59. static uint16_t tx_packet_size=0;
  60. #define RX_NUM 3
  61. static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
  62. static uint8_t rx_buffer[RX_NUM * CDC_RX_SIZE_480];
  63. static uint16_t rx_count[RX_NUM];
  64. static uint16_t rx_index[RX_NUM];
  65. static uint16_t rx_packet_size=0;
  66. static void rx_event(transfer_t *t)
  67. {
  68. int len = rx_packet_size - ((t->status >> 16) & 0x7FFF);
  69. int index = t->callback_param;
  70. //printf("rx event, len=%d, i=%d\n", len, index);
  71. rx_count[index] = len;
  72. rx_index[index] = 0;
  73. }
  74. void usb_serial_reset(void)
  75. {
  76. printf("usb_serial_reset\n");
  77. // deallocate all transfer descriptors
  78. }
  79. void usb_serial_configure(void)
  80. {
  81. printf("usb_serial_configure\n");
  82. if (usb_high_speed) {
  83. tx_packet_size = CDC_TX_SIZE_480;
  84. rx_packet_size = CDC_RX_SIZE_480;
  85. } else {
  86. tx_packet_size = CDC_TX_SIZE_12;
  87. rx_packet_size = CDC_RX_SIZE_12;
  88. }
  89. memset(tx_transfer, 0, sizeof(tx_transfer));
  90. tx_head = 0;
  91. tx_available = 0;
  92. memset(rx_transfer, 0, sizeof(rx_transfer));
  93. memset(rx_count, 0, sizeof(rx_count));
  94. memset(rx_index, 0, sizeof(rx_index));
  95. usb_config_tx(CDC_ACM_ENDPOINT, CDC_ACM_SIZE, 0, NULL); // size same 12 & 480
  96. usb_config_rx(CDC_RX_ENDPOINT, rx_packet_size, 0, rx_event);
  97. usb_config_tx(CDC_TX_ENDPOINT, tx_packet_size, 0, NULL);
  98. usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, rx_packet_size, 0);
  99. usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
  100. timer_config(usb_serial_flush_callback, TRANSMIT_FLUSH_TIMEOUT);
  101. }
  102. // get the next character, or -1 if nothing received
  103. int usb_serial_getchar(void)
  104. {
  105. if (rx_index[0] < rx_count[0]) {
  106. int c = rx_buffer[rx_index[0]++];
  107. if (rx_index[0] >= rx_count[0]) {
  108. // reschedule transfer
  109. usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, rx_packet_size, 0);
  110. usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
  111. }
  112. return c;
  113. }
  114. return -1;
  115. }
  116. // peek at the next character, or -1 if nothing received
  117. int usb_serial_peekchar(void)
  118. {
  119. if (rx_index[0] < rx_count[0]) {
  120. return rx_buffer[rx_index[0]];
  121. }
  122. return -1;
  123. }
  124. // number of bytes available in the receive buffer
  125. int usb_serial_available(void)
  126. {
  127. return rx_count[0] - rx_index[0];
  128. }
  129. // read a block of bytes to a buffer
  130. int usb_serial_read(void *buffer, uint32_t size)
  131. {
  132. // Quick and dirty to make it at least limp...
  133. uint8_t *p = (uint8_t *)buffer;
  134. uint32_t count=0;
  135. while (size) {
  136. int ch = usb_serial_getchar();
  137. if (ch == -1) break;
  138. *p++ = (uint8_t)ch;
  139. size--;
  140. count++;
  141. }
  142. return count;
  143. }
  144. // discard any buffered input
  145. void usb_serial_flush_input(void)
  146. {
  147. if (rx_index[0] < rx_count[0]) {
  148. rx_index[0] = rx_count[0];
  149. usb_prepare_transfer(rx_transfer + 0, rx_buffer + 0, rx_packet_size, 0);
  150. usb_receive(CDC_RX_ENDPOINT, rx_transfer + 0);
  151. }
  152. }
  153. // When the PC isn't listening, how long do we wait before discarding data? If this is
  154. // too short, we risk losing data during the stalls that are common with ordinary desktop
  155. // software. If it's too long, we stall the user's program when no software is running.
  156. #define TX_TIMEOUT_MSEC 120
  157. // When we've suffered the transmit timeout, don't wait again until the computer
  158. // begins accepting data. If no software is running to receive, we'll just discard
  159. // data as rapidly as Serial.print() can generate it, until there's something to
  160. // actually receive it.
  161. static uint8_t transmit_previous_timeout=0;
  162. // transmit a character. 0 returned on success, -1 on error
  163. int usb_serial_putchar(uint8_t c)
  164. {
  165. return usb_serial_write(&c, 1);
  166. }
  167. extern volatile uint32_t systick_millis_count;
  168. static void timer_config(void (*callback)(void), uint32_t microseconds);
  169. static void timer_start_oneshot();
  170. static void timer_stop();
  171. static void timer_config(void (*callback)(void), uint32_t microseconds)
  172. {
  173. usb_timer0_callback = callback;
  174. USB1_GPTIMER0CTRL = 0;
  175. USB1_GPTIMER0LD = microseconds - 1;
  176. USB1_USBINTR |= USB_USBINTR_TIE0;
  177. }
  178. static void timer_start_oneshot(void)
  179. {
  180. // restarts timer if already running (retriggerable one-shot)
  181. USB1_GPTIMER0CTRL = USB_GPTIMERCTRL_GPTRUN | USB_GPTIMERCTRL_GPTRST;
  182. }
  183. static void timer_stop(void)
  184. {
  185. USB1_GPTIMER0CTRL = 0;
  186. }
  187. int usb_serial_write(const void *buffer, uint32_t size)
  188. {
  189. uint32_t sent=0;
  190. const uint8_t *data = (const uint8_t *)buffer;
  191. if (!usb_configuration) return 0;
  192. while (size > 0) {
  193. transfer_t *xfer = tx_transfer + tx_head;
  194. int waiting=0;
  195. uint32_t wait_begin_at=0;
  196. while (!tx_available) {
  197. //digitalWriteFast(3, HIGH);
  198. uint32_t status = usb_transfer_status(xfer);
  199. if (!(status & 0x80)) {
  200. if (status & 0x68) {
  201. // TODO: what if status has errors???
  202. printf("ERROR status = %x, i=%d, ms=%u\n",
  203. status, tx_head, systick_millis_count);
  204. }
  205. tx_available = TX_SIZE;
  206. transmit_previous_timeout = 0;
  207. break;
  208. }
  209. if (!waiting) {
  210. wait_begin_at = systick_millis_count;
  211. waiting = 1;
  212. }
  213. if (transmit_previous_timeout) return sent;
  214. if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
  215. // waited too long, assume the USB host isn't listening
  216. transmit_previous_timeout = 1;
  217. return sent;
  218. //printf("\nstop, waited too long\n");
  219. //printf("status = %x\n", status);
  220. //printf("tx head=%d\n", tx_head);
  221. //printf("TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
  222. //usb_print_transfer_log();
  223. //while (1) ;
  224. }
  225. if (!usb_configuration) return sent;
  226. yield();
  227. }
  228. //digitalWriteFast(3, LOW);
  229. uint8_t *txdata = txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available);
  230. if (size >= tx_available) {
  231. memcpy(txdata, data, tx_available);
  232. //*(txbuffer + (tx_head * TX_SIZE)) = 'A' + tx_head; // to see which buffer
  233. //*(txbuffer + (tx_head * TX_SIZE) + 1) = ' '; // really see it
  234. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  235. usb_prepare_transfer(xfer, txbuf, TX_SIZE, 0);
  236. arm_dcache_flush_delete(txbuf, TX_SIZE);
  237. usb_transmit(CDC_TX_ENDPOINT, xfer);
  238. if (++tx_head >= TX_NUM) tx_head = 0;
  239. size -= tx_available;
  240. sent += tx_available;
  241. data += tx_available;
  242. tx_available = 0;
  243. timer_stop();
  244. } else {
  245. memcpy(txdata, data, size);
  246. tx_available -= size;
  247. sent += size;
  248. size = 0;
  249. timer_start_oneshot();
  250. }
  251. }
  252. return sent;
  253. }
  254. int usb_serial_write_buffer_free(void)
  255. {
  256. uint32_t sum = 0;
  257. tx_noautoflush = 1;
  258. for (uint32_t i=0; i < TX_NUM; i++) {
  259. if (i == tx_head) continue;
  260. if (!(usb_transfer_status(tx_transfer + i) & 0x80)) sum += TX_SIZE;
  261. }
  262. tx_noautoflush = 0;
  263. return sum;
  264. }
  265. void usb_serial_flush_output(void)
  266. {
  267. if (!usb_configuration) return;
  268. if (tx_available == 0) return;
  269. tx_noautoflush = 1;
  270. transfer_t *xfer = tx_transfer + tx_head;
  271. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  272. uint32_t txnum = TX_SIZE - tx_available;
  273. usb_prepare_transfer(xfer, txbuf, txnum, 0);
  274. arm_dcache_flush_delete(txbuf, txnum);
  275. usb_transmit(CDC_TX_ENDPOINT, xfer);
  276. if (++tx_head >= TX_NUM) tx_head = 0;
  277. tx_available = 0;
  278. tx_noautoflush = 0;
  279. }
  280. static void usb_serial_flush_callback(void)
  281. {
  282. if (tx_noautoflush) return;
  283. if (!usb_configuration) return;
  284. if (tx_available == 0) return;
  285. //printf("flush callback, %d bytes\n", TX_SIZE - tx_available);
  286. transfer_t *xfer = tx_transfer + tx_head;
  287. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  288. uint32_t txnum = TX_SIZE - tx_available;
  289. usb_prepare_transfer(xfer, txbuf, txnum, 0);
  290. arm_dcache_flush_delete(txbuf, txnum);
  291. usb_transmit(CDC_TX_ENDPOINT, xfer);
  292. if (++tx_head >= TX_NUM) tx_head = 0;
  293. tx_available = 0;
  294. }
  295. //#endif // F_CPU
  296. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE