drv_usbh.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-10-30 ZYH the first version
  9. * 2019-12-19 tyustli port to stm32 series
  10. * 2021-01-19 Leslie Lee port to imxrt series
  11. */
  12. #include <rtthread.h>
  13. #if defined(BSP_USB0_HOST) || defined(BSP_USB1_HOST)
  14. #include "drv_usbh.h"
  15. #include <usb/include/usb_host_config.h>
  16. #include <usb/include/usb.h>
  17. #include <usb/phy/usb_phy.h>
  18. #include <usb/host/usb_host.h>
  19. #include <usb/host/usb_host_hci.h>
  20. #include <usb/host/usb_host_ehci.h>
  21. #include <rtdevice.h>
  22. /* USB PHY configuration */
  23. #ifndef BOARD_USB_PHY_D_CAL
  24. #define BOARD_USB_PHY_D_CAL (0x0CU)
  25. #endif
  26. #ifndef BOARD_USB_PHY_TXCAL45DP
  27. #define BOARD_USB_PHY_TXCAL45DP (0x06U)
  28. #endif
  29. #ifndef BOARD_USB_PHY_TXCAL45DM
  30. #define BOARD_USB_PHY_TXCAL45DM (0x06U)
  31. #endif
  32. #define USB_HOST_INTERRUPT_PRIORITY 3
  33. enum
  34. {
  35. #ifdef BSP_USB0_HOST
  36. USBH0_INDEX,
  37. #endif
  38. #ifdef BSP_USB1_HOST
  39. USBH1_INDEX,
  40. #endif
  41. };
  42. struct imxrt_usb_host_pipe
  43. {
  44. usb_host_pipe_handle pipe_handle;
  45. struct rt_completion urb_completion;
  46. usb_status_t transfer_status;
  47. };
  48. struct imxrt_usb_host
  49. {
  50. struct uhcd uhcd;
  51. usb_host_handle host_handle;
  52. usb_device_handle device_handle;
  53. struct imxrt_usb_host_pipe pipes[16];
  54. volatile rt_bool_t connect_status;
  55. char *name;
  56. };
  57. static struct imxrt_usb_host imxrt_usb_host_obj[] =
  58. {
  59. #ifdef BSP_USB0_HOST
  60. {
  61. .connect_status = RT_FALSE,
  62. .name = "usbh0"
  63. },
  64. #endif
  65. #ifdef BSP_USB1_HOST
  66. {
  67. .connect_status = RT_FALSE,
  68. .name = "usbh1"
  69. },
  70. #endif
  71. };
  72. static void _imxrt_usb_host_send_callback(void *param, usb_host_transfer_t *transfer, usb_status_t status)
  73. {
  74. struct imxrt_usb_host_pipe *pipe = (struct imxrt_usb_host_pipe *)param;
  75. pipe->transfer_status = status;
  76. rt_completion_done(&pipe->urb_completion);
  77. }
  78. /*!
  79. * @brief Initializes USB specific setting that was not set by the Clocks tool.
  80. */
  81. static void USB_HostClockInit(usb_controller_index_t controller_id)
  82. {
  83. usb_phy_config_struct_t phyConfig = {
  84. BOARD_USB_PHY_D_CAL, BOARD_USB_PHY_TXCAL45DP, BOARD_USB_PHY_TXCAL45DM,
  85. };
  86. uint32_t notUsed = 0;
  87. if (controller_id == kUSB_ControllerEhci0)
  88. {
  89. CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  90. CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);
  91. }
  92. else
  93. {
  94. CLOCK_EnableUsbhs1PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  95. CLOCK_EnableUsbhs1Clock(kCLOCK_Usb480M, 480000000U);
  96. }
  97. USB_EhciPhyInit(controller_id, 24000000U, &phyConfig);
  98. }
  99. /*!
  100. * @brief Enables interrupt service routines for device.
  101. */
  102. void USB_HostIsrEnable(usb_controller_index_t controller_id)
  103. {
  104. uint8_t irqNumber;
  105. #if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI > 0U))
  106. IRQn_Type usbHOSTEhciIrq[] = USBHS_IRQS;
  107. irqNumber = usbHOSTEhciIrq[controller_id - kUSB_ControllerEhci0];
  108. #endif
  109. /* Install isr, set priority, and enable IRQ. */
  110. #if defined(__GIC_PRIO_BITS)
  111. GIC_SetPriority((IRQn_Type)irqNumber, USB_HOST_INTERRUPT_PRIORITY);
  112. #else
  113. NVIC_SetPriority((IRQn_Type)irqNumber, USB_HOST_INTERRUPT_PRIORITY);
  114. #endif
  115. EnableIRQ((IRQn_Type)irqNumber);
  116. }
  117. #ifdef BSP_USB0_HOST
  118. void USB_OTG1_IRQHandler(void)
  119. {
  120. /* enter interrupt */
  121. rt_interrupt_enter();
  122. USB_HostEhciIsrFunction(imxrt_usb_host_obj[USBH0_INDEX].host_handle);
  123. /* leave interrupt */
  124. rt_interrupt_leave();
  125. }
  126. static rt_err_t _ehci0_reset_port(rt_uint8_t port)
  127. {
  128. // No reset port function available
  129. return RT_EOK;
  130. }
  131. static uint8_t _ehci0_pipe_buf[64];
  132. static uint8_t _ehci0_pipe_idx;
  133. static int _ehci0_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeouts)
  134. {
  135. int timeout = timeouts;
  136. if (!imxrt_usb_host_obj[USBH0_INDEX].connect_status)
  137. {
  138. return -1;
  139. }
  140. usb_host_transfer_t *transfer;
  141. if (imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index].pipe_handle == NULL)
  142. {
  143. RT_DEBUG_LOG(RT_DEBUG_USB, ("error operation on null pipe\n"));
  144. return -1;
  145. }
  146. if (USB_HostMallocTransfer(imxrt_usb_host_obj[USBH0_INDEX].host_handle, &transfer) != kStatus_USB_Success)
  147. {
  148. RT_DEBUG_LOG(RT_DEBUG_USB, ("error to get transfer\n"));
  149. return -1;
  150. }
  151. transfer->transferBuffer = buffer;
  152. transfer->transferLength = nbytes;
  153. transfer->transferSofar = 0;
  154. transfer->callbackFn = _imxrt_usb_host_send_callback;
  155. transfer->callbackParam = &(imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index]);
  156. transfer->direction = (pipe->ep.bEndpointAddress & USB_DIR_IN) ? USB_IN : USB_OUT;
  157. if (pipe->ep.bmAttributes == USB_ENDPOINT_CONTROL)
  158. {
  159. if (token == USBH_PID_SETUP)
  160. {
  161. struct urequest *setup = (struct urequest *)buffer;
  162. transfer->setupStatus = 0;
  163. transfer->setupPacket->bmRequestType = setup->request_type;
  164. transfer->setupPacket->bRequest = setup->bRequest;
  165. transfer->setupPacket->wIndex = setup->wIndex;
  166. transfer->setupPacket->wLength = setup->wLength;
  167. transfer->setupPacket->wValue = setup->wValue;
  168. transfer->transferBuffer = RT_NULL;
  169. transfer->transferLength = 0;
  170. transfer->next = RT_NULL;
  171. if ((transfer->setupPacket->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_IN)
  172. {
  173. transfer->direction = USB_IN;
  174. transfer->transferBuffer = _ehci0_pipe_buf;
  175. transfer->transferLength = setup->wLength;
  176. _ehci0_pipe_idx = 0;
  177. }
  178. else
  179. {
  180. transfer->direction = USB_OUT;
  181. }
  182. }
  183. else
  184. {
  185. rt_memcpy(buffer, _ehci0_pipe_buf + _ehci0_pipe_idx, nbytes);
  186. imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index].transfer_status = kStatus_USB_Success;
  187. transfer->transferSofar = nbytes;
  188. _ehci0_pipe_idx += nbytes;
  189. if (_ehci0_pipe_idx >= 64)
  190. {
  191. _ehci0_pipe_idx = 0;
  192. }
  193. goto _ehci0_pipe_xfer_finish;
  194. }
  195. }
  196. rt_completion_init(&(imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index].urb_completion));
  197. if (USB_HostEhciWritePipe(((usb_host_instance_t *)imxrt_usb_host_obj[USBH0_INDEX].host_handle)->controllerHandle, imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index].pipe_handle, transfer) != kStatus_USB_Success)
  198. {
  199. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb host failed to send\n"));
  200. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH0_INDEX].host_handle, transfer);
  201. return -1;
  202. }
  203. if (-RT_ETIMEOUT == rt_completion_wait(&(imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index].urb_completion), RT_WAITING_FOREVER))
  204. {
  205. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb transfer timeout\n"));
  206. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH0_INDEX].host_handle, transfer);
  207. return -1;
  208. }
  209. _ehci0_pipe_xfer_finish:
  210. switch (imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index].transfer_status)
  211. {
  212. case kStatus_USB_Success:
  213. {
  214. RT_DEBUG_LOG(RT_DEBUG_USB, ("ok\n"));
  215. pipe->status = UPIPE_STATUS_OK;
  216. if (pipe->callback != RT_NULL)
  217. {
  218. pipe->callback(pipe);
  219. }
  220. size_t size = transfer->transferSofar;
  221. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH0_INDEX].host_handle, transfer);
  222. if (pipe->ep.bEndpointAddress & 0x80)
  223. {
  224. return size;
  225. }
  226. else if (pipe->ep.bEndpointAddress & 0x00)
  227. {
  228. return size;
  229. }
  230. return nbytes;
  231. break;
  232. }
  233. case kStatus_USB_TransferStall:
  234. {
  235. RT_DEBUG_LOG(RT_DEBUG_USB, ("stall\n"));
  236. pipe->status = UPIPE_STATUS_STALL;
  237. if (pipe->callback != RT_NULL)
  238. {
  239. pipe->callback(pipe);
  240. }
  241. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH0_INDEX].host_handle, transfer);
  242. return -1;
  243. break;
  244. }
  245. case kStatus_USB_TransferFailed:
  246. default:
  247. {
  248. RT_DEBUG_LOG(RT_DEBUG_USB, ("error\n"));
  249. pipe->status = UPIPE_STATUS_ERROR;
  250. if (pipe->callback != RT_NULL)
  251. {
  252. pipe->callback(pipe);
  253. }
  254. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH0_INDEX].host_handle, transfer);
  255. return -1;
  256. break;
  257. }
  258. }
  259. }
  260. static rt_uint16_t _ehci0_pipe_index = 0;
  261. static rt_uint8_t _ehci0_get_free_pipe_index(void)
  262. {
  263. rt_uint8_t idx;
  264. for (idx = 1; idx < 16; idx++)
  265. {
  266. if (!(_ehci0_pipe_index & (0x01 << idx)))
  267. {
  268. _ehci0_pipe_index |= (0x01 << idx);
  269. return idx;
  270. }
  271. }
  272. return 0xff;
  273. }
  274. static void _ehci0_free_pipe_index(rt_uint8_t index)
  275. {
  276. _ehci0_pipe_index &= ~(0x01 << index);
  277. }
  278. static rt_err_t _ehci0_open_pipe(upipe_t pipe)
  279. {
  280. pipe->pipe_index = _ehci0_get_free_pipe_index();
  281. if (pipe->pipe_index == 0xFF)
  282. {
  283. return -RT_ERROR;
  284. }
  285. usb_host_pipe_init_t pipe_init =
  286. {
  287. .devInstance = imxrt_usb_host_obj[USBH0_INDEX].device_handle,
  288. .pipeType = pipe->ep.bmAttributes,
  289. .direction = (pipe->ep.bEndpointAddress & USB_DIR_IN) ? USB_IN : USB_OUT,
  290. .endpointAddress = (pipe->ep.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK),
  291. .interval = pipe->ep.bInterval,
  292. .maxPacketSize = (uint16_t)(pipe->ep.wMaxPacketSize & USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_SIZE_MASK),
  293. .numberPerUframe = (uint8_t)(pipe->ep.wMaxPacketSize & USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_MASK),
  294. .nakCount = USB_HOST_CONFIG_MAX_NAK,
  295. };
  296. USB_HostOpenPipe(imxrt_usb_host_obj[USBH0_INDEX].host_handle, &imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index].pipe_handle, &pipe_init);
  297. return RT_EOK;
  298. }
  299. static rt_err_t _ehci0_close_pipe(upipe_t pipe)
  300. {
  301. (void)USB_HostClosePipe(imxrt_usb_host_obj[USBH0_INDEX].host_handle, imxrt_usb_host_obj[USBH0_INDEX].pipes[pipe->pipe_index].pipe_handle);
  302. _ehci0_free_pipe_index(pipe->pipe_index);
  303. return RT_EOK;
  304. }
  305. static struct uhcd_ops _ehci0_uhcd_ops =
  306. {
  307. _ehci0_reset_port,
  308. _ehci0_pipe_xfer,
  309. _ehci0_open_pipe,
  310. _ehci0_close_pipe,
  311. };
  312. static usb_status_t usb0_host_callback(usb_device_handle handle, usb_host_configuration_handle config_handle, rt_uint32_t event_code)
  313. {
  314. usb_status_t status = kStatus_USB_Success;
  315. switch (event_code)
  316. {
  317. case kUSB_HostEventAttach:
  318. if (!imxrt_usb_host_obj[USBH0_INDEX].connect_status)
  319. {
  320. imxrt_usb_host_obj[USBH0_INDEX].connect_status = RT_TRUE;
  321. imxrt_usb_host_obj[USBH0_INDEX].device_handle = handle;
  322. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb connected\n"));
  323. rt_usbh_root_hub_connect_handler(&(imxrt_usb_host_obj[USBH0_INDEX].uhcd), OTG_PORT, RT_TRUE);
  324. }
  325. break;
  326. case kUSB_HostEventNotSupported:
  327. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb device not supported\n"));
  328. break;
  329. case kUSB_HostEventEnumerationDone:
  330. RT_DEBUG_LOG(RT_DEBUG_USB, ("enumeration done\n"));
  331. break;
  332. case kUSB_HostEventDetach:
  333. if (imxrt_usb_host_obj[USBH0_INDEX].connect_status)
  334. {
  335. imxrt_usb_host_obj[USBH0_INDEX].connect_status = RT_FALSE;
  336. imxrt_usb_host_obj[USBH0_INDEX].device_handle = handle;
  337. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb disconnnect\n"));
  338. rt_usbh_root_hub_disconnect_handler(&(imxrt_usb_host_obj[USBH0_INDEX].uhcd), OTG_PORT);
  339. (void)USB_HostCloseDeviceInterface(handle, NULL);
  340. }
  341. break;
  342. default:
  343. break;
  344. }
  345. return status;
  346. }
  347. rt_thread_t usbh0_thread;
  348. static void _ehci0_usbh_thread(void* param)
  349. {
  350. while (1)
  351. {
  352. USB_HostEhciTaskFunction(imxrt_usb_host_obj[USBH0_INDEX].host_handle);
  353. }
  354. }
  355. static rt_err_t _ehci0_usbh_init(rt_device_t device)
  356. {
  357. USB_HostClockInit(kUSB_ControllerEhci0);
  358. if (kStatus_USB_Success == USB_HostInit(kUSB_ControllerEhci0, &imxrt_usb_host_obj[USBH0_INDEX].host_handle, usb0_host_callback))
  359. {
  360. usbh0_thread = rt_thread_create("ehci0", _ehci0_usbh_thread, RT_NULL, 500, 4, 9999999);
  361. rt_thread_startup(usbh0_thread);
  362. USB_HostIsrEnable(kUSB_ControllerEhci0);
  363. }
  364. else
  365. {
  366. rt_kprintf("USB_HostInit ehci0 error\r\n");
  367. return -RT_ERROR;
  368. }
  369. return RT_EOK;
  370. }
  371. #endif
  372. #ifdef BSP_USB1_HOST
  373. void USB_OTG2_IRQHandler(void)
  374. {
  375. /* enter interrupt */
  376. rt_interrupt_enter();
  377. USB_HostEhciIsrFunction(imxrt_usb_host_obj[USBH1_INDEX].host_handle);
  378. /* leave interrupt */
  379. rt_interrupt_leave();
  380. }
  381. static rt_err_t _ehci1_reset_port(rt_uint8_t port)
  382. {
  383. // No reset port function available
  384. return RT_EOK;
  385. }
  386. static uint8_t _ehci1_pipe_buf[64];
  387. static uint8_t _ehci1_pipe_idx;
  388. static int _ehci1_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeouts)
  389. {
  390. int timeout = timeouts;
  391. if (!imxrt_usb_host_obj[USBH1_INDEX].connect_status)
  392. {
  393. return -1;
  394. }
  395. usb_host_transfer_t *transfer;
  396. if (imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index].pipe_handle == NULL)
  397. {
  398. RT_DEBUG_LOG(RT_DEBUG_USB, ("error operation on null pipe\n"));
  399. return -1;
  400. }
  401. if (USB_HostMallocTransfer(imxrt_usb_host_obj[USBH1_INDEX].host_handle, &transfer) != kStatus_USB_Success)
  402. {
  403. RT_DEBUG_LOG(RT_DEBUG_USB, ("error to get transfer\n"));
  404. return -1;
  405. }
  406. transfer->transferBuffer = buffer;
  407. transfer->transferLength = nbytes;
  408. transfer->transferSofar = 0;
  409. transfer->callbackFn = _imxrt_usb_host_send_callback;
  410. transfer->callbackParam = &(imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index]);
  411. transfer->direction = (pipe->ep.bEndpointAddress & USB_DIR_IN) ? USB_IN : USB_OUT;
  412. if (pipe->ep.bmAttributes == USB_ENDPOINT_CONTROL)
  413. {
  414. if (token == USBH_PID_SETUP)
  415. {
  416. struct urequest *setup = (struct urequest *)buffer;
  417. transfer->setupStatus = 0;
  418. transfer->setupPacket->bmRequestType = setup->request_type;
  419. transfer->setupPacket->bRequest = setup->bRequest;
  420. transfer->setupPacket->wIndex = setup->wIndex;
  421. transfer->setupPacket->wLength = setup->wLength;
  422. transfer->setupPacket->wValue = setup->wValue;
  423. transfer->transferBuffer = RT_NULL;
  424. transfer->transferLength = 0;
  425. transfer->next = RT_NULL;
  426. if ((transfer->setupPacket->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_IN)
  427. {
  428. transfer->direction = USB_IN;
  429. transfer->transferBuffer = _ehci1_pipe_buf;
  430. transfer->transferLength = setup->wLength;
  431. _ehci1_pipe_idx = 0;
  432. }
  433. else
  434. {
  435. transfer->direction = USB_OUT;
  436. }
  437. }
  438. else
  439. {
  440. rt_memcpy(buffer, _ehci1_pipe_buf + _ehci1_pipe_idx, nbytes);
  441. imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index].transfer_status = kStatus_USB_Success;
  442. transfer->transferSofar = nbytes;
  443. _ehci1_pipe_idx += nbytes;
  444. if (_ehci1_pipe_idx >= 64)
  445. {
  446. _ehci1_pipe_idx = 0;
  447. }
  448. goto _ehci1_pipe_xfer_finish;
  449. }
  450. }
  451. rt_completion_init(&(imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index].urb_completion));
  452. if (USB_HostEhciWritePipe(((usb_host_instance_t *)imxrt_usb_host_obj[USBH1_INDEX].host_handle)->controllerHandle, imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index].pipe_handle, transfer) != kStatus_USB_Success)
  453. {
  454. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb host failed to send\n"));
  455. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH1_INDEX].host_handle, transfer);
  456. return -1;
  457. }
  458. if (-RT_ETIMEOUT == rt_completion_wait(&(imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index].urb_completion), RT_WAITING_FOREVER))
  459. {
  460. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb transfer timeout\n"));
  461. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH1_INDEX].host_handle, transfer);
  462. return -1;
  463. }
  464. // rt_thread_mdelay(1);
  465. _ehci1_pipe_xfer_finish:
  466. switch (imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index].transfer_status)
  467. {
  468. case kStatus_USB_Success:
  469. {
  470. RT_DEBUG_LOG(RT_DEBUG_USB, ("ok\n"));
  471. pipe->status = UPIPE_STATUS_OK;
  472. if (pipe->callback != RT_NULL)
  473. {
  474. pipe->callback(pipe);
  475. }
  476. size_t size = transfer->transferSofar;
  477. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH1_INDEX].host_handle, transfer);
  478. if (pipe->ep.bEndpointAddress & 0x80)
  479. {
  480. return size;
  481. }
  482. else if (pipe->ep.bEndpointAddress & 0x00)
  483. {
  484. return size;
  485. }
  486. return nbytes;
  487. break;
  488. }
  489. case kStatus_USB_TransferStall:
  490. {
  491. RT_DEBUG_LOG(RT_DEBUG_USB, ("stall\n"));
  492. pipe->status = UPIPE_STATUS_STALL;
  493. if (pipe->callback != RT_NULL)
  494. {
  495. pipe->callback(pipe);
  496. }
  497. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH1_INDEX].host_handle, transfer);
  498. return -1;
  499. break;
  500. }
  501. case kStatus_USB_TransferFailed:
  502. default:
  503. {
  504. RT_DEBUG_LOG(RT_DEBUG_USB, ("error\n"));
  505. pipe->status = UPIPE_STATUS_ERROR;
  506. if (pipe->callback != RT_NULL)
  507. {
  508. pipe->callback(pipe);
  509. }
  510. (void)USB_HostFreeTransfer(imxrt_usb_host_obj[USBH1_INDEX].host_handle, transfer);
  511. return -1;
  512. break;
  513. }
  514. }
  515. }
  516. static rt_uint16_t _ehci1_pipe_index = 0;
  517. static rt_uint8_t _ehci1_get_free_pipe_index(void)
  518. {
  519. rt_uint8_t idx;
  520. for (idx = 1; idx < 16; idx++)
  521. {
  522. if (!(_ehci1_pipe_index & (0x01 << idx)))
  523. {
  524. _ehci1_pipe_index |= (0x01 << idx);
  525. return idx;
  526. }
  527. }
  528. return 0xff;
  529. }
  530. static void _ehci1_free_pipe_index(rt_uint8_t index)
  531. {
  532. _ehci1_pipe_index &= ~(0x01 << index);
  533. }
  534. static rt_err_t _ehci1_open_pipe(upipe_t pipe)
  535. {
  536. pipe->pipe_index = _ehci1_get_free_pipe_index();
  537. if (pipe->pipe_index == 0xFF)
  538. {
  539. return -RT_ERROR;
  540. }
  541. usb_host_pipe_init_t pipe_init =
  542. {
  543. .devInstance = imxrt_usb_host_obj[USBH1_INDEX].device_handle,
  544. .pipeType = pipe->ep.bmAttributes,
  545. .direction = (pipe->ep.bEndpointAddress & USB_DIR_IN) ? USB_IN : USB_OUT,
  546. .endpointAddress = (pipe->ep.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK),
  547. .interval = pipe->ep.bInterval,
  548. .maxPacketSize = (uint16_t)(pipe->ep.wMaxPacketSize & USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_SIZE_MASK),
  549. .numberPerUframe = (uint8_t)(pipe->ep.wMaxPacketSize & USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_MASK),
  550. .nakCount = USB_HOST_CONFIG_MAX_NAK,
  551. };
  552. USB_HostOpenPipe(imxrt_usb_host_obj[USBH1_INDEX].host_handle, &imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index].pipe_handle, &pipe_init);
  553. return RT_EOK;
  554. }
  555. static rt_err_t _ehci1_close_pipe(upipe_t pipe)
  556. {
  557. (void)USB_HostClosePipe(imxrt_usb_host_obj[USBH1_INDEX].host_handle, imxrt_usb_host_obj[USBH1_INDEX].pipes[pipe->pipe_index].pipe_handle);
  558. _ehci1_free_pipe_index(pipe->pipe_index);
  559. return RT_EOK;
  560. }
  561. static struct uhcd_ops _ehci1_uhcd_ops =
  562. {
  563. _ehci1_reset_port,
  564. _ehci1_pipe_xfer,
  565. _ehci1_open_pipe,
  566. _ehci1_close_pipe,
  567. };
  568. static usb_status_t usb1_host_callback(usb_device_handle handle, usb_host_configuration_handle config_handle, rt_uint32_t event_code)
  569. {
  570. usb_status_t status = kStatus_USB_Success;
  571. switch (event_code)
  572. {
  573. case kUSB_HostEventAttach:
  574. if (!imxrt_usb_host_obj[USBH1_INDEX].connect_status)
  575. {
  576. imxrt_usb_host_obj[USBH1_INDEX].connect_status = RT_TRUE;
  577. imxrt_usb_host_obj[USBH1_INDEX].device_handle = handle;
  578. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb connected\n"));
  579. rt_usbh_root_hub_connect_handler(&(imxrt_usb_host_obj[USBH1_INDEX].uhcd), OTG_PORT, RT_TRUE);
  580. }
  581. break;
  582. case kUSB_HostEventNotSupported:
  583. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb device not supported\n"));
  584. break;
  585. case kUSB_HostEventEnumerationDone:
  586. RT_DEBUG_LOG(RT_DEBUG_USB, ("enumeration done\n"));
  587. break;
  588. case kUSB_HostEventDetach:
  589. if (imxrt_usb_host_obj[USBH1_INDEX].connect_status)
  590. {
  591. imxrt_usb_host_obj[USBH1_INDEX].connect_status = RT_FALSE;
  592. imxrt_usb_host_obj[USBH1_INDEX].device_handle = handle;
  593. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb disconnnect\n"));
  594. rt_usbh_root_hub_disconnect_handler(&(imxrt_usb_host_obj[USBH1_INDEX].uhcd), OTG_PORT);
  595. (void)USB_HostCloseDeviceInterface(handle, NULL);
  596. }
  597. break;
  598. default:
  599. break;
  600. }
  601. return status;
  602. }
  603. rt_thread_t usbh1_thread;
  604. static void _ehci1_usbh_thread(void* param)
  605. {
  606. while (1)
  607. {
  608. USB_HostEhciTaskFunction(imxrt_usb_host_obj[USBH1_INDEX].host_handle);
  609. }
  610. }
  611. static rt_err_t _ehci1_usbh_init(rt_device_t device)
  612. {
  613. USB_HostClockInit(kUSB_ControllerEhci1);
  614. if (kStatus_USB_Success == USB_HostInit(kUSB_ControllerEhci1, &imxrt_usb_host_obj[USBH1_INDEX].host_handle, usb1_host_callback))
  615. {
  616. usbh1_thread = rt_thread_create("ehci1", _ehci1_usbh_thread, RT_NULL, 500, 4, 9999999);
  617. rt_thread_startup(usbh1_thread);
  618. USB_HostIsrEnable(kUSB_ControllerEhci1);
  619. }
  620. else
  621. {
  622. rt_kprintf("USB_HostInit ehci1 error\r\n");
  623. return -RT_ERROR;
  624. }
  625. return RT_EOK;
  626. }
  627. #endif
  628. int imxrt_usbh_register(void)
  629. {
  630. rt_err_t res = -RT_ERROR;
  631. struct imxrt_usb_host *usb_host_obj;
  632. #ifdef BSP_USB0_HOST
  633. usb_host_obj = &(imxrt_usb_host_obj[USBH0_INDEX]);
  634. rt_memset((void *)(&(usb_host_obj->uhcd)), 0, sizeof(struct uhcd));
  635. usb_host_obj->uhcd.parent.type = RT_Device_Class_USBHost;
  636. usb_host_obj->uhcd.parent.init = _ehci0_usbh_init;
  637. usb_host_obj->uhcd.parent.user_data = usb_host_obj;
  638. usb_host_obj->uhcd.ops = &_ehci0_uhcd_ops;
  639. usb_host_obj->uhcd.num_ports = OTG_PORT;
  640. res = rt_device_register(&(usb_host_obj->uhcd.parent), usb_host_obj->name, RT_DEVICE_FLAG_DEACTIVATE);
  641. if (res != RT_EOK)
  642. {
  643. rt_kprintf("register usb0 host failed res = %d\r\n", res);
  644. return -RT_ERROR;
  645. }
  646. rt_usb_host_init(usb_host_obj->name);
  647. #endif
  648. #ifdef BSP_USB1_HOST
  649. usb_host_obj = &(imxrt_usb_host_obj[USBH1_INDEX]);
  650. rt_memset((void *)(&(usb_host_obj->uhcd)), 0, sizeof(struct uhcd));
  651. usb_host_obj->uhcd.parent.type = RT_Device_Class_USBHost;
  652. usb_host_obj->uhcd.parent.init = _ehci1_usbh_init;
  653. usb_host_obj->uhcd.parent.user_data = usb_host_obj;
  654. usb_host_obj->uhcd.ops = &_ehci1_uhcd_ops;
  655. usb_host_obj->uhcd.num_ports = OTG_PORT;
  656. res = rt_device_register(&(usb_host_obj->uhcd.parent), usb_host_obj->name, RT_DEVICE_FLAG_DEACTIVATE);
  657. if (res != RT_EOK)
  658. {
  659. rt_kprintf("register usb0 host failed res = %d\r\n", res);
  660. return -RT_ERROR;
  661. }
  662. rt_usb_host_init(usb_host_obj->name);
  663. #endif
  664. return RT_EOK;
  665. }
  666. INIT_DEVICE_EXPORT(imxrt_usbh_register);
  667. #endif