Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_pcd.c
blobca4cd8415294ed097035b6a700f721673942b2d8
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_pcd.c
4 * @author MCD Application Team
5 * @version V1.2.2
6 * @date 14-April-2017
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
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
19 [..]
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:
42 (##) HAL_PCD_Start();
44 @endverbatim
45 ******************************************************************************
46 * @attention
48 * <h2><center>&copy; COPYRIGHT(c) 2017 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 ******************************************************************************
73 */
75 /* Includes ------------------------------------------------------------------*/
76 #include "stm32f7xx_hal.h"
78 /** @addtogroup STM32F7xx_HAL_Driver
79 * @{
82 /** @defgroup PCD PCD
83 * @brief PCD HAL module driver
84 * @{
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
94 * @{
95 */
96 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
97 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
98 /**
99 * @}
102 /* Private functions prototypes ----------------------------------------------*/
103 /** @defgroup PCD_Private_Functions PCD Private Functions
104 * @{
106 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
108 * @}
111 /* Exported functions --------------------------------------------------------*/
112 /** @defgroup PCD_Exported_Functions PCD Exported Functions
113 * @{
116 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
117 * @brief Initialization and Configuration functions
119 @verbatim
120 ===============================================================================
121 ##### Initialization and de-initialization functions #####
122 ===============================================================================
123 [..] This section provides functions allowing to:
125 @endverbatim
126 * @{
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
133 * @retval HAL status
135 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
137 uint32_t i = 0;
139 /* Check the PCD handle allocation */
140 if(hpcd == NULL)
142 return HAL_ERROR;
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;
190 /* Init Device */
191 USB_DevInit(hpcd->Instance, hpcd->Init);
193 hpcd->State= HAL_PCD_STATE_READY;
195 /* Activate LPM */
196 if (hpcd->Init.lpm_enable ==1)
198 HAL_PCDEx_ActivateLPM(hpcd);
200 #if defined (USB_OTG_GCCFG_BCDEN)
201 /* Activate Battery charging */
202 if (hpcd->Init.battery_charging_enable ==1)
204 HAL_PCDEx_ActivateBCD(hpcd);
206 #endif /* USB_OTG_GCCFG_BCDEN */
208 USB_DevDisconnect (hpcd->Instance);
209 return HAL_OK;
213 * @brief DeInitializes the PCD peripheral.
214 * @param hpcd: PCD handle
215 * @retval HAL status
217 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
219 /* Check the PCD handle allocation */
220 if(hpcd == NULL)
222 return HAL_ERROR;
225 hpcd->State = HAL_PCD_STATE_BUSY;
227 /* Stop Device */
228 HAL_PCD_Stop(hpcd);
230 /* DeInit the low level hardware */
231 HAL_PCD_MspDeInit(hpcd);
233 hpcd->State = HAL_PCD_STATE_RESET;
235 return HAL_OK;
239 * @brief Initializes the PCD MSP.
240 * @param hpcd: PCD handle
241 * @retval None
243 __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
245 /* Prevent unused argument(s) compilation warning */
246 UNUSED(hpcd);
248 /* NOTE : This function Should not be modified, when the callback is needed,
249 the HAL_PCD_MspInit could be implemented in the user file
254 * @brief DeInitializes PCD MSP.
255 * @param hpcd: PCD handle
256 * @retval None
258 __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
260 /* Prevent unused argument(s) compilation warning */
261 UNUSED(hpcd);
263 /* NOTE : This function Should not be modified, when the callback is needed,
264 the HAL_PCD_MspDeInit could be implemented in the user file
269 * @}
272 /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
273 * @brief Data transfers functions
275 @verbatim
276 ===============================================================================
277 ##### IO operation functions #####
278 ===============================================================================
279 [..]
280 This subsection provides a set of functions allowing to manage the PCD data
281 transfers.
283 @endverbatim
284 * @{
288 * @brief Start The USB OTG Device.
289 * @param hpcd: PCD handle
290 * @retval HAL status
292 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
294 __HAL_LOCK(hpcd);
295 USB_DevConnect (hpcd->Instance);
296 __HAL_PCD_ENABLE(hpcd);
297 __HAL_UNLOCK(hpcd);
298 return HAL_OK;
302 * @brief Stop The USB OTG Device.
303 * @param hpcd: PCD handle
304 * @retval HAL status
306 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
308 __HAL_LOCK(hpcd);
309 __HAL_PCD_DISABLE(hpcd);
310 USB_StopDevice(hpcd->Instance);
311 USB_DevDisconnect (hpcd->Instance);
312 __HAL_UNLOCK(hpcd);
313 return HAL_OK;
317 * @brief Handle PCD interrupt request.
318 * @param hpcd: PCD handle
319 * @retval HAL status
321 void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
323 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
324 uint32_t i = 0, ep_intr = 0, epint = 0, epnum = 0;
325 uint32_t fifoemptymsk = 0, temp = 0;
326 USB_OTG_EPTypeDef *ep = NULL;
327 uint32_t hclk = 200000000;
329 /* ensure that we are in device mode */
330 if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
332 /* avoid spurious interrupt */
333 if(__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
335 return;
338 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
340 /* incorrect mode, acknowledge the interrupt */
341 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
344 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
346 epnum = 0;
348 /* Read in the device interrupt bits */
349 ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
351 while ( ep_intr )
353 if (ep_intr & 0x1)
355 epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, epnum);
357 if(( epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
359 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
361 if(hpcd->Init.dma_enable == 1)
363 hpcd->OUT_ep[epnum].xfer_count = hpcd->OUT_ep[epnum].maxpacket- (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
364 hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
367 HAL_PCD_DataOutStageCallback(hpcd, epnum);
368 if(hpcd->Init.dma_enable == 1)
370 if((epnum == 0) && (hpcd->OUT_ep[epnum].xfer_len == 0))
372 /* this is ZLP, so prepare EP0 for next setup */
373 USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
378 if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
380 /* Inform the upper layer that a setup packet is available */
381 HAL_PCD_SetupStageCallback(hpcd);
382 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
385 if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
387 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
389 /* Clear Status Phase Received interrupt */
390 if(( epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
392 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
395 epnum++;
396 ep_intr >>= 1;
400 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
402 /* Read in the device interrupt bits */
403 ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
405 epnum = 0;
407 while ( ep_intr )
409 if (ep_intr & 0x1) /* In ITR */
411 epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
413 if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
415 fifoemptymsk = 0x1 << epnum;
416 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
418 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
420 if (hpcd->Init.dma_enable == 1)
422 hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
425 HAL_PCD_DataInStageCallback(hpcd, epnum);
427 if (hpcd->Init.dma_enable == 1)
429 /* this is ZLP, so prepare EP0 for next setup */
430 if((epnum == 0) && (hpcd->IN_ep[epnum].xfer_len == 0))
432 /* prepare to rx more setup packets */
433 USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
437 if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
439 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
441 if(( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
443 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
445 if(( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
447 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
449 if(( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
451 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
453 if(( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
455 PCD_WriteEmptyTxFifo(hpcd , epnum);
458 epnum++;
459 ep_intr >>= 1;
463 /* Handle Resume Interrupt */
464 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
466 /* Clear the Remote Wake-up Signaling */
467 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
469 if(hpcd->LPM_State == LPM_L1)
471 hpcd->LPM_State = LPM_L0;
472 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
474 else
476 HAL_PCD_ResumeCallback(hpcd);
478 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
481 /* Handle Suspend Interrupt */
482 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
484 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
487 HAL_PCD_SuspendCallback(hpcd);
489 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
492 /* Handle LPM Interrupt */
493 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
495 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
496 if( hpcd->LPM_State == LPM_L0)
498 hpcd->LPM_State = LPM_L1;
499 hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >>2 ;
500 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
502 else
504 HAL_PCD_SuspendCallback(hpcd);
508 /* Handle Reset Interrupt */
509 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
511 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
512 USB_FlushTxFifo(hpcd->Instance, 0x10);
514 for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
516 USBx_INEP(i)->DIEPINT = 0xFF;
517 USBx_OUTEP(i)->DOEPINT = 0xFF;
519 USBx_DEVICE->DAINT = 0xFFFFFFFF;
520 USBx_DEVICE->DAINTMSK |= 0x10001;
522 if(hpcd->Init.use_dedicated_ep1)
524 USBx_DEVICE->DOUTEP1MSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
525 USBx_DEVICE->DINEP1MSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
527 else
529 USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM | USB_OTG_DOEPMSK_OTEPSPRM);
530 USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
533 /* Set Default Address to 0 */
534 USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
536 /* setup EP0 to receive SETUP packets */
537 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
539 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
542 /* Handle Enumeration done Interrupt */
543 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
545 USB_ActivateSetup(hpcd->Instance);
546 hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
548 if ( USB_GetDevSpeed(hpcd->Instance) == USB_OTG_SPEED_HIGH)
550 hpcd->Init.speed = USB_OTG_SPEED_HIGH;
551 hpcd->Init.ep0_mps = USB_OTG_HS_MAX_PACKET_SIZE ;
552 hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_HS_TRDT_VALUE << 10) & USB_OTG_GUSBCFG_TRDT);
554 else
556 hpcd->Init.speed = USB_OTG_SPEED_FULL;
557 hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE ;
559 /* The USBTRD is configured according to the tables below, depending on AHB frequency
560 used by application. In the low AHB frequency range it is used to stretch enough the USB response
561 time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
562 latency to the Data FIFO */
564 /* Get hclk frequency value */
565 hclk = HAL_RCC_GetHCLKFreq();
567 if((hclk >= 14200000)&&(hclk < 15000000))
569 /* hclk Clock Range between 14.2-15 MHz */
570 hpcd->Instance->GUSBCFG |= (uint32_t)((0xF << 10) & USB_OTG_GUSBCFG_TRDT);
573 else if((hclk >= 15000000)&&(hclk < 16000000))
575 /* hclk Clock Range between 15-16 MHz */
576 hpcd->Instance->GUSBCFG |= (uint32_t)((0xE << 10) & USB_OTG_GUSBCFG_TRDT);
579 else if((hclk >= 16000000)&&(hclk < 17200000))
581 /* hclk Clock Range between 16-17.2 MHz */
582 hpcd->Instance->GUSBCFG |= (uint32_t)((0xD << 10) & USB_OTG_GUSBCFG_TRDT);
585 else if((hclk >= 17200000)&&(hclk < 18500000))
587 /* hclk Clock Range between 17.2-18.5 MHz */
588 hpcd->Instance->GUSBCFG |= (uint32_t)((0xC << 10) & USB_OTG_GUSBCFG_TRDT);
591 else if((hclk >= 18500000)&&(hclk < 20000000))
593 /* hclk Clock Range between 18.5-20 MHz */
594 hpcd->Instance->GUSBCFG |= (uint32_t)((0xB << 10) & USB_OTG_GUSBCFG_TRDT);
597 else if((hclk >= 20000000)&&(hclk < 21800000))
599 /* hclk Clock Range between 20-21.8 MHz */
600 hpcd->Instance->GUSBCFG |= (uint32_t)((0xA << 10) & USB_OTG_GUSBCFG_TRDT);
603 else if((hclk >= 21800000)&&(hclk < 24000000))
605 /* hclk Clock Range between 21.8-24 MHz */
606 hpcd->Instance->GUSBCFG |= (uint32_t)((0x9 << 10) & USB_OTG_GUSBCFG_TRDT);
609 else if((hclk >= 24000000)&&(hclk < 27700000))
611 /* hclk Clock Range between 24-27.7 MHz */
612 hpcd->Instance->GUSBCFG |= (uint32_t)((0x8 << 10) & USB_OTG_GUSBCFG_TRDT);
615 else if((hclk >= 27700000)&&(hclk < 32000000))
617 /* hclk Clock Range between 27.7-32 MHz */
618 hpcd->Instance->GUSBCFG |= (uint32_t)((0x7 << 10) & USB_OTG_GUSBCFG_TRDT);
621 else /* if(hclk >= 32000000) */
623 /* hclk Clock Range between 32-200 MHz */
624 hpcd->Instance->GUSBCFG |= (uint32_t)((0x6 << 10) & USB_OTG_GUSBCFG_TRDT);
628 HAL_PCD_ResetCallback(hpcd);
630 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
633 /* Handle RxQLevel Interrupt */
634 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
636 USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
637 temp = USBx->GRXSTSP;
638 ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
640 if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
642 if((temp & USB_OTG_GRXSTSP_BCNT) != 0)
644 USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4);
645 ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
646 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
649 else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
651 USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8);
652 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
654 USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
657 /* Handle SOF Interrupt */
658 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
660 HAL_PCD_SOFCallback(hpcd);
661 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
664 /* Handle Incomplete ISO IN Interrupt */
665 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
667 HAL_PCD_ISOINIncompleteCallback(hpcd, epnum);
668 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
671 /* Handle Incomplete ISO OUT Interrupt */
672 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
674 HAL_PCD_ISOOUTIncompleteCallback(hpcd, epnum);
675 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
678 /* Handle Connection event Interrupt */
679 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
681 HAL_PCD_ConnectCallback(hpcd);
682 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
685 /* Handle Disconnection event Interrupt */
686 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
688 temp = hpcd->Instance->GOTGINT;
690 if((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
692 HAL_PCD_DisconnectCallback(hpcd);
694 hpcd->Instance->GOTGINT |= temp;
700 * @brief Data OUT stage callback.
701 * @param hpcd: PCD handle
702 * @param epnum: endpoint number
703 * @retval None
705 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
707 /* Prevent unused argument(s) compilation warning */
708 UNUSED(hpcd);
709 UNUSED(epnum);
710 /* NOTE : This function Should not be modified, when the callback is needed,
711 the HAL_PCD_DataOutStageCallback could be implemented in the user file
716 * @brief Data IN stage callback.
717 * @param hpcd: PCD handle
718 * @param epnum: endpoint number
719 * @retval None
721 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
723 /* Prevent unused argument(s) compilation warning */
724 UNUSED(hpcd);
725 UNUSED(epnum);
726 /* NOTE : This function Should not be modified, when the callback is needed,
727 the HAL_PCD_DataInStageCallback could be implemented in the user file
731 * @brief Setup stage callback.
732 * @param hpcd: PCD handle
733 * @retval None
735 __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
737 /* Prevent unused argument(s) compilation warning */
738 UNUSED(hpcd);
740 /* NOTE : This function Should not be modified, when the callback is needed,
741 the HAL_PCD_SetupStageCallback could be implemented in the user file
746 * @brief USB Start Of Frame callback.
747 * @param hpcd: PCD handle
748 * @retval None
750 __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
752 /* Prevent unused argument(s) compilation warning */
753 UNUSED(hpcd);
755 /* NOTE : This function Should not be modified, when the callback is needed,
756 the HAL_PCD_SOFCallback could be implemented in the user file
761 * @brief USB Reset callback.
762 * @param hpcd: PCD handle
763 * @retval None
765 __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
767 /* Prevent unused argument(s) compilation warning */
768 UNUSED(hpcd);
770 /* NOTE : This function Should not be modified, when the callback is needed,
771 the HAL_PCD_ResetCallback could be implemented in the user file
776 * @brief Suspend event callback.
777 * @param hpcd: PCD handle
778 * @retval None
780 __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
782 /* Prevent unused argument(s) compilation warning */
783 UNUSED(hpcd);
785 /* NOTE : This function Should not be modified, when the callback is needed,
786 the HAL_PCD_SuspendCallback could be implemented in the user file
791 * @brief Resume event callback.
792 * @param hpcd: PCD handle
793 * @retval None
795 __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
797 /* Prevent unused argument(s) compilation warning */
798 UNUSED(hpcd);
800 /* NOTE : This function Should not be modified, when the callback is needed,
801 the HAL_PCD_ResumeCallback could be implemented in the user file
806 * @brief Incomplete ISO OUT callback.
807 * @param hpcd: PCD handle
808 * @param epnum: endpoint number
809 * @retval None
811 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
813 /* Prevent unused argument(s) compilation warning */
814 UNUSED(hpcd);
815 UNUSED(epnum);
816 /* NOTE : This function Should not be modified, when the callback is needed,
817 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
822 * @brief Incomplete ISO IN callback.
823 * @param hpcd: PCD handle
824 * @param epnum: endpoint number
825 * @retval None
827 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
829 /* Prevent unused argument(s) compilation warning */
830 UNUSED(hpcd);
831 UNUSED(epnum);
832 /* NOTE : This function Should not be modified, when the callback is needed,
833 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
838 * @brief Connection event callback.
839 * @param hpcd: PCD handle
840 * @retval None
842 __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
844 /* Prevent unused argument(s) compilation warning */
845 UNUSED(hpcd);
847 /* NOTE : This function Should not be modified, when the callback is needed,
848 the HAL_PCD_ConnectCallback could be implemented in the user file
853 * @brief Disconnection event callback.
854 * @param hpcd: PCD handle
855 * @retval None
857 __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
859 /* Prevent unused argument(s) compilation warning */
860 UNUSED(hpcd);
862 /* NOTE : This function Should not be modified, when the callback is needed,
863 the HAL_PCD_DisconnectCallback could be implemented in the user file
868 * @}
871 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
872 * @brief management functions
874 @verbatim
875 ===============================================================================
876 ##### Peripheral Control functions #####
877 ===============================================================================
878 [..]
879 This subsection provides a set of functions allowing to control the PCD data
880 transfers.
882 @endverbatim
883 * @{
887 * @brief Connect the USB device.
888 * @param hpcd: PCD handle
889 * @retval HAL status
891 HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
893 __HAL_LOCK(hpcd);
894 USB_DevConnect(hpcd->Instance);
895 __HAL_UNLOCK(hpcd);
896 return HAL_OK;
900 * @brief Disconnect the USB device.
901 * @param hpcd: PCD handle
902 * @retval HAL status
904 HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
906 __HAL_LOCK(hpcd);
907 USB_DevDisconnect(hpcd->Instance);
908 __HAL_UNLOCK(hpcd);
909 return HAL_OK;
913 * @brief Set the USB Device address.
914 * @param hpcd: PCD handle
915 * @param address: new device address
916 * @retval HAL status
918 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
920 __HAL_LOCK(hpcd);
921 USB_SetDevAddress(hpcd->Instance, address);
922 __HAL_UNLOCK(hpcd);
923 return HAL_OK;
926 * @brief Open and configure an endpoint.
927 * @param hpcd: PCD handle
928 * @param ep_addr: endpoint address
929 * @param ep_mps: endpoint max packet size
930 * @param ep_type: endpoint type
931 * @retval HAL status
933 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
935 HAL_StatusTypeDef ret = HAL_OK;
936 USB_OTG_EPTypeDef *ep;
938 if ((ep_addr & 0x80) == 0x80)
940 ep = &hpcd->IN_ep[ep_addr & 0x7F];
942 else
944 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
946 ep->num = ep_addr & 0x7F;
948 ep->is_in = (0x80 & ep_addr) != 0;
949 ep->maxpacket = ep_mps;
950 ep->type = ep_type;
951 if (ep->is_in)
953 /* Assign a Tx FIFO */
954 ep->tx_fifo_num = ep->num;
956 /* Set initial data PID. */
957 if (ep_type == EP_TYPE_BULK )
959 ep->data_pid_start = 0;
962 __HAL_LOCK(hpcd);
963 USB_ActivateEndpoint(hpcd->Instance , ep);
964 __HAL_UNLOCK(hpcd);
965 return ret;
970 * @brief Deactivate an endpoint.
971 * @param hpcd: PCD handle
972 * @param ep_addr: endpoint address
973 * @retval HAL status
975 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
977 USB_OTG_EPTypeDef *ep;
979 if ((ep_addr & 0x80) == 0x80)
981 ep = &hpcd->IN_ep[ep_addr & 0x7F];
983 else
985 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
987 ep->num = ep_addr & 0x7F;
989 ep->is_in = (0x80 & ep_addr) != 0;
991 __HAL_LOCK(hpcd);
992 USB_DeactivateEndpoint(hpcd->Instance , ep);
993 __HAL_UNLOCK(hpcd);
994 return HAL_OK;
999 * @brief Receive an amount of data.
1000 * @param hpcd: PCD handle
1001 * @param ep_addr: endpoint address
1002 * @param pBuf: pointer to the reception buffer
1003 * @param len: amount of data to be received
1004 * @retval HAL status
1006 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1008 USB_OTG_EPTypeDef *ep;
1010 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
1012 /*setup and start the Xfer */
1013 ep->xfer_buff = pBuf;
1014 ep->xfer_len = len;
1015 ep->xfer_count = 0;
1016 ep->is_in = 0;
1017 ep->num = ep_addr & 0x7F;
1019 if (hpcd->Init.dma_enable == 1)
1021 ep->dma_addr = (uint32_t)pBuf;
1024 if ((ep_addr & 0x7F) == 0)
1026 USB_EP0StartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
1028 else
1030 USB_EPStartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
1033 return HAL_OK;
1037 * @brief Get Received Data Size.
1038 * @param hpcd: PCD handle
1039 * @param ep_addr: endpoint address
1040 * @retval Data Size
1042 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1044 return hpcd->OUT_ep[ep_addr & 0xF].xfer_count;
1047 * @brief Send an amount of data.
1048 * @param hpcd: PCD handle
1049 * @param ep_addr: endpoint address
1050 * @param pBuf: pointer to the transmission buffer
1051 * @param len: amount of data to be sent
1052 * @retval HAL status
1054 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1056 USB_OTG_EPTypeDef *ep;
1058 ep = &hpcd->IN_ep[ep_addr & 0x7F];
1060 /*setup and start the Xfer */
1061 ep->xfer_buff = pBuf;
1062 ep->xfer_len = len;
1063 ep->xfer_count = 0;
1064 ep->is_in = 1;
1065 ep->num = ep_addr & 0x7F;
1067 if (hpcd->Init.dma_enable == 1)
1069 ep->dma_addr = (uint32_t)pBuf;
1072 if ((ep_addr & 0x7F) == 0)
1074 USB_EP0StartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
1076 else
1078 USB_EPStartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
1081 return HAL_OK;
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];
1098 else
1100 ep = &hpcd->OUT_ep[ep_addr];
1103 ep->is_stall = 1;
1104 ep->num = ep_addr & 0x7F;
1105 ep->is_in = ((ep_addr & 0x80) == 0x80);
1108 __HAL_LOCK(hpcd);
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);
1114 __HAL_UNLOCK(hpcd);
1116 return HAL_OK;
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];
1133 else
1135 ep = &hpcd->OUT_ep[ep_addr];
1138 ep->is_stall = 0;
1139 ep->num = ep_addr & 0x7F;
1140 ep->is_in = ((ep_addr & 0x80) == 0x80);
1142 __HAL_LOCK(hpcd);
1143 USB_EPClearStall(hpcd->Instance , ep);
1144 __HAL_UNLOCK(hpcd);
1146 return HAL_OK;
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)
1157 __HAL_LOCK(hpcd);
1159 if ((ep_addr & 0x80) == 0x80)
1161 USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
1163 else
1165 USB_FlushRxFifo(hpcd->Instance);
1168 __HAL_UNLOCK(hpcd);
1170 return HAL_OK;
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;
1187 return HAL_OK;
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);
1201 return HAL_OK;
1204 * @}
1207 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1208 * @brief Peripheral State functions
1210 @verbatim
1211 ===============================================================================
1212 ##### Peripheral State functions #####
1213 ===============================================================================
1214 [..]
1215 This subsection permits to get in run-time the status of the peripheral
1216 and the data flow.
1218 @endverbatim
1219 * @{
1223 * @brief Return the PCD handle state.
1224 * @param hpcd: PCD handle
1225 * @retval HAL state
1227 PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
1229 return hpcd->State;
1232 * @}
1236 * @}
1239 /* Private functions ---------------------------------------------------------*/
1240 /** @addtogroup PCD_Private_Functions
1241 * @{
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;
1254 int32_t len = 0;
1255 uint32_t len32b;
1256 uint32_t fifoemptymsk = 0;
1258 ep = &hpcd->IN_ep[epnum];
1259 len = ep->xfer_len - ep->xfer_count;
1261 if (len > (int32_t)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 &&
1271 ep->xfer_len != 0)
1273 /* Write the FIFO */
1274 len = ep->xfer_len - ep->xfer_count;
1276 if (len > (int32_t)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;
1288 if(len <= 0)
1290 fifoemptymsk = 0x1 << epnum;
1291 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
1295 return HAL_OK;
1299 * @}
1302 #endif /* HAL_PCD_MODULE_ENABLED */
1304 * @}
1308 * @}
1311 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/