Merge pull request #11299 from daleckystepan/vtx-start-bit
[betaflight.git] / src / main / vcp_hal / usbd_conf_stm32g4xx.c
blob3e139a813e08c9307e3b75edf41db6cf25ed7e2e
1 /* USER CODE BEGIN Header */
2 /**
3 ******************************************************************************
4 * @file USB_Device/CDC_Standalone/USB_Device/Target/usbd_conf.c
5 * @author MCD Application Team
6 * @brief This file implements the board support package for the USB device library
7 ******************************************************************************
8 * @attention
10 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
11 * All rights reserved.</center></h2>
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
18 ******************************************************************************
20 /* USER CODE END Header */
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32g4xx.h"
24 #include "stm32g4xx_hal.h"
25 #include "usbd_def.h"
26 #include "usbd_core.h"
28 #include "usbd_cdc.h"
30 /* USER CODE BEGIN Includes */
32 #include "platform.h"
34 /* USER CODE END Includes */
36 /* Private typedef -----------------------------------------------------------*/
37 /* Private define ------------------------------------------------------------*/
38 /* Private macro -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* USER CODE BEGIN PV */
42 /* Private variables ---------------------------------------------------------*/
44 /* USER CODE END PV */
46 PCD_HandleTypeDef hpcd_USB_FS;
47 void Error_Handler(void);
49 /* USER CODE BEGIN 0 */
51 /* USER CODE END 0 */
53 /* Exported function prototypes ----------------------------------------------*/
55 /* USER CODE BEGIN PFP */
56 /* Private function prototypes -----------------------------------------------*/
58 /* USER CODE END PFP */
60 /* Private functions ---------------------------------------------------------*/
61 static USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status);
62 /* USER CODE BEGIN 1 */
63 static void SystemClockConfig_Resume(void);
64 extern void USBD_Clock_Config(void);
65 /* USER CODE END 1 */
66 extern void SystemClock_Config(void);
68 void USB_LP_IRQHandler(void)
70 HAL_PCD_IRQHandler(&hpcd_USB_FS);
73 /*******************************************************************************
74 LL Driver Callbacks (PCD -> USB Device Library)
75 *******************************************************************************/
76 /* MSP Init */
78 #if (USE_HAL_PCD_REGISTER_CALLBACK == 1U)
79 static void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
80 #else
81 void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
82 #endif /* USE_HAL_PCD_REGISTER_CALLBACK */
84 GPIO_InitTypeDef GPIO_InitStruct = {0};
85 if(pcdHandle->Instance==USB)
87 /* USER CODE BEGIN USB_MspInit 0 */
89 /* USER CODE END USB_MspInit 0 */
91 __HAL_RCC_GPIOA_CLK_ENABLE();
92 /**USB GPIO Configuration
93 PA11 ------> USB_DM
94 PA12 ------> USB_DP
96 GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
97 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
98 GPIO_InitStruct.Pull = GPIO_NOPULL;
99 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
100 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
102 /* Peripheral clock enable */
103 __HAL_RCC_USB_CLK_ENABLE();
105 /* Peripheral interrupt init */
106 HAL_NVIC_SetPriority(USB_LP_IRQn, 6, 0);
107 HAL_NVIC_EnableIRQ(USB_LP_IRQn);
108 /* USER CODE BEGIN USB_MspInit 1 */
110 /* USER CODE END USB_MspInit 1 */
114 #if (USE_HAL_PCD_REGISTER_CALLBACK == 1U)
115 static void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
116 #else
117 void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
118 #endif /* USE_HAL_PCD_REGISTER_CALLBACK */
120 if(pcdHandle->Instance==USB)
122 /* USER CODE BEGIN USB_MspDeInit 0 */
124 /* USER CODE END USB_MspDeInit 0 */
125 /* Peripheral clock disable */
126 __HAL_RCC_USB_CLK_DISABLE();
128 /**USB GPIO Configuration
129 PA11 ------> USB_DM
130 PA12 ------> USB_DP
132 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
134 /* Peripheral interrupt Deinit*/
135 HAL_NVIC_DisableIRQ(USB_LP_IRQn);
137 /* USER CODE BEGIN USB_MspDeInit 1 */
138 __HAL_RCC_GPIOA_CLK_DISABLE();
139 /* USER CODE END USB_MspDeInit 1 */
144 * @brief Setup stage callback
145 * @param hpcd: PCD handle
146 * @retval None
148 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
149 static void PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
150 #else
151 void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
152 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
154 /* USER CODE BEGIN HAL_PCD_SetupStageCallback_PreTreatment */
156 /* USER CODE END HAL_PCD_SetupStageCallback_PreTreatment */
157 USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup);
158 /* USER CODE BEGIN HAL_PCD_SetupStageCallback_PostTreatment */
160 /* USER CODE END HAL_PCD_SetupStageCallback_PostTreatment */
164 * @brief Data Out stage callback.
165 * @param hpcd: PCD handle
166 * @param epnum: Endpoint number
167 * @retval None
169 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
170 static void PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
171 #else
172 void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
173 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
175 /* USER CODE BEGIN HAL_PCD_DataOutStageCallback_PreTreatment */
177 /* USER CODE END HAL_PCD_DataOutStageCallback_PreTreatment */
178 USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
179 /* USER CODE BEGIN HAL_PCD_DataOutStageCallback_PostTreatment */
181 /* USER CODE END HAL_PCD_DataOutStageCallback_PostTreatment */
185 * @brief Data In stage callback.
186 * @param hpcd: PCD handle
187 * @param epnum: Endpoint number
188 * @retval None
190 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
191 static void PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
192 #else
193 void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
194 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
196 /* USER CODE BEGIN HAL_PCD_DataInStageCallback_PreTreatment */
198 /* USER CODE END HAL_PCD_DataInStageCallback_PreTreatment */
199 USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
200 /* USER CODE BEGIN HAL_PCD_DataInStageCallback_PostTreatment */
202 /* USER CODE END HAL_PCD_DataInStageCallback_PostTreatment */
206 * @brief SOF callback.
207 * @param hpcd: PCD handle
208 * @retval None
210 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
211 static void PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
212 #else
213 void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
214 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
216 /* USER CODE BEGIN HAL_PCD_SOFCallback_PreTreatment */
218 /* USER CODE END HAL_PCD_SOFCallback_PreTreatment */
219 USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData);
220 /* USER CODE BEGIN HAL_PCD_SOFCallback_PostTreatment */
222 /* USER CODE END HAL_PCD_SOFCallback_PostTreatment */
226 * @brief Reset callback.
227 * @param hpcd: PCD handle
228 * @retval None
230 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
231 static void PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
232 #else
233 void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
234 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
236 /* USER CODE BEGIN HAL_PCD_ResetCallback_PreTreatment */
238 /* USER CODE END HAL_PCD_ResetCallback_PreTreatment */
239 USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
241 if ( hpcd->Init.speed != PCD_SPEED_FULL)
243 Error_Handler();
245 /* Set Speed. */
246 USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed);
248 /* Reset Device. */
249 USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData);
250 /* USER CODE BEGIN HAL_PCD_ResetCallback_PostTreatment */
252 /* USER CODE END HAL_PCD_ResetCallback_PostTreatment */
256 * @brief Suspend callback.
257 * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
258 * @param hpcd: PCD handle
259 * @retval None
261 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
262 static void PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
263 #else
264 void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
265 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
267 /* USER CODE BEGIN HAL_PCD_SuspendCallback_PreTreatment */
269 /* USER CODE END HAL_PCD_SuspendCallback_PreTreatment */
270 /* Inform USB library that core enters in suspend Mode. */
271 USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
272 /* Enter in STOP mode. */
273 /* USER CODE BEGIN 2 */
274 if (hpcd->Init.low_power_enable)
276 /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
277 SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
279 /* USER CODE END 2 */
280 /* USER CODE BEGIN HAL_PCD_SuspendCallback_PostTreatment */
282 /* USER CODE END HAL_PCD_SuspendCallback_PostTreatment */
286 * @brief Resume callback.
287 * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
288 * @param hpcd: PCD handle
289 * @retval None
291 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
292 static void PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
293 #else
294 void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
295 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
297 /* USER CODE BEGIN HAL_PCD_ResumeCallback_PreTreatment */
299 /* USER CODE END HAL_PCD_ResumeCallback_PreTreatment */
301 /* USER CODE BEGIN 3 */
302 if (hpcd->Init.low_power_enable)
304 /* Reset SLEEPDEEP bit of Cortex System Control Register. */
305 SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
306 SystemClockConfig_Resume();
308 /* USER CODE END 3 */
310 USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData);
311 /* USER CODE BEGIN HAL_PCD_ResumeCallback_PostTreatment */
313 /* USER CODE END HAL_PCD_ResumeCallback_PostTreatment */
317 * @brief ISOOUTIncomplete callback.
318 * @param hpcd: PCD handle
319 * @param epnum: Endpoint number
320 * @retval None
322 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
323 static void PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
324 #else
325 void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
326 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
328 /* USER CODE BEGIN HAL_PCD_ISOOUTIncompleteCallback_PreTreatment */
330 /* USER CODE END HAL_PCD_ISOOUTIncompleteCallback_PreTreatment */
331 USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
332 /* USER CODE BEGIN HAL_PCD_ISOOUTIncompleteCallback_PostTreatment */
334 /* USER CODE END HAL_PCD_ISOOUTIncompleteCallback_PostTreatment */
338 * @brief ISOINIncomplete callback.
339 * @param hpcd: PCD handle
340 * @param epnum: Endpoint number
341 * @retval None
343 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
344 static void PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
345 #else
346 void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
347 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
349 /* USER CODE BEGIN HAL_PCD_ISOINIncompleteCallback_PreTreatment */
351 /* USER CODE END HAL_PCD_ISOINIncompleteCallback_PreTreatment */
352 USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
353 /* USER CODE BEGIN HAL_PCD_ISOINIncompleteCallback_PostTreatment */
355 /* USER CODE END HAL_PCD_ISOINIncompleteCallback_PostTreatment */
359 * @brief Connect callback.
360 * @param hpcd: PCD handle
361 * @retval None
363 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
364 static void PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
365 #else
366 void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
367 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
369 /* USER CODE BEGIN HAL_PCD_ConnectCallback_PreTreatment */
371 /* USER CODE END HAL_PCD_ConnectCallback_PreTreatment */
372 USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData);
373 /* USER CODE BEGIN HAL_PCD_ConnectCallback_PostTreatment */
375 /* USER CODE END HAL_PCD_ConnectCallback_PostTreatment */
379 * @brief Disconnect callback.
380 * @param hpcd: PCD handle
381 * @retval None
383 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
384 static void PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
385 #else
386 void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
387 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
389 /* USER CODE BEGIN HAL_PCD_DisconnectCallback_PreTreatment */
391 /* USER CODE END HAL_PCD_DisconnectCallback_PreTreatment */
392 USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData);
393 /* USER CODE BEGIN HAL_PCD_DisconnectCallback_PostTreatment */
395 /* USER CODE END HAL_PCD_DisconnectCallback_PostTreatment */
398 /* USER CODE BEGIN LowLevelInterface */
400 /* USER CODE END LowLevelInterface */
402 /*******************************************************************************
403 LL Driver Interface (USB Device Library --> PCD)
404 *******************************************************************************/
407 * @brief Initializes the low level portion of the device driver.
408 * @param pdev: Device handle
409 * @retval USBD status
411 USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
413 /* Init USB Ip. */
414 hpcd_USB_FS.pData = pdev;
415 /* Link the driver to the stack. */
416 pdev->pData = &hpcd_USB_FS;
418 hpcd_USB_FS.Instance = USB;
419 hpcd_USB_FS.Init.dev_endpoints = 8;
420 hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
421 hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
422 hpcd_USB_FS.Init.Sof_enable = DISABLE;
423 hpcd_USB_FS.Init.low_power_enable = DISABLE;
424 hpcd_USB_FS.Init.lpm_enable = DISABLE;
425 hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
427 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
428 /* register Msp Callbacks (before the Init) */
429 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_MSPINIT_CB_ID, PCD_MspInit);
430 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_MSPDEINIT_CB_ID, PCD_MspDeInit);
431 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
433 if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
435 Error_Handler( );
438 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
439 /* Register USB PCD CallBacks */
440 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_SOF_CB_ID, PCD_SOFCallback);
441 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_SETUPSTAGE_CB_ID, PCD_SetupStageCallback);
442 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_RESET_CB_ID, PCD_ResetCallback);
443 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_SUSPEND_CB_ID, PCD_SuspendCallback);
444 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_RESUME_CB_ID, PCD_ResumeCallback);
445 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_CONNECT_CB_ID, PCD_ConnectCallback);
446 HAL_PCD_RegisterCallback(&hpcd_USB_FS, HAL_PCD_DISCONNECT_CB_ID, PCD_DisconnectCallback);
447 /* USER CODE BEGIN RegisterCallBackFirstPart */
449 /* USER CODE END RegisterCallBackFirstPart */
450 HAL_PCD_RegisterDataOutStageCallback(&hpcd_USB_FS, PCD_DataOutStageCallback);
451 HAL_PCD_RegisterDataInStageCallback(&hpcd_USB_FS, PCD_DataInStageCallback);
452 HAL_PCD_RegisterIsoOutIncpltCallback(&hpcd_USB_FS, PCD_ISOOUTIncompleteCallback);
453 HAL_PCD_RegisterIsoInIncpltCallback(&hpcd_USB_FS, PCD_ISOINIncompleteCallback);
454 /* USER CODE BEGIN RegisterCallBackSecondPart */
456 /* USER CODE END RegisterCallBackSecondPart */
457 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
458 /* USER CODE BEGIN EndPoint_Configuration */
459 HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x00 , PCD_SNG_BUF, 0x14);
460 HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x80 , PCD_SNG_BUF, 0x54);
461 /* USER CODE END EndPoint_Configuration */
462 /* USER CODE BEGIN EndPoint_Configuration_CDC */
463 HAL_PCDEx_PMAConfig(&hpcd_USB_FS, CDC_IN_EP, PCD_SNG_BUF, 0x94);
464 HAL_PCDEx_PMAConfig(&hpcd_USB_FS, CDC_OUT_EP, PCD_SNG_BUF, 0xD4);
465 HAL_PCDEx_PMAConfig(&hpcd_USB_FS, CDC_CMD_EP, PCD_SNG_BUF, 0x114);
466 /* USER CODE END EndPoint_Configuration_CDC */
467 return USBD_OK;
471 * @brief De-Initializes the low level portion of the device driver.
472 * @param pdev: Device handle
473 * @retval USBD status
475 USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
477 HAL_StatusTypeDef hal_status = HAL_OK;
478 USBD_StatusTypeDef usb_status = USBD_OK;
480 hal_status = HAL_PCD_DeInit(pdev->pData);
482 usb_status = USBD_Get_USB_Status(hal_status);
484 return usb_status;
488 * @brief Starts the low level portion of the device driver.
489 * @param pdev: Device handle
490 * @retval USBD status
492 USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
494 HAL_StatusTypeDef hal_status = HAL_OK;
495 USBD_StatusTypeDef usb_status = USBD_OK;
497 hal_status = HAL_PCD_Start(pdev->pData);
499 usb_status = USBD_Get_USB_Status(hal_status);
501 return usb_status;
505 * @brief Stops the low level portion of the device driver.
506 * @param pdev: Device handle
507 * @retval USBD status
509 USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev)
511 HAL_StatusTypeDef hal_status = HAL_OK;
512 USBD_StatusTypeDef usb_status = USBD_OK;
514 hal_status = HAL_PCD_Stop(pdev->pData);
516 usb_status = USBD_Get_USB_Status(hal_status);
518 return usb_status;
522 * @brief Opens an endpoint of the low level driver.
523 * @param pdev: Device handle
524 * @param ep_addr: Endpoint number
525 * @param ep_type: Endpoint type
526 * @param ep_mps: Endpoint max packet size
527 * @retval USBD status
529 USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps)
531 HAL_StatusTypeDef hal_status = HAL_OK;
532 USBD_StatusTypeDef usb_status = USBD_OK;
534 hal_status = HAL_PCD_EP_Open(pdev->pData, ep_addr, ep_mps, ep_type);
536 usb_status = USBD_Get_USB_Status(hal_status);
538 return usb_status;
542 * @brief Closes an endpoint of the low level driver.
543 * @param pdev: Device handle
544 * @param ep_addr: Endpoint number
545 * @retval USBD status
547 USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
549 HAL_StatusTypeDef hal_status = HAL_OK;
550 USBD_StatusTypeDef usb_status = USBD_OK;
552 hal_status = HAL_PCD_EP_Close(pdev->pData, ep_addr);
554 usb_status = USBD_Get_USB_Status(hal_status);
556 return usb_status;
560 * @brief Flushes an endpoint of the Low Level Driver.
561 * @param pdev: Device handle
562 * @param ep_addr: Endpoint number
563 * @retval USBD status
565 USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
567 HAL_StatusTypeDef hal_status = HAL_OK;
568 USBD_StatusTypeDef usb_status = USBD_OK;
570 hal_status = HAL_PCD_EP_Flush(pdev->pData, ep_addr);
572 usb_status = USBD_Get_USB_Status(hal_status);
574 return usb_status;
578 * @brief Sets a Stall condition on an endpoint of the Low Level Driver.
579 * @param pdev: Device handle
580 * @param ep_addr: Endpoint number
581 * @retval USBD status
583 USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
585 HAL_StatusTypeDef hal_status = HAL_OK;
586 USBD_StatusTypeDef usb_status = USBD_OK;
588 hal_status = HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
590 usb_status = USBD_Get_USB_Status(hal_status);
592 return usb_status;
596 * @brief Clears a Stall condition on an endpoint of the Low Level Driver.
597 * @param pdev: Device handle
598 * @param ep_addr: Endpoint number
599 * @retval USBD status
601 USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
603 HAL_StatusTypeDef hal_status = HAL_OK;
604 USBD_StatusTypeDef usb_status = USBD_OK;
606 hal_status = HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
608 usb_status = USBD_Get_USB_Status(hal_status);
610 return usb_status;
614 * @brief Returns Stall condition.
615 * @param pdev: Device handle
616 * @param ep_addr: Endpoint number
617 * @retval Stall (1: Yes, 0: No)
619 uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
621 PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData;
623 if((ep_addr & 0x80) == 0x80)
625 return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
627 else
629 return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
634 * @brief Assigns a USB address to the device.
635 * @param pdev: Device handle
636 * @param dev_addr: Device address
637 * @retval USBD status
639 USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr)
641 HAL_StatusTypeDef hal_status = HAL_OK;
642 USBD_StatusTypeDef usb_status = USBD_OK;
644 hal_status = HAL_PCD_SetAddress(pdev->pData, dev_addr);
646 usb_status = USBD_Get_USB_Status(hal_status);
648 return usb_status;
652 * @brief Transmits data over an endpoint.
653 * @param pdev: Device handle
654 * @param ep_addr: Endpoint number
655 * @param pbuf: Pointer to data to be sent
656 * @param size: Data size
657 * @retval USBD status
659 USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size)
661 HAL_StatusTypeDef hal_status = HAL_OK;
662 USBD_StatusTypeDef usb_status = USBD_OK;
664 hal_status = HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
666 usb_status = USBD_Get_USB_Status(hal_status);
668 return usb_status;
672 * @brief Prepares an endpoint for reception.
673 * @param pdev: Device handle
674 * @param ep_addr: Endpoint number
675 * @param pbuf: Pointer to data to be received
676 * @param size: Data size
677 * @retval USBD status
679 USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size)
681 HAL_StatusTypeDef hal_status = HAL_OK;
682 USBD_StatusTypeDef usb_status = USBD_OK;
684 hal_status = HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
686 usb_status = USBD_Get_USB_Status(hal_status);
688 return usb_status;
692 * @brief Returns the last transfered packet size.
693 * @param pdev: Device handle
694 * @param ep_addr: Endpoint number
695 * @retval Recived Data Size
697 uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
699 return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr);
703 * @brief Delays routine for the USB Device Library.
704 * @param Delay: Delay in ms
705 * @retval None
707 void USBD_LL_Delay(uint32_t Delay)
709 HAL_Delay(Delay);
713 * @brief Static single allocation.
714 * @param size: Size of allocated memory
715 * @retval None
717 void *USBD_static_malloc(uint32_t size)
719 UNUSED(size);
721 static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */
722 return mem;
726 * @brief Dummy memory free
727 * @param p: Pointer to allocated memory address
728 * @retval None
730 void USBD_static_free(void *p)
732 UNUSED(p);
735 /* USER CODE BEGIN 5 */
737 * @brief Configures system clock after wake-up from USB resume callBack:
738 * enable HSI, PLL and select PLL as system clock source.
739 * @retval None
741 void SystemClockConfig_Resume(void)
743 #if 0
744 // We don't need this as we don't sleep
745 // XXX Entire sleep & wakeup support may be removed?
746 SystemClock_Config();
747 USBD_Clock_Config();
748 #endif
750 /* USER CODE END 5 */
753 * @brief Retuns the USB status depending on the HAL status:
754 * @param hal_status: HAL status
755 * @retval USB status
757 USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status)
759 USBD_StatusTypeDef usb_status = USBD_OK;
761 switch (hal_status)
763 case HAL_OK :
764 usb_status = USBD_OK;
765 break;
766 case HAL_ERROR :
767 usb_status = USBD_FAIL;
768 break;
769 case HAL_BUSY :
770 usb_status = USBD_BUSY;
771 break;
772 case HAL_TIMEOUT :
773 usb_status = USBD_FAIL;
774 break;
775 default :
776 usb_status = USBD_FAIL;
777 break;
779 return usb_status;
781 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/