usbd_conf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : Target/usbd_conf.c
  5. * @version : v3.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 "stm32g4xx.h"
  23. #include "stm32g4xx_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. /* Private variables ---------------------------------------------------------*/
  33. /* USER CODE BEGIN PV */
  34. /* USER CODE END PV */
  35. PCD_HandleTypeDef hpcd_USB_FS;
  36. void Error_Handler(void);
  37. /* USER CODE BEGIN 0 */
  38. /* USER CODE END 0 */
  39. /* Exported function prototypes ----------------------------------------------*/
  40. /* USER CODE BEGIN PFP */
  41. /* Private function prototypes -----------------------------------------------*/
  42. /* USER CODE END PFP */
  43. /* Private functions ---------------------------------------------------------*/
  44. static USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status);
  45. /* USER CODE BEGIN 1 */
  46. static void SystemClockConfig_Resume(void);
  47. /* USER CODE END 1 */
  48. extern void SystemClock_Config(void);
  49. /*******************************************************************************
  50. LL Driver Callbacks (PCD -> USB Device Library)
  51. *******************************************************************************/
  52. /* MSP Init */
  53. #if (USE_HAL_PCD_REGISTER_CALLBACK == 1U)
  54. static void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
  55. #else
  56. void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
  57. #endif /* USE_HAL_PCD_REGISTER_CALLBACK */
  58. {
  59. if(pcdHandle->Instance==USB)
  60. {
  61. /* USER CODE BEGIN USB_MspInit 0 */
  62. /* USER CODE END USB_MspInit 0 */
  63. /* Peripheral clock enable */
  64. __HAL_RCC_USB_CLK_ENABLE();
  65. /* Peripheral interrupt init */
  66. HAL_NVIC_SetPriority(USB_LP_IRQn, 0, 0);
  67. HAL_NVIC_EnableIRQ(USB_LP_IRQn);
  68. /* USER CODE BEGIN USB_MspInit 1 */
  69. /* USER CODE END USB_MspInit 1 */
  70. }
  71. }
  72. #if (USE_HAL_PCD_REGISTER_CALLBACK == 1U)
  73. static void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
  74. #else
  75. void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
  76. #endif /* USE_HAL_PCD_REGISTER_CALLBACK */
  77. {
  78. if(pcdHandle->Instance==USB)
  79. {
  80. /* USER CODE BEGIN USB_MspDeInit 0 */
  81. /* USER CODE END USB_MspDeInit 0 */
  82. /* Peripheral clock disable */
  83. __HAL_RCC_USB_CLK_DISABLE();
  84. /* Peripheral interrupt Deinit*/
  85. HAL_NVIC_DisableIRQ(USB_LP_IRQn);
  86. /* USER CODE BEGIN USB_MspDeInit 1 */
  87. /* USER CODE END USB_MspDeInit 1 */
  88. }
  89. }
  90. /**
  91. * @brief Setup stage callback
  92. * @param hpcd: PCD handle
  93. * @retval None
  94. */
  95. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  96. static void PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  97. #else
  98. void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  99. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  100. {
  101. /* USER CODE BEGIN HAL_PCD_SetupStageCallback_PreTreatment */
  102. /* USER CODE END HAL_PCD_SetupStageCallback_PreTreatment */
  103. USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup);
  104. /* USER CODE BEGIN HAL_PCD_SetupStageCallback_PostTreatment */
  105. /* USER CODE END HAL_PCD_SetupStageCallback_PostTreatment */
  106. }
  107. /**
  108. * @brief Data Out stage callback.
  109. * @param hpcd: PCD handle
  110. * @param epnum: Endpoint number
  111. * @retval None
  112. */
  113. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  114. static void PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  115. #else
  116. void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  117. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  118. {
  119. /* USER CODE BEGIN HAL_PCD_DataOutStageCallback_PreTreatment */
  120. /* USER CODE END HAL_PCD_DataOutStageCallback_PreTreatment */
  121. USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
  122. /* USER CODE BEGIN HAL_PCD_DataOutStageCallback_PostTreatment */
  123. /* USER CODE END HAL_PCD_DataOutStageCallback_PostTreatment */
  124. }
  125. /**
  126. * @brief Data In 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_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  133. #else
  134. void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  135. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  136. {
  137. /* USER CODE BEGIN HAL_PCD_DataInStageCallback_PreTreatment */
  138. /* USER CODE END HAL_PCD_DataInStageCallback_PreTreatment */
  139. USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
  140. /* USER CODE BEGIN HAL_PCD_DataInStageCallback_PostTreatment */
  141. /* USER CODE END HAL_PCD_DataInStageCallback_PostTreatment */
  142. }
  143. /**
  144. * @brief SOF callback.
  145. * @param hpcd: PCD handle
  146. * @retval None
  147. */
  148. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  149. static void PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  150. #else
  151. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  152. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  153. {
  154. /* USER CODE BEGIN HAL_PCD_SOFCallback_PreTreatment */
  155. /* USER CODE END HAL_PCD_SOFCallback_PreTreatment */
  156. USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData);
  157. /* USER CODE BEGIN HAL_PCD_SOFCallback_PostTreatment */
  158. /* USER CODE END HAL_PCD_SOFCallback_PostTreatment */
  159. }
  160. /**
  161. * @brief Reset callback.
  162. * @param hpcd: PCD handle
  163. * @retval None
  164. */
  165. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  166. static void PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  167. #else
  168. void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  169. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  170. {
  171. /* USER CODE BEGIN HAL_PCD_ResetCallback_PreTreatment */
  172. /* USER CODE END HAL_PCD_ResetCallback_PreTreatment */
  173. USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
  174. if ( hpcd->Init.speed != PCD_SPEED_FULL)
  175. {
  176. Error_Handler();
  177. }
  178. /* Set Speed. */
  179. USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed);
  180. /* Reset Device. */
  181. USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData);
  182. /* USER CODE BEGIN HAL_PCD_ResetCallback_PostTreatment */
  183. /* USER CODE END HAL_PCD_ResetCallback_PostTreatment */
  184. }
  185. /**
  186. * @brief Suspend callback.
  187. * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  188. * @param hpcd: PCD handle
  189. * @retval None
  190. */
  191. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  192. static void PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  193. #else
  194. void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  195. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  196. {
  197. /* USER CODE BEGIN HAL_PCD_SuspendCallback_PreTreatment */
  198. /* USER CODE END HAL_PCD_SuspendCallback_PreTreatment */
  199. /* Inform USB library that core enters in suspend Mode. */
  200. USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
  201. /* Enter in STOP mode. */
  202. /* USER CODE BEGIN 2 */
  203. if (hpcd->Init.low_power_enable)
  204. {
  205. /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
  206. SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  207. }
  208. /* USER CODE END 2 */
  209. /* USER CODE BEGIN HAL_PCD_SuspendCallback_PostTreatment */
  210. /* USER CODE END HAL_PCD_SuspendCallback_PostTreatment */
  211. }
  212. /**
  213. * @brief Resume callback.
  214. * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  215. * @param hpcd: PCD handle
  216. * @retval None
  217. */
  218. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  219. static void PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  220. #else
  221. void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  222. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  223. {
  224. /* USER CODE BEGIN HAL_PCD_ResumeCallback_PreTreatment */
  225. /* USER CODE END HAL_PCD_ResumeCallback_PreTreatment */
  226. /* USER CODE BEGIN 3 */
  227. if (hpcd->Init.low_power_enable)
  228. {
  229. /* Reset SLEEPDEEP bit of Cortex System Control Register. */
  230. SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  231. SystemClockConfig_Resume();
  232. }
  233. /* USER CODE END 3 */
  234. USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData);
  235. /* USER CODE BEGIN HAL_PCD_ResumeCallback_PostTreatment */
  236. /* USER CODE END HAL_PCD_ResumeCallback_PostTreatment */
  237. }
  238. /**
  239. * @brief ISOOUTIncomplete callback.
  240. * @param hpcd: PCD handle
  241. * @param epnum: Endpoint number
  242. * @retval None
  243. */
  244. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  245. static void PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  246. #else
  247. void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  248. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  249. {
  250. /* USER CODE BEGIN HAL_PCD_ISOOUTIncompleteCallback_PreTreatment */
  251. /* USER CODE END HAL_PCD_ISOOUTIncompleteCallback_PreTreatment */
  252. USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
  253. /* USER CODE BEGIN HAL_PCD_ISOOUTIncompleteCallback_PostTreatment */
  254. /* USER CODE END HAL_PCD_ISOOUTIncompleteCallback_PostTreatment */
  255. }
  256. /**
  257. * @brief ISOINIncomplete callback.
  258. * @param hpcd: PCD handle
  259. * @param epnum: Endpoint number
  260. * @retval None
  261. */
  262. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  263. static void PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  264. #else
  265. void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  266. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  267. {
  268. /* USER CODE BEGIN HAL_PCD_ISOINIncompleteCallback_PreTreatment */
  269. /* USER CODE END HAL_PCD_ISOINIncompleteCallback_PreTreatment */
  270. USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
  271. /* USER CODE BEGIN HAL_PCD_ISOINIncompleteCallback_PostTreatment */
  272. /* USER CODE END HAL_PCD_ISOINIncompleteCallback_PostTreatment */
  273. }
  274. /**
  275. * @brief Connect callback.
  276. * @param hpcd: PCD handle
  277. * @retval None
  278. */
  279. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  280. static void PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  281. #else
  282. void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  283. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  284. {
  285. /* USER CODE BEGIN HAL_PCD_ConnectCallback_PreTreatment */
  286. /* USER CODE END HAL_PCD_ConnectCallback_PreTreatment */
  287. USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData);
  288. /* USER CODE BEGIN HAL_PCD_ConnectCallback_PostTreatment */
  289. /* USER CODE END HAL_PCD_ConnectCallback_PostTreatment */
  290. }
  291. /**
  292. * @brief Disconnect callback.
  293. * @param hpcd: PCD handle
  294. * @retval None
  295. */
  296. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  297. static void PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  298. #else
  299. void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  300. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  301. {
  302. /* USER CODE BEGIN HAL_PCD_DisconnectCallback_PreTreatment */
  303. /* USER CODE END HAL_PCD_DisconnectCallback_PreTreatment */
  304. USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData);
  305. /* USER CODE BEGIN HAL_PCD_DisconnectCallback_PostTreatment */
  306. /* USER CODE END HAL_PCD_DisconnectCallback_PostTreatment */
  307. }
  308. /* USER CODE BEGIN LowLevelInterface */
  309. /* USER CODE END LowLevelInterface */
  310. /*******************************************************************************
  311. LL Driver Interface (USB Device Library --> PCD)
  312. *******************************************************************************/
  313. /**
  314. * @brief Initializes the low level portion of the device driver.
  315. * @param pdev: Device handle
  316. * @retval USBD status
  317. */
  318. USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
  319. {
  320. /* Init USB Ip. */
  321. hpcd_USB_FS.pData = pdev;
  322. /* Link the driver to the stack. */
  323. pdev->pData = &hpcd_USB_FS;
  324. hpcd_USB_FS.Instance = USB;
  325. hpcd_USB_FS.Init.dev_endpoints = 8;
  326. hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
  327. hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  328. hpcd_USB_FS.Init.Sof_enable = DISABLE;
  329. hpcd_USB_FS.Init.low_power_enable = DISABLE;
  330. hpcd_USB_FS.Init.lpm_enable = DISABLE;
  331. hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
  332. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  333. /* register Msp Callbacks (before the Init) */
  334. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_MSPINIT_CB_ID, PCD_MspInit);
  335. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_MSPDEINIT_CB_ID, PCD_MspDeInit);
  336. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  337. if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
  338. {
  339. Error_Handler( );
  340. }
  341. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  342. /* Register USB PCD CallBacks */
  343. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_SOF_CB_ID, PCD_SOFCallback);
  344. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_SETUPSTAGE_CB_ID, PCD_SetupStageCallback);
  345. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_RESET_CB_ID, PCD_ResetCallback);
  346. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_SUSPEND_CB_ID, PCD_SuspendCallback);
  347. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_RESUME_CB_ID, PCD_ResumeCallback);
  348. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_CONNECT_CB_ID, PCD_ConnectCallback);
  349. HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_DISCONNECT_CB_ID, PCD_DisconnectCallback);
  350. /* USER CODE BEGIN RegisterCallBackFirstPart */
  351. /* USER CODE END RegisterCallBackFirstPart */
  352. HAL_PCD_RegisterLpmCallback(&hpcd_USB_FS, PCDEx_LPM_Callback);
  353. HAL_PCD_RegisterDataOutStageCallback(&hpcd_USB_FS, PCD_DataOutStageCallback);
  354. HAL_PCD_RegisterDataInStageCallback(&hpcd_USB_FS, PCD_DataInStageCallback);
  355. HAL_PCD_RegisterIsoOutIncpltCallback(&hpcd_USB_FS, PCD_ISOOUTIncompleteCallback);
  356. HAL_PCD_RegisterIsoInIncpltCallback(&hpcd_USB_FS, PCD_ISOINIncompleteCallback);
  357. /* USER CODE BEGIN RegisterCallBackSecondPart */
  358. /* USER CODE END RegisterCallBackSecondPart */
  359. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  360. /* USER CODE BEGIN EndPoint_Configuration */
  361. HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18);
  362. HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58);
  363. /* USER CODE END EndPoint_Configuration */
  364. /* USER CODE BEGIN EndPoint_Configuration_CDC */
  365. HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0xC0);
  366. HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x110);
  367. HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x100);
  368. /* USER CODE END EndPoint_Configuration_CDC */
  369. return USBD_OK;
  370. }
  371. /**
  372. * @brief De-Initializes the low level portion of the device driver.
  373. * @param pdev: Device handle
  374. * @retval USBD status
  375. */
  376. USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
  377. {
  378. HAL_StatusTypeDef hal_status = HAL_OK;
  379. USBD_StatusTypeDef usb_status = USBD_OK;
  380. hal_status = HAL_PCD_DeInit(pdev->pData);
  381. usb_status = USBD_Get_USB_Status(hal_status);
  382. return usb_status;
  383. }
  384. /**
  385. * @brief Starts the low level portion of the device driver.
  386. * @param pdev: Device handle
  387. * @retval USBD status
  388. */
  389. USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
  390. {
  391. HAL_StatusTypeDef hal_status = HAL_OK;
  392. USBD_StatusTypeDef usb_status = USBD_OK;
  393. hal_status = HAL_PCD_Start(pdev->pData);
  394. usb_status = USBD_Get_USB_Status(hal_status);
  395. return usb_status;
  396. }
  397. /**
  398. * @brief Stops the low level portion of the device driver.
  399. * @param pdev: Device handle
  400. * @retval USBD status
  401. */
  402. USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev)
  403. {
  404. HAL_StatusTypeDef hal_status = HAL_OK;
  405. USBD_StatusTypeDef usb_status = USBD_OK;
  406. hal_status = HAL_PCD_Stop(pdev->pData);
  407. usb_status = USBD_Get_USB_Status(hal_status);
  408. return usb_status;
  409. }
  410. /**
  411. * @brief Opens an endpoint of the low level driver.
  412. * @param pdev: Device handle
  413. * @param ep_addr: Endpoint number
  414. * @param ep_type: Endpoint type
  415. * @param ep_mps: Endpoint max packet size
  416. * @retval USBD status
  417. */
  418. USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps)
  419. {
  420. HAL_StatusTypeDef hal_status = HAL_OK;
  421. USBD_StatusTypeDef usb_status = USBD_OK;
  422. hal_status = HAL_PCD_EP_Open(pdev->pData, ep_addr, ep_mps, ep_type);
  423. usb_status = USBD_Get_USB_Status(hal_status);
  424. return usb_status;
  425. }
  426. /**
  427. * @brief Closes an endpoint of the low level driver.
  428. * @param pdev: Device handle
  429. * @param ep_addr: Endpoint number
  430. * @retval USBD status
  431. */
  432. USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  433. {
  434. HAL_StatusTypeDef hal_status = HAL_OK;
  435. USBD_StatusTypeDef usb_status = USBD_OK;
  436. hal_status = HAL_PCD_EP_Close(pdev->pData, ep_addr);
  437. usb_status = USBD_Get_USB_Status(hal_status);
  438. return usb_status;
  439. }
  440. /**
  441. * @brief Flushes an endpoint of the Low Level Driver.
  442. * @param pdev: Device handle
  443. * @param ep_addr: Endpoint number
  444. * @retval USBD status
  445. */
  446. USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  447. {
  448. HAL_StatusTypeDef hal_status = HAL_OK;
  449. USBD_StatusTypeDef usb_status = USBD_OK;
  450. hal_status = HAL_PCD_EP_Flush(pdev->pData, ep_addr);
  451. usb_status = USBD_Get_USB_Status(hal_status);
  452. return usb_status;
  453. }
  454. /**
  455. * @brief Sets a Stall condition on an endpoint of the Low Level Driver.
  456. * @param pdev: Device handle
  457. * @param ep_addr: Endpoint number
  458. * @retval USBD status
  459. */
  460. USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  461. {
  462. HAL_StatusTypeDef hal_status = HAL_OK;
  463. USBD_StatusTypeDef usb_status = USBD_OK;
  464. hal_status = HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
  465. usb_status = USBD_Get_USB_Status(hal_status);
  466. return usb_status;
  467. }
  468. /**
  469. * @brief Clears a Stall condition on an endpoint of the Low Level Driver.
  470. * @param pdev: Device handle
  471. * @param ep_addr: Endpoint number
  472. * @retval USBD status
  473. */
  474. USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  475. {
  476. HAL_StatusTypeDef hal_status = HAL_OK;
  477. USBD_StatusTypeDef usb_status = USBD_OK;
  478. hal_status = HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
  479. usb_status = USBD_Get_USB_Status(hal_status);
  480. return usb_status;
  481. }
  482. /**
  483. * @brief Returns Stall condition.
  484. * @param pdev: Device handle
  485. * @param ep_addr: Endpoint number
  486. * @retval Stall (1: Yes, 0: No)
  487. */
  488. uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  489. {
  490. PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData;
  491. if((ep_addr & 0x80) == 0x80)
  492. {
  493. return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
  494. }
  495. else
  496. {
  497. return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
  498. }
  499. }
  500. /**
  501. * @brief Assigns a USB address to the device.
  502. * @param pdev: Device handle
  503. * @param dev_addr: Device address
  504. * @retval USBD status
  505. */
  506. USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr)
  507. {
  508. HAL_StatusTypeDef hal_status = HAL_OK;
  509. USBD_StatusTypeDef usb_status = USBD_OK;
  510. hal_status = HAL_PCD_SetAddress(pdev->pData, dev_addr);
  511. usb_status = USBD_Get_USB_Status(hal_status);
  512. return usb_status;
  513. }
  514. /**
  515. * @brief Transmits data over an endpoint.
  516. * @param pdev: Device handle
  517. * @param ep_addr: Endpoint number
  518. * @param pbuf: Pointer to data to be sent
  519. * @param size: Data size
  520. * @retval USBD status
  521. */
  522. USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size)
  523. {
  524. HAL_StatusTypeDef hal_status = HAL_OK;
  525. USBD_StatusTypeDef usb_status = USBD_OK;
  526. hal_status = HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
  527. usb_status = USBD_Get_USB_Status(hal_status);
  528. return usb_status;
  529. }
  530. /**
  531. * @brief Prepares an endpoint for reception.
  532. * @param pdev: Device handle
  533. * @param ep_addr: Endpoint number
  534. * @param pbuf: Pointer to data to be received
  535. * @param size: Data size
  536. * @retval USBD status
  537. */
  538. USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size)
  539. {
  540. HAL_StatusTypeDef hal_status = HAL_OK;
  541. USBD_StatusTypeDef usb_status = USBD_OK;
  542. hal_status = HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
  543. usb_status = USBD_Get_USB_Status(hal_status);
  544. return usb_status;
  545. }
  546. /**
  547. * @brief Returns the last transferred packet size.
  548. * @param pdev: Device handle
  549. * @param ep_addr: Endpoint number
  550. * @retval Received Data Size
  551. */
  552. uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
  553. {
  554. return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr);
  555. }
  556. /**
  557. * @brief Send LPM message to user layer
  558. * @param hpcd: PCD handle
  559. * @param msg: LPM message
  560. * @retval None
  561. */
  562. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  563. static void PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  564. #else
  565. void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  566. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  567. {
  568. /* USER CODE BEGIN LPM_Callback */
  569. switch (msg)
  570. {
  571. case PCD_LPM_L0_ACTIVE:
  572. if (hpcd->Init.low_power_enable)
  573. {
  574. SystemClockConfig_Resume();
  575. /* Reset SLEEPDEEP bit of Cortex System Control Register. */
  576. SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  577. }
  578. USBD_LL_Resume(hpcd->pData);
  579. break;
  580. case PCD_LPM_L1_ACTIVE:
  581. USBD_LL_Suspend(hpcd->pData);
  582. /* Enter in STOP mode. */
  583. if (hpcd->Init.low_power_enable)
  584. {
  585. /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
  586. SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  587. }
  588. break;
  589. }
  590. /* USER CODE END LPM_Callback */
  591. }
  592. /**
  593. * @brief Delays routine for the USB Device Library.
  594. * @param Delay: Delay in ms
  595. * @retval None
  596. */
  597. void USBD_LL_Delay(uint32_t Delay)
  598. {
  599. HAL_Delay(Delay);
  600. }
  601. /**
  602. * @brief Static single allocation.
  603. * @param size: Size of allocated memory
  604. * @retval None
  605. */
  606. void *USBD_static_malloc(uint32_t size)
  607. {
  608. static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */
  609. return mem;
  610. }
  611. /**
  612. * @brief Dummy memory free
  613. * @param p: Pointer to allocated memory address
  614. * @retval None
  615. */
  616. void USBD_static_free(void *p)
  617. {
  618. }
  619. /* USER CODE BEGIN 5 */
  620. /**
  621. * @brief Configures system clock after wake-up from USB resume callBack:
  622. * enable HSI, PLL and select PLL as system clock source.
  623. * @retval None
  624. */
  625. static void SystemClockConfig_Resume(void)
  626. {
  627. SystemClock_Config();
  628. }
  629. /* USER CODE END 5 */
  630. /**
  631. * @brief Returns the USB status depending on the HAL status:
  632. * @param hal_status: HAL status
  633. * @retval USB status
  634. */
  635. USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status)
  636. {
  637. USBD_StatusTypeDef usb_status = USBD_OK;
  638. switch (hal_status)
  639. {
  640. case HAL_OK :
  641. usb_status = USBD_OK;
  642. break;
  643. case HAL_ERROR :
  644. usb_status = USBD_FAIL;
  645. break;
  646. case HAL_BUSY :
  647. usb_status = USBD_BUSY;
  648. break;
  649. case HAL_TIMEOUT :
  650. usb_status = USBD_FAIL;
  651. break;
  652. default :
  653. usb_status = USBD_FAIL;
  654. break;
  655. }
  656. return usb_status;
  657. }
  658. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/