usbd_conf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. // MM: Don't let pins float.
  98. GPIO_InitTypeDef GPIO_InitStruct;
  99. GPIO_InitStruct.Pin = GPIO_PIN_9;
  100. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  101. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  102. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  103. GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
  104. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  105. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  106. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  107. /* Peripheral interrupt Deinit*/
  108. HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
  109. /* USER CODE BEGIN USB_OTG_FS_MspDeInit 1 */
  110. /* USER CODE END USB_OTG_FS_MspDeInit 1 */
  111. }
  112. }
  113. /**
  114. * @brief Setup stage callback
  115. * @param hpcd: PCD handle
  116. * @retval None
  117. */
  118. void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  119. {
  120. USBD_LL_SetupStage(hpcd->pData, (uint8_t *)hpcd->Setup);
  121. }
  122. /**
  123. * @brief Data Out stage callback.
  124. * @param hpcd: PCD handle
  125. * @param epnum: Endpoint Number
  126. * @retval None
  127. */
  128. void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  129. {
  130. USBD_LL_DataOutStage(hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
  131. }
  132. /**
  133. * @brief Data In stage callback..
  134. * @param hpcd: PCD handle
  135. * @param epnum: Endpoint Number
  136. * @retval None
  137. */
  138. void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  139. {
  140. USBD_LL_DataInStage(hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
  141. }
  142. /**
  143. * @brief SOF callback.
  144. * @param hpcd: PCD handle
  145. * @retval None
  146. */
  147. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  148. {
  149. USBD_LL_SOF(hpcd->pData);
  150. }
  151. /**
  152. * @brief Reset callback.
  153. * @param hpcd: PCD handle
  154. * @retval None
  155. */
  156. void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  157. {
  158. USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
  159. /*Set USB Current Speed*/
  160. switch (hpcd->Init.speed)
  161. {
  162. case PCD_SPEED_HIGH:
  163. speed = USBD_SPEED_HIGH;
  164. break;
  165. case PCD_SPEED_FULL:
  166. speed = USBD_SPEED_FULL;
  167. break;
  168. default:
  169. speed = USBD_SPEED_FULL;
  170. break;
  171. }
  172. USBD_LL_SetSpeed(hpcd->pData, speed);
  173. /*Reset Device*/
  174. USBD_LL_Reset(hpcd->pData);
  175. }
  176. /**
  177. * @brief Suspend callback.
  178. * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  179. * @param hpcd: PCD handle
  180. * @retval None
  181. */
  182. void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  183. {
  184. /* Inform USB library that core enters in suspend Mode */
  185. USBD_LL_Suspend(hpcd->pData);
  186. __HAL_PCD_GATE_PHYCLOCK(hpcd);
  187. /*Enter in STOP mode */
  188. /* USER CODE BEGIN 2 */
  189. if (hpcd->Init.low_power_enable)
  190. {
  191. /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */
  192. SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  193. }
  194. /* USER CODE END 2 */
  195. }
  196. /**
  197. * @brief Resume callback.
  198. When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  199. * @param hpcd: PCD handle
  200. * @retval None
  201. */
  202. void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  203. {
  204. /* USER CODE BEGIN 3 */
  205. /* USER CODE END 3 */
  206. USBD_LL_Resume(hpcd->pData);
  207. }
  208. /**
  209. * @brief ISOC Out Incomplete callback.
  210. * @param hpcd: PCD handle
  211. * @param epnum: Endpoint Number
  212. * @retval None
  213. */
  214. void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  215. {
  216. USBD_LL_IsoOUTIncomplete(hpcd->pData, epnum);
  217. }
  218. /**
  219. * @brief ISOC In Incomplete callback.
  220. * @param hpcd: PCD handle
  221. * @param epnum: Endpoint Number
  222. * @retval None
  223. */
  224. void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  225. {
  226. USBD_LL_IsoINIncomplete(hpcd->pData, epnum);
  227. }
  228. /**
  229. * @brief Connect callback.
  230. * @param hpcd: PCD handle
  231. * @retval None
  232. */
  233. void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  234. {
  235. USBD_LL_DevConnected(hpcd->pData);
  236. }
  237. /**
  238. * @brief Disconnect callback.
  239. * @param hpcd: PCD handle
  240. * @retval None
  241. */
  242. void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  243. {
  244. USBD_LL_DevDisconnected(hpcd->pData);
  245. }
  246. /*******************************************************************************
  247. LL Driver Interface (USB Device Library --> PCD)
  248. *******************************************************************************/
  249. /**
  250. * @brief Initializes the Low Level portion of the Device driver.
  251. * @param pdev: Device handle
  252. * @retval USBD Status
  253. */
  254. USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
  255. {
  256. /* Init USB_IP */
  257. if (pdev->id == DEVICE_FS) {
  258. /* Link The driver to the stack */
  259. hpcd_USB_OTG_FS.pData = pdev;
  260. pdev->pData = &hpcd_USB_OTG_FS;
  261. hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  262. hpcd_USB_OTG_FS.Init.dev_endpoints = 7;
  263. hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
  264. hpcd_USB_OTG_FS.Init.dma_enable = DISABLE;
  265. hpcd_USB_OTG_FS.Init.ep0_mps = DEP0CTL_MPS_64;
  266. hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  267. hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
  268. hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  269. hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE;
  270. hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  271. HAL_PCD_Init(&hpcd_USB_OTG_FS);
  272. // Sum of all FIFOs must be <= 320.
  273. HAL_PCD_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
  274. HAL_PCD_SetTxFiFo(&hpcd_USB_OTG_FS, 0, 0x40);
  275. HAL_PCD_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x40);
  276. HAL_PCD_SetTxFiFo(&hpcd_USB_OTG_FS, 2, 0x40);
  277. }
  278. return USBD_OK;
  279. }
  280. /**
  281. * @brief De-Initializes the Low Level portion of the Device driver.
  282. * @param pdev: Device handle
  283. * @retval USBD Status
  284. */
  285. USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev)
  286. {
  287. HAL_PCD_DeInit(pdev->pData);
  288. return USBD_OK;
  289. }
  290. /**
  291. * @brief Starts the Low Level portion of the Device driver.
  292. * @param pdev: Device handle
  293. * @retval USBD Status
  294. */
  295. USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
  296. {
  297. HAL_PCD_Start(pdev->pData);
  298. return USBD_OK;
  299. }
  300. /**
  301. * @brief Stops the Low Level portion of the Device driver.
  302. * @param pdev: Device handle
  303. * @retval USBD Status
  304. */
  305. USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev)
  306. {
  307. HAL_PCD_Stop(pdev->pData);
  308. return USBD_OK;
  309. }
  310. /**
  311. * @brief Opens an endpoint of the Low Level Driver.
  312. * @param pdev: Device handle
  313. * @param ep_addr: Endpoint Number
  314. * @param ep_type: Endpoint Type
  315. * @param ep_mps: Endpoint Max Packet Size
  316. * @retval USBD Status
  317. */
  318. USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev,
  319. uint8_t ep_addr,
  320. uint8_t ep_type,
  321. uint16_t ep_mps)
  322. {
  323. HAL_PCD_EP_Open(pdev->pData,
  324. ep_addr,
  325. ep_mps,
  326. ep_type);
  327. return USBD_OK;
  328. }
  329. /**
  330. * @brief Closes an endpoint of the Low Level Driver.
  331. * @param pdev: Device handle
  332. * @param ep_addr: Endpoint Number
  333. * @retval USBD Status
  334. */
  335. USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  336. {
  337. HAL_PCD_EP_Close(pdev->pData, ep_addr);
  338. return USBD_OK;
  339. }
  340. /**
  341. * @brief Flushes an endpoint of the Low Level Driver.
  342. * @param pdev: Device handle
  343. * @param ep_addr: Endpoint Number
  344. * @retval USBD Status
  345. */
  346. USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  347. {
  348. HAL_PCD_EP_Flush(pdev->pData, ep_addr);
  349. return USBD_OK;
  350. }
  351. /**
  352. * @brief Sets a Stall condition on an endpoint of the Low Level Driver.
  353. * @param pdev: Device handle
  354. * @param ep_addr: Endpoint Number
  355. * @retval USBD Status
  356. */
  357. USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  358. {
  359. HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
  360. return USBD_OK;
  361. }
  362. /**
  363. * @brief Clears a Stall condition on an endpoint of the Low Level Driver.
  364. * @param pdev: Device handle
  365. * @param ep_addr: Endpoint Number
  366. * @retval USBD Status
  367. */
  368. USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  369. {
  370. HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
  371. return USBD_OK;
  372. }
  373. /**
  374. * @brief Returns Stall condition.
  375. * @param pdev: Device handle
  376. * @param ep_addr: Endpoint Number
  377. * @retval Stall (1: Yes, 0: No)
  378. */
  379. uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  380. {
  381. PCD_HandleTypeDef *hpcd = pdev->pData;
  382. if((ep_addr & 0x80) == 0x80)
  383. {
  384. return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
  385. }
  386. else
  387. {
  388. return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
  389. }
  390. }
  391. /**
  392. * @brief Assigns a USB address to the device.
  393. * @param pdev: Device handle
  394. * @param ep_addr: Endpoint Number
  395. * @retval USBD Status
  396. */
  397. USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr)
  398. {
  399. HAL_PCD_SetAddress(pdev->pData, dev_addr);
  400. return USBD_OK;
  401. }
  402. /**
  403. * @brief Transmits data over an endpoint.
  404. * @param pdev: Device handle
  405. * @param ep_addr: Endpoint Number
  406. * @param pbuf: Pointer to data to be sent
  407. * @param size: Data size
  408. * @retval USBD Status
  409. */
  410. USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev,
  411. uint8_t ep_addr,
  412. uint8_t *pbuf,
  413. uint16_t size)
  414. {
  415. HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
  416. return USBD_OK;
  417. }
  418. /**
  419. * @brief Prepares an endpoint for reception.
  420. * @param pdev: Device handle
  421. * @param ep_addr: Endpoint Number
  422. * @param pbuf: Pointer to data to be received
  423. * @param size: Data size
  424. * @retval USBD Status
  425. */
  426. USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
  427. uint8_t ep_addr,
  428. uint8_t *pbuf,
  429. uint16_t size)
  430. {
  431. HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
  432. return USBD_OK;
  433. }
  434. /**
  435. * @brief Returns the last transfered packet size.
  436. * @param pdev: Device handle
  437. * @param ep_addr: Endpoint Number
  438. * @retval Recived Data Size
  439. */
  440. uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  441. {
  442. return HAL_PCD_EP_GetRxCount(pdev->pData, ep_addr);
  443. }
  444. /**
  445. * @brief Delays routine for the USB Device Library.
  446. * @param Delay: Delay in ms
  447. * @retval None
  448. */
  449. void USBD_LL_Delay (uint32_t Delay)
  450. {
  451. HAL_Delay(Delay);
  452. }
  453. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/