2 ******************************************************************************
3 * @file stm32h7xx_ll_usb.c
4 * @author MCD Application Team
5 * @brief USB Low Layer HAL module driver.
7 * This file provides firmware functions to manage the following
8 * functionalities of the USB Peripheral Controller:
9 * + Initialization/de-initialization functions
10 * + I/O operation functions
11 * + Peripheral Control functions
12 * + Peripheral State functions
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
19 (#) Fill parameters of Init structure in USB_OTG_CfgTypeDef structure.
21 (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
23 (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
26 ******************************************************************************
29 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
30 * All rights reserved.</center></h2>
32 * This software component is licensed by ST under BSD 3-Clause license,
33 * the "License"; You may not use this file except in compliance with the
34 * License. You may obtain a copy of the License at:
35 * opensource.org/licenses/BSD-3-Clause
37 ******************************************************************************
40 /* Includes ------------------------------------------------------------------*/
41 #include "stm32h7xx_hal.h"
43 /** @addtogroup STM32H7xx_LL_USB_DRIVER
47 #if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
48 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
49 /* Private typedef -----------------------------------------------------------*/
50 /* Private define ------------------------------------------------------------*/
51 /* Private macro -------------------------------------------------------------*/
52 /* Private variables ---------------------------------------------------------*/
53 /* Private function prototypes -----------------------------------------------*/
54 /* Private functions ---------------------------------------------------------*/
55 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
56 static HAL_StatusTypeDef
USB_CoreReset(USB_OTG_GlobalTypeDef
*USBx
);
58 /* Exported functions --------------------------------------------------------*/
59 /** @defgroup USB_LL_Exported_Functions USB Low Layer Exported Functions
63 /** @defgroup USB_LL_Exported_Functions_Group1 Initialization/de-initialization functions
64 * @brief Initialization and Configuration functions
67 ===============================================================================
68 ##### Initialization/de-initialization functions #####
69 ===============================================================================
76 * @brief Initializes the USB Core
77 * @param USBx USB Instance
78 * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
79 * the configuration information for the specified USBx peripheral.
82 HAL_StatusTypeDef
USB_CoreInit(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_CfgTypeDef cfg
)
84 HAL_StatusTypeDef ret
;
86 if (cfg
.phy_itface
== USB_OTG_ULPI_PHY
)
88 USBx
->GCCFG
&= ~(USB_OTG_GCCFG_PWRDWN
);
90 /* Init The ULPI Interface */
91 USBx
->GUSBCFG
&= ~(USB_OTG_GUSBCFG_TSDPS
| USB_OTG_GUSBCFG_ULPIFSLS
| USB_OTG_GUSBCFG_PHYSEL
);
93 /* Select vbus source */
94 USBx
->GUSBCFG
&= ~(USB_OTG_GUSBCFG_ULPIEVBUSD
| USB_OTG_GUSBCFG_ULPIEVBUSI
);
95 if (cfg
.use_external_vbus
== 1U)
97 USBx
->GUSBCFG
|= USB_OTG_GUSBCFG_ULPIEVBUSD
;
99 /* Reset after a PHY select */
100 ret
= USB_CoreReset(USBx
);
102 else /* FS interface (embedded Phy) */
104 /* Select FS Embedded PHY */
105 USBx
->GUSBCFG
|= USB_OTG_GUSBCFG_PHYSEL
;
107 /* Reset after a PHY select and set Host mode */
108 ret
= USB_CoreReset(USBx
);
110 if (cfg
.battery_charging_enable
== 0U)
112 /* Activate the USB Transceiver */
113 USBx
->GCCFG
|= USB_OTG_GCCFG_PWRDWN
;
117 /* Deactivate the USB Transceiver */
118 USBx
->GCCFG
&= ~(USB_OTG_GCCFG_PWRDWN
);
122 if (cfg
.dma_enable
== 1U)
124 USBx
->GAHBCFG
|= USB_OTG_GAHBCFG_HBSTLEN_2
;
125 USBx
->GAHBCFG
|= USB_OTG_GAHBCFG_DMAEN
;
133 * @brief Set the USB turnaround time
134 * @param USBx USB Instance
135 * @param hclk: AHB clock frequency
136 * @retval USB turnaround time In PHY Clocks number
138 HAL_StatusTypeDef
USB_SetTurnaroundTime(USB_OTG_GlobalTypeDef
*USBx
,
139 uint32_t hclk
, uint8_t speed
)
143 /* The USBTRD is configured according to the tables below, depending on AHB frequency
144 used by application. In the low AHB frequency range it is used to stretch enough the USB response
145 time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
146 latency to the Data FIFO */
147 if (speed
== USBD_FS_SPEED
)
149 if ((hclk
>= 14200000U) && (hclk
< 15000000U))
151 /* hclk Clock Range between 14.2-15 MHz */
154 else if ((hclk
>= 15000000U) && (hclk
< 16000000U))
156 /* hclk Clock Range between 15-16 MHz */
159 else if ((hclk
>= 16000000U) && (hclk
< 17200000U))
161 /* hclk Clock Range between 16-17.2 MHz */
164 else if ((hclk
>= 17200000U) && (hclk
< 18500000U))
166 /* hclk Clock Range between 17.2-18.5 MHz */
169 else if ((hclk
>= 18500000U) && (hclk
< 20000000U))
171 /* hclk Clock Range between 18.5-20 MHz */
174 else if ((hclk
>= 20000000U) && (hclk
< 21800000U))
176 /* hclk Clock Range between 20-21.8 MHz */
179 else if ((hclk
>= 21800000U) && (hclk
< 24000000U))
181 /* hclk Clock Range between 21.8-24 MHz */
184 else if ((hclk
>= 24000000U) && (hclk
< 27700000U))
186 /* hclk Clock Range between 24-27.7 MHz */
189 else if ((hclk
>= 27700000U) && (hclk
< 32000000U))
191 /* hclk Clock Range between 27.7-32 MHz */
194 else /* if(hclk >= 32000000) */
196 /* hclk Clock Range between 32-200 MHz */
200 else if (speed
== USBD_HS_SPEED
)
202 UsbTrd
= USBD_HS_TRDT_VALUE
;
206 UsbTrd
= USBD_DEFAULT_TRDT_VALUE
;
209 USBx
->GUSBCFG
&= ~USB_OTG_GUSBCFG_TRDT
;
210 USBx
->GUSBCFG
|= (uint32_t)((UsbTrd
<< 10) & USB_OTG_GUSBCFG_TRDT
);
216 * @brief USB_EnableGlobalInt
217 * Enables the controller's Global Int in the AHB Config reg
218 * @param USBx Selected device
221 HAL_StatusTypeDef
USB_EnableGlobalInt(USB_OTG_GlobalTypeDef
*USBx
)
223 USBx
->GAHBCFG
|= USB_OTG_GAHBCFG_GINT
;
228 * @brief USB_DisableGlobalInt
229 * Disable the controller's Global Int in the AHB Config reg
230 * @param USBx Selected device
233 HAL_StatusTypeDef
USB_DisableGlobalInt(USB_OTG_GlobalTypeDef
*USBx
)
235 USBx
->GAHBCFG
&= ~USB_OTG_GAHBCFG_GINT
;
240 * @brief USB_SetCurrentMode : Set functional mode
241 * @param USBx Selected device
242 * @param mode current core mode
243 * This parameter can be one of these values:
244 * @arg USB_DEVICE_MODE: Peripheral mode
245 * @arg USB_HOST_MODE: Host mode
246 * @arg USB_DRD_MODE: Dual Role Device mode
249 HAL_StatusTypeDef
USB_SetCurrentMode(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_ModeTypeDef mode
)
251 USBx
->GUSBCFG
&= ~(USB_OTG_GUSBCFG_FHMOD
| USB_OTG_GUSBCFG_FDMOD
);
253 if (mode
== USB_HOST_MODE
)
255 USBx
->GUSBCFG
|= USB_OTG_GUSBCFG_FHMOD
;
257 else if (mode
== USB_DEVICE_MODE
)
259 USBx
->GUSBCFG
|= USB_OTG_GUSBCFG_FDMOD
;
271 * @brief USB_DevInit : Initializes the USB_OTG controller registers
273 * @param USBx Selected device
274 * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
275 * the configuration information for the specified USBx peripheral.
278 HAL_StatusTypeDef
USB_DevInit(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_CfgTypeDef cfg
)
280 HAL_StatusTypeDef ret
= HAL_OK
;
281 uint32_t USBx_BASE
= (uint32_t)USBx
;
284 for (i
= 0U; i
< 15U; i
++)
286 USBx
->DIEPTXF
[i
] = 0U;
289 /* VBUS Sensing setup */
290 if (cfg
.vbus_sensing_enable
== 0U)
292 USBx_DEVICE
->DCTL
|= USB_OTG_DCTL_SDIS
;
294 /* Deactivate VBUS Sensing B */
295 USBx
->GCCFG
&= ~USB_OTG_GCCFG_VBDEN
;
297 /* B-peripheral session valid override enable */
298 USBx
->GOTGCTL
|= USB_OTG_GOTGCTL_BVALOEN
;
299 USBx
->GOTGCTL
|= USB_OTG_GOTGCTL_BVALOVAL
;
303 /* Enable HW VBUS sensing */
304 USBx
->GCCFG
|= USB_OTG_GCCFG_VBDEN
;
307 /* Restart the Phy Clock */
310 /* Device mode configuration */
311 USBx_DEVICE
->DCFG
|= DCFG_FRAME_INTERVAL_80
;
313 if (cfg
.phy_itface
== USB_OTG_ULPI_PHY
)
315 if (cfg
.speed
== USBD_HS_SPEED
)
317 /* Set Core speed to High speed mode */
318 (void)USB_SetDevSpeed(USBx
, USB_OTG_SPEED_HIGH
);
322 /* Set Core speed to Full speed mode */
323 (void)USB_SetDevSpeed(USBx
, USB_OTG_SPEED_HIGH_IN_FULL
);
328 /* Set Core speed to Full speed mode */
329 (void)USB_SetDevSpeed(USBx
, USB_OTG_SPEED_FULL
);
332 /* Flush the FIFOs */
333 if (USB_FlushTxFifo(USBx
, 0x10U
) != HAL_OK
) /* all Tx FIFOs */
338 if (USB_FlushRxFifo(USBx
) != HAL_OK
)
343 /* Clear all pending Device Interrupts */
344 USBx_DEVICE
->DIEPMSK
= 0U;
345 USBx_DEVICE
->DOEPMSK
= 0U;
346 USBx_DEVICE
->DAINTMSK
= 0U;
348 for (i
= 0U; i
< cfg
.dev_endpoints
; i
++)
350 if ((USBx_INEP(i
)->DIEPCTL
& USB_OTG_DIEPCTL_EPENA
) == USB_OTG_DIEPCTL_EPENA
)
354 USBx_INEP(i
)->DIEPCTL
= USB_OTG_DIEPCTL_SNAK
;
358 USBx_INEP(i
)->DIEPCTL
= USB_OTG_DIEPCTL_EPDIS
| USB_OTG_DIEPCTL_SNAK
;
363 USBx_INEP(i
)->DIEPCTL
= 0U;
366 USBx_INEP(i
)->DIEPTSIZ
= 0U;
367 USBx_INEP(i
)->DIEPINT
= 0xFB7FU
;
370 for (i
= 0U; i
< cfg
.dev_endpoints
; i
++)
372 if ((USBx_OUTEP(i
)->DOEPCTL
& USB_OTG_DOEPCTL_EPENA
) == USB_OTG_DOEPCTL_EPENA
)
376 USBx_OUTEP(i
)->DOEPCTL
= USB_OTG_DOEPCTL_SNAK
;
380 USBx_OUTEP(i
)->DOEPCTL
= USB_OTG_DOEPCTL_EPDIS
| USB_OTG_DOEPCTL_SNAK
;
385 USBx_OUTEP(i
)->DOEPCTL
= 0U;
388 USBx_OUTEP(i
)->DOEPTSIZ
= 0U;
389 USBx_OUTEP(i
)->DOEPINT
= 0xFB7FU
;
392 USBx_DEVICE
->DIEPMSK
&= ~(USB_OTG_DIEPMSK_TXFURM
);
394 /* Disable all interrupts. */
397 /* Clear any pending interrupts */
398 USBx
->GINTSTS
= 0xBFFFFFFFU
;
400 /* Enable the common interrupts */
401 if (cfg
.dma_enable
== 0U)
403 USBx
->GINTMSK
|= USB_OTG_GINTMSK_RXFLVLM
;
406 /* Enable interrupts matching to the Device mode ONLY */
407 USBx
->GINTMSK
|= USB_OTG_GINTMSK_USBSUSPM
| USB_OTG_GINTMSK_USBRST
|
408 USB_OTG_GINTMSK_ENUMDNEM
| USB_OTG_GINTMSK_IEPINT
|
409 USB_OTG_GINTMSK_OEPINT
| USB_OTG_GINTMSK_IISOIXFRM
|
410 USB_OTG_GINTMSK_PXFRM_IISOOXFRM
| USB_OTG_GINTMSK_WUIM
;
412 if (cfg
.Sof_enable
!= 0U)
414 USBx
->GINTMSK
|= USB_OTG_GINTMSK_SOFM
;
417 if (cfg
.vbus_sensing_enable
== 1U)
419 USBx
->GINTMSK
|= (USB_OTG_GINTMSK_SRQIM
| USB_OTG_GINTMSK_OTGINT
);
426 * @brief USB_OTG_FlushTxFifo : Flush a Tx FIFO
427 * @param USBx Selected device
428 * @param num FIFO number
429 * This parameter can be a value from 1 to 15
430 15 means Flush all Tx FIFOs
433 HAL_StatusTypeDef
USB_FlushTxFifo(USB_OTG_GlobalTypeDef
*USBx
, uint32_t num
)
437 USBx
->GRSTCTL
= (USB_OTG_GRSTCTL_TXFFLSH
| (num
<< 6));
441 if (++count
> 200000U)
446 while ((USBx
->GRSTCTL
& USB_OTG_GRSTCTL_TXFFLSH
) == USB_OTG_GRSTCTL_TXFFLSH
);
452 * @brief USB_FlushRxFifo : Flush Rx FIFO
453 * @param USBx Selected device
456 HAL_StatusTypeDef
USB_FlushRxFifo(USB_OTG_GlobalTypeDef
*USBx
)
460 USBx
->GRSTCTL
= USB_OTG_GRSTCTL_RXFFLSH
;
464 if (++count
> 200000U)
469 while ((USBx
->GRSTCTL
& USB_OTG_GRSTCTL_RXFFLSH
) == USB_OTG_GRSTCTL_RXFFLSH
);
475 * @brief USB_SetDevSpeed Initializes the DevSpd field of DCFG register
476 * depending the PHY type and the enumeration speed of the device.
477 * @param USBx Selected device
478 * @param speed device speed
479 * This parameter can be one of these values:
480 * @arg USB_OTG_SPEED_HIGH: High speed mode
481 * @arg USB_OTG_SPEED_HIGH_IN_FULL: High speed core in Full Speed mode
482 * @arg USB_OTG_SPEED_FULL: Full speed mode
485 HAL_StatusTypeDef
USB_SetDevSpeed(USB_OTG_GlobalTypeDef
*USBx
, uint8_t speed
)
487 uint32_t USBx_BASE
= (uint32_t)USBx
;
489 USBx_DEVICE
->DCFG
|= speed
;
494 * @brief USB_GetDevSpeed Return the Dev Speed
495 * @param USBx Selected device
496 * @retval speed device speed
497 * This parameter can be one of these values:
498 * @arg PCD_SPEED_HIGH: High speed mode
499 * @arg PCD_SPEED_FULL: Full speed mode
501 uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef
*USBx
)
503 uint32_t USBx_BASE
= (uint32_t)USBx
;
505 uint32_t DevEnumSpeed
= USBx_DEVICE
->DSTS
& USB_OTG_DSTS_ENUMSPD
;
507 if (DevEnumSpeed
== DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ
)
509 speed
= USBD_HS_SPEED
;
511 else if ((DevEnumSpeed
== DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ
) ||
512 (DevEnumSpeed
== DSTS_ENUMSPD_FS_PHY_48MHZ
))
514 speed
= USBD_FS_SPEED
;
525 * @brief Activate and configure an endpoint
526 * @param USBx Selected device
527 * @param ep pointer to endpoint structure
530 HAL_StatusTypeDef
USB_ActivateEndpoint(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
532 uint32_t USBx_BASE
= (uint32_t)USBx
;
533 uint32_t epnum
= (uint32_t)ep
->num
;
537 USBx_DEVICE
->DAINTMSK
|= USB_OTG_DAINTMSK_IEPM
& (uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
));
539 if ((USBx_INEP(epnum
)->DIEPCTL
& USB_OTG_DIEPCTL_USBAEP
) == 0U)
541 USBx_INEP(epnum
)->DIEPCTL
|= (ep
->maxpacket
& USB_OTG_DIEPCTL_MPSIZ
) |
542 ((uint32_t)ep
->type
<< 18) | (epnum
<< 22) |
543 USB_OTG_DIEPCTL_SD0PID_SEVNFRM
|
544 USB_OTG_DIEPCTL_USBAEP
;
549 USBx_DEVICE
->DAINTMSK
|= USB_OTG_DAINTMSK_OEPM
& ((uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
)) << 16);
551 if (((USBx_OUTEP(epnum
)->DOEPCTL
) & USB_OTG_DOEPCTL_USBAEP
) == 0U)
553 USBx_OUTEP(epnum
)->DOEPCTL
|= (ep
->maxpacket
& USB_OTG_DOEPCTL_MPSIZ
) |
554 ((uint32_t)ep
->type
<< 18) |
555 USB_OTG_DIEPCTL_SD0PID_SEVNFRM
|
556 USB_OTG_DOEPCTL_USBAEP
;
563 * @brief Activate and configure a dedicated endpoint
564 * @param USBx Selected device
565 * @param ep pointer to endpoint structure
568 HAL_StatusTypeDef
USB_ActivateDedicatedEndpoint(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
570 uint32_t USBx_BASE
= (uint32_t)USBx
;
571 uint32_t epnum
= (uint32_t)ep
->num
;
573 /* Read DEPCTLn register */
576 if (((USBx_INEP(epnum
)->DIEPCTL
) & USB_OTG_DIEPCTL_USBAEP
) == 0U)
578 USBx_INEP(epnum
)->DIEPCTL
|= (ep
->maxpacket
& USB_OTG_DIEPCTL_MPSIZ
) |
579 ((uint32_t)ep
->type
<< 18) | (epnum
<< 22) |
580 USB_OTG_DIEPCTL_SD0PID_SEVNFRM
|
581 USB_OTG_DIEPCTL_USBAEP
;
584 USBx_DEVICE
->DEACHMSK
|= USB_OTG_DAINTMSK_IEPM
& (uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
));
588 if (((USBx_OUTEP(epnum
)->DOEPCTL
) & USB_OTG_DOEPCTL_USBAEP
) == 0U)
590 USBx_OUTEP(epnum
)->DOEPCTL
|= (ep
->maxpacket
& USB_OTG_DOEPCTL_MPSIZ
) |
591 ((uint32_t)ep
->type
<< 18) | (epnum
<< 22) |
592 USB_OTG_DOEPCTL_USBAEP
;
595 USBx_DEVICE
->DEACHMSK
|= USB_OTG_DAINTMSK_OEPM
& ((uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
)) << 16);
602 * @brief De-activate and de-initialize an endpoint
603 * @param USBx Selected device
604 * @param ep pointer to endpoint structure
607 HAL_StatusTypeDef
USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
609 uint32_t USBx_BASE
= (uint32_t)USBx
;
610 uint32_t epnum
= (uint32_t)ep
->num
;
612 /* Read DEPCTLn register */
615 USBx_DEVICE
->DEACHMSK
&= ~(USB_OTG_DAINTMSK_IEPM
& (uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
)));
616 USBx_DEVICE
->DAINTMSK
&= ~(USB_OTG_DAINTMSK_IEPM
& (uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
)));
617 USBx_INEP(epnum
)->DIEPCTL
&= ~(USB_OTG_DIEPCTL_USBAEP
|
618 USB_OTG_DIEPCTL_MPSIZ
|
619 USB_OTG_DIEPCTL_TXFNUM
|
620 USB_OTG_DIEPCTL_SD0PID_SEVNFRM
|
621 USB_OTG_DIEPCTL_EPTYP
);
625 USBx_DEVICE
->DEACHMSK
&= ~(USB_OTG_DAINTMSK_OEPM
& ((uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
)) << 16));
626 USBx_DEVICE
->DAINTMSK
&= ~(USB_OTG_DAINTMSK_OEPM
& ((uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
)) << 16));
627 USBx_OUTEP(epnum
)->DOEPCTL
&= ~(USB_OTG_DOEPCTL_USBAEP
|
628 USB_OTG_DOEPCTL_MPSIZ
|
629 USB_OTG_DOEPCTL_SD0PID_SEVNFRM
|
630 USB_OTG_DOEPCTL_EPTYP
);
637 * @brief De-activate and de-initialize a dedicated endpoint
638 * @param USBx Selected device
639 * @param ep pointer to endpoint structure
642 HAL_StatusTypeDef
USB_DeactivateDedicatedEndpoint(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
644 uint32_t USBx_BASE
= (uint32_t)USBx
;
645 uint32_t epnum
= (uint32_t)ep
->num
;
647 /* Read DEPCTLn register */
650 USBx_INEP(epnum
)->DIEPCTL
&= ~ USB_OTG_DIEPCTL_USBAEP
;
651 USBx_DEVICE
->DAINTMSK
&= ~(USB_OTG_DAINTMSK_IEPM
& (uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
)));
655 USBx_OUTEP(epnum
)->DOEPCTL
&= ~USB_OTG_DOEPCTL_USBAEP
;
656 USBx_DEVICE
->DAINTMSK
&= ~(USB_OTG_DAINTMSK_OEPM
& ((uint32_t)(1UL << (ep
->num
& EP_ADDR_MSK
)) << 16));
663 * @brief USB_EPStartXfer : setup and starts a transfer over an EP
664 * @param USBx Selected device
665 * @param ep pointer to endpoint structure
666 * @param dma USB dma enabled or disabled
667 * This parameter can be one of these values:
668 * 0 : DMA feature not used
669 * 1 : DMA feature used
672 HAL_StatusTypeDef
USB_EPStartXfer(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
, uint8_t dma
)
674 uint32_t USBx_BASE
= (uint32_t)USBx
;
675 uint32_t epnum
= (uint32_t)ep
->num
;
681 /* Zero Length Packet? */
682 if (ep
->xfer_len
== 0U)
684 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_PKTCNT
);
685 USBx_INEP(epnum
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_PKTCNT
& (1U << 19));
686 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_XFRSIZ
);
690 /* Program the transfer size and packet count
691 * as follows: xfersize = N * maxpacket +
692 * short_packet pktcnt = N + (short_packet
695 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_XFRSIZ
);
696 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_PKTCNT
);
697 USBx_INEP(epnum
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_PKTCNT
& (((ep
->xfer_len
+ ep
->maxpacket
- 1U) / ep
->maxpacket
) << 19));
698 USBx_INEP(epnum
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_XFRSIZ
& ep
->xfer_len
);
700 if (ep
->type
== EP_TYPE_ISOC
)
702 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_MULCNT
);
703 USBx_INEP(epnum
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_MULCNT
& (1U << 29));
709 if ((uint32_t)ep
->dma_addr
!= 0U)
711 USBx_INEP(epnum
)->DIEPDMA
= (uint32_t)(ep
->dma_addr
);
714 if (ep
->type
== EP_TYPE_ISOC
)
716 if ((USBx_DEVICE
->DSTS
& (1U << 8)) == 0U)
718 USBx_INEP(epnum
)->DIEPCTL
|= USB_OTG_DIEPCTL_SODDFRM
;
722 USBx_INEP(epnum
)->DIEPCTL
|= USB_OTG_DIEPCTL_SD0PID_SEVNFRM
;
726 /* EP enable, IN data in FIFO */
727 USBx_INEP(epnum
)->DIEPCTL
|= (USB_OTG_DIEPCTL_CNAK
| USB_OTG_DIEPCTL_EPENA
);
731 /* EP enable, IN data in FIFO */
732 USBx_INEP(epnum
)->DIEPCTL
|= (USB_OTG_DIEPCTL_CNAK
| USB_OTG_DIEPCTL_EPENA
);
734 if (ep
->type
!= EP_TYPE_ISOC
)
736 /* Enable the Tx FIFO Empty Interrupt for this EP */
737 if (ep
->xfer_len
> 0U)
739 USBx_DEVICE
->DIEPEMPMSK
|= 1UL << (ep
->num
& EP_ADDR_MSK
);
744 if ((USBx_DEVICE
->DSTS
& (1U << 8)) == 0U)
746 USBx_INEP(epnum
)->DIEPCTL
|= USB_OTG_DIEPCTL_SODDFRM
;
750 USBx_INEP(epnum
)->DIEPCTL
|= USB_OTG_DIEPCTL_SD0PID_SEVNFRM
;
753 (void)USB_WritePacket(USBx
, ep
->xfer_buff
, ep
->num
, (uint16_t)ep
->xfer_len
, dma
);
757 else /* OUT endpoint */
759 /* Program the transfer size and packet count as follows:
761 * xfersize = N * maxpacket
763 USBx_OUTEP(epnum
)->DOEPTSIZ
&= ~(USB_OTG_DOEPTSIZ_XFRSIZ
);
764 USBx_OUTEP(epnum
)->DOEPTSIZ
&= ~(USB_OTG_DOEPTSIZ_PKTCNT
);
766 if (ep
->xfer_len
== 0U)
768 USBx_OUTEP(epnum
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_XFRSIZ
& ep
->maxpacket
);
769 USBx_OUTEP(epnum
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_PKTCNT
& (1U << 19));
773 pktcnt
= (uint16_t)((ep
->xfer_len
+ ep
->maxpacket
- 1U) / ep
->maxpacket
);
774 USBx_OUTEP(epnum
)->DOEPTSIZ
|= USB_OTG_DOEPTSIZ_PKTCNT
& ((uint32_t)pktcnt
<< 19);
775 USBx_OUTEP(epnum
)->DOEPTSIZ
|= USB_OTG_DOEPTSIZ_XFRSIZ
& (ep
->maxpacket
* pktcnt
);
780 if ((uint32_t)ep
->xfer_buff
!= 0U)
782 USBx_OUTEP(epnum
)->DOEPDMA
= (uint32_t)(ep
->xfer_buff
);
786 if (ep
->type
== EP_TYPE_ISOC
)
788 if ((USBx_DEVICE
->DSTS
& (1U << 8)) == 0U)
790 USBx_OUTEP(epnum
)->DOEPCTL
|= USB_OTG_DOEPCTL_SODDFRM
;
794 USBx_OUTEP(epnum
)->DOEPCTL
|= USB_OTG_DOEPCTL_SD0PID_SEVNFRM
;
798 USBx_OUTEP(epnum
)->DOEPCTL
|= (USB_OTG_DOEPCTL_CNAK
| USB_OTG_DOEPCTL_EPENA
);
805 * @brief USB_EP0StartXfer : setup and starts a transfer over the EP 0
806 * @param USBx Selected device
807 * @param ep pointer to endpoint structure
808 * @param dma USB dma enabled or disabled
809 * This parameter can be one of these values:
810 * 0 : DMA feature not used
811 * 1 : DMA feature used
814 HAL_StatusTypeDef
USB_EP0StartXfer(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
, uint8_t dma
)
816 uint32_t USBx_BASE
= (uint32_t)USBx
;
817 uint32_t epnum
= (uint32_t)ep
->num
;
822 /* Zero Length Packet? */
823 if (ep
->xfer_len
== 0U)
825 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_PKTCNT
);
826 USBx_INEP(epnum
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_PKTCNT
& (1U << 19));
827 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_XFRSIZ
);
831 /* Program the transfer size and packet count
832 * as follows: xfersize = N * maxpacket +
833 * short_packet pktcnt = N + (short_packet
836 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_XFRSIZ
);
837 USBx_INEP(epnum
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_PKTCNT
);
839 if (ep
->xfer_len
> ep
->maxpacket
)
841 ep
->xfer_len
= ep
->maxpacket
;
843 USBx_INEP(epnum
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_PKTCNT
& (1U << 19));
844 USBx_INEP(epnum
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_XFRSIZ
& ep
->xfer_len
);
849 if ((uint32_t)ep
->dma_addr
!= 0U)
851 USBx_INEP(epnum
)->DIEPDMA
= (uint32_t)(ep
->dma_addr
);
854 /* EP enable, IN data in FIFO */
855 USBx_INEP(epnum
)->DIEPCTL
|= (USB_OTG_DIEPCTL_CNAK
| USB_OTG_DIEPCTL_EPENA
);
859 /* EP enable, IN data in FIFO */
860 USBx_INEP(epnum
)->DIEPCTL
|= (USB_OTG_DIEPCTL_CNAK
| USB_OTG_DIEPCTL_EPENA
);
862 /* Enable the Tx FIFO Empty Interrupt for this EP */
863 if (ep
->xfer_len
> 0U)
865 USBx_DEVICE
->DIEPEMPMSK
|= 1UL << (ep
->num
& EP_ADDR_MSK
);
869 else /* OUT endpoint */
871 /* Program the transfer size and packet count as follows:
873 * xfersize = N * maxpacket
875 USBx_OUTEP(epnum
)->DOEPTSIZ
&= ~(USB_OTG_DOEPTSIZ_XFRSIZ
);
876 USBx_OUTEP(epnum
)->DOEPTSIZ
&= ~(USB_OTG_DOEPTSIZ_PKTCNT
);
878 if (ep
->xfer_len
> 0U)
880 ep
->xfer_len
= ep
->maxpacket
;
883 USBx_OUTEP(epnum
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_PKTCNT
& (1U << 19));
884 USBx_OUTEP(epnum
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_XFRSIZ
& (ep
->maxpacket
));
888 if ((uint32_t)ep
->xfer_buff
!= 0U)
890 USBx_OUTEP(epnum
)->DOEPDMA
= (uint32_t)(ep
->xfer_buff
);
895 USBx_OUTEP(epnum
)->DOEPCTL
|= (USB_OTG_DOEPCTL_CNAK
| USB_OTG_DOEPCTL_EPENA
);
902 * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
903 * with the EP/channel
904 * @param USBx Selected device
905 * @param src pointer to source buffer
906 * @param ch_ep_num endpoint or host channel number
907 * @param len Number of bytes to write
908 * @param dma USB dma enabled or disabled
909 * This parameter can be one of these values:
910 * 0 : DMA feature not used
911 * 1 : DMA feature used
914 HAL_StatusTypeDef
USB_WritePacket(USB_OTG_GlobalTypeDef
*USBx
, uint8_t *src
, uint8_t ch_ep_num
, uint16_t len
, uint8_t dma
)
916 uint32_t USBx_BASE
= (uint32_t)USBx
;
917 uint32_t *pSrc
= (uint32_t *)src
;
918 uint32_t count32b
, i
;
922 count32b
= ((uint32_t)len
+ 3U) / 4U;
923 for (i
= 0U; i
< count32b
; i
++)
925 USBx_DFIFO((uint32_t)ch_ep_num
) = __UNALIGNED_UINT32_READ(pSrc
);
934 * @brief USB_ReadPacket : read a packet from the RX FIFO
935 * @param USBx Selected device
936 * @param dest source pointer
937 * @param len Number of bytes to read
938 * @retval pointer to destination buffer
940 void *USB_ReadPacket(USB_OTG_GlobalTypeDef
*USBx
, uint8_t *dest
, uint16_t len
)
942 uint32_t USBx_BASE
= (uint32_t)USBx
;
943 uint32_t *pDest
= (uint32_t *)dest
;
945 uint32_t count32b
= ((uint32_t)len
+ 3U) / 4U;
947 for (i
= 0U; i
< count32b
; i
++)
949 __UNALIGNED_UINT32_WRITE(pDest
, USBx_DFIFO(0U));
953 return ((void *)pDest
);
957 * @brief USB_EPSetStall : set a stall condition over an EP
958 * @param USBx Selected device
959 * @param ep pointer to endpoint structure
962 HAL_StatusTypeDef
USB_EPSetStall(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
964 uint32_t USBx_BASE
= (uint32_t)USBx
;
965 uint32_t epnum
= (uint32_t)ep
->num
;
969 if (((USBx_INEP(epnum
)->DIEPCTL
& USB_OTG_DIEPCTL_EPENA
) == 0U) && (epnum
!= 0U))
971 USBx_INEP(epnum
)->DIEPCTL
&= ~(USB_OTG_DIEPCTL_EPDIS
);
973 USBx_INEP(epnum
)->DIEPCTL
|= USB_OTG_DIEPCTL_STALL
;
977 if (((USBx_OUTEP(epnum
)->DOEPCTL
& USB_OTG_DOEPCTL_EPENA
) == 0U) && (epnum
!= 0U))
979 USBx_OUTEP(epnum
)->DOEPCTL
&= ~(USB_OTG_DOEPCTL_EPDIS
);
981 USBx_OUTEP(epnum
)->DOEPCTL
|= USB_OTG_DOEPCTL_STALL
;
988 * @brief USB_EPClearStall : Clear a stall condition over an EP
989 * @param USBx Selected device
990 * @param ep pointer to endpoint structure
993 HAL_StatusTypeDef
USB_EPClearStall(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
995 uint32_t USBx_BASE
= (uint32_t)USBx
;
996 uint32_t epnum
= (uint32_t)ep
->num
;
1000 USBx_INEP(epnum
)->DIEPCTL
&= ~USB_OTG_DIEPCTL_STALL
;
1001 if ((ep
->type
== EP_TYPE_INTR
) || (ep
->type
== EP_TYPE_BULK
))
1003 USBx_INEP(epnum
)->DIEPCTL
|= USB_OTG_DIEPCTL_SD0PID_SEVNFRM
; /* DATA0 */
1008 USBx_OUTEP(epnum
)->DOEPCTL
&= ~USB_OTG_DOEPCTL_STALL
;
1009 if ((ep
->type
== EP_TYPE_INTR
) || (ep
->type
== EP_TYPE_BULK
))
1011 USBx_OUTEP(epnum
)->DOEPCTL
|= USB_OTG_DOEPCTL_SD0PID_SEVNFRM
; /* DATA0 */
1018 * @brief USB_StopDevice : Stop the usb device mode
1019 * @param USBx Selected device
1020 * @retval HAL status
1022 HAL_StatusTypeDef
USB_StopDevice(USB_OTG_GlobalTypeDef
*USBx
)
1024 HAL_StatusTypeDef ret
;
1025 uint32_t USBx_BASE
= (uint32_t)USBx
;
1028 /* Clear Pending interrupt */
1029 for (i
= 0U; i
< 15U; i
++)
1031 USBx_INEP(i
)->DIEPINT
= 0xFB7FU
;
1032 USBx_OUTEP(i
)->DOEPINT
= 0xFB7FU
;
1035 /* Clear interrupt masks */
1036 USBx_DEVICE
->DIEPMSK
= 0U;
1037 USBx_DEVICE
->DOEPMSK
= 0U;
1038 USBx_DEVICE
->DAINTMSK
= 0U;
1040 /* Flush the FIFO */
1041 ret
= USB_FlushRxFifo(USBx
);
1047 ret
= USB_FlushTxFifo(USBx
, 0x10U
);
1057 * @brief USB_SetDevAddress : Stop the usb device mode
1058 * @param USBx Selected device
1059 * @param address new device address to be assigned
1060 * This parameter can be a value from 0 to 255
1061 * @retval HAL status
1063 HAL_StatusTypeDef
USB_SetDevAddress(USB_OTG_GlobalTypeDef
*USBx
, uint8_t address
)
1065 uint32_t USBx_BASE
= (uint32_t)USBx
;
1067 USBx_DEVICE
->DCFG
&= ~(USB_OTG_DCFG_DAD
);
1068 USBx_DEVICE
->DCFG
|= ((uint32_t)address
<< 4) & USB_OTG_DCFG_DAD
;
1074 * @brief USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
1075 * @param USBx Selected device
1076 * @retval HAL status
1078 HAL_StatusTypeDef
USB_DevConnect(USB_OTG_GlobalTypeDef
*USBx
)
1080 uint32_t USBx_BASE
= (uint32_t)USBx
;
1082 USBx_DEVICE
->DCTL
&= ~USB_OTG_DCTL_SDIS
;
1089 * @brief USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
1090 * @param USBx Selected device
1091 * @retval HAL status
1093 HAL_StatusTypeDef
USB_DevDisconnect(USB_OTG_GlobalTypeDef
*USBx
)
1095 uint32_t USBx_BASE
= (uint32_t)USBx
;
1097 USBx_DEVICE
->DCTL
|= USB_OTG_DCTL_SDIS
;
1104 * @brief USB_ReadInterrupts: return the global USB interrupt status
1105 * @param USBx Selected device
1106 * @retval HAL status
1108 uint32_t USB_ReadInterrupts(USB_OTG_GlobalTypeDef
*USBx
)
1112 tmpreg
= USBx
->GINTSTS
;
1113 tmpreg
&= USBx
->GINTMSK
;
1119 * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
1120 * @param USBx Selected device
1121 * @retval HAL status
1123 uint32_t USB_ReadDevAllOutEpInterrupt(USB_OTG_GlobalTypeDef
*USBx
)
1125 uint32_t USBx_BASE
= (uint32_t)USBx
;
1128 tmpreg
= USBx_DEVICE
->DAINT
;
1129 tmpreg
&= USBx_DEVICE
->DAINTMSK
;
1131 return ((tmpreg
& 0xffff0000U
) >> 16);
1135 * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
1136 * @param USBx Selected device
1137 * @retval HAL status
1139 uint32_t USB_ReadDevAllInEpInterrupt(USB_OTG_GlobalTypeDef
*USBx
)
1141 uint32_t USBx_BASE
= (uint32_t)USBx
;
1144 tmpreg
= USBx_DEVICE
->DAINT
;
1145 tmpreg
&= USBx_DEVICE
->DAINTMSK
;
1147 return ((tmpreg
& 0xFFFFU
));
1151 * @brief Returns Device OUT EP Interrupt register
1152 * @param USBx Selected device
1153 * @param epnum endpoint number
1154 * This parameter can be a value from 0 to 15
1155 * @retval Device OUT EP Interrupt register
1157 uint32_t USB_ReadDevOutEPInterrupt(USB_OTG_GlobalTypeDef
*USBx
, uint8_t epnum
)
1159 uint32_t USBx_BASE
= (uint32_t)USBx
;
1162 tmpreg
= USBx_OUTEP((uint32_t)epnum
)->DOEPINT
;
1163 tmpreg
&= USBx_DEVICE
->DOEPMSK
;
1169 * @brief Returns Device IN EP Interrupt register
1170 * @param USBx Selected device
1171 * @param epnum endpoint number
1172 * This parameter can be a value from 0 to 15
1173 * @retval Device IN EP Interrupt register
1175 uint32_t USB_ReadDevInEPInterrupt(USB_OTG_GlobalTypeDef
*USBx
, uint8_t epnum
)
1177 uint32_t USBx_BASE
= (uint32_t)USBx
;
1178 uint32_t tmpreg
, msk
, emp
;
1180 msk
= USBx_DEVICE
->DIEPMSK
;
1181 emp
= USBx_DEVICE
->DIEPEMPMSK
;
1182 msk
|= ((emp
>> (epnum
& EP_ADDR_MSK
)) & 0x1U
) << 7;
1183 tmpreg
= USBx_INEP((uint32_t)epnum
)->DIEPINT
& msk
;
1189 * @brief USB_ClearInterrupts: clear a USB interrupt
1190 * @param USBx Selected device
1191 * @param interrupt interrupt flag
1194 void USB_ClearInterrupts(USB_OTG_GlobalTypeDef
*USBx
, uint32_t interrupt
)
1196 USBx
->GINTSTS
|= interrupt
;
1200 * @brief Returns USB core mode
1201 * @param USBx Selected device
1202 * @retval return core mode : Host or Device
1203 * This parameter can be one of these values:
1207 uint32_t USB_GetMode(USB_OTG_GlobalTypeDef
*USBx
)
1209 return ((USBx
->GINTSTS
) & 0x1U
);
1213 * @brief Activate EP0 for Setup transactions
1214 * @param USBx Selected device
1215 * @retval HAL status
1217 HAL_StatusTypeDef
USB_ActivateSetup(USB_OTG_GlobalTypeDef
*USBx
)
1219 uint32_t USBx_BASE
= (uint32_t)USBx
;
1221 /* Set the MPS of the IN EP0 to 64 bytes */
1222 USBx_INEP(0U)->DIEPCTL
&= ~USB_OTG_DIEPCTL_MPSIZ
;
1224 USBx_DEVICE
->DCTL
|= USB_OTG_DCTL_CGINAK
;
1230 * @brief Prepare the EP0 to start the first control setup
1231 * @param USBx Selected device
1232 * @param dma USB dma enabled or disabled
1233 * This parameter can be one of these values:
1234 * 0 : DMA feature not used
1235 * 1 : DMA feature used
1236 * @param psetup pointer to setup packet
1237 * @retval HAL status
1239 HAL_StatusTypeDef
USB_EP0_OutStart(USB_OTG_GlobalTypeDef
*USBx
, uint8_t dma
, uint8_t *psetup
)
1241 uint32_t USBx_BASE
= (uint32_t)USBx
;
1242 uint32_t gSNPSiD
= *(__IO
uint32_t *)(&USBx
->CID
+ 0x1U
);
1244 if (gSNPSiD
> USB_OTG_CORE_ID_300A
)
1246 if ((USBx_OUTEP(0U)->DOEPCTL
& USB_OTG_DOEPCTL_EPENA
) == USB_OTG_DOEPCTL_EPENA
)
1252 USBx_OUTEP(0U)->DOEPTSIZ
= 0U;
1253 USBx_OUTEP(0U)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_PKTCNT
& (1U << 19));
1254 USBx_OUTEP(0U)->DOEPTSIZ
|= (3U * 8U);
1255 USBx_OUTEP(0U)->DOEPTSIZ
|= USB_OTG_DOEPTSIZ_STUPCNT
;
1259 USBx_OUTEP(0U)->DOEPDMA
= (uint32_t)psetup
;
1261 USBx_OUTEP(0U)->DOEPCTL
|= USB_OTG_DOEPCTL_EPENA
| USB_OTG_DOEPCTL_USBAEP
;
1268 * @brief Reset the USB Core (needed after USB clock settings change)
1269 * @param USBx Selected device
1270 * @retval HAL status
1272 static HAL_StatusTypeDef
USB_CoreReset(USB_OTG_GlobalTypeDef
*USBx
)
1274 uint32_t count
= 0U;
1276 /* Wait for AHB master IDLE state. */
1279 if (++count
> 200000U)
1284 while ((USBx
->GRSTCTL
& USB_OTG_GRSTCTL_AHBIDL
) == 0U);
1286 /* Core Soft Reset */
1288 USBx
->GRSTCTL
|= USB_OTG_GRSTCTL_CSRST
;
1292 if (++count
> 200000U)
1297 while ((USBx
->GRSTCTL
& USB_OTG_GRSTCTL_CSRST
) == USB_OTG_GRSTCTL_CSRST
);
1303 * @brief USB_HostInit : Initializes the USB OTG controller registers
1305 * @param USBx Selected device
1306 * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
1307 * the configuration information for the specified USBx peripheral.
1308 * @retval HAL status
1310 HAL_StatusTypeDef
USB_HostInit(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_CfgTypeDef cfg
)
1312 uint32_t USBx_BASE
= (uint32_t)USBx
;
1315 /* Restart the Phy Clock */
1318 /* Disable VBUS sensing */
1319 USBx
->GCCFG
&= ~(USB_OTG_GCCFG_VBDEN
);
1321 /* Disable Battery chargin detector */
1322 USBx
->GCCFG
&= ~(USB_OTG_GCCFG_BCDEN
);
1325 if ((USBx
->CID
& (0x1U
<< 8)) != 0U)
1327 if (cfg
.speed
== USB_OTG_SPEED_FULL
)
1329 /* Force Device Enumeration to FS/LS mode only */
1330 USBx_HOST
->HCFG
|= USB_OTG_HCFG_FSLSS
;
1334 /* Set default Max speed support */
1335 USBx_HOST
->HCFG
&= ~(USB_OTG_HCFG_FSLSS
);
1340 /* Set default Max speed support */
1341 USBx_HOST
->HCFG
&= ~(USB_OTG_HCFG_FSLSS
);
1344 /* Make sure the FIFOs are flushed. */
1345 (void)USB_FlushTxFifo(USBx
, 0x10U
); /* all Tx FIFOs */
1346 (void)USB_FlushRxFifo(USBx
);
1348 /* Clear all pending HC Interrupts */
1349 for (i
= 0U; i
< cfg
.Host_channels
; i
++)
1351 USBx_HC(i
)->HCINT
= 0xFFFFFFFFU
;
1352 USBx_HC(i
)->HCINTMSK
= 0U;
1355 /* Enable VBUS driving */
1356 (void)USB_DriveVbus(USBx
, 1U);
1360 /* Disable all interrupts. */
1363 /* Clear any pending interrupts */
1364 USBx
->GINTSTS
= 0xFFFFFFFFU
;
1366 if ((USBx
->CID
& (0x1U
<< 8)) != 0U)
1368 /* set Rx FIFO size */
1369 USBx
->GRXFSIZ
= 0x200U
;
1370 USBx
->DIEPTXF0_HNPTXFSIZ
= (uint32_t)(((0x100U
<< 16) & USB_OTG_NPTXFD
) | 0x200U
);
1371 USBx
->HPTXFSIZ
= (uint32_t)(((0xE0U
<< 16) & USB_OTG_HPTXFSIZ_PTXFD
) | 0x300U
);
1375 /* set Rx FIFO size */
1376 USBx
->GRXFSIZ
= 0x80U
;
1377 USBx
->DIEPTXF0_HNPTXFSIZ
= (uint32_t)(((0x60U
<< 16) & USB_OTG_NPTXFD
) | 0x80U
);
1378 USBx
->HPTXFSIZ
= (uint32_t)(((0x40U
<< 16)& USB_OTG_HPTXFSIZ_PTXFD
) | 0xE0U
);
1381 /* Enable the common interrupts */
1382 if (cfg
.dma_enable
== 0U)
1384 USBx
->GINTMSK
|= USB_OTG_GINTMSK_RXFLVLM
;
1387 /* Enable interrupts matching to the Host mode ONLY */
1388 USBx
->GINTMSK
|= (USB_OTG_GINTMSK_PRTIM
| USB_OTG_GINTMSK_HCIM
| \
1389 USB_OTG_GINTMSK_SOFM
| USB_OTG_GINTSTS_DISCINT
| \
1390 USB_OTG_GINTMSK_PXFRM_IISOOXFRM
| USB_OTG_GINTMSK_WUIM
);
1396 * @brief USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
1397 * HCFG register on the PHY type and set the right frame interval
1398 * @param USBx Selected device
1399 * @param freq clock frequency
1400 * This parameter can be one of these values:
1401 * HCFG_48_MHZ : Full Speed 48 MHz Clock
1402 * HCFG_6_MHZ : Low Speed 6 MHz Clock
1403 * @retval HAL status
1405 HAL_StatusTypeDef
USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef
*USBx
, uint8_t freq
)
1407 uint32_t USBx_BASE
= (uint32_t)USBx
;
1409 USBx_HOST
->HCFG
&= ~(USB_OTG_HCFG_FSLSPCS
);
1410 USBx_HOST
->HCFG
|= (uint32_t)freq
& USB_OTG_HCFG_FSLSPCS
;
1412 if (freq
== HCFG_48_MHZ
)
1414 USBx_HOST
->HFIR
= 48000U;
1416 else if (freq
== HCFG_6_MHZ
)
1418 USBx_HOST
->HFIR
= 6000U;
1429 * @brief USB_OTG_ResetPort : Reset Host Port
1430 * @param USBx Selected device
1431 * @retval HAL status
1432 * @note (1)The application must wait at least 10 ms
1433 * before clearing the reset bit.
1435 HAL_StatusTypeDef
USB_ResetPort(USB_OTG_GlobalTypeDef
*USBx
)
1437 uint32_t USBx_BASE
= (uint32_t)USBx
;
1439 __IO
uint32_t hprt0
= 0U;
1443 hprt0
&= ~(USB_OTG_HPRT_PENA
| USB_OTG_HPRT_PCDET
|
1444 USB_OTG_HPRT_PENCHNG
| USB_OTG_HPRT_POCCHNG
);
1446 USBx_HPRT0
= (USB_OTG_HPRT_PRST
| hprt0
);
1447 HAL_Delay(100U); /* See Note #1 */
1448 USBx_HPRT0
= ((~USB_OTG_HPRT_PRST
) & hprt0
);
1455 * @brief USB_DriveVbus : activate or de-activate vbus
1456 * @param state VBUS state
1457 * This parameter can be one of these values:
1460 * @retval HAL status
1462 HAL_StatusTypeDef
USB_DriveVbus(USB_OTG_GlobalTypeDef
*USBx
, uint8_t state
)
1464 uint32_t USBx_BASE
= (uint32_t)USBx
;
1465 __IO
uint32_t hprt0
= 0U;
1469 hprt0
&= ~(USB_OTG_HPRT_PENA
| USB_OTG_HPRT_PCDET
|
1470 USB_OTG_HPRT_PENCHNG
| USB_OTG_HPRT_POCCHNG
);
1472 if (((hprt0
& USB_OTG_HPRT_PPWR
) == 0U) && (state
== 1U))
1474 USBx_HPRT0
= (USB_OTG_HPRT_PPWR
| hprt0
);
1476 if (((hprt0
& USB_OTG_HPRT_PPWR
) == USB_OTG_HPRT_PPWR
) && (state
== 0U))
1478 USBx_HPRT0
= ((~USB_OTG_HPRT_PPWR
) & hprt0
);
1484 * @brief Return Host Core speed
1485 * @param USBx Selected device
1486 * @retval speed : Host speed
1487 * This parameter can be one of these values:
1488 * @arg HCD_SPEED_HIGH: High speed mode
1489 * @arg HCD_SPEED_FULL: Full speed mode
1490 * @arg HCD_SPEED_LOW: Low speed mode
1492 uint32_t USB_GetHostSpeed(USB_OTG_GlobalTypeDef
*USBx
)
1494 uint32_t USBx_BASE
= (uint32_t)USBx
;
1495 __IO
uint32_t hprt0
= 0U;
1498 return ((hprt0
& USB_OTG_HPRT_PSPD
) >> 17);
1502 * @brief Return Host Current Frame number
1503 * @param USBx Selected device
1504 * @retval current frame number
1506 uint32_t USB_GetCurrentFrame(USB_OTG_GlobalTypeDef
*USBx
)
1508 uint32_t USBx_BASE
= (uint32_t)USBx
;
1510 return (USBx_HOST
->HFNUM
& USB_OTG_HFNUM_FRNUM
);
1514 * @brief Initialize a host channel
1515 * @param USBx Selected device
1516 * @param ch_num Channel number
1517 * This parameter can be a value from 1 to 15
1518 * @param epnum Endpoint number
1519 * This parameter can be a value from 1 to 15
1520 * @param dev_address Current device address
1521 * This parameter can be a value from 0 to 255
1522 * @param speed Current device speed
1523 * This parameter can be one of these values:
1524 * @arg USB_OTG_SPEED_HIGH: High speed mode
1525 * @arg USB_OTG_SPEED_FULL: Full speed mode
1526 * @arg USB_OTG_SPEED_LOW: Low speed mode
1527 * @param ep_type Endpoint Type
1528 * This parameter can be one of these values:
1529 * @arg EP_TYPE_CTRL: Control type
1530 * @arg EP_TYPE_ISOC: Isochronous type
1531 * @arg EP_TYPE_BULK: Bulk type
1532 * @arg EP_TYPE_INTR: Interrupt type
1533 * @param mps Max Packet Size
1534 * This parameter can be a value from 0 to32K
1537 HAL_StatusTypeDef
USB_HC_Init(USB_OTG_GlobalTypeDef
*USBx
,
1540 uint8_t dev_address
,
1545 HAL_StatusTypeDef ret
= HAL_OK
;
1546 uint32_t USBx_BASE
= (uint32_t)USBx
;
1547 uint32_t HCcharEpDir
, HCcharLowSpeed
;
1549 /* Clear old interrupt conditions for this host channel. */
1550 USBx_HC((uint32_t)ch_num
)->HCINT
= 0xFFFFFFFFU
;
1552 /* Enable channel interrupts required for this transfer. */
1557 USBx_HC((uint32_t)ch_num
)->HCINTMSK
= USB_OTG_HCINTMSK_XFRCM
|
1558 USB_OTG_HCINTMSK_STALLM
|
1559 USB_OTG_HCINTMSK_TXERRM
|
1560 USB_OTG_HCINTMSK_DTERRM
|
1561 USB_OTG_HCINTMSK_AHBERR
|
1562 USB_OTG_HCINTMSK_NAKM
;
1564 if ((epnum
& 0x80U
) == 0x80U
)
1566 USBx_HC((uint32_t)ch_num
)->HCINTMSK
|= USB_OTG_HCINTMSK_BBERRM
;
1570 if ((USBx
->CID
& (0x1U
<< 8)) != 0U)
1572 USBx_HC((uint32_t)ch_num
)->HCINTMSK
|= (USB_OTG_HCINTMSK_NYET
| USB_OTG_HCINTMSK_ACKM
);
1578 USBx_HC((uint32_t)ch_num
)->HCINTMSK
= USB_OTG_HCINTMSK_XFRCM
|
1579 USB_OTG_HCINTMSK_STALLM
|
1580 USB_OTG_HCINTMSK_TXERRM
|
1581 USB_OTG_HCINTMSK_DTERRM
|
1582 USB_OTG_HCINTMSK_NAKM
|
1583 USB_OTG_HCINTMSK_AHBERR
|
1584 USB_OTG_HCINTMSK_FRMORM
;
1586 if ((epnum
& 0x80U
) == 0x80U
)
1588 USBx_HC((uint32_t)ch_num
)->HCINTMSK
|= USB_OTG_HCINTMSK_BBERRM
;
1594 USBx_HC((uint32_t)ch_num
)->HCINTMSK
= USB_OTG_HCINTMSK_XFRCM
|
1595 USB_OTG_HCINTMSK_ACKM
|
1596 USB_OTG_HCINTMSK_AHBERR
|
1597 USB_OTG_HCINTMSK_FRMORM
;
1599 if ((epnum
& 0x80U
) == 0x80U
)
1601 USBx_HC((uint32_t)ch_num
)->HCINTMSK
|= (USB_OTG_HCINTMSK_TXERRM
| USB_OTG_HCINTMSK_BBERRM
);
1610 /* Enable the top level host channel interrupt. */
1611 USBx_HOST
->HAINTMSK
|= 1UL << (ch_num
& 0xFU
);
1613 /* Make sure host channel interrupts are enabled. */
1614 USBx
->GINTMSK
|= USB_OTG_GINTMSK_HCIM
;
1616 /* Program the HCCHAR register */
1617 if ((epnum
& 0x80U
) == 0x80U
)
1619 HCcharEpDir
= (0x1U
<< 15) & USB_OTG_HCCHAR_EPDIR
;
1626 if (speed
== HPRT0_PRTSPD_LOW_SPEED
)
1628 HCcharLowSpeed
= (0x1U
<< 17) & USB_OTG_HCCHAR_LSDEV
;
1632 HCcharLowSpeed
= 0U;
1635 USBx_HC((uint32_t)ch_num
)->HCCHAR
= (((uint32_t)dev_address
<< 22) & USB_OTG_HCCHAR_DAD
) |
1636 ((((uint32_t)epnum
& 0x7FU
) << 11) & USB_OTG_HCCHAR_EPNUM
) |
1637 (((uint32_t)ep_type
<< 18) & USB_OTG_HCCHAR_EPTYP
) |
1638 ((uint32_t)mps
& USB_OTG_HCCHAR_MPSIZ
) | HCcharEpDir
| HCcharLowSpeed
;
1640 if (ep_type
== EP_TYPE_INTR
)
1642 USBx_HC((uint32_t)ch_num
)->HCCHAR
|= USB_OTG_HCCHAR_ODDFRM
;
1649 * @brief Start a transfer over a host channel
1650 * @param USBx Selected device
1651 * @param hc pointer to host channel structure
1652 * @param dma USB dma enabled or disabled
1653 * This parameter can be one of these values:
1654 * 0 : DMA feature not used
1655 * 1 : DMA feature used
1658 HAL_StatusTypeDef
USB_HC_StartXfer(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_HCTypeDef
*hc
, uint8_t dma
)
1660 uint32_t USBx_BASE
= (uint32_t)USBx
;
1661 uint32_t ch_num
= (uint32_t)hc
->ch_num
;
1662 static __IO
uint32_t tmpreg
= 0U;
1663 uint8_t is_oddframe
;
1665 uint16_t num_packets
;
1666 uint16_t max_hc_pkt_count
= 256U;
1668 if (((USBx
->CID
& (0x1U
<< 8)) != 0U) && (hc
->speed
== USBH_HS_SPEED
))
1670 if ((dma
== 0U) && (hc
->do_ping
== 1U))
1672 (void)USB_DoPing(USBx
, hc
->ch_num
);
1677 USBx_HC(ch_num
)->HCINTMSK
&= ~(USB_OTG_HCINTMSK_NYET
| USB_OTG_HCINTMSK_ACKM
);
1686 /* Compute the expected number of packets associated to the transfer */
1687 if (hc
->xfer_len
> 0U)
1689 num_packets
= (uint16_t)((hc
->xfer_len
+ hc
->max_packet
- 1U) / hc
->max_packet
);
1691 if (num_packets
> max_hc_pkt_count
)
1693 num_packets
= max_hc_pkt_count
;
1694 hc
->xfer_len
= (uint32_t)num_packets
* hc
->max_packet
;
1701 if (hc
->ep_is_in
!= 0U)
1703 hc
->xfer_len
= (uint32_t)num_packets
* hc
->max_packet
;
1706 /* Initialize the HCTSIZn register */
1707 USBx_HC(ch_num
)->HCTSIZ
= (hc
->xfer_len
& USB_OTG_HCTSIZ_XFRSIZ
) |
1708 (((uint32_t)num_packets
<< 19) & USB_OTG_HCTSIZ_PKTCNT
) |
1709 (((uint32_t)hc
->data_pid
<< 29) & USB_OTG_HCTSIZ_DPID
);
1713 /* xfer_buff MUST be 32-bits aligned */
1714 USBx_HC(ch_num
)->HCDMA
= (uint32_t)hc
->xfer_buff
;
1717 is_oddframe
= (((uint32_t)USBx_HOST
->HFNUM
& 0x01U
) != 0U) ? 0U : 1U;
1718 USBx_HC(ch_num
)->HCCHAR
&= ~USB_OTG_HCCHAR_ODDFRM
;
1719 USBx_HC(ch_num
)->HCCHAR
|= (uint32_t)is_oddframe
<< 29;
1721 /* Set host channel enable */
1722 tmpreg
= USBx_HC(ch_num
)->HCCHAR
;
1723 tmpreg
&= ~USB_OTG_HCCHAR_CHDIS
;
1725 /* make sure to set the correct ep direction */
1726 if (hc
->ep_is_in
!= 0U)
1728 tmpreg
|= USB_OTG_HCCHAR_EPDIR
;
1732 tmpreg
&= ~USB_OTG_HCCHAR_EPDIR
;
1734 tmpreg
|= USB_OTG_HCCHAR_CHENA
;
1735 USBx_HC(ch_num
)->HCCHAR
= tmpreg
;
1737 if (dma
== 0U) /* Slave mode */
1739 if ((hc
->ep_is_in
== 0U) && (hc
->xfer_len
> 0U))
1741 switch (hc
->ep_type
)
1743 /* Non periodic transfer */
1747 len_words
= (uint16_t)((hc
->xfer_len
+ 3U) / 4U);
1749 /* check if there is enough space in FIFO space */
1750 if (len_words
> (USBx
->HNPTXSTS
& 0xFFFFU
))
1752 /* need to process data in nptxfempty interrupt */
1753 USBx
->GINTMSK
|= USB_OTG_GINTMSK_NPTXFEM
;
1757 /* Periodic transfer */
1760 len_words
= (uint16_t)((hc
->xfer_len
+ 3U) / 4U);
1761 /* check if there is enough space in FIFO space */
1762 if (len_words
> (USBx_HOST
->HPTXSTS
& 0xFFFFU
)) /* split the transfer */
1764 /* need to process data in ptxfempty interrupt */
1765 USBx
->GINTMSK
|= USB_OTG_GINTMSK_PTXFEM
;
1773 /* Write packet into the Tx FIFO. */
1774 (void)USB_WritePacket(USBx
, hc
->xfer_buff
, hc
->ch_num
, (uint16_t)hc
->xfer_len
, 0);
1782 * @brief Read all host channel interrupts status
1783 * @param USBx Selected device
1786 uint32_t USB_HC_ReadInterrupt(USB_OTG_GlobalTypeDef
*USBx
)
1788 uint32_t USBx_BASE
= (uint32_t)USBx
;
1790 return ((USBx_HOST
->HAINT
) & 0xFFFFU
);
1794 * @brief Halt a host channel
1795 * @param USBx Selected device
1796 * @param hc_num Host Channel number
1797 * This parameter can be a value from 1 to 15
1800 HAL_StatusTypeDef
USB_HC_Halt(USB_OTG_GlobalTypeDef
*USBx
, uint8_t hc_num
)
1802 uint32_t USBx_BASE
= (uint32_t)USBx
;
1803 uint32_t hcnum
= (uint32_t)hc_num
;
1804 uint32_t count
= 0U;
1805 uint32_t HcEpType
= (USBx_HC(hcnum
)->HCCHAR
& USB_OTG_HCCHAR_EPTYP
) >> 18;
1807 /* Check for space in the request queue to issue the halt. */
1808 if ((HcEpType
== HCCHAR_CTRL
) || (HcEpType
== HCCHAR_BULK
))
1810 USBx_HC(hcnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHDIS
;
1812 if ((USBx
->HNPTXSTS
& (0xFFU
<< 16)) == 0U)
1814 USBx_HC(hcnum
)->HCCHAR
&= ~USB_OTG_HCCHAR_CHENA
;
1815 USBx_HC(hcnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1816 USBx_HC(hcnum
)->HCCHAR
&= ~USB_OTG_HCCHAR_EPDIR
;
1819 if (++count
> 1000U)
1824 while ((USBx_HC(hcnum
)->HCCHAR
& USB_OTG_HCCHAR_CHENA
) == USB_OTG_HCCHAR_CHENA
);
1828 USBx_HC(hcnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1833 USBx_HC(hcnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHDIS
;
1835 if ((USBx_HOST
->HPTXSTS
& (0xFFU
<< 16)) == 0U)
1837 USBx_HC(hcnum
)->HCCHAR
&= ~USB_OTG_HCCHAR_CHENA
;
1838 USBx_HC(hcnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1839 USBx_HC(hcnum
)->HCCHAR
&= ~USB_OTG_HCCHAR_EPDIR
;
1842 if (++count
> 1000U)
1847 while ((USBx_HC(hcnum
)->HCCHAR
& USB_OTG_HCCHAR_CHENA
) == USB_OTG_HCCHAR_CHENA
);
1851 USBx_HC(hcnum
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1859 * @brief Initiate Do Ping protocol
1860 * @param USBx Selected device
1861 * @param hc_num Host Channel number
1862 * This parameter can be a value from 1 to 15
1865 HAL_StatusTypeDef
USB_DoPing(USB_OTG_GlobalTypeDef
*USBx
, uint8_t ch_num
)
1867 uint32_t USBx_BASE
= (uint32_t)USBx
;
1868 uint32_t chnum
= (uint32_t)ch_num
;
1869 uint32_t num_packets
= 1U;
1872 USBx_HC(chnum
)->HCTSIZ
= ((num_packets
<< 19) & USB_OTG_HCTSIZ_PKTCNT
) |
1873 USB_OTG_HCTSIZ_DOPING
;
1875 /* Set host channel enable */
1876 tmpreg
= USBx_HC(chnum
)->HCCHAR
;
1877 tmpreg
&= ~USB_OTG_HCCHAR_CHDIS
;
1878 tmpreg
|= USB_OTG_HCCHAR_CHENA
;
1879 USBx_HC(chnum
)->HCCHAR
= tmpreg
;
1885 * @brief Stop Host Core
1886 * @param USBx Selected device
1889 HAL_StatusTypeDef
USB_StopHost(USB_OTG_GlobalTypeDef
*USBx
)
1891 uint32_t USBx_BASE
= (uint32_t)USBx
;
1892 uint32_t count
= 0U;
1896 (void)USB_DisableGlobalInt(USBx
);
1899 (void)USB_FlushTxFifo(USBx
, 0x10U
);
1900 (void)USB_FlushRxFifo(USBx
);
1902 /* Flush out any leftover queued requests. */
1903 for (i
= 0U; i
<= 15U; i
++)
1905 value
= USBx_HC(i
)->HCCHAR
;
1906 value
|= USB_OTG_HCCHAR_CHDIS
;
1907 value
&= ~USB_OTG_HCCHAR_CHENA
;
1908 value
&= ~USB_OTG_HCCHAR_EPDIR
;
1909 USBx_HC(i
)->HCCHAR
= value
;
1912 /* Halt all channels to put them into a known state. */
1913 for (i
= 0U; i
<= 15U; i
++)
1915 value
= USBx_HC(i
)->HCCHAR
;
1916 value
|= USB_OTG_HCCHAR_CHDIS
;
1917 value
|= USB_OTG_HCCHAR_CHENA
;
1918 value
&= ~USB_OTG_HCCHAR_EPDIR
;
1919 USBx_HC(i
)->HCCHAR
= value
;
1923 if (++count
> 1000U)
1928 while ((USBx_HC(i
)->HCCHAR
& USB_OTG_HCCHAR_CHENA
) == USB_OTG_HCCHAR_CHENA
);
1931 /* Clear any pending Host interrupts */
1932 USBx_HOST
->HAINT
= 0xFFFFFFFFU
;
1933 USBx
->GINTSTS
= 0xFFFFFFFFU
;
1935 (void)USB_EnableGlobalInt(USBx
);
1941 * @brief USB_ActivateRemoteWakeup active remote wakeup signalling
1942 * @param USBx Selected device
1943 * @retval HAL status
1945 HAL_StatusTypeDef
USB_ActivateRemoteWakeup(USB_OTG_GlobalTypeDef
*USBx
)
1947 uint32_t USBx_BASE
= (uint32_t)USBx
;
1949 if ((USBx_DEVICE
->DSTS
& USB_OTG_DSTS_SUSPSTS
) == USB_OTG_DSTS_SUSPSTS
)
1951 /* active Remote wakeup signalling */
1952 USBx_DEVICE
->DCTL
|= USB_OTG_DCTL_RWUSIG
;
1959 * @brief USB_DeActivateRemoteWakeup de-active remote wakeup signalling
1960 * @param USBx Selected device
1961 * @retval HAL status
1963 HAL_StatusTypeDef
USB_DeActivateRemoteWakeup(USB_OTG_GlobalTypeDef
*USBx
)
1965 uint32_t USBx_BASE
= (uint32_t)USBx
;
1967 /* active Remote wakeup signalling */
1968 USBx_DEVICE
->DCTL
&= ~(USB_OTG_DCTL_RWUSIG
);
1972 #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
1982 #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
1983 #endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
1989 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/