選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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