drv_usbd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * File : drv_usbc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-12-04 ZYH first implementation
  13. */
  14. #include <usb/include/usb_device_config.h>
  15. #include <usb/include/usb.h>
  16. #include <rtthread.h>
  17. #include <usb/phy/usb_phy.h>
  18. #include <usb/device/usb_device.h>
  19. #include <usb/device/usb_device_dci.h>
  20. #include <rtdevice.h>
  21. #ifdef RT_USING_EHCI0_AS_DEVICE
  22. #ifdef RT_USING_EHCI1_AS_DEVICE
  23. #error Can not using 2 controller as usb device
  24. #endif
  25. #endif
  26. #define FSL_USB_HS
  27. /* USB PHY condfiguration */
  28. #define BOARD_USB_PHY_D_CAL (0x0CU)
  29. #define BOARD_USB_PHY_TXCAL45DP (0x06U)
  30. #define BOARD_USB_PHY_TXCAL45DM (0x06U)
  31. #define BOARD_XTAL0_CLK_HZ 24000000U
  32. #ifdef RT_USING_EHCI0_AS_DEVICE
  33. static usb_device_handle ehci0_handle;
  34. static struct udcd _fsl_udc_0;
  35. #endif
  36. #ifdef RT_USING_EHCI1_AS_DEVICE
  37. static usb_device_handle ehci1_handle;
  38. static struct udcd _fsl_udc_1;
  39. #endif
  40. static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam);
  41. static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam);
  42. void USB_DeviceIsrEnable(uint8_t controllerId)
  43. {
  44. uint8_t irqNumber;
  45. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  46. uint8_t usbDeviceEhciIrq[] = USBHS_IRQS;
  47. irqNumber = usbDeviceEhciIrq[controllerId - kUSB_ControllerEhci0];
  48. #endif
  49. /* Install isr, set priority, and enable IRQ. */
  50. #if defined(__GIC_PRIO_BITS)
  51. GIC_SetPriority((IRQn_Type)irqNumber, 3);
  52. #else
  53. NVIC_SetPriority((IRQn_Type)irqNumber, 3);
  54. #endif
  55. EnableIRQ((IRQn_Type)irqNumber);
  56. }
  57. void USB_DeviceClockInit(uint8_t controllerId)
  58. {
  59. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  60. usb_phy_config_struct_t phyConfig = {
  61. BOARD_USB_PHY_D_CAL, BOARD_USB_PHY_TXCAL45DP, BOARD_USB_PHY_TXCAL45DM,
  62. };
  63. #endif
  64. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  65. if (controllerId == kUSB_ControllerEhci0)
  66. {
  67. CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  68. CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);
  69. }
  70. else
  71. {
  72. CLOCK_EnableUsbhs1PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  73. CLOCK_EnableUsbhs1Clock(kCLOCK_Usb480M, 480000000U);
  74. }
  75. USB_EhciPhyInit(controllerId, BOARD_XTAL0_CLK_HZ, &phyConfig);
  76. #endif
  77. }
  78. #ifdef RT_USING_EHCI0_AS_DEVICE
  79. #ifdef FSL_USB_HS
  80. static struct ep_id _ehci0_ep_pool[] =
  81. {
  82. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  83. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  84. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  85. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  86. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  87. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  88. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  89. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  90. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  91. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  92. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  93. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  94. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  95. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  96. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  97. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  98. };
  99. #else
  100. static struct ep_id _ehci0_ep_pool[] =
  101. {
  102. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  103. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  104. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  105. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  106. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  107. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  108. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  109. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  110. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  111. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  112. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  113. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  114. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  115. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  116. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  117. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  118. };
  119. #endif
  120. void USB_OTG1_IRQHandler(void)
  121. {
  122. /* enter interrupt */
  123. rt_interrupt_enter();
  124. USB_DeviceEhciIsrFunction(ehci0_handle);
  125. /* leave interrupt */
  126. rt_interrupt_leave();
  127. }
  128. static rt_err_t _ehci0_ep_set_stall(rt_uint8_t address)
  129. {
  130. USB_DeviceStallEndpoint(ehci0_handle, address);
  131. return RT_EOK;
  132. }
  133. static rt_err_t _ehci0_ep_clear_stall(rt_uint8_t address)
  134. {
  135. USB_DeviceUnstallEndpoint(ehci0_handle, address);
  136. return RT_EOK;
  137. }
  138. static rt_err_t _ehci0_set_address(rt_uint8_t address)
  139. {
  140. USB_DeviceSetStatus(ehci0_handle, kUSB_DeviceStatusAddress, &address);
  141. return RT_EOK;
  142. }
  143. static rt_err_t _ehci0_set_config(rt_uint8_t address)
  144. {
  145. return RT_EOK;
  146. }
  147. static rt_err_t _ehci0_ep_enable(uep_t ep)
  148. {
  149. usb_device_endpoint_init_struct_t ep_init;
  150. usb_device_endpoint_callback_struct_t ep_callback;
  151. rt_uint32_t param = ep->ep_desc->bEndpointAddress;
  152. RT_ASSERT(ep != RT_NULL);
  153. RT_ASSERT(ep->ep_desc != RT_NULL);
  154. ep_init.maxPacketSize = ep->ep_desc->wMaxPacketSize;
  155. ep_init.endpointAddress = ep->ep_desc->bEndpointAddress;
  156. ep_init.transferType = ep->ep_desc->bmAttributes;
  157. ep_init.zlt = 0;
  158. ep_callback.callbackFn = usb_device_endpoint_callback;
  159. ep_callback.callbackParam = (void *)param;
  160. ep_callback.isBusy = 0;
  161. USB_DeviceInitEndpoint(ehci0_handle,&ep_init,&ep_callback);
  162. return RT_EOK;
  163. }
  164. static rt_err_t _ehci0_ep_disable(uep_t ep)
  165. {
  166. RT_ASSERT(ep != RT_NULL);
  167. RT_ASSERT(ep->ep_desc != RT_NULL);
  168. USB_DeviceDeinitEndpoint(ehci0_handle, ep->ep_desc->bEndpointAddress);
  169. return RT_EOK;
  170. }
  171. static rt_size_t _ehci0_ep_read(rt_uint8_t address, void *buffer)
  172. {
  173. rt_size_t size = 0;
  174. RT_ASSERT(buffer != RT_NULL);
  175. return size;
  176. }
  177. static rt_size_t _ehci0_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  178. {
  179. USB_DeviceRecvRequest(ehci0_handle, address, buffer, size);
  180. return size;
  181. }
  182. static rt_size_t _ehci0_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  183. {
  184. USB_DeviceSendRequest(ehci0_handle, address, buffer, size);
  185. return size;
  186. }
  187. static rt_err_t _ehci0_ep0_send_status(void)
  188. {
  189. _ehci0_ep_write(0x00, NULL, 0);
  190. return RT_EOK;
  191. }
  192. static rt_err_t _ehci0_suspend(void)
  193. {
  194. return RT_EOK;
  195. }
  196. static rt_err_t _ehci0_wakeup(void)
  197. {
  198. return RT_EOK;
  199. }
  200. const static struct udcd_ops _ehci0_udc_ops =
  201. {
  202. _ehci0_set_address,
  203. _ehci0_set_config,
  204. _ehci0_ep_set_stall,
  205. _ehci0_ep_clear_stall,
  206. _ehci0_ep_enable,
  207. _ehci0_ep_disable,
  208. _ehci0_ep_read_prepare,
  209. _ehci0_ep_read,
  210. _ehci0_ep_write,
  211. _ehci0_ep0_send_status,
  212. _ehci0_suspend,
  213. _ehci0_wakeup,
  214. };
  215. static rt_err_t drv_ehci0_usbd_init(rt_device_t device)
  216. {
  217. usb_status_t result;
  218. USB_DeviceClockInit(kUSB_ControllerEhci0);
  219. result = USB_DeviceInit(kUSB_ControllerEhci0,usb_device_callback,&ehci0_handle);
  220. RT_ASSERT(ehci0_handle);
  221. if(result == kStatus_USB_Success)
  222. {
  223. USB_DeviceIsrEnable(kUSB_ControllerEhci0);
  224. USB_DeviceRun(ehci0_handle);
  225. }
  226. else
  227. {
  228. rt_kprintf("USB_DeviceInit ehci0 error\r\n");
  229. return RT_ERROR;
  230. }
  231. return RT_EOK;
  232. }
  233. #endif
  234. #ifdef RT_USING_EHCI1_AS_DEVICE
  235. #ifdef FSL_USB_HS
  236. static struct ep_id _ehci1_ep_pool[] =
  237. {
  238. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  239. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  240. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  241. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  242. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  243. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  244. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  245. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  246. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  247. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  248. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  249. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  250. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  251. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  252. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  253. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  254. };
  255. #else
  256. static struct ep_id _ehci1_ep_pool[] =
  257. {
  258. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  259. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  260. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  261. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  262. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  263. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  264. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  265. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  266. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  267. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  268. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  269. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  270. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  271. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  272. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  273. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  274. };
  275. #endif
  276. void USB_OTG2_IRQHandler(void)
  277. {
  278. /* enter interrupt */
  279. rt_interrupt_enter();
  280. USB_DeviceEhciIsrFunction(ehci1_handle);
  281. /* leave interrupt */
  282. rt_interrupt_leave();
  283. }
  284. static rt_err_t _ehci1_ep_set_stall(rt_uint8_t address)
  285. {
  286. USB_DeviceStallEndpoint(ehci1_handle, address);
  287. return RT_EOK;
  288. }
  289. static rt_err_t _ehci1_ep_clear_stall(rt_uint8_t address)
  290. {
  291. USB_DeviceUnstallEndpoint(ehci1_handle, address);
  292. return RT_EOK;
  293. }
  294. static rt_err_t _ehci1_set_address(rt_uint8_t address)
  295. {
  296. USB_DeviceSetStatus(ehci1_handle, kUSB_DeviceStatusAddress, &address);
  297. return RT_EOK;
  298. }
  299. static rt_err_t _ehci1_set_config(rt_uint8_t address)
  300. {
  301. return RT_EOK;
  302. }
  303. static rt_err_t _ehci1_ep_enable(uep_t ep)
  304. {
  305. usb_device_endpoint_init_struct_t ep_init;
  306. usb_device_endpoint_callback_struct_t ep_callback;
  307. RT_ASSERT(ep != RT_NULL);
  308. RT_ASSERT(ep->ep_desc != RT_NULL);
  309. ep_init.maxPacketSize = ep->ep_desc->wMaxPacketSize;
  310. ep_init.endpointAddress = ep->ep_desc->bEndpointAddress;
  311. ep_init.transferType = ep->ep_desc->bmAttributes;
  312. ep_init.zlt = 0;
  313. ep_callback.callbackFn = usb_device_endpoint_callback;
  314. ep_callback.callbackParam = (void *)ep_init.endpointAddress;
  315. ep_callback.isBusy = 0;
  316. USB_DeviceInitEndpoint(ehci1_handle,&ep_init,&ep_callback);
  317. return RT_EOK;
  318. }
  319. static rt_err_t _ehci1_ep_disable(uep_t ep)
  320. {
  321. RT_ASSERT(ep != RT_NULL);
  322. RT_ASSERT(ep->ep_desc != RT_NULL);
  323. USB_DeviceDeinitEndpoint(ehci1_handle, ep->ep_desc->bEndpointAddress);
  324. return RT_EOK;
  325. }
  326. static rt_size_t _ehci1_ep_read(rt_uint8_t address, void *buffer)
  327. {
  328. rt_size_t size = 0;
  329. RT_ASSERT(buffer != RT_NULL);
  330. return size;
  331. }
  332. static rt_size_t _ehci1_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  333. {
  334. USB_DeviceRecvRequest(ehci1_handle, address, buffer, size);
  335. return size;
  336. }
  337. static rt_size_t _ehci1_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  338. {
  339. USB_DeviceSendRequest(ehci1_handle, address, buffer, size);
  340. return size;
  341. }
  342. static rt_err_t _ehci1_ep0_send_status(void)
  343. {
  344. _ehci1_ep_write(0x00, NULL, 0);
  345. return RT_EOK;
  346. }
  347. static rt_err_t _ehci1_suspend(void)
  348. {
  349. return RT_EOK;
  350. }
  351. static rt_err_t _ehci1_wakeup(void)
  352. {
  353. return RT_EOK;
  354. }
  355. const static struct udcd_ops _ehci1_udc_ops =
  356. {
  357. _ehci1_set_address,
  358. _ehci1_set_config,
  359. _ehci1_ep_set_stall,
  360. _ehci1_ep_clear_stall,
  361. _ehci1_ep_enable,
  362. _ehci1_ep_disable,
  363. _ehci1_ep_read_prepare,
  364. _ehci1_ep_read,
  365. _ehci1_ep_write,
  366. _ehci1_ep0_send_status,
  367. _ehci1_suspend,
  368. _ehci1_wakeup,
  369. };
  370. static rt_err_t drv_ehci1_usbd_init(rt_device_t device)
  371. {
  372. usb_status_t result;
  373. USB_DeviceClockInit(kUSB_ControllerEhci1);
  374. result = USB_DeviceInit(kUSB_ControllerEhci1,usb_device_callback,&ehci1_handle);
  375. RT_ASSERT(ehci1_handle);
  376. if(result == kStatus_USB_Success)
  377. {
  378. USB_DeviceIsrEnable(kUSB_ControllerEhci1);
  379. USB_DeviceRun(ehci1_handle);
  380. }
  381. else
  382. {
  383. rt_kprintf("USB_DeviceInit ehci1 error\r\n");
  384. return RT_ERROR;
  385. }
  386. return RT_EOK;
  387. }
  388. #endif
  389. #if defined(RT_USING_EHCI0_AS_DEVICE) && defined(RT_USING_EHCI1_AS_DEVICE)
  390. #error Can not using both now
  391. #endif
  392. static int rt_usbd_init(void)
  393. {
  394. #ifdef RT_USING_EHCI0_AS_DEVICE
  395. rt_memset((void *)&_fsl_udc_0, 0, sizeof(struct udcd));
  396. _fsl_udc_0.parent.type = RT_Device_Class_USBDevice;
  397. _fsl_udc_0.parent.init = drv_ehci0_usbd_init;
  398. _fsl_udc_0.ops = &_ehci0_udc_ops;
  399. /* Register endpoint infomation */
  400. _fsl_udc_0.ep_pool = _ehci0_ep_pool;
  401. _fsl_udc_0.ep0.id = &_ehci0_ep_pool[0];
  402. #ifdef FSL_USB_HS
  403. _fsl_udc_0.device_is_hs = RT_TRUE;
  404. #else
  405. _fsl_udc_0.device_is_hs = RT_FALSE;
  406. #endif
  407. rt_device_register((rt_device_t)&_fsl_udc_0, "usbd", 0);
  408. rt_usb_device_init();
  409. #endif
  410. #ifdef RT_USING_EHCI1_AS_DEVICE
  411. rt_memset((void *)&_fsl_udc_1, 0, sizeof(struct udcd));
  412. _fsl_udc_1.parent.type = RT_Device_Class_USBDevice;
  413. _fsl_udc_1.parent.init = drv_ehci1_usbd_init;
  414. _fsl_udc_1.ops = &_ehci1_udc_ops;
  415. /* Register endpoint infomation */
  416. _fsl_udc_1.ep_pool = _ehci1_ep_pool;
  417. _fsl_udc_1.ep0.id = &_ehci1_ep_pool[0];
  418. #ifdef FSL_USB_HS
  419. _fsl_udc_1.device_is_hs = RT_TRUE;
  420. #else
  421. _fsl_udc_1.device_is_hs = RT_FALSE;
  422. #endif
  423. rt_device_register((rt_device_t)&_fsl_udc_1, "usbd", 0);
  424. rt_usb_device_init();
  425. #endif
  426. return 0;
  427. }
  428. INIT_DEVICE_EXPORT(rt_usbd_init);
  429. static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam)
  430. {
  431. rt_uint32_t ep_addr = (rt_uint32_t)callbackParam;
  432. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  433. udcd_t udcd = RT_NULL;
  434. uint8_t state;
  435. if(deviceHandle->controllerId == kUSB_ControllerEhci0)
  436. {
  437. #ifdef RT_USING_EHCI0_AS_DEVICE
  438. udcd = &_fsl_udc_0;
  439. #endif
  440. }
  441. else
  442. {
  443. #ifdef RT_USING_EHCI1_AS_DEVICE
  444. udcd = &_fsl_udc_1;
  445. #endif
  446. }
  447. if(message->isSetup)
  448. {
  449. //rt_kprintf("1udcd:%#08X\n",udcd);
  450. rt_usbd_ep0_setup_handler(udcd, (struct urequest*)message->buffer);
  451. }
  452. else if(ep_addr == 0x00)
  453. {
  454. USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  455. if(state == kUSB_DeviceStateAddressing)
  456. {
  457. if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
  458. {
  459. state = kUSB_DeviceStateAddress;
  460. USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  461. }
  462. }
  463. //rt_kprintf("2udcd:%#08X\n",udcd);
  464. rt_usbd_ep0_out_handler(udcd,message->length);
  465. }
  466. else if(ep_addr == 0x80)
  467. {
  468. USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  469. if(state == kUSB_DeviceStateAddressing)
  470. {
  471. if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
  472. {
  473. state = kUSB_DeviceStateAddress;
  474. USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  475. }
  476. }
  477. //rt_kprintf("3udcd:%#08X\n",udcd);
  478. rt_usbd_ep0_in_handler(udcd);
  479. }
  480. else if(ep_addr&0x80)
  481. {
  482. rt_usbd_ep_in_handler(udcd, ep_addr, message->length);
  483. }
  484. else
  485. {
  486. rt_usbd_ep_out_handler(udcd, ep_addr, message->length);
  487. }
  488. return kStatus_USB_Success;
  489. }
  490. static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam)
  491. {
  492. usb_status_t error = kStatus_USB_Error;
  493. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  494. usb_device_endpoint_init_struct_t ep0_init =
  495. {
  496. 0x40,
  497. 0x00,
  498. USB_EP_ATTR_CONTROL,
  499. 0
  500. };
  501. usb_device_endpoint_callback_struct_t ep0_callback =
  502. {
  503. usb_device_endpoint_callback,
  504. 0,
  505. 0
  506. };
  507. udcd_t udcd = RT_NULL;
  508. if(deviceHandle->controllerId == kUSB_ControllerEhci0)
  509. {
  510. #ifdef RT_USING_EHCI0_AS_DEVICE
  511. udcd = &_fsl_udc_0;
  512. #endif
  513. }
  514. else
  515. {
  516. #ifdef RT_USING_EHCI1_AS_DEVICE
  517. udcd = &_fsl_udc_1;
  518. #endif
  519. }
  520. switch (callbackEvent)
  521. {
  522. case kUSB_DeviceEventBusReset:
  523. ep0_init.endpointAddress = 0x00;
  524. ep0_callback.callbackParam = (void *)0x00;
  525. USB_DeviceInitEndpoint(deviceHandle,&ep0_init,&ep0_callback);
  526. ep0_init.endpointAddress = 0x80;
  527. ep0_callback.callbackParam = (void *)0x80;
  528. USB_DeviceInitEndpoint(deviceHandle,&ep0_init,&ep0_callback);
  529. rt_usbd_reset_handler(udcd);
  530. break;
  531. case kUSB_DeviceEventAttach:
  532. rt_usbd_connect_handler(udcd);
  533. break;
  534. case kUSB_DeviceEventDetach:
  535. rt_usbd_disconnect_handler(udcd);
  536. break;
  537. }
  538. return error;
  539. }