Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_hal_pcd.c
blobd4e3d2df5583881e83c5b2a59c5ac66744a44579
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_hal_pcd.c
4 * @author MCD Application Team
5 * @brief PCD HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the USB Peripheral Controller:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State functions
13 @verbatim
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
17 [..]
18 The PCD HAL driver can be used as follows:
20 (#) Declare a PCD_HandleTypeDef handle structure, for example:
21 PCD_HandleTypeDef hpcd;
23 (#) Fill parameters of Init structure in HCD handle
25 (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
27 (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
28 (##) Enable the PCD/USB Low Level interface clock using
29 (+++) __HAL_RCC_USB_CLK_ENABLE(); For USB Device only FS peripheral
31 (##) Initialize the related GPIO clocks
32 (##) Configure PCD pin-out
33 (##) Configure PCD NVIC interrupt
35 (#)Associate the Upper USB device stack to the HAL PCD Driver:
36 (##) hpcd.pData = pdev;
38 (#)Enable PCD transmission and reception:
39 (##) HAL_PCD_Start();
41 @endverbatim
42 ******************************************************************************
43 * @attention
45 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
46 * All rights reserved.</center></h2>
48 * This software component is licensed by ST under BSD 3-Clause license,
49 * the "License"; You may not use this file except in compliance with the
50 * License. You may obtain a copy of the License at:
51 * opensource.org/licenses/BSD-3-Clause
53 ******************************************************************************
56 /* Includes ------------------------------------------------------------------*/
57 #include "stm32g4xx_hal.h"
59 /** @addtogroup STM32G4xx_HAL_Driver
60 * @{
63 /** @defgroup PCD PCD
64 * @brief PCD HAL module driver
65 * @{
68 #ifdef HAL_PCD_MODULE_ENABLED
70 #if defined (USB)
72 /* Private types -------------------------------------------------------------*/
73 /* Private variables ---------------------------------------------------------*/
74 /* Private constants ---------------------------------------------------------*/
75 /* Private macros ------------------------------------------------------------*/
76 /** @defgroup PCD_Private_Macros PCD Private Macros
77 * @{
79 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
80 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
81 /**
82 * @}
85 /* Private functions prototypes ----------------------------------------------*/
86 /** @defgroup PCD_Private_Functions PCD Private Functions
87 * @{
90 static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
92 /**
93 * @}
96 /* Exported functions --------------------------------------------------------*/
97 /** @defgroup PCD_Exported_Functions PCD Exported Functions
98 * @{
101 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
102 * @brief Initialization and Configuration functions
104 @verbatim
105 ===============================================================================
106 ##### Initialization and de-initialization functions #####
107 ===============================================================================
108 [..] This section provides functions allowing to:
110 @endverbatim
111 * @{
115 * @brief Initializes the PCD according to the specified
116 * parameters in the PCD_InitTypeDef and initialize the associated handle.
117 * @param hpcd PCD handle
118 * @retval HAL status
120 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
122 uint8_t i;
124 /* Check the PCD handle allocation */
125 if (hpcd == NULL)
127 return HAL_ERROR;
130 /* Check the parameters */
131 assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
133 if (hpcd->State == HAL_PCD_STATE_RESET)
135 /* Allocate lock resource and initialize it */
136 hpcd->Lock = HAL_UNLOCKED;
138 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
139 hpcd->SOFCallback = HAL_PCD_SOFCallback;
140 hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
141 hpcd->ResetCallback = HAL_PCD_ResetCallback;
142 hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
143 hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
144 hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
145 hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
146 hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
147 hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
148 hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
149 hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
150 hpcd->LPMCallback = HAL_PCDEx_LPM_Callback;
151 hpcd->BCDCallback = HAL_PCDEx_BCD_Callback;
153 if (hpcd->MspInitCallback == NULL)
155 hpcd->MspInitCallback = HAL_PCD_MspInit;
158 /* Init the low level hardware */
159 hpcd->MspInitCallback(hpcd);
160 #else
161 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
162 HAL_PCD_MspInit(hpcd);
163 #endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
166 hpcd->State = HAL_PCD_STATE_BUSY;
168 /* Disable the Interrupts */
169 __HAL_PCD_DISABLE(hpcd);
171 /* Init endpoints structures */
172 for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
174 /* Init ep structure */
175 hpcd->IN_ep[i].is_in = 1U;
176 hpcd->IN_ep[i].num = i;
177 hpcd->IN_ep[i].tx_fifo_num = i;
178 /* Control until ep is activated */
179 hpcd->IN_ep[i].type = EP_TYPE_CTRL;
180 hpcd->IN_ep[i].maxpacket = 0U;
181 hpcd->IN_ep[i].xfer_buff = 0U;
182 hpcd->IN_ep[i].xfer_len = 0U;
185 for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
187 hpcd->OUT_ep[i].is_in = 0U;
188 hpcd->OUT_ep[i].num = i;
189 /* Control until ep is activated */
190 hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
191 hpcd->OUT_ep[i].maxpacket = 0U;
192 hpcd->OUT_ep[i].xfer_buff = 0U;
193 hpcd->OUT_ep[i].xfer_len = 0U;
196 /* Init Device */
197 (void)USB_DevInit(hpcd->Instance, hpcd->Init);
199 hpcd->USB_Address = 0U;
200 hpcd->State = HAL_PCD_STATE_READY;
202 /* Activate LPM */
203 if (hpcd->Init.lpm_enable == 1U)
205 (void)HAL_PCDEx_ActivateLPM(hpcd);
208 return HAL_OK;
212 * @brief DeInitializes the PCD peripheral.
213 * @param hpcd PCD handle
214 * @retval HAL status
216 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
218 /* Check the PCD handle allocation */
219 if (hpcd == NULL)
221 return HAL_ERROR;
224 hpcd->State = HAL_PCD_STATE_BUSY;
226 /* Stop Device */
227 (void)HAL_PCD_Stop(hpcd);
229 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
230 if (hpcd->MspDeInitCallback == NULL)
232 hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit */
235 /* DeInit the low level hardware */
236 hpcd->MspDeInitCallback(hpcd);
237 #else
238 /* DeInit the low level hardware: CLOCK, NVIC.*/
239 HAL_PCD_MspDeInit(hpcd);
240 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
242 hpcd->State = HAL_PCD_STATE_RESET;
244 return HAL_OK;
248 * @brief Initializes the PCD MSP.
249 * @param hpcd PCD handle
250 * @retval None
252 __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
254 /* Prevent unused argument(s) compilation warning */
255 UNUSED(hpcd);
257 /* NOTE : This function should not be modified, when the callback is needed,
258 the HAL_PCD_MspInit could be implemented in the user file
263 * @brief DeInitializes PCD MSP.
264 * @param hpcd PCD handle
265 * @retval None
267 __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
269 /* Prevent unused argument(s) compilation warning */
270 UNUSED(hpcd);
272 /* NOTE : This function should not be modified, when the callback is needed,
273 the HAL_PCD_MspDeInit could be implemented in the user file
277 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
279 * @brief Register a User USB PCD Callback
280 * To be used instead of the weak predefined callback
281 * @param hpcd USB PCD handle
282 * @param CallbackID ID of the callback to be registered
283 * This parameter can be one of the following values:
284 * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
285 * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
286 * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
287 * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
288 * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
289 * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
290 * @arg @ref HAL_PCD_DISCONNECT_CB_ID OTG PCD Disconnect callback ID
291 * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
292 * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
293 * @param pCallback pointer to the Callback function
294 * @retval HAL status
296 HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID, pPCD_CallbackTypeDef pCallback)
298 HAL_StatusTypeDef status = HAL_OK;
300 if (pCallback == NULL)
302 /* Update the error code */
303 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
304 return HAL_ERROR;
306 /* Process locked */
307 __HAL_LOCK(hpcd);
309 if (hpcd->State == HAL_PCD_STATE_READY)
311 switch (CallbackID)
313 case HAL_PCD_SOF_CB_ID :
314 hpcd->SOFCallback = pCallback;
315 break;
317 case HAL_PCD_SETUPSTAGE_CB_ID :
318 hpcd->SetupStageCallback = pCallback;
319 break;
321 case HAL_PCD_RESET_CB_ID :
322 hpcd->ResetCallback = pCallback;
323 break;
325 case HAL_PCD_SUSPEND_CB_ID :
326 hpcd->SuspendCallback = pCallback;
327 break;
329 case HAL_PCD_RESUME_CB_ID :
330 hpcd->ResumeCallback = pCallback;
331 break;
333 case HAL_PCD_CONNECT_CB_ID :
334 hpcd->ConnectCallback = pCallback;
335 break;
337 case HAL_PCD_DISCONNECT_CB_ID :
338 hpcd->DisconnectCallback = pCallback;
339 break;
341 case HAL_PCD_MSPINIT_CB_ID :
342 hpcd->MspInitCallback = pCallback;
343 break;
345 case HAL_PCD_MSPDEINIT_CB_ID :
346 hpcd->MspDeInitCallback = pCallback;
347 break;
349 default :
350 /* Update the error code */
351 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
352 /* Return error status */
353 status = HAL_ERROR;
354 break;
357 else if (hpcd->State == HAL_PCD_STATE_RESET)
359 switch (CallbackID)
361 case HAL_PCD_MSPINIT_CB_ID :
362 hpcd->MspInitCallback = pCallback;
363 break;
365 case HAL_PCD_MSPDEINIT_CB_ID :
366 hpcd->MspDeInitCallback = pCallback;
367 break;
369 default :
370 /* Update the error code */
371 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
372 /* Return error status */
373 status = HAL_ERROR;
374 break;
377 else
379 /* Update the error code */
380 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
381 /* Return error status */
382 status = HAL_ERROR;
385 /* Release Lock */
386 __HAL_UNLOCK(hpcd);
387 return status;
391 * @brief Unregister an USB PCD Callback
392 * USB PCD callabck is redirected to the weak predefined callback
393 * @param hpcd USB PCD handle
394 * @param CallbackID ID of the callback to be unregistered
395 * This parameter can be one of the following values:
396 * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
397 * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
398 * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
399 * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
400 * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
401 * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
402 * @arg @ref HAL_PCD_DISCONNECT_CB_ID OTG PCD Disconnect callback ID
403 * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
404 * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
405 * @retval HAL status
407 HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
409 HAL_StatusTypeDef status = HAL_OK;
411 /* Process locked */
412 __HAL_LOCK(hpcd);
414 /* Setup Legacy weak Callbacks */
415 if (hpcd->State == HAL_PCD_STATE_READY)
417 switch (CallbackID)
419 case HAL_PCD_SOF_CB_ID :
420 hpcd->SOFCallback = HAL_PCD_SOFCallback;
421 break;
423 case HAL_PCD_SETUPSTAGE_CB_ID :
424 hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
425 break;
427 case HAL_PCD_RESET_CB_ID :
428 hpcd->ResetCallback = HAL_PCD_ResetCallback;
429 break;
431 case HAL_PCD_SUSPEND_CB_ID :
432 hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
433 break;
435 case HAL_PCD_RESUME_CB_ID :
436 hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
437 break;
439 case HAL_PCD_CONNECT_CB_ID :
440 hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
441 break;
443 case HAL_PCD_DISCONNECT_CB_ID :
444 hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
445 break;
447 case HAL_PCD_MSPINIT_CB_ID :
448 hpcd->MspInitCallback = HAL_PCD_MspInit;
449 break;
451 case HAL_PCD_MSPDEINIT_CB_ID :
452 hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
453 break;
455 default :
456 /* Update the error code */
457 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
459 /* Return error status */
460 status = HAL_ERROR;
461 break;
464 else if (hpcd->State == HAL_PCD_STATE_RESET)
466 switch (CallbackID)
468 case HAL_PCD_MSPINIT_CB_ID :
469 hpcd->MspInitCallback = HAL_PCD_MspInit;
470 break;
472 case HAL_PCD_MSPDEINIT_CB_ID :
473 hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
474 break;
476 default :
477 /* Update the error code */
478 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
480 /* Return error status */
481 status = HAL_ERROR;
482 break;
485 else
487 /* Update the error code */
488 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
490 /* Return error status */
491 status = HAL_ERROR;
494 /* Release Lock */
495 __HAL_UNLOCK(hpcd);
496 return status;
500 * @brief Register USB PCD Data OUT Stage Callback
501 * To be used instead of the weak HAL_PCD_DataOutStageCallback() predefined callback
502 * @param hpcd PCD handle
503 * @param pCallback pointer to the USB PCD Data OUT Stage Callback function
504 * @retval HAL status
506 HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd, pPCD_DataOutStageCallbackTypeDef pCallback)
508 HAL_StatusTypeDef status = HAL_OK;
510 if (pCallback == NULL)
512 /* Update the error code */
513 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
515 return HAL_ERROR;
518 /* Process locked */
519 __HAL_LOCK(hpcd);
521 if (hpcd->State == HAL_PCD_STATE_READY)
523 hpcd->DataOutStageCallback = pCallback;
525 else
527 /* Update the error code */
528 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
530 /* Return error status */
531 status = HAL_ERROR;
534 /* Release Lock */
535 __HAL_UNLOCK(hpcd);
537 return status;
541 * @brief UnRegister the USB PCD Data OUT Stage Callback
542 * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataOutStageCallback() predefined callback
543 * @param hpcd PCD handle
544 * @retval HAL status
546 HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
548 HAL_StatusTypeDef status = HAL_OK;
550 /* Process locked */
551 __HAL_LOCK(hpcd);
553 if (hpcd->State == HAL_PCD_STATE_READY)
555 hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback */
557 else
559 /* Update the error code */
560 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
562 /* Return error status */
563 status = HAL_ERROR;
566 /* Release Lock */
567 __HAL_UNLOCK(hpcd);
569 return status;
573 * @brief Register USB PCD Data IN Stage Callback
574 * To be used instead of the weak HAL_PCD_DataInStageCallback() predefined callback
575 * @param hpcd PCD handle
576 * @param pCallback pointer to the USB PCD Data IN Stage Callback function
577 * @retval HAL status
579 HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd, pPCD_DataInStageCallbackTypeDef pCallback)
581 HAL_StatusTypeDef status = HAL_OK;
583 if (pCallback == NULL)
585 /* Update the error code */
586 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
588 return HAL_ERROR;
591 /* Process locked */
592 __HAL_LOCK(hpcd);
594 if (hpcd->State == HAL_PCD_STATE_READY)
596 hpcd->DataInStageCallback = pCallback;
598 else
600 /* Update the error code */
601 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
603 /* Return error status */
604 status = HAL_ERROR;
607 /* Release Lock */
608 __HAL_UNLOCK(hpcd);
610 return status;
614 * @brief UnRegister the USB PCD Data IN Stage Callback
615 * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataInStageCallback() predefined callback
616 * @param hpcd PCD handle
617 * @retval HAL status
619 HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
621 HAL_StatusTypeDef status = HAL_OK;
623 /* Process locked */
624 __HAL_LOCK(hpcd);
626 if (hpcd->State == HAL_PCD_STATE_READY)
628 hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback */
630 else
632 /* Update the error code */
633 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
635 /* Return error status */
636 status = HAL_ERROR;
639 /* Release Lock */
640 __HAL_UNLOCK(hpcd);
642 return status;
646 * @brief Register USB PCD Iso OUT incomplete Callback
647 * To be used instead of the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
648 * @param hpcd PCD handle
649 * @param pCallback pointer to the USB PCD Iso OUT incomplete Callback function
650 * @retval HAL status
652 HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd, pPCD_IsoOutIncpltCallbackTypeDef pCallback)
654 HAL_StatusTypeDef status = HAL_OK;
656 if (pCallback == NULL)
658 /* Update the error code */
659 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
661 return HAL_ERROR;
664 /* Process locked */
665 __HAL_LOCK(hpcd);
667 if (hpcd->State == HAL_PCD_STATE_READY)
669 hpcd->ISOOUTIncompleteCallback = pCallback;
671 else
673 /* Update the error code */
674 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
676 /* Return error status */
677 status = HAL_ERROR;
680 /* Release Lock */
681 __HAL_UNLOCK(hpcd);
683 return status;
687 * @brief UnRegister the USB PCD Iso OUT incomplete Callback
688 * USB PCD Iso OUT incomplete Callback is redirected to the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
689 * @param hpcd PCD handle
690 * @retval HAL status
692 HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
694 HAL_StatusTypeDef status = HAL_OK;
696 /* Process locked */
697 __HAL_LOCK(hpcd);
699 if (hpcd->State == HAL_PCD_STATE_READY)
701 hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback */
703 else
705 /* Update the error code */
706 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
708 /* Return error status */
709 status = HAL_ERROR;
712 /* Release Lock */
713 __HAL_UNLOCK(hpcd);
715 return status;
719 * @brief Register USB PCD Iso IN incomplete Callback
720 * To be used instead of the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
721 * @param hpcd PCD handle
722 * @param pCallback pointer to the USB PCD Iso IN incomplete Callback function
723 * @retval HAL status
725 HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd, pPCD_IsoInIncpltCallbackTypeDef pCallback)
727 HAL_StatusTypeDef status = HAL_OK;
729 if (pCallback == NULL)
731 /* Update the error code */
732 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
734 return HAL_ERROR;
737 /* Process locked */
738 __HAL_LOCK(hpcd);
740 if (hpcd->State == HAL_PCD_STATE_READY)
742 hpcd->ISOINIncompleteCallback = pCallback;
744 else
746 /* Update the error code */
747 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
749 /* Return error status */
750 status = HAL_ERROR;
753 /* Release Lock */
754 __HAL_UNLOCK(hpcd);
756 return status;
760 * @brief UnRegister the USB PCD Iso IN incomplete Callback
761 * USB PCD Iso IN incomplete Callback is redirected to the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
762 * @param hpcd PCD handle
763 * @retval HAL status
765 HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
767 HAL_StatusTypeDef status = HAL_OK;
769 /* Process locked */
770 __HAL_LOCK(hpcd);
772 if (hpcd->State == HAL_PCD_STATE_READY)
774 hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback */
776 else
778 /* Update the error code */
779 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
781 /* Return error status */
782 status = HAL_ERROR;
785 /* Release Lock */
786 __HAL_UNLOCK(hpcd);
788 return status;
792 * @brief Register USB PCD BCD Callback
793 * To be used instead of the weak HAL_PCDEx_BCD_Callback() predefined callback
794 * @param hpcd PCD handle
795 * @param pCallback pointer to the USB PCD BCD Callback function
796 * @retval HAL status
798 HAL_StatusTypeDef HAL_PCD_RegisterBcdCallback(PCD_HandleTypeDef *hpcd, pPCD_BcdCallbackTypeDef pCallback)
800 HAL_StatusTypeDef status = HAL_OK;
802 if (pCallback == NULL)
804 /* Update the error code */
805 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
807 return HAL_ERROR;
810 /* Process locked */
811 __HAL_LOCK(hpcd);
813 if (hpcd->State == HAL_PCD_STATE_READY)
815 hpcd->BCDCallback = pCallback;
817 else
819 /* Update the error code */
820 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
822 /* Return error status */
823 status = HAL_ERROR;
826 /* Release Lock */
827 __HAL_UNLOCK(hpcd);
829 return status;
833 * @brief UnRegister the USB PCD BCD Callback
834 * USB BCD Callback is redirected to the weak HAL_PCDEx_BCD_Callback() predefined callback
835 * @param hpcd PCD handle
836 * @retval HAL status
838 HAL_StatusTypeDef HAL_PCD_UnRegisterBcdCallback(PCD_HandleTypeDef *hpcd)
840 HAL_StatusTypeDef status = HAL_OK;
842 /* Process locked */
843 __HAL_LOCK(hpcd);
845 if (hpcd->State == HAL_PCD_STATE_READY)
847 hpcd->BCDCallback = HAL_PCDEx_BCD_Callback; /* Legacy weak HAL_PCDEx_BCD_Callback */
849 else
851 /* Update the error code */
852 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
854 /* Return error status */
855 status = HAL_ERROR;
858 /* Release Lock */
859 __HAL_UNLOCK(hpcd);
861 return status;
865 * @brief Register USB PCD LPM Callback
866 * To be used instead of the weak HAL_PCDEx_LPM_Callback() predefined callback
867 * @param hpcd PCD handle
868 * @param pCallback pointer to the USB PCD LPM Callback function
869 * @retval HAL status
871 HAL_StatusTypeDef HAL_PCD_RegisterLpmCallback(PCD_HandleTypeDef *hpcd, pPCD_LpmCallbackTypeDef pCallback)
873 HAL_StatusTypeDef status = HAL_OK;
875 if (pCallback == NULL)
877 /* Update the error code */
878 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
880 return HAL_ERROR;
883 /* Process locked */
884 __HAL_LOCK(hpcd);
886 if (hpcd->State == HAL_PCD_STATE_READY)
888 hpcd->LPMCallback = pCallback;
890 else
892 /* Update the error code */
893 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
895 /* Return error status */
896 status = HAL_ERROR;
899 /* Release Lock */
900 __HAL_UNLOCK(hpcd);
902 return status;
906 * @brief UnRegister the USB PCD LPM Callback
907 * USB LPM Callback is redirected to the weak HAL_PCDEx_LPM_Callback() predefined callback
908 * @param hpcd PCD handle
909 * @retval HAL status
911 HAL_StatusTypeDef HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef *hpcd)
913 HAL_StatusTypeDef status = HAL_OK;
915 /* Process locked */
916 __HAL_LOCK(hpcd);
918 if (hpcd->State == HAL_PCD_STATE_READY)
920 hpcd->LPMCallback = HAL_PCDEx_LPM_Callback; /* Legacy weak HAL_PCDEx_LPM_Callback */
922 else
924 /* Update the error code */
925 hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
927 /* Return error status */
928 status = HAL_ERROR;
931 /* Release Lock */
932 __HAL_UNLOCK(hpcd);
934 return status;
936 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
939 * @}
942 /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
943 * @brief Data transfers functions
945 @verbatim
946 ===============================================================================
947 ##### IO operation functions #####
948 ===============================================================================
949 [..]
950 This subsection provides a set of functions allowing to manage the PCD data
951 transfers.
953 @endverbatim
954 * @{
958 * @brief Start the USB device
959 * @param hpcd PCD handle
960 * @retval HAL status
962 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
964 __HAL_LOCK(hpcd);
965 (void)USB_DevConnect(hpcd->Instance);
966 __HAL_PCD_ENABLE(hpcd);
967 __HAL_UNLOCK(hpcd);
968 return HAL_OK;
972 * @brief Stop the USB device.
973 * @param hpcd PCD handle
974 * @retval HAL status
976 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
978 __HAL_LOCK(hpcd);
979 __HAL_PCD_DISABLE(hpcd);
981 (void)USB_StopDevice(hpcd->Instance);
983 __HAL_UNLOCK(hpcd);
985 return HAL_OK;
990 * @brief This function handles PCD interrupt request.
991 * @param hpcd PCD handle
992 * @retval HAL status
994 void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
996 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_CTR))
998 /* servicing of the endpoint correct transfer interrupt */
999 /* clear of the CTR flag into the sub */
1000 (void)PCD_EP_ISR_Handler(hpcd);
1003 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_RESET))
1005 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
1007 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1008 hpcd->ResetCallback(hpcd);
1009 #else
1010 HAL_PCD_ResetCallback(hpcd);
1011 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1013 (void)HAL_PCD_SetAddress(hpcd, 0U);
1016 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_PMAOVR))
1018 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
1021 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_ERR))
1023 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
1026 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_WKUP))
1028 hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_LPMODE);
1029 hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_FSUSP);
1031 if (hpcd->LPM_State == LPM_L1)
1033 hpcd->LPM_State = LPM_L0;
1034 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1035 hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
1036 #else
1037 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
1038 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1041 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1042 hpcd->ResumeCallback(hpcd);
1043 #else
1044 HAL_PCD_ResumeCallback(hpcd);
1045 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1047 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
1050 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_SUSP))
1052 /* Force low-power mode in the macrocell */
1053 hpcd->Instance->CNTR |= USB_CNTR_FSUSP;
1055 /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
1056 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
1058 hpcd->Instance->CNTR |= USB_CNTR_LPMODE;
1060 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_WKUP) == 0U)
1062 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1063 hpcd->SuspendCallback(hpcd);
1064 #else
1065 HAL_PCD_SuspendCallback(hpcd);
1066 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1070 /* Handle LPM Interrupt */
1071 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_L1REQ))
1073 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_L1REQ);
1074 if (hpcd->LPM_State == LPM_L0)
1076 /* Force suspend and low-power mode before going to L1 state*/
1077 hpcd->Instance->CNTR |= USB_CNTR_LPMODE;
1078 hpcd->Instance->CNTR |= USB_CNTR_FSUSP;
1080 hpcd->LPM_State = LPM_L1;
1081 hpcd->BESL = ((uint32_t)hpcd->Instance->LPMCSR & USB_LPMCSR_BESL) >> 2;
1082 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1083 hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
1084 #else
1085 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
1086 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1088 else
1090 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1091 hpcd->SuspendCallback(hpcd);
1092 #else
1093 HAL_PCD_SuspendCallback(hpcd);
1094 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1098 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_SOF))
1100 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
1102 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1103 hpcd->SOFCallback(hpcd);
1104 #else
1105 HAL_PCD_SOFCallback(hpcd);
1106 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1109 if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_ESOF))
1111 /* clear ESOF flag in ISTR */
1112 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
1118 * @brief Data OUT stage callback.
1119 * @param hpcd PCD handle
1120 * @param epnum endpoint number
1121 * @retval None
1123 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1125 /* Prevent unused argument(s) compilation warning */
1126 UNUSED(hpcd);
1127 UNUSED(epnum);
1129 /* NOTE : This function should not be modified, when the callback is needed,
1130 the HAL_PCD_DataOutStageCallback could be implemented in the user file
1135 * @brief Data IN stage callback
1136 * @param hpcd PCD handle
1137 * @param epnum endpoint number
1138 * @retval None
1140 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1142 /* Prevent unused argument(s) compilation warning */
1143 UNUSED(hpcd);
1144 UNUSED(epnum);
1146 /* NOTE : This function should not be modified, when the callback is needed,
1147 the HAL_PCD_DataInStageCallback could be implemented in the user file
1151 * @brief Setup stage callback
1152 * @param hpcd PCD handle
1153 * @retval None
1155 __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
1157 /* Prevent unused argument(s) compilation warning */
1158 UNUSED(hpcd);
1160 /* NOTE : This function should not be modified, when the callback is needed,
1161 the HAL_PCD_SetupStageCallback could be implemented in the user file
1166 * @brief USB Start Of Frame callback.
1167 * @param hpcd PCD handle
1168 * @retval None
1170 __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
1172 /* Prevent unused argument(s) compilation warning */
1173 UNUSED(hpcd);
1175 /* NOTE : This function should not be modified, when the callback is needed,
1176 the HAL_PCD_SOFCallback could be implemented in the user file
1181 * @brief USB Reset callback.
1182 * @param hpcd PCD handle
1183 * @retval None
1185 __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
1187 /* Prevent unused argument(s) compilation warning */
1188 UNUSED(hpcd);
1190 /* NOTE : This function should not be modified, when the callback is needed,
1191 the HAL_PCD_ResetCallback could be implemented in the user file
1196 * @brief Suspend event callback.
1197 * @param hpcd PCD handle
1198 * @retval None
1200 __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
1202 /* Prevent unused argument(s) compilation warning */
1203 UNUSED(hpcd);
1205 /* NOTE : This function should not be modified, when the callback is needed,
1206 the HAL_PCD_SuspendCallback could be implemented in the user file
1211 * @brief Resume event callback.
1212 * @param hpcd PCD handle
1213 * @retval None
1215 __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
1217 /* Prevent unused argument(s) compilation warning */
1218 UNUSED(hpcd);
1220 /* NOTE : This function should not be modified, when the callback is needed,
1221 the HAL_PCD_ResumeCallback could be implemented in the user file
1226 * @brief Incomplete ISO OUT callback.
1227 * @param hpcd PCD handle
1228 * @param epnum endpoint number
1229 * @retval None
1231 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1233 /* Prevent unused argument(s) compilation warning */
1234 UNUSED(hpcd);
1235 UNUSED(epnum);
1237 /* NOTE : This function should not be modified, when the callback is needed,
1238 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
1243 * @brief Incomplete ISO IN callback.
1244 * @param hpcd PCD handle
1245 * @param epnum endpoint number
1246 * @retval None
1248 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1250 /* Prevent unused argument(s) compilation warning */
1251 UNUSED(hpcd);
1252 UNUSED(epnum);
1254 /* NOTE : This function should not be modified, when the callback is needed,
1255 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
1260 * @brief Connection event callback.
1261 * @param hpcd PCD handle
1262 * @retval None
1264 __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
1266 /* Prevent unused argument(s) compilation warning */
1267 UNUSED(hpcd);
1269 /* NOTE : This function should not be modified, when the callback is needed,
1270 the HAL_PCD_ConnectCallback could be implemented in the user file
1275 * @brief Disconnection event callback.
1276 * @param hpcd PCD handle
1277 * @retval None
1279 __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
1281 /* Prevent unused argument(s) compilation warning */
1282 UNUSED(hpcd);
1284 /* NOTE : This function should not be modified, when the callback is needed,
1285 the HAL_PCD_DisconnectCallback could be implemented in the user file
1290 * @}
1293 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
1294 * @brief management functions
1296 @verbatim
1297 ===============================================================================
1298 ##### Peripheral Control functions #####
1299 ===============================================================================
1300 [..]
1301 This subsection provides a set of functions allowing to control the PCD data
1302 transfers.
1304 @endverbatim
1305 * @{
1309 * @brief Connect the USB device
1310 * @param hpcd PCD handle
1311 * @retval HAL status
1313 HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
1315 __HAL_LOCK(hpcd);
1316 (void)USB_DevConnect(hpcd->Instance);
1317 __HAL_UNLOCK(hpcd);
1318 return HAL_OK;
1322 * @brief Disconnect the USB device.
1323 * @param hpcd PCD handle
1324 * @retval HAL status
1326 HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
1328 __HAL_LOCK(hpcd);
1329 (void)USB_DevDisconnect(hpcd->Instance);
1330 __HAL_UNLOCK(hpcd);
1331 return HAL_OK;
1335 * @brief Set the USB Device address.
1336 * @param hpcd PCD handle
1337 * @param address new device address
1338 * @retval HAL status
1340 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
1342 __HAL_LOCK(hpcd);
1343 hpcd->USB_Address = address;
1344 (void)USB_SetDevAddress(hpcd->Instance, address);
1345 __HAL_UNLOCK(hpcd);
1346 return HAL_OK;
1349 * @brief Open and configure an endpoint.
1350 * @param hpcd PCD handle
1351 * @param ep_addr endpoint address
1352 * @param ep_mps endpoint max packet size
1353 * @param ep_type endpoint type
1354 * @retval HAL status
1356 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
1358 HAL_StatusTypeDef ret = HAL_OK;
1359 PCD_EPTypeDef *ep;
1361 if ((ep_addr & 0x80U) == 0x80U)
1363 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1364 ep->is_in = 1U;
1366 else
1368 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1369 ep->is_in = 0U;
1372 ep->num = ep_addr & EP_ADDR_MSK;
1373 ep->maxpacket = ep_mps;
1374 ep->type = ep_type;
1376 if (ep->is_in != 0U)
1378 /* Assign a Tx FIFO */
1379 ep->tx_fifo_num = ep->num;
1381 /* Set initial data PID. */
1382 if (ep_type == EP_TYPE_BULK)
1384 ep->data_pid_start = 0U;
1387 __HAL_LOCK(hpcd);
1388 (void)USB_ActivateEndpoint(hpcd->Instance, ep);
1389 __HAL_UNLOCK(hpcd);
1391 return ret;
1395 * @brief Deactivate an endpoint.
1396 * @param hpcd PCD handle
1397 * @param ep_addr endpoint address
1398 * @retval HAL status
1400 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1402 PCD_EPTypeDef *ep;
1404 if ((ep_addr & 0x80U) == 0x80U)
1406 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1407 ep->is_in = 1U;
1409 else
1411 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1412 ep->is_in = 0U;
1414 ep->num = ep_addr & EP_ADDR_MSK;
1416 __HAL_LOCK(hpcd);
1417 (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
1418 __HAL_UNLOCK(hpcd);
1419 return HAL_OK;
1424 * @brief Receive an amount of data.
1425 * @param hpcd PCD handle
1426 * @param ep_addr endpoint address
1427 * @param pBuf pointer to the reception buffer
1428 * @param len amount of data to be received
1429 * @retval HAL status
1431 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1433 PCD_EPTypeDef *ep;
1435 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1437 /*setup and start the Xfer */
1438 ep->xfer_buff = pBuf;
1439 ep->xfer_len = len;
1440 ep->xfer_count = 0U;
1441 ep->is_in = 0U;
1442 ep->num = ep_addr & EP_ADDR_MSK;
1444 if ((ep_addr & EP_ADDR_MSK) == 0U)
1446 (void)USB_EP0StartXfer(hpcd->Instance, ep);
1448 else
1450 (void)USB_EPStartXfer(hpcd->Instance, ep);
1453 return HAL_OK;
1457 * @brief Get Received Data Size
1458 * @param hpcd PCD handle
1459 * @param ep_addr endpoint address
1460 * @retval Data Size
1462 uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1464 return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
1467 * @brief Send an amount of data
1468 * @param hpcd PCD handle
1469 * @param ep_addr endpoint address
1470 * @param pBuf pointer to the transmission buffer
1471 * @param len amount of data to be sent
1472 * @retval HAL status
1474 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1476 PCD_EPTypeDef *ep;
1478 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1480 /*setup and start the Xfer */
1481 ep->xfer_buff = pBuf;
1482 ep->xfer_len = len;
1483 ep->xfer_count = 0U;
1484 ep->is_in = 1U;
1485 ep->num = ep_addr & EP_ADDR_MSK;
1487 if ((ep_addr & EP_ADDR_MSK) == 0U)
1489 (void)USB_EP0StartXfer(hpcd->Instance, ep);
1491 else
1493 (void)USB_EPStartXfer(hpcd->Instance, ep);
1496 return HAL_OK;
1500 * @brief Set a STALL condition over an endpoint
1501 * @param hpcd PCD handle
1502 * @param ep_addr endpoint address
1503 * @retval HAL status
1505 HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1507 PCD_EPTypeDef *ep;
1509 if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
1511 return HAL_ERROR;
1514 if ((0x80U & ep_addr) == 0x80U)
1516 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1517 ep->is_in = 1U;
1519 else
1521 ep = &hpcd->OUT_ep[ep_addr];
1522 ep->is_in = 0U;
1525 ep->is_stall = 1U;
1526 ep->num = ep_addr & EP_ADDR_MSK;
1528 __HAL_LOCK(hpcd);
1530 (void)USB_EPSetStall(hpcd->Instance, ep);
1531 if ((ep_addr & EP_ADDR_MSK) == 0U)
1533 (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t *)hpcd->Setup);
1535 __HAL_UNLOCK(hpcd);
1537 return HAL_OK;
1541 * @brief Clear a STALL condition over in an endpoint
1542 * @param hpcd PCD handle
1543 * @param ep_addr endpoint address
1544 * @retval HAL status
1546 HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1548 PCD_EPTypeDef *ep;
1550 if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
1552 return HAL_ERROR;
1555 if ((0x80U & ep_addr) == 0x80U)
1557 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1558 ep->is_in = 1U;
1560 else
1562 ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1563 ep->is_in = 0U;
1566 ep->is_stall = 0U;
1567 ep->num = ep_addr & EP_ADDR_MSK;
1569 __HAL_LOCK(hpcd);
1570 (void)USB_EPClearStall(hpcd->Instance, ep);
1571 __HAL_UNLOCK(hpcd);
1573 return HAL_OK;
1577 * @brief Flush an endpoint
1578 * @param hpcd PCD handle
1579 * @param ep_addr endpoint address
1580 * @retval HAL status
1582 HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1584 /* Prevent unused argument(s) compilation warning */
1585 UNUSED(hpcd);
1586 UNUSED(ep_addr);
1588 return HAL_OK;
1592 * @brief Activate remote wakeup signalling
1593 * @param hpcd PCD handle
1594 * @retval HAL status
1596 HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
1598 return (USB_ActivateRemoteWakeup(hpcd->Instance));
1602 * @brief De-activate remote wakeup signalling.
1603 * @param hpcd PCD handle
1604 * @retval HAL status
1606 HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
1608 return (USB_DeActivateRemoteWakeup(hpcd->Instance));
1612 * @}
1615 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1616 * @brief Peripheral State functions
1618 @verbatim
1619 ===============================================================================
1620 ##### Peripheral State functions #####
1621 ===============================================================================
1622 [..]
1623 This subsection permits to get in run-time the status of the peripheral
1624 and the data flow.
1626 @endverbatim
1627 * @{
1631 * @brief Return the PCD handle state.
1632 * @param hpcd PCD handle
1633 * @retval HAL state
1635 PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
1637 return hpcd->State;
1641 * @}
1645 * @}
1648 /* Private functions ---------------------------------------------------------*/
1649 /** @addtogroup PCD_Private_Functions
1650 * @{
1655 * @brief This function handles PCD Endpoint interrupt request.
1656 * @param hpcd PCD handle
1657 * @retval HAL status
1659 static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1661 PCD_EPTypeDef *ep;
1662 uint16_t count;
1663 uint16_t wIstr;
1664 uint16_t wEPVal;
1665 uint8_t epindex;
1667 /* stay in loop while pending interrupts */
1668 while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
1670 wIstr = hpcd->Instance->ISTR;
1671 /* extract highest priority endpoint number */
1672 epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
1674 if (epindex == 0U)
1676 /* Decode and service control endpoint interrupt */
1678 /* DIR bit = origin of the interrupt */
1679 if ((wIstr & USB_ISTR_DIR) == 0U)
1681 /* DIR = 0 */
1683 /* DIR = 0 => IN int */
1684 /* DIR = 0 implies that (EP_CTR_TX = 1) always */
1685 PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
1686 ep = &hpcd->IN_ep[0];
1688 ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
1689 ep->xfer_buff += ep->xfer_count;
1691 /* TX COMPLETE */
1692 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1693 hpcd->DataInStageCallback(hpcd, 0U);
1694 #else
1695 HAL_PCD_DataInStageCallback(hpcd, 0U);
1696 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1698 if ((hpcd->USB_Address > 0U) && (ep->xfer_len == 0U))
1700 hpcd->Instance->DADDR = ((uint16_t)hpcd->USB_Address | USB_DADDR_EF);
1701 hpcd->USB_Address = 0U;
1704 else
1706 /* DIR = 1 */
1708 /* DIR = 1 & CTR_RX => SETUP or OUT int */
1709 /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
1710 ep = &hpcd->OUT_ep[0];
1711 wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
1713 if ((wEPVal & USB_EP_SETUP) != 0U)
1715 /* Get SETUP Packet*/
1716 ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
1717 USB_ReadPMA(hpcd->Instance, (uint8_t *)hpcd->Setup, ep->pmaadress, (uint16_t)ep->xfer_count);
1718 /* SETUP bit kept frozen while CTR_RX = 1*/
1719 PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
1721 /* Process SETUP Packet*/
1722 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1723 hpcd->SetupStageCallback(hpcd);
1724 #else
1725 HAL_PCD_SetupStageCallback(hpcd);
1726 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1729 else if ((wEPVal & USB_EP_CTR_RX) != 0U)
1731 PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
1732 /* Get Control Data OUT Packet*/
1733 ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
1735 if (ep->xfer_count != 0U)
1737 USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, (uint16_t)ep->xfer_count);
1738 ep->xfer_buff += ep->xfer_count;
1741 /* Process Control Data OUT Packet*/
1742 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1743 hpcd->DataOutStageCallback(hpcd, 0U);
1744 #else
1745 HAL_PCD_DataOutStageCallback(hpcd, 0U);
1746 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1748 PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
1749 PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
1753 else
1755 /* Decode and service non control endpoints interrupt */
1757 /* process related endpoint register */
1758 wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
1759 if ((wEPVal & USB_EP_CTR_RX) != 0U)
1761 /* clear int flag */
1762 PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
1763 ep = &hpcd->OUT_ep[epindex];
1765 /* OUT double Buffering*/
1766 if (ep->doublebuffer == 0U)
1768 count = (uint16_t)PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
1769 if (count != 0U)
1771 USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
1774 else
1776 if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U)
1778 /*read from endpoint BUF0Addr buffer*/
1779 count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
1780 if (count != 0U)
1782 USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
1785 else
1787 /*read from endpoint BUF1Addr buffer*/
1788 count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
1789 if (count != 0U)
1791 USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
1794 /* free EP OUT Buffer */
1795 PCD_FreeUserBuffer(hpcd->Instance, ep->num, 0U);
1797 /*multi-packet on the NON control OUT endpoint*/
1798 ep->xfer_count += count;
1799 ep->xfer_buff += count;
1801 if ((ep->xfer_len == 0U) || (count < ep->maxpacket))
1803 /* RX COMPLETE */
1804 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1805 hpcd->DataOutStageCallback(hpcd, ep->num);
1806 #else
1807 HAL_PCD_DataOutStageCallback(hpcd, ep->num);
1808 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1810 else
1812 (void)HAL_PCD_EP_Receive(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
1815 } /* if((wEPVal & EP_CTR_RX) */
1817 if ((wEPVal & USB_EP_CTR_TX) != 0U)
1819 ep = &hpcd->IN_ep[epindex];
1821 /* clear int flag */
1822 PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
1824 /*multi-packet on the NON control IN endpoint*/
1825 ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
1826 ep->xfer_buff += ep->xfer_count;
1828 /* Zero Length Packet? */
1829 if (ep->xfer_len == 0U)
1831 /* TX COMPLETE */
1832 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1833 hpcd->DataInStageCallback(hpcd, ep->num);
1834 #else
1835 HAL_PCD_DataInStageCallback(hpcd, ep->num);
1836 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1838 else
1840 (void)HAL_PCD_EP_Transmit(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
1845 return HAL_OK;
1850 * @}
1852 #endif /* defined (USB) */
1853 #endif /* HAL_PCD_MODULE_ENABLED */
1856 * @}
1860 * @}
1863 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/