usbd_conf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : Target/usbd_conf.c
  5. * @version : v1.0_Cube
  6. * @brief : This file implements the board support package for the USB device library
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under Ultimate Liberty license
  14. * SLA0044, the "License"; You may not use this file except in compliance with
  15. * the License. You may obtain a copy of the License at:
  16. * www.st.com/SLA0044
  17. *
  18. ******************************************************************************
  19. */
  20. /* USER CODE END Header */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f4xx.h"
  23. #include "stm32f4xx_hal.h"
  24. #include "usbd_def.h"
  25. #include "usbd_core.h"
  26. #include "usbd_cdc.h"
  27. /* USER CODE BEGIN Includes */
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* Private define ------------------------------------------------------------*/
  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN PV */
  33. /* Private variables ---------------------------------------------------------*/
  34. /* USER CODE END PV */
  35. PCD_HandleTypeDef hpcd_USB_OTG_FS;
  36. void Error_Handler(void);
  37. /* External functions --------------------------------------------------------*/
  38. void SystemClock_Config(void);
  39. /* USER CODE BEGIN 0 */
  40. /* USER CODE END 0 */
  41. /* USER CODE BEGIN PFP */
  42. /* Private function prototypes -----------------------------------------------*/
  43. USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status);
  44. /* USER CODE END PFP */
  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* pcdHandle)
  53. {
  54. GPIO_InitTypeDef GPIO_InitStruct = {0};
  55. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  56. if(pcdHandle->Instance==USB_OTG_FS)
  57. {
  58. /* USER CODE BEGIN USB_OTG_FS_MspInit 0 */
  59. /* USER CODE END USB_OTG_FS_MspInit 0 */
  60. /** Initializes the peripherals clock
  61. */
  62. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CLK48;
  63. PeriphClkInitStruct.PLLSAI.PLLSAIM = 8;
  64. PeriphClkInitStruct.PLLSAI.PLLSAIN = 192;
  65. PeriphClkInitStruct.PLLSAI.PLLSAIQ = 2;
  66. PeriphClkInitStruct.PLLSAI.PLLSAIP = RCC_PLLSAIP_DIV4;
  67. PeriphClkInitStruct.PLLSAIDivQ = 1;
  68. PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48CLKSOURCE_PLLSAIP;
  69. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  70. {
  71. Error_Handler();
  72. }
  73. __HAL_RCC_GPIOA_CLK_ENABLE();
  74. /**USB_OTG_FS GPIO Configuration
  75. PA11 ------> USB_OTG_FS_DM
  76. PA12 ------> USB_OTG_FS_DP
  77. */
  78. GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
  79. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  80. GPIO_InitStruct.Pull = GPIO_NOPULL;
  81. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  82. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
  83. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  84. /* Peripheral clock enable */
  85. __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
  86. /* Peripheral interrupt init */
  87. HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
  88. HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
  89. /* USER CODE BEGIN USB_OTG_FS_MspInit 1 */
  90. /* USER CODE END USB_OTG_FS_MspInit 1 */
  91. }
  92. }
  93. void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
  94. {
  95. if(pcdHandle->Instance==USB_OTG_FS)
  96. {
  97. /* USER CODE BEGIN USB_OTG_FS_MspDeInit 0 */
  98. /* USER CODE END USB_OTG_FS_MspDeInit 0 */
  99. /* Peripheral clock disable */
  100. __HAL_RCC_USB_OTG_FS_CLK_DISABLE();
  101. /**USB_OTG_FS GPIO Configuration
  102. PA11 ------> USB_OTG_FS_DM
  103. PA12 ------> USB_OTG_FS_DP
  104. */
  105. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
  106. /* Peripheral interrupt Deinit*/
  107. HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
  108. /* USER CODE BEGIN USB_OTG_FS_MspDeInit 1 */
  109. /* USER CODE END USB_OTG_FS_MspDeInit 1 */
  110. }
  111. }
  112. /**
  113. * @brief Setup stage callback
  114. * @param hpcd: PCD handle
  115. * @retval None
  116. */
  117. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  118. static void PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  119. #else
  120. void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  121. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  122. {
  123. USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup);
  124. }
  125. /**
  126. * @brief Data Out stage callback.
  127. * @param hpcd: PCD handle
  128. * @param epnum: Endpoint number
  129. * @retval None
  130. */
  131. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  132. static void PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  133. #else
  134. void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  135. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  136. {
  137. USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
  138. }
  139. /**
  140. * @brief Data In stage callback.
  141. * @param hpcd: PCD handle
  142. * @param epnum: Endpoint number
  143. * @retval None
  144. */
  145. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  146. static void PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  147. #else
  148. void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  149. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  150. {
  151. USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
  152. }
  153. /**
  154. * @brief SOF callback.
  155. * @param hpcd: PCD handle
  156. * @retval None
  157. */
  158. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  159. static void PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  160. #else
  161. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  162. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  163. {
  164. USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData);
  165. }
  166. /**
  167. * @brief Reset callback.
  168. * @param hpcd: PCD handle
  169. * @retval None
  170. */
  171. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  172. static void PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  173. #else
  174. void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  175. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  176. {
  177. USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
  178. if ( hpcd->Init.speed == PCD_SPEED_HIGH)
  179. {
  180. speed = USBD_SPEED_HIGH;
  181. }
  182. else if ( hpcd->Init.speed == PCD_SPEED_FULL)
  183. {
  184. speed = USBD_SPEED_FULL;
  185. }
  186. else
  187. {
  188. Error_Handler();
  189. }
  190. /* Set Speed. */
  191. USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed);
  192. /* Reset Device. */
  193. USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData);
  194. }
  195. /**
  196. * @brief Suspend callback.
  197. * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  198. * @param hpcd: PCD handle
  199. * @retval None
  200. */
  201. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  202. static void PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  203. #else
  204. void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  205. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  206. {
  207. /* Inform USB library that core enters in suspend Mode. */
  208. USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
  209. __HAL_PCD_GATE_PHYCLOCK(hpcd);
  210. /* Enter in STOP mode. */
  211. /* USER CODE BEGIN 2 */
  212. if (hpcd->Init.low_power_enable)
  213. {
  214. /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
  215. SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  216. }
  217. /* USER CODE END 2 */
  218. }
  219. /**
  220. * @brief Resume callback.
  221. * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  222. * @param hpcd: PCD handle
  223. * @retval None
  224. */
  225. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  226. static void PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  227. #else
  228. void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  229. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  230. {
  231. /* USER CODE BEGIN 3 */
  232. /* USER CODE END 3 */
  233. USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData);
  234. }
  235. /**
  236. * @brief ISOOUTIncomplete callback.
  237. * @param hpcd: PCD handle
  238. * @param epnum: Endpoint number
  239. * @retval None
  240. */
  241. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  242. static void PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  243. #else
  244. void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  245. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  246. {
  247. USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
  248. }
  249. /**
  250. * @brief ISOINIncomplete callback.
  251. * @param hpcd: PCD handle
  252. * @param epnum: Endpoint number
  253. * @retval None
  254. */
  255. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  256. static void PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  257. #else
  258. void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  259. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  260. {
  261. USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
  262. }
  263. /**
  264. * @brief Connect callback.
  265. * @param hpcd: PCD handle
  266. * @retval None
  267. */
  268. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  269. static void PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  270. #else
  271. void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  272. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  273. {
  274. USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData);
  275. }
  276. /**
  277. * @brief Disconnect callback.
  278. * @param hpcd: PCD handle
  279. * @retval None
  280. */
  281. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  282. static void PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  283. #else
  284. void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  285. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  286. {
  287. USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData);
  288. }
  289. /*******************************************************************************
  290. LL Driver Interface (USB Device Library --> PCD)
  291. *******************************************************************************/
  292. /**
  293. * @brief Initializes the low level portion of the device driver.
  294. * @param pdev: Device handle
  295. * @retval USBD status
  296. */
  297. USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
  298. {
  299. /* Init USB Ip. */
  300. if (pdev->id == DEVICE_FS) {
  301. /* Link the driver to the stack. */
  302. hpcd_USB_OTG_FS.pData = pdev;
  303. pdev->pData = &hpcd_USB_OTG_FS;
  304. hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  305. hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
  306. hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
  307. hpcd_USB_OTG_FS.Init.dma_enable = DISABLE;
  308. hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  309. hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
  310. hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  311. hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  312. hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
  313. hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  314. if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  315. {
  316. Error_Handler( );
  317. }
  318. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  319. /* Register USB PCD CallBacks */
  320. HAL_PCD_RegisterCallback(&hpcd_USB_OTG_FS, HAL_PCD_SOF_CB_ID, PCD_SOFCallback);
  321. HAL_PCD_RegisterCallback(&hpcd_USB_OTG_FS, HAL_PCD_SETUPSTAGE_CB_ID, PCD_SetupStageCallback);
  322. HAL_PCD_RegisterCallback(&hpcd_USB_OTG_FS, HAL_PCD_RESET_CB_ID, PCD_ResetCallback);
  323. HAL_PCD_RegisterCallback(&hpcd_USB_OTG_FS, HAL_PCD_SUSPEND_CB_ID, PCD_SuspendCallback);
  324. HAL_PCD_RegisterCallback(&hpcd_USB_OTG_FS, HAL_PCD_RESUME_CB_ID, PCD_ResumeCallback);
  325. HAL_PCD_RegisterCallback(&hpcd_USB_OTG_FS, HAL_PCD_CONNECT_CB_ID, PCD_ConnectCallback);
  326. HAL_PCD_RegisterCallback(&hpcd_USB_OTG_FS, HAL_PCD_DISCONNECT_CB_ID, PCD_DisconnectCallback);
  327. HAL_PCD_RegisterDataOutStageCallback(&hpcd_USB_OTG_FS, PCD_DataOutStageCallback);
  328. HAL_PCD_RegisterDataInStageCallback(&hpcd_USB_OTG_FS, PCD_DataInStageCallback);
  329. HAL_PCD_RegisterIsoOutIncpltCallback(&hpcd_USB_OTG_FS, PCD_ISOOUTIncompleteCallback);
  330. HAL_PCD_RegisterIsoInIncpltCallback(&hpcd_USB_OTG_FS, PCD_ISOINIncompleteCallback);
  331. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  332. HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
  333. HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 0, 0x40);
  334. HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x80);
  335. }
  336. return USBD_OK;
  337. }
  338. /**
  339. * @brief De-Initializes the low level portion of the device driver.
  340. * @param pdev: Device handle
  341. * @retval USBD status
  342. */
  343. USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
  344. {
  345. HAL_StatusTypeDef hal_status = HAL_OK;
  346. USBD_StatusTypeDef usb_status = USBD_OK;
  347. hal_status = HAL_PCD_DeInit(pdev->pData);
  348. usb_status = USBD_Get_USB_Status(hal_status);
  349. return usb_status;
  350. }
  351. /**
  352. * @brief Starts the low level portion of the device driver.
  353. * @param pdev: Device handle
  354. * @retval USBD status
  355. */
  356. USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
  357. {
  358. HAL_StatusTypeDef hal_status = HAL_OK;
  359. USBD_StatusTypeDef usb_status = USBD_OK;
  360. hal_status = HAL_PCD_Start(pdev->pData);
  361. usb_status = USBD_Get_USB_Status(hal_status);
  362. return usb_status;
  363. }
  364. /**
  365. * @brief Stops the low level portion of the device driver.
  366. * @param pdev: Device handle
  367. * @retval USBD status
  368. */
  369. USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev)
  370. {
  371. HAL_StatusTypeDef hal_status = HAL_OK;
  372. USBD_StatusTypeDef usb_status = USBD_OK;
  373. hal_status = HAL_PCD_Stop(pdev->pData);
  374. usb_status = USBD_Get_USB_Status(hal_status);
  375. return usb_status;
  376. }
  377. /**
  378. * @brief Opens an endpoint of the low level driver.
  379. * @param pdev: Device handle
  380. * @param ep_addr: Endpoint number
  381. * @param ep_type: Endpoint type
  382. * @param ep_mps: Endpoint max packet size
  383. * @retval USBD status
  384. */
  385. USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps)
  386. {
  387. HAL_StatusTypeDef hal_status = HAL_OK;
  388. USBD_StatusTypeDef usb_status = USBD_OK;
  389. hal_status = HAL_PCD_EP_Open(pdev->pData, ep_addr, ep_mps, ep_type);
  390. usb_status = USBD_Get_USB_Status(hal_status);
  391. return usb_status;
  392. }
  393. /**
  394. * @brief Closes an endpoint of the low level driver.
  395. * @param pdev: Device handle
  396. * @param ep_addr: Endpoint number
  397. * @retval USBD status
  398. */
  399. USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  400. {
  401. HAL_StatusTypeDef hal_status = HAL_OK;
  402. USBD_StatusTypeDef usb_status = USBD_OK;
  403. hal_status = HAL_PCD_EP_Close(pdev->pData, ep_addr);
  404. usb_status = USBD_Get_USB_Status(hal_status);
  405. return usb_status;
  406. }
  407. /**
  408. * @brief Flushes an endpoint of the Low Level Driver.
  409. * @param pdev: Device handle
  410. * @param ep_addr: Endpoint number
  411. * @retval USBD status
  412. */
  413. USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  414. {
  415. HAL_StatusTypeDef hal_status = HAL_OK;
  416. USBD_StatusTypeDef usb_status = USBD_OK;
  417. hal_status = HAL_PCD_EP_Flush(pdev->pData, ep_addr);
  418. usb_status = USBD_Get_USB_Status(hal_status);
  419. return usb_status;
  420. }
  421. /**
  422. * @brief Sets a Stall condition on an endpoint of the Low Level Driver.
  423. * @param pdev: Device handle
  424. * @param ep_addr: Endpoint number
  425. * @retval USBD status
  426. */
  427. USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  428. {
  429. HAL_StatusTypeDef hal_status = HAL_OK;
  430. USBD_StatusTypeDef usb_status = USBD_OK;
  431. hal_status = HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
  432. usb_status = USBD_Get_USB_Status(hal_status);
  433. return usb_status;
  434. }
  435. /**
  436. * @brief Clears a Stall condition on an endpoint of the Low Level Driver.
  437. * @param pdev: Device handle
  438. * @param ep_addr: Endpoint number
  439. * @retval USBD status
  440. */
  441. USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  442. {
  443. HAL_StatusTypeDef hal_status = HAL_OK;
  444. USBD_StatusTypeDef usb_status = USBD_OK;
  445. hal_status = HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
  446. usb_status = USBD_Get_USB_Status(hal_status);
  447. return usb_status;
  448. }
  449. /**
  450. * @brief Returns Stall condition.
  451. * @param pdev: Device handle
  452. * @param ep_addr: Endpoint number
  453. * @retval Stall (1: Yes, 0: No)
  454. */
  455. uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  456. {
  457. PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData;
  458. if((ep_addr & 0x80) == 0x80)
  459. {
  460. return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
  461. }
  462. else
  463. {
  464. return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
  465. }
  466. }
  467. /**
  468. * @brief Assigns a USB address to the device.
  469. * @param pdev: Device handle
  470. * @param dev_addr: Device address
  471. * @retval USBD status
  472. */
  473. USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr)
  474. {
  475. HAL_StatusTypeDef hal_status = HAL_OK;
  476. USBD_StatusTypeDef usb_status = USBD_OK;
  477. hal_status = HAL_PCD_SetAddress(pdev->pData, dev_addr);
  478. usb_status = USBD_Get_USB_Status(hal_status);
  479. return usb_status;
  480. }
  481. /**
  482. * @brief Transmits data over an endpoint.
  483. * @param pdev: Device handle
  484. * @param ep_addr: Endpoint number
  485. * @param pbuf: Pointer to data to be sent
  486. * @param size: Data size
  487. * @retval USBD status
  488. */
  489. USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size)
  490. {
  491. HAL_StatusTypeDef hal_status = HAL_OK;
  492. USBD_StatusTypeDef usb_status = USBD_OK;
  493. hal_status = HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
  494. usb_status = USBD_Get_USB_Status(hal_status);
  495. return usb_status;
  496. }
  497. /**
  498. * @brief Prepares an endpoint for reception.
  499. * @param pdev: Device handle
  500. * @param ep_addr: Endpoint number
  501. * @param pbuf: Pointer to data to be received
  502. * @param size: Data size
  503. * @retval USBD status
  504. */
  505. USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size)
  506. {
  507. HAL_StatusTypeDef hal_status = HAL_OK;
  508. USBD_StatusTypeDef usb_status = USBD_OK;
  509. hal_status = HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
  510. usb_status = USBD_Get_USB_Status(hal_status);
  511. return usb_status;
  512. }
  513. /**
  514. * @brief Returns the last transferred packet size.
  515. * @param pdev: Device handle
  516. * @param ep_addr: Endpoint number
  517. * @retval Received Data Size
  518. */
  519. uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  520. {
  521. return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr);
  522. }
  523. /**
  524. * @brief Send LPM message to user layer
  525. * @param hpcd: PCD handle
  526. * @param msg: LPM message
  527. * @retval None
  528. */
  529. void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  530. {
  531. switch (msg)
  532. {
  533. case PCD_LPM_L0_ACTIVE:
  534. if (hpcd->Init.low_power_enable)
  535. {
  536. SystemClock_Config();
  537. /* Reset SLEEPDEEP bit of Cortex System Control Register. */
  538. SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  539. }
  540. __HAL_PCD_UNGATE_PHYCLOCK(hpcd);
  541. USBD_LL_Resume(hpcd->pData);
  542. break;
  543. case PCD_LPM_L1_ACTIVE:
  544. __HAL_PCD_GATE_PHYCLOCK(hpcd);
  545. USBD_LL_Suspend(hpcd->pData);
  546. /* Enter in STOP mode. */
  547. if (hpcd->Init.low_power_enable)
  548. {
  549. /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
  550. SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  551. }
  552. break;
  553. }
  554. }
  555. /**
  556. * @brief Static single allocation.
  557. * @param size: Size of allocated memory
  558. * @retval None
  559. */
  560. void *USBD_static_malloc(uint32_t size)
  561. {
  562. static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */
  563. return mem;
  564. }
  565. /**
  566. * @brief Dummy memory free
  567. * @param p: Pointer to allocated memory address
  568. * @retval None
  569. */
  570. void USBD_static_free(void *p)
  571. {
  572. }
  573. /**
  574. * @brief Delays routine for the USB Device Library.
  575. * @param Delay: Delay in ms
  576. * @retval None
  577. */
  578. void USBD_LL_Delay(uint32_t Delay)
  579. {
  580. HAL_Delay(Delay);
  581. }
  582. /**
  583. * @brief Returns the USB status depending on the HAL status:
  584. * @param hal_status: HAL status
  585. * @retval USB status
  586. */
  587. USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status)
  588. {
  589. USBD_StatusTypeDef usb_status = USBD_OK;
  590. switch (hal_status)
  591. {
  592. case HAL_OK :
  593. usb_status = USBD_OK;
  594. break;
  595. case HAL_ERROR :
  596. usb_status = USBD_FAIL;
  597. break;
  598. case HAL_BUSY :
  599. usb_status = USBD_BUSY;
  600. break;
  601. case HAL_TIMEOUT :
  602. usb_status = USBD_FAIL;
  603. break;
  604. default :
  605. usb_status = USBD_FAIL;
  606. break;
  607. }
  608. return usb_status;
  609. }
  610. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/