drv_usbotgh.c 11 KB

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