No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

358 líneas
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 "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. // 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. if (!usb_configuration) return 0;
  193. // TODO: do something so much better that this quick hack....
  194. if (size > sizeof(txbuffer)) size = sizeof(txbuffer);
  195. int count=0;
  196. //digitalWriteFast(13, HIGH);
  197. while (1) {
  198. uint32_t status = (volatile)(transfer.status);
  199. if (count > 10) printf("status = %x\n", status);
  200. if (!(status & 0x80)) break;
  201. count++;
  202. //if (count > 50) break; // TODO: proper timout?
  203. // TODO: check for USB offline
  204. delayMicroseconds(5); // polling too quickly seem to block DMA - maybe DTCM issue?
  205. }
  206. //digitalWriteFast(13, LOW);
  207. delayMicroseconds(1); // TODO: this must not be the answer!
  208. memcpy(txbuffer, buffer, size);
  209. usb_prepare_transfer(&transfer, txbuffer, size, 0);
  210. usb_transmit(CDC_TX_ENDPOINT, &transfer);
  211. #if 0
  212. uint32_t ret = size;
  213. uint32_t len;
  214. uint32_t wait_count;
  215. const uint8_t *src = (const uint8_t *)buffer;
  216. uint8_t *dest;
  217. tx_noautoflush = 1;
  218. while (size > 0) {
  219. if (!tx_packet) {
  220. wait_count = 0;
  221. while (1) {
  222. if (!usb_configuration) {
  223. tx_noautoflush = 0;
  224. return -1;
  225. }
  226. if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  227. tx_noautoflush = 1;
  228. tx_packet = usb_malloc();
  229. if (tx_packet) break;
  230. tx_noautoflush = 0;
  231. }
  232. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  233. transmit_previous_timeout = 1;
  234. return -1;
  235. }
  236. yield();
  237. }
  238. }
  239. transmit_previous_timeout = 0;
  240. len = CDC_TX_SIZE - tx_packet->index;
  241. if (len > size) len = size;
  242. dest = tx_packet->buf + tx_packet->index;
  243. tx_packet->index += len;
  244. size -= len;
  245. while (len-- > 0) *dest++ = *src++;
  246. if (tx_packet->index >= CDC_TX_SIZE) {
  247. tx_packet->len = CDC_TX_SIZE;
  248. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  249. tx_packet = NULL;
  250. }
  251. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  252. }
  253. tx_noautoflush = 0;
  254. return ret;
  255. #endif
  256. return 0;
  257. }
  258. int usb_serial_write_buffer_free(void)
  259. {
  260. #if 0
  261. uint32_t len;
  262. tx_noautoflush = 1;
  263. if (!tx_packet) {
  264. if (!usb_configuration ||
  265. usb_tx_packet_count(CDC_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
  266. (tx_packet = usb_malloc()) == NULL) {
  267. tx_noautoflush = 0;
  268. return 0;
  269. }
  270. }
  271. len = CDC_TX_SIZE - tx_packet->index;
  272. // TODO: Perhaps we need "usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT"
  273. // added here, so the SOF interrupt can't take away the available buffer
  274. // space we just promised the user could write without blocking?
  275. // But does this come with other performance downsides? Could it lead to
  276. // buffer data never actually transmitting in some usage cases? More
  277. // investigation is needed.
  278. // https://github.com/PaulStoffregen/cores/issues/10#issuecomment-61514955
  279. tx_noautoflush = 0;
  280. return len;
  281. #endif
  282. return 0;
  283. }
  284. void usb_serial_flush_output(void)
  285. {
  286. #if 0
  287. if (!usb_configuration) return;
  288. tx_noautoflush = 1;
  289. if (tx_packet) {
  290. usb_cdc_transmit_flush_timer = 0;
  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_cdc_transmit_flush_timer = 0;
  298. usb_tx(CDC_TX_ENDPOINT, tx);
  299. } else {
  300. usb_cdc_transmit_flush_timer = 1;
  301. }
  302. }
  303. tx_noautoflush = 0;
  304. #endif
  305. }
  306. void usb_serial_flush_callback(void)
  307. {
  308. #if 0
  309. if (tx_noautoflush) return;
  310. if (tx_packet) {
  311. tx_packet->len = tx_packet->index;
  312. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  313. tx_packet = NULL;
  314. } else {
  315. usb_packet_t *tx = usb_malloc();
  316. if (tx) {
  317. usb_tx(CDC_TX_ENDPOINT, tx);
  318. } else {
  319. usb_cdc_transmit_flush_timer = 1;
  320. }
  321. }
  322. #endif
  323. }
  324. //#endif // F_CPU
  325. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE