drv_usbd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-12-04 ZYH first implementation
  9. */
  10. #include <usb/include/usb_device_config.h>
  11. #include <usb/include/usb.h>
  12. #include <rtthread.h>
  13. #include <usb/phy/usb_phy.h>
  14. #include <usb/device/usb_device.h>
  15. #include <usb/device/usb_device_dci.h>
  16. #include <rtdevice.h>
  17. /* USB PHY condfiguration */
  18. #define BOARD_USB_PHY_D_CAL (0x0CU)
  19. #define BOARD_USB_PHY_TXCAL45DP (0x06U)
  20. #define BOARD_USB_PHY_TXCAL45DM (0x06U)
  21. static usb_device_handle ehci0_handle;
  22. static struct udcd _fsl_udc_0;
  23. static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam);
  24. static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam);
  25. static void USB_DeviceIsrEnable(uint8_t controllerId)
  26. {
  27. uint8_t irqNumber;
  28. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  29. uint8_t usbDeviceEhciIrq[] = USBHS_IRQS;
  30. irqNumber = usbDeviceEhciIrq[controllerId - kUSB_ControllerEhci0];
  31. #endif
  32. /* Install isr, set priority, and enable IRQ. */
  33. #if defined(__GIC_PRIO_BITS)
  34. GIC_SetPriority((IRQn_Type)irqNumber, 3);
  35. #else
  36. NVIC_SetPriority((IRQn_Type)irqNumber, 3);
  37. #endif
  38. EnableIRQ((IRQn_Type)irqNumber);
  39. }
  40. /*!
  41. * @brief Initializes USB specific setting that was not set by the Clocks tool.
  42. */
  43. static void USB_DeviceClockInit(uint8_t controllerId)
  44. {
  45. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  46. usb_phy_config_struct_t phyConfig = {
  47. BOARD_USB_PHY_D_CAL, BOARD_USB_PHY_TXCAL45DP, BOARD_USB_PHY_TXCAL45DM,
  48. };
  49. #endif
  50. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  51. if (controllerId == kUSB_ControllerEhci0)
  52. {
  53. CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  54. CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);
  55. }
  56. else
  57. {
  58. CLOCK_EnableUsbhs1PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  59. CLOCK_EnableUsbhs1Clock(kCLOCK_Usb480M, 480000000U);
  60. }
  61. USB_EhciPhyInit(controllerId, 0, &phyConfig);
  62. #endif
  63. }
  64. static struct ep_id _ehci0_ep_pool[] =
  65. {
  66. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  67. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  68. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  69. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  70. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  71. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  72. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  73. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  74. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  75. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  76. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  77. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  78. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  79. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  80. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  81. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  82. };
  83. /*!
  84. * @brief USB Interrupt service routine.
  85. *
  86. * This function serves as the USB interrupt service routine.
  87. *
  88. * @return None.
  89. */
  90. void USB_OTG1_IRQHandler(void)
  91. {
  92. /* enter interrupt */
  93. rt_interrupt_enter();
  94. USB_DeviceEhciIsrFunction(ehci0_handle);
  95. /* leave interrupt */
  96. rt_interrupt_leave();
  97. }
  98. static rt_err_t _ehci0_ep_set_stall(rt_uint8_t address)
  99. {
  100. USB_DeviceStallEndpoint(ehci0_handle, address);
  101. return RT_EOK;
  102. }
  103. static rt_err_t _ehci0_ep_clear_stall(rt_uint8_t address)
  104. {
  105. USB_DeviceUnstallEndpoint(ehci0_handle, address);
  106. return RT_EOK;
  107. }
  108. static rt_err_t _ehci0_set_address(rt_uint8_t address)
  109. {
  110. USB_DeviceSetStatus(ehci0_handle, kUSB_DeviceStatusAddress, &address);
  111. return RT_EOK;
  112. }
  113. static rt_err_t _ehci0_set_config(rt_uint8_t address)
  114. {
  115. return RT_EOK;
  116. }
  117. static rt_err_t _ehci0_ep_enable(uep_t ep)
  118. {
  119. usb_device_endpoint_init_struct_t ep_init;
  120. usb_device_endpoint_callback_struct_t ep_callback;
  121. rt_uint32_t param = ep->ep_desc->bEndpointAddress;
  122. RT_ASSERT(ep != RT_NULL);
  123. RT_ASSERT(ep->ep_desc != RT_NULL);
  124. ep_init.maxPacketSize = ep->ep_desc->wMaxPacketSize;
  125. ep_init.endpointAddress = ep->ep_desc->bEndpointAddress;
  126. ep_init.transferType = ep->ep_desc->bmAttributes;
  127. ep_init.zlt = 0;
  128. ep_callback.callbackFn = usb_device_endpoint_callback;
  129. ep_callback.callbackParam = (void *)param;
  130. ep_callback.isBusy = 0;
  131. USB_DeviceInitEndpoint(ehci0_handle, &ep_init, &ep_callback);
  132. return RT_EOK;
  133. }
  134. static rt_err_t _ehci0_ep_disable(uep_t ep)
  135. {
  136. RT_ASSERT(ep != RT_NULL);
  137. RT_ASSERT(ep->ep_desc != RT_NULL);
  138. USB_DeviceDeinitEndpoint(ehci0_handle, ep->ep_desc->bEndpointAddress);
  139. return RT_EOK;
  140. }
  141. static rt_size_t _ehci0_ep_read(rt_uint8_t address, void *buffer)
  142. {
  143. rt_size_t size = 0;
  144. RT_ASSERT(buffer != RT_NULL);
  145. return size;
  146. }
  147. static rt_size_t _ehci0_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  148. {
  149. USB_DeviceRecvRequest(ehci0_handle, address, buffer, size);
  150. return size;
  151. }
  152. static rt_size_t _ehci0_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  153. {
  154. USB_DeviceSendRequest(ehci0_handle, address, buffer, size);
  155. return size;
  156. }
  157. static rt_err_t _ehci0_ep0_send_status(void)
  158. {
  159. _ehci0_ep_write(0x00, NULL, 0);
  160. return RT_EOK;
  161. }
  162. static rt_err_t _ehci0_suspend(void)
  163. {
  164. return RT_EOK;
  165. }
  166. static rt_err_t _ehci0_wakeup(void)
  167. {
  168. return RT_EOK;
  169. }
  170. const static struct udcd_ops _ehci0_udc_ops =
  171. {
  172. _ehci0_set_address,
  173. _ehci0_set_config,
  174. _ehci0_ep_set_stall,
  175. _ehci0_ep_clear_stall,
  176. _ehci0_ep_enable,
  177. _ehci0_ep_disable,
  178. _ehci0_ep_read_prepare,
  179. _ehci0_ep_read,
  180. _ehci0_ep_write,
  181. _ehci0_ep0_send_status,
  182. _ehci0_suspend,
  183. _ehci0_wakeup,
  184. };
  185. static rt_err_t drv_ehci0_usbd_init(rt_device_t device)
  186. {
  187. usb_status_t result;
  188. USB_DeviceClockInit(kUSB_ControllerEhci0);
  189. result = USB_DeviceInit(kUSB_ControllerEhci0, usb_device_callback, &ehci0_handle);
  190. RT_ASSERT(ehci0_handle);
  191. if(result == kStatus_USB_Success)
  192. {
  193. USB_DeviceIsrEnable(kUSB_ControllerEhci0);
  194. USB_DeviceRun(ehci0_handle);
  195. }
  196. else
  197. {
  198. rt_kprintf("USB_DeviceInit ehci0 error\r\n");
  199. return RT_ERROR;
  200. }
  201. return RT_EOK;
  202. }
  203. static int rt_usbd_init(void)
  204. {
  205. rt_memset((void *)&_fsl_udc_0, 0, sizeof(struct udcd));
  206. _fsl_udc_0.parent.type = RT_Device_Class_USBDevice;
  207. _fsl_udc_0.parent.init = drv_ehci0_usbd_init;
  208. _fsl_udc_0.ops = &_ehci0_udc_ops;
  209. /* Register endpoint infomation */
  210. _fsl_udc_0.ep_pool = _ehci0_ep_pool;
  211. _fsl_udc_0.ep0.id = &_ehci0_ep_pool[0];
  212. _fsl_udc_0.device_is_hs = RT_FALSE;
  213. rt_device_register((rt_device_t)&_fsl_udc_0, "usbd", 0);
  214. rt_usb_device_init();
  215. return 0;
  216. }
  217. INIT_DEVICE_EXPORT(rt_usbd_init);
  218. static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam)
  219. {
  220. rt_uint32_t ep_addr = (rt_uint32_t)callbackParam;
  221. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  222. udcd_t udcd = RT_NULL;
  223. uint8_t state;
  224. if(deviceHandle->controllerId == kUSB_ControllerEhci0)
  225. udcd = &_fsl_udc_0;
  226. if(message->isSetup)
  227. {
  228. rt_usbd_ep0_setup_handler(udcd, (struct urequest*)message->buffer);
  229. }
  230. else if(ep_addr == 0x00)
  231. {
  232. USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  233. if(state == kUSB_DeviceStateAddressing)
  234. {
  235. if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
  236. {
  237. state = kUSB_DeviceStateAddress;
  238. USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  239. }
  240. }
  241. rt_usbd_ep0_out_handler(udcd, message->length);
  242. }
  243. else if(ep_addr == 0x80)
  244. {
  245. USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  246. if(state == kUSB_DeviceStateAddressing)
  247. {
  248. if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
  249. {
  250. state = kUSB_DeviceStateAddress;
  251. USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  252. }
  253. }
  254. rt_usbd_ep0_in_handler(udcd);
  255. }
  256. else if(ep_addr & 0x80)
  257. {
  258. rt_usbd_ep_in_handler(udcd, ep_addr, message->length);
  259. }
  260. else
  261. {
  262. rt_usbd_ep_out_handler(udcd, ep_addr, message->length);
  263. }
  264. return kStatus_USB_Success;
  265. }
  266. static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam)
  267. {
  268. usb_status_t error = kStatus_USB_Error;
  269. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  270. usb_device_endpoint_init_struct_t ep0_init =
  271. {
  272. 0x40,
  273. 0x00,
  274. USB_EP_ATTR_CONTROL,
  275. 0
  276. };
  277. usb_device_endpoint_callback_struct_t ep0_callback =
  278. {
  279. usb_device_endpoint_callback,
  280. 0,
  281. 0
  282. };
  283. udcd_t udcd = RT_NULL;
  284. if(deviceHandle->controllerId == kUSB_ControllerEhci0)
  285. udcd = &_fsl_udc_0;
  286. switch (callbackEvent)
  287. {
  288. case kUSB_DeviceEventBusReset:
  289. ep0_init.endpointAddress = 0x00;
  290. ep0_callback.callbackParam = (void *)0x00;
  291. USB_DeviceInitEndpoint(deviceHandle, &ep0_init, &ep0_callback);
  292. ep0_init.endpointAddress = 0x80;
  293. ep0_callback.callbackParam = (void *)0x80;
  294. USB_DeviceInitEndpoint(deviceHandle, &ep0_init, &ep0_callback);
  295. rt_usbd_reset_handler(udcd);
  296. break;
  297. case kUSB_DeviceEventAttach:
  298. rt_usbd_connect_handler(udcd);
  299. break;
  300. case kUSB_DeviceEventDetach:
  301. rt_usbd_disconnect_handler(udcd);
  302. break;
  303. }
  304. return error;
  305. }
  306. /********************* end of file ************************/