usbd_conf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /**
  2. ******************************************************************************
  3. * @file : usbd_conf.c
  4. * @version : v1.0_Cube
  5. * @brief : This file implements the board support package for the USB device library
  6. ******************************************************************************
  7. *
  8. * COPYRIGHT(c) 2016 STMicroelectronics
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f2xx.h"
  36. #include "stm32f2xx_hal.h"
  37. #include "usbd_def.h"
  38. #include "usbd_core.h"
  39. PCD_HandleTypeDef hpcd_USB_OTG_FS;
  40. /* External functions --------------------------------------------------------*/
  41. void SystemClock_Config(void);
  42. /* USER CODE BEGIN 0 */
  43. /* USER CODE END 0 */
  44. /* Private function prototypes -----------------------------------------------*/
  45. /* Private functions ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 1 */
  47. /* USER CODE END 1 */
  48. /*******************************************************************************
  49. LL Driver Callbacks (PCD -> USB Device Library)
  50. *******************************************************************************/
  51. /* MSP Init */
  52. void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd)
  53. {
  54. GPIO_InitTypeDef GPIO_InitStruct;
  55. if(hpcd->Instance==USB_OTG_FS)
  56. {
  57. /* USER CODE BEGIN USB_OTG_FS_MspInit 0 */
  58. /* USER CODE END USB_OTG_FS_MspInit 0 */
  59. /**USB_OTG_FS GPIO Configuration
  60. PA9 ------> USB_OTG_FS_VBUS
  61. PA11 ------> USB_OTG_FS_DM
  62. PA12 ------> USB_OTG_FS_DP
  63. */
  64. GPIO_InitStruct.Pin = GPIO_PIN_9;
  65. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  66. GPIO_InitStruct.Pull = GPIO_NOPULL;
  67. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  68. GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
  69. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  70. GPIO_InitStruct.Pull = GPIO_NOPULL;
  71. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  72. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
  73. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  74. /* Peripheral clock enable */
  75. __USB_OTG_FS_CLK_ENABLE();
  76. /* Peripheral interrupt init*/
  77. HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
  78. HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
  79. /* USER CODE BEGIN USB_OTG_FS_MspInit 1 */
  80. /* USER CODE END USB_OTG_FS_MspInit 1 */
  81. }
  82. }
  83. void HAL_PCD_MspDeInit(PCD_HandleTypeDef* hpcd)
  84. {
  85. if(hpcd->Instance==USB_OTG_FS)
  86. {
  87. /* USER CODE BEGIN USB_OTG_FS_MspDeInit 0 */
  88. /* USER CODE END USB_OTG_FS_MspDeInit 0 */
  89. /* Peripheral clock disable */
  90. __USB_OTG_FS_CLK_DISABLE();
  91. /**USB_OTG_FS GPIO Configuration
  92. PA9 ------> USB_OTG_FS_VBUS
  93. PA11 ------> USB_OTG_FS_DM
  94. PA12 ------> USB_OTG_FS_DP
  95. */
  96. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_11|GPIO_PIN_12);
  97. /* Peripheral interrupt Deinit*/
  98. HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
  99. /* USER CODE BEGIN USB_OTG_FS_MspDeInit 1 */
  100. /* USER CODE END USB_OTG_FS_MspDeInit 1 */
  101. }
  102. }
  103. /**
  104. * @brief Setup stage callback
  105. * @param hpcd: PCD handle
  106. * @retval None
  107. */
  108. void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  109. {
  110. USBD_LL_SetupStage(hpcd->pData, (uint8_t *)hpcd->Setup);
  111. }
  112. /**
  113. * @brief Data Out stage callback.
  114. * @param hpcd: PCD handle
  115. * @param epnum: Endpoint Number
  116. * @retval None
  117. */
  118. void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  119. {
  120. USBD_LL_DataOutStage(hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
  121. }
  122. /**
  123. * @brief Data In stage callback..
  124. * @param hpcd: PCD handle
  125. * @param epnum: Endpoint Number
  126. * @retval None
  127. */
  128. void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  129. {
  130. USBD_LL_DataInStage(hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
  131. }
  132. /**
  133. * @brief SOF callback.
  134. * @param hpcd: PCD handle
  135. * @retval None
  136. */
  137. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  138. {
  139. USBD_LL_SOF(hpcd->pData);
  140. }
  141. /**
  142. * @brief Reset callback.
  143. * @param hpcd: PCD handle
  144. * @retval None
  145. */
  146. void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  147. {
  148. USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
  149. /*Set USB Current Speed*/
  150. switch (hpcd->Init.speed)
  151. {
  152. case PCD_SPEED_HIGH:
  153. speed = USBD_SPEED_HIGH;
  154. break;
  155. case PCD_SPEED_FULL:
  156. speed = USBD_SPEED_FULL;
  157. break;
  158. default:
  159. speed = USBD_SPEED_FULL;
  160. break;
  161. }
  162. USBD_LL_SetSpeed(hpcd->pData, speed);
  163. /*Reset Device*/
  164. USBD_LL_Reset(hpcd->pData);
  165. }
  166. /**
  167. * @brief Suspend callback.
  168. * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  169. * @param hpcd: PCD handle
  170. * @retval None
  171. */
  172. void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  173. {
  174. /* Inform USB library that core enters in suspend Mode */
  175. USBD_LL_Suspend(hpcd->pData);
  176. __HAL_PCD_GATE_PHYCLOCK(hpcd);
  177. /*Enter in STOP mode */
  178. /* USER CODE BEGIN 2 */
  179. if (hpcd->Init.low_power_enable)
  180. {
  181. /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */
  182. SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  183. }
  184. /* USER CODE END 2 */
  185. }
  186. /**
  187. * @brief Resume callback.
  188. When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  189. * @param hpcd: PCD handle
  190. * @retval None
  191. */
  192. void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  193. {
  194. /* USER CODE BEGIN 3 */
  195. /* USER CODE END 3 */
  196. USBD_LL_Resume(hpcd->pData);
  197. }
  198. /**
  199. * @brief ISOC Out Incomplete callback.
  200. * @param hpcd: PCD handle
  201. * @param epnum: Endpoint Number
  202. * @retval None
  203. */
  204. void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  205. {
  206. USBD_LL_IsoOUTIncomplete(hpcd->pData, epnum);
  207. }
  208. /**
  209. * @brief ISOC In Incomplete callback.
  210. * @param hpcd: PCD handle
  211. * @param epnum: Endpoint Number
  212. * @retval None
  213. */
  214. void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  215. {
  216. USBD_LL_IsoINIncomplete(hpcd->pData, epnum);
  217. }
  218. /**
  219. * @brief Connect callback.
  220. * @param hpcd: PCD handle
  221. * @retval None
  222. */
  223. void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  224. {
  225. USBD_LL_DevConnected(hpcd->pData);
  226. }
  227. /**
  228. * @brief Disconnect callback.
  229. * @param hpcd: PCD handle
  230. * @retval None
  231. */
  232. void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  233. {
  234. USBD_LL_DevDisconnected(hpcd->pData);
  235. }
  236. /*******************************************************************************
  237. LL Driver Interface (USB Device Library --> PCD)
  238. *******************************************************************************/
  239. /**
  240. * @brief Initializes the Low Level portion of the Device driver.
  241. * @param pdev: Device handle
  242. * @retval USBD Status
  243. */
  244. USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
  245. {
  246. /* Init USB_IP */
  247. if (pdev->id == DEVICE_FS) {
  248. /* Link The driver to the stack */
  249. hpcd_USB_OTG_FS.pData = pdev;
  250. pdev->pData = &hpcd_USB_OTG_FS;
  251. hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  252. hpcd_USB_OTG_FS.Init.dev_endpoints = 7;
  253. hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
  254. hpcd_USB_OTG_FS.Init.dma_enable = DISABLE;
  255. hpcd_USB_OTG_FS.Init.ep0_mps = DEP0CTL_MPS_64;
  256. hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  257. hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
  258. hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  259. hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE;
  260. hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  261. HAL_PCD_Init(&hpcd_USB_OTG_FS);
  262. // Sum of all FIFOs must be <= 320.
  263. HAL_PCD_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
  264. HAL_PCD_SetTxFiFo(&hpcd_USB_OTG_FS, 0, 0x40);
  265. HAL_PCD_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x40);
  266. HAL_PCD_SetTxFiFo(&hpcd_USB_OTG_FS, 2, 0x40);
  267. }
  268. return USBD_OK;
  269. }
  270. /**
  271. * @brief De-Initializes the Low Level portion of the Device driver.
  272. * @param pdev: Device handle
  273. * @retval USBD Status
  274. */
  275. USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev)
  276. {
  277. HAL_PCD_DeInit(pdev->pData);
  278. return USBD_OK;
  279. }
  280. /**
  281. * @brief Starts the Low Level portion of the Device driver.
  282. * @param pdev: Device handle
  283. * @retval USBD Status
  284. */
  285. USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
  286. {
  287. HAL_PCD_Start(pdev->pData);
  288. return USBD_OK;
  289. }
  290. /**
  291. * @brief Stops the Low Level portion of the Device driver.
  292. * @param pdev: Device handle
  293. * @retval USBD Status
  294. */
  295. USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev)
  296. {
  297. HAL_PCD_Stop(pdev->pData);
  298. return USBD_OK;
  299. }
  300. /**
  301. * @brief Opens an endpoint of the Low Level Driver.
  302. * @param pdev: Device handle
  303. * @param ep_addr: Endpoint Number
  304. * @param ep_type: Endpoint Type
  305. * @param ep_mps: Endpoint Max Packet Size
  306. * @retval USBD Status
  307. */
  308. USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev,
  309. uint8_t ep_addr,
  310. uint8_t ep_type,
  311. uint16_t ep_mps)
  312. {
  313. HAL_PCD_EP_Open(pdev->pData,
  314. ep_addr,
  315. ep_mps,
  316. ep_type);
  317. return USBD_OK;
  318. }
  319. /**
  320. * @brief Closes an endpoint of the Low Level Driver.
  321. * @param pdev: Device handle
  322. * @param ep_addr: Endpoint Number
  323. * @retval USBD Status
  324. */
  325. USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  326. {
  327. HAL_PCD_EP_Close(pdev->pData, ep_addr);
  328. return USBD_OK;
  329. }
  330. /**
  331. * @brief Flushes an endpoint of the Low Level Driver.
  332. * @param pdev: Device handle
  333. * @param ep_addr: Endpoint Number
  334. * @retval USBD Status
  335. */
  336. USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  337. {
  338. HAL_PCD_EP_Flush(pdev->pData, ep_addr);
  339. return USBD_OK;
  340. }
  341. /**
  342. * @brief Sets a Stall condition on an endpoint of the Low Level Driver.
  343. * @param pdev: Device handle
  344. * @param ep_addr: Endpoint Number
  345. * @retval USBD Status
  346. */
  347. USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  348. {
  349. HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
  350. return USBD_OK;
  351. }
  352. /**
  353. * @brief Clears a Stall condition on an endpoint of the Low Level Driver.
  354. * @param pdev: Device handle
  355. * @param ep_addr: Endpoint Number
  356. * @retval USBD Status
  357. */
  358. USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  359. {
  360. HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
  361. return USBD_OK;
  362. }
  363. /**
  364. * @brief Returns Stall condition.
  365. * @param pdev: Device handle
  366. * @param ep_addr: Endpoint Number
  367. * @retval Stall (1: Yes, 0: No)
  368. */
  369. uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  370. {
  371. PCD_HandleTypeDef *hpcd = pdev->pData;
  372. if((ep_addr & 0x80) == 0x80)
  373. {
  374. return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
  375. }
  376. else
  377. {
  378. return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
  379. }
  380. }
  381. /**
  382. * @brief Assigns a USB address to the device.
  383. * @param pdev: Device handle
  384. * @param ep_addr: Endpoint Number
  385. * @retval USBD Status
  386. */
  387. USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr)
  388. {
  389. HAL_PCD_SetAddress(pdev->pData, dev_addr);
  390. return USBD_OK;
  391. }
  392. /**
  393. * @brief Transmits data over an endpoint.
  394. * @param pdev: Device handle
  395. * @param ep_addr: Endpoint Number
  396. * @param pbuf: Pointer to data to be sent
  397. * @param size: Data size
  398. * @retval USBD Status
  399. */
  400. USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev,
  401. uint8_t ep_addr,
  402. uint8_t *pbuf,
  403. uint16_t size)
  404. {
  405. HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
  406. return USBD_OK;
  407. }
  408. /**
  409. * @brief Prepares an endpoint for reception.
  410. * @param pdev: Device handle
  411. * @param ep_addr: Endpoint Number
  412. * @param pbuf: Pointer to data to be received
  413. * @param size: Data size
  414. * @retval USBD Status
  415. */
  416. USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
  417. uint8_t ep_addr,
  418. uint8_t *pbuf,
  419. uint16_t size)
  420. {
  421. HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
  422. return USBD_OK;
  423. }
  424. /**
  425. * @brief Returns the last transfered packet size.
  426. * @param pdev: Device handle
  427. * @param ep_addr: Endpoint Number
  428. * @retval Recived Data Size
  429. */
  430. uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  431. {
  432. return HAL_PCD_EP_GetRxCount(pdev->pData, ep_addr);
  433. }
  434. /**
  435. * @brief Delays routine for the USB Device Library.
  436. * @param Delay: Delay in ms
  437. * @retval None
  438. */
  439. void USBD_LL_Delay (uint32_t Delay)
  440. {
  441. HAL_Delay(Delay);
  442. }
  443. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/