Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

508 lines
13KB

  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. #define print USBHost::print_
  26. #define println USBHost::println_
  27. /************************************************************/
  28. // Initialization and claiming of devices & interfaces
  29. /************************************************************/
  30. void USBSerial::init()
  31. {
  32. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  33. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  34. driver_ready_for_device(this);
  35. }
  36. bool USBSerial::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  37. {
  38. // only claim at interface level
  39. println("USBSerial claim this=", (uint32_t)this, HEX);
  40. print("vid=", dev->idVendor, HEX);
  41. println(", pid=", dev->idProduct, HEX);
  42. if (type == 0) {
  43. if (dev->idVendor == 0x0403 && dev->idProduct == 0x6001) {
  44. // FTDI FT232
  45. println("len = ", len);
  46. if (len < 23) return false;
  47. if (descriptors[0] != 9) return false; // length 9
  48. if (descriptors[9] != 7) return false; // length 7
  49. if (descriptors[10] != 5) return false; // ep desc
  50. uint32_t rxep = descriptors[11];
  51. if (descriptors[12] != 2) return false; // bulk type
  52. if (descriptors[13] != 64) return false; // size 64
  53. if (descriptors[14] != 0) return false;
  54. if (descriptors[16] != 7) return false; // length 7
  55. if (descriptors[17] != 5) return false; // ep desc
  56. uint32_t txep = descriptors[18];
  57. if (descriptors[19] != 2) return false; // bulk type
  58. if (descriptors[20] != 64) return false; // size 64
  59. if (descriptors[21] != 0) return false;
  60. if (!check_rxtx_ep(rxep, txep)) return false;
  61. print("FTDI, rxep=", rxep & 15);
  62. println(", txep=", txep);
  63. if (!init_buffers(64, 64)) return false;
  64. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, 64);
  65. if (!rxpipe) return false;
  66. txpipe = new_Pipe(dev, 2, txep, 0, 64);
  67. if (!txpipe) {
  68. // TODO: free rxpipe
  69. return false;
  70. }
  71. sertype = FTDI;
  72. rxpipe->callback_function = rx_callback;
  73. queue_Data_Transfer(rxpipe, rx1, 64, this);
  74. rxstate = 1;
  75. if (rxsize > 128) {
  76. queue_Data_Transfer(rxpipe, rx2, 64, this);
  77. rxstate = 3;
  78. }
  79. txstate = 0;
  80. txpipe->callback_function = tx_callback;
  81. baudrate = 115200;
  82. pending_control = 0x0F;
  83. mk_setup(setup, 0x40, 0, 0, 0, 0); // reset port
  84. queue_Control_Transfer(dev, &setup, NULL, this);
  85. control_queued = true;
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. // check if two legal endpoints, 1 receive & 1 transmit
  92. bool USBSerial::check_rxtx_ep(uint32_t &rxep, uint32_t &txep)
  93. {
  94. if ((rxep & 0x0F) == 0) return false;
  95. if ((txep & 0x0F) == 0) return false;
  96. uint32_t rxdir = rxep & 0xF0;
  97. uint32_t txdir = txep & 0xF0;
  98. if (rxdir == 0x80 && txdir == 0x00) {
  99. return true;
  100. }
  101. if (rxdir == 0x00 && txdir == 0x80) {
  102. std::swap(rxep, txep);
  103. return true;
  104. }
  105. return false;
  106. }
  107. // initialize buffer sizes and pointers
  108. bool USBSerial::init_buffers(uint32_t rsize, uint32_t tsize)
  109. {
  110. // buffer must be able to hold 2 of each packet, plus have room to
  111. if (sizeof(bigbuffer) < (rsize + tsize) * 3 + 2) return false;
  112. rx1 = (uint8_t *)bigbuffer;
  113. rx2 = rx1 + rsize;
  114. tx1 = rx2 + rsize;
  115. tx2 = tx1 + tsize;
  116. rxbuf = tx2 + tsize;
  117. // FIXME: this assume 50-50 split - not true when rsize != tsize
  118. rxsize = (sizeof(bigbuffer) - (rsize + tsize) * 2) / 2;
  119. txsize = rxsize;
  120. txbuf = rxbuf + rxsize;
  121. rxhead = 0;
  122. rxtail = 0;
  123. txhead = 0;
  124. txtail = 0;
  125. rxstate = 0;
  126. return true;
  127. }
  128. void USBSerial::disconnect()
  129. {
  130. }
  131. /************************************************************/
  132. // Control Transfer For Configuration
  133. /************************************************************/
  134. void USBSerial::control(const Transfer_t *transfer)
  135. {
  136. println("control callback (serial)");
  137. control_queued = false;
  138. // set data format
  139. if (pending_control & 1) {
  140. pending_control &= ~1;
  141. mk_setup(setup, 0x40, 4, 8, 0, 0); // data format 8N1
  142. queue_Control_Transfer(device, &setup, NULL, this);
  143. control_queued = true;
  144. return;
  145. }
  146. // set baud rate
  147. if (pending_control & 2) {
  148. pending_control &= ~2;
  149. uint32_t baudval = 3000000 / baudrate;
  150. mk_setup(setup, 0x40, 3, baudval, 0, 0);
  151. queue_Control_Transfer(device, &setup, NULL, this);
  152. control_queued = true;
  153. return;
  154. }
  155. // configure flow control
  156. if (pending_control & 4) {
  157. pending_control &= ~4;
  158. mk_setup(setup, 0x40, 2, 0, 0, 0);
  159. queue_Control_Transfer(device, &setup, NULL, this);
  160. control_queued = true;
  161. return;
  162. }
  163. // set DTR
  164. if (pending_control & 8) {
  165. pending_control &= ~8;
  166. mk_setup(setup, 0x40, 1, 0x0101, 0, 0);
  167. queue_Control_Transfer(device, &setup, NULL, this);
  168. control_queued = true;
  169. return;
  170. }
  171. }
  172. /************************************************************/
  173. // Interrupt-based Data Movement
  174. /************************************************************/
  175. void USBSerial::rx_callback(const Transfer_t *transfer)
  176. {
  177. if (!transfer->driver) return;
  178. ((USBSerial *)(transfer->driver))->rx_data(transfer);
  179. }
  180. void USBSerial::tx_callback(const Transfer_t *transfer)
  181. {
  182. if (!transfer->driver) return;
  183. ((USBSerial *)(transfer->driver))->tx_data(transfer);
  184. }
  185. void USBSerial::rx_data(const Transfer_t *transfer)
  186. {
  187. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  188. // first update rxstate bitmask, since buffer is no longer queued
  189. if (transfer->buffer == rx1) {
  190. rxstate &= 0xFE;
  191. } else if (transfer->buffer == rx2) {
  192. rxstate &= 0xFD;
  193. }
  194. // get start of data and actual length
  195. const uint8_t *p = (const uint8_t *)transfer->buffer;
  196. if (sertype == FTDI) {
  197. if (len >= 2) {
  198. p += 2;
  199. len -= 2;
  200. } else {
  201. len = 0;
  202. }
  203. }
  204. //if (len > 0) {
  205. //print("rx: ");
  206. //print_hexbytes(p, len);
  207. //}
  208. // Copy data from packet buffer to circular buffer.
  209. // Assume the buffer will always have space, since we
  210. // check before queuing the buffers
  211. uint32_t head = rxhead;
  212. uint32_t tail = rxtail;
  213. if (++head >= rxsize) head = 0;
  214. uint32_t avail;
  215. if (len > 0) {
  216. //print("head=", head);
  217. //print(", tail=", tail);
  218. avail = rxsize - head;
  219. //print(", avail=", avail);
  220. //println(", rxsize=", rxsize);
  221. if (avail > len) avail = len;
  222. memcpy(rxbuf + head, p, avail);
  223. if (len <= avail) {
  224. head += avail - 1;
  225. if (head >= rxsize) head = 0;
  226. } else {
  227. head = len - avail - 1;
  228. memcpy(rxbuf, p + avail, head + 1);
  229. }
  230. rxhead = head;
  231. }
  232. // TODO: can be this more efficient? We know from above which
  233. // buffer is no longer queued, so possible skip most of this work?
  234. rx_queue_packets(head, tail);
  235. }
  236. // re-queue packet buffer(s) if possible
  237. void USBSerial::rx_queue_packets(uint32_t head, uint32_t tail)
  238. {
  239. uint32_t avail;
  240. if (head >= tail) {
  241. avail = rxsize - 1 - head + tail;
  242. } else {
  243. avail = tail - head - 1;
  244. }
  245. uint32_t packetsize = rx2 - rx1;
  246. if (avail >= packetsize) {
  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. if ((rxstate & 0x03) != 0x03 && avail >= packetsize * 2) {
  255. if ((rxstate & 0x01) == 0) {
  256. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  257. rxstate |= 0x01;
  258. } else if ((rxstate & 0x02) == 0) {
  259. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  260. rxstate |= 0x02;
  261. }
  262. }
  263. }
  264. }
  265. void USBSerial::tx_data(const Transfer_t *transfer)
  266. {
  267. uint32_t mask;
  268. uint8_t *p = (uint8_t *)transfer->buffer;
  269. if (p == tx1) {
  270. println("tx1:");
  271. mask = 1;
  272. //txstate &= 0xFE;
  273. } else if (p == tx2) {
  274. println("tx2:");
  275. mask = 2;
  276. //txstate &= 0xFD;
  277. } else {
  278. return; // should never happen
  279. }
  280. // check how much more data remains in the transmit buffer
  281. uint32_t head = txhead;
  282. uint32_t tail = txtail;
  283. uint32_t count;
  284. if (head >= tail) {
  285. count = head - tail;
  286. } else {
  287. count = txsize + head - tail;
  288. }
  289. uint32_t packetsize = tx2 - tx1;
  290. if (count < packetsize) {
  291. // not enough data in buffer to fill a full packet
  292. txstate &= ~mask;
  293. return;
  294. }
  295. // immediately transmit another full packet, if we have enough data
  296. println("TX:moar data!!!!");
  297. if (++tail >= txsize) tail = 0;
  298. uint32_t n = txsize - tail;
  299. if (n > packetsize) n = packetsize;
  300. memcpy(p, txbuf + tail, n);
  301. if (n >= packetsize) {
  302. tail += n - 1;
  303. if (tail >= txsize) tail = 0;
  304. } else {
  305. uint32_t len = packetsize - n;
  306. memcpy(p + n, txbuf, len);
  307. tail = len - 1;
  308. }
  309. txtail = tail;
  310. queue_Data_Transfer(txpipe, p, packetsize, this);
  311. }
  312. void USBSerial::timer_event(USBDriverTimer *whichTimer)
  313. {
  314. println("txtimer");
  315. uint32_t count;
  316. uint32_t head = txhead;
  317. uint32_t tail = txtail;
  318. if (head == tail) {
  319. return; // nothing to transmit
  320. } else if (head > tail) {
  321. count = head - tail;
  322. } else {
  323. count = txsize + head - tail;
  324. }
  325. uint8_t *p;
  326. if ((txstate & 0x01) == 0) {
  327. p = tx1;
  328. txstate |= 0x01;
  329. } else if ((txstate & 0x02) == 0) {
  330. p = tx2;
  331. txstate |= 0x02;
  332. } else {
  333. txtimer.start(1200);
  334. return; // no outgoing buffers available, try again later
  335. }
  336. if (++tail >= txsize) tail = 0;
  337. uint32_t n = txsize - tail;
  338. if (n > count) n = count;
  339. memcpy(p, txbuf + tail, n);
  340. if (n >= count) {
  341. tail += n - 1;
  342. if (tail >= txsize) tail = 0;
  343. } else {
  344. uint32_t len = count - n;
  345. memcpy(p + n, txbuf, len);
  346. tail = len - 1;
  347. }
  348. txtail = tail;
  349. queue_Data_Transfer(txpipe, p, count, this);
  350. }
  351. /************************************************************/
  352. // User Functions - must disable USBHQ IRQ for EHCI access
  353. /************************************************************/
  354. void USBSerial::begin(uint32_t baud, uint32_t format)
  355. {
  356. NVIC_DISABLE_IRQ(IRQ_USBHS);
  357. baudrate = baud;
  358. pending_control |= 2;
  359. if (!control_queued) control(NULL);
  360. NVIC_ENABLE_IRQ(IRQ_USBHS);
  361. }
  362. void USBSerial::end(void)
  363. {
  364. // TODO: lower DTR
  365. }
  366. int USBSerial::available(void)
  367. {
  368. if (!device) return 0;
  369. uint32_t head = rxhead;
  370. uint32_t tail = rxtail;
  371. if (head >= tail) return head - tail;
  372. return rxsize + head - tail;
  373. }
  374. int USBSerial::peek(void)
  375. {
  376. if (!device) return -1;
  377. uint32_t head = rxhead;
  378. uint32_t tail = rxtail;
  379. if (head == tail) return -1;
  380. if (++tail >= rxsize) tail = 0;
  381. return rxbuf[tail];
  382. }
  383. int USBSerial::read(void)
  384. {
  385. if (!device) return -1;
  386. uint32_t head = rxhead;
  387. uint32_t tail = rxtail;
  388. if (head == tail) return -1;
  389. if (++tail >= rxsize) tail = 0;
  390. int c = rxbuf[tail];
  391. rxtail = tail;
  392. if ((rxstate & 0x03) != 0x03) {
  393. NVIC_DISABLE_IRQ(IRQ_USBHS);
  394. rx_queue_packets(head, tail);
  395. NVIC_ENABLE_IRQ(IRQ_USBHS);
  396. }
  397. return c;
  398. }
  399. int USBSerial::availableForWrite()
  400. {
  401. if (!device) return 0;
  402. uint32_t head = txhead;
  403. uint32_t tail = txtail;
  404. if (head >= tail) return txsize - 1 - head + tail;
  405. return tail - head - 1;
  406. }
  407. size_t USBSerial::write(uint8_t c)
  408. {
  409. if (!device) return 0;
  410. uint32_t head = txhead;
  411. if (++head >= txsize) head = 0;
  412. while (txtail == head) {
  413. // wait...
  414. }
  415. txbuf[head] = c;
  416. txhead = head;
  417. //print("head=", head);
  418. //println(", tail=", txtail);
  419. // if full packet in buffer and tx packet ready, queue it
  420. NVIC_DISABLE_IRQ(IRQ_USBHS);
  421. uint32_t tail = txtail;
  422. if ((txstate & 0x03) != 0x03) {
  423. // at least one packet buffer is ready to transmit
  424. uint32_t count;
  425. if (head >= tail) {
  426. count = head - tail;
  427. } else {
  428. count = txsize + head - tail;
  429. }
  430. uint32_t packetsize = tx2 - tx1;
  431. if (count >= packetsize) {
  432. //println("txsize=", txsize);
  433. uint8_t *p;
  434. if ((txstate & 0x01) == 0) {
  435. p = tx1;
  436. txstate |= 0x01;
  437. } else /* if ((txstate & 0x02) == 0) */ {
  438. p = tx2;
  439. txstate |= 0x02;
  440. }
  441. // copy data to packet buffer
  442. if (++tail >= txsize) tail = 0;
  443. uint32_t n = txsize - tail;
  444. if (n > packetsize) n = packetsize;
  445. //print("memcpy, offset=", tail);
  446. //println(", len=", n);
  447. memcpy(p, txbuf + tail, n);
  448. if (n >= packetsize) {
  449. tail += n - 1;
  450. if (tail >= txsize) tail = 0;
  451. } else {
  452. //n = txsize - n;
  453. uint32_t len = packetsize - n;
  454. //println("memcpy, offset=0, len=", len);
  455. memcpy(p + n, txbuf, len);
  456. tail = len - 1;
  457. }
  458. txtail = tail;
  459. //println("queue tx packet, newtail=", tail);
  460. queue_Data_Transfer(txpipe, p, packetsize, this);
  461. NVIC_ENABLE_IRQ(IRQ_USBHS);
  462. return 1;
  463. }
  464. }
  465. // otherwise, set a latency timer to later transmit partial packet
  466. txtimer.stop();
  467. txtimer.start(3500);
  468. NVIC_ENABLE_IRQ(IRQ_USBHS);
  469. return 1;
  470. }