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
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
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:
42 ******************************************************************************
45 * <h2><center>© 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
64 * @brief PCD HAL module driver
68 #ifdef HAL_PCD_MODULE_ENABLED
72 /* Private types -------------------------------------------------------------*/
73 /* Private variables ---------------------------------------------------------*/
74 /* Private constants ---------------------------------------------------------*/
75 /* Private macros ------------------------------------------------------------*/
76 /** @defgroup PCD_Private_Macros PCD Private Macros
79 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
80 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
85 /* Private functions prototypes ----------------------------------------------*/
86 /** @defgroup PCD_Private_Functions PCD Private Functions
90 static HAL_StatusTypeDef
PCD_EP_ISR_Handler(PCD_HandleTypeDef
*hpcd
);
96 /* Exported functions --------------------------------------------------------*/
97 /** @defgroup PCD_Exported_Functions PCD Exported Functions
101 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
102 * @brief Initialization and Configuration functions
105 ===============================================================================
106 ##### Initialization and de-initialization functions #####
107 ===============================================================================
108 [..] This section provides functions allowing to:
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
120 HAL_StatusTypeDef
HAL_PCD_Init(PCD_HandleTypeDef
*hpcd
)
124 /* Check the PCD handle allocation */
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
);
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;
197 (void)USB_DevInit(hpcd
->Instance
, hpcd
->Init
);
199 hpcd
->USB_Address
= 0U;
200 hpcd
->State
= HAL_PCD_STATE_READY
;
203 if (hpcd
->Init
.lpm_enable
== 1U)
205 (void)HAL_PCDEx_ActivateLPM(hpcd
);
212 * @brief DeInitializes the PCD peripheral.
213 * @param hpcd PCD handle
216 HAL_StatusTypeDef
HAL_PCD_DeInit(PCD_HandleTypeDef
*hpcd
)
218 /* Check the PCD handle allocation */
224 hpcd
->State
= HAL_PCD_STATE_BUSY
;
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
);
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
;
248 * @brief Initializes the PCD MSP.
249 * @param hpcd PCD handle
252 __weak
void HAL_PCD_MspInit(PCD_HandleTypeDef
*hpcd
)
254 /* Prevent unused argument(s) compilation warning */
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
267 __weak
void HAL_PCD_MspDeInit(PCD_HandleTypeDef
*hpcd
)
269 /* Prevent unused argument(s) compilation warning */
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
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
;
309 if (hpcd
->State
== HAL_PCD_STATE_READY
)
313 case HAL_PCD_SOF_CB_ID
:
314 hpcd
->SOFCallback
= pCallback
;
317 case HAL_PCD_SETUPSTAGE_CB_ID
:
318 hpcd
->SetupStageCallback
= pCallback
;
321 case HAL_PCD_RESET_CB_ID
:
322 hpcd
->ResetCallback
= pCallback
;
325 case HAL_PCD_SUSPEND_CB_ID
:
326 hpcd
->SuspendCallback
= pCallback
;
329 case HAL_PCD_RESUME_CB_ID
:
330 hpcd
->ResumeCallback
= pCallback
;
333 case HAL_PCD_CONNECT_CB_ID
:
334 hpcd
->ConnectCallback
= pCallback
;
337 case HAL_PCD_DISCONNECT_CB_ID
:
338 hpcd
->DisconnectCallback
= pCallback
;
341 case HAL_PCD_MSPINIT_CB_ID
:
342 hpcd
->MspInitCallback
= pCallback
;
345 case HAL_PCD_MSPDEINIT_CB_ID
:
346 hpcd
->MspDeInitCallback
= pCallback
;
350 /* Update the error code */
351 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
352 /* Return error status */
357 else if (hpcd
->State
== HAL_PCD_STATE_RESET
)
361 case HAL_PCD_MSPINIT_CB_ID
:
362 hpcd
->MspInitCallback
= pCallback
;
365 case HAL_PCD_MSPDEINIT_CB_ID
:
366 hpcd
->MspDeInitCallback
= pCallback
;
370 /* Update the error code */
371 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
372 /* Return error status */
379 /* Update the error code */
380 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
381 /* Return error 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
407 HAL_StatusTypeDef
HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef
*hpcd
, HAL_PCD_CallbackIDTypeDef CallbackID
)
409 HAL_StatusTypeDef status
= HAL_OK
;
414 /* Setup Legacy weak Callbacks */
415 if (hpcd
->State
== HAL_PCD_STATE_READY
)
419 case HAL_PCD_SOF_CB_ID
:
420 hpcd
->SOFCallback
= HAL_PCD_SOFCallback
;
423 case HAL_PCD_SETUPSTAGE_CB_ID
:
424 hpcd
->SetupStageCallback
= HAL_PCD_SetupStageCallback
;
427 case HAL_PCD_RESET_CB_ID
:
428 hpcd
->ResetCallback
= HAL_PCD_ResetCallback
;
431 case HAL_PCD_SUSPEND_CB_ID
:
432 hpcd
->SuspendCallback
= HAL_PCD_SuspendCallback
;
435 case HAL_PCD_RESUME_CB_ID
:
436 hpcd
->ResumeCallback
= HAL_PCD_ResumeCallback
;
439 case HAL_PCD_CONNECT_CB_ID
:
440 hpcd
->ConnectCallback
= HAL_PCD_ConnectCallback
;
443 case HAL_PCD_DISCONNECT_CB_ID
:
444 hpcd
->DisconnectCallback
= HAL_PCD_DisconnectCallback
;
447 case HAL_PCD_MSPINIT_CB_ID
:
448 hpcd
->MspInitCallback
= HAL_PCD_MspInit
;
451 case HAL_PCD_MSPDEINIT_CB_ID
:
452 hpcd
->MspDeInitCallback
= HAL_PCD_MspDeInit
;
456 /* Update the error code */
457 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
459 /* Return error status */
464 else if (hpcd
->State
== HAL_PCD_STATE_RESET
)
468 case HAL_PCD_MSPINIT_CB_ID
:
469 hpcd
->MspInitCallback
= HAL_PCD_MspInit
;
472 case HAL_PCD_MSPDEINIT_CB_ID
:
473 hpcd
->MspDeInitCallback
= HAL_PCD_MspDeInit
;
477 /* Update the error code */
478 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
480 /* Return error status */
487 /* Update the error code */
488 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
490 /* Return error 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
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
;
521 if (hpcd
->State
== HAL_PCD_STATE_READY
)
523 hpcd
->DataOutStageCallback
= pCallback
;
527 /* Update the error code */
528 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
530 /* Return error 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
546 HAL_StatusTypeDef
HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef
*hpcd
)
548 HAL_StatusTypeDef status
= HAL_OK
;
553 if (hpcd
->State
== HAL_PCD_STATE_READY
)
555 hpcd
->DataOutStageCallback
= HAL_PCD_DataOutStageCallback
; /* Legacy weak DataOutStageCallback */
559 /* Update the error code */
560 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
562 /* Return error 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
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
;
594 if (hpcd
->State
== HAL_PCD_STATE_READY
)
596 hpcd
->DataInStageCallback
= pCallback
;
600 /* Update the error code */
601 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
603 /* Return error 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
619 HAL_StatusTypeDef
HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef
*hpcd
)
621 HAL_StatusTypeDef status
= HAL_OK
;
626 if (hpcd
->State
== HAL_PCD_STATE_READY
)
628 hpcd
->DataInStageCallback
= HAL_PCD_DataInStageCallback
; /* Legacy weak DataInStageCallback */
632 /* Update the error code */
633 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
635 /* Return error 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
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
;
667 if (hpcd
->State
== HAL_PCD_STATE_READY
)
669 hpcd
->ISOOUTIncompleteCallback
= pCallback
;
673 /* Update the error code */
674 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
676 /* Return error 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
692 HAL_StatusTypeDef
HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef
*hpcd
)
694 HAL_StatusTypeDef status
= HAL_OK
;
699 if (hpcd
->State
== HAL_PCD_STATE_READY
)
701 hpcd
->ISOOUTIncompleteCallback
= HAL_PCD_ISOOUTIncompleteCallback
; /* Legacy weak ISOOUTIncompleteCallback */
705 /* Update the error code */
706 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
708 /* Return error 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
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
;
740 if (hpcd
->State
== HAL_PCD_STATE_READY
)
742 hpcd
->ISOINIncompleteCallback
= pCallback
;
746 /* Update the error code */
747 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
749 /* Return error 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
765 HAL_StatusTypeDef
HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef
*hpcd
)
767 HAL_StatusTypeDef status
= HAL_OK
;
772 if (hpcd
->State
== HAL_PCD_STATE_READY
)
774 hpcd
->ISOINIncompleteCallback
= HAL_PCD_ISOINIncompleteCallback
; /* Legacy weak ISOINIncompleteCallback */
778 /* Update the error code */
779 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
781 /* Return error 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
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
;
813 if (hpcd
->State
== HAL_PCD_STATE_READY
)
815 hpcd
->BCDCallback
= pCallback
;
819 /* Update the error code */
820 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
822 /* Return error 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
838 HAL_StatusTypeDef
HAL_PCD_UnRegisterBcdCallback(PCD_HandleTypeDef
*hpcd
)
840 HAL_StatusTypeDef status
= HAL_OK
;
845 if (hpcd
->State
== HAL_PCD_STATE_READY
)
847 hpcd
->BCDCallback
= HAL_PCDEx_BCD_Callback
; /* Legacy weak HAL_PCDEx_BCD_Callback */
851 /* Update the error code */
852 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
854 /* Return error 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
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
;
886 if (hpcd
->State
== HAL_PCD_STATE_READY
)
888 hpcd
->LPMCallback
= pCallback
;
892 /* Update the error code */
893 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
895 /* Return error 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
911 HAL_StatusTypeDef
HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef
*hpcd
)
913 HAL_StatusTypeDef status
= HAL_OK
;
918 if (hpcd
->State
== HAL_PCD_STATE_READY
)
920 hpcd
->LPMCallback
= HAL_PCDEx_LPM_Callback
; /* Legacy weak HAL_PCDEx_LPM_Callback */
924 /* Update the error code */
925 hpcd
->ErrorCode
|= HAL_PCD_ERROR_INVALID_CALLBACK
;
927 /* Return error status */
936 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
942 /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
943 * @brief Data transfers functions
946 ===============================================================================
947 ##### IO operation functions #####
948 ===============================================================================
950 This subsection provides a set of functions allowing to manage the PCD data
958 * @brief Start the USB device
959 * @param hpcd PCD handle
962 HAL_StatusTypeDef
HAL_PCD_Start(PCD_HandleTypeDef
*hpcd
)
965 (void)USB_DevConnect(hpcd
->Instance
);
966 __HAL_PCD_ENABLE(hpcd
);
972 * @brief Stop the USB device.
973 * @param hpcd PCD handle
976 HAL_StatusTypeDef
HAL_PCD_Stop(PCD_HandleTypeDef
*hpcd
)
979 __HAL_PCD_DISABLE(hpcd
);
981 (void)USB_StopDevice(hpcd
->Instance
);
990 * @brief This function handles PCD interrupt request.
991 * @param hpcd PCD handle
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
);
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
);
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
);
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
);
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
);
1085 HAL_PCDEx_LPM_Callback(hpcd
, PCD_LPM_L1_ACTIVE
);
1086 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1090 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1091 hpcd
->SuspendCallback(hpcd
);
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
);
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
1123 __weak
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
1125 /* Prevent unused argument(s) compilation warning */
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
1140 __weak
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
1142 /* Prevent unused argument(s) compilation warning */
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
1155 __weak
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef
*hpcd
)
1157 /* Prevent unused argument(s) compilation warning */
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
1170 __weak
void HAL_PCD_SOFCallback(PCD_HandleTypeDef
*hpcd
)
1172 /* Prevent unused argument(s) compilation warning */
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
1185 __weak
void HAL_PCD_ResetCallback(PCD_HandleTypeDef
*hpcd
)
1187 /* Prevent unused argument(s) compilation warning */
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
1200 __weak
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef
*hpcd
)
1202 /* Prevent unused argument(s) compilation warning */
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
1215 __weak
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef
*hpcd
)
1217 /* Prevent unused argument(s) compilation warning */
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
1231 __weak
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
1233 /* Prevent unused argument(s) compilation warning */
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
1248 __weak
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
1250 /* Prevent unused argument(s) compilation warning */
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
1264 __weak
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef
*hpcd
)
1266 /* Prevent unused argument(s) compilation warning */
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
1279 __weak
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef
*hpcd
)
1281 /* Prevent unused argument(s) compilation warning */
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
1293 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
1294 * @brief management functions
1297 ===============================================================================
1298 ##### Peripheral Control functions #####
1299 ===============================================================================
1301 This subsection provides a set of functions allowing to control the PCD data
1309 * @brief Connect the USB device
1310 * @param hpcd PCD handle
1311 * @retval HAL status
1313 HAL_StatusTypeDef
HAL_PCD_DevConnect(PCD_HandleTypeDef
*hpcd
)
1316 (void)USB_DevConnect(hpcd
->Instance
);
1322 * @brief Disconnect the USB device.
1323 * @param hpcd PCD handle
1324 * @retval HAL status
1326 HAL_StatusTypeDef
HAL_PCD_DevDisconnect(PCD_HandleTypeDef
*hpcd
)
1329 (void)USB_DevDisconnect(hpcd
->Instance
);
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
)
1343 hpcd
->USB_Address
= address
;
1344 (void)USB_SetDevAddress(hpcd
->Instance
, address
);
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
;
1361 if ((ep_addr
& 0x80U
) == 0x80U
)
1363 ep
= &hpcd
->IN_ep
[ep_addr
& EP_ADDR_MSK
];
1368 ep
= &hpcd
->OUT_ep
[ep_addr
& EP_ADDR_MSK
];
1372 ep
->num
= ep_addr
& EP_ADDR_MSK
;
1373 ep
->maxpacket
= ep_mps
;
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;
1388 (void)USB_ActivateEndpoint(hpcd
->Instance
, ep
);
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
)
1404 if ((ep_addr
& 0x80U
) == 0x80U
)
1406 ep
= &hpcd
->IN_ep
[ep_addr
& EP_ADDR_MSK
];
1411 ep
= &hpcd
->OUT_ep
[ep_addr
& EP_ADDR_MSK
];
1414 ep
->num
= ep_addr
& EP_ADDR_MSK
;
1417 (void)USB_DeactivateEndpoint(hpcd
->Instance
, ep
);
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
)
1435 ep
= &hpcd
->OUT_ep
[ep_addr
& EP_ADDR_MSK
];
1437 /*setup and start the Xfer */
1438 ep
->xfer_buff
= pBuf
;
1440 ep
->xfer_count
= 0U;
1442 ep
->num
= ep_addr
& EP_ADDR_MSK
;
1444 if ((ep_addr
& EP_ADDR_MSK
) == 0U)
1446 (void)USB_EP0StartXfer(hpcd
->Instance
, ep
);
1450 (void)USB_EPStartXfer(hpcd
->Instance
, ep
);
1457 * @brief Get Received Data Size
1458 * @param hpcd PCD handle
1459 * @param ep_addr endpoint address
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
)
1478 ep
= &hpcd
->IN_ep
[ep_addr
& EP_ADDR_MSK
];
1480 /*setup and start the Xfer */
1481 ep
->xfer_buff
= pBuf
;
1483 ep
->xfer_count
= 0U;
1485 ep
->num
= ep_addr
& EP_ADDR_MSK
;
1487 if ((ep_addr
& EP_ADDR_MSK
) == 0U)
1489 (void)USB_EP0StartXfer(hpcd
->Instance
, ep
);
1493 (void)USB_EPStartXfer(hpcd
->Instance
, ep
);
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
)
1509 if (((uint32_t)ep_addr
& EP_ADDR_MSK
) > hpcd
->Init
.dev_endpoints
)
1514 if ((0x80U
& ep_addr
) == 0x80U
)
1516 ep
= &hpcd
->IN_ep
[ep_addr
& EP_ADDR_MSK
];
1521 ep
= &hpcd
->OUT_ep
[ep_addr
];
1526 ep
->num
= ep_addr
& EP_ADDR_MSK
;
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
);
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
)
1550 if (((uint32_t)ep_addr
& 0x0FU
) > hpcd
->Init
.dev_endpoints
)
1555 if ((0x80U
& ep_addr
) == 0x80U
)
1557 ep
= &hpcd
->IN_ep
[ep_addr
& EP_ADDR_MSK
];
1562 ep
= &hpcd
->OUT_ep
[ep_addr
& EP_ADDR_MSK
];
1567 ep
->num
= ep_addr
& EP_ADDR_MSK
;
1570 (void)USB_EPClearStall(hpcd
->Instance
, ep
);
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 */
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
));
1615 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1616 * @brief Peripheral State functions
1619 ===============================================================================
1620 ##### Peripheral State functions #####
1621 ===============================================================================
1623 This subsection permits to get in run-time the status of the peripheral
1631 * @brief Return the PCD handle state.
1632 * @param hpcd PCD handle
1635 PCD_StateTypeDef
HAL_PCD_GetState(PCD_HandleTypeDef
*hpcd
)
1648 /* Private functions ---------------------------------------------------------*/
1649 /** @addtogroup PCD_Private_Functions
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
)
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
);
1676 /* Decode and service control endpoint interrupt */
1678 /* DIR bit = origin of the interrupt */
1679 if ((wIstr
& USB_ISTR_DIR
) == 0U)
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
;
1692 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1693 hpcd
->DataInStageCallback(hpcd
, 0U);
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;
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
);
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);
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
);
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
);
1771 USB_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, count
);
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
);
1782 USB_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr0
, count
);
1787 /*read from endpoint BUF1Addr buffer*/
1788 count
= (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
);
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
))
1804 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1805 hpcd
->DataOutStageCallback(hpcd
, ep
->num
);
1807 HAL_PCD_DataOutStageCallback(hpcd
, ep
->num
);
1808 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
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)
1832 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1833 hpcd
->DataInStageCallback(hpcd
, ep
->num
);
1835 HAL_PCD_DataInStageCallback(hpcd
, ep
->num
);
1836 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1840 (void)HAL_PCD_EP_Transmit(hpcd
, ep
->num
, ep
->xfer_buff
, ep
->xfer_len
);
1852 #endif /* defined (USB) */
1853 #endif /* HAL_PCD_MODULE_ENABLED */
1863 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/