PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. // True when any hub port is in the reset or reset recovery phase.
  26. // Only one USB device may be reset at a time, because it will
  27. // begin responding to address zero.
  28. volatile bool USBHub::reset_busy = false;
  29. #define print USBHost::print_
  30. #define println USBHost::println_
  31. void USBHub::init()
  32. {
  33. contribute_Devices(mydevices, sizeof(mydevices)/sizeof(Device_t));
  34. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  35. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  36. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  37. driver_ready_for_device(this);
  38. }
  39. bool USBHub::claim(Device_t *dev, int type, const uint8_t *d, uint32_t len)
  40. {
  41. // only claim entire device, never at interface level
  42. if (type != 0) return false;
  43. println("USBHub memory usage = ", sizeof(USBHub));
  44. println("USBHub claim_device this=", (uint32_t)this, HEX);
  45. resettimer.pointer = (void *)"Hello, I'm resettimer";
  46. debouncetimer.pointer = (void *)"Debounce Timer";
  47. // check for HUB type
  48. if (dev->bDeviceClass != 9 || dev->bDeviceSubClass != 0) return false;
  49. // protocol must be 0=FS, 1=HS Single-TT, or 2=HS Multi-TT
  50. if (dev->bDeviceProtocol > 2) return false;
  51. interface_count = 0;
  52. while (len >= 16) {
  53. if (d[0] == 9 && d[1] == 4 && // valid interface descriptor
  54. d[4] == 1 && // has 1 endpoint
  55. d[5] == 9 && // bInterfaceClass is HUB type
  56. d[7] >= 0 && d[7] <= 2 && // bInterfaceProtocol is ok
  57. d[9] == 7 && d[10] == 5 && // valid endpoint descriptor
  58. (d[11] & 0xF0) == 0x80 && // endpoint direction is IN
  59. d[12] == 3 && // endpoint type is interrupt
  60. d[13] == 1 && d[14] == 0) { // max packet size is 1 byte
  61. println("found possible interface, altsetting=", d[3]);
  62. if (interface_count == 0) {
  63. interface_number = d[2];
  64. altsetting = d[3];
  65. protocol = d[7];
  66. endpoint = d[11] & 0x0F;
  67. interval = d[15];
  68. } else {
  69. if (d[2] != interface_number) break;
  70. if (d[7] > protocol) {
  71. altsetting = d[3];
  72. protocol = d[7];
  73. endpoint = d[11] & 0x0F;
  74. interval = d[15];
  75. }
  76. }
  77. interface_count++;
  78. }
  79. d += 16; // jump forward to next interface
  80. len -= 16;
  81. }
  82. if (interface_count == 0) return false; // no usable interface found
  83. println("number of interfaces found = ", interface_count);
  84. if (interface_count > 1) {
  85. print("best interface is ", interface_number);
  86. println(" using altsetting ", altsetting);
  87. }
  88. numports = 0; // unknown until hub descriptor is read
  89. changepipe = NULL;
  90. changebits = 0;
  91. sending_control_transfer = 0;
  92. port_doing_reset = 0;
  93. memset(portstate, 0, sizeof(portstate));
  94. memset(devicelist, 0, sizeof(devicelist));
  95. mk_setup(setup, 0xA0, 6, 0x2900, 0, sizeof(hub_desc));
  96. queue_Control_Transfer(dev, &setup, hub_desc, this);
  97. return true;
  98. }
  99. bool USBHub::can_send_control_now()
  100. {
  101. if (sending_control_transfer) return false;
  102. sending_control_transfer = 1;
  103. return true;
  104. }
  105. void USBHub::send_poweron(uint32_t port)
  106. {
  107. if (port == 0 || port > numports) return;
  108. if (can_send_control_now()) {
  109. mk_setup(setup, 0x23, 3, 8, port, 0);
  110. queue_Control_Transfer(device, &setup, NULL, this);
  111. send_pending_poweron &= ~(1 << port);
  112. } else {
  113. send_pending_poweron |= (1 << port);
  114. }
  115. }
  116. void USBHub::send_getstatus(uint32_t port)
  117. {
  118. if (port > numports) return;
  119. if (can_send_control_now()) {
  120. println("getstatus, port = ", port);
  121. mk_setup(setup, ((port > 0) ? 0xA3 : 0xA0), 0, 0, port, 4);
  122. queue_Control_Transfer(device, &setup, &statusbits, this);
  123. send_pending_getstatus &= ~(1 << port);
  124. } else {
  125. println("deferred getstatus, port = ", port);
  126. send_pending_getstatus |= (1 << port);
  127. }
  128. }
  129. void USBHub::send_clearstatus_connect(uint32_t port)
  130. {
  131. if (port == 0 || port > numports) return;
  132. if (can_send_control_now()) {
  133. mk_setup(setup, 0x23, 1, 16, port, 0); // 16=C_PORT_CONNECTION
  134. queue_Control_Transfer(device, &setup, NULL, this);
  135. send_pending_clearstatus_connect &= ~(1 << port);
  136. } else {
  137. send_pending_clearstatus_connect |= (1 << port);
  138. }
  139. }
  140. void USBHub::send_clearstatus_enable(uint32_t port)
  141. {
  142. if (port == 0 || port > numports) return;
  143. if (can_send_control_now()) {
  144. mk_setup(setup, 0x23, 1, 17, port, 0); // 17=C_PORT_ENABLE
  145. queue_Control_Transfer(device, &setup, NULL, this);
  146. send_pending_clearstatus_enable &= ~(1 << port);
  147. } else {
  148. send_pending_clearstatus_enable |= (1 << port);
  149. }
  150. }
  151. void USBHub::send_clearstatus_suspend(uint32_t port)
  152. {
  153. if (port == 0 || port > numports) return;
  154. if (can_send_control_now()) {
  155. mk_setup(setup, 0x23, 1, 18, port, 0); // 18=C_PORT_SUSPEND
  156. queue_Control_Transfer(device, &setup, NULL, this);
  157. send_pending_clearstatus_suspend &= ~(1 << port);
  158. } else {
  159. send_pending_clearstatus_suspend |= (1 << port);
  160. }
  161. }
  162. void USBHub::send_clearstatus_overcurrent(uint32_t port)
  163. {
  164. if (port == 0 || port > numports) return;
  165. if (can_send_control_now()) {
  166. mk_setup(setup, 0x23, 1, 19, port, 0); // 19=C_PORT_OVER_CURRENT
  167. queue_Control_Transfer(device, &setup, NULL, this);
  168. send_pending_clearstatus_overcurrent &= ~(1 << port);
  169. } else {
  170. send_pending_clearstatus_overcurrent |= (1 << port);
  171. }
  172. }
  173. void USBHub::send_clearstatus_reset(uint32_t port)
  174. {
  175. if (port == 0 || port > numports) return;
  176. if (can_send_control_now()) {
  177. mk_setup(setup, 0x23, 1, 20, port, 0); // 20=C_PORT_RESET
  178. queue_Control_Transfer(device, &setup, NULL, this);
  179. send_pending_clearstatus_reset &= ~(1 << port);
  180. } else {
  181. send_pending_clearstatus_reset |= (1 << port);
  182. }
  183. }
  184. void USBHub::send_setreset(uint32_t port)
  185. {
  186. if (port == 0 || port > numports) return;
  187. println("send_setreset");
  188. if (can_send_control_now()) {
  189. mk_setup(setup, 0x23, 3, 4, port, 0); // set feature PORT_RESET
  190. queue_Control_Transfer(device, &setup, NULL, this);
  191. send_pending_setreset &= ~(1 << port);
  192. } else {
  193. send_pending_setreset |= (1 << port);
  194. }
  195. }
  196. void USBHub::send_setinterface()
  197. {
  198. // assumes not already sending another control transfer
  199. mk_setup(setup, 1, 11, altsetting, interface_number, 0);
  200. queue_Control_Transfer(device, &setup, NULL, this);
  201. sending_control_transfer = 1;
  202. }
  203. static uint32_t lowestbit(uint32_t bitmask)
  204. {
  205. return __builtin_ctz(bitmask);
  206. }
  207. void USBHub::control(const Transfer_t *transfer)
  208. {
  209. println("USBHub control callback");
  210. print_hexbytes(transfer->buffer, transfer->length);
  211. sending_control_transfer = 0;
  212. uint32_t port = transfer->setup.wIndex;
  213. uint32_t mesg = transfer->setup.word1;
  214. switch (mesg) {
  215. case 0x290006A0: // read hub descriptor
  216. numports = hub_desc[2];
  217. characteristics = hub_desc[3];
  218. powertime = hub_desc[5];
  219. if (interface_count > 1) {
  220. send_setinterface();
  221. }
  222. // TODO: do we need to use the DeviceRemovable
  223. // bits to make synthetic device connect events?
  224. println("Hub ports = ", numports);
  225. for (uint32_t i=1; i <= numports; i++) {
  226. send_poweron(i);
  227. }
  228. break;
  229. case 0x00080323: // power turned on
  230. if (port == numports && changepipe == NULL) {
  231. println("power turned on to all ports");
  232. println("device addr = ", device->address);
  233. changepipe = new_Pipe(device, 3, endpoint, 1, 1, interval);
  234. println("pipe cap1 = ", changepipe->qh.capabilities[0], HEX);
  235. changepipe->callback_function = callback;
  236. queue_Data_Transfer(changepipe, &changebits, 1, this);
  237. }
  238. break;
  239. case 0x000000A0: // get hub status
  240. println("New Hub Status");
  241. break;
  242. case 0x000000A3: // get port status
  243. println("New Port Status");
  244. if (transfer->length == 4) {
  245. uint32_t status = *(uint32_t *)(transfer->buffer);
  246. if (status != statusbits) println("ERROR: status not same");
  247. new_port_status(port, status);
  248. }
  249. //if (changebits & (1 << port)) {
  250. //changebits &= ~(1 << port);
  251. //send_clearstatus(port);
  252. //}
  253. break;
  254. case 0x00100120: // clear hub status
  255. println("Hub Status Cleared");
  256. break;
  257. case 0x00100123: // clear port status
  258. println("Port Status Cleared, port=", port);
  259. break;
  260. default:
  261. println("unhandled setup, message = ", mesg, HEX);
  262. }
  263. // After we've completed processing for this control
  264. // transfer, check if any more need to be sent. These
  265. // allow only a single control transfer to occur at once
  266. // which isn't fast, but requires only 3 Transfer_t and
  267. // allows reusing the setup and other buffers
  268. if (sending_control_transfer) return;
  269. if (send_pending_poweron) {
  270. send_poweron(lowestbit(send_pending_poweron));
  271. } else if (send_pending_clearstatus_connect) {
  272. send_clearstatus_connect(lowestbit(send_pending_clearstatus_connect));
  273. } else if (send_pending_clearstatus_enable) {
  274. send_clearstatus_enable(lowestbit(send_pending_clearstatus_enable));
  275. } else if (send_pending_clearstatus_suspend) {
  276. send_clearstatus_suspend(lowestbit(send_pending_clearstatus_suspend));
  277. } else if (send_pending_clearstatus_overcurrent) {
  278. send_clearstatus_overcurrent(lowestbit(send_pending_clearstatus_overcurrent));
  279. } else if (send_pending_clearstatus_reset) {
  280. send_clearstatus_reset(lowestbit(send_pending_clearstatus_reset));
  281. } else if (send_pending_getstatus) {
  282. send_getstatus(lowestbit(send_pending_getstatus));
  283. } else if (send_pending_setreset) {
  284. send_setreset(lowestbit(send_pending_setreset));
  285. }
  286. }
  287. void USBHub::callback(const Transfer_t *transfer)
  288. {
  289. //println("HUB Callback (static)");
  290. if (transfer->driver) ((USBHub *)(transfer->driver))->status_change(transfer);
  291. }
  292. void USBHub::status_change(const Transfer_t *transfer)
  293. {
  294. println("HUB Callback (member)");
  295. println("status = ", changebits, HEX);
  296. for (uint32_t i=0; i <= numports; i++) {
  297. if (changebits & (1 << i)) {
  298. send_getstatus(i);
  299. }
  300. }
  301. queue_Data_Transfer(changepipe, &changebits, 1, this);
  302. }
  303. void USBHub::new_port_status(uint32_t port, uint32_t status)
  304. {
  305. if (port == 0 || port > numports) return;
  306. #if 1
  307. print(" status=");
  308. print(status, HEX);
  309. println(" port=", port);
  310. println(" state=", portstate[port-1]);
  311. // status bits, USB 2.0: 11.24.2.7.1 page 427
  312. if (status & 0x0001) println(" Device is present: ");
  313. if (status & 0x0002) {
  314. print(" Enabled, speed = ");
  315. if (status & 0x0200) {
  316. print("1.5");
  317. } else {
  318. if (status & 0x0400) {
  319. print("480");
  320. } else {
  321. print("12");
  322. }
  323. }
  324. println(" Mbit/sec");
  325. }
  326. if (status & 0x0004) println(" Suspended");
  327. if (status & 0x0008) println(" Over-current");
  328. if (status & 0x0010) println(" Reset");
  329. if (status & 0x0100) println(" Has Power");
  330. if (status & 0x0800) println(" Test Mode");
  331. if (status & 0x1000) println(" Software Controls LEDs");
  332. #endif
  333. uint8_t &state = portstate[port-1];
  334. switch (state) {
  335. case PORT_OFF:
  336. case PORT_DISCONNECT:
  337. if (status & 0x0001) { // connected
  338. state = PORT_DEBOUNCE1;
  339. start_debounce_timer(port);
  340. send_clearstatus_connect(port);
  341. }
  342. break;
  343. case PORT_DEBOUNCE1:
  344. case PORT_DEBOUNCE2:
  345. case PORT_DEBOUNCE3:
  346. case PORT_DEBOUNCE4:
  347. case PORT_DEBOUNCE5:
  348. if (status & 0x0001) {
  349. if (++state > PORT_DEBOUNCE5) {
  350. if (USBHub::reset_busy || USBHost::enumeration_busy) {
  351. // wait in debounce state if another port is
  352. // resetting or a device is busy enumerating
  353. state = PORT_DEBOUNCE5;
  354. break;
  355. }
  356. USBHub::reset_busy = true;
  357. stop_debounce_timer(port);
  358. state = PORT_RESET;
  359. println("sending reset");
  360. send_setreset(port);
  361. port_doing_reset = port;
  362. }
  363. } else {
  364. stop_debounce_timer(port);
  365. state = PORT_DISCONNECT;
  366. }
  367. break;
  368. case PORT_RESET:
  369. if (status & 0x0002) {
  370. // port is now enabled
  371. send_clearstatus_reset(port);
  372. state = PORT_RECOVERY;
  373. uint8_t speed=0;
  374. if (status & 0x0200) speed = 1;
  375. else if (status & 0x0400) speed = 2;
  376. port_doing_reset_speed = speed;
  377. resettimer.start(25000);
  378. } else if (!(status & 0x0001)) {
  379. send_clearstatus_connect(port);
  380. USBHub::reset_busy = false;
  381. state = PORT_DISCONNECT;
  382. }
  383. break;
  384. case PORT_RECOVERY:
  385. if (!(status & 0x0001)) {
  386. send_clearstatus_connect(port);
  387. USBHub::reset_busy = false;
  388. state = PORT_DISCONNECT;
  389. }
  390. break;
  391. case PORT_ACTIVE:
  392. if (!(status & 0x0001)) {
  393. disconnect_Device(devicelist[port-1]);
  394. devicelist[port-1] = NULL;
  395. send_clearstatus_connect(port);
  396. state = PORT_DISCONNECT;
  397. }
  398. break;
  399. }
  400. }
  401. void USBHub::timer_event(USBDriverTimer *timer)
  402. {
  403. uint32_t us = micros() - timer->started_micros;
  404. print("timer event (");
  405. print(us);
  406. print(" us): ");
  407. print((char *)timer->pointer);
  408. print(", this = ");
  409. print((uint32_t)this, HEX);
  410. println(", timer = ", (uint32_t)timer, HEX);
  411. if (timer == &debouncetimer) {
  412. uint32_t in_use = debounce_in_use;
  413. println("ports in use bitmask = ", in_use, HEX);
  414. if (in_use) {
  415. for (uint32_t i=1; i <= numports; i++) {
  416. if (in_use & (1 << i)) send_getstatus(i);
  417. }
  418. debouncetimer.start(20000);
  419. }
  420. } else if (timer == &resettimer) {
  421. uint8_t port = port_doing_reset;
  422. println("port_doing_reset = ", port);
  423. if (port_doing_reset) {
  424. uint8_t &state = portstate[port-1];
  425. if (state == PORT_RECOVERY) {
  426. port_doing_reset = 0;
  427. println("PORT_RECOVERY");
  428. // begin enumeration process
  429. uint8_t speed = port_doing_reset_speed;
  430. devicelist[port-1] = new_Device(speed, device->address, port);
  431. // TODO: if return is NULL, what to do? Panic?
  432. // Can we disable the port? Will this device
  433. // play havoc if it sits unconfigured responding
  434. // to address zero? Does that even matter? Maybe
  435. // we have far worse issues when memory isn't
  436. // available?!
  437. USBHub::reset_busy = false;
  438. state = PORT_ACTIVE;
  439. }
  440. }
  441. }
  442. // TODO: testing only!!!
  443. //static uint32_t count=0;
  444. //if (++count > 36) while (1) ; // stop here
  445. }
  446. void USBHub::start_debounce_timer(uint32_t port)
  447. {
  448. if (debounce_in_use == 0) debouncetimer.start(20000);
  449. debounce_in_use |= (1 << port);
  450. }
  451. void USBHub::stop_debounce_timer(uint32_t port)
  452. {
  453. debounce_in_use &= ~(1 << port);
  454. }
  455. void USBHub::disconnect()
  456. {
  457. // disconnect all downstream devices, which may be more hubs
  458. for (uint32_t i=0; i < numports; i++) {
  459. if (devicelist[i]) disconnect_Device(devicelist[i]);
  460. }
  461. numports = 0;
  462. changepipe = NULL;
  463. changebits = 0;
  464. sending_control_transfer = 0;
  465. port_doing_reset = 0;
  466. memset(portstate, 0, sizeof(portstate));
  467. memset(devicelist, 0, sizeof(devicelist));
  468. send_pending_poweron = 0;
  469. send_pending_getstatus = 0;
  470. send_pending_clearstatus_connect = 0;
  471. send_pending_clearstatus_enable = 0;
  472. send_pending_clearstatus_suspend = 0;
  473. send_pending_clearstatus_overcurrent = 0;
  474. send_pending_clearstatus_reset = 0;
  475. send_pending_setreset = 0;
  476. debounce_in_use = 0;
  477. }
  478. /*
  479. config descriptor from a Multi-TT hub
  480. 09 02 29 00 01 01 00 E0 32
  481. 09 04 00 00 01 09 00 01 00
  482. 07 05 81 03 01 00 0C
  483. 09 04 00 01 01 09 00 02 00
  484. 07 05 81 03 01 00 0C
  485. */