Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 * CDC_RX_SIZE];
  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 = CDC_RX_SIZE - ((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, CDC_RX_SIZE, 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, CDC_RX_SIZE, 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. #define TX_NUM 4
  223. #define TX_SIZE 64 /* should be a multiple of CDC_TX_SIZE */
  224. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  225. static uint8_t txbuffer[TX_SIZE * TX_NUM];
  226. static uint8_t tx_head=0;
  227. int usb_serial_write(const void *buffer, uint32_t size)
  228. {
  229. if (!usb_configuration) return 0;
  230. if (size > TX_SIZE) size = TX_SIZE;
  231. transfer_t *xfer = tx_transfer + tx_head;
  232. int count=0;
  233. while (1) {
  234. uint32_t status = usb_transfer_status(xfer);
  235. if (count > 2000) {
  236. printf("status = %x\n", status);
  237. //while (1) ;
  238. }
  239. if (!(status & 0x80)) break;
  240. count++;
  241. //if (count > 50) break; // TODO: proper timout?
  242. // TODO: check for USB offline
  243. }
  244. uint8_t *txdata = txbuffer + (tx_head * TX_SIZE);
  245. memcpy(txdata, buffer, size);
  246. usb_prepare_transfer(xfer, txdata, size, 0);
  247. usb_transmit(CDC_TX_ENDPOINT, xfer);
  248. if (++tx_head >= TX_NUM) tx_head = 0;
  249. return size;
  250. #if 0
  251. uint32_t ret = size;
  252. uint32_t len;
  253. uint32_t wait_count;
  254. const uint8_t *src = (const uint8_t *)buffer;
  255. uint8_t *dest;
  256. tx_noautoflush = 1;
  257. while (size > 0) {
  258. if (!tx_packet) {
  259. wait_count = 0;
  260. while (1) {
  261. if (!usb_configuration) {
  262. tx_noautoflush = 0;
  263. return -1;
  264. }
  265. if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  266. tx_noautoflush = 1;
  267. tx_packet = usb_malloc();
  268. if (tx_packet) break;
  269. tx_noautoflush = 0;
  270. }
  271. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  272. transmit_previous_timeout = 1;
  273. return -1;
  274. }
  275. yield();
  276. }
  277. }
  278. transmit_previous_timeout = 0;
  279. len = CDC_TX_SIZE - tx_packet->index;
  280. if (len > size) len = size;
  281. dest = tx_packet->buf + tx_packet->index;
  282. tx_packet->index += len;
  283. size -= len;
  284. while (len-- > 0) *dest++ = *src++;
  285. if (tx_packet->index >= CDC_TX_SIZE) {
  286. tx_packet->len = CDC_TX_SIZE;
  287. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  288. tx_packet = NULL;
  289. }
  290. usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  291. }
  292. tx_noautoflush = 0;
  293. return ret;
  294. #endif
  295. return 0;
  296. }
  297. int usb_serial_write_buffer_free(void)
  298. {
  299. #if 0
  300. uint32_t len;
  301. tx_noautoflush = 1;
  302. if (!tx_packet) {
  303. if (!usb_configuration ||
  304. usb_tx_packet_count(CDC_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
  305. (tx_packet = usb_malloc()) == NULL) {
  306. tx_noautoflush = 0;
  307. return 0;
  308. }
  309. }
  310. len = CDC_TX_SIZE - tx_packet->index;
  311. // TODO: Perhaps we need "usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT"
  312. // added here, so the SOF interrupt can't take away the available buffer
  313. // space we just promised the user could write without blocking?
  314. // But does this come with other performance downsides? Could it lead to
  315. // buffer data never actually transmitting in some usage cases? More
  316. // investigation is needed.
  317. // https://github.com/PaulStoffregen/cores/issues/10#issuecomment-61514955
  318. tx_noautoflush = 0;
  319. return len;
  320. #endif
  321. return 0;
  322. }
  323. void usb_serial_flush_output(void)
  324. {
  325. #if 0
  326. if (!usb_configuration) return;
  327. tx_noautoflush = 1;
  328. if (tx_packet) {
  329. usb_cdc_transmit_flush_timer = 0;
  330. tx_packet->len = tx_packet->index;
  331. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  332. tx_packet = NULL;
  333. } else {
  334. usb_packet_t *tx = usb_malloc();
  335. if (tx) {
  336. usb_cdc_transmit_flush_timer = 0;
  337. usb_tx(CDC_TX_ENDPOINT, tx);
  338. } else {
  339. usb_cdc_transmit_flush_timer = 1;
  340. }
  341. }
  342. tx_noautoflush = 0;
  343. #endif
  344. }
  345. void usb_serial_flush_callback(void)
  346. {
  347. #if 0
  348. if (tx_noautoflush) return;
  349. if (tx_packet) {
  350. tx_packet->len = tx_packet->index;
  351. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  352. tx_packet = NULL;
  353. } else {
  354. usb_packet_t *tx = usb_malloc();
  355. if (tx) {
  356. usb_tx(CDC_TX_ENDPOINT, tx);
  357. } else {
  358. usb_cdc_transmit_flush_timer = 1;
  359. }
  360. }
  361. #endif
  362. }
  363. //#endif // F_CPU
  364. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE