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.

1314 satır
42KB

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