You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serial.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. // quick hack - ultimately USBHost print & println need to be renamed
  26. #define print USBHost::print
  27. #define println USBHost::println
  28. void USBSerial::init()
  29. {
  30. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  31. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  32. driver_ready_for_device(this);
  33. }
  34. bool USBSerial::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  35. {
  36. // only claim at interface level
  37. println("USBSerial claim this=", (uint32_t)this, HEX);
  38. print("vid=", dev->idVendor, HEX);
  39. println(", pid=", dev->idProduct, HEX);
  40. if (type == 0) {
  41. if (dev->idVendor == 0x0403 && dev->idProduct == 0x6001) {
  42. // FTDI FT232
  43. println("len = ", len);
  44. if (len < 23) return false;
  45. if (descriptors[0] != 9) return false; // length 9
  46. if (descriptors[9] != 7) return false; // length 7
  47. if (descriptors[10] != 5) return false; // ep desc
  48. uint32_t rxep = descriptors[11];
  49. if (descriptors[12] != 2) return false; // bulk type
  50. if (descriptors[13] != 64) return false; // size 64
  51. if (descriptors[14] != 0) return false;
  52. if (descriptors[16] != 7) return false; // length 7
  53. if (descriptors[17] != 5) return false; // ep desc
  54. uint32_t txep = descriptors[18];
  55. if (descriptors[19] != 2) return false; // bulk type
  56. if (descriptors[20] != 64) return false; // size 64
  57. if (descriptors[21] != 0) return false;
  58. if (!check_rxtx_ep(rxep, txep)) return false;
  59. print("FTDI, rxep=", rxep & 15);
  60. println(", txep=", txep);
  61. if (!init_buffers(64, 64)) return false;
  62. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, 64);
  63. if (!rxpipe) return false;
  64. txpipe = new_Pipe(dev, 2, txep, 0, 64);
  65. if (!txpipe) {
  66. // TODO: free rxpipe
  67. return false;
  68. }
  69. sertype = FTDI;
  70. rxpipe->callback_function = rx_callback;
  71. queue_Data_Transfer(rxpipe, rx1, 64, this);
  72. rxstate = 1;
  73. if (rxsize > 128) {
  74. queue_Data_Transfer(rxpipe, rx2, 64, this);
  75. rxstate = 3;
  76. }
  77. txstate = 0;
  78. txpipe->callback_function = tx_callback;
  79. baudrate = 115200;
  80. pending_control = 0x0F;
  81. mk_setup(setup, 0x40, 0, 0, 0, 0); // reset port
  82. queue_Control_Transfer(dev, &setup, NULL, this);
  83. control_queued = true;
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. // check if two legal endpoints, 1 receive & 1 transmit
  90. bool USBSerial::check_rxtx_ep(uint32_t &rxep, uint32_t &txep)
  91. {
  92. if ((rxep & 0x0F) == 0) return false;
  93. if ((txep & 0x0F) == 0) return false;
  94. uint32_t rxdir = rxep & 0xF0;
  95. uint32_t txdir = txep & 0xF0;
  96. if (rxdir == 0x80 && txdir == 0x00) {
  97. return true;
  98. }
  99. if (rxdir == 0x00 && txdir == 0x80) {
  100. std::swap(rxep, txep);
  101. return true;
  102. }
  103. return false;
  104. }
  105. // initialize buffer sizes and pointers
  106. bool USBSerial::init_buffers(uint32_t rsize, uint32_t tsize)
  107. {
  108. // buffer must be able to hold 2 of each packet, plus have room to
  109. if (sizeof(bigbuffer) < (rsize + tsize) * 3 + 2) return false;
  110. rx1 = (uint8_t *)bigbuffer;
  111. rx2 = rx1 + rsize;
  112. tx1 = rx2 + rsize;
  113. tx2 = tx1 + tsize;
  114. rxbuf = tx2 + tsize;
  115. // FIXME: this assume 50-50 split - not true when rsize != tsize
  116. rxsize = (sizeof(bigbuffer) - (rsize + tsize) * 2) / 2;
  117. txsize = rxsize;
  118. txbuf = rxbuf + rxsize;
  119. rxhead = 0;
  120. rxtail = 0;
  121. txhead = 0;
  122. txtail = 0;
  123. rxstate = 0;
  124. return true;
  125. }
  126. void USBSerial::disconnect()
  127. {
  128. }
  129. void USBSerial::control(const Transfer_t *transfer)
  130. {
  131. println("control callback (serial)");
  132. control_queued = false;
  133. // set data format
  134. if (pending_control & 1) {
  135. pending_control &= ~1;
  136. mk_setup(setup, 0x40, 4, 8, 0, 0); // data format 8N1
  137. queue_Control_Transfer(device, &setup, NULL, this);
  138. control_queued = true;
  139. return;
  140. }
  141. // set baud rate
  142. if (pending_control & 2) {
  143. pending_control &= ~2;
  144. uint32_t baudval = 3000000 / baudrate;
  145. mk_setup(setup, 0x40, 3, baudval, 0, 0);
  146. queue_Control_Transfer(device, &setup, NULL, this);
  147. control_queued = true;
  148. return;
  149. }
  150. // configure flow control
  151. if (pending_control & 4) {
  152. pending_control &= ~4;
  153. mk_setup(setup, 0x40, 2, 0, 0, 0);
  154. queue_Control_Transfer(device, &setup, NULL, this);
  155. control_queued = true;
  156. return;
  157. }
  158. // set DTR
  159. if (pending_control & 8) {
  160. pending_control &= ~8;
  161. mk_setup(setup, 0x40, 1, 0x0101, 0, 0);
  162. queue_Control_Transfer(device, &setup, NULL, this);
  163. control_queued = true;
  164. return;
  165. }
  166. }
  167. void USBSerial::rx_callback(const Transfer_t *transfer)
  168. {
  169. if (!transfer->driver) return;
  170. ((USBSerial *)(transfer->driver))->rx_data(transfer);
  171. }
  172. void USBSerial::tx_callback(const Transfer_t *transfer)
  173. {
  174. if (!transfer->driver) return;
  175. ((USBSerial *)(transfer->driver))->tx_data(transfer);
  176. }
  177. void USBSerial::rx_data(const Transfer_t *transfer)
  178. {
  179. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  180. // first update rxstate bitmask, since buffer is no longer queued
  181. if (transfer->buffer == rx1) {
  182. rxstate &= 0xFE;
  183. } else if (transfer->buffer == rx2) {
  184. rxstate &= 0xFD;
  185. }
  186. // get start of data and actual length
  187. const uint8_t *p = (const uint8_t *)transfer->buffer;
  188. if (sertype == FTDI) {
  189. if (len >= 2) {
  190. p += 2;
  191. len -= 2;
  192. } else {
  193. len = 0;
  194. }
  195. }
  196. //if (len > 0) {
  197. //print("rx: ");
  198. //print_hexbytes(p, len);
  199. //}
  200. // Copy data from packet buffer to circular buffer.
  201. // Assume the buffer will always have space, since we
  202. // check before queuing the buffers
  203. uint32_t head = rxhead;
  204. uint32_t tail = rxtail;
  205. if (++head >= rxsize) head = 0;
  206. uint32_t avail;
  207. if (len > 0) {
  208. //print("head=", head);
  209. //print(", tail=", tail);
  210. avail = rxsize - head;
  211. //print(", avail=", avail);
  212. //println(", rxsize=", rxsize);
  213. if (avail > len) avail = len;
  214. memcpy(rxbuf + head, p, avail);
  215. if (len <= avail) {
  216. head += avail - 1;
  217. if (head >= rxsize) head = 0;
  218. } else {
  219. head = len - avail - 1;
  220. memcpy(rxbuf, p + avail, head + 1);
  221. }
  222. rxhead = head;
  223. }
  224. // TODO: can be this more efficient? We know from above which
  225. // buffer is no longer queued, so possible skip most of this work?
  226. rx_queue_packets(head, tail);
  227. }
  228. // re-queue packet buffer(s) if possible
  229. void USBSerial::rx_queue_packets(uint32_t head, uint32_t tail)
  230. {
  231. uint32_t avail;
  232. if (head >= tail) {
  233. avail = rxsize - 1 - head + tail;
  234. } else {
  235. avail = tail - head - 1;
  236. }
  237. uint32_t packetsize = rx2 - rx1;
  238. if (avail >= packetsize) {
  239. if ((rxstate & 0x01) == 0) {
  240. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  241. rxstate |= 0x01;
  242. } else if ((rxstate & 0x02) == 0) {
  243. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  244. rxstate |= 0x02;
  245. }
  246. if ((rxstate & 0x03) != 0x03 && avail >= packetsize * 2) {
  247. if ((rxstate & 0x01) == 0) {
  248. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  249. rxstate |= 0x01;
  250. } else if ((rxstate & 0x02) == 0) {
  251. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  252. rxstate |= 0x02;
  253. }
  254. }
  255. }
  256. }
  257. void USBSerial::tx_data(const Transfer_t *transfer)
  258. {
  259. if (transfer->buffer == tx1) {
  260. println("tx1:");
  261. txstate &= 0xFE;
  262. } else if (transfer->buffer == tx2) {
  263. println("tx2:");
  264. txstate &= 0xFD;
  265. }
  266. // TODO: anything need to be done with latency timeout?
  267. }
  268. void USBSerial::begin(uint32_t baud, uint32_t format)
  269. {
  270. NVIC_DISABLE_IRQ(IRQ_USBHS);
  271. baudrate = baud;
  272. pending_control |= 2;
  273. if (!control_queued) control(NULL);
  274. NVIC_ENABLE_IRQ(IRQ_USBHS);
  275. }
  276. void USBSerial::end(void)
  277. {
  278. }
  279. int USBSerial::available(void)
  280. {
  281. if (!device) return 0;
  282. uint32_t head = rxhead;
  283. uint32_t tail = rxtail;
  284. if (head >= tail) return head - tail;
  285. return rxsize + head - tail;
  286. }
  287. int USBSerial::peek(void)
  288. {
  289. if (!device) return -1;
  290. uint32_t head = rxhead;
  291. uint32_t tail = rxtail;
  292. if (head == tail) return -1;
  293. if (++tail >= rxsize) tail = 0;
  294. return rxbuf[tail];
  295. }
  296. int USBSerial::read(void)
  297. {
  298. if (!device) return -1;
  299. uint32_t head = rxhead;
  300. uint32_t tail = rxtail;
  301. if (head == tail) return -1;
  302. if (++tail >= rxsize) tail = 0;
  303. int c = rxbuf[tail];
  304. rxtail = tail;
  305. if ((rxstate & 0x03) != 0x03) {
  306. NVIC_DISABLE_IRQ(IRQ_USBHS);
  307. rx_queue_packets(head, tail);
  308. NVIC_ENABLE_IRQ(IRQ_USBHS);
  309. }
  310. return c;
  311. }
  312. int USBSerial::availableForWrite()
  313. {
  314. if (!device) return 0;
  315. uint32_t head = txhead;
  316. uint32_t tail = txtail;
  317. if (head >= tail) return txsize - 1 - head + tail;
  318. return tail - head - 1;
  319. }
  320. size_t USBSerial::write(uint8_t c)
  321. {
  322. if (!device) return 0;
  323. uint32_t head = txhead;
  324. if (++head >= txsize) head = 0;
  325. while (txtail == head) {
  326. // wait...
  327. }
  328. txbuf[head] = c;
  329. txhead = head;
  330. //print("head=", head);
  331. //println(", tail=", txtail);
  332. // if full packet in buffer and tx packet ready, queue it
  333. NVIC_DISABLE_IRQ(IRQ_USBHS);
  334. uint32_t tail = txtail;
  335. if ((txstate & 0x03) != 0x03) {
  336. // at least one packet buffer is ready to transmit
  337. uint32_t count;
  338. if (head >= tail) {
  339. count = head - tail;
  340. } else {
  341. count = txsize + head - tail;
  342. }
  343. uint32_t packetsize = tx2 - tx1;
  344. if (count >= packetsize) {
  345. //println("txsize=", txsize);
  346. uint8_t *p;
  347. if ((txstate & 0x01) == 0) {
  348. p = tx1;
  349. txstate |= 0x01;
  350. } else /* if ((txstate & 0x02) == 0) */ {
  351. p = tx2;
  352. txstate |= 0x02;
  353. }
  354. // copy data to packet buffer
  355. if (++tail >= txsize) tail = 0;
  356. uint32_t n = txsize - tail;
  357. if (n > packetsize) n = packetsize;
  358. //print("memcpy, offset=", tail);
  359. //println(", len=", n);
  360. memcpy(p, txbuf + tail, n);
  361. if (n >= packetsize) {
  362. tail += n - 1;
  363. if (tail >= txsize) tail = 0;
  364. } else {
  365. //n = txsize - n;
  366. uint32_t len = packetsize - n;
  367. //println("memcpy, offset=0, len=", len);
  368. memcpy(p + n, txbuf, len);
  369. tail = len - 1;
  370. }
  371. txtail = tail;
  372. //println("queue tx packet, newtail=", tail);
  373. queue_Data_Transfer(txpipe, p, packetsize, this);
  374. NVIC_ENABLE_IRQ(IRQ_USBHS);
  375. return 1;
  376. }
  377. }
  378. // TODO: otherwise set a latency timer to transmit partial packet
  379. NVIC_ENABLE_IRQ(IRQ_USBHS);
  380. return 1;
  381. }