2 ******************************************************************************
3 * @file stm32f7xx_hal_pcd.c
4 * @author MCD Application Team
7 * @brief PCD HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the USB Peripheral Controller:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The PCD HAL driver can be used as follows:
22 (#) Declare a PCD_HandleTypeDef handle structure, for example:
23 PCD_HandleTypeDef hpcd;
25 (#) Fill parameters of Init structure in HCD handle
27 (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
29 (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
30 (##) Enable the PCD/USB Low Level interface clock using
31 (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
32 (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
34 (##) Initialize the related GPIO clocks
35 (##) Configure PCD pin-out
36 (##) Configure PCD NVIC interrupt
38 (#)Associate the Upper USB device stack to the HAL PCD Driver:
39 (##) hpcd.pData = pdev;
41 (#)Enable PCD transmission and reception:
45 ******************************************************************************
48 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
50 * Redistribution and use in source and binary forms, with or without modification,
51 * are permitted provided that the following conditions are met:
52 * 1. Redistributions of source code must retain the above copyright notice,
53 * this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright notice,
55 * this list of conditions and the following disclaimer in the documentation
56 * and/or other materials provided with the distribution.
57 * 3. Neither the name of STMicroelectronics nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
62 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
67 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
68 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
69 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
70 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72 ******************************************************************************
75 /* Includes ------------------------------------------------------------------*/
76 #include "stm32f7xx_hal.h"
78 /** @addtogroup STM32F7xx_HAL_Driver
83 * @brief PCD HAL module driver
87 #ifdef HAL_PCD_MODULE_ENABLED
89 /* Private types -------------------------------------------------------------*/
90 /* Private variables ---------------------------------------------------------*/
91 /* Private constants ---------------------------------------------------------*/
92 /* Private macros ------------------------------------------------------------*/
93 /** @defgroup PCD_Private_Macros PCD Private Macros
96 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
97 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
102 /* Private functions prototypes ----------------------------------------------*/
103 /** @defgroup PCD_Private_Functions PCD Private Functions
106 static HAL_StatusTypeDef
PCD_WriteEmptyTxFifo(PCD_HandleTypeDef
*hpcd
, uint32_t epnum
);
111 /* Exported functions --------------------------------------------------------*/
112 /** @defgroup PCD_Exported_Functions PCD Exported Functions
116 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
117 * @brief Initialization and Configuration functions
120 ===============================================================================
121 ##### Initialization and de-initialization functions #####
122 ===============================================================================
123 [..] This section provides functions allowing to:
130 * @brief Initializes the PCD according to the specified
131 * parameters in the PCD_InitTypeDef and create the associated handle.
132 * @param hpcd: PCD handle
135 HAL_StatusTypeDef
HAL_PCD_Init(PCD_HandleTypeDef
*hpcd
)
139 /* Check the PCD handle allocation */
145 /* Check the parameters */
146 assert_param(IS_PCD_ALL_INSTANCE(hpcd
->Instance
));
148 hpcd
->State
= HAL_PCD_STATE_BUSY
;
150 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
151 HAL_PCD_MspInit(hpcd
);
153 /* Disable the Interrupts */
154 __HAL_PCD_DISABLE(hpcd
);
156 /*Init the Core (common init.) */
157 USB_CoreInit(hpcd
->Instance
, hpcd
->Init
);
159 /* Force Device Mode*/
160 USB_SetCurrentMode(hpcd
->Instance
, USB_OTG_DEVICE_MODE
);
162 /* Init endpoints structures */
163 for (i
= 0; i
< 15 ; i
++)
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;
176 for (i
= 0; i
< 15 ; i
++)
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;
187 hpcd
->Instance
->DIEPTXF
[i
] = 0;
191 USB_DevInit(hpcd
->Instance
, hpcd
->Init
);
193 hpcd
->State
= HAL_PCD_STATE_READY
;
196 if (hpcd
->Init
.lpm_enable
== 1)
198 HAL_PCDEx_ActivateLPM(hpcd
);
201 USB_DevDisconnect (hpcd
->Instance
);
206 * @brief DeInitializes the PCD peripheral.
207 * @param hpcd: PCD handle
210 HAL_StatusTypeDef
HAL_PCD_DeInit(PCD_HandleTypeDef
*hpcd
)
212 /* Check the PCD handle allocation */
218 hpcd
->State
= HAL_PCD_STATE_BUSY
;
223 /* DeInit the low level hardware */
224 HAL_PCD_MspDeInit(hpcd
);
226 hpcd
->State
= HAL_PCD_STATE_RESET
;
232 * @brief Initializes the PCD MSP.
233 * @param hpcd: PCD handle
236 __weak
void HAL_PCD_MspInit(PCD_HandleTypeDef
*hpcd
)
238 /* Prevent unused argument(s) compilation warning */
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
247 * @brief DeInitializes PCD MSP.
248 * @param hpcd: PCD handle
251 __weak
void HAL_PCD_MspDeInit(PCD_HandleTypeDef
*hpcd
)
253 /* Prevent unused argument(s) compilation warning */
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
265 /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
266 * @brief Data transfers functions
269 ===============================================================================
270 ##### IO operation functions #####
271 ===============================================================================
273 This subsection provides a set of functions allowing to manage the PCD data
281 * @brief Start The USB OTG Device.
282 * @param hpcd: PCD handle
285 HAL_StatusTypeDef
HAL_PCD_Start(PCD_HandleTypeDef
*hpcd
)
288 USB_DevConnect (hpcd
->Instance
);
289 __HAL_PCD_ENABLE(hpcd
);
295 * @brief Stop The USB OTG Device.
296 * @param hpcd: PCD handle
299 HAL_StatusTypeDef
HAL_PCD_Stop(PCD_HandleTypeDef
*hpcd
)
302 __HAL_PCD_DISABLE(hpcd
);
303 USB_StopDevice(hpcd
->Instance
);
304 USB_DevDisconnect (hpcd
->Instance
);
310 * @brief Handle PCD interrupt request.
311 * @param hpcd: PCD handle
314 void HAL_PCD_IRQHandler(PCD_HandleTypeDef
*hpcd
)
316 USB_OTG_GlobalTypeDef
*USBx
= hpcd
->Instance
;
317 uint32_t i
= 0, ep_intr
= 0, epint
= 0, epnum
= 0;
318 uint32_t fifoemptymsk
= 0, temp
= 0;
319 USB_OTG_EPTypeDef
*ep
= NULL
;
320 uint32_t hclk
= 200000000;
322 /* ensure that we are in device mode */
323 if (USB_GetMode(hpcd
->Instance
) == USB_OTG_MODE_DEVICE
)
325 /* avoid spurious interrupt */
326 if(__HAL_PCD_IS_INVALID_INTERRUPT(hpcd
))
331 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_MMIS
))
333 /* incorrect mode, acknowledge the interrupt */
334 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_MMIS
);
337 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_OEPINT
))
341 /* Read in the device interrupt bits */
342 ep_intr
= USB_ReadDevAllOutEpInterrupt(hpcd
->Instance
);
348 epint
= USB_ReadDevOutEPInterrupt(hpcd
->Instance
, epnum
);
350 if(( epint
& USB_OTG_DOEPINT_XFRC
) == USB_OTG_DOEPINT_XFRC
)
352 CLEAR_OUT_EP_INTR(epnum
, USB_OTG_DOEPINT_XFRC
);
354 if(hpcd
->Init
.dma_enable
== 1)
356 hpcd
->OUT_ep
[epnum
].xfer_count
= hpcd
->OUT_ep
[epnum
].maxpacket
- (USBx_OUTEP(epnum
)->DOEPTSIZ
& USB_OTG_DOEPTSIZ_XFRSIZ
);
357 hpcd
->OUT_ep
[epnum
].xfer_buff
+= hpcd
->OUT_ep
[epnum
].maxpacket
;
360 HAL_PCD_DataOutStageCallback(hpcd
, epnum
);
361 if(hpcd
->Init
.dma_enable
== 1)
363 if((epnum
== 0) && (hpcd
->OUT_ep
[epnum
].xfer_len
== 0))
365 /* this is ZLP, so prepare EP0 for next setup */
366 USB_EP0_OutStart(hpcd
->Instance
, 1, (uint8_t *)hpcd
->Setup
);
371 if(( epint
& USB_OTG_DOEPINT_STUP
) == USB_OTG_DOEPINT_STUP
)
373 /* Inform the upper layer that a setup packet is available */
374 HAL_PCD_SetupStageCallback(hpcd
);
375 CLEAR_OUT_EP_INTR(epnum
, USB_OTG_DOEPINT_STUP
);
378 if(( epint
& USB_OTG_DOEPINT_OTEPDIS
) == USB_OTG_DOEPINT_OTEPDIS
)
380 CLEAR_OUT_EP_INTR(epnum
, USB_OTG_DOEPINT_OTEPDIS
);
382 /* Clear Status Phase Received interrupt */
383 if(( epint
& USB_OTG_DOEPINT_OTEPSPR
) == USB_OTG_DOEPINT_OTEPSPR
)
385 CLEAR_OUT_EP_INTR(epnum
, USB_OTG_DOEPINT_OTEPSPR
);
393 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_IEPINT
))
395 /* Read in the device interrupt bits */
396 ep_intr
= USB_ReadDevAllInEpInterrupt(hpcd
->Instance
);
402 if (ep_intr
& 0x1) /* In ITR */
404 epint
= USB_ReadDevInEPInterrupt(hpcd
->Instance
, epnum
);
406 if(( epint
& USB_OTG_DIEPINT_XFRC
) == USB_OTG_DIEPINT_XFRC
)
408 fifoemptymsk
= 0x1 << epnum
;
409 USBx_DEVICE
->DIEPEMPMSK
&= ~fifoemptymsk
;
411 CLEAR_IN_EP_INTR(epnum
, USB_OTG_DIEPINT_XFRC
);
413 if (hpcd
->Init
.dma_enable
== 1)
415 hpcd
->IN_ep
[epnum
].xfer_buff
+= hpcd
->IN_ep
[epnum
].maxpacket
;
418 HAL_PCD_DataInStageCallback(hpcd
, epnum
);
420 if (hpcd
->Init
.dma_enable
== 1)
422 /* this is ZLP, so prepare EP0 for next setup */
423 if((epnum
== 0) && (hpcd
->IN_ep
[epnum
].xfer_len
== 0))
425 /* prepare to rx more setup packets */
426 USB_EP0_OutStart(hpcd
->Instance
, 1, (uint8_t *)hpcd
->Setup
);
430 if(( epint
& USB_OTG_DIEPINT_TOC
) == USB_OTG_DIEPINT_TOC
)
432 CLEAR_IN_EP_INTR(epnum
, USB_OTG_DIEPINT_TOC
);
434 if(( epint
& USB_OTG_DIEPINT_ITTXFE
) == USB_OTG_DIEPINT_ITTXFE
)
436 CLEAR_IN_EP_INTR(epnum
, USB_OTG_DIEPINT_ITTXFE
);
438 if(( epint
& USB_OTG_DIEPINT_INEPNE
) == USB_OTG_DIEPINT_INEPNE
)
440 CLEAR_IN_EP_INTR(epnum
, USB_OTG_DIEPINT_INEPNE
);
442 if(( epint
& USB_OTG_DIEPINT_EPDISD
) == USB_OTG_DIEPINT_EPDISD
)
444 CLEAR_IN_EP_INTR(epnum
, USB_OTG_DIEPINT_EPDISD
);
446 if(( epint
& USB_OTG_DIEPINT_TXFE
) == USB_OTG_DIEPINT_TXFE
)
448 PCD_WriteEmptyTxFifo(hpcd
, epnum
);
456 /* Handle Resume Interrupt */
457 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_WKUINT
))
459 /* Clear the Remote Wake-up Signaling */
460 USBx_DEVICE
->DCTL
&= ~USB_OTG_DCTL_RWUSIG
;
462 if(hpcd
->LPM_State
== LPM_L1
)
464 hpcd
->LPM_State
= LPM_L0
;
465 HAL_PCDEx_LPM_Callback(hpcd
, PCD_LPM_L0_ACTIVE
);
469 HAL_PCD_ResumeCallback(hpcd
);
471 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_WKUINT
);
474 /* Handle Suspend Interrupt */
475 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_USBSUSP
))
477 if((USBx_DEVICE
->DSTS
& USB_OTG_DSTS_SUSPSTS
) == USB_OTG_DSTS_SUSPSTS
)
480 HAL_PCD_SuspendCallback(hpcd
);
482 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_USBSUSP
);
485 /* Handle LPM Interrupt */
486 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_LPMINT
))
488 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_LPMINT
);
489 if( hpcd
->LPM_State
== LPM_L0
)
491 hpcd
->LPM_State
= LPM_L1
;
492 hpcd
->BESL
= (hpcd
->Instance
->GLPMCFG
& USB_OTG_GLPMCFG_BESL
) >>2 ;
493 HAL_PCDEx_LPM_Callback(hpcd
, PCD_LPM_L1_ACTIVE
);
497 HAL_PCD_SuspendCallback(hpcd
);
501 /* Handle Reset Interrupt */
502 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_USBRST
))
504 USBx_DEVICE
->DCTL
&= ~USB_OTG_DCTL_RWUSIG
;
505 USB_FlushTxFifo(hpcd
->Instance
, 0 );
507 for (i
= 0; i
< hpcd
->Init
.dev_endpoints
; i
++)
509 USBx_INEP(i
)->DIEPINT
= 0xFF;
510 USBx_OUTEP(i
)->DOEPINT
= 0xFF;
512 USBx_DEVICE
->DAINT
= 0xFFFFFFFF;
513 USBx_DEVICE
->DAINTMSK
|= 0x10001;
515 if(hpcd
->Init
.use_dedicated_ep1
)
517 USBx_DEVICE
->DOUTEP1MSK
|= (USB_OTG_DOEPMSK_STUPM
| USB_OTG_DOEPMSK_XFRCM
| USB_OTG_DOEPMSK_EPDM
);
518 USBx_DEVICE
->DINEP1MSK
|= (USB_OTG_DIEPMSK_TOM
| USB_OTG_DIEPMSK_XFRCM
| USB_OTG_DIEPMSK_EPDM
);
522 USBx_DEVICE
->DOEPMSK
|= (USB_OTG_DOEPMSK_STUPM
| USB_OTG_DOEPMSK_XFRCM
| USB_OTG_DOEPMSK_EPDM
| USB_OTG_DOEPMSK_OTEPSPRM
);
523 USBx_DEVICE
->DIEPMSK
|= (USB_OTG_DIEPMSK_TOM
| USB_OTG_DIEPMSK_XFRCM
| USB_OTG_DIEPMSK_EPDM
);
526 /* Set Default Address to 0 */
527 USBx_DEVICE
->DCFG
&= ~USB_OTG_DCFG_DAD
;
529 /* setup EP0 to receive SETUP packets */
530 USB_EP0_OutStart(hpcd
->Instance
, hpcd
->Init
.dma_enable
, (uint8_t *)hpcd
->Setup
);
532 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_USBRST
);
535 /* Handle Enumeration done Interrupt */
536 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_ENUMDNE
))
538 USB_ActivateSetup(hpcd
->Instance
);
539 hpcd
->Instance
->GUSBCFG
&= ~USB_OTG_GUSBCFG_TRDT
;
541 if ( USB_GetDevSpeed(hpcd
->Instance
) == USB_OTG_SPEED_HIGH
)
543 hpcd
->Init
.speed
= USB_OTG_SPEED_HIGH
;
544 hpcd
->Init
.ep0_mps
= USB_OTG_HS_MAX_PACKET_SIZE
;
545 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((USBD_HS_TRDT_VALUE
<< 10) & USB_OTG_GUSBCFG_TRDT
);
549 hpcd
->Init
.speed
= USB_OTG_SPEED_FULL
;
550 hpcd
->Init
.ep0_mps
= USB_OTG_FS_MAX_PACKET_SIZE
;
552 /* The USBTRD is configured according to the tables below, depending on AHB frequency
553 used by application. In the low AHB frequency range it is used to stretch enough the USB response
554 time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
555 latency to the Data FIFO */
557 /* Get hclk frequency value */
558 hclk
= HAL_RCC_GetHCLKFreq();
560 if((hclk
>= 14200000)&&(hclk
< 15000000))
562 /* hclk Clock Range between 14.2-15 MHz */
563 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0xF << 10) & USB_OTG_GUSBCFG_TRDT
);
566 else if((hclk
>= 15000000)&&(hclk
< 16000000))
568 /* hclk Clock Range between 15-16 MHz */
569 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0xE << 10) & USB_OTG_GUSBCFG_TRDT
);
572 else if((hclk
>= 16000000)&&(hclk
< 17200000))
574 /* hclk Clock Range between 16-17.2 MHz */
575 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0xD << 10) & USB_OTG_GUSBCFG_TRDT
);
578 else if((hclk
>= 17200000)&&(hclk
< 18500000))
580 /* hclk Clock Range between 17.2-18.5 MHz */
581 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0xC << 10) & USB_OTG_GUSBCFG_TRDT
);
584 else if((hclk
>= 18500000)&&(hclk
< 20000000))
586 /* hclk Clock Range between 18.5-20 MHz */
587 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0xB << 10) & USB_OTG_GUSBCFG_TRDT
);
590 else if((hclk
>= 20000000)&&(hclk
< 21800000))
592 /* hclk Clock Range between 20-21.8 MHz */
593 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0xA << 10) & USB_OTG_GUSBCFG_TRDT
);
596 else if((hclk
>= 21800000)&&(hclk
< 24000000))
598 /* hclk Clock Range between 21.8-24 MHz */
599 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0x9 << 10) & USB_OTG_GUSBCFG_TRDT
);
602 else if((hclk
>= 24000000)&&(hclk
< 27700000))
604 /* hclk Clock Range between 24-27.7 MHz */
605 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0x8 << 10) & USB_OTG_GUSBCFG_TRDT
);
608 else if((hclk
>= 27700000)&&(hclk
< 32000000))
610 /* hclk Clock Range between 27.7-32 MHz */
611 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0x7 << 10) & USB_OTG_GUSBCFG_TRDT
);
614 else /* if(hclk >= 32000000) */
616 /* hclk Clock Range between 32-200 MHz */
617 hpcd
->Instance
->GUSBCFG
|= (uint32_t)((0x6 << 10) & USB_OTG_GUSBCFG_TRDT
);
621 HAL_PCD_ResetCallback(hpcd
);
623 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_ENUMDNE
);
626 /* Handle RxQLevel Interrupt */
627 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_RXFLVL
))
629 USB_MASK_INTERRUPT(hpcd
->Instance
, USB_OTG_GINTSTS_RXFLVL
);
630 temp
= USBx
->GRXSTSP
;
631 ep
= &hpcd
->OUT_ep
[temp
& USB_OTG_GRXSTSP_EPNUM
];
633 if(((temp
& USB_OTG_GRXSTSP_PKTSTS
) >> 17) == STS_DATA_UPDT
)
635 if((temp
& USB_OTG_GRXSTSP_BCNT
) != 0)
637 USB_ReadPacket(USBx
, ep
->xfer_buff
, (temp
& USB_OTG_GRXSTSP_BCNT
) >> 4);
638 ep
->xfer_buff
+= (temp
& USB_OTG_GRXSTSP_BCNT
) >> 4;
639 ep
->xfer_count
+= (temp
& USB_OTG_GRXSTSP_BCNT
) >> 4;
642 else if (((temp
& USB_OTG_GRXSTSP_PKTSTS
) >> 17) == STS_SETUP_UPDT
)
644 USB_ReadPacket(USBx
, (uint8_t *)hpcd
->Setup
, 8);
645 ep
->xfer_count
+= (temp
& USB_OTG_GRXSTSP_BCNT
) >> 4;
647 USB_UNMASK_INTERRUPT(hpcd
->Instance
, USB_OTG_GINTSTS_RXFLVL
);
650 /* Handle SOF Interrupt */
651 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_SOF
))
653 HAL_PCD_SOFCallback(hpcd
);
654 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_SOF
);
657 /* Handle Incomplete ISO IN Interrupt */
658 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_IISOIXFR
))
660 HAL_PCD_ISOINIncompleteCallback(hpcd
, epnum
);
661 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_IISOIXFR
);
664 /* Handle Incomplete ISO OUT Interrupt */
665 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT
))
667 HAL_PCD_ISOOUTIncompleteCallback(hpcd
, epnum
);
668 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT
);
671 /* Handle Connection event Interrupt */
672 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_SRQINT
))
674 HAL_PCD_ConnectCallback(hpcd
);
675 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_OTG_GINTSTS_SRQINT
);
678 /* Handle Disconnection event Interrupt */
679 if(__HAL_PCD_GET_FLAG(hpcd
, USB_OTG_GINTSTS_OTGINT
))
681 temp
= hpcd
->Instance
->GOTGINT
;
683 if((temp
& USB_OTG_GOTGINT_SEDET
) == USB_OTG_GOTGINT_SEDET
)
685 HAL_PCD_DisconnectCallback(hpcd
);
687 hpcd
->Instance
->GOTGINT
|= temp
;
693 * @brief Data OUT stage callback.
694 * @param hpcd: PCD handle
695 * @param epnum: endpoint number
698 __weak
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
700 /* Prevent unused argument(s) compilation warning */
703 /* NOTE : This function Should not be modified, when the callback is needed,
704 the HAL_PCD_DataOutStageCallback could be implemented in the user file
709 * @brief Data IN stage callback.
710 * @param hpcd: PCD handle
711 * @param epnum: endpoint number
714 __weak
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
716 /* Prevent unused argument(s) compilation warning */
719 /* NOTE : This function Should not be modified, when the callback is needed,
720 the HAL_PCD_DataInStageCallback could be implemented in the user file
724 * @brief Setup stage callback.
725 * @param hpcd: PCD handle
728 __weak
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef
*hpcd
)
730 /* Prevent unused argument(s) compilation warning */
733 /* NOTE : This function Should not be modified, when the callback is needed,
734 the HAL_PCD_SetupStageCallback could be implemented in the user file
739 * @brief USB Start Of Frame callback.
740 * @param hpcd: PCD handle
743 __weak
void HAL_PCD_SOFCallback(PCD_HandleTypeDef
*hpcd
)
745 /* Prevent unused argument(s) compilation warning */
748 /* NOTE : This function Should not be modified, when the callback is needed,
749 the HAL_PCD_SOFCallback could be implemented in the user file
754 * @brief USB Reset callback.
755 * @param hpcd: PCD handle
758 __weak
void HAL_PCD_ResetCallback(PCD_HandleTypeDef
*hpcd
)
760 /* Prevent unused argument(s) compilation warning */
763 /* NOTE : This function Should not be modified, when the callback is needed,
764 the HAL_PCD_ResetCallback could be implemented in the user file
769 * @brief Suspend event callback.
770 * @param hpcd: PCD handle
773 __weak
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef
*hpcd
)
775 /* Prevent unused argument(s) compilation warning */
778 /* NOTE : This function Should not be modified, when the callback is needed,
779 the HAL_PCD_SuspendCallback could be implemented in the user file
784 * @brief Resume event callback.
785 * @param hpcd: PCD handle
788 __weak
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef
*hpcd
)
790 /* Prevent unused argument(s) compilation warning */
793 /* NOTE : This function Should not be modified, when the callback is needed,
794 the HAL_PCD_ResumeCallback could be implemented in the user file
799 * @brief Incomplete ISO OUT callback.
800 * @param hpcd: PCD handle
801 * @param epnum: endpoint number
804 __weak
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
806 /* Prevent unused argument(s) compilation warning */
809 /* NOTE : This function Should not be modified, when the callback is needed,
810 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
815 * @brief Incomplete ISO IN callback.
816 * @param hpcd: PCD handle
817 * @param epnum: endpoint number
820 __weak
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
822 /* Prevent unused argument(s) compilation warning */
825 /* NOTE : This function Should not be modified, when the callback is needed,
826 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
831 * @brief Connection event callback.
832 * @param hpcd: PCD handle
835 __weak
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef
*hpcd
)
837 /* Prevent unused argument(s) compilation warning */
840 /* NOTE : This function Should not be modified, when the callback is needed,
841 the HAL_PCD_ConnectCallback could be implemented in the user file
846 * @brief Disconnection event callback.
847 * @param hpcd: PCD handle
850 __weak
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef
*hpcd
)
852 /* Prevent unused argument(s) compilation warning */
855 /* NOTE : This function Should not be modified, when the callback is needed,
856 the HAL_PCD_DisconnectCallback could be implemented in the user file
864 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
865 * @brief management functions
868 ===============================================================================
869 ##### Peripheral Control functions #####
870 ===============================================================================
872 This subsection provides a set of functions allowing to control the PCD data
880 * @brief Connect the USB device.
881 * @param hpcd: PCD handle
884 HAL_StatusTypeDef
HAL_PCD_DevConnect(PCD_HandleTypeDef
*hpcd
)
887 USB_DevConnect(hpcd
->Instance
);
893 * @brief Disconnect the USB device.
894 * @param hpcd: PCD handle
897 HAL_StatusTypeDef
HAL_PCD_DevDisconnect(PCD_HandleTypeDef
*hpcd
)
900 USB_DevDisconnect(hpcd
->Instance
);
906 * @brief Set the USB Device address.
907 * @param hpcd: PCD handle
908 * @param address: new device address
911 HAL_StatusTypeDef
HAL_PCD_SetAddress(PCD_HandleTypeDef
*hpcd
, uint8_t address
)
914 USB_SetDevAddress(hpcd
->Instance
, address
);
919 * @brief Open and configure an endpoint.
920 * @param hpcd: PCD handle
921 * @param ep_addr: endpoint address
922 * @param ep_mps: endpoint max packet size
923 * @param ep_type: endpoint type
926 HAL_StatusTypeDef
HAL_PCD_EP_Open(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint16_t ep_mps
, uint8_t ep_type
)
928 HAL_StatusTypeDef ret
= HAL_OK
;
929 USB_OTG_EPTypeDef
*ep
;
931 if ((ep_addr
& 0x80) == 0x80)
933 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
937 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
939 ep
->num
= ep_addr
& 0x7F;
941 ep
->is_in
= (0x80 & ep_addr
) != 0;
942 ep
->maxpacket
= ep_mps
;
946 /* Assign a Tx FIFO */
947 ep
->tx_fifo_num
= ep
->num
;
949 /* Set initial data PID. */
950 if (ep_type
== EP_TYPE_BULK
)
952 ep
->data_pid_start
= 0;
956 USB_ActivateEndpoint(hpcd
->Instance
, ep
);
963 * @brief Deactivate an endpoint.
964 * @param hpcd: PCD handle
965 * @param ep_addr: endpoint address
968 HAL_StatusTypeDef
HAL_PCD_EP_Close(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
970 USB_OTG_EPTypeDef
*ep
;
972 if ((ep_addr
& 0x80) == 0x80)
974 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
978 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
980 ep
->num
= ep_addr
& 0x7F;
982 ep
->is_in
= (0x80 & ep_addr
) != 0;
985 USB_DeactivateEndpoint(hpcd
->Instance
, ep
);
992 * @brief Receive an amount of data.
993 * @param hpcd: PCD handle
994 * @param ep_addr: endpoint address
995 * @param pBuf: pointer to the reception buffer
996 * @param len: amount of data to be received
999 HAL_StatusTypeDef
HAL_PCD_EP_Receive(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint8_t *pBuf
, uint32_t len
)
1001 USB_OTG_EPTypeDef
*ep
;
1003 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
1005 /*setup and start the Xfer */
1006 ep
->xfer_buff
= pBuf
;
1010 ep
->num
= ep_addr
& 0x7F;
1012 if (hpcd
->Init
.dma_enable
== 1)
1014 ep
->dma_addr
= (uint32_t)pBuf
;
1019 if ((ep_addr
& 0x7F) == 0 )
1021 USB_EP0StartXfer(hpcd
->Instance
, ep
, hpcd
->Init
.dma_enable
);
1025 USB_EPStartXfer(hpcd
->Instance
, ep
, hpcd
->Init
.dma_enable
);
1033 * @brief Get Received Data Size.
1034 * @param hpcd: PCD handle
1035 * @param ep_addr: endpoint address
1038 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1040 return hpcd
->OUT_ep
[ep_addr
& 0x7F].xfer_count
;
1043 * @brief Send an amount of data.
1044 * @param hpcd: PCD handle
1045 * @param ep_addr: endpoint address
1046 * @param pBuf: pointer to the transmission buffer
1047 * @param len: amount of data to be sent
1048 * @retval HAL status
1050 HAL_StatusTypeDef
HAL_PCD_EP_Transmit(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint8_t *pBuf
, uint32_t len
)
1052 USB_OTG_EPTypeDef
*ep
;
1054 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
1056 /*setup and start the Xfer */
1057 ep
->xfer_buff
= pBuf
;
1061 ep
->num
= ep_addr
& 0x7F;
1063 if (hpcd
->Init
.dma_enable
== 1)
1065 ep
->dma_addr
= (uint32_t)pBuf
;
1070 if ((ep_addr
& 0x7F) == 0 )
1072 USB_EP0StartXfer(hpcd
->Instance
, ep
, hpcd
->Init
.dma_enable
);
1076 USB_EPStartXfer(hpcd
->Instance
, ep
, hpcd
->Init
.dma_enable
);
1085 * @brief Set a STALL condition over an endpoint.
1086 * @param hpcd: PCD handle
1087 * @param ep_addr: endpoint address
1088 * @retval HAL status
1090 HAL_StatusTypeDef
HAL_PCD_EP_SetStall(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1092 USB_OTG_EPTypeDef
*ep
;
1094 if ((0x80 & ep_addr
) == 0x80)
1096 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
1100 ep
= &hpcd
->OUT_ep
[ep_addr
];
1104 ep
->num
= ep_addr
& 0x7F;
1105 ep
->is_in
= ((ep_addr
& 0x80) == 0x80);
1109 USB_EPSetStall(hpcd
->Instance
, ep
);
1110 if((ep_addr
& 0x7F) == 0)
1112 USB_EP0_OutStart(hpcd
->Instance
, hpcd
->Init
.dma_enable
, (uint8_t *)hpcd
->Setup
);
1120 * @brief Clear a STALL condition over in an endpoint.
1121 * @param hpcd: PCD handle
1122 * @param ep_addr: endpoint address
1123 * @retval HAL status
1125 HAL_StatusTypeDef
HAL_PCD_EP_ClrStall(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1127 USB_OTG_EPTypeDef
*ep
;
1129 if ((0x80 & ep_addr
) == 0x80)
1131 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
1135 ep
= &hpcd
->OUT_ep
[ep_addr
];
1139 ep
->num
= ep_addr
& 0x7F;
1140 ep
->is_in
= ((ep_addr
& 0x80) == 0x80);
1143 USB_EPClearStall(hpcd
->Instance
, ep
);
1150 * @brief Flush an endpoint.
1151 * @param hpcd: PCD handle
1152 * @param ep_addr: endpoint address
1153 * @retval HAL status
1155 HAL_StatusTypeDef
HAL_PCD_EP_Flush(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1159 if ((ep_addr
& 0x80) == 0x80)
1161 USB_FlushTxFifo(hpcd
->Instance
, ep_addr
& 0x7F);
1165 USB_FlushRxFifo(hpcd
->Instance
);
1174 * @brief Activate remote wakeup signalling.
1175 * @param hpcd: PCD handle
1176 * @retval HAL status
1178 HAL_StatusTypeDef
HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef
*hpcd
)
1180 USB_OTG_GlobalTypeDef
*USBx
= hpcd
->Instance
;
1182 if((USBx_DEVICE
->DSTS
& USB_OTG_DSTS_SUSPSTS
) == USB_OTG_DSTS_SUSPSTS
)
1184 /* Activate Remote wakeup signaling */
1185 USBx_DEVICE
->DCTL
|= USB_OTG_DCTL_RWUSIG
;
1191 * @brief De-activate remote wakeup signalling.
1192 * @param hpcd: PCD handle
1193 * @retval HAL status
1195 HAL_StatusTypeDef
HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef
*hpcd
)
1197 USB_OTG_GlobalTypeDef
*USBx
= hpcd
->Instance
;
1199 /* De-activate Remote wakeup signaling */
1200 USBx_DEVICE
->DCTL
&= ~(USB_OTG_DCTL_RWUSIG
);
1207 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1208 * @brief Peripheral State functions
1211 ===============================================================================
1212 ##### Peripheral State functions #####
1213 ===============================================================================
1215 This subsection permits to get in run-time the status of the peripheral
1223 * @brief Return the PCD handle state.
1224 * @param hpcd: PCD handle
1227 PCD_StateTypeDef
HAL_PCD_GetState(PCD_HandleTypeDef
*hpcd
)
1239 /* Private functions ---------------------------------------------------------*/
1240 /** @addtogroup PCD_Private_Functions
1245 * @brief Check FIFO for the next packet to be loaded.
1246 * @param hpcd: PCD handle
1247 * @param epnum : endpoint number
1248 * @retval HAL status
1250 static HAL_StatusTypeDef
PCD_WriteEmptyTxFifo(PCD_HandleTypeDef
*hpcd
, uint32_t epnum
)
1252 USB_OTG_GlobalTypeDef
*USBx
= hpcd
->Instance
;
1253 USB_OTG_EPTypeDef
*ep
;
1256 uint32_t fifoemptymsk
= 0;
1258 ep
= &hpcd
->IN_ep
[epnum
];
1259 len
= ep
->xfer_len
- ep
->xfer_count
;
1261 if (len
> ep
->maxpacket
)
1263 len
= ep
->maxpacket
;
1267 len32b
= (len
+ 3) / 4;
1269 while ( (USBx_INEP(epnum
)->DTXFSTS
& USB_OTG_DTXFSTS_INEPTFSAV
) > len32b
&&
1270 ep
->xfer_count
< ep
->xfer_len
&&
1273 /* Write the FIFO */
1274 len
= ep
->xfer_len
- ep
->xfer_count
;
1276 if (len
> ep
->maxpacket
)
1278 len
= ep
->maxpacket
;
1280 len32b
= (len
+ 3) / 4;
1282 USB_WritePacket(USBx
, ep
->xfer_buff
, epnum
, len
, hpcd
->Init
.dma_enable
);
1284 ep
->xfer_buff
+= len
;
1285 ep
->xfer_count
+= len
;
1290 fifoemptymsk
= 0x1 << epnum
;
1291 USBx_DEVICE
->DIEPEMPMSK
&= ~fifoemptymsk
;
1302 #endif /* HAL_PCD_MODULE_ENABLED */
1311 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/