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.

1000 lines
31KB

  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. // Control Transfer For Configuration
  29. /************************************************************/
  30. typedef struct {
  31. uint32_t dwDTERate; // Data Terminal Rate in bits per second
  32. uint8_t bCharFormat; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  33. uint8_t bParityType; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  34. uint8_t bDataBits; // Data bits (5, 6, 7, 8 or 16)
  35. } LINE_CODING;
  36. /************************************************************/
  37. // Initialization and claiming of devices & interfaces
  38. /************************************************************/
  39. void USBSerial::init()
  40. {
  41. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  42. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  43. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  44. driver_ready_for_device(this);
  45. }
  46. bool USBSerial::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  47. {
  48. // only claim at interface level
  49. println("USBSerial claim this=", (uint32_t)this, HEX);
  50. print("vid=", dev->idVendor, HEX);
  51. print(", pid=", dev->idProduct, HEX);
  52. print(", bDeviceClass = ", dev->bDeviceClass);
  53. print(", bDeviceSubClass = ", dev->bDeviceSubClass);
  54. println(", bDeviceProtocol = ", dev->bDeviceProtocol);
  55. print_hexbytes(descriptors, len);
  56. if (type == 0) {
  57. if (dev->idVendor == 0x0403 && dev->idProduct == 0x6001) {
  58. // FTDI FT232
  59. println("len = ", len);
  60. if (len < 23) return false;
  61. if (descriptors[0] != 9) return false; // length 9
  62. if (descriptors[9] != 7) return false; // length 7
  63. if (descriptors[10] != 5) return false; // ep desc
  64. uint32_t rxep = descriptors[11];
  65. if (descriptors[12] != 2) return false; // bulk type
  66. if (descriptors[13] != 64) return false; // size 64
  67. if (descriptors[14] != 0) return false;
  68. if (descriptors[16] != 7) return false; // length 7
  69. if (descriptors[17] != 5) return false; // ep desc
  70. uint32_t txep = descriptors[18];
  71. if (descriptors[19] != 2) return false; // bulk type
  72. if (descriptors[20] != 64) return false; // size 64
  73. if (descriptors[21] != 0) return false;
  74. if (!check_rxtx_ep(rxep, txep)) return false;
  75. print("FTDI, rxep=", rxep & 15);
  76. println(", txep=", txep);
  77. if (!init_buffers(64, 64)) return false;
  78. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, 64);
  79. if (!rxpipe) return false;
  80. txpipe = new_Pipe(dev, 2, txep, 0, 64);
  81. if (!txpipe) {
  82. // TODO: free rxpipe
  83. return false;
  84. }
  85. sertype = FTDI;
  86. rxpipe->callback_function = rx_callback;
  87. queue_Data_Transfer(rxpipe, rx1, 64, this);
  88. rxstate = 1;
  89. if (rxsize > 128) {
  90. queue_Data_Transfer(rxpipe, rx2, 64, this);
  91. rxstate = 3;
  92. }
  93. txstate = 0;
  94. txpipe->callback_function = tx_callback;
  95. baudrate = 115200;
  96. pending_control = 0x0F;
  97. mk_setup(setup, 0x40, 0, 0, 0, 0); // reset port
  98. queue_Control_Transfer(dev, &setup, NULL, this);
  99. control_queued = true;
  100. return true;
  101. } else if ((dev->bDeviceClass == 2) && (dev->bDeviceSubClass == 0)) {
  102. // It is a communication device see if we can extract the data...
  103. // Try some ttyACM types?
  104. // This code may be similar to MIDI code.
  105. // But first pass see if we can simply look at the interface...
  106. // Lets walk through end points and see if we
  107. // can find an RX and TX bulk transfer end point.
  108. // 0 1 2 3 4 5 6 7 8 *9 10 1 2 3 *4 5 6 7 *8 9 20 1 2 *3 4 5 6 7 8 9*30 1 2 3 4 5 6 7 8 *9 40 1 2 3 4 5 *6 7 8 9 50 1 2
  109. // USB2AX
  110. //09 04 00 00 01 02 02 01 00 05 24 00 10 01 04 24 02 06 05 24 06 00 01 07 05 82 03 08 00 FF 09 04 01 00 02 0A 00 00 00 07 05 04 02 10 00 01 07 05 83 02 10 00 01
  111. //09 04 01 00 02 0A 00 00 00 07 05 04 02 10 00 01 07 05 83 02 10 00 01
  112. // Teensy 3.6
  113. //09 04 00 00 01 02 02 01 00 05 24 00 10 01 05 24 01 01 01 04 24 02 06 05 24 06 00 01 07 05 82 03 10 00 40 09 04 01 00 02 0A 00 00 00 07 05 03 02 40 00 00 07 05 84 02 40 00 00
  114. //09 04 01 00 02 0A 00 00 00 07 05 03 02 40 00 00 07 05 84 02 40 00 00
  115. const uint8_t *p = descriptors;
  116. const uint8_t *end = p + len;
  117. if (p[0] != 9 || p[1] != 4) return false; // interface descriptor
  118. //println(" bInterfaceClass=", p[5]);
  119. //println(" bInterfaceSubClass=", p[6]);
  120. if (p[5] != 2) return false; // bInterfaceClass: 2 Communications
  121. if (p[6] != 2) return false; // bInterfaceSubClass: 2 serial
  122. p += 9;
  123. println(" Interface is Serial");
  124. uint8_t rx_ep = 0;
  125. uint8_t tx_ep = 0;
  126. uint16_t rx_size = 0;
  127. uint16_t tx_size = 0;
  128. interface = 0; // clear out any interface numbers passed in.
  129. while (p < end) {
  130. len = *p;
  131. if (len < 4) return false;
  132. if (p + len > end) return false; // reject if beyond end of data
  133. uint32_t type = p[1];
  134. //println("type: ", type);
  135. // Unlike Audio, we need to look at Interface as our endpoints are after them...
  136. if (type == 4 ) { // Interface - lets remember it's number...
  137. interface = p[2];
  138. println(" Interface: ", interface);
  139. }
  140. else if (type == 0x24) { // 0x24 = CS_INTERFACE,
  141. uint32_t subtype = p[2];
  142. print(" CS_INTERFACE - subtype: ", subtype);
  143. if (len >= 4) print(" ", p[3], HEX);
  144. if (len >= 5) print(" ", p[4], HEX);
  145. if (len >= 6) print(" ", p[5], HEX);
  146. switch (subtype) {
  147. case 0: println(" - Header Functional Descriptor"); break;
  148. case 1: println(" - Call Management Functional"); break;
  149. case 2: println(" - Abstract Control Management"); break;
  150. case 4: println(" - Telephone Ringer"); break;
  151. case 6: println(" - union Functional"); break;
  152. default: println(" - ??? other"); break;
  153. }
  154. // First pass ignore...
  155. } else if (type == 5) {
  156. // endpoint descriptor
  157. if (p[0] < 7) return false; // at least 7 bytes
  158. if (p[3] == 2) { // First try ignore the first one which is interrupt...
  159. println(" Endpoint: ", p[2], HEX);
  160. switch (p[2] & 0xF0) {
  161. case 0x80:
  162. // IN endpoint
  163. if (rx_ep == 0) {
  164. rx_ep = p[2] & 0x0F;
  165. rx_size = p[4] | (p[5] << 8);
  166. println(" rx_size = ", rx_size);
  167. }
  168. break;
  169. case 0x00:
  170. // OUT endpoint
  171. if (tx_ep == 0) {
  172. tx_ep = p[2];
  173. tx_size = p[4] | (p[5] << 8);
  174. println(" tx_size = ", tx_size);
  175. }
  176. break;
  177. default:
  178. println(" invalid end point: ", p[2]);
  179. return false;
  180. }
  181. }
  182. } else {
  183. println(" Unknown type: ", type);
  184. return false; // unknown
  185. }
  186. p += len;
  187. }
  188. print(" exited loop rx:", rx_ep);
  189. println(", tx:", tx_ep);
  190. if (!rx_ep || !tx_ep) return false; // did not get our two end points
  191. if (!init_buffers(rx_size, tx_size)) return false;
  192. rxpipe = new_Pipe(dev, 2, rx_ep & 15, 1, rx_size);
  193. if (!rxpipe) return false;
  194. txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size);
  195. if (!txpipe) {
  196. // TODO: free rxpipe
  197. return false;
  198. }
  199. sertype = CDCACM;
  200. rxpipe->callback_function = rx_callback;
  201. queue_Data_Transfer(rxpipe, rx1, (rx_size < 64)? rx_size : 64, this);
  202. rxstate = 1;
  203. if (rx_size > 128) {
  204. queue_Data_Transfer(rxpipe, rx2, rx_size, this);
  205. rxstate = 3;
  206. }
  207. txstate = 0;
  208. txpipe->callback_function = tx_callback;
  209. baudrate = 115200;
  210. // Wish I could just call Control to do the output... Maybe can defer until the user calls begin()
  211. // control requires that device is setup which is not until this call completes...
  212. println("Control - CDCACM DTR...");
  213. // Need to setup the data the line coding data
  214. mk_setup(setup, 0x21, 0x22, 3, 0, 0);
  215. queue_Control_Transfer(dev, &setup, NULL, this);
  216. control_queued = true;
  217. pending_control = 0x0; // Maybe don't need to do...
  218. return true;
  219. }
  220. #if 1
  221. // BUGBUG: Note: there are probably more vendor/product pairs.. Maybe should create table of them
  222. if (dev->idVendor == 0x67B && dev->idProduct == 0x2303) {
  223. // Prolific Technology, Inc. PL2303 Serial Port
  224. println("len = ", len);
  225. uint8_t count_end_points = descriptors[4];
  226. if (count_end_points < 2) return false; // not enough end points
  227. if (len < 23) return false;
  228. if (descriptors[0] != 9) return false; // length 9
  229. // Lets walk through end points and see if we
  230. // can find an RX and TX bulk transfer end point.
  231. //vid=67B, pid=2303
  232. // 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 20 1 2 3 4 5 6 7 8 9
  233. //09 04 00 00 03 FF 00 00 00 07 05 81 03 0A 00 01 07 05 02 02 40 00 00 07 05 83 02 40 00 00
  234. uint32_t rxep = 0;
  235. uint32_t txep = 0;
  236. uint32_t descriptor_index = 9;
  237. while (count_end_points-- && ((rxep == 0) || txep == 0)) {
  238. if (descriptors[descriptor_index] != 7) return false; // length 7
  239. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  240. if ((descriptors[descriptor_index+3] == 2)
  241. && (descriptors[descriptor_index+4] == 64)
  242. && (descriptors[descriptor_index+5] == 0)) {
  243. // have a bulk EP size
  244. if (descriptors[descriptor_index+2] & 0x80 ) {
  245. rxep = descriptors[descriptor_index+2];
  246. } else {
  247. txep = descriptors[descriptor_index+2];
  248. }
  249. }
  250. descriptor_index += 7; // setup to look at next one...
  251. }
  252. // Try to verify the end points.
  253. if (!check_rxtx_ep(rxep, txep)) return false;
  254. print("FTDI, rxep=", rxep & 15);
  255. println(", txep=", txep);
  256. if (!init_buffers(64, 64)) return false;
  257. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, 64);
  258. if (!rxpipe) return false;
  259. txpipe = new_Pipe(dev, 2, txep, 0, 64);
  260. if (!txpipe) {
  261. // TODO: free rxpipe
  262. return false;
  263. }
  264. sertype = PL2303;
  265. rxpipe->callback_function = rx_callback;
  266. queue_Data_Transfer(rxpipe, rx1, 64, this);
  267. rxstate = 1;
  268. if (rxsize > 128) {
  269. queue_Data_Transfer(rxpipe, rx2, 64, this);
  270. rxstate = 3;
  271. }
  272. txstate = 0;
  273. txpipe->callback_function = tx_callback;
  274. baudrate = 115200;
  275. // Lets see if it will handle the same CDCACM - messages?
  276. println("PL2303: readRegister(0x04)");
  277. // Need to setup the data the line coding data
  278. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  279. queue_Control_Transfer(dev, &setup, setupdata, this);
  280. control_queued = true;
  281. setup_state = 1; // We are at step one of setup...
  282. pending_control = 0x3f; // Maybe don't need to do...
  283. return true;
  284. }
  285. #endif
  286. } else if (type != 1) return false;
  287. #if 1
  288. // Try some ttyACM types?
  289. // This code may be similar to MIDI code.
  290. // But first pass see if we can simply look at the interface...
  291. // Lets walk through end points and see if we
  292. // can find an RX and TX bulk transfer end point.
  293. // 0 1 2 3 4 5 6 7 8 *9 10 1 2 3 *4 5 6 7 *8 9 20 1 2 *3 4 5 6 7 8 9*30 1 2 3 4 5 6 7 8 *9 40 1 2 3 4 5 *6 7 8 9 50 1 2
  294. // USB2AX
  295. //09 04 00 00 01 02 02 01 00 05 24 00 10 01 04 24 02 06 05 24 06 00 01 07 05 82 03 08 00 FF
  296. //09 04 01 00 02 0A 00 00 00 07 05 04 02 10 00 01 07 05 83 02 10 00 01
  297. // Teensy 3.6
  298. //09 04 00 00 01 02 02 01 00 05 24 00 10 01 05 24 01 01 01 04 24 02 06 05 24 06 00 01 07 05 82 03 10 00 40
  299. //09 04 01 00 02 0A 00 00 00 07 05 03 02 40 00 00 07 05 84 02 40 00 00
  300. if (descriptors[0] != 9 || descriptors[1] != 4) return false; // interface descriptor
  301. if (descriptors[4] < 2) return false; // less than 2 end points
  302. if (descriptors[5] != 0xA) return false; // bInterfaceClass, 0xa = CDC data
  303. if (descriptors[6] != 0) return false; // bInterfaceSubClass
  304. if (descriptors[7] != 0) return false; // bInterfaceProtocol, 1 = Keyboard
  305. if (descriptors[9] != 7) return false; // length 7
  306. if (descriptors[10] != 5) return false; // ep desc
  307. uint32_t txep = descriptors[11];
  308. uint32_t txsize = descriptors[13];
  309. if (descriptors[12] != 2) return false; // bulk type
  310. if (descriptors[13] > 64) return false; // size 64 Max
  311. if (descriptors[14] != 0) return false;
  312. if (descriptors[16] != 7) return false; // length 7
  313. if (descriptors[17] != 5) return false; // ep desc
  314. uint32_t rxep = descriptors[18];
  315. uint32_t rxsize = descriptors[20];
  316. if (descriptors[19] != 2) return false; // bulk type
  317. if (descriptors[20] > 64) return false; // size 64 Max
  318. if (descriptors[21] != 0) return false;
  319. if (!check_rxtx_ep(rxep, txep)) return false;
  320. interface = descriptors[2];
  321. print("CDC, rxep=", rxep & 15);
  322. println(", txep=", txep);
  323. if (!init_buffers(rxsize, txsize)) return false;
  324. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, rxsize);
  325. if (!rxpipe) return false;
  326. txpipe = new_Pipe(dev, 2, txep, 0, txsize);
  327. if (!txpipe) {
  328. // TODO: free rxpipe
  329. return false;
  330. }
  331. sertype = CDCACM;
  332. rxpipe->callback_function = rx_callback;
  333. queue_Data_Transfer(rxpipe, rx1, 64, this);
  334. rxstate = 1;
  335. if (rxsize > 128) {
  336. queue_Data_Transfer(rxpipe, rx2, 64, this);
  337. rxstate = 3;
  338. }
  339. txstate = 0;
  340. txpipe->callback_function = tx_callback;
  341. // See if we can do just the inteface...
  342. baudrate = 115200;
  343. println("Control - CDCACM LINE_CODING");
  344. setupdata[0] = 0; // Setup baud rate 115200 - 0x1C200
  345. setupdata[1] = 0xc2;
  346. setupdata[2] = 0x1;
  347. setupdata[3] = 0;
  348. setupdata[4] = 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  349. setupdata[5] = 0; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  350. setupdata[6] = 8; // Data bits (5, 6, 7, 8 or 16)
  351. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  352. queue_Control_Transfer(dev, &setup, setupdata, this);
  353. pending_control = 0x04; // Maybe don't need to do...
  354. control_queued = true;
  355. return true;
  356. #else
  357. return false;
  358. #endif
  359. }
  360. // check if two legal endpoints, 1 receive & 1 transmit
  361. bool USBSerial::check_rxtx_ep(uint32_t &rxep, uint32_t &txep)
  362. {
  363. if ((rxep & 0x0F) == 0) return false;
  364. if ((txep & 0x0F) == 0) return false;
  365. uint32_t rxdir = rxep & 0xF0;
  366. uint32_t txdir = txep & 0xF0;
  367. if (rxdir == 0x80 && txdir == 0x00) {
  368. return true;
  369. }
  370. if (rxdir == 0x00 && txdir == 0x80) {
  371. std::swap(rxep, txep);
  372. return true;
  373. }
  374. return false;
  375. }
  376. // initialize buffer sizes and pointers
  377. bool USBSerial::init_buffers(uint32_t rsize, uint32_t tsize)
  378. {
  379. // buffer must be able to hold 2 of each packet, plus buffer
  380. // space to hold RX and TX data.
  381. if (sizeof(bigbuffer) < (rsize + tsize) * 3 + 2) return false;
  382. rx1 = (uint8_t *)bigbuffer;
  383. rx2 = rx1 + rsize;
  384. tx1 = rx2 + rsize;
  385. tx2 = tx1 + tsize;
  386. rxbuf = tx2 + tsize;
  387. // FIXME: this assume 50-50 split - not true when rsize != tsize
  388. rxsize = (sizeof(bigbuffer) - (rsize + tsize) * 2) / 2;
  389. txsize = rxsize;
  390. txbuf = rxbuf + rxsize;
  391. rxhead = 0;
  392. rxtail = 0;
  393. txhead = 0;
  394. txtail = 0;
  395. rxstate = 0;
  396. return true;
  397. }
  398. void USBSerial::disconnect()
  399. {
  400. }
  401. void USBSerial::control(const Transfer_t *transfer)
  402. {
  403. println("control callback (serial) ", pending_control, HEX);
  404. control_queued = false;
  405. // We will split this up by Serial type, maybe different functions?
  406. //-------------------------------------------------------------------------
  407. // First FTDI
  408. if (sertype == FTDI) {
  409. if (pending_control & 1) {
  410. pending_control &= ~1;
  411. // set data format
  412. mk_setup(setup, 0x40, 4, 8, 0, 0); // data format 8N1
  413. queue_Control_Transfer(device, &setup, NULL, this);
  414. control_queued = true;
  415. return;
  416. }
  417. // set baud rate
  418. if (pending_control & 2) {
  419. pending_control &= ~2;
  420. uint32_t baudval = 3000000 / baudrate;
  421. mk_setup(setup, 0x40, 3, baudval, 0, 0);
  422. queue_Control_Transfer(device, &setup, NULL, this);
  423. control_queued = true;
  424. return;
  425. }
  426. // configure flow control
  427. if (pending_control & 4) {
  428. pending_control &= ~4;
  429. mk_setup(setup, 0x40, 2, 0, 1, 0);
  430. queue_Control_Transfer(device, &setup, NULL, this);
  431. control_queued = true;
  432. return;
  433. }
  434. // set DTR
  435. if (pending_control & 8) {
  436. pending_control &= ~8;
  437. mk_setup(setup, 0x40, 1, 0x0101, 0, 0);
  438. queue_Control_Transfer(device, &setup, NULL, this);
  439. control_queued = true;
  440. return;
  441. }
  442. }
  443. //-------------------------------------------------------------------------
  444. // Now CDCACM
  445. if (sertype == CDCACM) {
  446. if (pending_control & 2) {
  447. pending_control &= ~2;
  448. // Should probably use data structure, but that may depend on byte ordering...
  449. setupdata[0] = (baudrate) & 0xff; // Setup baud rate 115200 - 0x1C200
  450. setupdata[1] = (baudrate >> 8) & 0xff;
  451. setupdata[2] = (baudrate >> 16) & 0xff;
  452. setupdata[3] = (baudrate >> 24) & 0xff;
  453. setupdata[4] = 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  454. setupdata[5] = 0; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  455. setupdata[6] = 8; // Data bits (5, 6, 7, 8 or 16)
  456. print("CDCACM setup: ");
  457. print_hexbytes(&setupdata, 7);
  458. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  459. queue_Control_Transfer(device, &setup, setupdata, this);
  460. control_queued = true;
  461. return;
  462. }
  463. // configure flow control
  464. if (pending_control & 4) {
  465. pending_control &= ~4;
  466. println("Control - 0x21,0x22, 0x3");
  467. // Need to setup the data the line coding data
  468. mk_setup(setup, 0x21, 0x22, 3, 0, 0);
  469. queue_Control_Transfer(device, &setup, NULL, this);
  470. control_queued = true;
  471. return;
  472. }
  473. }
  474. //-------------------------------------------------------------------------
  475. // Now PL2303 - Which appears to be a little more complicated
  476. if (sertype == PL2303) {
  477. if (pending_control & 1) {
  478. // Still in larger setup state mode
  479. switch (setup_state) {
  480. case 1:
  481. println("PL2303: writeRegister(0x04, 0x00)");
  482. mk_setup(setup, 0x40, 1, 0x0404, 0, 0); //
  483. queue_Control_Transfer(device, &setup, NULL, this);
  484. setup_state = 2;
  485. control_queued = true;
  486. return;
  487. case 2:
  488. println("PL2303: readRegister(0x04)");
  489. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  490. queue_Control_Transfer(device, &setup, setupdata, this);
  491. control_queued = true;
  492. setup_state = 3;
  493. return;
  494. case 3:
  495. println("PL2303: v1 = readRegister(0x03)");
  496. mk_setup(setup, 0xC0, 0x1, 0x8383, 0, 1);
  497. queue_Control_Transfer(device, &setup, setupdata, this);
  498. control_queued = true;
  499. setup_state = 4;
  500. return;
  501. case 4:
  502. println("PL2303: readRegister(0x04)");
  503. // Do we need this value long term or we could just leave in setup data?
  504. pl2303_v1 = setupdata[0]; // save the first bye of version
  505. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  506. queue_Control_Transfer(device, &setup, setupdata, this);
  507. control_queued = true;
  508. setup_state = 5;
  509. return;
  510. case 5:
  511. println("PL2303: writeRegister(0x04, 0x01)");
  512. mk_setup(setup, 0x40, 1, 0x0404, 1, 0); //
  513. queue_Control_Transfer(device, &setup, NULL, this);
  514. setup_state = 6;
  515. control_queued = true;
  516. return;
  517. case 6:
  518. println("PL2303: readRegister(0x04)");
  519. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  520. queue_Control_Transfer(device, &setup, setupdata, this);
  521. control_queued = true;
  522. setup_state = 7;
  523. return;
  524. case 7:
  525. println("PL2303: v2 = readRegister(0x03)");
  526. mk_setup(setup, 0xC0, 0x1, 0x8383, 0, 1);
  527. queue_Control_Transfer(device, &setup, setupdata, this);
  528. control_queued = true;
  529. setup_state = 8;
  530. return;
  531. case 8:
  532. pl2303_v2 = setupdata[0]; // save the first bye of version
  533. print(" PL2303 Version ", pl2303_v1, HEX);
  534. println(":", pl2303_v2, HEX);
  535. println("PL2303: writeRegister(0, 1)");
  536. mk_setup(setup, 0x40, 1, 0, 1, 0); //
  537. queue_Control_Transfer(device, &setup, NULL, this);
  538. setup_state = 9;
  539. control_queued = true;
  540. return;
  541. case 9:
  542. println("PL2303: writeRegister(1, 0)");
  543. mk_setup(setup, 0x40, 1, 1, 0, 0); //
  544. queue_Control_Transfer(device, &setup, NULL, this);
  545. setup_state = 10;
  546. control_queued = true;
  547. return;
  548. case 10:
  549. println("PL2303: writeRegister(2, 44)");
  550. mk_setup(setup, 0x40, 1, 2, 0x44, 0); //
  551. queue_Control_Transfer(device, &setup, NULL, this);
  552. setup_state = 11;
  553. control_queued = true;
  554. return;
  555. case 11:
  556. println("PL2303: writeRegister(8, 0)");
  557. mk_setup(setup, 0x40, 1, 8, 0, 0); //
  558. queue_Control_Transfer(device, &setup, NULL, this);
  559. setup_state = 12;
  560. control_queued = true;
  561. return;
  562. case 12:
  563. println("PL2303: writeRegister(9, 0)");
  564. mk_setup(setup, 0x40, 1, 9, 0, 0); //
  565. queue_Control_Transfer(device, &setup, NULL, this);
  566. setup_state = 13;
  567. control_queued = true;
  568. return;
  569. case 13:
  570. println("PL2303: Read current Baud/control");
  571. mk_setup(setup, 0xA1, 0x21, 0, 0, 7);
  572. queue_Control_Transfer(device, &setup, setupdata, this);
  573. control_queued = true;
  574. break;
  575. }
  576. pending_control &= ~1; // We are finally going to leave this list and join the rest
  577. if (control_queued) return;
  578. }
  579. // set baud rate
  580. if (pending_control & 2) {
  581. pending_control &= ~2;
  582. // See what the read returned earlier
  583. print("PL2303: Returned configuration data: ");
  584. print_hexbytes(setupdata, 7);
  585. // Should probably use data structure, but that may depend on byte ordering...
  586. setupdata[0] = (baudrate) & 0xff; // Setup baud rate 115200 - 0x1C200
  587. setupdata[1] = (baudrate >> 8) & 0xff;
  588. setupdata[2] = (baudrate >> 16) & 0xff;
  589. setupdata[3] = (baudrate >> 24) & 0xff;
  590. setupdata[4] = 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  591. setupdata[5] = 0; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  592. setupdata[6] = 8; // Data bits (5, 6, 7, 8 or 16)
  593. print("PL2303: Set baud/control: ", baudrate, HEX);
  594. print(" = ");
  595. print_hexbytes(&setupdata, 7);
  596. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  597. queue_Control_Transfer(device, &setup, setupdata, this);
  598. control_queued = true;
  599. return;
  600. }
  601. if (pending_control & 4) {
  602. pending_control &= ~4;
  603. println("PL2303: writeRegister(0, 0)");
  604. mk_setup(setup, 0x40, 1, 0, 0, 0); //
  605. queue_Control_Transfer(device, &setup, NULL, this);
  606. control_queued = true;
  607. return;
  608. }
  609. if (pending_control & 8) {
  610. pending_control &= ~8;
  611. println("PL2303: Read current Baud/control");
  612. memset(setupdata, 0, sizeof(setupdata)); // clear it to see if we read it...
  613. mk_setup(setup, 0xA1, 0x21, 0, 0, 7);
  614. queue_Control_Transfer(device, &setup, setupdata, this);
  615. control_queued = true;
  616. }
  617. if (pending_control & 0x10) {
  618. pending_control &= ~0x10;
  619. print("PL2303: Returned configuration data: ");
  620. print_hexbytes(setupdata, 7);
  621. println("PL2303: 0x21, 0x22, 0x3");
  622. mk_setup(setup, 0x21, 0x22, 3, 0, 0); //
  623. queue_Control_Transfer(device, &setup, NULL, this);
  624. control_queued = true;
  625. return;
  626. }
  627. if (pending_control & 0x30) {
  628. pending_control &= ~0x30;
  629. println("PL2303: 0x21, 0x22, 0x3");
  630. mk_setup(setup, 0x21, 0x22, 3, 0, 0); //
  631. queue_Control_Transfer(device, &setup, NULL, this);
  632. control_queued = true;
  633. }
  634. }
  635. }
  636. /************************************************************/
  637. // Interrupt-based Data Movement
  638. /************************************************************/
  639. void USBSerial::rx_callback(const Transfer_t *transfer)
  640. {
  641. if (!transfer->driver) return;
  642. ((USBSerial *)(transfer->driver))->rx_data(transfer);
  643. }
  644. void USBSerial::tx_callback(const Transfer_t *transfer)
  645. {
  646. if (!transfer->driver) return;
  647. ((USBSerial *)(transfer->driver))->tx_data(transfer);
  648. }
  649. void USBSerial::rx_data(const Transfer_t *transfer)
  650. {
  651. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  652. // first update rxstate bitmask, since buffer is no longer queued
  653. if (transfer->buffer == rx1) {
  654. rxstate &= 0xFE;
  655. } else if (transfer->buffer == rx2) {
  656. rxstate &= 0xFD;
  657. }
  658. // get start of data and actual length
  659. const uint8_t *p = (const uint8_t *)transfer->buffer;
  660. if (sertype == FTDI) {
  661. if (len >= 2) {
  662. p += 2;
  663. len -= 2;
  664. } else {
  665. len = 0;
  666. }
  667. }
  668. if (len > 0) {
  669. print("rx: ");
  670. print_hexbytes(p, len);
  671. }
  672. // Copy data from packet buffer to circular buffer.
  673. // Assume the buffer will always have space, since we
  674. // check before queuing the buffers
  675. uint32_t head = rxhead;
  676. uint32_t tail = rxtail;
  677. if (++head >= rxsize) head = 0;
  678. uint32_t avail;
  679. if (len > 0) {
  680. //print("head=", head);
  681. //print(", tail=", tail);
  682. avail = rxsize - head;
  683. //print(", avail=", avail);
  684. //println(", rxsize=", rxsize);
  685. if (avail > len) avail = len;
  686. memcpy(rxbuf + head, p, avail);
  687. if (len <= avail) {
  688. head += avail - 1;
  689. if (head >= rxsize) head = 0;
  690. } else {
  691. head = len - avail - 1;
  692. memcpy(rxbuf, p + avail, head + 1);
  693. }
  694. rxhead = head;
  695. }
  696. // TODO: can be this more efficient? We know from above which
  697. // buffer is no longer queued, so possible skip most of this work?
  698. rx_queue_packets(head, tail);
  699. }
  700. // re-queue packet buffer(s) if possible
  701. void USBSerial::rx_queue_packets(uint32_t head, uint32_t tail)
  702. {
  703. uint32_t avail;
  704. if (head >= tail) {
  705. avail = rxsize - 1 - head + tail;
  706. } else {
  707. avail = tail - head - 1;
  708. }
  709. uint32_t packetsize = rx2 - rx1;
  710. if (avail >= packetsize) {
  711. if ((rxstate & 0x01) == 0) {
  712. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  713. rxstate |= 0x01;
  714. } else if ((rxstate & 0x02) == 0) {
  715. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  716. rxstate |= 0x02;
  717. }
  718. if ((rxstate & 0x03) != 0x03 && avail >= packetsize * 2) {
  719. if ((rxstate & 0x01) == 0) {
  720. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  721. rxstate |= 0x01;
  722. } else if ((rxstate & 0x02) == 0) {
  723. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  724. rxstate |= 0x02;
  725. }
  726. }
  727. }
  728. }
  729. void USBSerial::tx_data(const Transfer_t *transfer)
  730. {
  731. uint32_t mask;
  732. uint8_t *p = (uint8_t *)transfer->buffer;
  733. if (p == tx1) {
  734. println("tx1:");
  735. mask = 1;
  736. //txstate &= 0xFE;
  737. } else if (p == tx2) {
  738. println("tx2:");
  739. mask = 2;
  740. //txstate &= 0xFD;
  741. } else {
  742. return; // should never happen
  743. }
  744. // check how much more data remains in the transmit buffer
  745. uint32_t head = txhead;
  746. uint32_t tail = txtail;
  747. uint32_t count;
  748. if (head >= tail) {
  749. count = head - tail;
  750. } else {
  751. count = txsize + head - tail;
  752. }
  753. uint32_t packetsize = tx2 - tx1;
  754. if (count < packetsize) {
  755. // not enough data in buffer to fill a full packet
  756. txstate &= ~mask;
  757. return;
  758. }
  759. // immediately transmit another full packet, if we have enough data
  760. println("TX:moar data!!!!");
  761. if (++tail >= txsize) tail = 0;
  762. uint32_t n = txsize - tail;
  763. if (n > packetsize) n = packetsize;
  764. memcpy(p, txbuf + tail, n);
  765. if (n >= packetsize) {
  766. tail += n - 1;
  767. if (tail >= txsize) tail = 0;
  768. } else {
  769. uint32_t len = packetsize - n;
  770. memcpy(p + n, txbuf, len);
  771. tail = len - 1;
  772. }
  773. txtail = tail;
  774. queue_Data_Transfer(txpipe, p, packetsize, this);
  775. }
  776. void USBSerial::timer_event(USBDriverTimer *whichTimer)
  777. {
  778. println("txtimer");
  779. uint32_t count;
  780. uint32_t head = txhead;
  781. uint32_t tail = txtail;
  782. if (pending_control) {
  783. // We are still doing setup postpone for awhile..
  784. txtimer.start(1200);
  785. println(" Postpone: setup pending_control");
  786. return; // no outgoing buffers available, try again later
  787. }
  788. if (head == tail) {
  789. println(" *** Empty ***");
  790. return; // nothing to transmit
  791. } else if (head > tail) {
  792. count = head - tail;
  793. } else {
  794. count = txsize + head - tail;
  795. }
  796. uint8_t *p;
  797. if ((txstate & 0x01) == 0) {
  798. p = tx1;
  799. txstate |= 0x01;
  800. } else if ((txstate & 0x02) == 0) {
  801. p = tx2;
  802. txstate |= 0x02;
  803. } else {
  804. txtimer.start(1200);
  805. println(" *** No buffers ***");
  806. return; // no outgoing buffers available, try again later
  807. }
  808. if (++tail >= txsize) tail = 0;
  809. uint32_t n = txsize - tail;
  810. if (n > count) n = count;
  811. memcpy(p, txbuf + tail, n);
  812. if (n >= count) {
  813. tail += n - 1;
  814. if (tail >= txsize) tail = 0;
  815. } else {
  816. uint32_t len = count - n;
  817. memcpy(p + n, txbuf, len);
  818. tail = len - 1;
  819. }
  820. txtail = tail;
  821. print(" TX data (", count);
  822. print(") ");
  823. print_hexbytes(p, count);
  824. queue_Data_Transfer(txpipe, p, count, this);
  825. }
  826. /************************************************************/
  827. // User Functions - must disable USBHQ IRQ for EHCI access
  828. /************************************************************/
  829. void USBSerial::begin(uint32_t baud, uint32_t format)
  830. {
  831. NVIC_DISABLE_IRQ(IRQ_USBHS);
  832. baudrate = baud;
  833. pending_control |= 2;
  834. if (!control_queued) control(NULL);
  835. NVIC_ENABLE_IRQ(IRQ_USBHS);
  836. }
  837. void USBSerial::end(void)
  838. {
  839. // TODO: lower DTR
  840. }
  841. int USBSerial::available(void)
  842. {
  843. if (!device) return 0;
  844. uint32_t head = rxhead;
  845. uint32_t tail = rxtail;
  846. if (head >= tail) return head - tail;
  847. return rxsize + head - tail;
  848. }
  849. int USBSerial::peek(void)
  850. {
  851. if (!device) return -1;
  852. uint32_t head = rxhead;
  853. uint32_t tail = rxtail;
  854. if (head == tail) return -1;
  855. if (++tail >= rxsize) tail = 0;
  856. return rxbuf[tail];
  857. }
  858. int USBSerial::read(void)
  859. {
  860. if (!device) return -1;
  861. uint32_t head = rxhead;
  862. uint32_t tail = rxtail;
  863. if (head == tail) return -1;
  864. if (++tail >= rxsize) tail = 0;
  865. int c = rxbuf[tail];
  866. rxtail = tail;
  867. if ((rxstate & 0x03) != 0x03) {
  868. NVIC_DISABLE_IRQ(IRQ_USBHS);
  869. rx_queue_packets(head, tail);
  870. NVIC_ENABLE_IRQ(IRQ_USBHS);
  871. }
  872. return c;
  873. }
  874. int USBSerial::availableForWrite()
  875. {
  876. if (!device) return 0;
  877. uint32_t head = txhead;
  878. uint32_t tail = txtail;
  879. if (head >= tail) return txsize - 1 - head + tail;
  880. return tail - head - 1;
  881. }
  882. size_t USBSerial::write(uint8_t c)
  883. {
  884. if (!device) return 0;
  885. uint32_t head = txhead;
  886. if (++head >= txsize) head = 0;
  887. while (txtail == head) {
  888. // wait...
  889. }
  890. txbuf[head] = c;
  891. txhead = head;
  892. //print("head=", head);
  893. //println(", tail=", txtail);
  894. // if full packet in buffer and tx packet ready, queue it
  895. NVIC_DISABLE_IRQ(IRQ_USBHS);
  896. uint32_t tail = txtail;
  897. if ((txstate & 0x03) != 0x03) {
  898. // at least one packet buffer is ready to transmit
  899. uint32_t count;
  900. if (head >= tail) {
  901. count = head - tail;
  902. } else {
  903. count = txsize + head - tail;
  904. }
  905. uint32_t packetsize = tx2 - tx1;
  906. if (count >= packetsize) {
  907. //println("txsize=", txsize);
  908. uint8_t *p;
  909. if ((txstate & 0x01) == 0) {
  910. p = tx1;
  911. txstate |= 0x01;
  912. } else /* if ((txstate & 0x02) == 0) */ {
  913. p = tx2;
  914. txstate |= 0x02;
  915. }
  916. // copy data to packet buffer
  917. if (++tail >= txsize) tail = 0;
  918. uint32_t n = txsize - tail;
  919. if (n > packetsize) n = packetsize;
  920. //print("memcpy, offset=", tail);
  921. //println(", len=", n);
  922. memcpy(p, txbuf + tail, n);
  923. if (n >= packetsize) {
  924. tail += n - 1;
  925. if (tail >= txsize) tail = 0;
  926. } else {
  927. //n = txsize - n;
  928. uint32_t len = packetsize - n;
  929. //println("memcpy, offset=0, len=", len);
  930. memcpy(p + n, txbuf, len);
  931. tail = len - 1;
  932. }
  933. txtail = tail;
  934. //println("queue tx packet, newtail=", tail);
  935. queue_Data_Transfer(txpipe, p, packetsize, this);
  936. NVIC_ENABLE_IRQ(IRQ_USBHS);
  937. return 1;
  938. }
  939. }
  940. // otherwise, set a latency timer to later transmit partial packet
  941. txtimer.stop();
  942. txtimer.start(3500);
  943. NVIC_ENABLE_IRQ(IRQ_USBHS);
  944. return 1;
  945. }