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.

329 satır
9.7KB

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