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

396 行
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 * 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 7
  223. #define TX_SIZE 256 /* 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. static uint16_t tx_available=0;
  228. extern volatile uint32_t systick_millis_count;
  229. int usb_serial_write(const void *buffer, uint32_t size)
  230. {
  231. uint32_t sent=0;
  232. const uint8_t *data = (const uint8_t *)buffer;
  233. if (!usb_configuration) return 0;
  234. while (size > 0) {
  235. transfer_t *xfer = tx_transfer + tx_head;
  236. int waiting=0;
  237. uint32_t wait_begin_at=0;
  238. while (!tx_available) {
  239. //digitalWriteFast(3, HIGH);
  240. uint32_t status = usb_transfer_status(xfer);
  241. if (status & 0x7F) {
  242. printf("ERROR status = %x, i=%d, ms=%u\n", status, tx_head, systick_millis_count);
  243. }
  244. if (!(status & 0x80)) {
  245. // TODO: what if status has errors???
  246. tx_available = TX_SIZE;
  247. break;
  248. }/* else {
  249. if (!waiting) {
  250. wait_begin_at = systick_millis_count;
  251. waiting = 1;
  252. } else {
  253. if (systick_millis_count - wait_begin_at > 250) {
  254. printf("\nstop, waited too long\n");
  255. printf("status = %x\n", status);
  256. printf("tx head=%d\n", tx_head);
  257. printf("TXFILLTUNING=%08lX\n", USB1_TXFILLTUNING);
  258. usb_print_transfer_log();
  259. while (1) ;
  260. }
  261. }
  262. } */
  263. // TODO: proper timout?
  264. // TODO: check for USB offline
  265. }
  266. //digitalWriteFast(3, LOW);
  267. uint8_t *txdata = txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available);
  268. if (size >= tx_available) {
  269. memcpy(txdata, data, tx_available);
  270. //*(txbuffer + (tx_head * TX_SIZE)) = 'A' + tx_head; // to see which buffer
  271. //*(txbuffer + (tx_head * TX_SIZE) + 1) = ' '; // really see it
  272. usb_prepare_transfer(xfer, txbuffer + (tx_head * TX_SIZE), TX_SIZE, 0);
  273. usb_transmit(CDC_TX_ENDPOINT, xfer);
  274. if (++tx_head >= TX_NUM) tx_head = 0;
  275. size -= tx_available;
  276. sent += tx_available;
  277. data += tx_available;
  278. tx_available = 0;
  279. } else {
  280. memcpy(txdata, data, size);
  281. tx_available -= size;
  282. sent += size;
  283. size = 0;
  284. }
  285. }
  286. return sent;
  287. }
  288. int usb_serial_write_buffer_free(void)
  289. {
  290. #if 0
  291. uint32_t len;
  292. tx_noautoflush = 1;
  293. if (!tx_packet) {
  294. if (!usb_configuration ||
  295. usb_tx_packet_count(CDC_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
  296. (tx_packet = usb_malloc()) == NULL) {
  297. tx_noautoflush = 0;
  298. return 0;
  299. }
  300. }
  301. len = CDC_TX_SIZE - tx_packet->index;
  302. // TODO: Perhaps we need "usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT"
  303. // added here, so the SOF interrupt can't take away the available buffer
  304. // space we just promised the user could write without blocking?
  305. // But does this come with other performance downsides? Could it lead to
  306. // buffer data never actually transmitting in some usage cases? More
  307. // investigation is needed.
  308. // https://github.com/PaulStoffregen/cores/issues/10#issuecomment-61514955
  309. tx_noautoflush = 0;
  310. return len;
  311. #endif
  312. return 0;
  313. }
  314. void usb_serial_flush_output(void)
  315. {
  316. #if 0
  317. if (!usb_configuration) return;
  318. tx_noautoflush = 1;
  319. if (tx_packet) {
  320. usb_cdc_transmit_flush_timer = 0;
  321. tx_packet->len = tx_packet->index;
  322. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  323. tx_packet = NULL;
  324. } else {
  325. usb_packet_t *tx = usb_malloc();
  326. if (tx) {
  327. usb_cdc_transmit_flush_timer = 0;
  328. usb_tx(CDC_TX_ENDPOINT, tx);
  329. } else {
  330. usb_cdc_transmit_flush_timer = 1;
  331. }
  332. }
  333. tx_noautoflush = 0;
  334. #endif
  335. }
  336. void usb_serial_flush_callback(void)
  337. {
  338. #if 0
  339. if (tx_noautoflush) return;
  340. if (tx_packet) {
  341. tx_packet->len = tx_packet->index;
  342. usb_tx(CDC_TX_ENDPOINT, tx_packet);
  343. tx_packet = NULL;
  344. } else {
  345. usb_packet_t *tx = usb_malloc();
  346. if (tx) {
  347. usb_tx(CDC_TX_ENDPOINT, tx);
  348. } else {
  349. usb_cdc_transmit_flush_timer = 1;
  350. }
  351. }
  352. #endif
  353. }
  354. //#endif // F_CPU
  355. #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE