usbd_conf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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) 2019 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. HAL_PCD_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
  263. HAL_PCD_SetTxFiFo(&hpcd_USB_OTG_FS, 0, 0x40);
  264. HAL_PCD_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x80);
  265. }
  266. return USBD_OK;
  267. }
  268. /**
  269. * @brief De-Initializes the Low Level portion of the Device driver.
  270. * @param pdev: Device handle
  271. * @retval USBD Status
  272. */
  273. USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev)
  274. {
  275. HAL_PCD_DeInit(pdev->pData);
  276. return USBD_OK;
  277. }
  278. /**
  279. * @brief Starts the Low Level portion of the Device driver.
  280. * @param pdev: Device handle
  281. * @retval USBD Status
  282. */
  283. USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
  284. {
  285. HAL_PCD_Start(pdev->pData);
  286. return USBD_OK;
  287. }
  288. /**
  289. * @brief Stops the Low Level portion of the Device driver.
  290. * @param pdev: Device handle
  291. * @retval USBD Status
  292. */
  293. USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev)
  294. {
  295. HAL_PCD_Stop(pdev->pData);
  296. return USBD_OK;
  297. }
  298. /**
  299. * @brief Opens an endpoint of the Low Level Driver.
  300. * @param pdev: Device handle
  301. * @param ep_addr: Endpoint Number
  302. * @param ep_type: Endpoint Type
  303. * @param ep_mps: Endpoint Max Packet Size
  304. * @retval USBD Status
  305. */
  306. USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev,
  307. uint8_t ep_addr,
  308. uint8_t ep_type,
  309. uint16_t ep_mps)
  310. {
  311. HAL_PCD_EP_Open(pdev->pData,
  312. ep_addr,
  313. ep_mps,
  314. ep_type);
  315. return USBD_OK;
  316. }
  317. /**
  318. * @brief Closes an endpoint of the Low Level Driver.
  319. * @param pdev: Device handle
  320. * @param ep_addr: Endpoint Number
  321. * @retval USBD Status
  322. */
  323. USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  324. {
  325. HAL_PCD_EP_Close(pdev->pData, ep_addr);
  326. return USBD_OK;
  327. }
  328. /**
  329. * @brief Flushes an endpoint of the Low Level Driver.
  330. * @param pdev: Device handle
  331. * @param ep_addr: Endpoint Number
  332. * @retval USBD Status
  333. */
  334. USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  335. {
  336. HAL_PCD_EP_Flush(pdev->pData, ep_addr);
  337. return USBD_OK;
  338. }
  339. /**
  340. * @brief Sets a Stall condition on an endpoint of the Low Level Driver.
  341. * @param pdev: Device handle
  342. * @param ep_addr: Endpoint Number
  343. * @retval USBD Status
  344. */
  345. USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  346. {
  347. HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
  348. return USBD_OK;
  349. }
  350. /**
  351. * @brief Clears a Stall condition on an endpoint of the Low Level Driver.
  352. * @param pdev: Device handle
  353. * @param ep_addr: Endpoint Number
  354. * @retval USBD Status
  355. */
  356. USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  357. {
  358. HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
  359. return USBD_OK;
  360. }
  361. /**
  362. * @brief Returns Stall condition.
  363. * @param pdev: Device handle
  364. * @param ep_addr: Endpoint Number
  365. * @retval Stall (1: Yes, 0: No)
  366. */
  367. uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  368. {
  369. PCD_HandleTypeDef *hpcd = pdev->pData;
  370. if((ep_addr & 0x80) == 0x80)
  371. {
  372. return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
  373. }
  374. else
  375. {
  376. return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
  377. }
  378. }
  379. /**
  380. * @brief Assigns a USB address to the device.
  381. * @param pdev: Device handle
  382. * @param ep_addr: Endpoint Number
  383. * @retval USBD Status
  384. */
  385. USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr)
  386. {
  387. HAL_PCD_SetAddress(pdev->pData, dev_addr);
  388. return USBD_OK;
  389. }
  390. /**
  391. * @brief Transmits data over an endpoint.
  392. * @param pdev: Device handle
  393. * @param ep_addr: Endpoint Number
  394. * @param pbuf: Pointer to data to be sent
  395. * @param size: Data size
  396. * @retval USBD Status
  397. */
  398. USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev,
  399. uint8_t ep_addr,
  400. uint8_t *pbuf,
  401. uint16_t size)
  402. {
  403. HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
  404. return USBD_OK;
  405. }
  406. /**
  407. * @brief Prepares an endpoint for reception.
  408. * @param pdev: Device handle
  409. * @param ep_addr: Endpoint Number
  410. * @param pbuf: Pointer to data to be received
  411. * @param size: Data size
  412. * @retval USBD Status
  413. */
  414. USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
  415. uint8_t ep_addr,
  416. uint8_t *pbuf,
  417. uint16_t size)
  418. {
  419. HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
  420. return USBD_OK;
  421. }
  422. /**
  423. * @brief Returns the last transfered packet size.
  424. * @param pdev: Device handle
  425. * @param ep_addr: Endpoint Number
  426. * @retval Recived Data Size
  427. */
  428. uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  429. {
  430. return HAL_PCD_EP_GetRxCount(pdev->pData, ep_addr);
  431. }
  432. /**
  433. * @brief Delays routine for the USB Device Library.
  434. * @param Delay: Delay in ms
  435. * @retval None
  436. */
  437. void USBD_LL_Delay (uint32_t Delay)
  438. {
  439. HAL_Delay(Delay);
  440. }
  441. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/