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.

338 lines
8.6KB

  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 "delay.h" // for yield()
  33. //#include "HardwareSerial.h"
  34. #include <string.h> // for memcpy()
  35. // defined by usb_dev.h -> usb_desc.h
  36. #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  37. //#if F_CPU >= 20000000
  38. uint32_t usb_cdc_line_coding[2];
  39. volatile uint32_t usb_cdc_line_rtsdtr_millis;
  40. volatile uint8_t usb_cdc_line_rtsdtr=0;
  41. volatile uint8_t usb_cdc_transmit_flush_timer=0;
  42. //static usb_packet_t *rx_packet=NULL;
  43. //static usb_packet_t *tx_packet=NULL;
  44. static volatile uint8_t tx_noautoflush=0;
  45. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  46. // get the next character, or -1 if nothing received
  47. int usb_serial_getchar(void)
  48. {
  49. #if 0
  50. unsigned int i;
  51. int c;
  52. if (!rx_packet) {
  53. if (!usb_configuration) return -1;
  54. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  55. if (!rx_packet) return -1;
  56. }
  57. i = rx_packet->index;
  58. c = rx_packet->buf[i++];
  59. if (i >= rx_packet->len) {
  60. usb_free(rx_packet);
  61. rx_packet = NULL;
  62. } else {
  63. rx_packet->index = i;
  64. }
  65. return c;
  66. #endif
  67. return -1;
  68. }
  69. // peek at the next character, or -1 if nothing received
  70. int usb_serial_peekchar(void)
  71. {
  72. #if 0
  73. if (!rx_packet) {
  74. if (!usb_configuration) return -1;
  75. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  76. if (!rx_packet) return -1;
  77. }
  78. if (!rx_packet) return -1;
  79. return rx_packet->buf[rx_packet->index];
  80. #endif
  81. return -1;
  82. }
  83. // number of bytes available in the receive buffer
  84. int usb_serial_available(void)
  85. {
  86. #if 0
  87. int count;
  88. count = usb_rx_byte_count(CDC_RX_ENDPOINT);
  89. if (rx_packet) count += rx_packet->len - rx_packet->index;
  90. return count;
  91. #endif
  92. return 0;
  93. }
  94. // read a block of bytes to a buffer
  95. int usb_serial_read(void *buffer, uint32_t size)
  96. {
  97. #if 0
  98. uint8_t *p = (uint8_t *)buffer;
  99. uint32_t qty, count=0;
  100. while (size) {
  101. if (!usb_configuration) break;
  102. if (!rx_packet) {
  103. rx:
  104. rx_packet = usb_rx(CDC_RX_ENDPOINT);
  105. if (!rx_packet) break;
  106. if (rx_packet->len == 0) {
  107. usb_free(rx_packet);
  108. goto rx;
  109. }
  110. }
  111. qty = rx_packet->len - rx_packet->index;
  112. if (qty > size) qty = size;
  113. memcpy(p, rx_packet->buf + rx_packet->index, qty);
  114. p += qty;
  115. count += qty;
  116. size -= qty;
  117. rx_packet->index += qty;
  118. if (rx_packet->index >= rx_packet->len) {
  119. usb_free(rx_packet);
  120. rx_packet = NULL;
  121. }
  122. }
  123. return count;
  124. #endif
  125. return 0;
  126. }
  127. // discard any buffered input
  128. void usb_serial_flush_input(void)
  129. {
  130. #if 0
  131. usb_packet_t *rx;
  132. if (!usb_configuration) return;
  133. if (rx_packet) {
  134. usb_free(rx_packet);
  135. rx_packet = NULL;
  136. }
  137. while (1) {
  138. rx = usb_rx(CDC_RX_ENDPOINT);
  139. if (!rx) break;
  140. usb_free(rx);
  141. }
  142. #endif
  143. }
  144. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  145. //#define TX_PACKET_LIMIT 8
  146. // When the PC isn't listening, how long do we wait before discarding data? If this is
  147. // too short, we risk losing data during the stalls that are common with ordinary desktop
  148. // software. If it's too long, we stall the user's program when no software is running.
  149. #define TX_TIMEOUT_MSEC 70
  150. /*#if F_CPU == 240000000
  151. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
  152. #elif F_CPU == 216000000
  153. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
  154. #elif F_CPU == 192000000
  155. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  156. #elif F_CPU == 180000000
  157. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  158. #elif F_CPU == 168000000
  159. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  160. #elif F_CPU == 144000000
  161. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  162. #elif F_CPU == 120000000
  163. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  164. #elif F_CPU == 96000000
  165. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  166. #elif F_CPU == 72000000
  167. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  168. #elif F_CPU == 48000000
  169. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  170. #elif F_CPU == 24000000
  171. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  172. #endif */
  173. // When we've suffered the transmit timeout, don't wait again until the computer
  174. // begins accepting data. If no software is running to receive, we'll just discard
  175. // data as rapidly as Serial.print() can generate it, until there's something to
  176. // actually receive it.
  177. //static uint8_t transmit_previous_timeout=0;
  178. // transmit a character. 0 returned on success, -1 on error
  179. int usb_serial_putchar(uint8_t c)
  180. {
  181. return usb_serial_write(&c, 1);
  182. }
  183. static transfer_t transfer __attribute__ ((used, aligned(32)));
  184. static uint8_t txbuffer[256];
  185. int usb_serial_write(const void *buffer, uint32_t size)
  186. {
  187. if (size > sizeof(txbuffer)) size = sizeof(txbuffer);
  188. memcpy(txbuffer, buffer, size);
  189. usb_prepare_transfer(&transfer, txbuffer, size, 0);
  190. usb_transmit(CDC_TX_ENDPOINT, &transfer);
  191. #if 0
  192. uint32_t ret = size;
  193. uint32_t len;
  194. uint32_t wait_count;
  195. const uint8_t *src = (const uint8_t *)buffer;
  196. uint8_t *dest;
  197. tx_noautoflush = 1;
  198. while (size > 0) {
  199. if (!tx_packet) {
  200. wait_count = 0;
  201. while (1) {
  202. if (!usb_configuration) {
  203. tx_noautoflush = 0;
  204. return -1;
  205. }
  206. if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  207. tx_noautoflush = 1;
  208. tx_packet = usb_malloc();
  209. if (tx_packet) break;
  210. tx_noautoflush = 0;
  211. }
  212. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  213. transmit_previous_timeout = 1;
  214. return -1;
  215. }
  216. yield();
  217. }
  218. }
  219. transmit_previous_timeout = 0;
  220. len = CDC_TX_SIZE - tx_packet->index;
  221. if (len > size) len = size;
  222. dest = tx_packet->buf + tx_packet->index;
  223. tx_packet->index += len;
  224. size -= len;
  225. while (len-- > 0) *dest++ = *src++;
  226. if (tx_packet->index >= CDC_TX_SIZE) {
  227. tx_packet->len = CDC_TX_SIZE;
  228. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  229. tx_packet = NULL;
  230. }
  231. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  232. }
  233. tx_noautoflush = 0;
  234. return ret;
  235. #endif
  236. return 0;
  237. }
  238. int usb_serial_write_buffer_free(void)
  239. {
  240. #if 0
  241. uint32_t len;
  242. tx_noautoflush = 1;
  243. if (!tx_packet) {
  244. if (!usb_configuration ||
  245. usb_tx_packet_count(CDC_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
  246. (tx_packet = usb_malloc()) == NULL) {
  247. tx_noautoflush = 0;
  248. return 0;
  249. }
  250. }
  251. len = CDC_TX_SIZE - tx_packet->index;
  252. // TODO: Perhaps we need "usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT"
  253. // added here, so the SOF interrupt can't take away the available buffer
  254. // space we just promised the user could write without blocking?
  255. // But does this come with other performance downsides? Could it lead to
  256. // buffer data never actually transmitting in some usage cases? More
  257. // investigation is needed.
  258. // https://github.com/PaulStoffregen/cores/issues/10#issuecomment-61514955
  259. tx_noautoflush = 0;
  260. return len;
  261. #endif
  262. return 0;
  263. }
  264. void usb_serial_flush_output(void)
  265. {
  266. #if 0
  267. if (!usb_configuration) return;
  268. tx_noautoflush = 1;
  269. if (tx_packet) {
  270. usb_cdc_transmit_flush_timer = 0;
  271. tx_packet->len = tx_packet->index;
  272. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  273. tx_packet = NULL;
  274. } else {
  275. usb_packet_t *tx = usb_malloc();
  276. if (tx) {
  277. usb_cdc_transmit_flush_timer = 0;
  278. usb_tx(CDC_TX_ENDPOINT, tx);
  279. } else {
  280. usb_cdc_transmit_flush_timer = 1;
  281. }
  282. }
  283. tx_noautoflush = 0;
  284. #endif
  285. }
  286. void usb_serial_flush_callback(void)
  287. {
  288. #if 0
  289. if (tx_noautoflush) return;
  290. if (tx_packet) {
  291. tx_packet->len = tx_packet->index;
  292. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  293. tx_packet = NULL;
  294. } else {
  295. usb_packet_t *tx = usb_malloc();
  296. if (tx) {
  297. usb_tx(CDC_TX_ENDPOINT, tx);
  298. } else {
  299. usb_cdc_transmit_flush_timer = 1;
  300. }
  301. }
  302. #endif
  303. }
  304. //#endif // F_CPU
  305. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE