STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_pcd.c
Go to the documentation of this file.
1 
75 /* Includes ------------------------------------------------------------------*/
76 #include "stm32f7xx_hal.h"
77 
87 #ifdef HAL_PCD_MODULE_ENABLED
88 
89 /* Private types -------------------------------------------------------------*/
90 /* Private variables ---------------------------------------------------------*/
91 /* Private constants ---------------------------------------------------------*/
92 /* Private macros ------------------------------------------------------------*/
96 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
97 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
98 
102 /* Private functions prototypes ----------------------------------------------*/
106 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
111 /* Exported functions --------------------------------------------------------*/
136 {
137  uint32_t i = 0;
138 
139  /* Check the PCD handle allocation */
140  if(hpcd == NULL)
141  {
142  return HAL_ERROR;
143  }
144 
145  /* Check the parameters */
147 
148  hpcd->State = HAL_PCD_STATE_BUSY;
149 
150  /* Init the low level hardware : GPIO, CLOCK, NVIC... */
151  HAL_PCD_MspInit(hpcd);
152 
153  /* Disable the Interrupts */
154  __HAL_PCD_DISABLE(hpcd);
155 
156  /*Init the Core (common init.) */
157  USB_CoreInit(hpcd->Instance, hpcd->Init);
158 
159  /* Force Device Mode*/
161 
162  /* Init endpoints structures */
163  for (i = 0; i < 15 ; i++)
164  {
165  /* Init ep structure */
166  hpcd->IN_ep[i].is_in = 1;
167  hpcd->IN_ep[i].num = i;
168  hpcd->IN_ep[i].tx_fifo_num = i;
169  /* Control until ep is activated */
170  hpcd->IN_ep[i].type = EP_TYPE_CTRL;
171  hpcd->IN_ep[i].maxpacket = 0;
172  hpcd->IN_ep[i].xfer_buff = 0;
173  hpcd->IN_ep[i].xfer_len = 0;
174  }
175 
176  for (i = 0; i < 15 ; i++)
177  {
178  hpcd->OUT_ep[i].is_in = 0;
179  hpcd->OUT_ep[i].num = i;
180  hpcd->IN_ep[i].tx_fifo_num = i;
181  /* Control until ep is activated */
182  hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
183  hpcd->OUT_ep[i].maxpacket = 0;
184  hpcd->OUT_ep[i].xfer_buff = 0;
185  hpcd->OUT_ep[i].xfer_len = 0;
186 
187  hpcd->Instance->DIEPTXF[i] = 0;
188  }
189 
190  /* Init Device */
191  USB_DevInit(hpcd->Instance, hpcd->Init);
192 
193  hpcd->State= HAL_PCD_STATE_READY;
194 
195  /* Activate LPM */
196  if (hpcd->Init.lpm_enable == 1)
197  {
198  HAL_PCDEx_ActivateLPM(hpcd);
199  }
200 
201  USB_DevDisconnect (hpcd->Instance);
202  return HAL_OK;
203 }
204 
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  HAL_PCD_Stop(hpcd);
222 
223  /* DeInit the low level hardware */
224  HAL_PCD_MspDeInit(hpcd);
225 
226  hpcd->State = HAL_PCD_STATE_RESET;
227 
228  return HAL_OK;
229 }
230 
236 __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
237 {
238  /* Prevent unused argument(s) compilation warning */
239  UNUSED(hpcd);
240 
241  /* NOTE : This function Should not be modified, when the callback is needed,
242  the HAL_PCD_MspInit could be implemented in the user file
243  */
244 }
245 
251 __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
252 {
253  /* Prevent unused argument(s) compilation warning */
254  UNUSED(hpcd);
255 
256  /* NOTE : This function Should not be modified, when the callback is needed,
257  the HAL_PCD_MspDeInit could be implemented in the user file
258  */
259 }
260 
286 {
287  __HAL_LOCK(hpcd);
288  USB_DevConnect (hpcd->Instance);
289  __HAL_PCD_ENABLE(hpcd);
290  __HAL_UNLOCK(hpcd);
291  return HAL_OK;
292 }
293 
300 {
301  __HAL_LOCK(hpcd);
302  __HAL_PCD_DISABLE(hpcd);
303  USB_StopDevice(hpcd->Instance);
304  USB_DevDisconnect (hpcd->Instance);
305  __HAL_UNLOCK(hpcd);
306  return HAL_OK;
307 }
308 
309 uint32_t EP1PktLen;
310 
317 {
318  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
319  uint32_t i = 0, ep_intr = 0, epint = 0, epnum = 0;
320  uint32_t fifoemptymsk = 0, temp = 0;
321  USB_OTG_EPTypeDef *ep = NULL;
322  uint32_t hclk = 200000000;
323 
324  /* ensure that we are in device mode */
326  {
327  /* avoid spurious interrupt */
329  {
330  return;
331  }
332 
334  {
335  /* incorrect mode, acknowledge the interrupt */
337  }
338 
340  {
341  epnum = 0;
342 
343  /* Read in the device interrupt bits */
344  ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
345 
346  while ( ep_intr )
347  {
348  if (ep_intr & 0x1)
349  {
350  epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, epnum);
351 
352  if(( epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
353  {
354  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
355 
356  if(hpcd->Init.dma_enable == 1)
357  {
358  hpcd->OUT_ep[epnum].xfer_count = hpcd->OUT_ep[epnum].maxpacket- (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
359  hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
360  }
361 
362  //YYYY
363  if (epnum == 1)
364  {
365  EP1PktLen = hpcd->OUT_ep[epnum].xfer_count;
366  }
367 
368  HAL_PCD_DataOutStageCallback(hpcd, epnum);
369  if(hpcd->Init.dma_enable == 1)
370  {
371  if((epnum == 0) && (hpcd->OUT_ep[epnum].xfer_len == 0))
372  {
373  /* this is ZLP, so prepare EP0 for next setup */
374  USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
375  }
376  }
377  }
378 
379  if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
380  {
381  /* Inform the upper layer that a setup packet is available */
383  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
384  }
385 
386  if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
387  {
388  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
389  }
390  /* Clear Status Phase Received interrupt */
391  if(( epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
392  {
393  CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
394  }
395  }
396  epnum++;
397  ep_intr >>= 1;
398  }
399  }
400 
402  {
403  /* Read in the device interrupt bits */
404  ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
405 
406  epnum = 0;
407 
408  while ( ep_intr )
409  {
410  if (ep_intr & 0x1) /* In ITR */
411  {
412  epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
413 
414  if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
415  {
416  fifoemptymsk = 0x1 << epnum;
417  USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
418 
419  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
420 
421  if (hpcd->Init.dma_enable == 1)
422  {
423  hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
424  }
425 
426  HAL_PCD_DataInStageCallback(hpcd, epnum);
427 
428  if (hpcd->Init.dma_enable == 1)
429  {
430  /* this is ZLP, so prepare EP0 for next setup */
431  if((epnum == 0) && (hpcd->IN_ep[epnum].xfer_len == 0))
432  {
433  /* prepare to rx more setup packets */
434  USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
435  }
436  }
437  }
438  if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
439  {
440  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
441  }
442  if(( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
443  {
444  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
445  }
446  if(( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
447  {
448  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
449  }
450  if(( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
451  {
452  CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
453  }
454  if(( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
455  {
456  PCD_WriteEmptyTxFifo(hpcd , epnum);
457  }
458  }
459  epnum++;
460  ep_intr >>= 1;
461  }
462  }
463 
464  /* Handle Resume Interrupt */
466  {
467  /* Clear the Remote Wake-up Signaling */
469 
470  if(hpcd->LPM_State == LPM_L1)
471  {
472  hpcd->LPM_State = LPM_L0;
474  }
475  else
476  {
478  }
480  }
481 
482  /* Handle Suspend Interrupt */
484  {
486  {
487 
489  }
491  }
492 
493  /* Handle LPM Interrupt */
495  {
497  if( hpcd->LPM_State == LPM_L0)
498  {
499  hpcd->LPM_State = LPM_L1;
500  hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >>2 ;
502  }
503  else
504  {
506  }
507  }
508 
509  /* Handle Reset Interrupt */
511  {
512  USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
513  USB_FlushTxFifo(hpcd->Instance , 0 );
514 
515  for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
516  {
517  USBx_INEP(i)->DIEPINT = 0xFF;
518  USBx_OUTEP(i)->DOEPINT = 0xFF;
519  }
520  USBx_DEVICE->DAINT = 0xFFFFFFFF;
521  USBx_DEVICE->DAINTMSK |= 0x10001;
522 
523  if(hpcd->Init.use_dedicated_ep1)
524  {
527  }
528  else
529  {
532  }
533 
534  /* Set Default Address to 0 */
535  USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
536 
537  /* setup EP0 to receive SETUP packets */
538  USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
539 
541  }
542 
543  /* Handle Enumeration done Interrupt */
545  {
548 
550  {
551  hpcd->Init.speed = USB_OTG_SPEED_HIGH;
553  hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_HS_TRDT_VALUE << 10) & USB_OTG_GUSBCFG_TRDT);
554  }
555  else
556  {
557  hpcd->Init.speed = USB_OTG_SPEED_FULL;
559 
560  /* The USBTRD is configured according to the tables below, depending on AHB frequency
561  used by application. In the low AHB frequency range it is used to stretch enough the USB response
562  time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
563  latency to the Data FIFO */
564 
565  /* Get hclk frequency value */
566  hclk = HAL_RCC_GetHCLKFreq();
567 
568  if((hclk >= 14200000)&&(hclk < 15000000))
569  {
570  /* hclk Clock Range between 14.2-15 MHz */
571  hpcd->Instance->GUSBCFG |= (uint32_t)((0xF << 10) & USB_OTG_GUSBCFG_TRDT);
572  }
573 
574  else if((hclk >= 15000000)&&(hclk < 16000000))
575  {
576  /* hclk Clock Range between 15-16 MHz */
577  hpcd->Instance->GUSBCFG |= (uint32_t)((0xE << 10) & USB_OTG_GUSBCFG_TRDT);
578  }
579 
580  else if((hclk >= 16000000)&&(hclk < 17200000))
581  {
582  /* hclk Clock Range between 16-17.2 MHz */
583  hpcd->Instance->GUSBCFG |= (uint32_t)((0xD << 10) & USB_OTG_GUSBCFG_TRDT);
584  }
585 
586  else if((hclk >= 17200000)&&(hclk < 18500000))
587  {
588  /* hclk Clock Range between 17.2-18.5 MHz */
589  hpcd->Instance->GUSBCFG |= (uint32_t)((0xC << 10) & USB_OTG_GUSBCFG_TRDT);
590  }
591 
592  else if((hclk >= 18500000)&&(hclk < 20000000))
593  {
594  /* hclk Clock Range between 18.5-20 MHz */
595  hpcd->Instance->GUSBCFG |= (uint32_t)((0xB << 10) & USB_OTG_GUSBCFG_TRDT);
596  }
597 
598  else if((hclk >= 20000000)&&(hclk < 21800000))
599  {
600  /* hclk Clock Range between 20-21.8 MHz */
601  hpcd->Instance->GUSBCFG |= (uint32_t)((0xA << 10) & USB_OTG_GUSBCFG_TRDT);
602  }
603 
604  else if((hclk >= 21800000)&&(hclk < 24000000))
605  {
606  /* hclk Clock Range between 21.8-24 MHz */
607  hpcd->Instance->GUSBCFG |= (uint32_t)((0x9 << 10) & USB_OTG_GUSBCFG_TRDT);
608  }
609 
610  else if((hclk >= 24000000)&&(hclk < 27700000))
611  {
612  /* hclk Clock Range between 24-27.7 MHz */
613  hpcd->Instance->GUSBCFG |= (uint32_t)((0x8 << 10) & USB_OTG_GUSBCFG_TRDT);
614  }
615 
616  else if((hclk >= 27700000)&&(hclk < 32000000))
617  {
618  /* hclk Clock Range between 27.7-32 MHz */
619  hpcd->Instance->GUSBCFG |= (uint32_t)((0x7 << 10) & USB_OTG_GUSBCFG_TRDT);
620  }
621 
622  else /* if(hclk >= 32000000) */
623  {
624  /* hclk Clock Range between 32-200 MHz */
625  hpcd->Instance->GUSBCFG |= (uint32_t)((0x6 << 10) & USB_OTG_GUSBCFG_TRDT);
626  }
627  }
628 
629  HAL_PCD_ResetCallback(hpcd);
630 
632  }
633 
634  /* Handle RxQLevel Interrupt */
636  {
638  temp = USBx->GRXSTSP;
639  ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
640 
641  if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
642  {
643  if((temp & USB_OTG_GRXSTSP_BCNT) != 0)
644  {
645  USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4);
646  ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
647  ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
648  }
649  }
650  else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
651  {
652  USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8);
653  ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
654  }
656  }
657 
658  /* Handle SOF Interrupt */
660  {
661  HAL_PCD_SOFCallback(hpcd);
663  }
664 
665  /* Handle Incomplete ISO IN Interrupt */
667  {
668  HAL_PCD_ISOINIncompleteCallback(hpcd, epnum);
670  }
671 
672  /* Handle Incomplete ISO OUT Interrupt */
674  {
677  }
678 
679  /* Handle Connection event Interrupt */
681  {
684  }
685 
686  /* Handle Disconnection event Interrupt */
688  {
689  temp = hpcd->Instance->GOTGINT;
690 
691  if((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
692  {
694  }
695  hpcd->Instance->GOTGINT |= temp;
696  }
697  }
698 }
699 
706  __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
707 {
708  /* Prevent unused argument(s) compilation warning */
709  UNUSED(hpcd);
710  UNUSED(epnum);
711  /* NOTE : This function Should not be modified, when the callback is needed,
712  the HAL_PCD_DataOutStageCallback could be implemented in the user file
713  */
714 }
715 
722  __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
723 {
724  /* Prevent unused argument(s) compilation warning */
725  UNUSED(hpcd);
726  UNUSED(epnum);
727  /* NOTE : This function Should not be modified, when the callback is needed,
728  the HAL_PCD_DataInStageCallback could be implemented in the user file
729  */
730 }
737 {
738  /* Prevent unused argument(s) compilation warning */
739  UNUSED(hpcd);
740 
741  /* NOTE : This function Should not be modified, when the callback is needed,
742  the HAL_PCD_SetupStageCallback could be implemented in the user file
743  */
744 }
745 
751  __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
752 {
753  /* Prevent unused argument(s) compilation warning */
754  UNUSED(hpcd);
755 
756  /* NOTE : This function Should not be modified, when the callback is needed,
757  the HAL_PCD_SOFCallback could be implemented in the user file
758  */
759 }
760 
766  __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
767 {
768  /* Prevent unused argument(s) compilation warning */
769  UNUSED(hpcd);
770 
771  /* NOTE : This function Should not be modified, when the callback is needed,
772  the HAL_PCD_ResetCallback could be implemented in the user file
773  */
774 }
775 
781  __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
782 {
783  /* Prevent unused argument(s) compilation warning */
784  UNUSED(hpcd);
785 
786  /* NOTE : This function Should not be modified, when the callback is needed,
787  the HAL_PCD_SuspendCallback could be implemented in the user file
788  */
789 }
790 
796  __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
797 {
798  /* Prevent unused argument(s) compilation warning */
799  UNUSED(hpcd);
800 
801  /* NOTE : This function Should not be modified, when the callback is needed,
802  the HAL_PCD_ResumeCallback could be implemented in the user file
803  */
804 }
805 
812  __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
813 {
814  /* Prevent unused argument(s) compilation warning */
815  UNUSED(hpcd);
816  UNUSED(epnum);
817  /* NOTE : This function Should not be modified, when the callback is needed,
818  the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
819  */
820 }
821 
828  __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
829 {
830  /* Prevent unused argument(s) compilation warning */
831  UNUSED(hpcd);
832  UNUSED(epnum);
833  /* NOTE : This function Should not be modified, when the callback is needed,
834  the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
835  */
836 }
837 
843  __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
844 {
845  /* Prevent unused argument(s) compilation warning */
846  UNUSED(hpcd);
847 
848  /* NOTE : This function Should not be modified, when the callback is needed,
849  the HAL_PCD_ConnectCallback could be implemented in the user file
850  */
851 }
852 
859 {
860  /* Prevent unused argument(s) compilation warning */
861  UNUSED(hpcd);
862 
863  /* NOTE : This function Should not be modified, when the callback is needed,
864  the HAL_PCD_DisconnectCallback could be implemented in the user file
865  */
866 }
867 
893 {
894  __HAL_LOCK(hpcd);
895  USB_DevConnect(hpcd->Instance);
896  __HAL_UNLOCK(hpcd);
897  return HAL_OK;
898 }
899 
906 {
907  __HAL_LOCK(hpcd);
909  __HAL_UNLOCK(hpcd);
910  return HAL_OK;
911 }
912 
920 {
921  __HAL_LOCK(hpcd);
922  USB_SetDevAddress(hpcd->Instance, address);
923  __HAL_UNLOCK(hpcd);
924  return HAL_OK;
925 }
934 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
935 {
937  USB_OTG_EPTypeDef *ep;
938 
939  if ((ep_addr & 0x80) == 0x80)
940  {
941  ep = &hpcd->IN_ep[ep_addr & 0x7F];
942  }
943  else
944  {
945  ep = &hpcd->OUT_ep[ep_addr & 0x7F];
946  }
947  ep->num = ep_addr & 0x7F;
948 
949  ep->is_in = (0x80 & ep_addr) != 0;
950  ep->maxpacket = ep_mps;
951  ep->type = ep_type;
952  if (ep->is_in)
953  {
954  /* Assign a Tx FIFO */
955  ep->tx_fifo_num = ep->num;
956  }
957  /* Set initial data PID. */
958  if (ep_type == EP_TYPE_BULK )
959  {
960  ep->data_pid_start = 0;
961  }
962 
963  __HAL_LOCK(hpcd);
964  USB_ActivateEndpoint(hpcd->Instance , ep);
965  __HAL_UNLOCK(hpcd);
966  return ret;
967 }
968 
969 
977 {
978  USB_OTG_EPTypeDef *ep;
979 
980  if ((ep_addr & 0x80) == 0x80)
981  {
982  ep = &hpcd->IN_ep[ep_addr & 0x7F];
983  }
984  else
985  {
986  ep = &hpcd->OUT_ep[ep_addr & 0x7F];
987  }
988  ep->num = ep_addr & 0x7F;
989 
990  ep->is_in = (0x80 & ep_addr) != 0;
991 
992  __HAL_LOCK(hpcd);
993  USB_DeactivateEndpoint(hpcd->Instance , ep);
994  __HAL_UNLOCK(hpcd);
995  return HAL_OK;
996 }
997 
998 
1007 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1008 {
1009  USB_OTG_EPTypeDef *ep;
1010 
1011  ep = &hpcd->OUT_ep[ep_addr & 0x7F];
1012 
1013  /*setup and start the Xfer */
1014  ep->xfer_buff = pBuf;
1015  ep->xfer_len = len;
1016  ep->xfer_count = 0;
1017  ep->is_in = 0;
1018  ep->num = ep_addr & 0x7F;
1019 
1020  if (hpcd->Init.dma_enable == 1)
1021  {
1022  ep->dma_addr = (uint32_t)pBuf;
1023  }
1024 
1025  __HAL_LOCK(hpcd);
1026 
1027  if ((ep_addr & 0x7F) == 0 )
1028  {
1029  USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
1030  }
1031  else
1032  {
1033  USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
1034  }
1035  __HAL_UNLOCK(hpcd);
1036 
1037  return HAL_OK;
1038 }
1039 
1046 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1047 {
1048  return hpcd->OUT_ep[ep_addr & 0x7F].xfer_count;
1049 }
1058 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1059 {
1060  USB_OTG_EPTypeDef *ep;
1061 
1062  ep = &hpcd->IN_ep[ep_addr & 0x7F];
1063 
1064  /*setup and start the Xfer */
1065  ep->xfer_buff = pBuf;
1066  ep->xfer_len = len;
1067  ep->xfer_count = 0;
1068  ep->is_in = 1;
1069  ep->num = ep_addr & 0x7F;
1070 
1071  if (hpcd->Init.dma_enable == 1)
1072  {
1073  ep->dma_addr = (uint32_t)pBuf;
1074  }
1075 
1076  __HAL_LOCK(hpcd);
1077 
1078  if ((ep_addr & 0x7F) == 0 )
1079  {
1080  USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
1081  }
1082  else
1083  {
1084  USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
1085  }
1086 
1087  __HAL_UNLOCK(hpcd);
1088 
1089  return HAL_OK;
1090 }
1091 
1099 {
1100  USB_OTG_EPTypeDef *ep;
1101 
1102  if ((0x80 & ep_addr) == 0x80)
1103  {
1104  ep = &hpcd->IN_ep[ep_addr & 0x7F];
1105  }
1106  else
1107  {
1108  ep = &hpcd->OUT_ep[ep_addr];
1109  }
1110 
1111  ep->is_stall = 1;
1112  ep->num = ep_addr & 0x7F;
1113  ep->is_in = ((ep_addr & 0x80) == 0x80);
1114 
1115 
1116  __HAL_LOCK(hpcd);
1117  USB_EPSetStall(hpcd->Instance , ep);
1118  if((ep_addr & 0x7F) == 0)
1119  {
1120  USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
1121  }
1122  __HAL_UNLOCK(hpcd);
1123 
1124  return HAL_OK;
1125 }
1126 
1134 {
1135  USB_OTG_EPTypeDef *ep;
1136 
1137  if ((0x80 & ep_addr) == 0x80)
1138  {
1139  ep = &hpcd->IN_ep[ep_addr & 0x7F];
1140  }
1141  else
1142  {
1143  ep = &hpcd->OUT_ep[ep_addr];
1144  }
1145 
1146  ep->is_stall = 0;
1147  ep->num = ep_addr & 0x7F;
1148  ep->is_in = ((ep_addr & 0x80) == 0x80);
1149 
1150  __HAL_LOCK(hpcd);
1151  USB_EPClearStall(hpcd->Instance , ep);
1152  __HAL_UNLOCK(hpcd);
1153 
1154  return HAL_OK;
1155 }
1156 
1164 {
1165  __HAL_LOCK(hpcd);
1166 
1167  if ((ep_addr & 0x80) == 0x80)
1168  {
1169  USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
1170  }
1171  else
1172  {
1173  USB_FlushRxFifo(hpcd->Instance);
1174  }
1175 
1176  __HAL_UNLOCK(hpcd);
1177 
1178  return HAL_OK;
1179 }
1180 
1187 {
1188  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1189 
1191  {
1192  /* Activate Remote wakeup signaling */
1194  }
1195  return HAL_OK;
1196 }
1197 
1204 {
1205  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1206 
1207  /* De-activate Remote wakeup signaling */
1208  USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
1209  return HAL_OK;
1210 }
1236 {
1237  return hpcd->State;
1238 }
1247 /* Private functions ---------------------------------------------------------*/
1258 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
1259 {
1260  USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1261  USB_OTG_EPTypeDef *ep;
1262  int32_t len = 0;
1263  uint32_t len32b;
1264  uint32_t fifoemptymsk = 0;
1265 
1266  ep = &hpcd->IN_ep[epnum];
1267  len = ep->xfer_len - ep->xfer_count;
1268 
1269  if (len > ep->maxpacket)
1270  {
1271  len = ep->maxpacket;
1272  }
1273 
1274 
1275  len32b = (len + 3) / 4;
1276 
1277  while ( (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b &&
1278  ep->xfer_count < ep->xfer_len &&
1279  ep->xfer_len != 0)
1280  {
1281  /* Write the FIFO */
1282  len = ep->xfer_len - ep->xfer_count;
1283 
1284  if (len > ep->maxpacket)
1285  {
1286  len = ep->maxpacket;
1287  }
1288  len32b = (len + 3) / 4;
1289 
1290  USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
1291 
1292  ep->xfer_buff += len;
1293  ep->xfer_count += len;
1294  }
1295 
1296  if(len <= 0)
1297  {
1298  fifoemptymsk = 0x1 << epnum;
1299  USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
1300 
1301  }
1302 
1303  return HAL_OK;
1304 }
1305 
1310 #endif /* HAL_PCD_MODULE_ENABLED */
1311 
1319 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef USB_FlushTxFifo(USB_OTG_GlobalTypeDef *USBx, uint32_t num)
HAL_StatusTypeDef USB_DevInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef Init)
#define USB_OTG_GINTSTS_OTGINT
Definition: stm32f745xx.h:8290
__IO uint32_t DIEPTXF[0x0F]
Definition: stm32f745xx.h:1000
#define USB_OTG_DCTL_RWUSIG
Definition: stm32f745xx.h:8158
#define USB_OTG_DOEPINT_XFRC
Definition: stm32f745xx.h:8746
#define USB_OTG_DIEPMSK_TOM
Definition: stm32f745xx.h:8245
#define USB_OTG_FS_MAX_PACKET_SIZE
#define USB_OTG_GINTSTS_RXFLVL
Definition: stm32f745xx.h:8292
#define USB_OTG_GINTSTS_IISOIXFR
Definition: stm32f745xx.h:8304
PCD_StateTypeDef
PCD State structure definition.
uint32_t USB_ReadDevInEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
#define USB_OTG_GINTSTS_USBSUSP
Definition: stm32f745xx.h:8297
HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
ISOINIncomplete callback.
Definition: usbd_conf.c:328
#define assert_param(expr)
Include module&#39;s header file.
HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
#define USB_OTG_SPEED_HIGH
HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
#define USB_OTG_GINTSTS_LPMINT
Definition: stm32f745xx.h:8311
HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
#define USB_OTG_GINTSTS_MMIS
Definition: stm32f745xx.h:8289
HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
Resume callback.
Definition: usbd_conf.c:306
#define __HAL_PCD_DISABLE(__HANDLE__)
HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
#define __HAL_UNLOCK(__HANDLE__)
#define USB_OTG_GLPMCFG_BESL
Definition: stm32f745xx.h:8516
HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
Suspend callback.
Definition: usbd_conf.c:296
uint32_t EP1PktLen
HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
#define CLEAR_IN_EP_INTR(__EPNUM__, __INTERRUPT__)
#define USBx_INEP(i)
HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx, USB_OTG_ModeTypeDef mode)
#define USBD_HS_TRDT_VALUE
#define __HAL_PCD_ENABLE(__HANDLE__)
HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t dma, uint8_t *psetup)
#define USB_OTG_DIEPMSK_EPDM
Definition: stm32f745xx.h:8244
#define USB_OTG_DIEPINT_EPDISD
Definition: stm32f745xx.h:8679
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
Disconnect callback.
Definition: usbd_conf.c:348
#define USB_OTG_DOEPTSIZ_XFRSIZ
Definition: stm32f745xx.h:8755
void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
__IO uint32_t GRXSTSP
Definition: stm32f745xx.h:984
HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
#define USB_OTG_DIEPINT_TXFE
Definition: stm32f745xx.h:8683
PCD_TypeDef * Instance
#define USB_OTG_GRXSTSP_PKTSTS
Definition: stm32f745xx.h:8358
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
DataOut Stage callback.
Definition: usbd_conf.c:234
HAL_StatusTypeDef USB_DevDisconnect(USB_OTG_GlobalTypeDef *USBx)
HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
#define USB_OTG_GUSBCFG_TRDT
Definition: stm32f745xx.h:8208
#define USB_OTG_GINTSTS_SOF
Definition: stm32f745xx.h:8291
#define USB_OTG_DIEPINT_XFRC
Definition: stm32f745xx.h:8678
__IO uint32_t GLPMCFG
Definition: stm32f745xx.h:994
#define USB_OTG_DOEPMSK_OTEPSPRM
Definition: stm32f745xx.h:8282
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
SOF callback.
Definition: usbd_conf.c:255
#define __HAL_PCD_IS_INVALID_INTERRUPT(__HANDLE__)
USB_OTG_Core_Registers.
Definition: stm32f745xx.h:974
#define EP_TYPE_CTRL
#define STS_SETUP_UPDT
HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep, uint8_t dma)
#define IS_PCD_ALL_INSTANCE(INSTANCE)
uint32_t USB_ReadDevAllInEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
#define EP_TYPE_BULK
#define USB_OTG_DOEPINT_OTEPDIS
Definition: stm32f745xx.h:8749
#define __HAL_LOCK(__HANDLE__)
#define NULL
Definition: usbd_def.h:53
#define USB_OTG_GINTSTS_USBRST
Definition: stm32f745xx.h:8298
HAL_StatusTypeDef USB_ActivateSetup(USB_OTG_GlobalTypeDef *USBx)
HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep, uint8_t dma)
#define USB_OTG_GRXSTSP_BCNT
Definition: stm32f745xx.h:8356
HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
PCD_EPTypeDef OUT_ep[15]
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
DataIn Stage callback.
Definition: usbd_conf.c:245
HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx)
uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
This file contains all the functions prototypes for the HAL module driver.
#define __HAL_PCD_GET_FLAG(__HANDLE__, __INTERRUPT__)
HAL_StatusTypeDef USB_DevConnect(USB_OTG_GlobalTypeDef *USBx)
HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
#define USB_OTG_SPEED_FULL
#define USB_OTG_GINTSTS_SRQINT
Definition: stm32f745xx.h:8314
#define USB_OTG_GINTSTS_PXFR_INCOMPISOOUT
Definition: stm32f745xx.h:8305
PCD_LPM_StateTypeDef LPM_State
#define UNUSED(x)
#define USB_UNMASK_INTERRUPT(__INSTANCE__, __INTERRUPT__)
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
SetupStage callback.
Definition: usbd_conf.c:223
PCD Handle Structure definition.
HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len, uint8_t dma)
uint32_t USB_ReadDevAllOutEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
#define USB_OTG_GINTSTS_WKUINT
Definition: stm32f745xx.h:8315
PCD_EPTypeDef IN_ep[15]
uint32_t HAL_RCC_GetHCLKFreq(void)
#define USB_OTG_GINTSTS_IEPINT
Definition: stm32f745xx.h:8302
#define CLEAR_OUT_EP_INTR(__EPNUM__, __INTERRUPT__)
void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
Reset callback.
Definition: usbd_conf.c:265
#define USB_OTG_MODE_DEVICE
#define USB_OTG_DOEPINT_STUP
Definition: stm32f745xx.h:8748
#define USB_OTG_DIEPINT_INEPNE
Definition: stm32f745xx.h:8682
#define USB_OTG_DIEPINT_TOC
Definition: stm32f745xx.h:8680
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
ConnectCallback callback.
Definition: usbd_conf.c:338
HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
#define __HAL_PCD_CLEAR_FLAG(__HANDLE__, __INTERRUPT__)
#define USB_OTG_DSTS_SUSPSTS
Definition: stm32f745xx.h:8181
#define USB_OTG_GOTGINT_SEDET
Definition: stm32f745xx.h:8149
void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
Initializes the PCD MSP.
Definition: usbd_conf.c:69
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
ISOOUTIncomplete callback.
Definition: usbd_conf.c:317
#define USB_OTG_GRXSTSP_EPNUM
Definition: stm32f745xx.h:8355
PCD_HandleTypeDef hpcd
Definition: usbd_conf.c:55
__IO PCD_StateTypeDef State
void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
#define STS_DATA_UPDT
#define USBx_DEVICE
#define USB_OTG_DOEPMSK_XFRCM
Definition: stm32f745xx.h:8278
HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
De-Initializes the PCD MSP.
Definition: usbd_conf.c:198
PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
#define USB_OTG_DOEPMSK_EPDM
Definition: stm32f745xx.h:8279
PCD_InitTypeDef Init
__IO uint32_t GUSBCFG
Definition: stm32f745xx.h:979
uint32_t USB_ReadDevOutEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
#define USB_OTG_DCFG_DAD
Definition: stm32f745xx.h:8126
HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef Init)
HAL_StatusTypeDef
HAL Status structures definition.
#define USB_OTG_DOEPMSK_STUPM
Definition: stm32f745xx.h:8280
#define USB_OTG_DIEPINT_ITTXFE
Definition: stm32f745xx.h:8681
#define USB_OTG_DOEPINT_OTEPSPR
Definition: stm32f745xx.h:8750
void * USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
#define USB_OTG_GINTSTS_OEPINT
Definition: stm32f745xx.h:8303
#define USB_OTG_DIEPMSK_XFRCM
Definition: stm32f745xx.h:8243
#define USBx_OUTEP(i)
HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
#define USB_MASK_INTERRUPT(__INSTANCE__, __INTERRUPT__)
__IO uint32_t GOTGINT
Definition: stm32f745xx.h:977
#define USB_OTG_GINTSTS_ENUMDNE
Definition: stm32f745xx.h:8299
#define USB_OTG_DTXFSTS_INEPTFSAV
Definition: stm32f745xx.h:8723
HAL_StatusTypeDef USB_SetDevAddress(USB_OTG_GlobalTypeDef *USBx, uint8_t address)
uint32_t USB_GetMode(USB_OTG_GlobalTypeDef *USBx)
#define USB_OTG_HS_MAX_PACKET_SIZE