drv_usbd.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-28 leo first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "drv_common.h"
  13. #if defined(BSP_USING_USBD)
  14. #include "usbd_int.h"
  15. #include "drv_usbd.h"
  16. #include "drv_config.h"
  17. static struct at32_usbd *p_usbd_instance = RT_NULL;
  18. static struct ep_id endpoint_pool[] =
  19. {
  20. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  21. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  22. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  23. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  24. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  25. {0x3, USB_EP_ATTR_ISOC, USB_DIR_IN, 64, ID_UNASSIGNED},
  26. {0x3, USB_EP_ATTR_ISOC, USB_DIR_OUT, 64, ID_UNASSIGNED},
  27. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  28. };
  29. enum
  30. {
  31. #ifdef BSP_USING_USBD
  32. USBD1_INDEX,
  33. #endif
  34. };
  35. static struct at32_usbd usbd_config[] = {
  36. #ifdef BSP_USING_USBD
  37. USBD_CONFIG,
  38. #endif
  39. };
  40. void USB_IRQHandler(void)
  41. {
  42. /* enter interrupt */
  43. rt_interrupt_enter();
  44. usbd_irq_handler(p_usbd_instance->p_usbd_core);
  45. /* leave interrupt */
  46. rt_interrupt_leave();
  47. }
  48. void usbd_reset_callback(usbd_core_type *udev)
  49. {
  50. udcd_t udcd = (udcd_t)udev->pdata;
  51. rt_usbd_reset_handler(udcd);
  52. }
  53. void usbd_connectCallback(usbd_core_type *udev)
  54. {
  55. udcd_t udcd = (udcd_t)udev->pdata;
  56. rt_usbd_connect_handler(udcd);
  57. }
  58. void usbd_disconnectCallback(usbd_core_type *udev)
  59. {
  60. udcd_t udcd = (udcd_t)udev->pdata;
  61. rt_usbd_disconnect_handler(udcd);
  62. }
  63. void usbd_setup_phase_done_callback(usbd_core_type *udev)
  64. {
  65. udcd_t udcd = (udcd_t)udev->pdata;
  66. rt_usbd_ep0_setup_handler(udcd, (struct urequest*)udev->setup_buffer);
  67. }
  68. void usbd_data_in_stage_callback(usbd_core_type *udev, uint32_t ept_num)
  69. {
  70. udcd_t udcd = (udcd_t)udev->pdata;
  71. if (ept_num == 0)
  72. {
  73. rt_usbd_ep0_in_handler(udcd);
  74. }
  75. else
  76. {
  77. rt_usbd_ep_in_handler(udcd, 0x80 | ept_num, udev->ept_in[ept_num].trans_len);
  78. }
  79. }
  80. void usbd_sof_callback(usbd_core_type *udev)
  81. {
  82. udcd_t udcd = (udcd_t)udev->pdata;
  83. rt_usbd_sof_handler(udcd);
  84. }
  85. void usbd_data_out_stage_callback(usbd_core_type *udev, uint32_t ept_num)
  86. {
  87. udcd_t udcd = (udcd_t)udev->pdata;
  88. if (ept_num != 0)
  89. {
  90. rt_usbd_ep_out_handler(udcd, ept_num, udev->ept_out[ept_num].trans_len);
  91. }
  92. else
  93. {
  94. rt_usbd_ep0_out_handler(udcd, udev->ept_out[0].trans_len);
  95. }
  96. }
  97. static rt_err_t _ep_set_stall(rt_uint8_t address)
  98. {
  99. usbd_set_stall(p_usbd_instance->p_usbd_core, address);
  100. return RT_EOK;
  101. }
  102. static rt_err_t _ep_clear_stall(rt_uint8_t address)
  103. {
  104. usbd_clear_stall(p_usbd_instance->p_usbd_core, address);
  105. return RT_EOK;
  106. }
  107. static rt_err_t _set_address(rt_uint8_t address)
  108. {
  109. usbd_set_device_addr(p_usbd_instance->p_usbd_core, address);
  110. return RT_EOK;
  111. }
  112. static rt_err_t _set_config(rt_uint8_t address)
  113. {
  114. return RT_EOK;
  115. }
  116. static rt_err_t _ep_enable(uep_t ep)
  117. {
  118. RT_ASSERT(ep != RT_NULL);
  119. RT_ASSERT(ep->ep_desc != RT_NULL);
  120. usbd_ept_open(p_usbd_instance->p_usbd_core, ep->ep_desc->bEndpointAddress, ep->ep_desc->bmAttributes, ep->ep_desc->wMaxPacketSize);
  121. return RT_EOK;
  122. }
  123. static rt_err_t _ep_disable(uep_t ep)
  124. {
  125. RT_ASSERT(ep != RT_NULL);
  126. RT_ASSERT(ep->ep_desc != RT_NULL);
  127. usbd_ept_close(p_usbd_instance->p_usbd_core, ep->ep_desc->bEndpointAddress);
  128. return RT_EOK;
  129. }
  130. static rt_size_t _ep_read(rt_uint8_t address, void *buffer)
  131. {
  132. rt_size_t size = 0;
  133. RT_ASSERT(buffer != RT_NULL);
  134. return size;
  135. }
  136. static rt_size_t _ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  137. {
  138. usbd_ept_recv(p_usbd_instance->p_usbd_core, address, buffer, size);
  139. return size;
  140. }
  141. static rt_size_t _ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  142. {
  143. usbd_ept_send(p_usbd_instance->p_usbd_core, address, buffer, size);
  144. return size;
  145. }
  146. static rt_err_t _ep0_send_status(void)
  147. {
  148. usbd_ctrl_send_status(p_usbd_instance->p_usbd_core);
  149. return RT_EOK;
  150. }
  151. static rt_err_t _suspend(void)
  152. {
  153. return RT_EOK;
  154. }
  155. static rt_err_t _wakeup(void)
  156. {
  157. return RT_EOK;
  158. }
  159. static rt_err_t at32_dcd_init(rt_device_t device)
  160. {
  161. /* usb gpio config */
  162. at32_msp_usb_init(device);
  163. /* enable usb interrupt */
  164. nvic_irq_enable(p_usbd_instance->irqn, 2, 0);
  165. /* usb core init */
  166. usbd_core_init(p_usbd_instance->p_usbd_core, USB, 0);
  167. /* enable usb pull-up */
  168. usbd_connect(p_usbd_instance->p_usbd_core);
  169. return RT_EOK;
  170. }
  171. const static struct udcd_ops _udc_ops =
  172. {
  173. _set_address,
  174. _set_config,
  175. _ep_set_stall,
  176. _ep_clear_stall,
  177. _ep_enable,
  178. _ep_disable,
  179. _ep_read_prepare,
  180. _ep_read,
  181. _ep_write,
  182. _ep0_send_status,
  183. _suspend,
  184. _wakeup,
  185. };
  186. int at32_usbd_register(void)
  187. {
  188. rt_size_t obj_num;
  189. rt_err_t result = 0;
  190. int index;
  191. obj_num = sizeof(usbd_config) / sizeof(struct at32_usbd);
  192. for (index = 0; index < obj_num; index++) {
  193. udcd_t udcd = (udcd_t)rt_malloc(sizeof(struct udcd));
  194. if (udcd == RT_NULL)
  195. {
  196. rt_kprintf("udcd malloc failed\r\n");
  197. return -RT_ERROR;
  198. }
  199. rt_memset((void *)udcd, 0, sizeof(struct udcd));
  200. usbd_core_type *p_usbd_core = (usbd_core_type *)rt_malloc(sizeof(usbd_core_type));
  201. if (p_usbd_core == RT_NULL)
  202. {
  203. rt_kprintf("usbd_core malloc failed\r\n");
  204. return -RT_ERROR;
  205. }
  206. rt_memset((void *)p_usbd_core, 0, sizeof(usbd_core_type));
  207. udcd->parent.type = RT_Device_Class_USBDevice;
  208. udcd->parent.init = at32_dcd_init;
  209. udcd->parent.user_data = p_usbd_core;
  210. udcd->ops = &_udc_ops;
  211. p_usbd_core->pdata = udcd;
  212. usbd_config[index].p_usbd_core = p_usbd_core;
  213. /* register endpoint infomation */
  214. udcd->ep_pool = endpoint_pool;
  215. udcd->ep0.id = &endpoint_pool[0];
  216. result = rt_device_register((rt_device_t)udcd, usbd_config[index].name, 0);
  217. RT_ASSERT(result == RT_EOK);
  218. p_usbd_instance = &usbd_config[index];
  219. result = rt_usb_device_init();
  220. RT_ASSERT(result == RT_EOK);
  221. }
  222. return result;
  223. }
  224. INIT_DEVICE_EXPORT(at32_usbd_register);
  225. #endif