usbd_conf.c 19 KB

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