STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_pcd.c
Go to the documentation of this file.
1 
56 /* Includes ------------------------------------------------------------------*/
57 #include "stm32l4xx_hal.h"
58 
68 #ifdef HAL_PCD_MODULE_ENABLED
69 
70 #if defined (USB) || defined (USB_OTG_FS)
71 
72 /* Private types -------------------------------------------------------------*/
73 /* Private variables ---------------------------------------------------------*/
74 /* Private constants ---------------------------------------------------------*/
75 /* Private macros ------------------------------------------------------------*/
79 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
80 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
81 
85 /* Private functions prototypes ----------------------------------------------*/
89 #if defined (USB_OTG_FS)
90 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
91 static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
92 static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
93 #endif /* defined (USB_OTG_FS) */
94 
95 #if defined (USB)
96 static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
97 #endif /* defined (USB) */
98 
102 /* Exported functions --------------------------------------------------------*/
126 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
127 {
128 #if defined (USB_OTG_FS)
129  USB_OTG_GlobalTypeDef *USBx;
130 #endif /* defined (USB_OTG_FS) */
131  uint8_t i;
132 
133  /* Check the PCD handle allocation */
134  if (hpcd == NULL)
135  {
136  return HAL_ERROR;
137  }
138 
139  /* Check the parameters */
140  assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
141 
142 #if defined (USB_OTG_FS)
143  USBx = hpcd->Instance;
144 #endif /* defined (USB_OTG_FS) */
145 
146  if (hpcd->State == HAL_PCD_STATE_RESET)
147  {
148  /* Allocate lock resource and initialize it */
149  hpcd->Lock = HAL_UNLOCKED;
150 
151 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
152  hpcd->SOFCallback = HAL_PCD_SOFCallback;
153  hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
154  hpcd->ResetCallback = HAL_PCD_ResetCallback;
155  hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
156  hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
157  hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
158  hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
159  hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
160  hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
161  hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
162  hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
163  hpcd->LPMCallback = HAL_PCDEx_LPM_Callback;
164  hpcd->BCDCallback = HAL_PCDEx_BCD_Callback;
165 
166  if (hpcd->MspInitCallback == NULL)
167  {
168  hpcd->MspInitCallback = HAL_PCD_MspInit;
169  }
170 
171  /* Init the low level hardware */
172  hpcd->MspInitCallback(hpcd);
173 #else
174  /* Init the low level hardware : GPIO, CLOCK, NVIC... */
175  HAL_PCD_MspInit(hpcd);
176 #endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
177  }
178 
179  hpcd->State = HAL_PCD_STATE_BUSY;
180 
181 #if defined (USB_OTG_FS)
182  /* Disable DMA mode for FS instance */
183  if ((USBx->CID & (0x1U << 8)) == 0U)
184  {
185  hpcd->Init.dma_enable = 0U;
186  }
187 #endif /* defined (USB_OTG_FS) */
188 
189  /* Disable the Interrupts */
190  __HAL_PCD_DISABLE(hpcd);
191 
192  /*Init the Core (common init.) */
193  if (USB_CoreInit(hpcd->Instance, hpcd->Init) != HAL_OK)
194  {
195  hpcd->State = HAL_PCD_STATE_ERROR;
196  return HAL_ERROR;
197  }
198 
199  /* Force Device Mode*/
200  (void)USB_SetCurrentMode(hpcd->Instance, USB_DEVICE_MODE);
201 
202  /* Init endpoints structures */
203  for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
204  {
205  /* Init ep structure */
206  hpcd->IN_ep[i].is_in = 1U;
207  hpcd->IN_ep[i].num = i;
208  hpcd->IN_ep[i].tx_fifo_num = i;
209  /* Control until ep is activated */
210  hpcd->IN_ep[i].type = EP_TYPE_CTRL;
211  hpcd->IN_ep[i].maxpacket = 0U;
212  hpcd->IN_ep[i].xfer_buff = 0U;
213  hpcd->IN_ep[i].xfer_len = 0U;
214  }
215 
216  for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
217  {
218  hpcd->OUT_ep[i].is_in = 0U;
219  hpcd->OUT_ep[i].num = i;
220  /* Control until ep is activated */
221  hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
222  hpcd->OUT_ep[i].maxpacket = 0U;
223  hpcd->OUT_ep[i].xfer_buff = 0U;
224  hpcd->OUT_ep[i].xfer_len = 0U;
225  }
226 
227  /* Init Device */
228  if (USB_DevInit(hpcd->Instance, hpcd->Init) != HAL_OK)
229  {
230  hpcd->State = HAL_PCD_STATE_ERROR;
231  return HAL_ERROR;
232  }
233 
234  hpcd->USB_Address = 0U;
235  hpcd->State = HAL_PCD_STATE_READY;
236 
237  /* Activate LPM */
238  if (hpcd->Init.lpm_enable == 1U)
239  {
240  (void)HAL_PCDEx_ActivateLPM(hpcd);
241  }
242 
243  (void)USB_DevDisconnect(hpcd->Instance);
244 
245  return HAL_OK;
246 }
247 
253 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
254 {
255  /* Check the PCD handle allocation */
256  if (hpcd == NULL)
257  {
258  return HAL_ERROR;
259  }
260 
261  hpcd->State = HAL_PCD_STATE_BUSY;
262 
263  /* Stop Device */
264  (void)HAL_PCD_Stop(hpcd);
265 
266 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
267  if (hpcd->MspDeInitCallback == NULL)
268  {
269  hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit */
270  }
271 
272  /* DeInit the low level hardware */
273  hpcd->MspDeInitCallback(hpcd);
274 #else
275  /* DeInit the low level hardware: CLOCK, NVIC.*/
276  HAL_PCD_MspDeInit(hpcd);
277 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
278 
279  hpcd->State = HAL_PCD_STATE_RESET;
280 
281  return HAL_OK;
282 }
283 
290 {
291  /* Prevent unused argument(s) compilation warning */
292  UNUSED(hpcd);
293 
294  /* NOTE : This function should not be modified, when the callback is needed,
295  the HAL_PCD_MspInit could be implemented in the user file
296  */
297 }
298 
305 {
306  /* Prevent unused argument(s) compilation warning */
307  UNUSED(hpcd);
308 
309  /* NOTE : This function should not be modified, when the callback is needed,
310  the HAL_PCD_MspDeInit could be implemented in the user file
311  */
312 }
313 
314 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
315 
334 {
335  HAL_StatusTypeDef status = HAL_OK;
336 
337  if (pCallback == NULL)
338  {
339  /* Update the error code */
340  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
341  return HAL_ERROR;
342  }
343  /* Process locked */
344  __HAL_LOCK(hpcd);
345 
346  if (hpcd->State == HAL_PCD_STATE_READY)
347  {
348  switch (CallbackID)
349  {
350  case HAL_PCD_SOF_CB_ID :
351  hpcd->SOFCallback = pCallback;
352  break;
353 
355  hpcd->SetupStageCallback = pCallback;
356  break;
357 
358  case HAL_PCD_RESET_CB_ID :
359  hpcd->ResetCallback = pCallback;
360  break;
361 
362  case HAL_PCD_SUSPEND_CB_ID :
363  hpcd->SuspendCallback = pCallback;
364  break;
365 
366  case HAL_PCD_RESUME_CB_ID :
367  hpcd->ResumeCallback = pCallback;
368  break;
369 
370  case HAL_PCD_CONNECT_CB_ID :
371  hpcd->ConnectCallback = pCallback;
372  break;
373 
375  hpcd->DisconnectCallback = pCallback;
376  break;
377 
378  case HAL_PCD_MSPINIT_CB_ID :
379  hpcd->MspInitCallback = pCallback;
380  break;
381 
383  hpcd->MspDeInitCallback = pCallback;
384  break;
385 
386  default :
387  /* Update the error code */
388  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
389  /* Return error status */
390  status = HAL_ERROR;
391  break;
392  }
393  }
394  else if (hpcd->State == HAL_PCD_STATE_RESET)
395  {
396  switch (CallbackID)
397  {
398  case HAL_PCD_MSPINIT_CB_ID :
399  hpcd->MspInitCallback = pCallback;
400  break;
401 
403  hpcd->MspDeInitCallback = pCallback;
404  break;
405 
406  default :
407  /* Update the error code */
408  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
409  /* Return error status */
410  status = HAL_ERROR;
411  break;
412  }
413  }
414  else
415  {
416  /* Update the error code */
417  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
418  /* Return error status */
419  status = HAL_ERROR;
420  }
421 
422  /* Release Lock */
423  __HAL_UNLOCK(hpcd);
424  return status;
425 }
426 
445 {
446  HAL_StatusTypeDef status = HAL_OK;
447 
448  /* Process locked */
449  __HAL_LOCK(hpcd);
450 
451  /* Setup Legacy weak Callbacks */
452  if (hpcd->State == HAL_PCD_STATE_READY)
453  {
454  switch (CallbackID)
455  {
456  case HAL_PCD_SOF_CB_ID :
457  hpcd->SOFCallback = HAL_PCD_SOFCallback;
458  break;
459 
461  hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
462  break;
463 
464  case HAL_PCD_RESET_CB_ID :
465  hpcd->ResetCallback = HAL_PCD_ResetCallback;
466  break;
467 
468  case HAL_PCD_SUSPEND_CB_ID :
469  hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
470  break;
471 
472  case HAL_PCD_RESUME_CB_ID :
473  hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
474  break;
475 
476  case HAL_PCD_CONNECT_CB_ID :
477  hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
478  break;
479 
481  hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
482  break;
483 
484  case HAL_PCD_MSPINIT_CB_ID :
485  hpcd->MspInitCallback = HAL_PCD_MspInit;
486  break;
487 
489  hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
490  break;
491 
492  default :
493  /* Update the error code */
494  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
495 
496  /* Return error status */
497  status = HAL_ERROR;
498  break;
499  }
500  }
501  else if (hpcd->State == HAL_PCD_STATE_RESET)
502  {
503  switch (CallbackID)
504  {
505  case HAL_PCD_MSPINIT_CB_ID :
506  hpcd->MspInitCallback = HAL_PCD_MspInit;
507  break;
508 
510  hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
511  break;
512 
513  default :
514  /* Update the error code */
515  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
516 
517  /* Return error status */
518  status = HAL_ERROR;
519  break;
520  }
521  }
522  else
523  {
524  /* Update the error code */
525  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
526 
527  /* Return error status */
528  status = HAL_ERROR;
529  }
530 
531  /* Release Lock */
532  __HAL_UNLOCK(hpcd);
533  return status;
534 }
535 
544 {
545  HAL_StatusTypeDef status = HAL_OK;
546 
547  if (pCallback == NULL)
548  {
549  /* Update the error code */
550  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
551 
552  return HAL_ERROR;
553  }
554 
555  /* Process locked */
556  __HAL_LOCK(hpcd);
557 
558  if (hpcd->State == HAL_PCD_STATE_READY)
559  {
560  hpcd->DataOutStageCallback = pCallback;
561  }
562  else
563  {
564  /* Update the error code */
565  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
566 
567  /* Return error status */
568  status = HAL_ERROR;
569  }
570 
571  /* Release Lock */
572  __HAL_UNLOCK(hpcd);
573 
574  return status;
575 }
576 
584 {
585  HAL_StatusTypeDef status = HAL_OK;
586 
587  /* Process locked */
588  __HAL_LOCK(hpcd);
589 
590  if (hpcd->State == HAL_PCD_STATE_READY)
591  {
592  hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback */
593  }
594  else
595  {
596  /* Update the error code */
597  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
598 
599  /* Return error status */
600  status = HAL_ERROR;
601  }
602 
603  /* Release Lock */
604  __HAL_UNLOCK(hpcd);
605 
606  return status;
607 }
608 
617 {
618  HAL_StatusTypeDef status = HAL_OK;
619 
620  if (pCallback == NULL)
621  {
622  /* Update the error code */
623  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
624 
625  return HAL_ERROR;
626  }
627 
628  /* Process locked */
629  __HAL_LOCK(hpcd);
630 
631  if (hpcd->State == HAL_PCD_STATE_READY)
632  {
633  hpcd->DataInStageCallback = pCallback;
634  }
635  else
636  {
637  /* Update the error code */
638  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
639 
640  /* Return error status */
641  status = HAL_ERROR;
642  }
643 
644  /* Release Lock */
645  __HAL_UNLOCK(hpcd);
646 
647  return status;
648 }
649 
657 {
658  HAL_StatusTypeDef status = HAL_OK;
659 
660  /* Process locked */
661  __HAL_LOCK(hpcd);
662 
663  if (hpcd->State == HAL_PCD_STATE_READY)
664  {
665  hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback */
666  }
667  else
668  {
669  /* Update the error code */
670  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
671 
672  /* Return error status */
673  status = HAL_ERROR;
674  }
675 
676  /* Release Lock */
677  __HAL_UNLOCK(hpcd);
678 
679  return status;
680 }
681 
690 {
691  HAL_StatusTypeDef status = HAL_OK;
692 
693  if (pCallback == NULL)
694  {
695  /* Update the error code */
696  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
697 
698  return HAL_ERROR;
699  }
700 
701  /* Process locked */
702  __HAL_LOCK(hpcd);
703 
704  if (hpcd->State == HAL_PCD_STATE_READY)
705  {
706  hpcd->ISOOUTIncompleteCallback = pCallback;
707  }
708  else
709  {
710  /* Update the error code */
711  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
712 
713  /* Return error status */
714  status = HAL_ERROR;
715  }
716 
717  /* Release Lock */
718  __HAL_UNLOCK(hpcd);
719 
720  return status;
721 }
722 
730 {
731  HAL_StatusTypeDef status = HAL_OK;
732 
733  /* Process locked */
734  __HAL_LOCK(hpcd);
735 
736  if (hpcd->State == HAL_PCD_STATE_READY)
737  {
738  hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback */
739  }
740  else
741  {
742  /* Update the error code */
743  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
744 
745  /* Return error status */
746  status = HAL_ERROR;
747  }
748 
749  /* Release Lock */
750  __HAL_UNLOCK(hpcd);
751 
752  return status;
753 }
754 
763 {
764  HAL_StatusTypeDef status = HAL_OK;
765 
766  if (pCallback == NULL)
767  {
768  /* Update the error code */
769  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
770 
771  return HAL_ERROR;
772  }
773 
774  /* Process locked */
775  __HAL_LOCK(hpcd);
776 
777  if (hpcd->State == HAL_PCD_STATE_READY)
778  {
779  hpcd->ISOINIncompleteCallback = pCallback;
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 
803 {
804  HAL_StatusTypeDef status = HAL_OK;
805 
806  /* Process locked */
807  __HAL_LOCK(hpcd);
808 
809  if (hpcd->State == HAL_PCD_STATE_READY)
810  {
811  hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback */
812  }
813  else
814  {
815  /* Update the error code */
816  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
817 
818  /* Return error status */
819  status = HAL_ERROR;
820  }
821 
822  /* Release Lock */
823  __HAL_UNLOCK(hpcd);
824 
825  return status;
826 }
827 
836 {
837  HAL_StatusTypeDef status = HAL_OK;
838 
839  if (pCallback == NULL)
840  {
841  /* Update the error code */
842  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
843 
844  return HAL_ERROR;
845  }
846 
847  /* Process locked */
848  __HAL_LOCK(hpcd);
849 
850  if (hpcd->State == HAL_PCD_STATE_READY)
851  {
852  hpcd->BCDCallback = pCallback;
853  }
854  else
855  {
856  /* Update the error code */
857  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
858 
859  /* Return error status */
860  status = HAL_ERROR;
861  }
862 
863  /* Release Lock */
864  __HAL_UNLOCK(hpcd);
865 
866  return status;
867 }
868 
876 {
877  HAL_StatusTypeDef status = HAL_OK;
878 
879  /* Process locked */
880  __HAL_LOCK(hpcd);
881 
882  if (hpcd->State == HAL_PCD_STATE_READY)
883  {
884  hpcd->BCDCallback = HAL_PCDEx_BCD_Callback; /* Legacy weak HAL_PCDEx_BCD_Callback */
885  }
886  else
887  {
888  /* Update the error code */
889  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
890 
891  /* Return error status */
892  status = HAL_ERROR;
893  }
894 
895  /* Release Lock */
896  __HAL_UNLOCK(hpcd);
897 
898  return status;
899 }
900 
909 {
910  HAL_StatusTypeDef status = HAL_OK;
911 
912  if (pCallback == NULL)
913  {
914  /* Update the error code */
915  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
916 
917  return HAL_ERROR;
918  }
919 
920  /* Process locked */
921  __HAL_LOCK(hpcd);
922 
923  if (hpcd->State == HAL_PCD_STATE_READY)
924  {
925  hpcd->LPMCallback = pCallback;
926  }
927  else
928  {
929  /* Update the error code */
930  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
931 
932  /* Return error status */
933  status = HAL_ERROR;
934  }
935 
936  /* Release Lock */
937  __HAL_UNLOCK(hpcd);
938 
939  return status;
940 }
941 
949 {
950  HAL_StatusTypeDef status = HAL_OK;
951 
952  /* Process locked */
953  __HAL_LOCK(hpcd);
954 
955  if (hpcd->State == HAL_PCD_STATE_READY)
956  {
957  hpcd->LPMCallback = HAL_PCDEx_LPM_Callback; /* Legacy weak HAL_PCDEx_LPM_Callback */
958  }
959  else
960  {
961  /* Update the error code */
962  hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
963 
964  /* Return error status */
965  status = HAL_ERROR;
966  }
967 
968  /* Release Lock */
969  __HAL_UNLOCK(hpcd);
970 
971  return status;
972 }
973 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
974 
999 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
1000 {
1001 #if defined (USB_OTG_FS)
1002  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1003 #endif /* defined (USB_OTG_FS) */
1004 
1005  __HAL_LOCK(hpcd);
1006 #if defined (USB_OTG_FS)
1007  if (hpcd->Init.battery_charging_enable == 1U)
1008  {
1009  /* Enable USB Transceiver */
1010  USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
1011  }
1012 #endif /* defined (USB_OTG_FS) */
1013  (void)USB_DevConnect(hpcd->Instance);
1014  __HAL_PCD_ENABLE(hpcd);
1015  __HAL_UNLOCK(hpcd);
1016  return HAL_OK;
1017 }
1018 
1024 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
1025 {
1026  __HAL_LOCK(hpcd);
1027  __HAL_PCD_DISABLE(hpcd);
1028 
1029  if (USB_StopDevice(hpcd->Instance) != HAL_OK)
1030  {
1031  __HAL_UNLOCK(hpcd);
1032  return HAL_ERROR;
1033  }
1034 
1035  (void)USB_DevDisconnect(hpcd->Instance);
1036  __HAL_UNLOCK(hpcd);
1037 
1038  return HAL_OK;
1039 }
1040 #if defined (USB_OTG_FS)
1041 
1047 {
1048  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1049  uint32_t USBx_BASE = (uint32_t)USBx;
1050  uint32_t i, ep_intr, epint, epnum = 0U;
1051  uint32_t fifoemptymsk, temp;
1052  USB_OTG_EPTypeDef *ep;
1053 
1054  /* ensure that we are in device mode */
1055  if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
1056  {
1057  /* avoid spurious interrupt */
1058  if (__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
1059  {
1060  return;
1061  }
1062 
1063  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
1064  {
1065  /* incorrect mode, acknowledge the interrupt */
1066  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
1067  }
1068 
1069  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
1070  {
1071  epnum = 0U;
1072 
1073  /* Read in the device interrupt bits */
1074  ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
1075 
1076  while (ep_intr != 0U)
1077  {
1078  if ((ep_intr & 0x1U) != 0U)
1079  {
1080  epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, (uint8_t)epnum);
1081 
1082  if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
1083  {
1084  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
1085  (void)PCD_EP_OutXfrComplete_int(hpcd, epnum);
1086  }
1087 
1088  if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
1089  {
1090  /* Class B setup phase done for previous decoded setup */
1091  (void)PCD_EP_OutSetupPacket_int(hpcd, epnum);
1092  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
1093  }
1094 
1095  if ((epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
1096  {
1097  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
1098  }
1099 
1100  /* Clear Status Phase Received interrupt */
1101  if ((epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
1102  {
1103  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
1104  }
1105 
1106  /* Clear OUT NAK interrupt */
1107  if ((epint & USB_OTG_DOEPINT_NAK) == USB_OTG_DOEPINT_NAK)
1108  {
1109  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_NAK);
1110  }
1111  }
1112  epnum++;
1113  ep_intr >>= 1U;
1114  }
1115  }
1116 
1117  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
1118  {
1119  /* Read in the device interrupt bits */
1120  ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
1121 
1122  epnum = 0U;
1123 
1124  while (ep_intr != 0U)
1125  {
1126  if ((ep_intr & 0x1U) != 0U) /* In ITR */
1127  {
1128  epint = USB_ReadDevInEPInterrupt(hpcd->Instance, (uint8_t)epnum);
1129 
1130  if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
1131  {
1132  fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
1133  USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
1134 
1135  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
1136 
1137 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1138  hpcd->DataInStageCallback(hpcd, (uint8_t)epnum);
1139 #else
1140  HAL_PCD_DataInStageCallback(hpcd, (uint8_t)epnum);
1141 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1142  }
1143  if ((epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
1144  {
1145  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
1146  }
1147  if ((epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
1148  {
1149  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
1150  }
1151  if ((epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
1152  {
1153  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
1154  }
1155  if ((epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
1156  {
1157  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
1158  }
1159  if ((epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
1160  {
1161  (void)PCD_WriteEmptyTxFifo(hpcd, epnum);
1162  }
1163  }
1164  epnum++;
1165  ep_intr >>= 1U;
1166  }
1167  }
1168 
1169  /* Handle Resume Interrupt */
1170  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
1171  {
1172  /* Clear the Remote Wake-up Signaling */
1173  USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
1174 
1175  if (hpcd->LPM_State == LPM_L1)
1176  {
1177  hpcd->LPM_State = LPM_L0;
1178 
1179 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1180  hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
1181 #else
1183 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1184  }
1185  else
1186  {
1187 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1188  hpcd->ResumeCallback(hpcd);
1189 #else
1190  HAL_PCD_ResumeCallback(hpcd);
1191 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1192  }
1193 
1194  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
1195  }
1196 
1197  /* Handle Suspend Interrupt */
1198  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
1199  {
1200  if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
1201  {
1202 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1203  hpcd->SuspendCallback(hpcd);
1204 #else
1206 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1207  }
1208  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
1209  }
1210 
1211  /* Handle LPM Interrupt */
1212  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
1213  {
1214  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
1215 
1216  if (hpcd->LPM_State == LPM_L0)
1217  {
1218  hpcd->LPM_State = LPM_L1;
1219  hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
1220 
1221 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1222  hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
1223 #else
1225 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1226  }
1227  else
1228  {
1229 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1230  hpcd->SuspendCallback(hpcd);
1231 #else
1233 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1234  }
1235  }
1236 
1237  /* Handle Reset Interrupt */
1238  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
1239  {
1240  USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
1241  (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
1242 
1243  for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
1244  {
1245  USBx_INEP(i)->DIEPINT = 0xFB7FU;
1246  USBx_INEP(i)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
1247  USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
1248  USBx_OUTEP(i)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
1249  }
1250  USBx_DEVICE->DAINTMSK |= 0x10001U;
1251 
1252  if (hpcd->Init.use_dedicated_ep1 != 0U)
1253  {
1254  USBx_DEVICE->DOUTEP1MSK |= USB_OTG_DOEPMSK_STUPM |
1255  USB_OTG_DOEPMSK_XFRCM |
1256  USB_OTG_DOEPMSK_EPDM;
1257 
1258  USBx_DEVICE->DINEP1MSK |= USB_OTG_DIEPMSK_TOM |
1259  USB_OTG_DIEPMSK_XFRCM |
1260  USB_OTG_DIEPMSK_EPDM;
1261  }
1262  else
1263  {
1264  USBx_DEVICE->DOEPMSK |= USB_OTG_DOEPMSK_STUPM |
1265  USB_OTG_DOEPMSK_XFRCM |
1266  USB_OTG_DOEPMSK_EPDM |
1267  USB_OTG_DOEPMSK_OTEPSPRM |
1268  USB_OTG_DOEPMSK_NAKM;
1269 
1270  USBx_DEVICE->DIEPMSK |= USB_OTG_DIEPMSK_TOM |
1271  USB_OTG_DIEPMSK_XFRCM |
1272  USB_OTG_DIEPMSK_EPDM;
1273  }
1274 
1275  /* Set Default Address to 0 */
1276  USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
1277 
1278  /* setup EP0 to receive SETUP packets */
1279  (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t *)hpcd->Setup);
1280 
1281  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
1282  }
1283 
1284  /* Handle Enumeration done Interrupt */
1285  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
1286  {
1287  (void)USB_ActivateSetup(hpcd->Instance);
1288  hpcd->Init.speed = USB_GetDevSpeed(hpcd->Instance);
1289 
1290  /* Set USB Turnaround time */
1291  (void)USB_SetTurnaroundTime(hpcd->Instance,
1293  (uint8_t)hpcd->Init.speed);
1294 
1295 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1296  hpcd->ResetCallback(hpcd);
1297 #else
1298  HAL_PCD_ResetCallback(hpcd);
1299 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1300 
1301  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
1302  }
1303 
1304  /* Handle RxQLevel Interrupt */
1305  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
1306  {
1307  USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
1308 
1309  temp = USBx->GRXSTSP;
1310 
1311  ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
1312 
1313  if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
1314  {
1315  if ((temp & USB_OTG_GRXSTSP_BCNT) != 0U)
1316  {
1317  (void)USB_ReadPacket(USBx, ep->xfer_buff,
1318  (uint16_t)((temp & USB_OTG_GRXSTSP_BCNT) >> 4));
1319 
1320  ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
1321  ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
1322  }
1323  }
1324  else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
1325  {
1326  (void)USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
1327  ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
1328  }
1329  else
1330  {
1331  /* ... */
1332  }
1333  USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
1334  }
1335 
1336  /* Handle SOF Interrupt */
1337  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
1338  {
1339 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1340  hpcd->SOFCallback(hpcd);
1341 #else
1342  HAL_PCD_SOFCallback(hpcd);
1343 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1344 
1345  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
1346  }
1347 
1348  /* Handle Incomplete ISO IN Interrupt */
1349  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
1350  {
1351 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1352  hpcd->ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
1353 #else
1354  HAL_PCD_ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
1355 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1356 
1357  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
1358  }
1359 
1360  /* Handle Incomplete ISO OUT Interrupt */
1361  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
1362  {
1363 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1364  hpcd->ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
1365 #else
1366  HAL_PCD_ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
1367 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1368 
1369  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
1370  }
1371 
1372  /* Handle Connection event Interrupt */
1373  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
1374  {
1375 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1376  hpcd->ConnectCallback(hpcd);
1377 #else
1379 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1380 
1381  __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
1382  }
1383 
1384  /* Handle Disconnection event Interrupt */
1385  if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
1386  {
1387  temp = hpcd->Instance->GOTGINT;
1388 
1389  if ((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
1390  {
1391 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1392  hpcd->DisconnectCallback(hpcd);
1393 #else
1395 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1396  }
1397  hpcd->Instance->GOTGINT |= temp;
1398  }
1399  }
1400 }
1401 #endif /* defined (USB_OTG_FS) */
1402 
1403 #if defined (USB)
1404 
1410 {
1411  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_CTR))
1412  {
1413  /* servicing of the endpoint correct transfer interrupt */
1414  /* clear of the CTR flag into the sub */
1415  (void)PCD_EP_ISR_Handler(hpcd);
1416  }
1417 
1418  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_RESET))
1419  {
1420  __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
1421 
1422 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1423  hpcd->ResetCallback(hpcd);
1424 #else
1425  HAL_PCD_ResetCallback(hpcd);
1426 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1427 
1428  (void)HAL_PCD_SetAddress(hpcd, 0U);
1429  }
1430 
1431  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_PMAOVR))
1432  {
1433  __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
1434  }
1435 
1436  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_ERR))
1437  {
1438  __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
1439  }
1440 
1441  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_WKUP))
1442  {
1443  hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_LPMODE);
1444  hpcd->Instance->CNTR &= (uint16_t) ~(USB_CNTR_FSUSP);
1445 
1446  if (hpcd->LPM_State == LPM_L1)
1447  {
1448  hpcd->LPM_State = LPM_L0;
1449 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1450  hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
1451 #else
1453 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1454  }
1455 
1456 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1457  hpcd->ResumeCallback(hpcd);
1458 #else
1459  HAL_PCD_ResumeCallback(hpcd);
1460 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1461 
1462  __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
1463  }
1464 
1465  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_SUSP))
1466  {
1467  /* Force low-power mode in the macrocell */
1468  hpcd->Instance->CNTR |= USB_CNTR_FSUSP;
1469 
1470  /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
1471  __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
1472 
1473  hpcd->Instance->CNTR |= USB_CNTR_LPMODE;
1474 
1475  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_WKUP) == 0U)
1476  {
1477 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1478  hpcd->SuspendCallback(hpcd);
1479 #else
1481 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1482  }
1483  }
1484 
1485  /* Handle LPM Interrupt */
1486  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_L1REQ))
1487  {
1488  __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_L1REQ);
1489  if (hpcd->LPM_State == LPM_L0)
1490  {
1491  /* Force suspend and low-power mode before going to L1 state*/
1492  hpcd->Instance->CNTR |= USB_CNTR_LPMODE;
1493  hpcd->Instance->CNTR |= USB_CNTR_FSUSP;
1494 
1495  hpcd->LPM_State = LPM_L1;
1496  hpcd->BESL = ((uint32_t)hpcd->Instance->LPMCSR & USB_LPMCSR_BESL) >> 2;
1497 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1498  hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
1499 #else
1501 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1502  }
1503  else
1504  {
1505 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1506  hpcd->SuspendCallback(hpcd);
1507 #else
1509 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1510  }
1511  }
1512 
1513  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_SOF))
1514  {
1515  __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
1516 
1517 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
1518  hpcd->SOFCallback(hpcd);
1519 #else
1520  HAL_PCD_SOFCallback(hpcd);
1521 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
1522  }
1523 
1524  if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_ESOF))
1525  {
1526  /* clear ESOF flag in ISTR */
1527  __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
1528  }
1529 }
1530 #endif /* defined (USB) */
1531 
1538 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1539 {
1540  /* Prevent unused argument(s) compilation warning */
1541  UNUSED(hpcd);
1542  UNUSED(epnum);
1543 
1544  /* NOTE : This function should not be modified, when the callback is needed,
1545  the HAL_PCD_DataOutStageCallback could be implemented in the user file
1546  */
1547 }
1548 
1555 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1556 {
1557  /* Prevent unused argument(s) compilation warning */
1558  UNUSED(hpcd);
1559  UNUSED(epnum);
1560 
1561  /* NOTE : This function should not be modified, when the callback is needed,
1562  the HAL_PCD_DataInStageCallback could be implemented in the user file
1563  */
1564 }
1571 {
1572  /* Prevent unused argument(s) compilation warning */
1573  UNUSED(hpcd);
1574 
1575  /* NOTE : This function should not be modified, when the callback is needed,
1576  the HAL_PCD_SetupStageCallback could be implemented in the user file
1577  */
1578 }
1579 
1586 {
1587  /* Prevent unused argument(s) compilation warning */
1588  UNUSED(hpcd);
1589 
1590  /* NOTE : This function should not be modified, when the callback is needed,
1591  the HAL_PCD_SOFCallback could be implemented in the user file
1592  */
1593 }
1594 
1601 {
1602  /* Prevent unused argument(s) compilation warning */
1603  UNUSED(hpcd);
1604 
1605  /* NOTE : This function should not be modified, when the callback is needed,
1606  the HAL_PCD_ResetCallback could be implemented in the user file
1607  */
1608 }
1609 
1616 {
1617  /* Prevent unused argument(s) compilation warning */
1618  UNUSED(hpcd);
1619 
1620  /* NOTE : This function should not be modified, when the callback is needed,
1621  the HAL_PCD_SuspendCallback could be implemented in the user file
1622  */
1623 }
1624 
1631 {
1632  /* Prevent unused argument(s) compilation warning */
1633  UNUSED(hpcd);
1634 
1635  /* NOTE : This function should not be modified, when the callback is needed,
1636  the HAL_PCD_ResumeCallback could be implemented in the user file
1637  */
1638 }
1639 
1646 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1647 {
1648  /* Prevent unused argument(s) compilation warning */
1649  UNUSED(hpcd);
1650  UNUSED(epnum);
1651 
1652  /* NOTE : This function should not be modified, when the callback is needed,
1653  the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
1654  */
1655 }
1656 
1663 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
1664 {
1665  /* Prevent unused argument(s) compilation warning */
1666  UNUSED(hpcd);
1667  UNUSED(epnum);
1668 
1669  /* NOTE : This function should not be modified, when the callback is needed,
1670  the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
1671  */
1672 }
1673 
1680 {
1681  /* Prevent unused argument(s) compilation warning */
1682  UNUSED(hpcd);
1683 
1684  /* NOTE : This function should not be modified, when the callback is needed,
1685  the HAL_PCD_ConnectCallback could be implemented in the user file
1686  */
1687 }
1688 
1695 {
1696  /* Prevent unused argument(s) compilation warning */
1697  UNUSED(hpcd);
1698 
1699  /* NOTE : This function should not be modified, when the callback is needed,
1700  the HAL_PCD_DisconnectCallback could be implemented in the user file
1701  */
1702 }
1703 
1728 HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
1729 {
1730 #if defined (USB_OTG_FS)
1731  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1732 #endif /* defined (USB_OTG_FS) */
1733 
1734  __HAL_LOCK(hpcd);
1735 #if defined (USB_OTG_FS)
1736  if (hpcd->Init.battery_charging_enable == 1U)
1737  {
1738  /* Enable USB Transceiver */
1739  USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
1740  }
1741 #endif /* defined (USB_OTG_FS) */
1742  (void)USB_DevConnect(hpcd->Instance);
1743  __HAL_UNLOCK(hpcd);
1744  return HAL_OK;
1745 }
1746 
1753 {
1754 #if defined (USB_OTG_FS)
1755  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1756 
1757 #endif /* defined (USB_OTG_FS) */
1758  __HAL_LOCK(hpcd);
1759  (void)USB_DevDisconnect(hpcd->Instance);
1760 #if defined (USB_OTG_FS)
1761  if (hpcd->Init.battery_charging_enable == 1U)
1762  {
1763  /* Disable USB Transceiver */
1764  USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
1765  }
1766 #endif /* defined (USB_OTG_FS) */
1767  __HAL_UNLOCK(hpcd);
1768  return HAL_OK;
1769 }
1770 
1777 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
1778 {
1779  __HAL_LOCK(hpcd);
1780  hpcd->USB_Address = address;
1781  (void)USB_SetDevAddress(hpcd->Instance, address);
1782  __HAL_UNLOCK(hpcd);
1783  return HAL_OK;
1784 }
1793 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
1794 {
1795  HAL_StatusTypeDef ret = HAL_OK;
1796  PCD_EPTypeDef *ep;
1797 
1798  if ((ep_addr & 0x80U) == 0x80U)
1799  {
1800  ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1801  ep->is_in = 1U;
1802  }
1803  else
1804  {
1805  ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1806  ep->is_in = 0U;
1807  }
1808 
1809  ep->num = ep_addr & EP_ADDR_MSK;
1810  ep->maxpacket = ep_mps;
1811  ep->type = ep_type;
1812 
1813  if (ep->is_in != 0U)
1814  {
1815  /* Assign a Tx FIFO */
1816  ep->tx_fifo_num = ep->num;
1817  }
1818  /* Set initial data PID. */
1819  if (ep_type == EP_TYPE_BULK)
1820  {
1821  ep->data_pid_start = 0U;
1822  }
1823 
1824  __HAL_LOCK(hpcd);
1825  (void)USB_ActivateEndpoint(hpcd->Instance, ep);
1826  __HAL_UNLOCK(hpcd);
1827 
1828  return ret;
1829 }
1830 
1837 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1838 {
1839  PCD_EPTypeDef *ep;
1840 
1841  if ((ep_addr & 0x80U) == 0x80U)
1842  {
1843  ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1844  ep->is_in = 1U;
1845  }
1846  else
1847  {
1848  ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1849  ep->is_in = 0U;
1850  }
1851  ep->num = ep_addr & EP_ADDR_MSK;
1852 
1853  __HAL_LOCK(hpcd);
1854  (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
1855  __HAL_UNLOCK(hpcd);
1856  return HAL_OK;
1857 }
1858 
1859 
1868 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1869 {
1870  PCD_EPTypeDef *ep;
1871 
1872  ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
1873 
1874  /*setup and start the Xfer */
1875  ep->xfer_buff = pBuf;
1876  ep->xfer_len = len;
1877  ep->xfer_count = 0U;
1878  ep->is_in = 0U;
1879  ep->num = ep_addr & EP_ADDR_MSK;
1880 
1881  if ((ep_addr & EP_ADDR_MSK) == 0U)
1882  {
1883  (void)USB_EP0StartXfer(hpcd->Instance, ep);
1884  }
1885  else
1886  {
1887  (void)USB_EPStartXfer(hpcd->Instance, ep);
1888  }
1889 
1890  return HAL_OK;
1891 }
1892 
1899 uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1900 {
1901  return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
1902 }
1911 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1912 {
1913  PCD_EPTypeDef *ep;
1914 
1915  ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1916 
1917  /*setup and start the Xfer */
1918  ep->xfer_buff = pBuf;
1919  ep->xfer_len = len;
1920  ep->xfer_count = 0U;
1921  ep->is_in = 1U;
1922  ep->num = ep_addr & EP_ADDR_MSK;
1923 
1924  if ((ep_addr & EP_ADDR_MSK) == 0U)
1925  {
1926  (void)USB_EP0StartXfer(hpcd->Instance, ep);
1927  }
1928  else
1929  {
1930  (void)USB_EPStartXfer(hpcd->Instance, ep);
1931  }
1932 
1933  return HAL_OK;
1934 }
1935 
1942 HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1943 {
1944  PCD_EPTypeDef *ep;
1945 
1946  if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
1947  {
1948  return HAL_ERROR;
1949  }
1950 
1951  if ((0x80U & ep_addr) == 0x80U)
1952  {
1953  ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1954  ep->is_in = 1U;
1955  }
1956  else
1957  {
1958  ep = &hpcd->OUT_ep[ep_addr];
1959  ep->is_in = 0U;
1960  }
1961 
1962  ep->is_stall = 1U;
1963  ep->num = ep_addr & EP_ADDR_MSK;
1964 
1965  __HAL_LOCK(hpcd);
1966 
1967  (void)USB_EPSetStall(hpcd->Instance, ep);
1968  if ((ep_addr & EP_ADDR_MSK) == 0U)
1969  {
1970  (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t *)hpcd->Setup);
1971  }
1972  __HAL_UNLOCK(hpcd);
1973 
1974  return HAL_OK;
1975 }
1976 
1983 HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1984 {
1985  PCD_EPTypeDef *ep;
1986 
1987  if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
1988  {
1989  return HAL_ERROR;
1990  }
1991 
1992  if ((0x80U & ep_addr) == 0x80U)
1993  {
1994  ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
1995  ep->is_in = 1U;
1996  }
1997  else
1998  {
1999  ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
2000  ep->is_in = 0U;
2001  }
2002 
2003  ep->is_stall = 0U;
2004  ep->num = ep_addr & EP_ADDR_MSK;
2005 
2006  __HAL_LOCK(hpcd);
2007  (void)USB_EPClearStall(hpcd->Instance, ep);
2008  __HAL_UNLOCK(hpcd);
2009 
2010  return HAL_OK;
2011 }
2012 
2019 HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
2020 {
2021  __HAL_LOCK(hpcd);
2022 
2023  if ((ep_addr & 0x80U) == 0x80U)
2024  {
2025  (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
2026  }
2027  else
2028  {
2029  (void)USB_FlushRxFifo(hpcd->Instance);
2030  }
2031 
2032  __HAL_UNLOCK(hpcd);
2033 
2034  return HAL_OK;
2035 }
2036 
2043 {
2044  return (USB_ActivateRemoteWakeup(hpcd->Instance));
2045 }
2046 
2053 {
2054  return (USB_DeActivateRemoteWakeup(hpcd->Instance));
2055 }
2056 
2082 {
2083  return hpcd->State;
2084 }
2085 
2094 /* Private functions ---------------------------------------------------------*/
2098 #if defined (USB_OTG_FS)
2099 
2105 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
2106 {
2107  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
2108  uint32_t USBx_BASE = (uint32_t)USBx;
2109  USB_OTG_EPTypeDef *ep;
2110  uint32_t len;
2111  uint32_t len32b;
2112  uint32_t fifoemptymsk;
2113 
2114  ep = &hpcd->IN_ep[epnum];
2115 
2116  if (ep->xfer_count > ep->xfer_len)
2117  {
2118  return HAL_ERROR;
2119  }
2120 
2121  len = ep->xfer_len - ep->xfer_count;
2122 
2123  if (len > ep->maxpacket)
2124  {
2125  len = ep->maxpacket;
2126  }
2127 
2128  len32b = (len + 3U) / 4U;
2129 
2130  while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= len32b) &&
2131  (ep->xfer_count < ep->xfer_len) && (ep->xfer_len != 0U))
2132  {
2133  /* Write the FIFO */
2134  len = ep->xfer_len - ep->xfer_count;
2135 
2136  if (len > ep->maxpacket)
2137  {
2138  len = ep->maxpacket;
2139  }
2140  len32b = (len + 3U) / 4U;
2141 
2142  (void)USB_WritePacket(USBx, ep->xfer_buff, (uint8_t)epnum, (uint16_t)len);
2143 
2144  ep->xfer_buff += len;
2145  ep->xfer_count += len;
2146  }
2147 
2148  if (ep->xfer_len <= ep->xfer_count)
2149  {
2150  fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
2151  USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
2152  }
2153 
2154  return HAL_OK;
2155 }
2156 
2157 
2164 static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
2165 {
2166  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
2167  uint32_t USBx_BASE = (uint32_t)USBx;
2168  uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
2169  uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
2170 
2171  if (gSNPSiD == USB_OTG_CORE_ID_310A)
2172  {
2173  /* StupPktRcvd = 1 this is a setup packet */
2174  if ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX)
2175  {
2176  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
2177  }
2178  else
2179  {
2180  if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
2181  {
2182  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
2183  }
2184 
2185 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2186  hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
2187 #else
2188  HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
2189 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2190  }
2191  }
2192  else
2193  {
2194 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2195  hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
2196 #else
2197  HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
2198 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2199  }
2200 
2201  return HAL_OK;
2202 }
2203 
2204 
2211 static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
2212 {
2213  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
2214  uint32_t USBx_BASE = (uint32_t)USBx;
2215  uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
2216  uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
2217 
2218 
2219  if ((gSNPSiD == USB_OTG_CORE_ID_310A) &&
2220  ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
2221  {
2222  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
2223  }
2224 
2225  /* Inform the upper layer that a setup packet is available */
2226 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2227  hpcd->SetupStageCallback(hpcd);
2228 #else
2230 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2231 
2232  return HAL_OK;
2233 }
2234 #endif /* defined (USB_OTG_FS) */
2235 
2236 #if defined (USB)
2237 
2242 static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
2243 {
2244  PCD_EPTypeDef *ep;
2245  uint16_t count;
2246  uint16_t wIstr;
2247  uint16_t wEPVal;
2248  uint8_t epindex;
2249 
2250  /* stay in loop while pending interrupts */
2251  while ((hpcd->Instance->ISTR & USB_ISTR_CTR) != 0U)
2252  {
2253  wIstr = hpcd->Instance->ISTR;
2254  /* extract highest priority endpoint number */
2255  epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
2256 
2257  if (epindex == 0U)
2258  {
2259  /* Decode and service control endpoint interrupt */
2260 
2261  /* DIR bit = origin of the interrupt */
2262  if ((wIstr & USB_ISTR_DIR) == 0U)
2263  {
2264  /* DIR = 0 */
2265 
2266  /* DIR = 0 => IN int */
2267  /* DIR = 0 implies that (EP_CTR_TX = 1) always */
2268  PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
2269  ep = &hpcd->IN_ep[0];
2270 
2271  ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
2272  ep->xfer_buff += ep->xfer_count;
2273 
2274  /* TX COMPLETE */
2275 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2276  hpcd->DataInStageCallback(hpcd, 0U);
2277 #else
2278  HAL_PCD_DataInStageCallback(hpcd, 0U);
2279 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2280 
2281  if ((hpcd->USB_Address > 0U) && (ep->xfer_len == 0U))
2282  {
2283  hpcd->Instance->DADDR = ((uint16_t)hpcd->USB_Address | USB_DADDR_EF);
2284  hpcd->USB_Address = 0U;
2285  }
2286  }
2287  else
2288  {
2289  /* DIR = 1 */
2290 
2291  /* DIR = 1 & CTR_RX => SETUP or OUT int */
2292  /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
2293  ep = &hpcd->OUT_ep[0];
2294  wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
2295 
2296  if ((wEPVal & USB_EP_SETUP) != 0U)
2297  {
2298  /* Get SETUP Packet*/
2299  ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
2300 
2301  USB_ReadPMA(hpcd->Instance, (uint8_t *)hpcd->Setup,
2302  ep->pmaadress, (uint16_t)ep->xfer_count);
2303 
2304  /* SETUP bit kept frozen while CTR_RX = 1*/
2305  PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
2306 
2307  /* Process SETUP Packet*/
2308 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2309  hpcd->SetupStageCallback(hpcd);
2310 #else
2312 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2313  }
2314 
2315  else if ((wEPVal & USB_EP_CTR_RX) != 0U)
2316  {
2317  PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
2318 
2319  /* Get Control Data OUT Packet*/
2320  ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
2321 
2322  if ((ep->xfer_count != 0U) && (ep->xfer_buff != 0U))
2323  {
2324  USB_ReadPMA(hpcd->Instance, ep->xfer_buff,
2325  ep->pmaadress, (uint16_t)ep->xfer_count);
2326 
2327  ep->xfer_buff += ep->xfer_count;
2328 
2329  /* Process Control Data OUT Packet*/
2330 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2331  hpcd->DataOutStageCallback(hpcd, 0U);
2332 #else
2333  HAL_PCD_DataOutStageCallback(hpcd, 0U);
2334 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2335  }
2336 
2337  PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
2338  PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
2339  }
2340  }
2341  }
2342  else
2343  {
2344  /* Decode and service non control endpoints interrupt */
2345 
2346  /* process related endpoint register */
2347  wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
2348  if ((wEPVal & USB_EP_CTR_RX) != 0U)
2349  {
2350  /* clear int flag */
2351  PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
2352  ep = &hpcd->OUT_ep[epindex];
2353 
2354  /* OUT double Buffering*/
2355  if (ep->doublebuffer == 0U)
2356  {
2357  count = (uint16_t)PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
2358  if (count != 0U)
2359  {
2360  USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
2361  }
2362  }
2363  else
2364  {
2365  if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U)
2366  {
2367  /*read from endpoint BUF0Addr buffer*/
2368  count = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
2369  if (count != 0U)
2370  {
2371  USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
2372  }
2373  }
2374  else
2375  {
2376  /*read from endpoint BUF1Addr buffer*/
2377  count = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
2378  if (count != 0U)
2379  {
2380  USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
2381  }
2382  }
2383  /* free EP OUT Buffer */
2384  PCD_FreeUserBuffer(hpcd->Instance, ep->num, 0U);
2385  }
2386  /*multi-packet on the NON control OUT endpoint*/
2387  ep->xfer_count += count;
2388  ep->xfer_buff += count;
2389 
2390  if ((ep->xfer_len == 0U) || (count < ep->maxpacket))
2391  {
2392  /* RX COMPLETE */
2393 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2394  hpcd->DataOutStageCallback(hpcd, ep->num);
2395 #else
2396  HAL_PCD_DataOutStageCallback(hpcd, ep->num);
2397 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2398  }
2399  else
2400  {
2401  (void)HAL_PCD_EP_Receive(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
2402  }
2403 
2404  } /* if((wEPVal & EP_CTR_RX) */
2405 
2406  if ((wEPVal & USB_EP_CTR_TX) != 0U)
2407  {
2408  ep = &hpcd->IN_ep[epindex];
2409 
2410  /* clear int flag */
2411  PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
2412 
2413  /*multi-packet on the NON control IN endpoint*/
2414  ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
2415  ep->xfer_buff += ep->xfer_count;
2416 
2417  /* Zero Length Packet? */
2418  if (ep->xfer_len == 0U)
2419  {
2420  /* TX COMPLETE */
2421 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
2422  hpcd->DataInStageCallback(hpcd, ep->num);
2423 #else
2424  HAL_PCD_DataInStageCallback(hpcd, ep->num);
2425 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
2426  }
2427  else
2428  {
2429  (void)HAL_PCD_EP_Transmit(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
2430  }
2431  }
2432  }
2433  }
2434  return HAL_OK;
2435 }
2436 #endif /* defined (USB) */
2437 
2441 #endif /* defined (USB) || defined (USB_OTG_FS) */
2442 #endif /* HAL_PCD_MODULE_ENABLED */
2443 
2452 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd, pPCD_DataInStageCallbackTypeDef pCallback)
Register USB PCD Data IN Stage Callback To be used instead of the weak HAL_PCD_DataInStageCallback() ...
static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
process EP OUT setup packet received interrupt.
uint32_t USB_ReadDevOutEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
Returns Device OUT EP Interrupt register.
HAL_StatusTypeDef USB_SetDevAddress(USB_OTG_GlobalTypeDef *USBx, uint8_t address)
USB_SetDevAddress : Stop the usb device mode.
void(* pPCD_CallbackTypeDef)(PCD_HandleTypeDef *hpcd)
void(* pPCD_IsoInIncpltCallbackTypeDef)(PCD_HandleTypeDef *hpcd, uint8_t epnum)
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
Resume event callback.
HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
USB_DeActivateRemoteWakeup de-active remote wakeup signalling.
HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
USB_EPStartXfer : setup and starts a transfer over an EP.
uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
Get Received Data Size.
HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID, pPCD_CallbackTypeDef pCallback)
Register a User USB PCD Callback To be used instead of the weak predefined callback.
HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
Activate and configure an endpoint.
void * USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
USB_ReadPacket : read a packet from the RX FIFO.
HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd, pPCD_IsoOutIncpltCallbackTypeDef pCallback)
Register USB PCD Iso OUT incomplete Callback To be used instead of the weak HAL_PCD_ISOOUTIncompleteC...
HAL_StatusTypeDef USB_DevInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
USB_DevInit : Initializes the USB_OTG controller registers for device mode.
void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
Initializes the PCD MSP.
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
Data OUT stage callback.
HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
UnRegister the USB PCD Data IN Stage Callback USB PCD Data OUT Stage Callback is redirected to the we...
PCD_StateTypeDef
PCD State structure definition.
void(* pPCD_DataInStageCallbackTypeDef)(PCD_HandleTypeDef *hpcd, uint8_t epnum)
HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t *psetup)
Prepare the EP0 to start the first control setup.
HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
Clear a STALL condition over in an endpoint.
HAL_StatusTypeDef HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef *hpcd)
UnRegister the USB PCD LPM Callback USB LPM Callback is redirected to the weak HAL_PCDEx_LPM_Callback...
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
USB Start Of Frame callback.
This file contains all the functions prototypes for the HAL module driver.
void(* pPCD_DataOutStageCallbackTypeDef)(PCD_HandleTypeDef *hpcd, uint8_t epnum)
HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
Deactivate an endpoint.
HAL_PCD_CallbackIDTypeDef
struct __PCD_HandleTypeDef else typedef struct endif PCD_HandleTypeDef
PCD Handle Structure definition.
HAL_StatusTypeDef HAL_PCD_UnRegisterBcdCallback(PCD_HandleTypeDef *hpcd)
UnRegister the USB PCD BCD Callback USB BCD Callback is redirected to the weak HAL_PCDEx_BCD_Callback...
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
Incomplete ISO IN callback.
HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
Disconnect the USB device.
void(* pPCD_IsoOutIncpltCallbackTypeDef)(PCD_HandleTypeDef *hpcd, uint8_t epnum)
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
Data IN stage callback.
HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
Send an amount of data.
HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len)
USB_WritePacket : Writes a packet into the Tx FIFO associated with the EP/channel.
HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
Activate LPM feature.
HAL_StatusTypeDef USB_ActivateSetup(USB_OTG_GlobalTypeDef *USBx)
Activate EP0 for Setup transactions.
static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
This function handles PCD Endpoint interrupt request.
__HAL_UNLOCK(hrtc)
HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
USB_ActivateRemoteWakeup active remote wakeup signalling.
uint32_t USB_ReadDevAllOutEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status.
void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
USB Reset callback.
HAL_StatusTypeDef HAL_PCD_RegisterBcdCallback(PCD_HandleTypeDef *hpcd, pPCD_BcdCallbackTypeDef pCallback)
Register USB PCD BCD Callback To be used instead of the weak HAL_PCDEx_BCD_Callback() predefined call...
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
Incomplete ISO OUT callback.
HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
USB_EP0StartXfer : setup and starts a transfer over the EP 0.
HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
USB_EPClearStall : Clear a stall condition over an EP.
static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
Check FIFO for the next packet to be loaded.
HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
Connect the USB device.
HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
USB_StopDevice : Stop the usb device mode.
__HAL_LOCK(hrtc)
uint32_t USB_ReadDevInEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
Returns Device IN EP Interrupt register.
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
Setup stage callback.
HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
Start the USB device.
HAL_StatusTypeDef HAL_PCD_RegisterLpmCallback(PCD_HandleTypeDef *hpcd, pPCD_LpmCallbackTypeDef pCallback)
Register USB PCD LPM Callback To be used instead of the weak HAL_PCDEx_LPM_Callback() predefined call...
HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
Set a STALL condition over an endpoint.
return HAL_OK
HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
Initializes the PCD according to the specified parameters in the PCD_InitTypeDef and initialize the a...
HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
Initializes the USB Core.
HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
Flush an endpoint.
HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
DeInitializes the PCD peripheral.
HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
UnRegister the USB PCD Data OUT Stage Callback USB PCD Data OUT Stage Callback is redirected to the w...
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
Suspend event callback.
uint32_t USB_ReadDevAllInEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status. ...
void USB_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
Copy a buffer from user memory area to packet memory area (PMA)
PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
Return the PCD handle state.
static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
process EP OUT transfer complete interrupt.
HAL_StatusTypeDef USB_SetTurnaroundTime(USB_OTG_GlobalTypeDef *USBx, uint32_t hclk, uint8_t speed)
Set the USB turnaround time.
HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
USB_FlushRxFifo : Flush Rx FIFO.
uint32_t HAL_RCC_GetHCLKFreq(void)
Return the HCLK frequency.
HAL_StatusTypeDef USB_DevDisconnect(USB_OTG_GlobalTypeDef *USBx)
USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down. ...
void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
Send LPM message to user layer callback.
HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
UnRegister the USB PCD Iso IN incomplete Callback USB PCD Iso IN incomplete Callback is redirected to...
HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
Receive an amount of data.
HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
Stop the USB device.
HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
Open and configure an endpoint.
HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd, pPCD_DataOutStageCallbackTypeDef pCallback)
Register USB PCD Data OUT Stage Callback To be used instead of the weak HAL_PCD_DataOutStageCallback(...
void(* pPCD_LpmCallbackTypeDef)(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
Set the USB Device address.
HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
UnRegister the USB PCD Iso OUT incomplete Callback USB PCD Iso OUT incomplete Callback is redirected ...
HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
Activate remote wakeup signalling.
uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx)
USB_GetDevSpeed Return the Dev Speed.
HAL_StatusTypeDef USB_DevConnect(USB_OTG_GlobalTypeDef *USBx)
USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down.
HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx, USB_ModeTypeDef mode)
USB_SetCurrentMode : Set functional mode.
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
Disconnection event callback.
HAL_StatusTypeDef USB_FlushTxFifo(USB_OTG_GlobalTypeDef *USBx, uint32_t num)
USB_OTG_FlushTxFifo : Flush a Tx FIFO.
HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
De-activate remote wakeup signalling.
HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
De-activate and de-initialize an endpoint.
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
Connection event callback.
HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
USB_EPSetStall : set a stall condition over an EP.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
uint32_t USB_GetMode(USB_OTG_GlobalTypeDef *USBx)
Returns USB core mode.
HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
Unregister an USB PCD Callback USB PCD callabck is redirected to the weak predefined callback...
void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
Send BatteryCharging message to user layer callback.
void(* pPCD_BcdCallbackTypeDef)(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
Handles PCD interrupt request.
void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
DeInitializes PCD MSP.
HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd, pPCD_IsoInIncpltCallbackTypeDef pCallback)
Register USB PCD Iso IN incomplete Callback To be used instead of the weak HAL_PCD_ISOINIncompleteCal...