1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_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
12   *
13   ******************************************************************************
14   * @attention
15   *
16   * Copyright (c) 2016 STMicroelectronics.
17   * All rights reserved.
18   *
19   * This software is licensed under terms that can be found in the LICENSE file
20   * in the root directory of this software component.
21   * If no LICENSE file comes with this software, it is provided AS-IS.
22   *
23   ******************************************************************************
24   @verbatim
25   ==============================================================================
26                     ##### How to use this driver #####
27   ==============================================================================
28     [..]
29       The PCD HAL driver can be used as follows:
30 
31      (#) Declare a PCD_HandleTypeDef handle structure, for example:
32          PCD_HandleTypeDef  hpcd;
33 
34      (#) Fill parameters of Init structure in HCD handle
35 
36      (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
37 
38      (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
39          (##) Enable the PCD/USB Low Level interface clock using
40               (+++) __HAL_RCC_USB_CLK_ENABLE(); For USB Device FS peripheral
41 
42          (##) Initialize the related GPIO clocks
43          (##) Configure PCD pin-out
44          (##) Configure PCD NVIC interrupt
45 
46      (#)Associate the Upper USB device stack to the HAL PCD Driver:
47          (##) hpcd.pData = pdev;
48 
49      (#)Enable PCD transmission and reception:
50          (##) HAL_PCD_Start();
51 
52   @endverbatim
53   ******************************************************************************
54   */
55 
56 /* Includes ------------------------------------------------------------------*/
57 #include "stm32f3xx_hal.h"
58 
59 /** @addtogroup STM32F3xx_HAL_Driver
60   * @{
61   */
62 
63 /** @defgroup PCD PCD
64   * @brief PCD HAL module driver
65   * @{
66   */
67 
68 #ifdef HAL_PCD_MODULE_ENABLED
69 
70 #if defined (USB)
71 
72 /* Private types -------------------------------------------------------------*/
73 /* Private variables ---------------------------------------------------------*/
74 /* Private constants ---------------------------------------------------------*/
75 /* Private macros ------------------------------------------------------------*/
76 /** @defgroup PCD_Private_Macros PCD Private Macros
77   * @{
78   */
79 #define PCD_MIN(a, b)  (((a) < (b)) ? (a) : (b))
80 #define PCD_MAX(a, b)  (((a) > (b)) ? (a) : (b))
81 /**
82   * @}
83   */
84 
85 /* Private functions prototypes ----------------------------------------------*/
86 /** @defgroup PCD_Private_Functions PCD Private Functions
87   * @{
88   */
89 
90 static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
91 #if (USE_USB_DOUBLE_BUFFER == 1U)
92 static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal);
93 static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal);
94 #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
95 
96 /**
97   * @}
98   */
99 
100 /* Exported functions --------------------------------------------------------*/
101 /** @defgroup PCD_Exported_Functions PCD Exported Functions
102   * @{
103   */
104 
105 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
106   *  @brief    Initialization and Configuration functions
107   *
108 @verbatim
109  ===============================================================================
110             ##### Initialization and de-initialization functions #####
111  ===============================================================================
112     [..]  This section provides functions allowing to:
113 
114 @endverbatim
115   * @{
116   */
117 
118 /**
119   * @brief  Initializes the PCD according to the specified
120   *         parameters in the PCD_InitTypeDef and initialize the associated handle.
121   * @param  hpcd PCD handle
122   * @retval HAL status
123   */
HAL_PCD_Init(PCD_HandleTypeDef * hpcd)124 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
125 {
126   uint8_t i;
127 
128   /* Check the PCD handle allocation */
129   if (hpcd == NULL)
130   {
131     return HAL_ERROR;
132   }
133 
134   /* Check the parameters */
135   assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
136 
137   if (hpcd->State == HAL_PCD_STATE_RESET)
138   {
139     /* Allocate lock resource and initialize it */
140     hpcd->Lock = HAL_UNLOCKED;
141 
142 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
143     hpcd->SOFCallback = HAL_PCD_SOFCallback;
144     hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
145     hpcd->ResetCallback = HAL_PCD_ResetCallback;
146     hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
147     hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
148     hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
149     hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
150     hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
151     hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
152     hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
153     hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
154 
155     if (hpcd->MspInitCallback == NULL)
156     {
157       hpcd->MspInitCallback = HAL_PCD_MspInit;
158     }
159 
160     /* Init the low level hardware */
161     hpcd->MspInitCallback(hpcd);
162 #else
163     /* Init the low level hardware : GPIO, CLOCK, NVIC... */
164     HAL_PCD_MspInit(hpcd);
165 #endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
166   }
167 
168   hpcd->State = HAL_PCD_STATE_BUSY;
169 
170   /* Disable the Interrupts */
171   __HAL_PCD_DISABLE(hpcd);
172 
173   /* Init endpoints structures */
174   for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
175   {
176     /* Init ep structure */
177     hpcd->IN_ep[i].is_in = 1U;
178     hpcd->IN_ep[i].num = i;
179     /* Control until ep is activated */
180     hpcd->IN_ep[i].type = EP_TYPE_CTRL;
181     hpcd->IN_ep[i].maxpacket = 0U;
182     hpcd->IN_ep[i].xfer_buff = 0U;
183     hpcd->IN_ep[i].xfer_len = 0U;
184   }
185 
186   for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
187   {
188     hpcd->OUT_ep[i].is_in = 0U;
189     hpcd->OUT_ep[i].num = i;
190     /* Control until ep is activated */
191     hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
192     hpcd->OUT_ep[i].maxpacket = 0U;
193     hpcd->OUT_ep[i].xfer_buff = 0U;
194     hpcd->OUT_ep[i].xfer_len = 0U;
195   }
196 
197   /* Init Device */
198   (void)USB_DevInit(hpcd->Instance, hpcd->Init);
199 
200   hpcd->USB_Address = 0U;
201   hpcd->State = HAL_PCD_STATE_READY;
202   return HAL_OK;
203 }
204 
205 /**
206   * @brief  DeInitializes the PCD peripheral.
207   * @param  hpcd PCD handle
208   * @retval HAL status
209   */
HAL_PCD_DeInit(PCD_HandleTypeDef * hpcd)210 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
211 {
212   /* Check the PCD handle allocation */
213   if (hpcd == NULL)
214   {
215     return HAL_ERROR;
216   }
217 
218   hpcd->State = HAL_PCD_STATE_BUSY;
219 
220   /* Stop Device */
221   if (USB_StopDevice(hpcd->Instance) != HAL_OK)
222   {
223     return HAL_ERROR;
224   }
225 
226 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
227   if (hpcd->MspDeInitCallback == NULL)
228   {
229     hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit  */
230   }
231 
232   /* DeInit the low level hardware */
233   hpcd->MspDeInitCallback(hpcd);
234 #else
235   /* DeInit the low level hardware: CLOCK, NVIC.*/
236   HAL_PCD_MspDeInit(hpcd);
237 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
238 
239   hpcd->State = HAL_PCD_STATE_RESET;
240 
241   return HAL_OK;
242 }
243 
244 /**
245   * @brief  Initializes the PCD MSP.
246   * @param  hpcd PCD handle
247   * @retval None
248   */
HAL_PCD_MspInit(PCD_HandleTypeDef * hpcd)249 __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
250 {
251   /* Prevent unused argument(s) compilation warning */
252   UNUSED(hpcd);
253 
254   /* NOTE : This function should not be modified, when the callback is needed,
255             the HAL_PCD_MspInit could be implemented in the user file
256    */
257 }
258 
259 /**
260   * @brief  DeInitializes PCD MSP.
261   * @param  hpcd PCD handle
262   * @retval None
263   */
HAL_PCD_MspDeInit(PCD_HandleTypeDef * hpcd)264 __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
265 {
266   /* Prevent unused argument(s) compilation warning */
267   UNUSED(hpcd);
268 
269   /* NOTE : This function should not be modified, when the callback is needed,
270             the HAL_PCD_MspDeInit could be implemented in the user file
271    */
272 }
273 
274 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
275 /**
276   * @brief  Register a User USB PCD Callback
277   *         To be used instead of the weak predefined callback
278   * @param  hpcd USB PCD handle
279   * @param  CallbackID ID of the callback to be registered
280   *         This parameter can be one of the following values:
281   *          @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
282   *          @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
283   *          @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
284   *          @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
285   *          @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
286   *          @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
287   *          @arg @ref HAL_PCD_DISCONNECT_CB_ID USB PCD Disconnect callback ID
288   *          @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
289   *          @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
290   * @param  pCallback pointer to the Callback function
291   * @retval HAL status
292   */
HAL_PCD_RegisterCallback(PCD_HandleTypeDef * hpcd,HAL_PCD_CallbackIDTypeDef CallbackID,pPCD_CallbackTypeDef pCallback)293 HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd,
294                                            HAL_PCD_CallbackIDTypeDef CallbackID,
295                                            pPCD_CallbackTypeDef pCallback)
296 {
297   HAL_StatusTypeDef status = HAL_OK;
298 
299   if (pCallback == NULL)
300   {
301     /* Update the error code */
302     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
303     return HAL_ERROR;
304   }
305   /* Process locked */
306   __HAL_LOCK(hpcd);
307 
308   if (hpcd->State == HAL_PCD_STATE_READY)
309   {
310     switch (CallbackID)
311     {
312       case HAL_PCD_SOF_CB_ID :
313         hpcd->SOFCallback = pCallback;
314         break;
315 
316       case HAL_PCD_SETUPSTAGE_CB_ID :
317         hpcd->SetupStageCallback = pCallback;
318         break;
319 
320       case HAL_PCD_RESET_CB_ID :
321         hpcd->ResetCallback = pCallback;
322         break;
323 
324       case HAL_PCD_SUSPEND_CB_ID :
325         hpcd->SuspendCallback = pCallback;
326         break;
327 
328       case HAL_PCD_RESUME_CB_ID :
329         hpcd->ResumeCallback = pCallback;
330         break;
331 
332       case HAL_PCD_CONNECT_CB_ID :
333         hpcd->ConnectCallback = pCallback;
334         break;
335 
336       case HAL_PCD_DISCONNECT_CB_ID :
337         hpcd->DisconnectCallback = pCallback;
338         break;
339 
340       case HAL_PCD_MSPINIT_CB_ID :
341         hpcd->MspInitCallback = pCallback;
342         break;
343 
344       case HAL_PCD_MSPDEINIT_CB_ID :
345         hpcd->MspDeInitCallback = pCallback;
346         break;
347 
348       default :
349         /* Update the error code */
350         hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
351         /* Return error status */
352         status =  HAL_ERROR;
353         break;
354     }
355   }
356   else if (hpcd->State == HAL_PCD_STATE_RESET)
357   {
358     switch (CallbackID)
359     {
360       case HAL_PCD_MSPINIT_CB_ID :
361         hpcd->MspInitCallback = pCallback;
362         break;
363 
364       case HAL_PCD_MSPDEINIT_CB_ID :
365         hpcd->MspDeInitCallback = pCallback;
366         break;
367 
368       default :
369         /* Update the error code */
370         hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
371         /* Return error status */
372         status =  HAL_ERROR;
373         break;
374     }
375   }
376   else
377   {
378     /* Update the error code */
379     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
380     /* Return error status */
381     status =  HAL_ERROR;
382   }
383 
384   /* Release Lock */
385   __HAL_UNLOCK(hpcd);
386   return status;
387 }
388 
389 /**
390   * @brief  Unregister an USB PCD Callback
391   *         USB PCD callback is redirected to the weak predefined callback
392   * @param  hpcd USB PCD handle
393   * @param  CallbackID ID of the callback to be unregistered
394   *         This parameter can be one of the following values:
395   *          @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
396   *          @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
397   *          @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
398   *          @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
399   *          @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
400   *          @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
401   *          @arg @ref HAL_PCD_DISCONNECT_CB_ID USB PCD Disconnect callback ID
402   *          @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
403   *          @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
404   * @retval HAL status
405   */
HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef * hpcd,HAL_PCD_CallbackIDTypeDef CallbackID)406 HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
407 {
408   HAL_StatusTypeDef status = HAL_OK;
409 
410   /* Process locked */
411   __HAL_LOCK(hpcd);
412 
413   /* Setup Legacy weak Callbacks  */
414   if (hpcd->State == HAL_PCD_STATE_READY)
415   {
416     switch (CallbackID)
417     {
418       case HAL_PCD_SOF_CB_ID :
419         hpcd->SOFCallback = HAL_PCD_SOFCallback;
420         break;
421 
422       case HAL_PCD_SETUPSTAGE_CB_ID :
423         hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
424         break;
425 
426       case HAL_PCD_RESET_CB_ID :
427         hpcd->ResetCallback = HAL_PCD_ResetCallback;
428         break;
429 
430       case HAL_PCD_SUSPEND_CB_ID :
431         hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
432         break;
433 
434       case HAL_PCD_RESUME_CB_ID :
435         hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
436         break;
437 
438       case HAL_PCD_CONNECT_CB_ID :
439         hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
440         break;
441 
442       case HAL_PCD_DISCONNECT_CB_ID :
443         hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
444         break;
445 
446       case HAL_PCD_MSPINIT_CB_ID :
447         hpcd->MspInitCallback = HAL_PCD_MspInit;
448         break;
449 
450       case HAL_PCD_MSPDEINIT_CB_ID :
451         hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
452         break;
453 
454       default :
455         /* Update the error code */
456         hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
457 
458         /* Return error status */
459         status =  HAL_ERROR;
460         break;
461     }
462   }
463   else if (hpcd->State == HAL_PCD_STATE_RESET)
464   {
465     switch (CallbackID)
466     {
467       case HAL_PCD_MSPINIT_CB_ID :
468         hpcd->MspInitCallback = HAL_PCD_MspInit;
469         break;
470 
471       case HAL_PCD_MSPDEINIT_CB_ID :
472         hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
473         break;
474 
475       default :
476         /* Update the error code */
477         hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
478 
479         /* Return error status */
480         status =  HAL_ERROR;
481         break;
482     }
483   }
484   else
485   {
486     /* Update the error code */
487     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
488 
489     /* Return error status */
490     status =  HAL_ERROR;
491   }
492 
493   /* Release Lock */
494   __HAL_UNLOCK(hpcd);
495   return status;
496 }
497 
498 /**
499   * @brief  Register USB PCD Data OUT Stage Callback
500   *         To be used instead of the weak HAL_PCD_DataOutStageCallback() predefined callback
501   * @param  hpcd PCD handle
502   * @param  pCallback pointer to the USB PCD Data OUT Stage Callback function
503   * @retval HAL status
504   */
HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef * hpcd,pPCD_DataOutStageCallbackTypeDef pCallback)505 HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd,
506                                                        pPCD_DataOutStageCallbackTypeDef pCallback)
507 {
508   HAL_StatusTypeDef status = HAL_OK;
509 
510   if (pCallback == NULL)
511   {
512     /* Update the error code */
513     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
514 
515     return HAL_ERROR;
516   }
517 
518   /* Process locked */
519   __HAL_LOCK(hpcd);
520 
521   if (hpcd->State == HAL_PCD_STATE_READY)
522   {
523     hpcd->DataOutStageCallback = pCallback;
524   }
525   else
526   {
527     /* Update the error code */
528     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
529 
530     /* Return error status */
531     status =  HAL_ERROR;
532   }
533 
534   /* Release Lock */
535   __HAL_UNLOCK(hpcd);
536 
537   return status;
538 }
539 
540 /**
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
545   */
HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef * hpcd)546 HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
547 {
548   HAL_StatusTypeDef status = HAL_OK;
549 
550   /* Process locked */
551   __HAL_LOCK(hpcd);
552 
553   if (hpcd->State == HAL_PCD_STATE_READY)
554   {
555     hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback  */
556   }
557   else
558   {
559     /* Update the error code */
560     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
561 
562     /* Return error status */
563     status =  HAL_ERROR;
564   }
565 
566   /* Release Lock */
567   __HAL_UNLOCK(hpcd);
568 
569   return status;
570 }
571 
572 /**
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
578   */
HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef * hpcd,pPCD_DataInStageCallbackTypeDef pCallback)579 HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd,
580                                                       pPCD_DataInStageCallbackTypeDef pCallback)
581 {
582   HAL_StatusTypeDef status = HAL_OK;
583 
584   if (pCallback == NULL)
585   {
586     /* Update the error code */
587     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
588 
589     return HAL_ERROR;
590   }
591 
592   /* Process locked */
593   __HAL_LOCK(hpcd);
594 
595   if (hpcd->State == HAL_PCD_STATE_READY)
596   {
597     hpcd->DataInStageCallback = pCallback;
598   }
599   else
600   {
601     /* Update the error code */
602     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
603 
604     /* Return error status */
605     status =  HAL_ERROR;
606   }
607 
608   /* Release Lock */
609   __HAL_UNLOCK(hpcd);
610 
611   return status;
612 }
613 
614 /**
615   * @brief  Unregister the USB PCD Data IN Stage Callback
616   *         USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataInStageCallback() predefined callback
617   * @param  hpcd PCD handle
618   * @retval HAL status
619   */
HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef * hpcd)620 HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
621 {
622   HAL_StatusTypeDef status = HAL_OK;
623 
624   /* Process locked */
625   __HAL_LOCK(hpcd);
626 
627   if (hpcd->State == HAL_PCD_STATE_READY)
628   {
629     hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback  */
630   }
631   else
632   {
633     /* Update the error code */
634     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
635 
636     /* Return error status */
637     status =  HAL_ERROR;
638   }
639 
640   /* Release Lock */
641   __HAL_UNLOCK(hpcd);
642 
643   return status;
644 }
645 
646 /**
647   * @brief  Register USB PCD Iso OUT incomplete Callback
648   *         To be used instead of the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
649   * @param  hpcd PCD handle
650   * @param  pCallback pointer to the USB PCD Iso OUT incomplete Callback function
651   * @retval HAL status
652   */
HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef * hpcd,pPCD_IsoOutIncpltCallbackTypeDef pCallback)653 HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd,
654                                                        pPCD_IsoOutIncpltCallbackTypeDef pCallback)
655 {
656   HAL_StatusTypeDef status = HAL_OK;
657 
658   if (pCallback == NULL)
659   {
660     /* Update the error code */
661     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
662 
663     return HAL_ERROR;
664   }
665 
666   /* Process locked */
667   __HAL_LOCK(hpcd);
668 
669   if (hpcd->State == HAL_PCD_STATE_READY)
670   {
671     hpcd->ISOOUTIncompleteCallback = pCallback;
672   }
673   else
674   {
675     /* Update the error code */
676     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
677 
678     /* Return error status */
679     status =  HAL_ERROR;
680   }
681 
682   /* Release Lock */
683   __HAL_UNLOCK(hpcd);
684 
685   return status;
686 }
687 
688 /**
689   * @brief  Unregister the USB PCD Iso OUT incomplete Callback
690   *         USB PCD Iso OUT incomplete Callback is redirected
691   *         to the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
692   * @param  hpcd PCD handle
693   * @retval HAL status
694   */
HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef * hpcd)695 HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
696 {
697   HAL_StatusTypeDef status = HAL_OK;
698 
699   /* Process locked */
700   __HAL_LOCK(hpcd);
701 
702   if (hpcd->State == HAL_PCD_STATE_READY)
703   {
704     hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback  */
705   }
706   else
707   {
708     /* Update the error code */
709     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
710 
711     /* Return error status */
712     status =  HAL_ERROR;
713   }
714 
715   /* Release Lock */
716   __HAL_UNLOCK(hpcd);
717 
718   return status;
719 }
720 
721 /**
722   * @brief  Register USB PCD Iso IN incomplete Callback
723   *         To be used instead of the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
724   * @param  hpcd PCD handle
725   * @param  pCallback pointer to the USB PCD Iso IN incomplete Callback function
726   * @retval HAL status
727   */
HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef * hpcd,pPCD_IsoInIncpltCallbackTypeDef pCallback)728 HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd,
729                                                       pPCD_IsoInIncpltCallbackTypeDef pCallback)
730 {
731   HAL_StatusTypeDef status = HAL_OK;
732 
733   if (pCallback == NULL)
734   {
735     /* Update the error code */
736     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
737 
738     return HAL_ERROR;
739   }
740 
741   /* Process locked */
742   __HAL_LOCK(hpcd);
743 
744   if (hpcd->State == HAL_PCD_STATE_READY)
745   {
746     hpcd->ISOINIncompleteCallback = pCallback;
747   }
748   else
749   {
750     /* Update the error code */
751     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
752 
753     /* Return error status */
754     status =  HAL_ERROR;
755   }
756 
757   /* Release Lock */
758   __HAL_UNLOCK(hpcd);
759 
760   return status;
761 }
762 
763 /**
764   * @brief  Unregister the USB PCD Iso IN incomplete Callback
765   *         USB PCD Iso IN incomplete Callback is redirected
766   *         to the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
767   * @param  hpcd PCD handle
768   * @retval HAL status
769   */
HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef * hpcd)770 HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
771 {
772   HAL_StatusTypeDef status = HAL_OK;
773 
774   /* Process locked */
775   __HAL_LOCK(hpcd);
776 
777   if (hpcd->State == HAL_PCD_STATE_READY)
778   {
779     hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback  */
780   }
781   else
782   {
783     /* Update the error code */
784     hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
785 
786     /* Return error status */
787     status =  HAL_ERROR;
788   }
789 
790   /* Release Lock */
791   __HAL_UNLOCK(hpcd);
792 
793   return status;
794 }
795 
796 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
797 
798 /**
799   * @}
800   */
801 
802 /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
803   *  @brief   Data transfers functions
804   *
805 @verbatim
806  ===============================================================================
807                       ##### IO operation functions #####
808  ===============================================================================
809     [..]
810     This subsection provides a set of functions allowing to manage the PCD data
811     transfers.
812 
813 @endverbatim
814   * @{
815   */
816 
817 /**
818   * @brief  Start the USB device
819   * @param  hpcd PCD handle
820   * @retval HAL status
821   */
HAL_PCD_Start(PCD_HandleTypeDef * hpcd)822 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
823 {
824   __HAL_LOCK(hpcd);
825   __HAL_PCD_ENABLE(hpcd);
826 
827   HAL_PCDEx_SetConnectionState(hpcd, 1U);
828 
829   (void)USB_DevConnect(hpcd->Instance);
830   __HAL_UNLOCK(hpcd);
831 
832   return HAL_OK;
833 }
834 
835 /**
836   * @brief  Stop the USB device.
837   * @param  hpcd PCD handle
838   * @retval HAL status
839   */
HAL_PCD_Stop(PCD_HandleTypeDef * hpcd)840 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
841 {
842   __HAL_LOCK(hpcd);
843   __HAL_PCD_DISABLE(hpcd);
844 
845   HAL_PCDEx_SetConnectionState(hpcd, 0U);
846 
847   (void)USB_DevDisconnect(hpcd->Instance);
848   __HAL_UNLOCK(hpcd);
849 
850   return HAL_OK;
851 }
852 
853 
854 /**
855   * @brief  This function handles PCD interrupt request.
856   * @param  hpcd PCD handle
857   * @retval HAL status
858   */
HAL_PCD_IRQHandler(PCD_HandleTypeDef * hpcd)859 void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
860 {
861   uint32_t wIstr = USB_ReadInterrupts(hpcd->Instance);
862 
863   if ((wIstr & USB_ISTR_CTR) == USB_ISTR_CTR)
864   {
865     /* servicing of the endpoint correct transfer interrupt */
866     /* clear of the CTR flag into the sub */
867     (void)PCD_EP_ISR_Handler(hpcd);
868 
869     return;
870   }
871 
872   if ((wIstr & USB_ISTR_RESET) == USB_ISTR_RESET)
873   {
874     __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
875 
876 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
877     hpcd->ResetCallback(hpcd);
878 #else
879     HAL_PCD_ResetCallback(hpcd);
880 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
881 
882     (void)HAL_PCD_SetAddress(hpcd, 0U);
883 
884     return;
885   }
886 
887   if ((wIstr & USB_ISTR_PMAOVR) == USB_ISTR_PMAOVR)
888   {
889     __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
890 
891     return;
892   }
893 
894   if ((wIstr & USB_ISTR_ERR) == USB_ISTR_ERR)
895   {
896     __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
897 
898     return;
899   }
900 
901   if ((wIstr & USB_ISTR_WKUP) == USB_ISTR_WKUP)
902   {
903     hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_LPMODE);
904     hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_FSUSP);
905 
906 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
907     hpcd->ResumeCallback(hpcd);
908 #else
909     HAL_PCD_ResumeCallback(hpcd);
910 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
911 
912     __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
913 
914     return;
915   }
916 
917   if ((wIstr & USB_ISTR_SUSP) == USB_ISTR_SUSP)
918   {
919     /* Force low-power mode in the macrocell */
920     hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_FSUSP;
921 
922     /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
923     __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
924 
925     hpcd->Instance->CNTR |= (uint16_t)USB_CNTR_LPMODE;
926 
927 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
928     hpcd->SuspendCallback(hpcd);
929 #else
930     HAL_PCD_SuspendCallback(hpcd);
931 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
932 
933     return;
934   }
935 
936   if ((wIstr & USB_ISTR_SOF) == USB_ISTR_SOF)
937   {
938     __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
939 
940 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
941     hpcd->SOFCallback(hpcd);
942 #else
943     HAL_PCD_SOFCallback(hpcd);
944 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
945 
946     return;
947   }
948 
949   if ((wIstr & USB_ISTR_ESOF) == USB_ISTR_ESOF)
950   {
951     /* clear ESOF flag in ISTR */
952     __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
953 
954     return;
955   }
956 }
957 
958 
959 /**
960   * @brief  Handles PCD Wakeup interrupt request.
961   * @param  hpcd PCD handle
962   * @retval HAL status
963   */
HAL_PCD_WKUP_IRQHandler(PCD_HandleTypeDef * hpcd)964 void HAL_PCD_WKUP_IRQHandler(PCD_HandleTypeDef *hpcd)
965 {
966   /* Clear EXTI pending Bit */
967   __HAL_USB_WAKEUP_EXTI_CLEAR_FLAG();
968 }
969 
970 
971 /**
972   * @brief  Data OUT stage callback.
973   * @param  hpcd PCD handle
974   * @param  epnum endpoint number
975   * @retval None
976   */
HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)977 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
978 {
979   /* Prevent unused argument(s) compilation warning */
980   UNUSED(hpcd);
981   UNUSED(epnum);
982 
983   /* NOTE : This function should not be modified, when the callback is needed,
984             the HAL_PCD_DataOutStageCallback could be implemented in the user file
985    */
986 }
987 
988 /**
989   * @brief  Data IN stage callback
990   * @param  hpcd PCD handle
991   * @param  epnum endpoint number
992   * @retval None
993   */
HAL_PCD_DataInStageCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)994 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
995 {
996   /* Prevent unused argument(s) compilation warning */
997   UNUSED(hpcd);
998   UNUSED(epnum);
999 
1000   /* NOTE : This function should not be modified, when the callback is needed,
1001             the HAL_PCD_DataInStageCallback could be implemented in the user file
1002    */
1003 }
1004 /**
1005   * @brief  Setup stage callback
1006   * @param  hpcd PCD handle
1007   * @retval None
1008   */
HAL_PCD_SetupStageCallback(PCD_HandleTypeDef * hpcd)1009 __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
1010 {
1011   /* Prevent unused argument(s) compilation warning */
1012   UNUSED(hpcd);
1013 
1014   /* NOTE : This function should not be modified, when the callback is needed,
1015             the HAL_PCD_SetupStageCallback could be implemented in the user file
1016    */
1017 }
1018 
1019 /**
1020   * @brief  USB Start Of Frame callback.
1021   * @param  hpcd PCD handle
1022   * @retval None
1023   */
HAL_PCD_SOFCallback(PCD_HandleTypeDef * hpcd)1024 __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
1025 {
1026   /* Prevent unused argument(s) compilation warning */
1027   UNUSED(hpcd);
1028 
1029   /* NOTE : This function should not be modified, when the callback is needed,
1030             the HAL_PCD_SOFCallback could be implemented in the user file
1031    */
1032 }
1033 
1034 /**
1035   * @brief  USB Reset callback.
1036   * @param  hpcd PCD handle
1037   * @retval None
1038   */
HAL_PCD_ResetCallback(PCD_HandleTypeDef * hpcd)1039 __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
1040 {
1041   /* Prevent unused argument(s) compilation warning */
1042   UNUSED(hpcd);
1043 
1044   /* NOTE : This function should not be modified, when the callback is needed,
1045             the HAL_PCD_ResetCallback could be implemented in the user file
1046    */
1047 }
1048 
1049 /**
1050   * @brief  Suspend event callback.
1051   * @param  hpcd PCD handle
1052   * @retval None
1053   */
HAL_PCD_SuspendCallback(PCD_HandleTypeDef * hpcd)1054 __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
1055 {
1056   /* Prevent unused argument(s) compilation warning */
1057   UNUSED(hpcd);
1058 
1059   /* NOTE : This function should not be modified, when the callback is needed,
1060             the HAL_PCD_SuspendCallback could be implemented in the user file
1061    */
1062 }
1063 
1064 /**
1065   * @brief  Resume event callback.
1066   * @param  hpcd PCD handle
1067   * @retval None
1068   */
HAL_PCD_ResumeCallback(PCD_HandleTypeDef * hpcd)1069 __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
1070 {
1071   /* Prevent unused argument(s) compilation warning */
1072   UNUSED(hpcd);
1073 
1074   /* NOTE : This function should not be modified, when the callback is needed,
1075             the HAL_PCD_ResumeCallback could be implemented in the user file
1076    */
1077 }
1078 
1079 /**
1080   * @brief  Incomplete ISO OUT callback.
1081   * @param  hpcd PCD handle
1082   * @param  epnum endpoint number
1083   * @retval None
1084   */
HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)1085 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1086 {
1087   /* Prevent unused argument(s) compilation warning */
1088   UNUSED(hpcd);
1089   UNUSED(epnum);
1090 
1091   /* NOTE : This function should not be modified, when the callback is needed,
1092             the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
1093    */
1094 }
1095 
1096 /**
1097   * @brief  Incomplete ISO IN callback.
1098   * @param  hpcd PCD handle
1099   * @param  epnum endpoint number
1100   * @retval None
1101   */
HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)1102 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1103 {
1104   /* Prevent unused argument(s) compilation warning */
1105   UNUSED(hpcd);
1106   UNUSED(epnum);
1107 
1108   /* NOTE : This function should not be modified, when the callback is needed,
1109             the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
1110    */
1111 }
1112 
1113 /**
1114   * @brief  Connection event callback.
1115   * @param  hpcd PCD handle
1116   * @retval None
1117   */
HAL_PCD_ConnectCallback(PCD_HandleTypeDef * hpcd)1118 __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
1119 {
1120   /* Prevent unused argument(s) compilation warning */
1121   UNUSED(hpcd);
1122 
1123   /* NOTE : This function should not be modified, when the callback is needed,
1124             the HAL_PCD_ConnectCallback could be implemented in the user file
1125    */
1126 }
1127 
1128 /**
1129   * @brief  Disconnection event callback.
1130   * @param  hpcd PCD handle
1131   * @retval None
1132   */
HAL_PCD_DisconnectCallback(PCD_HandleTypeDef * hpcd)1133 __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
1134 {
1135   /* Prevent unused argument(s) compilation warning */
1136   UNUSED(hpcd);
1137 
1138   /* NOTE : This function should not be modified, when the callback is needed,
1139             the HAL_PCD_DisconnectCallback could be implemented in the user file
1140    */
1141 }
1142 
1143 /**
1144   * @}
1145   */
1146 
1147 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
1148   *  @brief   management functions
1149   *
1150 @verbatim
1151  ===============================================================================
1152                       ##### Peripheral Control functions #####
1153  ===============================================================================
1154     [..]
1155     This subsection provides a set of functions allowing to control the PCD data
1156     transfers.
1157 
1158 @endverbatim
1159   * @{
1160   */
1161 
1162 /**
1163   * @brief  Connect the USB device
1164   * @param  hpcd PCD handle
1165   * @retval HAL status
1166   */
HAL_PCD_DevConnect(PCD_HandleTypeDef * hpcd)1167 HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
1168 {
1169   __HAL_LOCK(hpcd);
1170 
1171   HAL_PCDEx_SetConnectionState(hpcd, 1U);
1172 
1173   (void)USB_DevConnect(hpcd->Instance);
1174   __HAL_UNLOCK(hpcd);
1175 
1176   return HAL_OK;
1177 }
1178 
1179 /**
1180   * @brief  Disconnect the USB device.
1181   * @param  hpcd PCD handle
1182   * @retval HAL status
1183   */
HAL_PCD_DevDisconnect(PCD_HandleTypeDef * hpcd)1184 HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
1185 {
1186   __HAL_LOCK(hpcd);
1187 
1188   HAL_PCDEx_SetConnectionState(hpcd, 0U);
1189 
1190   (void)USB_DevDisconnect(hpcd->Instance);
1191   __HAL_UNLOCK(hpcd);
1192 
1193   return HAL_OK;
1194 }
1195 
1196 /**
1197   * @brief  Set the USB Device address.
1198   * @param  hpcd PCD handle
1199   * @param  address new device address
1200   * @retval HAL status
1201   */
HAL_PCD_SetAddress(PCD_HandleTypeDef * hpcd,uint8_t address)1202 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
1203 {
1204   __HAL_LOCK(hpcd);
1205   hpcd->USB_Address = address;
1206   (void)USB_SetDevAddress(hpcd->Instance, address);
1207   __HAL_UNLOCK(hpcd);
1208 
1209   return HAL_OK;
1210 }
1211 /**
1212   * @brief  Open and configure an endpoint.
1213   * @param  hpcd PCD handle
1214   * @param  ep_addr endpoint address
1215   * @param  ep_mps endpoint max packet size
1216   * @param  ep_type endpoint type
1217   * @retval HAL status
1218   */
HAL_PCD_EP_Open(PCD_HandleTypeDef * hpcd,uint8_t ep_addr,uint16_t ep_mps,uint8_t ep_type)1219 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
1220                                   uint16_t ep_mps, uint8_t ep_type)
1221 {
1222   HAL_StatusTypeDef  ret = HAL_OK;
1223   PCD_EPTypeDef *ep;
1224 
1225   if ((ep_addr & 0x80U) == 0x80U)
1226   {
1227     ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1228     ep->is_in = 1U;
1229   }
1230   else
1231   {
1232     ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1233     ep->is_in = 0U;
1234   }
1235 
1236   ep->num = ep_addr & EP_ADDR_MSK;
1237   ep->maxpacket = ep_mps;
1238   ep->type = ep_type;
1239 
1240   /* Set initial data PID. */
1241   if (ep_type == EP_TYPE_BULK)
1242   {
1243     ep->data_pid_start = 0U;
1244   }
1245 
1246   __HAL_LOCK(hpcd);
1247   (void)USB_ActivateEndpoint(hpcd->Instance, ep);
1248   __HAL_UNLOCK(hpcd);
1249 
1250   return ret;
1251 }
1252 
1253 /**
1254   * @brief  Deactivate an endpoint.
1255   * @param  hpcd PCD handle
1256   * @param  ep_addr endpoint address
1257   * @retval HAL status
1258   */
HAL_PCD_EP_Close(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)1259 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1260 {
1261   PCD_EPTypeDef *ep;
1262 
1263   if ((ep_addr & 0x80U) == 0x80U)
1264   {
1265     ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1266     ep->is_in = 1U;
1267   }
1268   else
1269   {
1270     ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1271     ep->is_in = 0U;
1272   }
1273   ep->num = ep_addr & EP_ADDR_MSK;
1274 
1275   __HAL_LOCK(hpcd);
1276   (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
1277   __HAL_UNLOCK(hpcd);
1278   return HAL_OK;
1279 }
1280 
1281 
1282 /**
1283   * @brief  Receive an amount of data.
1284   * @param  hpcd PCD handle
1285   * @param  ep_addr endpoint address
1286   * @param  pBuf pointer to the reception buffer
1287   * @param  len amount of data to be received
1288   * @retval HAL status
1289   */
HAL_PCD_EP_Receive(PCD_HandleTypeDef * hpcd,uint8_t ep_addr,uint8_t * pBuf,uint32_t len)1290 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1291 {
1292   PCD_EPTypeDef *ep;
1293 
1294   ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1295 
1296   /*setup and start the Xfer */
1297   ep->xfer_buff = pBuf;
1298   ep->xfer_len = len;
1299   ep->xfer_count = 0U;
1300   ep->is_in = 0U;
1301   ep->num = ep_addr & EP_ADDR_MSK;
1302 
1303   (void)USB_EPStartXfer(hpcd->Instance, ep);
1304 
1305   return HAL_OK;
1306 }
1307 
1308 /**
1309   * @brief  Get Received Data Size
1310   * @param  hpcd PCD handle
1311   * @param  ep_addr endpoint address
1312   * @retval Data Size
1313   */
HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)1314 uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1315 {
1316   return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
1317 }
1318 /**
1319   * @brief  Send an amount of data
1320   * @param  hpcd PCD handle
1321   * @param  ep_addr endpoint address
1322   * @param  pBuf pointer to the transmission buffer
1323   * @param  len amount of data to be sent
1324   * @retval HAL status
1325   */
HAL_PCD_EP_Transmit(PCD_HandleTypeDef * hpcd,uint8_t ep_addr,uint8_t * pBuf,uint32_t len)1326 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1327 {
1328   PCD_EPTypeDef *ep;
1329 
1330   ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1331 
1332   /*setup and start the Xfer */
1333   ep->xfer_buff = pBuf;
1334   ep->xfer_len = len;
1335   ep->xfer_fill_db = 1U;
1336   ep->xfer_len_db = len;
1337   ep->xfer_count = 0U;
1338   ep->is_in = 1U;
1339   ep->num = ep_addr & EP_ADDR_MSK;
1340 
1341   (void)USB_EPStartXfer(hpcd->Instance, ep);
1342 
1343   return HAL_OK;
1344 }
1345 
1346 /**
1347   * @brief  Set a STALL condition over an endpoint
1348   * @param  hpcd PCD handle
1349   * @param  ep_addr endpoint address
1350   * @retval HAL status
1351   */
HAL_PCD_EP_SetStall(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)1352 HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1353 {
1354   PCD_EPTypeDef *ep;
1355 
1356   if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
1357   {
1358     return HAL_ERROR;
1359   }
1360 
1361   if ((0x80U & ep_addr) == 0x80U)
1362   {
1363     ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1364     ep->is_in = 1U;
1365   }
1366   else
1367   {
1368     ep = &hpcd->OUT_ep[ep_addr];
1369     ep->is_in = 0U;
1370   }
1371 
1372   ep->is_stall = 1U;
1373   ep->num = ep_addr & EP_ADDR_MSK;
1374 
1375   __HAL_LOCK(hpcd);
1376 
1377   (void)USB_EPSetStall(hpcd->Instance, ep);
1378 
1379   __HAL_UNLOCK(hpcd);
1380 
1381   return HAL_OK;
1382 }
1383 
1384 /**
1385   * @brief  Clear a STALL condition over in an endpoint
1386   * @param  hpcd PCD handle
1387   * @param  ep_addr endpoint address
1388   * @retval HAL status
1389   */
HAL_PCD_EP_ClrStall(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)1390 HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1391 {
1392   PCD_EPTypeDef *ep;
1393 
1394   if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
1395   {
1396     return HAL_ERROR;
1397   }
1398 
1399   if ((0x80U & ep_addr) == 0x80U)
1400   {
1401     ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1402     ep->is_in = 1U;
1403   }
1404   else
1405   {
1406     ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1407     ep->is_in = 0U;
1408   }
1409 
1410   ep->is_stall = 0U;
1411   ep->num = ep_addr & EP_ADDR_MSK;
1412 
1413   __HAL_LOCK(hpcd);
1414   (void)USB_EPClearStall(hpcd->Instance, ep);
1415   __HAL_UNLOCK(hpcd);
1416 
1417   return HAL_OK;
1418 }
1419 
1420 /**
1421    * @brief  Abort an USB EP transaction.
1422    * @param  hpcd PCD handle
1423    * @param  ep_addr endpoint address
1424    * @retval HAL status
1425    */
HAL_PCD_EP_Abort(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)1426 HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1427 {
1428   HAL_StatusTypeDef ret;
1429   PCD_EPTypeDef *ep;
1430 
1431   if ((0x80U & ep_addr) == 0x80U)
1432   {
1433     ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1434   }
1435   else
1436   {
1437     ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1438   }
1439 
1440   /* Stop Xfer */
1441   ret = USB_EPStopXfer(hpcd->Instance, ep);
1442 
1443   return ret;
1444 }
1445 
1446 /**
1447   * @brief  Flush an endpoint
1448   * @param  hpcd PCD handle
1449   * @param  ep_addr endpoint address
1450   * @retval HAL status
1451   */
HAL_PCD_EP_Flush(PCD_HandleTypeDef * hpcd,uint8_t ep_addr)1452 HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1453 {
1454   /* Prevent unused argument(s) compilation warning */
1455   UNUSED(hpcd);
1456   UNUSED(ep_addr);
1457 
1458   return HAL_OK;
1459 }
1460 
1461 /**
1462   * @brief  Activate remote wakeup signalling
1463   * @param  hpcd PCD handle
1464   * @retval HAL status
1465   */
HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef * hpcd)1466 HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
1467 {
1468   return (USB_ActivateRemoteWakeup(hpcd->Instance));
1469 }
1470 
1471 /**
1472   * @brief  De-activate remote wakeup signalling.
1473   * @param  hpcd PCD handle
1474   * @retval HAL status
1475   */
HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef * hpcd)1476 HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
1477 {
1478   return (USB_DeActivateRemoteWakeup(hpcd->Instance));
1479 }
1480 
1481 /**
1482   * @}
1483   */
1484 
1485 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1486   *  @brief   Peripheral State functions
1487   *
1488 @verbatim
1489  ===============================================================================
1490                       ##### Peripheral State functions #####
1491  ===============================================================================
1492     [..]
1493     This subsection permits to get in run-time the status of the peripheral
1494     and the data flow.
1495 
1496 @endverbatim
1497   * @{
1498   */
1499 
1500 /**
1501   * @brief  Return the PCD handle state.
1502   * @param  hpcd PCD handle
1503   * @retval HAL state
1504   */
HAL_PCD_GetState(PCD_HandleTypeDef * hpcd)1505 PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
1506 {
1507   return hpcd->State;
1508 }
1509 
1510 /**
1511   * @}
1512   */
1513 
1514 /**
1515   * @}
1516   */
1517 
1518 /* Private functions ---------------------------------------------------------*/
1519 /** @addtogroup PCD_Private_Functions
1520   * @{
1521   */
1522 
1523 
1524 /**
1525   * @brief  This function handles PCD Endpoint interrupt request.
1526   * @param  hpcd PCD handle
1527   * @retval HAL status
1528   */
PCD_EP_ISR_Handler(PCD_HandleTypeDef * hpcd)1529 static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1530 {
1531   PCD_EPTypeDef *ep;
1532   uint16_t count;
1533   uint16_t wIstr;
1534   uint16_t wEPVal;
1535   uint16_t TxPctSize;
1536   uint8_t epindex;
1537 
1538 #if (USE_USB_DOUBLE_BUFFER != 1U)
1539   count = 0U;
1540 #endif /* USE_USB_DOUBLE_BUFFER */
1541 
1542   /* stay in loop while pending interrupts */
1543   while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
1544   {
1545     wIstr = hpcd->Instance->ISTR;
1546 
1547     /* extract highest priority endpoint number */
1548     epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
1549 
1550     if (epindex == 0U)
1551     {
1552       /* Decode and service control endpoint interrupt */
1553 
1554       /* DIR bit = origin of the interrupt */
1555       if ((wIstr & USB_ISTR_DIR) == 0U)
1556       {
1557         /* DIR = 0 */
1558 
1559         /* DIR = 0 => IN  int */
1560         /* DIR = 0 implies that (EP_CTR_TX = 1) always */
1561         PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
1562         ep = &hpcd->IN_ep[0];
1563 
1564         ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
1565         ep->xfer_buff += ep->xfer_count;
1566 
1567         /* TX COMPLETE */
1568 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1569         hpcd->DataInStageCallback(hpcd, 0U);
1570 #else
1571         HAL_PCD_DataInStageCallback(hpcd, 0U);
1572 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1573 
1574         if ((hpcd->USB_Address > 0U) && (ep->xfer_len == 0U))
1575         {
1576           hpcd->Instance->DADDR = ((uint16_t)hpcd->USB_Address | USB_DADDR_EF);
1577           hpcd->USB_Address = 0U;
1578         }
1579       }
1580       else
1581       {
1582         /* DIR = 1 */
1583 
1584         /* DIR = 1 & CTR_RX => SETUP or OUT int */
1585         /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
1586         ep = &hpcd->OUT_ep[0];
1587         wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
1588 
1589         if ((wEPVal & USB_EP_SETUP) != 0U)
1590         {
1591           /* Get SETUP Packet */
1592           ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
1593 
1594           USB_ReadPMA(hpcd->Instance, (uint8_t *)hpcd->Setup,
1595                       ep->pmaadress, (uint16_t)ep->xfer_count);
1596 
1597           /* SETUP bit kept frozen while CTR_RX = 1 */
1598           PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
1599 
1600           /* Process SETUP Packet*/
1601 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1602           hpcd->SetupStageCallback(hpcd);
1603 #else
1604           HAL_PCD_SetupStageCallback(hpcd);
1605 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1606         }
1607         else if ((wEPVal & USB_EP_CTR_RX) != 0U)
1608         {
1609           PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
1610 
1611           /* Get Control Data OUT Packet */
1612           ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
1613 
1614           if ((ep->xfer_count != 0U) && (ep->xfer_buff != 0U))
1615           {
1616             USB_ReadPMA(hpcd->Instance, ep->xfer_buff,
1617                         ep->pmaadress, (uint16_t)ep->xfer_count);
1618 
1619             ep->xfer_buff += ep->xfer_count;
1620 
1621             /* Process Control Data OUT Packet */
1622 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1623             hpcd->DataOutStageCallback(hpcd, 0U);
1624 #else
1625             HAL_PCD_DataOutStageCallback(hpcd, 0U);
1626 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1627           }
1628 
1629           wEPVal = (uint16_t)PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
1630 
1631           if (((wEPVal & USB_EP_SETUP) == 0U) && ((wEPVal & USB_EP_RX_STRX) != USB_EP_RX_VALID))
1632           {
1633             PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
1634             PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
1635           }
1636         }
1637       }
1638     }
1639     else
1640     {
1641       /* Decode and service non control endpoints interrupt */
1642       /* process related endpoint register */
1643       wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
1644 
1645       if ((wEPVal & USB_EP_CTR_RX) != 0U)
1646       {
1647         /* clear int flag */
1648         PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
1649         ep = &hpcd->OUT_ep[epindex];
1650 
1651         /* OUT Single Buffering */
1652         if (ep->doublebuffer == 0U)
1653         {
1654           count = (uint16_t)PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
1655 
1656           if (count != 0U)
1657           {
1658             USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
1659           }
1660         }
1661 #if (USE_USB_DOUBLE_BUFFER == 1U)
1662         else
1663         {
1664           /* manage double buffer bulk out */
1665           if (ep->type == EP_TYPE_BULK)
1666           {
1667             count = HAL_PCD_EP_DB_Receive(hpcd, ep, wEPVal);
1668           }
1669           else /* manage double buffer iso out */
1670           {
1671             /* free EP OUT Buffer */
1672             PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
1673 
1674             if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U)
1675             {
1676               /* read from endpoint BUF0Addr buffer */
1677               count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
1678 
1679               if (count != 0U)
1680               {
1681                 USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
1682               }
1683             }
1684             else
1685             {
1686               /* read from endpoint BUF1Addr buffer */
1687               count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
1688 
1689               if (count != 0U)
1690               {
1691                 USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
1692               }
1693             }
1694           }
1695         }
1696 #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
1697 
1698         /* multi-packet on the NON control OUT endpoint */
1699         ep->xfer_count += count;
1700         ep->xfer_buff += count;
1701 
1702         if ((ep->xfer_len == 0U) || (count < ep->maxpacket))
1703         {
1704           /* RX COMPLETE */
1705 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1706           hpcd->DataOutStageCallback(hpcd, ep->num);
1707 #else
1708           HAL_PCD_DataOutStageCallback(hpcd, ep->num);
1709 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1710         }
1711         else
1712         {
1713           (void)USB_EPStartXfer(hpcd->Instance, ep);
1714         }
1715       }
1716 
1717       if ((wEPVal & USB_EP_CTR_TX) != 0U)
1718       {
1719         ep = &hpcd->IN_ep[epindex];
1720 
1721         /* clear int flag */
1722         PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
1723 
1724         if (ep->type == EP_TYPE_ISOC)
1725         {
1726           ep->xfer_len = 0U;
1727 
1728 #if (USE_USB_DOUBLE_BUFFER == 1U)
1729           if (ep->doublebuffer != 0U)
1730           {
1731             if ((wEPVal & USB_EP_DTOG_TX) != 0U)
1732             {
1733               PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
1734             }
1735             else
1736             {
1737               PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
1738             }
1739           }
1740 #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
1741 
1742           /* TX COMPLETE */
1743 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1744           hpcd->DataInStageCallback(hpcd, ep->num);
1745 #else
1746           HAL_PCD_DataInStageCallback(hpcd, ep->num);
1747 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1748         }
1749         else
1750         {
1751           /* Manage Single Buffer Transaction */
1752           if ((wEPVal & USB_EP_KIND) == 0U)
1753           {
1754             /* multi-packet on the NON control IN endpoint */
1755             TxPctSize = (uint16_t)PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
1756 
1757             if (ep->xfer_len > TxPctSize)
1758             {
1759               ep->xfer_len -= TxPctSize;
1760             }
1761             else
1762             {
1763               ep->xfer_len = 0U;
1764             }
1765 
1766             /* Zero Length Packet? */
1767             if (ep->xfer_len == 0U)
1768             {
1769               /* TX COMPLETE */
1770 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1771               hpcd->DataInStageCallback(hpcd, ep->num);
1772 #else
1773               HAL_PCD_DataInStageCallback(hpcd, ep->num);
1774 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1775             }
1776             else
1777             {
1778               /* Transfer is not yet Done */
1779               ep->xfer_buff += TxPctSize;
1780               ep->xfer_count += TxPctSize;
1781               (void)USB_EPStartXfer(hpcd->Instance, ep);
1782             }
1783           }
1784 #if (USE_USB_DOUBLE_BUFFER == 1U)
1785           /* Double Buffer bulk IN (bulk transfer Len > Ep_Mps) */
1786           else
1787           {
1788             (void)HAL_PCD_EP_DB_Transmit(hpcd, ep, wEPVal);
1789           }
1790 #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
1791         }
1792       }
1793     }
1794   }
1795 
1796   return HAL_OK;
1797 }
1798 
1799 
1800 #if (USE_USB_DOUBLE_BUFFER == 1U)
1801 /**
1802   * @brief  Manage double buffer bulk out transaction from ISR
1803   * @param  hpcd PCD handle
1804   * @param  ep current endpoint handle
1805   * @param  wEPVal Last snapshot of EPRx register value taken in ISR
1806   * @retval HAL status
1807   */
HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef * hpcd,PCD_EPTypeDef * ep,uint16_t wEPVal)1808 static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd,
1809                                       PCD_EPTypeDef *ep, uint16_t wEPVal)
1810 {
1811   uint16_t count;
1812 
1813   /* Manage Buffer0 OUT */
1814   if ((wEPVal & USB_EP_DTOG_RX) != 0U)
1815   {
1816     /* Get count of received Data on buffer0 */
1817     count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
1818 
1819     if (ep->xfer_len >= count)
1820     {
1821       ep->xfer_len -= count;
1822     }
1823     else
1824     {
1825       ep->xfer_len = 0U;
1826     }
1827 
1828     if (ep->xfer_len == 0U)
1829     {
1830       /* set NAK to OUT endpoint since double buffer is enabled */
1831       PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
1832     }
1833 
1834     /* Check if Buffer1 is in blocked state which requires to toggle */
1835     if ((wEPVal & USB_EP_DTOG_TX) != 0U)
1836     {
1837       PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
1838     }
1839 
1840     if (count != 0U)
1841     {
1842       USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
1843     }
1844   }
1845   /* Manage Buffer 1 DTOG_RX=0 */
1846   else
1847   {
1848     /* Get count of received data */
1849     count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
1850 
1851     if (ep->xfer_len >= count)
1852     {
1853       ep->xfer_len -= count;
1854     }
1855     else
1856     {
1857       ep->xfer_len = 0U;
1858     }
1859 
1860     if (ep->xfer_len == 0U)
1861     {
1862       /* set NAK on the current endpoint */
1863       PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK);
1864     }
1865 
1866     /*Need to FreeUser Buffer*/
1867     if ((wEPVal & USB_EP_DTOG_TX) == 0U)
1868     {
1869       PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U);
1870     }
1871 
1872     if (count != 0U)
1873     {
1874       USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
1875     }
1876   }
1877 
1878   return count;
1879 }
1880 
1881 
1882 /**
1883   * @brief  Manage double buffer bulk IN transaction from ISR
1884   * @param  hpcd PCD handle
1885   * @param  ep current endpoint handle
1886   * @param  wEPVal Last snapshot of EPRx register value taken in ISR
1887   * @retval HAL status
1888   */
HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef * hpcd,PCD_EPTypeDef * ep,uint16_t wEPVal)1889 static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd,
1890                                                 PCD_EPTypeDef *ep, uint16_t wEPVal)
1891 {
1892   uint32_t len;
1893   uint16_t TxPctSize;
1894 
1895   /* Data Buffer0 ACK received */
1896   if ((wEPVal & USB_EP_DTOG_TX) != 0U)
1897   {
1898     /* multi-packet on the NON control IN endpoint */
1899     TxPctSize = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
1900 
1901     if (ep->xfer_len > TxPctSize)
1902     {
1903       ep->xfer_len -= TxPctSize;
1904     }
1905     else
1906     {
1907       ep->xfer_len = 0U;
1908     }
1909 
1910     /* Transfer is completed */
1911     if (ep->xfer_len == 0U)
1912     {
1913       PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
1914       PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
1915 
1916       /* TX COMPLETE */
1917 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1918       hpcd->DataInStageCallback(hpcd, ep->num);
1919 #else
1920       HAL_PCD_DataInStageCallback(hpcd, ep->num);
1921 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1922 
1923       if ((wEPVal & USB_EP_DTOG_RX) != 0U)
1924       {
1925         PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
1926       }
1927     }
1928     else /* Transfer is not yet Done */
1929     {
1930       /* need to Free USB Buff */
1931       if ((wEPVal & USB_EP_DTOG_RX) != 0U)
1932       {
1933         PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
1934       }
1935 
1936       /* Still there is data to Fill in the next Buffer */
1937       if (ep->xfer_fill_db == 1U)
1938       {
1939         ep->xfer_buff += TxPctSize;
1940         ep->xfer_count += TxPctSize;
1941 
1942         /* Calculate the len of the new buffer to fill */
1943         if (ep->xfer_len_db >= ep->maxpacket)
1944         {
1945           len = ep->maxpacket;
1946           ep->xfer_len_db -= len;
1947         }
1948         else if (ep->xfer_len_db == 0U)
1949         {
1950           len = TxPctSize;
1951           ep->xfer_fill_db = 0U;
1952         }
1953         else
1954         {
1955           ep->xfer_fill_db = 0U;
1956           len = ep->xfer_len_db;
1957           ep->xfer_len_db = 0U;
1958         }
1959 
1960         /* Write remaining Data to Buffer */
1961         /* Set the Double buffer counter for pma buffer1 */
1962         PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, len);
1963 
1964         /* Copy user buffer to USB PMA */
1965         USB_WritePMA(hpcd->Instance, ep->xfer_buff,  ep->pmaaddr0, (uint16_t)len);
1966       }
1967     }
1968   }
1969   else /* Data Buffer1 ACK received */
1970   {
1971     /* multi-packet on the NON control IN endpoint */
1972     TxPctSize = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
1973 
1974     if (ep->xfer_len >= TxPctSize)
1975     {
1976       ep->xfer_len -= TxPctSize;
1977     }
1978     else
1979     {
1980       ep->xfer_len = 0U;
1981     }
1982 
1983     /* Transfer is completed */
1984     if (ep->xfer_len == 0U)
1985     {
1986       PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
1987       PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U);
1988 
1989       /* TX COMPLETE */
1990 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1991       hpcd->DataInStageCallback(hpcd, ep->num);
1992 #else
1993       HAL_PCD_DataInStageCallback(hpcd, ep->num);
1994 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1995 
1996       /* need to Free USB Buff */
1997       if ((wEPVal & USB_EP_DTOG_RX) == 0U)
1998       {
1999         PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
2000       }
2001     }
2002     else /* Transfer is not yet Done */
2003     {
2004       /* need to Free USB Buff */
2005       if ((wEPVal & USB_EP_DTOG_RX) == 0U)
2006       {
2007         PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U);
2008       }
2009 
2010       /* Still there is data to Fill in the next Buffer */
2011       if (ep->xfer_fill_db == 1U)
2012       {
2013         ep->xfer_buff += TxPctSize;
2014         ep->xfer_count += TxPctSize;
2015 
2016         /* Calculate the len of the new buffer to fill */
2017         if (ep->xfer_len_db >= ep->maxpacket)
2018         {
2019           len = ep->maxpacket;
2020           ep->xfer_len_db -= len;
2021         }
2022         else if (ep->xfer_len_db == 0U)
2023         {
2024           len = TxPctSize;
2025           ep->xfer_fill_db = 0U;
2026         }
2027         else
2028         {
2029           len = ep->xfer_len_db;
2030           ep->xfer_len_db = 0U;
2031           ep->xfer_fill_db = 0;
2032         }
2033 
2034         /* Set the Double buffer counter for pmabuffer1 */
2035         PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, len);
2036 
2037         /* Copy the user buffer to USB PMA */
2038         USB_WritePMA(hpcd->Instance, ep->xfer_buff,  ep->pmaaddr1, (uint16_t)len);
2039       }
2040     }
2041   }
2042 
2043   /*enable endpoint IN*/
2044   PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_VALID);
2045 
2046   return HAL_OK;
2047 }
2048 #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
2049 
2050 
2051 
2052 /**
2053   * @}
2054   */
2055 #endif /* defined (USB) */
2056 #endif /* HAL_PCD_MODULE_ENABLED */
2057 
2058 /**
2059   * @}
2060   */
2061 
2062 /**
2063   * @}
2064   */
2065