Teensy 4.1 core updated for C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

357 linhas
9.3KB

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