PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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