Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1444 rindas
46KB

  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. * Note: special thanks to the Linux kernel for the CH341's method of operation, particularly how the baud rate is encoded.
  24. */
  25. #include <Arduino.h>
  26. #include "USBHost_t36.h" // Read this header first for key info
  27. #define print USBHost::print_
  28. #define println USBHost::println_
  29. /************************************************************/
  30. // Define mapping VID/PID - to Serial Device type.
  31. /************************************************************/
  32. USBSerial::product_vendor_mapping_t USBSerial::pid_vid_mapping[] = {
  33. // FTDI mappings.
  34. {0x0403, 0x6001, USBSerial::FTDI},
  35. // PL2303
  36. {0x67B,0x2303, USBSerial::PL2303},
  37. // CH341
  38. {0x4348, 0x5523, USBSerial::CH341 },
  39. {0x1a86, 0x7523, USBSerial::CH341 },
  40. {0x1a86, 0x5523, USBSerial::CH341 },
  41. // Silex CP210...
  42. {0x10c4, 0xea60, USBSerial::CP210X }
  43. };
  44. /************************************************************/
  45. // Initialization and claiming of devices & interfaces
  46. /************************************************************/
  47. void USBSerial::init()
  48. {
  49. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  50. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  51. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  52. driver_ready_for_device(this);
  53. format_ = USBHOST_SERIAL_8N1;
  54. }
  55. bool USBSerial::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  56. {
  57. // only claim at interface level
  58. println("USBSerial claim this=", (uint32_t)this, HEX);
  59. print("vid=", dev->idVendor, HEX);
  60. print(", pid=", dev->idProduct, HEX);
  61. print(", bDeviceClass = ", dev->bDeviceClass);
  62. print(", bDeviceSubClass = ", dev->bDeviceSubClass);
  63. println(", bDeviceProtocol = ", dev->bDeviceProtocol);
  64. print_hexbytes(descriptors, len);
  65. if (type == 0) {
  66. //---------------------------------------------------------------------
  67. // CDCACM
  68. if ((dev->bDeviceClass == 2) && (dev->bDeviceSubClass == 0)) {
  69. // It is a communication device see if we can extract the data...
  70. // Try some ttyACM types?
  71. // This code may be similar to MIDI code.
  72. // But first pass see if we can simply look at the interface...
  73. // Lets walk through end points and see if we
  74. // can find an RX and TX bulk transfer end point.
  75. // 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
  76. // USB2AX
  77. //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
  78. //09 04 01 00 02 0A 00 00 00 07 05 04 02 10 00 01 07 05 83 02 10 00 01
  79. // Teensy 3.6
  80. //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
  81. //09 04 01 00 02 0A 00 00 00 07 05 03 02 40 00 00 07 05 84 02 40 00 00
  82. const uint8_t *p = descriptors;
  83. const uint8_t *end = p + len;
  84. if (p[0] != 9 || p[1] != 4) return false; // interface descriptor
  85. //println(" bInterfaceClass=", p[5]);
  86. //println(" bInterfaceSubClass=", p[6]);
  87. if (p[5] != 2) return false; // bInterfaceClass: 2 Communications
  88. if (p[6] != 2) return false; // bInterfaceSubClass: 2 serial
  89. p += 9;
  90. println(" Interface is Serial");
  91. uint8_t rx_ep = 0;
  92. uint8_t tx_ep = 0;
  93. uint16_t rx_size = 0;
  94. uint16_t tx_size = 0;
  95. interface = 0; // clear out any interface numbers passed in.
  96. while (p < end) {
  97. len = *p;
  98. if (len < 4) return false;
  99. if (p + len > end) return false; // reject if beyond end of data
  100. uint32_t type = p[1];
  101. //println("type: ", type);
  102. // Unlike Audio, we need to look at Interface as our endpoints are after them...
  103. if (type == 4 ) { // Interface - lets remember it's number...
  104. interface = p[2];
  105. println(" Interface: ", interface);
  106. }
  107. else if (type == 0x24) { // 0x24 = CS_INTERFACE,
  108. uint32_t subtype = p[2];
  109. print(" CS_INTERFACE - subtype: ", subtype);
  110. if (len >= 4) print(" ", p[3], HEX);
  111. if (len >= 5) print(" ", p[4], HEX);
  112. if (len >= 6) print(" ", p[5], HEX);
  113. switch (subtype) {
  114. case 0: println(" - Header Functional Descriptor"); break;
  115. case 1: println(" - Call Management Functional"); break;
  116. case 2: println(" - Abstract Control Management"); break;
  117. case 4: println(" - Telephone Ringer"); break;
  118. case 6: println(" - union Functional"); break;
  119. default: println(" - ??? other"); break;
  120. }
  121. // First pass ignore...
  122. } else if (type == 5) {
  123. // endpoint descriptor
  124. if (p[0] < 7) return false; // at least 7 bytes
  125. if (p[3] == 2) { // First try ignore the first one which is interrupt...
  126. println(" Endpoint: ", p[2], HEX);
  127. switch (p[2] & 0xF0) {
  128. case 0x80:
  129. // IN endpoint
  130. if (rx_ep == 0) {
  131. rx_ep = p[2] & 0x0F;
  132. rx_size = p[4] | (p[5] << 8);
  133. println(" rx_size = ", rx_size);
  134. }
  135. break;
  136. case 0x00:
  137. // OUT endpoint
  138. if (tx_ep == 0) {
  139. tx_ep = p[2];
  140. tx_size = p[4] | (p[5] << 8);
  141. println(" tx_size = ", tx_size);
  142. }
  143. break;
  144. default:
  145. println(" invalid end point: ", p[2]);
  146. return false;
  147. }
  148. }
  149. } else {
  150. println(" Unknown type: ", type);
  151. return false; // unknown
  152. }
  153. p += len;
  154. }
  155. print(" exited loop rx:", rx_ep);
  156. println(", tx:", tx_ep);
  157. if (!rx_ep || !tx_ep) return false; // did not get our two end points
  158. if (!init_buffers(rx_size, tx_size)) return false;
  159. rxpipe = new_Pipe(dev, 2, rx_ep & 15, 1, rx_size);
  160. if (!rxpipe) return false;
  161. txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size);
  162. if (!txpipe) {
  163. // TODO: free rxpipe
  164. return false;
  165. }
  166. sertype = CDCACM;
  167. rxpipe->callback_function = rx_callback;
  168. queue_Data_Transfer(rxpipe, rx1, (rx_size < 64)? rx_size : 64, this);
  169. rxstate = 1;
  170. if (rx_size > 128) {
  171. queue_Data_Transfer(rxpipe, rx2, rx_size, this);
  172. rxstate = 3;
  173. }
  174. txstate = 0;
  175. txpipe->callback_function = tx_callback;
  176. baudrate = 115200;
  177. // Wish I could just call Control to do the output... Maybe can defer until the user calls begin()
  178. // control requires that device is setup which is not until this call completes...
  179. println("Control - CDCACM DTR...");
  180. // Need to setup the data the line coding data
  181. mk_setup(setup, 0x21, 0x22, 3, 0, 0);
  182. queue_Control_Transfer(dev, &setup, NULL, this);
  183. control_queued = true;
  184. pending_control = 0x0; // Maybe don't need to do...
  185. return true;
  186. }
  187. // See if the vendor_id:product_id is in our list of products.
  188. sertype = UNKNOWN;
  189. for (uint8_t i = 0; i < (sizeof(pid_vid_mapping)/sizeof(pid_vid_mapping[0])); i++) {
  190. if ((dev->idVendor == pid_vid_mapping[i].idVendor) && (dev->idProduct == pid_vid_mapping[i].idProduct)) {
  191. sertype = pid_vid_mapping[i].sertype;
  192. break;
  193. }
  194. }
  195. switch (sertype) {
  196. //---------------------------------------------------------------------
  197. // FTDI
  198. case FTDI:
  199. {
  200. // FTDI FT232
  201. println("len = ", len);
  202. if (len < 23) return false;
  203. if (descriptors[0] != 9) return false; // length 9
  204. if (descriptors[9] != 7) return false; // length 7
  205. if (descriptors[10] != 5) return false; // ep desc
  206. uint32_t rxep = descriptors[11];
  207. if (descriptors[12] != 2) return false; // bulk type
  208. if (descriptors[13] != 64) return false; // size 64
  209. if (descriptors[14] != 0) return false;
  210. if (descriptors[16] != 7) return false; // length 7
  211. if (descriptors[17] != 5) return false; // ep desc
  212. uint32_t txep = descriptors[18];
  213. if (descriptors[19] != 2) return false; // bulk type
  214. if (descriptors[20] != 64) return false; // size 64
  215. if (descriptors[21] != 0) return false;
  216. if (!check_rxtx_ep(rxep, txep)) return false;
  217. print("FTDI, rxep=", rxep & 15);
  218. println(", txep=", txep);
  219. if (!init_buffers(64, 64)) return false;
  220. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, 64);
  221. if (!rxpipe) return false;
  222. txpipe = new_Pipe(dev, 2, txep, 0, 64);
  223. if (!txpipe) {
  224. // TODO: free rxpipe
  225. return false;
  226. }
  227. sertype = FTDI;
  228. rxpipe->callback_function = rx_callback;
  229. rxsize = 64;
  230. queue_Data_Transfer(rxpipe, rx1, 64, this);
  231. rxstate = 1;
  232. txsize = 64;
  233. txstate = 0;
  234. txpipe->callback_function = tx_callback;
  235. baudrate = 115200;
  236. pending_control = 0x0F;
  237. mk_setup(setup, 0x40, 0, 0, 0, 0); // reset port
  238. queue_Control_Transfer(dev, &setup, NULL, this);
  239. control_queued = true;
  240. return true;
  241. }
  242. //------------------------------------------------------------------------
  243. // Prolific
  244. // TODO: Note: there are probably more vendor/product pairs.. Maybe should create table of them
  245. case PL2303:
  246. {
  247. // Prolific Technology, Inc. PL2303 Serial Port
  248. println("len = ", len);
  249. uint8_t count_end_points = descriptors[4];
  250. if (count_end_points < 2) return false; // not enough end points
  251. if (len < 23) return false;
  252. if (descriptors[0] != 9) return false; // length 9
  253. // Lets walk through end points and see if we
  254. // can find an RX and TX bulk transfer end point.
  255. //vid=67B, pid=2303
  256. // 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
  257. //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
  258. uint32_t rxep = 0;
  259. uint32_t txep = 0;
  260. uint32_t descriptor_index = 9;
  261. while (count_end_points-- && ((rxep == 0) || txep == 0)) {
  262. if (descriptors[descriptor_index] != 7) return false; // length 7
  263. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  264. if ((descriptors[descriptor_index+3] == 2)
  265. && (descriptors[descriptor_index+4] == 64)
  266. && (descriptors[descriptor_index+5] == 0)) {
  267. // have a bulk EP size
  268. if (descriptors[descriptor_index+2] & 0x80 ) {
  269. rxep = descriptors[descriptor_index+2];
  270. rxsize = descriptors[descriptor_index+4];
  271. } else {
  272. txep = descriptors[descriptor_index+2];
  273. txsize = descriptors[descriptor_index+4];
  274. }
  275. }
  276. descriptor_index += 7; // setup to look at next one...
  277. }
  278. // Try to verify the end points.
  279. if (!check_rxtx_ep(rxep, txep)) return false;
  280. print("PL2303, rxep=", rxep & 15);
  281. println(", txep=", txep);
  282. if (!init_buffers(64, 64)) return false;
  283. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, 64);
  284. if (!rxpipe) return false;
  285. txpipe = new_Pipe(dev, 2, txep, 0, 64);
  286. if (!txpipe) {
  287. // TODO: free rxpipe
  288. return false;
  289. }
  290. sertype = PL2303;
  291. rxpipe->callback_function = rx_callback;
  292. queue_Data_Transfer(rxpipe, rx1, 64, this);
  293. rxstate = 1;
  294. txstate = 0;
  295. txpipe->callback_function = tx_callback;
  296. baudrate = 115200;
  297. // First attempt keep it simple...
  298. println("PL2303: readRegister(0x04)");
  299. // Need to setup the data the line coding data
  300. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  301. queue_Control_Transfer(dev, &setup, setupdata, this);
  302. control_queued = true;
  303. setup_state = 1; // We are at step one of setup...
  304. pending_control = 0x3f;
  305. return true;
  306. }
  307. //------------------------------------------------------------------------
  308. // CH341
  309. case CH341:
  310. {
  311. println("len = ", len);
  312. uint8_t count_end_points = descriptors[4];
  313. if (count_end_points < 2) return false; // not enough end points
  314. if (len < 23) return false;
  315. if (descriptors[0] != 9) return false; // length 9
  316. // Lets walk through end points and see if we
  317. // can find an RX and TX bulk transfer end point.
  318. // vid=1A86, pid=7523, bDeviceClass = 255, bDeviceSubClass = 0, bDeviceProtocol = 0
  319. // 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
  320. //09 04 00 00 03 FF 01 02 00 07 05 82 02 20 00 00 07 05 02 02 20 00 00 07 05 81 03 08 00 01
  321. uint32_t rxep = 0;
  322. uint32_t txep = 0;
  323. uint32_t descriptor_index = 9;
  324. while (count_end_points-- && ((rxep == 0) || txep == 0)) {
  325. if (descriptors[descriptor_index] != 7) return false; // length 7
  326. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  327. if ((descriptors[descriptor_index+3] == 2)
  328. && (descriptors[descriptor_index+4] <= 64)
  329. && (descriptors[descriptor_index+5] == 0)) {
  330. // have a bulk EP size
  331. if (descriptors[descriptor_index+2] & 0x80 ) {
  332. rxep = descriptors[descriptor_index+2];
  333. rxsize = descriptors[descriptor_index+4];
  334. } else {
  335. txep = descriptors[descriptor_index+2];
  336. txsize = descriptors[descriptor_index+4];
  337. }
  338. }
  339. descriptor_index += 7; // setup to look at next one...
  340. }
  341. // Try to verify the end points.
  342. if (!check_rxtx_ep(rxep, txep)) return false;
  343. print("ch341, rxep=", rxep & 15);
  344. println(", txep=", txep);
  345. if (!init_buffers(rxsize, txsize)) return false;
  346. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, rxsize);
  347. if (!rxpipe) return false;
  348. txpipe = new_Pipe(dev, 2, txep, 0, txsize);
  349. if (!txpipe) {
  350. // TODO: free rxpipe
  351. return false;
  352. }
  353. rxpipe->callback_function = rx_callback;
  354. queue_Data_Transfer(rxpipe, rx1, rxsize, this);
  355. rxstate = 1;
  356. txstate = 0;
  357. txpipe->callback_function = tx_callback;
  358. baudrate = 115200;
  359. println("CH341: 0xC0, 0x5f, 0, 0, 8");
  360. // Need to setup the data the line coding data
  361. mk_setup(setup, 0xC0, 0x5f, 0, 0, sizeof(setupdata));
  362. queue_Control_Transfer(dev, &setup, setupdata, this);
  363. control_queued = true;
  364. setup_state = 1; // We are at step one of setup...
  365. pending_control = 0x7f;
  366. return true;
  367. }
  368. //------------------------------------------------------------------------
  369. // CP210X
  370. case CP210X:
  371. {
  372. println("len = ", len);
  373. uint8_t count_end_points = descriptors[4];
  374. if (count_end_points < 2) return false; // not enough end points
  375. if (len < 23) return false;
  376. if (descriptors[0] != 9) return false; // length 9
  377. // Lets walk through end points and see if we
  378. // can find an RX and TX bulk transfer end point.
  379. // BUGBUG: should factor out this code as duplicated...
  380. // vid=10C4, pid=EA60, bDeviceClass = 0, bDeviceSubClass = 0, bDeviceProtocol = 0
  381. // 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
  382. //09 04 00 00 02 FF 00 00 02 07 05 81 02 40 00 00 07 05 01 02 40 00 00
  383. uint32_t rxep = 0;
  384. uint32_t txep = 0;
  385. uint32_t descriptor_index = 9;
  386. while (count_end_points-- && ((rxep == 0) || txep == 0)) {
  387. if (descriptors[descriptor_index] != 7) return false; // length 7
  388. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  389. if ((descriptors[descriptor_index+3] == 2)
  390. && (descriptors[descriptor_index+4] <= 64)
  391. && (descriptors[descriptor_index+5] == 0)) {
  392. // have a bulk EP size
  393. if (descriptors[descriptor_index+2] & 0x80 ) {
  394. rxep = descriptors[descriptor_index+2];
  395. rxsize = descriptors[descriptor_index+4];
  396. } else {
  397. txep = descriptors[descriptor_index+2];
  398. txsize = descriptors[descriptor_index+4];
  399. }
  400. }
  401. descriptor_index += 7; // setup to look at next one...
  402. }
  403. // Try to verify the end points.
  404. if (!check_rxtx_ep(rxep, txep)) return false;
  405. print("CP210X, rxep=", rxep & 15);
  406. println(", txep=", txep);
  407. if (!init_buffers(rxsize, txsize)) return false;
  408. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, rxsize);
  409. if (!rxpipe) return false;
  410. txpipe = new_Pipe(dev, 2, txep, 0, txsize);
  411. if (!txpipe) {
  412. // TODO: free rxpipe
  413. return false;
  414. }
  415. rxpipe->callback_function = rx_callback;
  416. queue_Data_Transfer(rxpipe, rx1, rxsize, this);
  417. rxstate = 1;
  418. txstate = 0;
  419. txpipe->callback_function = tx_callback;
  420. baudrate = 115200;
  421. println("CP210X: 0x41, 0x11, 0, 0, 0 - reset port");
  422. // Need to setup the data the line coding data
  423. mk_setup(setup, 0x41, 0x11, 0, 0, 0);
  424. queue_Control_Transfer(dev, &setup, NULL, this);
  425. control_queued = true;
  426. setup_state = 1; // We are at step one of setup...
  427. pending_control = 0xf;
  428. return true;
  429. }
  430. //------------------------------------------------------------------------
  431. // PID:VID - not in our product list.
  432. default:
  433. return false;
  434. }
  435. } else if (type != 1) return false;
  436. // TTYACM: <Composit device>
  437. //
  438. // We first tried to claim a simple ttyACM device like a teensy who is configured
  439. // only as Serial at the device level like what was done for midi
  440. //
  441. // However some devices are a compisit of multiple Interfaces, so see if this Interface
  442. // is of the CDC Interface class and 0 for SubClass and protocol
  443. // Todo: some of this can maybe be combined with the Whole device code above.
  444. if (descriptors[0] != 9 || descriptors[1] != 4) return false; // interface descriptor
  445. if (descriptors[4] < 2) return false; // less than 2 end points
  446. if (descriptors[5] != 0xA) return false; // bInterfaceClass, 0xa = CDC data
  447. if (descriptors[6] != 0) return false; // bInterfaceSubClass
  448. if (descriptors[7] != 0) return false; // bInterfaceProtocol
  449. if (descriptors[9] != 7) return false; // length 7
  450. if (descriptors[10] != 5) return false; // ep desc
  451. uint32_t txep = descriptors[11];
  452. uint32_t txsize = descriptors[13];
  453. if (descriptors[12] != 2) return false; // bulk type
  454. if (descriptors[13] > 64) return false; // size 64 Max
  455. if (descriptors[14] != 0) return false;
  456. if (descriptors[16] != 7) return false; // length 7
  457. if (descriptors[17] != 5) return false; // ep desc
  458. uint32_t rxep = descriptors[18];
  459. uint32_t rxsize = descriptors[20];
  460. if (descriptors[19] != 2) return false; // bulk type
  461. if (descriptors[20] > 64) return false; // size 64 Max
  462. if (descriptors[21] != 0) return false;
  463. if (!check_rxtx_ep(rxep, txep)) return false;
  464. interface = descriptors[2];
  465. print("CDC, rxep=", rxep & 15);
  466. println(", txep=", txep);
  467. if (!init_buffers(rxsize, txsize)) return false;
  468. rxpipe = new_Pipe(dev, 2, rxep & 15, 1, rxsize);
  469. if (!rxpipe) return false;
  470. txpipe = new_Pipe(dev, 2, txep, 0, txsize);
  471. if (!txpipe) {
  472. // TODO: free rxpipe
  473. return false;
  474. }
  475. sertype = CDCACM;
  476. rxpipe->callback_function = rx_callback;
  477. queue_Data_Transfer(rxpipe, rx1, 64, this);
  478. rxstate = 1;
  479. if (rxsize > 128) {
  480. queue_Data_Transfer(rxpipe, rx2, 64, this);
  481. rxstate = 3;
  482. }
  483. txstate = 0;
  484. txpipe->callback_function = tx_callback;
  485. // See if we can do just the inteface...
  486. baudrate = 115200;
  487. println("Control - CDCACM LINE_CODING");
  488. setupdata[0] = 0; // Setup baud rate 115200 - 0x1C200
  489. setupdata[1] = 0xc2;
  490. setupdata[2] = 0x1;
  491. setupdata[3] = 0;
  492. setupdata[4] = 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  493. setupdata[5] = 0; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  494. setupdata[6] = 8; // Data bits (5, 6, 7, 8 or 16)
  495. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  496. queue_Control_Transfer(dev, &setup, setupdata, this);
  497. pending_control = 0x04; // Maybe don't need to do...
  498. control_queued = true;
  499. return true;
  500. }
  501. // check if two legal endpoints, 1 receive & 1 transmit
  502. bool USBSerial::check_rxtx_ep(uint32_t &rxep, uint32_t &txep)
  503. {
  504. if ((rxep & 0x0F) == 0) return false;
  505. if ((txep & 0x0F) == 0) return false;
  506. uint32_t rxdir = rxep & 0xF0;
  507. uint32_t txdir = txep & 0xF0;
  508. if (rxdir == 0x80 && txdir == 0x00) {
  509. return true;
  510. }
  511. if (rxdir == 0x00 && txdir == 0x80) {
  512. std::swap(rxep, txep);
  513. return true;
  514. }
  515. return false;
  516. }
  517. // initialize buffer sizes and pointers
  518. bool USBSerial::init_buffers(uint32_t rsize, uint32_t tsize)
  519. {
  520. // buffer must be able to hold 2 of each packet, plus buffer
  521. // space to hold RX and TX data.
  522. if (sizeof(bigbuffer) < (rsize + tsize) * 3 + 2) return false;
  523. rx1 = (uint8_t *)bigbuffer;
  524. rx2 = rx1 + rsize;
  525. tx1 = rx2 + rsize;
  526. tx2 = tx1 + tsize;
  527. rxbuf = tx2 + tsize;
  528. // FIXME: this assume 50-50 split - not true when rsize != tsize
  529. rxsize = (sizeof(bigbuffer) - (rsize + tsize) * 2) / 2;
  530. txsize = rxsize;
  531. txbuf = rxbuf + rxsize;
  532. rxhead = 0;
  533. rxtail = 0;
  534. txhead = 0;
  535. txtail = 0;
  536. rxstate = 0;
  537. return true;
  538. }
  539. void USBSerial::disconnect()
  540. {
  541. }
  542. void USBSerial::control(const Transfer_t *transfer)
  543. {
  544. println("control callback (serial) ", pending_control, HEX);
  545. control_queued = false;
  546. // We will split this up by Serial type, maybe different functions?
  547. //-------------------------------------------------------------------------
  548. // First FTDI
  549. if (sertype == FTDI) {
  550. if (pending_control & 1) {
  551. pending_control &= ~1;
  552. // set data format
  553. uint16_t ftdi_format = format_ & 0xf; // This should give us the number of bits.
  554. // now lets extract the parity from our encoding
  555. ftdi_format |= (format_ & 0xe0) << 3; // they encode bits 9-11
  556. // See if two stop bits
  557. if (format_ & 0x100) ftdi_format |= (0x2 << 11);
  558. mk_setup(setup, 0x40, 4, ftdi_format, 0, 0); // data format 8N1
  559. queue_Control_Transfer(device, &setup, NULL, this);
  560. control_queued = true;
  561. return;
  562. }
  563. // set baud rate
  564. if (pending_control & 2) {
  565. pending_control &= ~2;
  566. uint32_t baudval = 3000000 / baudrate;
  567. mk_setup(setup, 0x40, 3, baudval, 0, 0);
  568. queue_Control_Transfer(device, &setup, NULL, this);
  569. control_queued = true;
  570. return;
  571. }
  572. // configure flow control
  573. if (pending_control & 4) {
  574. pending_control &= ~4;
  575. mk_setup(setup, 0x40, 2, 0, 1, 0);
  576. queue_Control_Transfer(device, &setup, NULL, this);
  577. control_queued = true;
  578. return;
  579. }
  580. // set DTR
  581. if (pending_control & 8) {
  582. pending_control &= ~8;
  583. mk_setup(setup, 0x40, 1, 0x0101, 0, 0);
  584. queue_Control_Transfer(device, &setup, NULL, this);
  585. control_queued = true;
  586. return;
  587. }
  588. // clear DTR
  589. if (pending_control & 0x80) {
  590. pending_control &= ~0x80;
  591. println("FTDI clear DTR");
  592. mk_setup(setup, 0x40, 1, 0x0100, 0, 0);
  593. queue_Control_Transfer(device, &setup, NULL, this);
  594. control_queued = true;
  595. return;
  596. }
  597. }
  598. //-------------------------------------------------------------------------
  599. // Now CDCACM
  600. if (sertype == CDCACM) {
  601. if (pending_control & 2) {
  602. pending_control &= ~2;
  603. // Should probably use data structure, but that may depend on byte ordering...
  604. setupdata[0] = (baudrate) & 0xff; // Setup baud rate 115200 - 0x1C200
  605. setupdata[1] = (baudrate >> 8) & 0xff;
  606. setupdata[2] = (baudrate >> 16) & 0xff;
  607. setupdata[3] = (baudrate >> 24) & 0xff;
  608. setupdata[4] = (format_ & 0x100)? 2 : 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  609. setupdata[5] = (format_ & 0xe0) >> 5; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  610. setupdata[6] = format_ & 0x1f; // Data bits (5, 6, 7, 8 or 16)
  611. print("CDCACM setup: ");
  612. print_hexbytes(&setupdata, 7);
  613. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  614. queue_Control_Transfer(device, &setup, setupdata, this);
  615. control_queued = true;
  616. return;
  617. }
  618. // configure flow control
  619. if (pending_control & 4) {
  620. pending_control &= ~4;
  621. println("Control - 0x21,0x22, 0x3");
  622. // Need to setup the data the line coding data
  623. mk_setup(setup, 0x21, 0x22, 3, 0, 0);
  624. queue_Control_Transfer(device, &setup, NULL, this);
  625. control_queued = true;
  626. return;
  627. }
  628. if (pending_control & 0x80) {
  629. pending_control &= ~0x80;
  630. println("Control - 0x21,0x22, 0x0 - clear DTR");
  631. // Need to setup the data the line coding data
  632. mk_setup(setup, 0x21, 0x22, 0, 0, 0);
  633. queue_Control_Transfer(device, &setup, NULL, this);
  634. control_queued = true;
  635. return;
  636. }
  637. }
  638. //-------------------------------------------------------------------------
  639. // Now PL2303 - Which appears to be a little more complicated
  640. if (sertype == PL2303) {
  641. if (pending_control & 1) {
  642. // Still in larger setup state mode
  643. switch (setup_state) {
  644. case 1:
  645. println("PL2303: writeRegister(0x04, 0x00)");
  646. mk_setup(setup, 0x40, 1, 0x0404, 0, 0); //
  647. queue_Control_Transfer(device, &setup, NULL, this);
  648. setup_state = 2;
  649. control_queued = true;
  650. return;
  651. case 2:
  652. println("PL2303: readRegister(0x04)");
  653. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  654. queue_Control_Transfer(device, &setup, setupdata, this);
  655. control_queued = true;
  656. setup_state = 3;
  657. return;
  658. case 3:
  659. println("PL2303: v1 = readRegister(0x03)");
  660. mk_setup(setup, 0xC0, 0x1, 0x8383, 0, 1);
  661. queue_Control_Transfer(device, &setup, setupdata, this);
  662. control_queued = true;
  663. setup_state = 4;
  664. return;
  665. case 4:
  666. println("PL2303: readRegister(0x04)");
  667. // Do we need this value long term or we could just leave in setup data?
  668. pl2303_v1 = setupdata[0]; // save the first bye of version
  669. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  670. queue_Control_Transfer(device, &setup, setupdata, this);
  671. control_queued = true;
  672. setup_state = 5;
  673. return;
  674. case 5:
  675. println("PL2303: writeRegister(0x04, 0x01)");
  676. mk_setup(setup, 0x40, 1, 0x0404, 1, 0); //
  677. queue_Control_Transfer(device, &setup, NULL, this);
  678. setup_state = 6;
  679. control_queued = true;
  680. return;
  681. case 6:
  682. println("PL2303: readRegister(0x04)");
  683. mk_setup(setup, 0xC0, 0x1, 0x8484, 0, 1);
  684. queue_Control_Transfer(device, &setup, setupdata, this);
  685. control_queued = true;
  686. setup_state = 7;
  687. return;
  688. case 7:
  689. println("PL2303: v2 = readRegister(0x03)");
  690. mk_setup(setup, 0xC0, 0x1, 0x8383, 0, 1);
  691. queue_Control_Transfer(device, &setup, setupdata, this);
  692. control_queued = true;
  693. setup_state = 8;
  694. return;
  695. case 8:
  696. pl2303_v2 = setupdata[0]; // save the first bye of version
  697. print(" PL2303 Version ", pl2303_v1, HEX);
  698. println(":", pl2303_v2, HEX);
  699. println("PL2303: writeRegister(0, 1)");
  700. mk_setup(setup, 0x40, 1, 0, 1, 0); //
  701. queue_Control_Transfer(device, &setup, NULL, this);
  702. setup_state = 9;
  703. control_queued = true;
  704. return;
  705. case 9:
  706. println("PL2303: writeRegister(1, 0)");
  707. mk_setup(setup, 0x40, 1, 1, 0, 0); //
  708. queue_Control_Transfer(device, &setup, NULL, this);
  709. setup_state = 10;
  710. control_queued = true;
  711. return;
  712. case 10:
  713. println("PL2303: writeRegister(2, 44)");
  714. mk_setup(setup, 0x40, 1, 2, 0x44, 0); //
  715. queue_Control_Transfer(device, &setup, NULL, this);
  716. setup_state = 11;
  717. control_queued = true;
  718. return;
  719. case 11:
  720. println("PL2303: writeRegister(8, 0)");
  721. mk_setup(setup, 0x40, 1, 8, 0, 0); //
  722. queue_Control_Transfer(device, &setup, NULL, this);
  723. setup_state = 12;
  724. control_queued = true;
  725. return;
  726. case 12:
  727. println("PL2303: writeRegister(9, 0)");
  728. mk_setup(setup, 0x40, 1, 9, 0, 0); //
  729. queue_Control_Transfer(device, &setup, NULL, this);
  730. setup_state = 13;
  731. control_queued = true;
  732. return;
  733. case 13:
  734. println("PL2303: Read current Baud/control");
  735. mk_setup(setup, 0xA1, 0x21, 0, 0, 7);
  736. queue_Control_Transfer(device, &setup, setupdata, this);
  737. control_queued = true;
  738. break;
  739. }
  740. pending_control &= ~1; // We are finally going to leave this list and join the rest
  741. if (control_queued) return;
  742. }
  743. // set baud rate
  744. if (pending_control & 2) {
  745. pending_control &= ~2;
  746. // See what the read returned earlier
  747. print("PL2303: Returned configuration data: ");
  748. print_hexbytes(setupdata, 7);
  749. // Should probably use data structure, but that may depend on byte ordering...
  750. setupdata[0] = (baudrate) & 0xff; // Setup baud rate 115200 - 0x1C200
  751. setupdata[1] = (baudrate >> 8) & 0xff;
  752. setupdata[2] = (baudrate >> 16) & 0xff;
  753. setupdata[3] = (baudrate >> 24) & 0xff;
  754. setupdata[4] = (format_ & 0x100)? 2 : 0; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  755. setupdata[5] = (format_ & 0xe0) >> 5; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  756. setupdata[6] = format_ & 0x1f; // Data bits (5, 6, 7, 8 or 16)
  757. print("PL2303: Set baud/control: ", baudrate, HEX);
  758. print(" = ");
  759. print_hexbytes(&setupdata, 7);
  760. mk_setup(setup, 0x21, 0x20, 0, 0, 7);
  761. queue_Control_Transfer(device, &setup, setupdata, this);
  762. control_queued = true;
  763. return;
  764. }
  765. if (pending_control & 4) {
  766. pending_control &= ~4;
  767. println("PL2303: writeRegister(0, 0)");
  768. mk_setup(setup, 0x40, 1, 0, 0, 0); //
  769. queue_Control_Transfer(device, &setup, NULL, this);
  770. control_queued = true;
  771. return;
  772. }
  773. if (pending_control & 8) {
  774. pending_control &= ~8;
  775. println("PL2303: Read current Baud/control");
  776. memset(setupdata, 0, sizeof(setupdata)); // clear it to see if we read it...
  777. mk_setup(setup, 0xA1, 0x21, 0, 0, 7);
  778. queue_Control_Transfer(device, &setup, setupdata, this);
  779. control_queued = true;
  780. }
  781. if (pending_control & 0x10) {
  782. pending_control &= ~0x10;
  783. print("PL2303: Returned configuration data: ");
  784. print_hexbytes(setupdata, 7);
  785. // This sets the control lines (0x1=DTR, 0x2=RTS)
  786. println("PL2303: 0x21, 0x22, 0x3");
  787. mk_setup(setup, 0x21, 0x22, 3, 0, 0); //
  788. queue_Control_Transfer(device, &setup, NULL, this);
  789. control_queued = true;
  790. return;
  791. }
  792. if (pending_control & 0x20) {
  793. pending_control &= ~0x20;
  794. println("PL2303: 0x21, 0x22, 0x3");
  795. mk_setup(setup, 0x21, 0x22, 3, 0, 0); //
  796. queue_Control_Transfer(device, &setup, NULL, this);
  797. control_queued = true;
  798. }
  799. if (pending_control & 0x80) {
  800. pending_control &= ~0x80;
  801. println("PL2303: 0x21, 0x22, 0x0"); // Clear DTR/RTS
  802. mk_setup(setup, 0x21, 0x22, 0, 0, 0); //
  803. queue_Control_Transfer(device, &setup, NULL, this);
  804. control_queued = true;
  805. }
  806. }
  807. if (sertype == CH341) {
  808. #if 0
  809. print(" Transfer: ");
  810. print_hexbytes(&transfer->setup, sizeof(setup_t));
  811. if (transfer->length) {
  812. print(" data: ");
  813. print_hexbytes(transfer->buffer, transfer->length);
  814. }
  815. #endif
  816. if (pending_control & 1) {
  817. // Still in larger setup state mode
  818. switch (setup_state) {
  819. case 1:
  820. print(" Returned: ");
  821. print_hexbytes(transfer->buffer, transfer->length);
  822. println("CH341: 40, a1, 0, 0, 0");
  823. mk_setup(setup, 0x40, 0xa1, 0, 0, 0); //
  824. queue_Control_Transfer(device, &setup, NULL, this);
  825. setup_state = 2;
  826. control_queued = true;
  827. return;
  828. case 2:
  829. ch341_setBaud(0); // send the first byte of the baud rate
  830. control_queued = true;
  831. setup_state = 3;
  832. return;
  833. case 3:
  834. ch341_setBaud(1); // send the second byte of the baud rate
  835. control_queued = true;
  836. setup_state = 4;
  837. return;
  838. case 4:
  839. println("CH341: c0, 95, 2518, 0, 8");
  840. mk_setup(setup, 0xc0, 0x95, 0x2518, 0, sizeof(setup)); //
  841. queue_Control_Transfer(device, &setup, setupdata, this);
  842. setup_state = 5;
  843. control_queued = true;
  844. return;
  845. case 5:
  846. print(" Returned: ");
  847. print_hexbytes(transfer->buffer, transfer->length);
  848. println("CH341: 40, 0x9a, 0x2518, 0x0050, 0");
  849. mk_setup(setup, 0x40, 0x9a, 0x2518, 0x0050, 0); //
  850. queue_Control_Transfer(device, &setup, NULL, this);
  851. setup_state = 6;
  852. control_queued = true;
  853. return;
  854. case 6:
  855. println("CH341: c0, 95, 0x706, 0, 8 - get status");
  856. mk_setup(setup, 0xc0, 0x95, 0x706, 0, sizeof(setup)); //
  857. queue_Control_Transfer(device, &setup, setupdata, this);
  858. setup_state = 7;
  859. control_queued = true;
  860. return;
  861. case 7:
  862. print(" Returned: ");
  863. print_hexbytes(transfer->buffer, transfer->length);
  864. println("CH341: 40, 0xa1, 0x501f, 0xd90a, 0");
  865. mk_setup(setup, 0x40, 0xa1, 0x501f, 0xd90a, 0); //
  866. queue_Control_Transfer(device, &setup, NULL, this);
  867. setup_state = 8;
  868. control_queued = true;
  869. break;
  870. }
  871. pending_control &= ~1; // We are finally going to leave this list and join the rest
  872. if (control_queued) return;
  873. }
  874. // set baud rate
  875. if (pending_control & 2) {
  876. pending_control &= ~2;
  877. ch341_setBaud(0); // send the first byte of the baud rate
  878. control_queued = true;
  879. return;
  880. }
  881. if (pending_control & 4) {
  882. pending_control &= ~4;
  883. ch341_setBaud(1); // send the first byte of the baud rate
  884. control_queued = true;
  885. return;
  886. }
  887. if (pending_control & 8) {
  888. pending_control &= ~8;
  889. uint16_t ch341_format;
  890. switch (format_) {
  891. default:
  892. // These values were observed when used on PC... Need to flush out others.
  893. case USBHOST_SERIAL_8N1: ch341_format = 0xc3; break;
  894. case USBHOST_SERIAL_7E1: ch341_format = 0xda; break;
  895. case USBHOST_SERIAL_7O1: ch341_format = 0xca; break;
  896. case USBHOST_SERIAL_8N2: ch341_format = 0xc7; break;
  897. }
  898. println("CH341: 40, 0x9a, 0x2518: ", ch341_format, HEX);
  899. mk_setup(setup, 0x40, 0x9a, 0x2518, ch341_format, 0); //
  900. queue_Control_Transfer(device, &setup, NULL, this);
  901. control_queued = true;
  902. return;
  903. }
  904. if (pending_control & 0x10) {
  905. pending_control &= ~0x10;
  906. // This is setting handshake need to figure out what...
  907. // 0x20=DTR, 0x40=RTS send ~ of values.
  908. println("CH341: 0x40, 0xa4, 0xff9f, 0, 0 - Handshake");
  909. mk_setup(setup, 0x40, 0xa4, 0xff9f, 0, 0); //
  910. queue_Control_Transfer(device, &setup, NULL, this);
  911. control_queued = true;
  912. return;
  913. }
  914. if (pending_control & 0x20) {
  915. pending_control &= ~0x20;
  916. // This is setting handshake need to figure out what...
  917. println("CH341: c0, 95, 0x706, 0, 8 - get status");
  918. mk_setup(setup, 0xc0, 0x95, 0x706, 0, sizeof(setup)); //
  919. queue_Control_Transfer(device, &setup, setupdata, this);
  920. control_queued = true;
  921. return;
  922. }
  923. if (pending_control & 0x40) {
  924. pending_control &= ~0x40;
  925. print(" Returned: ");
  926. print_hexbytes(transfer->buffer, transfer->length);
  927. println("CH341: 0x40, 0x9a, 0x2727, 0, 0");
  928. mk_setup(setup, 0x40, 0x9a, 0x2727, 0, 0); //
  929. queue_Control_Transfer(device, &setup, NULL, this);
  930. return;
  931. }
  932. if (pending_control & 0x80) {
  933. pending_control &= ~0x80;
  934. println("CH341: 0x40, 0xa4, 0xffff, 0, 0 - Handshake");
  935. mk_setup(setup, 0x40, 0xa4, 0xffff, 0, 0); //
  936. queue_Control_Transfer(device, &setup, NULL, this);
  937. control_queued = true;
  938. return;
  939. }
  940. }
  941. //-------------------------------------------------------------------------
  942. // First CP210X
  943. if (sertype == CP210X) {
  944. if (pending_control & 1) {
  945. pending_control &= ~1;
  946. // set data format
  947. uint16_t cp210x_format = (format_ & 0xf) << 8; // This should give us the number of bits.
  948. // now lets extract the parity from our encoding bits 5-7 and in theres 4-7
  949. cp210x_format |= (format_ & 0xe0) >> 1; // they encode bits 9-11
  950. // See if two stop bits
  951. if (format_ & 0x100) cp210x_format |= 2;
  952. mk_setup(setup, 0x41, 3, cp210x_format, 0, 0); // data format 8N1
  953. queue_Control_Transfer(device, &setup, NULL, this);
  954. control_queued = true;
  955. return;
  956. }
  957. // set baud rate
  958. if (pending_control & 2) {
  959. pending_control &= ~2;
  960. setupdata[0] = (baudrate) & 0xff; // Setup baud rate 115200 - 0x1C200
  961. setupdata[1] = (baudrate >> 8) & 0xff;
  962. setupdata[2] = (baudrate >> 16) & 0xff;
  963. setupdata[3] = (baudrate >> 24) & 0xff;
  964. mk_setup(setup, 0x40, 0x1e, 0, 0, 4);
  965. queue_Control_Transfer(device, &setup, setupdata, this);
  966. control_queued = true;
  967. return;
  968. }
  969. // configure flow control
  970. if (pending_control & 4) {
  971. pending_control &= ~4;
  972. memset(setupdata, 0, sizeof(setupdata)); // clear out the data
  973. setupdata[0] = 1; // Set dtr active?
  974. mk_setup(setup, 0x41, 13, 0, 0, 0x10);
  975. queue_Control_Transfer(device, &setup, setupdata, this);
  976. control_queued = true;
  977. return;
  978. }
  979. // set DTR
  980. if (pending_control & 8) {
  981. pending_control &= ~8;
  982. mk_setup(setup, 0x41, 7, 0x0101, 0, 0);
  983. queue_Control_Transfer(device, &setup, NULL, this);
  984. control_queued = true;
  985. return;
  986. }
  987. // clear DTR
  988. if (pending_control & 0x80) {
  989. pending_control &= ~0x80;
  990. println("CP210x clear DTR");
  991. mk_setup(setup, 0x40, 1, 0x0100, 0, 0);
  992. queue_Control_Transfer(device, &setup, NULL, this);
  993. control_queued = true;
  994. return;
  995. }
  996. }
  997. }
  998. #define CH341_BAUDBASE_FACTOR 1532620800
  999. #define CH341_BAUDBASE_DIVMAX 3
  1000. void USBSerial::ch341_setBaud(uint8_t byte_index) {
  1001. if (byte_index == 0) {
  1002. uint32_t factor;
  1003. uint16_t divisor;
  1004. factor = (CH341_BAUDBASE_FACTOR / baudrate);
  1005. divisor = CH341_BAUDBASE_DIVMAX;
  1006. while ((factor > 0xfff0) && divisor) {
  1007. factor >>= 3;
  1008. divisor--;
  1009. }
  1010. factor = 0x10000 - factor;
  1011. factor = (factor & 0xff00) | divisor;
  1012. setupdata[0] = factor & 0xff; // save away the low byte for 2nd message
  1013. println("CH341: 40, 0x9a, 0x1312... (Baud word 0):", factor, HEX);
  1014. mk_setup(setup, 0x40, 0x9a, 0x1312, factor, 0); //
  1015. } else {
  1016. // Second packet use the byte we saved away during the calculation above
  1017. println("CH341: 40, 0x9a, 0x0f2c... (Baud word 1):", setupdata[0], HEX);
  1018. mk_setup(setup, 0x40, 0x9a, 0x0f2c, setupdata[0], 0); //
  1019. }
  1020. queue_Control_Transfer(device, &setup, setupdata, this);
  1021. control_queued = true;
  1022. }
  1023. /************************************************************/
  1024. // Interrupt-based Data Movement
  1025. /************************************************************/
  1026. void USBSerial::rx_callback(const Transfer_t *transfer)
  1027. {
  1028. if (!transfer->driver) return;
  1029. ((USBSerial *)(transfer->driver))->rx_data(transfer);
  1030. }
  1031. void USBSerial::tx_callback(const Transfer_t *transfer)
  1032. {
  1033. if (!transfer->driver) return;
  1034. ((USBSerial *)(transfer->driver))->tx_data(transfer);
  1035. }
  1036. void USBSerial::rx_data(const Transfer_t *transfer)
  1037. {
  1038. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  1039. // first update rxstate bitmask, since buffer is no longer queued
  1040. if (transfer->buffer == rx1) {
  1041. rxstate &= 0xFE;
  1042. } else if (transfer->buffer == rx2) {
  1043. rxstate &= 0xFD;
  1044. }
  1045. // get start of data and actual length
  1046. const uint8_t *p = (const uint8_t *)transfer->buffer;
  1047. if (sertype == FTDI) {
  1048. if (len >= 2) {
  1049. p += 2;
  1050. len -= 2;
  1051. } else {
  1052. len = 0;
  1053. }
  1054. }
  1055. if (len > 0) {
  1056. print("rx: ");
  1057. print_hexbytes(p, len);
  1058. }
  1059. // Copy data from packet buffer to circular buffer.
  1060. // Assume the buffer will always have space, since we
  1061. // check before queuing the buffers
  1062. uint32_t head = rxhead;
  1063. uint32_t tail = rxtail;
  1064. if (++head >= rxsize) head = 0;
  1065. uint32_t avail;
  1066. if (len > 0) {
  1067. //print("head=", head);
  1068. //print(", tail=", tail);
  1069. avail = rxsize - head;
  1070. //print(", avail=", avail);
  1071. //println(", rxsize=", rxsize);
  1072. if (avail > len) avail = len;
  1073. memcpy(rxbuf + head, p, avail);
  1074. if (len <= avail) {
  1075. head += avail - 1;
  1076. if (head >= rxsize) head = 0;
  1077. } else {
  1078. head = len - avail - 1;
  1079. memcpy(rxbuf, p + avail, head + 1);
  1080. }
  1081. rxhead = head;
  1082. }
  1083. // TODO: can be this more efficient? We know from above which
  1084. // buffer is no longer queued, so possible skip most of this work?
  1085. rx_queue_packets(head, tail);
  1086. }
  1087. // re-queue packet buffer(s) if possible
  1088. void USBSerial::rx_queue_packets(uint32_t head, uint32_t tail)
  1089. {
  1090. uint32_t avail;
  1091. if (head >= tail) {
  1092. avail = rxsize - 1 - head + tail;
  1093. } else {
  1094. avail = tail - head - 1;
  1095. }
  1096. uint32_t packetsize = rx2 - rx1;
  1097. if (avail >= packetsize) {
  1098. if ((rxstate & 0x01) == 0) {
  1099. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  1100. rxstate |= 0x01;
  1101. } else if ((rxstate & 0x02) == 0) {
  1102. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  1103. rxstate |= 0x02;
  1104. }
  1105. if ((rxstate & 0x03) != 0x03 && avail >= packetsize * 2) {
  1106. if ((rxstate & 0x01) == 0) {
  1107. queue_Data_Transfer(rxpipe, rx1, packetsize, this);
  1108. rxstate |= 0x01;
  1109. } else if ((rxstate & 0x02) == 0) {
  1110. queue_Data_Transfer(rxpipe, rx2, packetsize, this);
  1111. rxstate |= 0x02;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. void USBSerial::tx_data(const Transfer_t *transfer)
  1117. {
  1118. uint32_t mask;
  1119. uint8_t *p = (uint8_t *)transfer->buffer;
  1120. if (p == tx1) {
  1121. println("tx1:");
  1122. mask = 1;
  1123. //txstate &= 0xFE;
  1124. } else if (p == tx2) {
  1125. println("tx2:");
  1126. mask = 2;
  1127. //txstate &= 0xFD;
  1128. } else {
  1129. return; // should never happen
  1130. }
  1131. // check how much more data remains in the transmit buffer
  1132. uint32_t head = txhead;
  1133. uint32_t tail = txtail;
  1134. uint32_t count;
  1135. if (head >= tail) {
  1136. count = head - tail;
  1137. } else {
  1138. count = txsize + head - tail;
  1139. }
  1140. uint32_t packetsize = tx2 - tx1;
  1141. if (count < packetsize) {
  1142. // not enough data in buffer to fill a full packet
  1143. txstate &= ~mask;
  1144. return;
  1145. }
  1146. // immediately transmit another full packet, if we have enough data
  1147. println("TX:moar data!!!!");
  1148. if (++tail >= txsize) tail = 0;
  1149. uint32_t n = txsize - tail;
  1150. if (n > packetsize) n = packetsize;
  1151. memcpy(p, txbuf + tail, n);
  1152. if (n >= packetsize) {
  1153. tail += n - 1;
  1154. if (tail >= txsize) tail = 0;
  1155. } else {
  1156. uint32_t len = packetsize - n;
  1157. memcpy(p + n, txbuf, len);
  1158. tail = len - 1;
  1159. }
  1160. txtail = tail;
  1161. queue_Data_Transfer(txpipe, p, packetsize, this);
  1162. }
  1163. void USBSerial::timer_event(USBDriverTimer *whichTimer)
  1164. {
  1165. println("txtimer");
  1166. uint32_t count;
  1167. uint32_t head = txhead;
  1168. uint32_t tail = txtail;
  1169. if (pending_control) {
  1170. // We are still doing setup postpone for awhile..
  1171. txtimer.start(1200);
  1172. println(" Postpone: setup pending_control");
  1173. return; // no outgoing buffers available, try again later
  1174. }
  1175. if (head == tail) {
  1176. println(" *** Empty ***");
  1177. return; // nothing to transmit
  1178. } else if (head > tail) {
  1179. count = head - tail;
  1180. } else {
  1181. count = txsize + head - tail;
  1182. }
  1183. uint8_t *p;
  1184. if ((txstate & 0x01) == 0) {
  1185. p = tx1;
  1186. txstate |= 0x01;
  1187. } else if ((txstate & 0x02) == 0) {
  1188. p = tx2;
  1189. txstate |= 0x02;
  1190. } else {
  1191. txtimer.start(1200);
  1192. println(" *** No buffers ***");
  1193. return; // no outgoing buffers available, try again later
  1194. }
  1195. if (++tail >= txsize) tail = 0;
  1196. uint32_t n = txsize - tail;
  1197. if (n > count) n = count;
  1198. memcpy(p, txbuf + tail, n);
  1199. if (n >= count) {
  1200. tail += n - 1;
  1201. if (tail >= txsize) tail = 0;
  1202. } else {
  1203. uint32_t len = count - n;
  1204. memcpy(p + n, txbuf, len);
  1205. tail = len - 1;
  1206. }
  1207. txtail = tail;
  1208. print(" TX data (", count);
  1209. print(") ");
  1210. print_hexbytes(p, count);
  1211. queue_Data_Transfer(txpipe, p, count, this);
  1212. }
  1213. /************************************************************/
  1214. // User Functions - must disable USBHQ IRQ for EHCI access
  1215. /************************************************************/
  1216. void USBSerial::begin(uint32_t baud, uint32_t format)
  1217. {
  1218. NVIC_DISABLE_IRQ(IRQ_USBHS);
  1219. baudrate = baud;
  1220. bool format_changed = format != format_;
  1221. format_ = format;
  1222. switch (sertype) {
  1223. default:
  1224. case CDCACM: pending_control |= 0x6; break;
  1225. case FTDI: pending_control |= (format_changed? 0xf : 0xe); break; // Set BAUD, FLOW, DTR
  1226. case PL2303: pending_control |= 0x1e; break; // set more stuff...
  1227. case CH341: pending_control |= 0x1e; break;
  1228. case CP210X: pending_control |= 0xf; break;
  1229. }
  1230. if (!control_queued) control(NULL);
  1231. NVIC_ENABLE_IRQ(IRQ_USBHS);
  1232. // Wait until all packets have been queued before we return to caller.
  1233. while (pending_control) {
  1234. yield(); // not sure if we want to yield or what?
  1235. }
  1236. }
  1237. void USBSerial::end(void)
  1238. {
  1239. NVIC_DISABLE_IRQ(IRQ_USBHS);
  1240. switch (sertype) {
  1241. default:
  1242. case CDCACM: pending_control |= 0x80; break;
  1243. case FTDI: pending_control |= 0x80; break; // clear DTR
  1244. case PL2303: pending_control |= 0x80; break;
  1245. case CH341: pending_control |= 0x80; break;
  1246. }
  1247. if (!control_queued) control(NULL);
  1248. NVIC_ENABLE_IRQ(IRQ_USBHS);
  1249. // Wait until all packets have been queued before we return to caller.
  1250. while (pending_control) {
  1251. yield(); // not sure if we want to yield or what?
  1252. }
  1253. }
  1254. int USBSerial::available(void)
  1255. {
  1256. if (!device) return 0;
  1257. uint32_t head = rxhead;
  1258. uint32_t tail = rxtail;
  1259. if (head >= tail) return head - tail;
  1260. return rxsize + head - tail;
  1261. }
  1262. int USBSerial::peek(void)
  1263. {
  1264. if (!device) return -1;
  1265. uint32_t head = rxhead;
  1266. uint32_t tail = rxtail;
  1267. if (head == tail) return -1;
  1268. if (++tail >= rxsize) tail = 0;
  1269. return rxbuf[tail];
  1270. }
  1271. int USBSerial::read(void)
  1272. {
  1273. if (!device) return -1;
  1274. uint32_t head = rxhead;
  1275. uint32_t tail = rxtail;
  1276. if (head == tail) return -1;
  1277. if (++tail >= rxsize) tail = 0;
  1278. int c = rxbuf[tail];
  1279. rxtail = tail;
  1280. if ((rxstate & 0x03) != 0x03) {
  1281. NVIC_DISABLE_IRQ(IRQ_USBHS);
  1282. rx_queue_packets(head, tail);
  1283. NVIC_ENABLE_IRQ(IRQ_USBHS);
  1284. }
  1285. return c;
  1286. }
  1287. int USBSerial::availableForWrite()
  1288. {
  1289. if (!device) return 0;
  1290. uint32_t head = txhead;
  1291. uint32_t tail = txtail;
  1292. if (head >= tail) return txsize - 1 - head + tail;
  1293. return tail - head - 1;
  1294. }
  1295. size_t USBSerial::write(uint8_t c)
  1296. {
  1297. if (!device) return 0;
  1298. uint32_t head = txhead;
  1299. if (++head >= txsize) head = 0;
  1300. while (txtail == head) {
  1301. // wait...
  1302. }
  1303. txbuf[head] = c;
  1304. txhead = head;
  1305. //print("head=", head);
  1306. //println(", tail=", txtail);
  1307. // if full packet in buffer and tx packet ready, queue it
  1308. NVIC_DISABLE_IRQ(IRQ_USBHS);
  1309. uint32_t tail = txtail;
  1310. if ((txstate & 0x03) != 0x03) {
  1311. // at least one packet buffer is ready to transmit
  1312. uint32_t count;
  1313. if (head >= tail) {
  1314. count = head - tail;
  1315. } else {
  1316. count = txsize + head - tail;
  1317. }
  1318. uint32_t packetsize = tx2 - tx1;
  1319. if (count >= packetsize) {
  1320. //println("txsize=", txsize);
  1321. uint8_t *p;
  1322. if ((txstate & 0x01) == 0) {
  1323. p = tx1;
  1324. txstate |= 0x01;
  1325. } else /* if ((txstate & 0x02) == 0) */ {
  1326. p = tx2;
  1327. txstate |= 0x02;
  1328. }
  1329. // copy data to packet buffer
  1330. if (++tail >= txsize) tail = 0;
  1331. uint32_t n = txsize - tail;
  1332. if (n > packetsize) n = packetsize;
  1333. //print("memcpy, offset=", tail);
  1334. //println(", len=", n);
  1335. memcpy(p, txbuf + tail, n);
  1336. if (n >= packetsize) {
  1337. tail += n - 1;
  1338. if (tail >= txsize) tail = 0;
  1339. } else {
  1340. //n = txsize - n;
  1341. uint32_t len = packetsize - n;
  1342. //println("memcpy, offset=0, len=", len);
  1343. memcpy(p + n, txbuf, len);
  1344. tail = len - 1;
  1345. }
  1346. txtail = tail;
  1347. //println("queue tx packet, newtail=", tail);
  1348. queue_Data_Transfer(txpipe, p, packetsize, this);
  1349. NVIC_ENABLE_IRQ(IRQ_USBHS);
  1350. return 1;
  1351. }
  1352. }
  1353. // otherwise, set a latency timer to later transmit partial packet
  1354. txtimer.stop();
  1355. txtimer.start(3500);
  1356. NVIC_ENABLE_IRQ(IRQ_USBHS);
  1357. return 1;
  1358. }