drv_usbh.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-10-30 ZYH the first version
  9. */
  10. #include "drv_usbh.h"
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #define OTG_FS_PORT 1
  15. static HCD_HandleTypeDef _stm_hhcd_fs;
  16. static struct rt_completion urb_completion;
  17. void HAL_HCD_MspInit(HCD_HandleTypeDef *hcdHandle)
  18. {
  19. GPIO_InitTypeDef GPIO_InitStruct;
  20. if (hcdHandle->Instance == USB_OTG_FS)
  21. {
  22. /**USB_OTG_FS GPIO Configuration
  23. PA9 ------> USB_OTG_FS_VBUS
  24. PA10 ------> USB_OTG_FS_ID
  25. PA11 ------> USB_OTG_FS_DM
  26. PA12 ------> USB_OTG_FS_DP
  27. */
  28. __HAL_RCC_GPIOA_CLK_ENABLE();
  29. #ifdef USBH_USING_VBUS
  30. GPIO_InitStruct.Pin = GPIO_PIN_9;
  31. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  32. GPIO_InitStruct.Pull = GPIO_NOPULL;
  33. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  34. #endif
  35. GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_11;
  36. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  37. GPIO_InitStruct.Pull = GPIO_NOPULL;
  38. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  39. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
  40. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  41. /* Peripheral clock enable */
  42. __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
  43. /* Peripheral interrupt init */
  44. HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0);
  45. HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
  46. }
  47. }
  48. void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hcdHandle)
  49. {
  50. if (hcdHandle->Instance == USB_OTG_FS)
  51. {
  52. /* Peripheral clock disable */
  53. __HAL_RCC_USB_OTG_FS_CLK_DISABLE();
  54. /**USB_OTG_FS GPIO Configuration
  55. PA9 ------> USB_OTG_FS_VBUS
  56. PA10 ------> USB_OTG_FS_ID
  57. PA11 ------> USB_OTG_FS_DM
  58. PA12 ------> USB_OTG_FS_DP
  59. */
  60. #ifdef USBH_USING_VBUS
  61. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9);
  62. #endif
  63. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12 | GPIO_PIN_11);
  64. /* Peripheral interrupt Deinit*/
  65. HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
  66. }
  67. }
  68. void OTG_FS_IRQHandler(void)
  69. {
  70. rt_interrupt_enter();
  71. HAL_HCD_IRQHandler(&_stm_hhcd_fs);
  72. rt_interrupt_leave();
  73. }
  74. void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)//提供定时器
  75. {
  76. //rt_kprintf("sof callback\n");
  77. }
  78. static __IO rt_bool_t connect_status = RT_FALSE;
  79. void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
  80. {
  81. uhcd_t hcd = (uhcd_t)hhcd->pData;
  82. if (!connect_status)
  83. {
  84. connect_status = RT_TRUE;
  85. RT_DEBUG_LOG(RT_DEBUG_USB, ("connected\n"));
  86. rt_usbh_root_hub_connect_handler(hcd, OTG_FS_PORT, RT_FALSE);
  87. }
  88. }
  89. void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
  90. {
  91. uhcd_t hcd = (uhcd_t)hhcd->pData;
  92. if (connect_status)
  93. {
  94. connect_status = RT_FALSE;
  95. RT_DEBUG_LOG(RT_DEBUG_USB, ("disconnnect\n"));
  96. rt_usbh_root_hub_disconnect_handler(hcd, OTG_FS_PORT);
  97. }
  98. }
  99. void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
  100. {
  101. //rt_kprintf("NotifyURBChange_Callback\n");
  102. rt_completion_done(&urb_completion);
  103. }
  104. static rt_err_t drv_reset_port(rt_uint8_t port)
  105. {
  106. RT_DEBUG_LOG(RT_DEBUG_USB, ("reset port\n"));
  107. HAL_HCD_ResetPort(&_stm_hhcd_fs);
  108. return RT_EOK;
  109. }
  110. static int drv_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeout)
  111. {
  112. while (1)
  113. {
  114. if (!connect_status)
  115. {
  116. return -1;
  117. }
  118. rt_completion_init(&urb_completion);
  119. HAL_HCD_HC_SubmitRequest(&_stm_hhcd_fs,
  120. pipe->pipe_index,
  121. (pipe->ep.bEndpointAddress & 0x80) >> 7,
  122. pipe->ep.bmAttributes,
  123. token,
  124. buffer,
  125. nbytes,
  126. 0);
  127. rt_completion_wait(&urb_completion, timeout);
  128. if (HAL_HCD_HC_GetState(&_stm_hhcd_fs, pipe->pipe_index) == HC_NAK)
  129. {
  130. RT_DEBUG_LOG(RT_DEBUG_USB, ("nak\n"));
  131. if (pipe->ep.bmAttributes == USB_EP_ATTR_INT)
  132. {
  133. rt_thread_delay((pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) > 0 ? (pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) : 1);
  134. }
  135. HAL_HCD_HC_Halt(&_stm_hhcd_fs, pipe->pipe_index);
  136. HAL_HCD_HC_Init(&_stm_hhcd_fs,
  137. pipe->pipe_index,
  138. pipe->ep.bEndpointAddress,
  139. pipe->inst->address,
  140. USB_OTG_SPEED_FULL,
  141. pipe->ep.bmAttributes,
  142. pipe->ep.wMaxPacketSize);
  143. continue;
  144. }
  145. else if (HAL_HCD_HC_GetState(&_stm_hhcd_fs, pipe->pipe_index) == HC_STALL)
  146. {
  147. RT_DEBUG_LOG(RT_DEBUG_USB, ("stall\n"));
  148. pipe->status = UPIPE_STATUS_STALL;
  149. if (pipe->callback != RT_NULL)
  150. {
  151. pipe->callback(pipe);
  152. }
  153. return -1;
  154. }
  155. else if (HAL_HCD_HC_GetState(&_stm_hhcd_fs, pipe->pipe_index) == URB_ERROR)
  156. {
  157. RT_DEBUG_LOG(RT_DEBUG_USB, ("error\n"));
  158. pipe->status = UPIPE_STATUS_ERROR;
  159. if (pipe->callback != RT_NULL)
  160. {
  161. pipe->callback(pipe);
  162. }
  163. return -1;
  164. }
  165. else if (HAL_HCD_HC_GetURBState(&_stm_hhcd_fs, pipe->pipe_index) != URB_NOTREADY &&
  166. HAL_HCD_HC_GetURBState(&_stm_hhcd_fs, pipe->pipe_index) != URB_NYET)
  167. {
  168. RT_DEBUG_LOG(RT_DEBUG_USB, ("ok\n"));
  169. pipe->status = UPIPE_STATUS_OK;
  170. if (pipe->callback != RT_NULL)
  171. {
  172. pipe->callback(pipe);
  173. }
  174. return nbytes;
  175. }
  176. return -1;
  177. }
  178. }
  179. static rt_uint16_t pipe_index = 0;
  180. static rt_uint8_t drv_get_free_pipe_index()
  181. {
  182. rt_uint8_t idx;
  183. for (idx = 1; idx < 16; idx++)
  184. {
  185. if (!(pipe_index & (0x01 << idx)))
  186. {
  187. pipe_index |= (0x01 << idx);
  188. return idx;
  189. }
  190. }
  191. return 0xff;
  192. }
  193. static void drv_free_pipe_index(rt_uint8_t index)
  194. {
  195. pipe_index &= ~(0x01 << index);
  196. }
  197. static rt_err_t drv_open_pipe(upipe_t pipe)
  198. {
  199. pipe->pipe_index = drv_get_free_pipe_index();
  200. HAL_HCD_HC_Init(&_stm_hhcd_fs,
  201. pipe->pipe_index,
  202. pipe->ep.bEndpointAddress,
  203. pipe->inst->address,
  204. USB_OTG_SPEED_FULL,
  205. pipe->ep.bmAttributes,
  206. pipe->ep.wMaxPacketSize);
  207. /* Set DATA0 PID token*/
  208. if (_stm_hhcd_fs.hc[pipe->pipe_index].ep_is_in)
  209. {
  210. _stm_hhcd_fs.hc[pipe->pipe_index].toggle_in = 0;
  211. }
  212. else
  213. {
  214. _stm_hhcd_fs.hc[pipe->pipe_index].toggle_out = 0;
  215. }
  216. return RT_EOK;
  217. }
  218. static rt_err_t drv_close_pipe(upipe_t pipe)
  219. {
  220. HAL_HCD_HC_Halt(&_stm_hhcd_fs, pipe->pipe_index);
  221. drv_free_pipe_index(pipe->pipe_index);
  222. return RT_EOK;
  223. }
  224. struct uhcd_ops _uhcd_ops =
  225. {
  226. drv_reset_port,
  227. drv_pipe_xfer,
  228. drv_open_pipe,
  229. drv_close_pipe,
  230. };
  231. static rt_err_t _init(rt_device_t device)
  232. {
  233. HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef *)device->user_data;
  234. hhcd->Instance = USB_OTG_FS;
  235. hhcd->Init.Host_channels = 8;
  236. hhcd->Init.speed = HCD_SPEED_FULL;
  237. hhcd->Init.dma_enable = DISABLE;
  238. hhcd->Init.phy_itface = HCD_PHY_EMBEDDED;
  239. hhcd->Init.Sof_enable = DISABLE;
  240. RT_ASSERT(HAL_HCD_Init(hhcd) == HAL_OK);
  241. HAL_HCD_Start(hhcd);
  242. #ifdef USBH_USING_CONTROLLABLE_POWER
  243. rt_pin_mode(USBH_POWER_PIN, PIN_MODE_OUTPUT);
  244. rt_pin_write(USBH_POWER_PIN, PIN_LOW);
  245. #endif
  246. return RT_EOK;
  247. }
  248. int stm_usbh_register(void)
  249. {
  250. uhcd_t uhcd = (uhcd_t)rt_malloc(sizeof(struct uhcd));
  251. RT_ASSERT(uhcd != RT_NULL);
  252. rt_memset((void *)uhcd, 0, sizeof(struct uhcd));
  253. uhcd->parent.type = RT_Device_Class_USBHost;
  254. uhcd->parent.init = _init;
  255. uhcd->parent.user_data = &_stm_hhcd_fs;
  256. uhcd->ops = &_uhcd_ops;
  257. uhcd->num_ports = 1;
  258. _stm_hhcd_fs.pData = uhcd;
  259. rt_device_register((rt_device_t)uhcd, "usbh", 0);
  260. rt_usb_host_init();
  261. return RT_EOK;
  262. }
  263. INIT_DEVICE_EXPORT(stm_usbh_register);