usbd_conf.c 20 KB

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