2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_pcd.c
\r
4 * @author MCD Application Team
\r
6 * @date 06-March-2015
\r
7 * @brief PCD HAL module driver.
\r
8 * This file provides firmware functions to manage the following
\r
9 * functionalities of the USB Peripheral Controller:
\r
10 * + Initialization and de-initialization functions
\r
11 * + IO operation functions
\r
12 * + Peripheral Control functions
\r
13 * + Peripheral State functions
\r
16 ==============================================================================
\r
17 ##### How to use this driver #####
\r
18 ==============================================================================
\r
20 The PCD HAL driver can be used as follows:
\r
22 (#) Declare a PCD_HandleTypeDef handle structure, for example:
\r
23 PCD_HandleTypeDef hpcd;
\r
25 (#) Fill parameters of Init structure in HCD handle
\r
27 (#) Call HAL_PCD_Init() API to initialize the HCD peripheral (Core, Device core, ...)
\r
29 (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
\r
30 (##) Enable the PCD/USB Low Level interface clock using
\r
31 (+++) __OTGFS-OTG_CLK_ENABLE()/__OTGHS-OTG_CLK_ENABLE();
\r
32 (+++) __OTGHSULPI_CLK_ENABLE(); (For High Speed Mode)
\r
34 (##) Initialize the related GPIO clocks
\r
35 (##) Configure PCD pin-out
\r
36 (##) Configure PCD NVIC interrupt
\r
38 (#)Associate the Upper USB device stack to the HAL PCD Driver:
\r
39 (##) hpcd.pData = pdev;
\r
41 (#)Enable HCD transmission and reception:
\r
42 (##) HAL_PCD_Start();
\r
45 ******************************************************************************
\r
48 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
\r
50 * Redistribution and use in source and binary forms, with or without modification,
\r
51 * are permitted provided that the following conditions are met:
\r
52 * 1. Redistributions of source code must retain the above copyright notice,
\r
53 * this list of conditions and the following disclaimer.
\r
54 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
55 * this list of conditions and the following disclaimer in the documentation
\r
56 * and/or other materials provided with the distribution.
\r
57 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
58 * may be used to endorse or promote products derived from this software
\r
59 * without specific prior written permission.
\r
61 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
62 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
64 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
67 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
68 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
69 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
70 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
72 ******************************************************************************
\r
75 /* Includes ------------------------------------------------------------------*/
\r
76 #include "stm32f7xx_hal.h"
\r
78 /** @addtogroup STM32F7xx_HAL_Driver
\r
82 /** @defgroup PCD PCD
\r
83 * @brief PCD HAL module driver
\r
87 #ifdef HAL_PCD_MODULE_ENABLED
\r
89 /* Private types -------------------------------------------------------------*/
\r
90 /* Private variables ---------------------------------------------------------*/
\r
91 /* Private constants ---------------------------------------------------------*/
\r
92 /* Private macros ------------------------------------------------------------*/
\r
93 /** @defgroup PCD_Private_Macros PCD Private Macros
\r
96 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
\r
97 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
\r
102 /* Private functions prototypes ----------------------------------------------*/
\r
103 /** @defgroup PCD_Private_Functions PCD Private Functions
\r
106 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
\r
111 /* Exported functions --------------------------------------------------------*/
\r
112 /** @defgroup PCD_Exported_Functions PCD Exported Functions
\r
116 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
\r
117 * @brief Initialization and Configuration functions
\r
120 ===============================================================================
\r
121 ##### Initialization and de-initialization functions #####
\r
122 ===============================================================================
\r
123 [..] This section provides functions allowing to:
\r
130 * @brief Initializes the PCD according to the specified
\r
131 * parameters in the PCD_InitTypeDef and create the associated handle.
\r
132 * @param hpcd: PCD handle
\r
133 * @retval HAL status
\r
135 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
\r
139 /* Check the PCD handle allocation */
\r
145 /* Check the parameters */
\r
146 assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
\r
148 hpcd->State = HAL_PCD_STATE_BUSY;
\r
150 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
\r
151 HAL_PCD_MspInit(hpcd);
\r
153 /* Disable the Interrupts */
\r
154 __HAL_PCD_DISABLE(hpcd);
\r
156 /*Init the Core (common init.) */
\r
157 USB_CoreInit(hpcd->Instance, hpcd->Init);
\r
159 /* Force Device Mode*/
\r
160 USB_SetCurrentMode(hpcd->Instance , USB_OTG_DEVICE_MODE);
\r
162 /* Init endpoints structures */
\r
163 for (i = 0; i < 15 ; i++)
\r
165 /* Init ep structure */
\r
166 hpcd->IN_ep[i].is_in = 1;
\r
167 hpcd->IN_ep[i].num = i;
\r
168 hpcd->IN_ep[i].tx_fifo_num = i;
\r
169 /* Control until ep is activated */
\r
170 hpcd->IN_ep[i].type = EP_TYPE_CTRL;
\r
171 hpcd->IN_ep[i].maxpacket = 0;
\r
172 hpcd->IN_ep[i].xfer_buff = 0;
\r
173 hpcd->IN_ep[i].xfer_len = 0;
\r
176 for (i = 0; i < 15 ; i++)
\r
178 hpcd->OUT_ep[i].is_in = 0;
\r
179 hpcd->OUT_ep[i].num = i;
\r
180 hpcd->IN_ep[i].tx_fifo_num = i;
\r
181 /* Control until ep is activated */
\r
182 hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
\r
183 hpcd->OUT_ep[i].maxpacket = 0;
\r
184 hpcd->OUT_ep[i].xfer_buff = 0;
\r
185 hpcd->OUT_ep[i].xfer_len = 0;
\r
187 hpcd->Instance->DIEPTXF[i] = 0;
\r
191 USB_DevInit(hpcd->Instance, hpcd->Init);
\r
193 hpcd->State= HAL_PCD_STATE_READY;
\r
196 if (hpcd->Init.lpm_enable == 1)
\r
198 HAL_PCDEx_ActivateLPM(hpcd);
\r
201 USB_DevDisconnect (hpcd->Instance);
\r
206 * @brief DeInitializes the PCD peripheral
\r
207 * @param hpcd: PCD handle
\r
208 * @retval HAL status
\r
210 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
\r
212 /* Check the PCD handle allocation */
\r
218 hpcd->State = HAL_PCD_STATE_BUSY;
\r
221 HAL_PCD_Stop(hpcd);
\r
223 /* DeInit the low level hardware */
\r
224 HAL_PCD_MspDeInit(hpcd);
\r
226 hpcd->State = HAL_PCD_STATE_RESET;
\r
232 * @brief Initializes the PCD MSP.
\r
233 * @param hpcd: PCD handle
\r
236 __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
\r
238 /* NOTE : This function Should not be modified, when the callback is needed,
\r
239 the HAL_PCD_MspInit could be implemented in the user file
\r
244 * @brief DeInitializes PCD MSP.
\r
245 * @param hpcd: PCD handle
\r
248 __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
\r
250 /* NOTE : This function Should not be modified, when the callback is needed,
\r
251 the HAL_PCD_MspDeInit could be implemented in the user file
\r
259 /** @defgroup PCD_Exported_Functions_Group2 IO operation functions
\r
260 * @brief Data transfers functions
\r
263 ===============================================================================
\r
264 ##### IO operation functions #####
\r
265 ===============================================================================
\r
267 This subsection provides a set of functions allowing to manage the PCD data
\r
275 * @brief Start The USB OTG Device.
\r
276 * @param hpcd: PCD handle
\r
277 * @retval HAL status
\r
279 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
\r
282 USB_DevConnect (hpcd->Instance);
\r
283 __HAL_PCD_ENABLE(hpcd);
\r
284 __HAL_UNLOCK(hpcd);
\r
289 * @brief Stop The USB OTG Device.
\r
290 * @param hpcd: PCD handle
\r
291 * @retval HAL status
\r
293 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
\r
296 __HAL_PCD_DISABLE(hpcd);
\r
297 USB_StopDevice(hpcd->Instance);
\r
298 USB_DevDisconnect (hpcd->Instance);
\r
299 __HAL_UNLOCK(hpcd);
\r
304 * @brief This function handles PCD interrupt request.
\r
305 * @param hpcd: PCD handle
\r
306 * @retval HAL status
\r
308 void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
\r
310 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
\r
311 uint32_t i = 0, ep_intr = 0, epint = 0, epnum = 0;
\r
312 uint32_t fifoemptymsk = 0, temp = 0;
\r
313 USB_OTG_EPTypeDef *ep;
\r
315 /* ensure that we are in device mode */
\r
316 if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
\r
318 /* avoid spurious interrupt */
\r
319 if(__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
\r
324 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
\r
326 /* incorrect mode, acknowledge the interrupt */
\r
327 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
\r
330 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
\r
334 /* Read in the device interrupt bits */
\r
335 ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
\r
341 epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, epnum);
\r
343 if(( epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
\r
345 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
\r
347 if(hpcd->Init.dma_enable == 1)
\r
349 hpcd->OUT_ep[epnum].xfer_count = hpcd->OUT_ep[epnum].maxpacket- (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
\r
350 hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
\r
353 HAL_PCD_DataOutStageCallback(hpcd, epnum);
\r
354 if(hpcd->Init.dma_enable == 1)
\r
356 if((epnum == 0) && (hpcd->OUT_ep[epnum].xfer_len == 0))
\r
358 /* this is ZLP, so prepare EP0 for next setup */
\r
359 USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
\r
364 if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
\r
366 /* Inform the upper layer that a setup packet is available */
\r
367 HAL_PCD_SetupStageCallback(hpcd);
\r
368 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
\r
371 if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
\r
373 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
\r
381 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
\r
383 /* Read in the device interrupt bits */
\r
384 ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
\r
390 if (ep_intr & 0x1) /* In ITR */
\r
392 epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
\r
394 if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
\r
396 fifoemptymsk = 0x1 << epnum;
\r
397 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
\r
399 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
\r
401 if (hpcd->Init.dma_enable == 1)
\r
403 hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
\r
406 HAL_PCD_DataInStageCallback(hpcd, epnum);
\r
408 if (hpcd->Init.dma_enable == 1)
\r
410 /* this is ZLP, so prepare EP0 for next setup */
\r
411 if((epnum == 0) && (hpcd->IN_ep[epnum].xfer_len == 0))
\r
413 /* prepare to rx more setup packets */
\r
414 USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
\r
418 if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
\r
420 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
\r
422 if(( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
\r
424 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
\r
426 if(( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
\r
428 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
\r
430 if(( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
\r
432 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
\r
434 if(( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
\r
436 PCD_WriteEmptyTxFifo(hpcd , epnum);
\r
444 /* Handle Resume Interrupt */
\r
445 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
\r
447 /* Clear the Remote Wake-up Signaling */
\r
448 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
\r
450 if(hpcd->LPM_State == LPM_L1)
\r
452 hpcd->LPM_State = LPM_L0;
\r
453 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
\r
457 HAL_PCD_ResumeCallback(hpcd);
\r
459 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
\r
462 /* Handle Suspend Interrupt */
\r
463 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
\r
466 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
\r
469 HAL_PCD_SuspendCallback(hpcd);
\r
471 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
\r
474 /* Handle LPM Interrupt */
\r
475 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
\r
477 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
\r
478 if( hpcd->LPM_State == LPM_L0)
\r
480 hpcd->LPM_State = LPM_L1;
\r
481 hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >>2 ;
\r
482 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
\r
486 HAL_PCD_SuspendCallback(hpcd);
\r
490 /* Handle Reset Interrupt */
\r
491 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
\r
493 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
\r
494 USB_FlushTxFifo(hpcd->Instance , 0 );
\r
496 for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
\r
498 USBx_INEP(i)->DIEPINT = 0xFF;
\r
499 USBx_OUTEP(i)->DOEPINT = 0xFF;
\r
501 USBx_DEVICE->DAINT = 0xFFFFFFFF;
\r
502 USBx_DEVICE->DAINTMSK |= 0x10001;
\r
504 if(hpcd->Init.use_dedicated_ep1)
\r
506 USBx_DEVICE->DOUTEP1MSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
\r
507 USBx_DEVICE->DINEP1MSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
\r
511 USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
\r
512 USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
\r
515 /* Set Default Address to 0 */
\r
516 USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
\r
518 /* setup EP0 to receive SETUP packets */
\r
519 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
\r
521 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
\r
524 /* Handle Enumeration done Interrupt */
\r
525 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
\r
527 USB_ActivateSetup(hpcd->Instance);
\r
528 hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
\r
530 if ( USB_GetDevSpeed(hpcd->Instance) == USB_OTG_SPEED_HIGH)
\r
532 hpcd->Init.speed = USB_OTG_SPEED_HIGH;
\r
533 hpcd->Init.ep0_mps = USB_OTG_HS_MAX_PACKET_SIZE ;
\r
534 hpcd->Instance->GUSBCFG |= (USB_OTG_GUSBCFG_TRDT_0 | USB_OTG_GUSBCFG_TRDT_3);
\r
538 hpcd->Init.speed = USB_OTG_SPEED_FULL;
\r
539 hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE ;
\r
540 hpcd->Instance->GUSBCFG |= (USB_OTG_GUSBCFG_TRDT_0 | USB_OTG_GUSBCFG_TRDT_2);
\r
543 HAL_PCD_ResetCallback(hpcd);
\r
545 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
\r
548 /* Handle RxQLevel Interrupt */
\r
549 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
\r
551 USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
\r
552 temp = USBx->GRXSTSP;
\r
553 ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
\r
555 if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
\r
557 if((temp & USB_OTG_GRXSTSP_BCNT) != 0)
\r
559 USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4);
\r
560 ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
\r
561 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
\r
564 else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
\r
566 USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8);
\r
567 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
\r
569 USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
\r
572 /* Handle SOF Interrupt */
\r
573 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
\r
575 HAL_PCD_SOFCallback(hpcd);
\r
576 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
\r
579 /* Handle Incomplete ISO IN Interrupt */
\r
580 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
\r
582 HAL_PCD_ISOINIncompleteCallback(hpcd, epnum);
\r
583 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
\r
586 /* Handle Incomplete ISO OUT Interrupt */
\r
587 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
\r
589 HAL_PCD_ISOOUTIncompleteCallback(hpcd, epnum);
\r
590 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
\r
593 /* Handle Connection event Interrupt */
\r
594 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
\r
596 HAL_PCD_ConnectCallback(hpcd);
\r
597 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
\r
600 /* Handle Disconnection event Interrupt */
\r
601 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
\r
603 temp = hpcd->Instance->GOTGINT;
\r
605 if((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
\r
607 HAL_PCD_DisconnectCallback(hpcd);
\r
609 hpcd->Instance->GOTGINT |= temp;
\r
615 * @brief Data out stage callbacks
\r
616 * @param hpcd: PCD handle
\r
617 * @param epnum: endpoint number
\r
620 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
\r
622 /* NOTE : This function Should not be modified, when the callback is needed,
\r
623 the HAL_PCD_DataOutStageCallback could be implemented in the user file
\r
628 * @brief Data IN stage callbacks
\r
629 * @param hpcd: PCD handle
\r
630 * @param epnum: endpoint number
\r
633 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
\r
635 /* NOTE : This function Should not be modified, when the callback is needed,
\r
636 the HAL_PCD_DataInStageCallback could be implemented in the user file
\r
640 * @brief Setup stage callback
\r
641 * @param hpcd: PCD handle
\r
644 __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
\r
646 /* NOTE : This function Should not be modified, when the callback is needed,
\r
647 the HAL_PCD_SetupStageCallback could be implemented in the user file
\r
652 * @brief USB Start Of Frame callbacks
\r
653 * @param hpcd: PCD handle
\r
656 __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
\r
658 /* NOTE : This function Should not be modified, when the callback is needed,
\r
659 the HAL_PCD_SOFCallback could be implemented in the user file
\r
664 * @brief USB Reset callbacks
\r
665 * @param hpcd: PCD handle
\r
668 __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
\r
670 /* NOTE : This function Should not be modified, when the callback is needed,
\r
671 the HAL_PCD_ResetCallback could be implemented in the user file
\r
677 * @brief Suspend event callbacks
\r
678 * @param hpcd: PCD handle
\r
681 __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
\r
683 /* NOTE : This function Should not be modified, when the callback is needed,
\r
684 the HAL_PCD_SuspendCallback could be implemented in the user file
\r
689 * @brief Resume event callbacks
\r
690 * @param hpcd: PCD handle
\r
693 __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
\r
695 /* NOTE : This function Should not be modified, when the callback is needed,
\r
696 the HAL_PCD_ResumeCallback could be implemented in the user file
\r
701 * @brief Incomplete ISO OUT callbacks
\r
702 * @param hpcd: PCD handle
\r
703 * @param epnum: endpoint number
\r
706 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
\r
708 /* NOTE : This function Should not be modified, when the callback is needed,
\r
709 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
\r
714 * @brief Incomplete ISO IN callbacks
\r
715 * @param hpcd: PCD handle
\r
716 * @param epnum: endpoint number
\r
719 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
\r
721 /* NOTE : This function Should not be modified, when the callback is needed,
\r
722 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
\r
727 * @brief Connection event callbacks
\r
728 * @param hpcd: PCD handle
\r
731 __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
\r
733 /* NOTE : This function Should not be modified, when the callback is needed,
\r
734 the HAL_PCD_ConnectCallback could be implemented in the user file
\r
739 * @brief Disconnection event callbacks
\r
740 * @param hpcd: PCD handle
\r
743 __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
\r
745 /* NOTE : This function Should not be modified, when the callback is needed,
\r
746 the HAL_PCD_DisconnectCallback could be implemented in the user file
\r
754 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
\r
755 * @brief management functions
\r
758 ===============================================================================
\r
759 ##### Peripheral Control functions #####
\r
760 ===============================================================================
\r
762 This subsection provides a set of functions allowing to control the PCD data
\r
770 * @brief Connect the USB device
\r
771 * @param hpcd: PCD handle
\r
772 * @retval HAL status
\r
774 HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
\r
777 USB_DevConnect(hpcd->Instance);
\r
778 __HAL_UNLOCK(hpcd);
\r
783 * @brief Disconnect the USB device
\r
784 * @param hpcd: PCD handle
\r
785 * @retval HAL status
\r
787 HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
\r
790 USB_DevDisconnect(hpcd->Instance);
\r
791 __HAL_UNLOCK(hpcd);
\r
796 * @brief Set the USB Device address
\r
797 * @param hpcd: PCD handle
\r
798 * @param address: new device address
\r
799 * @retval HAL status
\r
801 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
\r
804 USB_SetDevAddress(hpcd->Instance, address);
\r
805 __HAL_UNLOCK(hpcd);
\r
809 * @brief Open and configure an endpoint
\r
810 * @param hpcd: PCD handle
\r
811 * @param ep_addr: endpoint address
\r
812 * @param ep_mps: endpoint max packet size
\r
813 * @param ep_type: endpoint type
\r
814 * @retval HAL status
\r
816 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
\r
818 HAL_StatusTypeDef ret = HAL_OK;
\r
819 USB_OTG_EPTypeDef *ep;
\r
821 if ((ep_addr & 0x80) == 0x80)
\r
823 ep = &hpcd->IN_ep[ep_addr & 0x7F];
\r
827 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
\r
829 ep->num = ep_addr & 0x7F;
\r
831 ep->is_in = (0x80 & ep_addr) != 0;
\r
832 ep->maxpacket = ep_mps;
\r
833 ep->type = ep_type;
\r
836 /* Assign a Tx FIFO */
\r
837 ep->tx_fifo_num = ep->num;
\r
839 /* Set initial data PID. */
\r
840 if (ep_type == EP_TYPE_BULK )
\r
842 ep->data_pid_start = 0;
\r
846 USB_ActivateEndpoint(hpcd->Instance , ep);
\r
847 __HAL_UNLOCK(hpcd);
\r
853 * @brief Deactivate an endpoint
\r
854 * @param hpcd: PCD handle
\r
855 * @param ep_addr: endpoint address
\r
856 * @retval HAL status
\r
858 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
\r
860 USB_OTG_EPTypeDef *ep;
\r
862 if ((ep_addr & 0x80) == 0x80)
\r
864 ep = &hpcd->IN_ep[ep_addr & 0x7F];
\r
868 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
\r
870 ep->num = ep_addr & 0x7F;
\r
872 ep->is_in = (0x80 & ep_addr) != 0;
\r
875 USB_DeactivateEndpoint(hpcd->Instance , ep);
\r
876 __HAL_UNLOCK(hpcd);
\r
882 * @brief Receive an amount of data
\r
883 * @param hpcd: PCD handle
\r
884 * @param ep_addr: endpoint address
\r
885 * @param pBuf: pointer to the reception buffer
\r
886 * @param len: amount of data to be received
\r
887 * @retval HAL status
\r
889 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
\r
891 USB_OTG_EPTypeDef *ep;
\r
893 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
\r
895 /*setup and start the Xfer */
\r
896 ep->xfer_buff = pBuf;
\r
897 ep->xfer_len = len;
\r
898 ep->xfer_count = 0;
\r
900 ep->num = ep_addr & 0x7F;
\r
902 if (hpcd->Init.dma_enable == 1)
\r
904 ep->dma_addr = (uint32_t)pBuf;
\r
909 if ((ep_addr & 0x7F) == 0 )
\r
911 USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
\r
915 USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
\r
917 __HAL_UNLOCK(hpcd);
\r
923 * @brief Get Received Data Size
\r
924 * @param hpcd: PCD handle
\r
925 * @param ep_addr: endpoint address
\r
926 * @retval Data Size
\r
928 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
\r
930 return hpcd->OUT_ep[ep_addr & 0x7F].xfer_count;
\r
933 * @brief Send an amount of data
\r
934 * @param hpcd: PCD handle
\r
935 * @param ep_addr: endpoint address
\r
936 * @param pBuf: pointer to the transmission buffer
\r
937 * @param len: amount of data to be sent
\r
938 * @retval HAL status
\r
940 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
\r
942 USB_OTG_EPTypeDef *ep;
\r
944 ep = &hpcd->IN_ep[ep_addr & 0x7F];
\r
946 /*setup and start the Xfer */
\r
947 ep->xfer_buff = pBuf;
\r
948 ep->xfer_len = len;
\r
949 ep->xfer_count = 0;
\r
951 ep->num = ep_addr & 0x7F;
\r
953 if (hpcd->Init.dma_enable == 1)
\r
955 ep->dma_addr = (uint32_t)pBuf;
\r
960 if ((ep_addr & 0x7F) == 0 )
\r
962 USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
\r
966 USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
\r
969 __HAL_UNLOCK(hpcd);
\r
975 * @brief Set a STALL condition over an endpoint
\r
976 * @param hpcd: PCD handle
\r
977 * @param ep_addr: endpoint address
\r
978 * @retval HAL status
\r
980 HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
\r
982 USB_OTG_EPTypeDef *ep;
\r
984 if ((0x80 & ep_addr) == 0x80)
\r
986 ep = &hpcd->IN_ep[ep_addr & 0x7F];
\r
990 ep = &hpcd->OUT_ep[ep_addr];
\r
994 ep->num = ep_addr & 0x7F;
\r
995 ep->is_in = ((ep_addr & 0x80) == 0x80);
\r
999 USB_EPSetStall(hpcd->Instance , ep);
\r
1000 if((ep_addr & 0x7F) == 0)
\r
1002 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
\r
1004 __HAL_UNLOCK(hpcd);
\r
1010 * @brief Clear a STALL condition over in an endpoint
\r
1011 * @param hpcd: PCD handle
\r
1012 * @param ep_addr: endpoint address
\r
1013 * @retval HAL status
\r
1015 HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
\r
1017 USB_OTG_EPTypeDef *ep;
\r
1019 if ((0x80 & ep_addr) == 0x80)
\r
1021 ep = &hpcd->IN_ep[ep_addr & 0x7F];
\r
1025 ep = &hpcd->OUT_ep[ep_addr];
\r
1029 ep->num = ep_addr & 0x7F;
\r
1030 ep->is_in = ((ep_addr & 0x80) == 0x80);
\r
1032 __HAL_LOCK(hpcd);
\r
1033 USB_EPClearStall(hpcd->Instance , ep);
\r
1034 __HAL_UNLOCK(hpcd);
\r
1040 * @brief Flush an endpoint
\r
1041 * @param hpcd: PCD handle
\r
1042 * @param ep_addr: endpoint address
\r
1043 * @retval HAL status
\r
1045 HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
\r
1047 __HAL_LOCK(hpcd);
\r
1049 if ((ep_addr & 0x80) == 0x80)
\r
1051 USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
\r
1055 USB_FlushRxFifo(hpcd->Instance);
\r
1058 __HAL_UNLOCK(hpcd);
\r
1064 * @brief HAL_PCD_ActivateRemoteWakeup : Active remote wake-up signalling
\r
1065 * @param hpcd: PCD handle
\r
1066 * @retval HAL status
\r
1068 HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
\r
1070 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
\r
1072 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
\r
1074 /* Activate Remote wake-up signaling */
\r
1075 USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
\r
1081 * @brief HAL_PCD_DeActivateRemoteWakeup : de-active remote wake-up signalling
\r
1082 * @param hpcd: PCD handle
\r
1083 * @retval HAL status
\r
1085 HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
\r
1087 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
\r
1089 /* De-activate Remote wake-up signaling */
\r
1090 USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
\r
1097 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
\r
1098 * @brief Peripheral State functions
\r
1101 ===============================================================================
\r
1102 ##### Peripheral State functions #####
\r
1103 ===============================================================================
\r
1105 This subsection permits to get in run-time the status of the peripheral
\r
1106 and the data flow.
\r
1113 * @brief Return the PCD state
\r
1114 * @param hpcd: PCD handle
\r
1115 * @retval HAL state
\r
1117 PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
\r
1119 return hpcd->State;
\r
1129 /* Private functions ---------------------------------------------------------*/
\r
1130 /** @addtogroup PCD_Private_Functions
\r
1135 * @brief DCD_WriteEmptyTxFifo
\r
1136 * check FIFO for the next packet to be loaded
\r
1137 * @param hpcd: PCD handle
\r
1138 * @param epnum : endpoint number
\r
1139 * @retval HAL status
\r
1141 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
\r
1143 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
\r
1144 USB_OTG_EPTypeDef *ep;
\r
1147 uint32_t fifoemptymsk = 0;
\r
1149 ep = &hpcd->IN_ep[epnum];
\r
1150 len = ep->xfer_len - ep->xfer_count;
\r
1152 if (len > ep->maxpacket)
\r
1154 len = ep->maxpacket;
\r
1158 len32b = (len + 3) / 4;
\r
1160 while ( (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b &&
\r
1161 ep->xfer_count < ep->xfer_len &&
\r
1162 ep->xfer_len != 0)
\r
1164 /* Write the FIFO */
\r
1165 len = ep->xfer_len - ep->xfer_count;
\r
1167 if (len > ep->maxpacket)
\r
1169 len = ep->maxpacket;
\r
1171 len32b = (len + 3) / 4;
\r
1173 USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
\r
1175 ep->xfer_buff += len;
\r
1176 ep->xfer_count += len;
\r
1181 fifoemptymsk = 0x1 << epnum;
\r
1182 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
\r
1193 #endif /* HAL_PCD_MODULE_ENABLED */
\r
1202 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r