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.

400 lines
10KB

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