Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

1217 lines
38KB

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