drv_usbfsh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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_HOST_USBFS1) || defined(BSP_USING_HOST_USBFS2)
  14. #include "usbh_int.h"
  15. #include "drv_usbfs.h"
  16. #include "drv_config.h"
  17. //#define DRV_DEBUG
  18. #define LOG_TAG "drv.usb"
  19. #include <drv_log.h>
  20. static struct rt_completion urb_completion;
  21. static volatile rt_bool_t connect_status = RT_FALSE;
  22. static struct at32_usbfs *p_usbfs_instance = RT_NULL;
  23. enum
  24. {
  25. #ifdef BSP_USING_HOST_USBFS1
  26. USBFS1_INDEX,
  27. #endif
  28. #ifdef BSP_USING_HOST_USBFS2
  29. USBFS2_INDEX,
  30. #endif
  31. };
  32. static struct at32_usbfs usbfsh_config[] = {
  33. #ifdef BSP_USING_HOST_USBFS1
  34. USBFS1_CONFIG,
  35. #endif
  36. #ifdef BSP_USING_HOST_USBFS2
  37. USBFS2_CONFIG,
  38. #endif
  39. };
  40. #ifdef BSP_USING_HOST_USBFS1
  41. void OTGFS1_IRQHandler(void)
  42. {
  43. /* enter interrupt */
  44. rt_interrupt_enter();
  45. usbh_irq_handler(p_usbfs_instance->p_otg_core);
  46. /* leave interrupt */
  47. rt_interrupt_leave();
  48. }
  49. #endif
  50. #ifdef BSP_USING_HOST_USBFS2
  51. void OTGFS2_IRQHandler(void)
  52. {
  53. /* enter interrupt */
  54. rt_interrupt_enter();
  55. usbh_irq_handler(p_usbfs_instance->p_otg_core);
  56. /* leave interrupt */
  57. rt_interrupt_leave();
  58. }
  59. #endif
  60. void usbh_connect_callback(usbh_core_type *uhost)
  61. {
  62. uhcd_t hcd = (uhcd_t)uhost->pdata;
  63. if (!connect_status)
  64. {
  65. connect_status = RT_TRUE;
  66. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb connected\n"));
  67. rt_usbh_root_hub_connect_handler(hcd, 1, RT_FALSE);
  68. }
  69. }
  70. void usbh_disconnect_callback(usbh_core_type *uhost)
  71. {
  72. uhcd_t hcd = (uhcd_t)uhost->pdata;
  73. if (connect_status)
  74. {
  75. connect_status = RT_FALSE;
  76. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb disconnnect\n"));
  77. rt_usbh_root_hub_disconnect_handler(hcd, 1);
  78. }
  79. }
  80. void usbd_notify_urbchange_callback(usbh_core_type *uhost, uint8_t chnum, urb_sts_type sts)
  81. {
  82. rt_completion_done(&urb_completion);
  83. }
  84. static rt_err_t drv_reset_port(rt_uint8_t port)
  85. {
  86. RT_DEBUG_LOG(RT_DEBUG_USB, ("reset port\n"));
  87. usbh_reset_port(&p_usbfs_instance->p_otg_core->host);
  88. return RT_EOK;
  89. }
  90. static int drv_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeouts)
  91. {
  92. int timeout = timeouts;
  93. while(1)
  94. {
  95. if(!connect_status)
  96. {
  97. return -1;
  98. }
  99. rt_completion_init(&urb_completion);
  100. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].dir = (pipe->ep.bEndpointAddress & 0x80) >> 7;
  101. if(token == 0U)
  102. {
  103. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_SETUP;
  104. }
  105. else
  106. {
  107. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA1;
  108. }
  109. /* endpoint type */
  110. switch(pipe->ep.bmAttributes)
  111. {
  112. /* endpoint is control type */
  113. case EPT_CONTROL_TYPE:
  114. if((token == 1U) && (((pipe->ep.bEndpointAddress & 0x80) >> 7) == 0U))
  115. {
  116. if(nbytes == 0U)
  117. {
  118. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].toggle_out = 1U;
  119. }
  120. if((&p_usbfs_instance->p_otg_core->host)->hch[pipe->pipe_index].toggle_out == 0U)
  121. {
  122. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA0;
  123. }
  124. else
  125. {
  126. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA1;
  127. }
  128. }
  129. break;
  130. /* endpoint is bulk type */
  131. case EPT_BULK_TYPE:
  132. if(((pipe->ep.bEndpointAddress & 0x80) >> 7) == 0U)
  133. {
  134. if( p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].toggle_out == 0U)
  135. {
  136. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA0;
  137. }
  138. else
  139. {
  140. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA1;
  141. }
  142. }
  143. else
  144. {
  145. if( p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].toggle_in == 0U)
  146. {
  147. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA0;
  148. }
  149. else
  150. {
  151. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA1;
  152. }
  153. }
  154. break;
  155. /* endpoint is int type */
  156. case EPT_INT_TYPE:
  157. if(((pipe->ep.bEndpointAddress & 0x80) >> 7) == 0U)
  158. {
  159. if( p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].toggle_out == 0U)
  160. {
  161. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA0;
  162. }
  163. else
  164. {
  165. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA1;
  166. }
  167. }
  168. else
  169. {
  170. if( p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].toggle_in == 0U)
  171. {
  172. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA0;
  173. }
  174. else
  175. {
  176. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA1;
  177. }
  178. }
  179. break;
  180. /* endpoint is isoc type */
  181. case EPT_ISO_TYPE:
  182. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].data_pid = HCH_PID_DATA0;
  183. break;
  184. default:
  185. break;
  186. }
  187. /* set transfer buffer */
  188. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].trans_buf = buffer;
  189. /* set transfer len*/
  190. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].trans_len = nbytes;
  191. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].urb_sts = URB_IDLE;
  192. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].ch_num = pipe->pipe_index;
  193. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].trans_count = 0;
  194. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].state = HCH_IDLE;
  195. /* data in/out for host */
  196. usbh_in_out_request((&p_usbfs_instance->p_otg_core->host), pipe->pipe_index);
  197. rt_completion_wait(&urb_completion, timeout);
  198. rt_thread_mdelay(1);
  199. if(usbh_get_status((&p_usbfs_instance->p_otg_core->host), pipe->pipe_index) == HCH_NAK)
  200. {
  201. RT_DEBUG_LOG(RT_DEBUG_USB, ("nak\n"));
  202. if (pipe->ep.bmAttributes == USB_EP_ATTR_INT)
  203. {
  204. rt_thread_delay((pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) > 0 ? (pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) : 1);
  205. }
  206. usb_hch_halt((&p_usbfs_instance->p_otg_core->host)->usb_reg, pipe->pipe_index);
  207. usbh_hc_open(&p_usbfs_instance->p_otg_core->host,
  208. pipe->pipe_index,
  209. pipe->ep.bEndpointAddress,
  210. pipe->inst->address,
  211. pipe->ep.bmAttributes,
  212. pipe->ep.wMaxPacketSize,
  213. USB_PRTSPD_FULL_SPEED);
  214. continue;
  215. }
  216. else if (usbh_get_status(&p_usbfs_instance->p_otg_core->host, pipe->pipe_index) == HCH_STALL)
  217. {
  218. RT_DEBUG_LOG(RT_DEBUG_USB, ("stall\n"));
  219. pipe->status = UPIPE_STATUS_STALL;
  220. if (pipe->callback != RT_NULL)
  221. {
  222. pipe->callback(pipe);
  223. }
  224. return -1;
  225. }
  226. else if (usbh_get_status(&p_usbfs_instance->p_otg_core->host, pipe->pipe_index) == URB_ERROR)
  227. {
  228. RT_DEBUG_LOG(RT_DEBUG_USB, ("error\n"));
  229. pipe->status = UPIPE_STATUS_ERROR;
  230. if (pipe->callback != RT_NULL)
  231. {
  232. pipe->callback(pipe);
  233. }
  234. return -1;
  235. }
  236. else if(URB_DONE == usbh_get_urb_status(&p_usbfs_instance->p_otg_core->host, pipe->pipe_index))
  237. {
  238. RT_DEBUG_LOG(RT_DEBUG_USB, ("ok\n"));
  239. pipe->status = UPIPE_STATUS_OK;
  240. if (pipe->callback != RT_NULL)
  241. {
  242. pipe->callback(pipe);
  243. }
  244. size_t size = (&p_usbfs_instance->p_otg_core->host)->hch[pipe->pipe_index].trans_count;
  245. if (pipe->ep.bEndpointAddress & 0x80)
  246. {
  247. return size;
  248. }
  249. else if (pipe->ep.bEndpointAddress & 0x00)
  250. {
  251. return size;
  252. }
  253. return nbytes;
  254. }
  255. continue;
  256. }
  257. }
  258. static rt_uint16_t pipe_index = 0;
  259. static rt_uint8_t drv_get_free_pipe_index(void)
  260. {
  261. rt_uint8_t idx;
  262. for (idx = 1; idx < 16; idx++)
  263. {
  264. if (!(pipe_index & (0x01 << idx)))
  265. {
  266. pipe_index |= (0x01 << idx);
  267. return idx;
  268. }
  269. }
  270. return 0xff;
  271. }
  272. static void drv_free_pipe_index(rt_uint8_t index)
  273. {
  274. pipe_index &= ~(0x01 << index);
  275. }
  276. static rt_err_t drv_open_pipe(upipe_t pipe)
  277. {
  278. pipe->pipe_index = drv_get_free_pipe_index();
  279. usbh_hc_open(&p_usbfs_instance->p_otg_core->host,
  280. pipe->pipe_index,
  281. pipe->ep.bEndpointAddress,
  282. pipe->inst->address,
  283. pipe->ep.bmAttributes,
  284. pipe->ep.wMaxPacketSize,
  285. USB_PRTSPD_FULL_SPEED);
  286. /* set data0 pid token*/
  287. if (p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].dir)
  288. {
  289. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].toggle_in = 0;
  290. }
  291. else
  292. {
  293. p_usbfs_instance->p_otg_core->host.hch[pipe->pipe_index].toggle_out = 0;
  294. }
  295. return RT_EOK;
  296. }
  297. static rt_err_t drv_close_pipe(upipe_t pipe)
  298. {
  299. usb_hch_halt((&p_usbfs_instance->p_otg_core->host)->usb_reg, pipe->pipe_index);
  300. drv_free_pipe_index(pipe->pipe_index);
  301. return RT_EOK;
  302. }
  303. static struct uhcd_ops _uhcd_ops =
  304. {
  305. drv_reset_port,
  306. drv_pipe_xfer,
  307. drv_open_pipe,
  308. drv_close_pipe,
  309. };
  310. static rt_err_t at32_hcd_init(rt_device_t device)
  311. {
  312. /* usb gpio config */
  313. at32_msp_usb_init(device);
  314. /* enable otgfs irq */
  315. nvic_irq_enable(p_usbfs_instance->irqn, 2, 0);
  316. /* init usb */
  317. usbh_init(p_usbfs_instance->p_otg_core,
  318. USB_FULL_SPEED_CORE_ID,
  319. p_usbfs_instance->id);
  320. return RT_EOK;
  321. }
  322. int at32_usbh_register(void)
  323. {
  324. rt_size_t obj_num;
  325. rt_err_t result = 0;
  326. int index;
  327. obj_num = sizeof(usbfsh_config) / sizeof(struct at32_usbfs);
  328. for (index = 0; index < obj_num; index++) {
  329. uhcd_t uhcd = (uhcd_t)rt_malloc(sizeof(struct uhcd));
  330. if (uhcd == RT_NULL)
  331. {
  332. rt_kprintf("uhcd malloc failed\r\n");
  333. return -RT_ERROR;
  334. }
  335. rt_memset((void *)uhcd, 0, sizeof(struct uhcd));
  336. otg_core_type *p_otg_core = (otg_core_type *)rt_malloc(sizeof(otg_core_type));
  337. if (p_otg_core == RT_NULL)
  338. {
  339. rt_kprintf("otg_core malloc failed\r\n");
  340. return -RT_ERROR;
  341. }
  342. rt_memset((void *)p_otg_core, 0, sizeof(otg_core_type));
  343. uhcd->parent.type = RT_Device_Class_USBHost;
  344. uhcd->parent.init = at32_hcd_init;
  345. uhcd->parent.user_data = &(p_otg_core->host);
  346. uhcd->ops = &_uhcd_ops;
  347. uhcd->num_ports = 1;
  348. p_otg_core->host.pdata = uhcd;
  349. usbfsh_config[index].p_otg_core = p_otg_core;
  350. result = rt_device_register(&uhcd->parent, usbfsh_config[index].name, RT_DEVICE_FLAG_DEACTIVATE);
  351. RT_ASSERT(result == RT_EOK);
  352. p_usbfs_instance = &usbfsh_config[index];
  353. result = rt_usb_host_init(usbfsh_config[index].name);
  354. RT_ASSERT(result == RT_EOK);
  355. }
  356. return result;
  357. }
  358. INIT_DEVICE_EXPORT(at32_usbh_register);
  359. #endif