drv_usbotgh.c 13 KB

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