usbh_conf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /**
  2. ******************************************************************************
  3. * @file : usbh_conf.c
  4. * @version : v1.0_Cube
  5. * @brief : This file implements the board support package for the USB host library
  6. ******************************************************************************
  7. * COPYRIGHT(c) 2016 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "usbh_core.h"
  35. HCD_HandleTypeDef hhcd_USB_OTG_HS;
  36. /*******************************************************************************
  37. LL Driver Callbacks (HCD -> USB Host Library)
  38. *******************************************************************************/
  39. /* MSP Init */
  40. void HAL_HCD_MspInit(HCD_HandleTypeDef* hhcd)
  41. {
  42. GPIO_InitTypeDef GPIO_InitStruct;
  43. if(hhcd->Instance==USB_OTG_HS)
  44. {
  45. /* USER CODE BEGIN USB_OTG_HS_MspInit 0 */
  46. /* USER CODE END USB_OTG_HS_MspInit 0 */
  47. /**USB_OTG_HS GPIO Configuration
  48. PB14 ------> USB_OTG_HS_DM
  49. PB15 ------> USB_OTG_HS_DP
  50. */
  51. GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
  52. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  53. GPIO_InitStruct.Pull = GPIO_NOPULL;
  54. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  55. GPIO_InitStruct.Alternate = GPIO_AF12_OTG_HS_FS;
  56. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  57. /* Peripheral clock enable */
  58. __USB_OTG_HS_CLK_ENABLE();
  59. /* Peripheral interrupt init*/
  60. HAL_NVIC_SetPriority(OTG_HS_IRQn, 0, 0);
  61. HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
  62. /* USER CODE BEGIN USB_OTG_HS_MspInit 1 */
  63. /* USER CODE END USB_OTG_HS_MspInit 1 */
  64. }
  65. }
  66. void HAL_HCD_MspDeInit(HCD_HandleTypeDef* hhcd)
  67. {
  68. if(hhcd->Instance==USB_OTG_HS)
  69. {
  70. /* USER CODE BEGIN USB_OTG_HS_MspDeInit 0 */
  71. /* USER CODE END USB_OTG_HS_MspDeInit 0 */
  72. /* Peripheral clock disable */
  73. __USB_OTG_HS_CLK_DISABLE();
  74. /**USB_OTG_HS GPIO Configuration
  75. PB14 ------> USB_OTG_HS_DM
  76. PB15 ------> USB_OTG_HS_DP
  77. */
  78. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_14|GPIO_PIN_15);
  79. /* Peripheral interrupt Deinit*/
  80. HAL_NVIC_DisableIRQ(OTG_HS_IRQn);
  81. /* USER CODE BEGIN USB_OTG_HS_MspDeInit 1 */
  82. /* USER CODE END USB_OTG_HS_MspDeInit 1 */
  83. }
  84. }
  85. /**
  86. * @brief SOF callback.
  87. * @param hhcd: HCD handle
  88. * @retval None
  89. */
  90. void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)
  91. {
  92. USBH_LL_IncTimer (hhcd->pData);
  93. }
  94. /**
  95. * @brief SOF callback.
  96. * @param hhcd: HCD handle
  97. * @retval None
  98. */
  99. void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
  100. {
  101. USBH_LL_Connect(hhcd->pData);
  102. }
  103. /**
  104. * @brief SOF callback.
  105. * @param hhcd: HCD handle
  106. * @retval None
  107. */
  108. void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
  109. {
  110. USBH_LL_Disconnect(hhcd->pData);
  111. }
  112. /**
  113. * @brief Notify URB state change callback.
  114. * @param hhcd: HCD handle
  115. * @retval None
  116. */
  117. void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
  118. {
  119. /* To be used with OS to sync URB state with the global state machine */
  120. #if (USBH_USE_OS == 1)
  121. USBH_LL_NotifyURBChange(hhcd->pData);
  122. #endif
  123. }
  124. /*******************************************************************************
  125. LL Driver Interface (USB Host Library --> HCD)
  126. *******************************************************************************/
  127. /**
  128. * @brief USBH_LL_Init
  129. * Initialize the Low Level portion of the Host driver.
  130. * @param phost: Host handle
  131. * @retval USBH Status
  132. */
  133. USBH_StatusTypeDef USBH_LL_Init (USBH_HandleTypeDef *phost)
  134. {
  135. /* Init USB_IP */
  136. if (phost->id == HOST_HS) {
  137. /* Link The driver to the stack */
  138. hhcd_USB_OTG_HS.pData = phost;
  139. phost->pData = &hhcd_USB_OTG_HS;
  140. hhcd_USB_OTG_HS.Instance = USB_OTG_HS;
  141. hhcd_USB_OTG_HS.Init.Host_channels = 12;
  142. hhcd_USB_OTG_HS.Init.speed = HCD_SPEED_FULL;
  143. hhcd_USB_OTG_HS.Init.dma_enable = DISABLE;
  144. hhcd_USB_OTG_HS.Init.phy_itface = USB_OTG_EMBEDDED_PHY;
  145. hhcd_USB_OTG_HS.Init.Sof_enable = DISABLE;
  146. hhcd_USB_OTG_HS.Init.low_power_enable = DISABLE;
  147. hhcd_USB_OTG_HS.Init.vbus_sensing_enable = DISABLE;
  148. hhcd_USB_OTG_HS.Init.use_external_vbus = DISABLE;
  149. HAL_HCD_Init(&hhcd_USB_OTG_HS);
  150. USBH_LL_SetTimer (phost, HAL_HCD_GetCurrentFrame(&hhcd_USB_OTG_HS));
  151. }
  152. return USBH_OK;
  153. }
  154. /**
  155. * @brief USBH_LL_DeInit
  156. * De-Initialize the Low Level portion of the Host driver.
  157. * @param phost: Host handle
  158. * @retval USBH Status
  159. */
  160. USBH_StatusTypeDef USBH_LL_DeInit (USBH_HandleTypeDef *phost)
  161. {
  162. HAL_HCD_DeInit(phost->pData);
  163. return USBH_OK;
  164. }
  165. /**
  166. * @brief USBH_LL_Start
  167. * Start the Low Level portion of the Host driver.
  168. * @param phost: Host handle
  169. * @retval USBH Status
  170. */
  171. USBH_StatusTypeDef USBH_LL_Start(USBH_HandleTypeDef *phost)
  172. {
  173. HAL_HCD_Start(phost->pData);
  174. return USBH_OK;
  175. }
  176. /**
  177. * @brief USBH_LL_Stop
  178. * Stop the Low Level portion of the Host driver.
  179. * @param phost: Host handle
  180. * @retval USBH Status
  181. */
  182. USBH_StatusTypeDef USBH_LL_Stop (USBH_HandleTypeDef *phost)
  183. {
  184. HAL_HCD_Stop(phost->pData);
  185. return USBH_OK;
  186. }
  187. /**
  188. * @brief USBH_LL_GetSpeed
  189. * Return the USB Host Speed from the Low Level Driver.
  190. * @param phost: Host handle
  191. * @retval USBH Speeds
  192. */
  193. USBH_SpeedTypeDef USBH_LL_GetSpeed (USBH_HandleTypeDef *phost)
  194. {
  195. USBH_SpeedTypeDef speed = USBH_SPEED_FULL;
  196. switch (HAL_HCD_GetCurrentSpeed(phost->pData))
  197. {
  198. case 0 :
  199. speed = USBH_SPEED_HIGH;
  200. break;
  201. case 1 :
  202. speed = USBH_SPEED_FULL;
  203. break;
  204. case 2 :
  205. speed = USBH_SPEED_LOW;
  206. break;
  207. default:
  208. speed = USBH_SPEED_FULL;
  209. break;
  210. }
  211. return speed;
  212. }
  213. /**
  214. * @brief USBH_LL_ResetPort
  215. * Reset the Host Port of the Low Level Driver.
  216. * @param phost: Host handle
  217. * @retval USBH Status
  218. */
  219. USBH_StatusTypeDef USBH_LL_ResetPort (USBH_HandleTypeDef *phost)
  220. {
  221. HAL_HCD_ResetPort(phost->pData);
  222. return USBH_OK;
  223. }
  224. /**
  225. * @brief USBH_LL_GetLastXferSize
  226. * Return the last transfered packet size.
  227. * @param phost: Host handle
  228. * @param pipe: Pipe index
  229. * @retval Packet Size
  230. */
  231. uint32_t USBH_LL_GetLastXferSize (USBH_HandleTypeDef *phost, uint8_t pipe)
  232. {
  233. return HAL_HCD_HC_GetXferCount(phost->pData, pipe);
  234. }
  235. /**
  236. * @brief USBH_LL_OpenPipe
  237. * Open a pipe of the Low Level Driver.
  238. * @param phost: Host handle
  239. * @param pipe_num: Pipe index
  240. * @param epnum: Endpoint Number
  241. * @param dev_address: Device USB address
  242. * @param speed: Device Speed
  243. * @param ep_type: Endpoint Type
  244. * @param mps: Endpoint Max Packet Size
  245. * @retval USBH Status
  246. */
  247. USBH_StatusTypeDef USBH_LL_OpenPipe (USBH_HandleTypeDef *phost,
  248. uint8_t pipe_num,
  249. uint8_t epnum,
  250. uint8_t dev_address,
  251. uint8_t speed,
  252. uint8_t ep_type,
  253. uint16_t mps)
  254. {
  255. HAL_HCD_HC_Init(phost->pData,
  256. pipe_num,
  257. epnum,
  258. dev_address,
  259. speed,
  260. ep_type,
  261. mps);
  262. return USBH_OK;
  263. }
  264. /**
  265. * @brief USBH_LL_ClosePipe
  266. * Close a pipe of the Low Level Driver.
  267. * @param phost: Host handle
  268. * @param pipe_num: Pipe index
  269. * @retval USBH Status
  270. */
  271. USBH_StatusTypeDef USBH_LL_ClosePipe (USBH_HandleTypeDef *phost, uint8_t pipe)
  272. {
  273. HAL_HCD_HC_Halt(phost->pData, pipe);
  274. return USBH_OK;
  275. }
  276. /**
  277. * @brief USBH_LL_SubmitURB
  278. * Submit a new URB to the low level driver.
  279. * @param phost: Host handle
  280. * @param pipe: Pipe index
  281. * This parameter can be a value from 1 to 15
  282. * @param direction : Channel number
  283. * This parameter can be one of the these values:
  284. * 0 : Output
  285. * 1 : Input
  286. * @param ep_type : Endpoint Type
  287. * This parameter can be one of the these values:
  288. * @arg EP_TYPE_CTRL: Control type
  289. * @arg EP_TYPE_ISOC: Isochrounous type
  290. * @arg EP_TYPE_BULK: Bulk type
  291. * @arg EP_TYPE_INTR: Interrupt type
  292. * @param token : Endpoint Type
  293. * This parameter can be one of the these values:
  294. * @arg 0: PID_SETUP
  295. * @arg 1: PID_DATA
  296. * @param pbuff : pointer to URB data
  297. * @param length : Length of URB data
  298. * @param do_ping : activate do ping protocol (for high speed only)
  299. * This parameter can be one of the these values:
  300. * 0 : do ping inactive
  301. * 1 : do ping active
  302. * @retval Status
  303. */
  304. USBH_StatusTypeDef USBH_LL_SubmitURB (USBH_HandleTypeDef *phost,
  305. uint8_t pipe,
  306. uint8_t direction ,
  307. uint8_t ep_type,
  308. uint8_t token,
  309. uint8_t* pbuff,
  310. uint16_t length,
  311. uint8_t do_ping )
  312. {
  313. HAL_HCD_HC_SubmitRequest (phost->pData,
  314. pipe,
  315. direction ,
  316. ep_type,
  317. token,
  318. pbuff,
  319. length,
  320. do_ping);
  321. return USBH_OK;
  322. }
  323. /**
  324. * @brief USBH_LL_GetURBState
  325. * Get a URB state from the low level driver.
  326. * @param phost: Host handle
  327. * @param pipe: Pipe index
  328. * This parameter can be a value from 1 to 15
  329. * @retval URB state
  330. * This parameter can be one of the these values:
  331. * @arg URB_IDLE
  332. * @arg URB_DONE
  333. * @arg URB_NOTREADY
  334. * @arg URB_NYET
  335. * @arg URB_ERROR
  336. * @arg URB_STALL
  337. */
  338. USBH_URBStateTypeDef USBH_LL_GetURBState (USBH_HandleTypeDef *phost, uint8_t pipe)
  339. {
  340. return (USBH_URBStateTypeDef)HAL_HCD_HC_GetURBState (phost->pData, pipe);
  341. }
  342. /**
  343. * @brief USBH_LL_DriverVBUS
  344. * Drive VBUS.
  345. * @param phost: Host handle
  346. * @param state : VBUS state
  347. * This parameter can be one of the these values:
  348. * 0 : VBUS Active
  349. * 1 : VBUS Inactive
  350. * @retval Status
  351. */
  352. USBH_StatusTypeDef USBH_LL_DriverVBUS (USBH_HandleTypeDef *phost, uint8_t state)
  353. {
  354. /* USER CODE BEGIN 0 */
  355. /* USER CODE END 0*/
  356. if (phost->id == HOST_HS)
  357. {
  358. if (state == 0)
  359. {
  360. /* Drive high Charge pump */
  361. /* ToDo: Add IOE driver control */
  362. /* USER CODE BEGIN DRIVE_HIGH_CHARGE_FOR_HS */
  363. /* USER CODE END DRIVE_HIGH_CHARGE_FOR_HS */
  364. }
  365. else
  366. {
  367. /* Drive low Charge pump */
  368. /* ToDo: Add IOE driver control */
  369. /* USER CODE BEGIN DRIVE_LOW_CHARGE_FOR_HS */
  370. /* USER CODE BEGIN DRIVE_LOW_CHARGE_FOR_HS */
  371. }
  372. }
  373. HAL_Delay(200);
  374. return USBH_OK;
  375. }
  376. /**
  377. * @brief USBH_LL_SetToggle
  378. * Set toggle for a pipe.
  379. * @param phost: Host handle
  380. * @param pipe: Pipe index
  381. * @param pipe_num: Pipe index
  382. * @param toggle: toggle (0/1)
  383. * @retval Status
  384. */
  385. USBH_StatusTypeDef USBH_LL_SetToggle (USBH_HandleTypeDef *phost, uint8_t pipe, uint8_t toggle)
  386. {
  387. HCD_HandleTypeDef *pHandle;
  388. pHandle = phost->pData;
  389. if(pHandle->hc[pipe].ep_is_in)
  390. {
  391. pHandle->hc[pipe].toggle_in = toggle;
  392. }
  393. else
  394. {
  395. pHandle->hc[pipe].toggle_out = toggle;
  396. }
  397. return USBH_OK;
  398. }
  399. /**
  400. * @brief USBH_LL_GetToggle
  401. * Return the current toggle of a pipe.
  402. * @param phost: Host handle
  403. * @param pipe: Pipe index
  404. * @retval toggle (0/1)
  405. */
  406. uint8_t USBH_LL_GetToggle (USBH_HandleTypeDef *phost, uint8_t pipe)
  407. {
  408. uint8_t toggle = 0;
  409. HCD_HandleTypeDef *pHandle;
  410. pHandle = phost->pData;
  411. if(pHandle->hc[pipe].ep_is_in)
  412. {
  413. toggle = pHandle->hc[pipe].toggle_in;
  414. }
  415. else
  416. {
  417. toggle = pHandle->hc[pipe].toggle_out;
  418. }
  419. return toggle;
  420. }
  421. /**
  422. * @brief USBH_Delay
  423. * Delay routine for the USB Host Library
  424. * @param Delay: Delay in ms
  425. * @retval None
  426. */
  427. void USBH_Delay (uint32_t Delay)
  428. {
  429. HAL_Delay(Delay);
  430. }
  431. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/