drv_usbh.c 8.4 KB

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