您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

992 行
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. // TODO: Note: there are probably more vendor/product pairs.. Maybe should create table of them
  221. if (dev->idVendor == 0x67B && dev->idProduct == 0x2303) {
  222. // Prolific Technology, Inc. PL2303 Serial Port
  223. println("len = ", len);
  224. uint8_t count_end_points = descriptors[4];
  225. if (count_end_points < 2) return false; // not enough end points
  226. if (len < 23) return false;
  227. if (descriptors[0] != 9) return false; // length 9
  228. // Lets walk through end points and see if we
  229. // can find an RX and TX bulk transfer end point.
  230. //vid=67B, pid=2303
  231. // 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
  232. //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
  233. uint32_t rxep = 0;
  234. uint32_t txep = 0;
  235. uint32_t descriptor_index = 9;
  236. while (count_end_points-- && ((rxep == 0) || txep == 0)) {
  237. if (descriptors[descriptor_index] != 7) return false; // length 7
  238. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  239. if ((descriptors[descriptor_index+3] == 2)
  240. && (descriptors[descriptor_index+4] == 64)
  241. && (descriptors[descriptor_index+5] == 0)) {
  242. // have a bulk EP size
  243. if (descriptors[descriptor_index+2] & 0x80 ) {
  244. rxep = descriptors[descriptor_index+2];
  245. } else {
  246. txep = descriptors[descriptor_index+2];
  247. }
  248. }
  249. descriptor_index += 7; // setup to look at next one...
  250. }
  251. // Try to verify the end points.
  252. if (!check_rxtx_ep(rxep, txep)) return false;
  253. print("FTDI, rxep=", rxep & 15);
  254. println(", txep=", txep);
  255. if (!init_buffers(64, 64)) return false;
  256. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, 64);
  257. if (!rxpipe) return false;
  258. txpipe = new_Pipe(dev, 2, txep, 0, 64);
  259. if (!txpipe) {
  260. // TODO: free rxpipe
  261. return false;
  262. }
  263. sertype = PL2303;
  264. rxpipe->callback_function = rx_callback;
  265. queue_Data_Transfer(rxpipe, rx1, 64, this);
  266. rxstate = 1;
  267. if (rxsize > 128) {
  268. queue_Data_Transfer(rxpipe, rx2, 64, this);
  269. rxstate = 3;
  270. }
  271. txstate = 0;
  272. txpipe->callback_function = tx_callback;
  273. baudrate = 115200;
  274. // Lets see if it will handle the same CDCACM - messages?
  275. println("PL2303: readRegister(0x04)");
  276. // Need to setup the data the line coding data
  277. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  278. queue_Control_Transfer(dev, &setup, setupdata, this);
  279. control_queued = true;
  280. setup_state = 1; // We are at step one of setup...
  281. pending_control = 0x3f; // Maybe don't need to do...
  282. return true;
  283. }
  284. } else if (type != 1) return false;
  285. // TTYACM: <Composit device>
  286. //
  287. // We first tried to claim a simple ttyACM device like a teensy who is configured
  288. // only as Serial at the device level like what was done for midi
  289. //
  290. // However some devices are a compisit of multiple Interfaces, so see if this Interface
  291. // is of the CDC Interface class and 0 for SubClass and protocol
  292. // Todo: some of this can maybe be combined with the Whole device code above.
  293. if (descriptors[0] != 9 || descriptors[1] != 4) return false; // interface descriptor
  294. if (descriptors[4] < 2) return false; // less than 2 end points
  295. if (descriptors[5] != 0xA) return false; // bInterfaceClass, 0xa = CDC data
  296. if (descriptors[6] != 0) return false; // bInterfaceSubClass
  297. if (descriptors[7] != 0) return false; // bInterfaceProtocol
  298. if (descriptors[9] != 7) return false; // length 7
  299. if (descriptors[10] != 5) return false; // ep desc
  300. uint32_t txep = descriptors[11];
  301. uint32_t txsize = descriptors[13];
  302. if (descriptors[12] != 2) return false; // bulk type
  303. if (descriptors[13] > 64) return false; // size 64 Max
  304. if (descriptors[14] != 0) return false;
  305. if (descriptors[16] != 7) return false; // length 7
  306. if (descriptors[17] != 5) return false; // ep desc
  307. uint32_t rxep = descriptors[18];
  308. uint32_t rxsize = descriptors[20];
  309. if (descriptors[19] != 2) return false; // bulk type
  310. if (descriptors[20] > 64) return false; // size 64 Max
  311. if (descriptors[21] != 0) return false;
  312. if (!check_rxtx_ep(rxep, txep)) return false;
  313. interface = descriptors[2];
  314. print("CDC, rxep=", rxep & 15);
  315. println(", txep=", txep);
  316. if (!init_buffers(rxsize, txsize)) return false;
  317. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, rxsize);
  318. if (!rxpipe) return false;
  319. txpipe = new_Pipe(dev, 2, txep, 0, txsize);
  320. if (!txpipe) {
  321. // TODO: free rxpipe
  322. return false;
  323. }
  324. sertype = CDCACM;
  325. rxpipe->callback_function = rx_callback;
  326. queue_Data_Transfer(rxpipe, rx1, 64, this);
  327. rxstate = 1;
  328. if (rxsize > 128) {
  329. queue_Data_Transfer(rxpipe, rx2, 64, this);
  330. rxstate = 3;
  331. }
  332. txstate = 0;
  333. txpipe->callback_function = tx_callback;
  334. // See if we can do just the inteface...
  335. baudrate = 115200;
  336. println("Control - CDCACM LINE_CODING");
  337. setupdata[0] = 0; // Setup baud rate 115200 - 0x1C200
  338. setupdata[1] = 0xc2;
  339. setupdata[2] = 0x1;
  340. setupdata[3] = 0;
  341. setupdata[4] = 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  342. setupdata[5] = 0; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  343. setupdata[6] = 8; // Data bits (5, 6, 7, 8 or 16)
  344. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  345. queue_Control_Transfer(dev, &setup, setupdata, this);
  346. pending_control = 0x04; // Maybe don't need to do...
  347. control_queued = true;
  348. return true;
  349. }
  350. // check if two legal endpoints, 1 receive & 1 transmit
  351. bool USBSerial::check_rxtx_ep(uint32_t &rxep, uint32_t &txep)
  352. {
  353. if ((rxep & 0x0F) == 0) return false;
  354. if ((txep & 0x0F) == 0) return false;
  355. uint32_t rxdir = rxep & 0xF0;
  356. uint32_t txdir = txep & 0xF0;
  357. if (rxdir == 0x80 && txdir == 0x00) {
  358. return true;
  359. }
  360. if (rxdir == 0x00 && txdir == 0x80) {
  361. std::swap(rxep, txep);
  362. return true;
  363. }
  364. return false;
  365. }
  366. // initialize buffer sizes and pointers
  367. bool USBSerial::init_buffers(uint32_t rsize, uint32_t tsize)
  368. {
  369. // buffer must be able to hold 2 of each packet, plus buffer
  370. // space to hold RX and TX data.
  371. if (sizeof(bigbuffer) < (rsize + tsize) * 3 + 2) return false;
  372. rx1 = (uint8_t *)bigbuffer;
  373. rx2 = rx1 + rsize;
  374. tx1 = rx2 + rsize;
  375. tx2 = tx1 + tsize;
  376. rxbuf = tx2 + tsize;
  377. // FIXME: this assume 50-50 split - not true when rsize != tsize
  378. rxsize = (sizeof(bigbuffer) - (rsize + tsize) * 2) / 2;
  379. txsize = rxsize;
  380. txbuf = rxbuf + rxsize;
  381. rxhead = 0;
  382. rxtail = 0;
  383. txhead = 0;
  384. txtail = 0;
  385. rxstate = 0;
  386. return true;
  387. }
  388. void USBSerial::disconnect()
  389. {
  390. }
  391. void USBSerial::control(const Transfer_t *transfer)
  392. {
  393. println("control callback (serial) ", pending_control, HEX);
  394. control_queued = false;
  395. // We will split this up by Serial type, maybe different functions?
  396. //-------------------------------------------------------------------------
  397. // First FTDI
  398. if (sertype == FTDI) {
  399. if (pending_control & 1) {
  400. pending_control &= ~1;
  401. // set data format
  402. mk_setup(setup, 0x40, 4, 8, 0, 0); // data format 8N1
  403. queue_Control_Transfer(device, &setup, NULL, this);
  404. control_queued = true;
  405. return;
  406. }
  407. // set baud rate
  408. if (pending_control & 2) {
  409. pending_control &= ~2;
  410. uint32_t baudval = 3000000 / baudrate;
  411. mk_setup(setup, 0x40, 3, baudval, 0, 0);
  412. queue_Control_Transfer(device, &setup, NULL, this);
  413. control_queued = true;
  414. return;
  415. }
  416. // configure flow control
  417. if (pending_control & 4) {
  418. pending_control &= ~4;
  419. mk_setup(setup, 0x40, 2, 0, 1, 0);
  420. queue_Control_Transfer(device, &setup, NULL, this);
  421. control_queued = true;
  422. return;
  423. }
  424. // set DTR
  425. if (pending_control & 8) {
  426. pending_control &= ~8;
  427. mk_setup(setup, 0x40, 1, 0x0101, 0, 0);
  428. queue_Control_Transfer(device, &setup, NULL, this);
  429. control_queued = true;
  430. return;
  431. }
  432. }
  433. //-------------------------------------------------------------------------
  434. // Now CDCACM
  435. if (sertype == CDCACM) {
  436. if (pending_control & 2) {
  437. pending_control &= ~2;
  438. // Should probably use data structure, but that may depend on byte ordering...
  439. setupdata[0] = (baudrate) & 0xff; // Setup baud rate 115200 - 0x1C200
  440. setupdata[1] = (baudrate >> 8) & 0xff;
  441. setupdata[2] = (baudrate >> 16) & 0xff;
  442. setupdata[3] = (baudrate >> 24) & 0xff;
  443. setupdata[4] = 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  444. setupdata[5] = 0; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  445. setupdata[6] = 8; // Data bits (5, 6, 7, 8 or 16)
  446. print("CDCACM setup: ");
  447. print_hexbytes(&setupdata, 7);
  448. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  449. queue_Control_Transfer(device, &setup, setupdata, this);
  450. control_queued = true;
  451. return;
  452. }
  453. // configure flow control
  454. if (pending_control & 4) {
  455. pending_control &= ~4;
  456. println("Control - 0x21,0x22, 0x3");
  457. // Need to setup the data the line coding data
  458. mk_setup(setup, 0x21, 0x22, 3, 0, 0);
  459. queue_Control_Transfer(device, &setup, NULL, this);
  460. control_queued = true;
  461. return;
  462. }
  463. }
  464. //-------------------------------------------------------------------------
  465. // Now PL2303 - Which appears to be a little more complicated
  466. if (sertype == PL2303) {
  467. if (pending_control & 1) {
  468. // Still in larger setup state mode
  469. switch (setup_state) {
  470. case 1:
  471. println("PL2303: writeRegister(0x04, 0x00)");
  472. mk_setup(setup, 0x40, 1, 0x0404, 0, 0); //
  473. queue_Control_Transfer(device, &setup, NULL, this);
  474. setup_state = 2;
  475. control_queued = true;
  476. return;
  477. case 2:
  478. println("PL2303: readRegister(0x04)");
  479. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  480. queue_Control_Transfer(device, &setup, setupdata, this);
  481. control_queued = true;
  482. setup_state = 3;
  483. return;
  484. case 3:
  485. println("PL2303: v1 = readRegister(0x03)");
  486. mk_setup(setup, 0xC0, 0x1, 0x8383, 0, 1);
  487. queue_Control_Transfer(device, &setup, setupdata, this);
  488. control_queued = true;
  489. setup_state = 4;
  490. return;
  491. case 4:
  492. println("PL2303: readRegister(0x04)");
  493. // Do we need this value long term or we could just leave in setup data?
  494. pl2303_v1 = setupdata[0]; // save the first bye of version
  495. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  496. queue_Control_Transfer(device, &setup, setupdata, this);
  497. control_queued = true;
  498. setup_state = 5;
  499. return;
  500. case 5:
  501. println("PL2303: writeRegister(0x04, 0x01)");
  502. mk_setup(setup, 0x40, 1, 0x0404, 1, 0); //
  503. queue_Control_Transfer(device, &setup, NULL, this);
  504. setup_state = 6;
  505. control_queued = true;
  506. return;
  507. case 6:
  508. println("PL2303: readRegister(0x04)");
  509. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  510. queue_Control_Transfer(device, &setup, setupdata, this);
  511. control_queued = true;
  512. setup_state = 7;
  513. return;
  514. case 7:
  515. println("PL2303: v2 = readRegister(0x03)");
  516. mk_setup(setup, 0xC0, 0x1, 0x8383, 0, 1);
  517. queue_Control_Transfer(device, &setup, setupdata, this);
  518. control_queued = true;
  519. setup_state = 8;
  520. return;
  521. case 8:
  522. pl2303_v2 = setupdata[0]; // save the first bye of version
  523. print(" PL2303 Version ", pl2303_v1, HEX);
  524. println(":", pl2303_v2, HEX);
  525. println("PL2303: writeRegister(0, 1)");
  526. mk_setup(setup, 0x40, 1, 0, 1, 0); //
  527. queue_Control_Transfer(device, &setup, NULL, this);
  528. setup_state = 9;
  529. control_queued = true;
  530. return;
  531. case 9:
  532. println("PL2303: writeRegister(1, 0)");
  533. mk_setup(setup, 0x40, 1, 1, 0, 0); //
  534. queue_Control_Transfer(device, &setup, NULL, this);
  535. setup_state = 10;
  536. control_queued = true;
  537. return;
  538. case 10:
  539. println("PL2303: writeRegister(2, 44)");
  540. mk_setup(setup, 0x40, 1, 2, 0x44, 0); //
  541. queue_Control_Transfer(device, &setup, NULL, this);
  542. setup_state = 11;
  543. control_queued = true;
  544. return;
  545. case 11:
  546. println("PL2303: writeRegister(8, 0)");
  547. mk_setup(setup, 0x40, 1, 8, 0, 0); //
  548. queue_Control_Transfer(device, &setup, NULL, this);
  549. setup_state = 12;
  550. control_queued = true;
  551. return;
  552. case 12:
  553. println("PL2303: writeRegister(9, 0)");
  554. mk_setup(setup, 0x40, 1, 9, 0, 0); //
  555. queue_Control_Transfer(device, &setup, NULL, this);
  556. setup_state = 13;
  557. control_queued = true;
  558. return;
  559. case 13:
  560. println("PL2303: Read current Baud/control");
  561. mk_setup(setup, 0xA1, 0x21, 0, 0, 7);
  562. queue_Control_Transfer(device, &setup, setupdata, this);
  563. control_queued = true;
  564. break;
  565. }
  566. pending_control &= ~1; // We are finally going to leave this list and join the rest
  567. if (control_queued) return;
  568. }
  569. // set baud rate
  570. if (pending_control & 2) {
  571. pending_control &= ~2;
  572. // See what the read returned earlier
  573. print("PL2303: Returned configuration data: ");
  574. print_hexbytes(setupdata, 7);
  575. // Should probably use data structure, but that may depend on byte ordering...
  576. setupdata[0] = (baudrate) & 0xff; // Setup baud rate 115200 - 0x1C200
  577. setupdata[1] = (baudrate >> 8) & 0xff;
  578. setupdata[2] = (baudrate >> 16) & 0xff;
  579. setupdata[3] = (baudrate >> 24) & 0xff;
  580. setupdata[4] = 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  581. setupdata[5] = 0; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  582. setupdata[6] = 8; // Data bits (5, 6, 7, 8 or 16)
  583. print("PL2303: Set baud/control: ", baudrate, HEX);
  584. print(" = ");
  585. print_hexbytes(&setupdata, 7);
  586. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  587. queue_Control_Transfer(device, &setup, setupdata, this);
  588. control_queued = true;
  589. return;
  590. }
  591. if (pending_control & 4) {
  592. pending_control &= ~4;
  593. println("PL2303: writeRegister(0, 0)");
  594. mk_setup(setup, 0x40, 1, 0, 0, 0); //
  595. queue_Control_Transfer(device, &setup, NULL, this);
  596. control_queued = true;
  597. return;
  598. }
  599. if (pending_control & 8) {
  600. pending_control &= ~8;
  601. println("PL2303: Read current Baud/control");
  602. memset(setupdata, 0, sizeof(setupdata)); // clear it to see if we read it...
  603. mk_setup(setup, 0xA1, 0x21, 0, 0, 7);
  604. queue_Control_Transfer(device, &setup, setupdata, this);
  605. control_queued = true;
  606. }
  607. if (pending_control & 0x10) {
  608. pending_control &= ~0x10;
  609. print("PL2303: Returned configuration data: ");
  610. print_hexbytes(setupdata, 7);
  611. println("PL2303: 0x21, 0x22, 0x3");
  612. mk_setup(setup, 0x21, 0x22, 3, 0, 0); //
  613. queue_Control_Transfer(device, &setup, NULL, this);
  614. control_queued = true;
  615. return;
  616. }
  617. if (pending_control & 0x30) {
  618. pending_control &= ~0x30;
  619. println("PL2303: 0x21, 0x22, 0x3");
  620. mk_setup(setup, 0x21, 0x22, 3, 0, 0); //
  621. queue_Control_Transfer(device, &setup, NULL, this);
  622. control_queued = true;
  623. }
  624. }
  625. }
  626. /************************************************************/
  627. // Interrupt-based Data Movement
  628. /************************************************************/
  629. void USBSerial::rx_callback(const Transfer_t *transfer)
  630. {
  631. if (!transfer->driver) return;
  632. ((USBSerial *)(transfer->driver))->rx_data(transfer);
  633. }
  634. void USBSerial::tx_callback(const Transfer_t *transfer)
  635. {
  636. if (!transfer->driver) return;
  637. ((USBSerial *)(transfer->driver))->tx_data(transfer);
  638. }
  639. void USBSerial::rx_data(const Transfer_t *transfer)
  640. {
  641. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  642. // first update rxstate bitmask, since buffer is no longer queued
  643. if (transfer->buffer == rx1) {
  644. rxstate &= 0xFE;
  645. } else if (transfer->buffer == rx2) {
  646. rxstate &= 0xFD;
  647. }
  648. // get start of data and actual length
  649. const uint8_t *p = (const uint8_t *)transfer->buffer;
  650. if (sertype == FTDI) {
  651. if (len >= 2) {
  652. p += 2;
  653. len -= 2;
  654. } else {
  655. len = 0;
  656. }
  657. }
  658. if (len > 0) {
  659. print("rx: ");
  660. print_hexbytes(p, len);
  661. }
  662. // Copy data from packet buffer to circular buffer.
  663. // Assume the buffer will always have space, since we
  664. // check before queuing the buffers
  665. uint32_t head = rxhead;
  666. uint32_t tail = rxtail;
  667. if (++head >= rxsize) head = 0;
  668. uint32_t avail;
  669. if (len > 0) {
  670. //print("head=", head);
  671. //print(", tail=", tail);
  672. avail = rxsize - head;
  673. //print(", avail=", avail);
  674. //println(", rxsize=", rxsize);
  675. if (avail > len) avail = len;
  676. memcpy(rxbuf + head, p, avail);
  677. if (len <= avail) {
  678. head += avail - 1;
  679. if (head >= rxsize) head = 0;
  680. } else {
  681. head = len - avail - 1;
  682. memcpy(rxbuf, p + avail, head + 1);
  683. }
  684. rxhead = head;
  685. }
  686. // TODO: can be this more efficient? We know from above which
  687. // buffer is no longer queued, so possible skip most of this work?
  688. rx_queue_packets(head, tail);
  689. }
  690. // re-queue packet buffer(s) if possible
  691. void USBSerial::rx_queue_packets(uint32_t head, uint32_t tail)
  692. {
  693. uint32_t avail;
  694. if (head >= tail) {
  695. avail = rxsize - 1 - head + tail;
  696. } else {
  697. avail = tail - head - 1;
  698. }
  699. uint32_t packetsize = rx2 - rx1;
  700. if (avail >= packetsize) {
  701. if ((rxstate & 0x01) == 0) {
  702. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  703. rxstate |= 0x01;
  704. } else if ((rxstate & 0x02) == 0) {
  705. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  706. rxstate |= 0x02;
  707. }
  708. if ((rxstate & 0x03) != 0x03 && avail >= packetsize * 2) {
  709. if ((rxstate & 0x01) == 0) {
  710. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  711. rxstate |= 0x01;
  712. } else if ((rxstate & 0x02) == 0) {
  713. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  714. rxstate |= 0x02;
  715. }
  716. }
  717. }
  718. }
  719. void USBSerial::tx_data(const Transfer_t *transfer)
  720. {
  721. uint32_t mask;
  722. uint8_t *p = (uint8_t *)transfer->buffer;
  723. if (p == tx1) {
  724. println("tx1:");
  725. mask = 1;
  726. //txstate &= 0xFE;
  727. } else if (p == tx2) {
  728. println("tx2:");
  729. mask = 2;
  730. //txstate &= 0xFD;
  731. } else {
  732. return; // should never happen
  733. }
  734. // check how much more data remains in the transmit buffer
  735. uint32_t head = txhead;
  736. uint32_t tail = txtail;
  737. uint32_t count;
  738. if (head >= tail) {
  739. count = head - tail;
  740. } else {
  741. count = txsize + head - tail;
  742. }
  743. uint32_t packetsize = tx2 - tx1;
  744. if (count < packetsize) {
  745. // not enough data in buffer to fill a full packet
  746. txstate &= ~mask;
  747. return;
  748. }
  749. // immediately transmit another full packet, if we have enough data
  750. println("TX:moar data!!!!");
  751. if (++tail >= txsize) tail = 0;
  752. uint32_t n = txsize - tail;
  753. if (n > packetsize) n = packetsize;
  754. memcpy(p, txbuf + tail, n);
  755. if (n >= packetsize) {
  756. tail += n - 1;
  757. if (tail >= txsize) tail = 0;
  758. } else {
  759. uint32_t len = packetsize - n;
  760. memcpy(p + n, txbuf, len);
  761. tail = len - 1;
  762. }
  763. txtail = tail;
  764. queue_Data_Transfer(txpipe, p, packetsize, this);
  765. }
  766. void USBSerial::timer_event(USBDriverTimer *whichTimer)
  767. {
  768. println("txtimer");
  769. uint32_t count;
  770. uint32_t head = txhead;
  771. uint32_t tail = txtail;
  772. if (pending_control) {
  773. // We are still doing setup postpone for awhile..
  774. txtimer.start(1200);
  775. println(" Postpone: setup pending_control");
  776. return; // no outgoing buffers available, try again later
  777. }
  778. if (head == tail) {
  779. println(" *** Empty ***");
  780. return; // nothing to transmit
  781. } else if (head > tail) {
  782. count = head - tail;
  783. } else {
  784. count = txsize + head - tail;
  785. }
  786. uint8_t *p;
  787. if ((txstate & 0x01) == 0) {
  788. p = tx1;
  789. txstate |= 0x01;
  790. } else if ((txstate & 0x02) == 0) {
  791. p = tx2;
  792. txstate |= 0x02;
  793. } else {
  794. txtimer.start(1200);
  795. println(" *** No buffers ***");
  796. return; // no outgoing buffers available, try again later
  797. }
  798. if (++tail >= txsize) tail = 0;
  799. uint32_t n = txsize - tail;
  800. if (n > count) n = count;
  801. memcpy(p, txbuf + tail, n);
  802. if (n >= count) {
  803. tail += n - 1;
  804. if (tail >= txsize) tail = 0;
  805. } else {
  806. uint32_t len = count - n;
  807. memcpy(p + n, txbuf, len);
  808. tail = len - 1;
  809. }
  810. txtail = tail;
  811. print(" TX data (", count);
  812. print(") ");
  813. print_hexbytes(p, count);
  814. queue_Data_Transfer(txpipe, p, count, this);
  815. }
  816. /************************************************************/
  817. // User Functions - must disable USBHQ IRQ for EHCI access
  818. /************************************************************/
  819. void USBSerial::begin(uint32_t baud, uint32_t format)
  820. {
  821. NVIC_DISABLE_IRQ(IRQ_USBHS);
  822. baudrate = baud;
  823. pending_control |= 2;
  824. if (!control_queued) control(NULL);
  825. NVIC_ENABLE_IRQ(IRQ_USBHS);
  826. }
  827. void USBSerial::end(void)
  828. {
  829. // TODO: lower DTR
  830. }
  831. int USBSerial::available(void)
  832. {
  833. if (!device) return 0;
  834. uint32_t head = rxhead;
  835. uint32_t tail = rxtail;
  836. if (head >= tail) return head - tail;
  837. return rxsize + head - tail;
  838. }
  839. int USBSerial::peek(void)
  840. {
  841. if (!device) return -1;
  842. uint32_t head = rxhead;
  843. uint32_t tail = rxtail;
  844. if (head == tail) return -1;
  845. if (++tail >= rxsize) tail = 0;
  846. return rxbuf[tail];
  847. }
  848. int USBSerial::read(void)
  849. {
  850. if (!device) return -1;
  851. uint32_t head = rxhead;
  852. uint32_t tail = rxtail;
  853. if (head == tail) return -1;
  854. if (++tail >= rxsize) tail = 0;
  855. int c = rxbuf[tail];
  856. rxtail = tail;
  857. if ((rxstate & 0x03) != 0x03) {
  858. NVIC_DISABLE_IRQ(IRQ_USBHS);
  859. rx_queue_packets(head, tail);
  860. NVIC_ENABLE_IRQ(IRQ_USBHS);
  861. }
  862. return c;
  863. }
  864. int USBSerial::availableForWrite()
  865. {
  866. if (!device) return 0;
  867. uint32_t head = txhead;
  868. uint32_t tail = txtail;
  869. if (head >= tail) return txsize - 1 - head + tail;
  870. return tail - head - 1;
  871. }
  872. size_t USBSerial::write(uint8_t c)
  873. {
  874. if (!device) return 0;
  875. uint32_t head = txhead;
  876. if (++head >= txsize) head = 0;
  877. while (txtail == head) {
  878. // wait...
  879. }
  880. txbuf[head] = c;
  881. txhead = head;
  882. //print("head=", head);
  883. //println(", tail=", txtail);
  884. // if full packet in buffer and tx packet ready, queue it
  885. NVIC_DISABLE_IRQ(IRQ_USBHS);
  886. uint32_t tail = txtail;
  887. if ((txstate & 0x03) != 0x03) {
  888. // at least one packet buffer is ready to transmit
  889. uint32_t count;
  890. if (head >= tail) {
  891. count = head - tail;
  892. } else {
  893. count = txsize + head - tail;
  894. }
  895. uint32_t packetsize = tx2 - tx1;
  896. if (count >= packetsize) {
  897. //println("txsize=", txsize);
  898. uint8_t *p;
  899. if ((txstate & 0x01) == 0) {
  900. p = tx1;
  901. txstate |= 0x01;
  902. } else /* if ((txstate & 0x02) == 0) */ {
  903. p = tx2;
  904. txstate |= 0x02;
  905. }
  906. // copy data to packet buffer
  907. if (++tail >= txsize) tail = 0;
  908. uint32_t n = txsize - tail;
  909. if (n > packetsize) n = packetsize;
  910. //print("memcpy, offset=", tail);
  911. //println(", len=", n);
  912. memcpy(p, txbuf + tail, n);
  913. if (n >= packetsize) {
  914. tail += n - 1;
  915. if (tail >= txsize) tail = 0;
  916. } else {
  917. //n = txsize - n;
  918. uint32_t len = packetsize - n;
  919. //println("memcpy, offset=0, len=", len);
  920. memcpy(p + n, txbuf, len);
  921. tail = len - 1;
  922. }
  923. txtail = tail;
  924. //println("queue tx packet, newtail=", tail);
  925. queue_Data_Transfer(txpipe, p, packetsize, this);
  926. NVIC_ENABLE_IRQ(IRQ_USBHS);
  927. return 1;
  928. }
  929. }
  930. // otherwise, set a latency timer to later transmit partial packet
  931. txtimer.stop();
  932. txtimer.start(3500);
  933. NVIC_ENABLE_IRQ(IRQ_USBHS);
  934. return 1;
  935. }