2 ******************************************************************************
3 * @file stm32f1xx_ll_usb.c
4 * @author MCD Application Team
7 * @brief USB Low Layer HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the USB Peripheral Controller:
11 * + Initialization/de-initialization functions
12 * + I/O operation functions
13 * + Peripheral Control functions
14 * + Peripheral State functions
17 ==============================================================================
18 ##### How to use this driver #####
19 ==============================================================================
21 (#) Fill parameters of Init structure in USB_OTG_CfgTypeDef structure.
23 (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
25 (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
28 ******************************************************************************
31 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
33 * Redistribution and use in source and binary forms, with or without modification,
34 * are permitted provided that the following conditions are met:
35 * 1. Redistributions of source code must retain the above copyright notice,
36 * this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright notice,
38 * this list of conditions and the following disclaimer in the documentation
39 * and/or other materials provided with the distribution.
40 * 3. Neither the name of STMicroelectronics nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 ******************************************************************************
58 /* Includes ------------------------------------------------------------------*/
59 #include "stm32f1xx_hal.h"
61 /** @addtogroup STM32F1xx_HAL_Driver
65 /** @defgroup USB_LL USB Low Layer
66 * @brief Low layer module for USB_FS and USB_OTG_FS drivers
70 #if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
72 #if defined(STM32F102x6) || defined(STM32F102xB) || \
73 defined(STM32F103x6) || defined(STM32F103xB) || \
74 defined(STM32F103xE) || defined(STM32F103xG) || \
75 defined(STM32F105xC) || defined(STM32F107xC)
77 /* Private types -------------------------------------------------------------*/
78 /* Private variables ---------------------------------------------------------*/
79 /* Private constants ---------------------------------------------------------*/
80 /* Private macros ------------------------------------------------------------*/
81 /* Private functions ---------------------------------------------------------*/
82 #if defined (USB_OTG_FS)
83 /** @defgroup USB_LL_Private_Functions USB Low Layer Private Functions
86 static HAL_StatusTypeDef
USB_CoreReset(USB_OTG_GlobalTypeDef
*USBx
);
90 #endif /* USB_OTG_FS */
92 /* Exported functions --------------------------------------------------------*/
93 /** @defgroup USB_LL_Exported_Functions USB Low Layer Exported Functions
97 /** @defgroup USB_LL_Exported_Functions_Group1 Peripheral Control functions
98 * @brief management functions
101 ===============================================================================
102 ##### Peripheral Control functions #####
103 ===============================================================================
105 This subsection provides a set of functions allowing to control the PCD data
112 /*==============================================================================
113 USB OTG FS peripheral available on STM32F105xx and STM32F107xx devices
114 ==============================================================================*/
115 #if defined (USB_OTG_FS)
118 * @brief Initializes the USB Core
119 * @param USBx: USB Instance
120 * @param cfg : pointer to a USB_OTG_CfgTypeDef structure that contains
121 * the configuration information for the specified USBx peripheral.
124 HAL_StatusTypeDef
USB_CoreInit(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_CfgTypeDef cfg
)
126 /* Prevent unused argument(s) compilation warning */
129 /* Select FS Embedded PHY */
130 USBx
->GUSBCFG
|= USB_OTG_GUSBCFG_PHYSEL
;
132 /* Reset after a PHY select and set Host mode */
135 /* Deactivate the power down*/
136 USBx
->GCCFG
= USB_OTG_GCCFG_PWRDWN
;
142 * @brief USB_EnableGlobalInt
143 * Enables the controller's Global Int in the AHB Config reg
144 * @param USBx : Selected device
147 HAL_StatusTypeDef
USB_EnableGlobalInt(USB_OTG_GlobalTypeDef
*USBx
)
149 USBx
->GAHBCFG
|= USB_OTG_GAHBCFG_GINT
;
154 * @brief USB_DisableGlobalInt
155 * Disable the controller's Global Int in the AHB Config reg
156 * @param USBx : Selected device
159 HAL_StatusTypeDef
USB_DisableGlobalInt(USB_OTG_GlobalTypeDef
*USBx
)
161 USBx
->GAHBCFG
&= ~USB_OTG_GAHBCFG_GINT
;
166 * @brief USB_SetCurrentMode : Set functional mode
167 * @param USBx : Selected device
168 * @param mode : current core mode
169 * This parameter can be one of the these values:
170 * @arg USB_DEVICE_MODE: Peripheral mode mode
171 * @arg USB_HOST_MODE: Host mode
172 * @arg USB_DRD_MODE: Dual Role Device mode
175 HAL_StatusTypeDef
USB_SetCurrentMode(USB_OTG_GlobalTypeDef
*USBx
, USB_ModeTypeDef mode
)
177 USBx
->GUSBCFG
&= ~(USB_OTG_GUSBCFG_FHMOD
| USB_OTG_GUSBCFG_FDMOD
);
179 if ( mode
== USB_HOST_MODE
)
181 USBx
->GUSBCFG
|= USB_OTG_GUSBCFG_FHMOD
;
183 else if (mode
== USB_DEVICE_MODE
)
185 USBx
->GUSBCFG
|= USB_OTG_GUSBCFG_FDMOD
;
193 * @brief USB_DevInit : Initializes the USB_OTG controller registers
195 * @param USBx : Selected device
196 * @param cfg : pointer to a USB_OTG_CfgTypeDef structure that contains
197 * the configuration information for the specified USBx peripheral.
200 HAL_StatusTypeDef
USB_DevInit (USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_CfgTypeDef cfg
)
204 for (index
= 0; index
< 15 ; index
++)
206 USBx
->DIEPTXF
[index
] = 0;
209 /*Activate VBUS Sensing B */
210 USBx
->GCCFG
|= USB_OTG_GCCFG_VBUSBSEN
;
212 /* Restart the Phy Clock */
215 /* Device mode configuration */
216 USBx_DEVICE
->DCFG
|= DCFG_FRAME_INTERVAL_80
;
218 /* Set Full speed phy */
219 USB_SetDevSpeed (USBx
, USB_OTG_SPEED_FULL
);
221 /* Flush the FIFOs */
222 USB_FlushTxFifo(USBx
, 0x10); /* all Tx FIFOs */
223 USB_FlushRxFifo(USBx
);
225 /* Clear all pending Device Interrupts */
226 USBx_DEVICE
->DIEPMSK
= 0;
227 USBx_DEVICE
->DOEPMSK
= 0;
228 USBx_DEVICE
->DAINT
= 0xFFFFFFFF;
229 USBx_DEVICE
->DAINTMSK
= 0;
231 for (index
= 0; index
< cfg
.dev_endpoints
; index
++)
233 if ((USBx_INEP(index
)->DIEPCTL
& USB_OTG_DIEPCTL_EPENA
) == USB_OTG_DIEPCTL_EPENA
)
235 USBx_INEP(index
)->DIEPCTL
= (USB_OTG_DIEPCTL_EPDIS
| USB_OTG_DIEPCTL_SNAK
);
239 USBx_INEP(index
)->DIEPCTL
= 0;
242 USBx_INEP(index
)->DIEPTSIZ
= 0;
243 USBx_INEP(index
)->DIEPINT
= 0xFF;
246 for (index
= 0; index
< cfg
.dev_endpoints
; index
++)
248 if ((USBx_OUTEP(index
)->DOEPCTL
& USB_OTG_DOEPCTL_EPENA
) == USB_OTG_DOEPCTL_EPENA
)
250 USBx_OUTEP(index
)->DOEPCTL
= (USB_OTG_DOEPCTL_EPDIS
| USB_OTG_DOEPCTL_SNAK
);
254 USBx_OUTEP(index
)->DOEPCTL
= 0;
257 USBx_OUTEP(index
)->DOEPTSIZ
= 0;
258 USBx_OUTEP(index
)->DOEPINT
= 0xFF;
261 USBx_DEVICE
->DIEPMSK
&= ~(USB_OTG_DIEPMSK_TXFURM
);
263 /* Disable all interrupts. */
266 /* Clear any pending interrupts */
267 USBx
->GINTSTS
= 0xBFFFFFFF;
269 /* Enable the common interrupts */
270 USBx
->GINTMSK
|= USB_OTG_GINTMSK_RXFLVLM
;
272 /* Enable interrupts matching to the Device mode ONLY */
273 USBx
->GINTMSK
|= (USB_OTG_GINTMSK_USBSUSPM
| USB_OTG_GINTMSK_USBRST
|\
274 USB_OTG_GINTMSK_ENUMDNEM
| USB_OTG_GINTMSK_IEPINT
|\
275 USB_OTG_GINTMSK_OEPINT
| USB_OTG_GINTMSK_IISOIXFRM
|\
276 USB_OTG_GINTMSK_PXFRM_IISOOXFRM
| USB_OTG_GINTMSK_WUIM
);
280 USBx
->GINTMSK
|= USB_OTG_GINTMSK_SOFM
;
283 if (cfg
.vbus_sensing_enable
== ENABLE
)
285 USBx
->GINTMSK
|= (USB_OTG_GINTMSK_SRQIM
| USB_OTG_GINTMSK_OTGINT
);
292 * @brief USB_OTG_FlushTxFifo : Flush a Tx FIFO
293 * @param USBx : Selected device
294 * @param num : FIFO number
295 * This parameter can be a value from 1 to 15
296 15 means Flush all Tx FIFOs
299 HAL_StatusTypeDef
USB_FlushTxFifo (USB_OTG_GlobalTypeDef
*USBx
, uint32_t num
)
303 USBx
->GRSTCTL
= (USB_OTG_GRSTCTL_TXFFLSH
|(uint32_t)(num
<< 6));
307 if (++count
> 200000)
312 while ((USBx
->GRSTCTL
& USB_OTG_GRSTCTL_TXFFLSH
) == USB_OTG_GRSTCTL_TXFFLSH
);
318 * @brief USB_FlushRxFifo : Flush Rx FIFO
319 * @param USBx : Selected device
322 HAL_StatusTypeDef
USB_FlushRxFifo(USB_OTG_GlobalTypeDef
*USBx
)
326 USBx
->GRSTCTL
= USB_OTG_GRSTCTL_RXFFLSH
;
330 if (++count
> 200000)
335 while ((USBx
->GRSTCTL
& USB_OTG_GRSTCTL_RXFFLSH
) == USB_OTG_GRSTCTL_RXFFLSH
);
341 * @brief USB_SetDevSpeed :Initializes the DevSpd field of DCFG register
342 * depending the PHY type and the enumeration speed of the device.
343 * @param USBx : Selected device
344 * @param speed : device speed
345 * This parameter can be one of the these values:
346 * @arg USB_OTG_SPEED_FULL: Full speed mode
347 * @arg USB_OTG_SPEED_LOW: Low speed mode
350 HAL_StatusTypeDef
USB_SetDevSpeed(USB_OTG_GlobalTypeDef
*USBx
, uint8_t speed
)
352 /* Prevent unused argument(s) compilation warning */
355 USBx_DEVICE
->DCFG
|= speed
;
360 * @brief USB_GetDevSpeed :Return the Dev Speed
361 * @param USBx : Selected device
362 * @retval speed : device speed
363 * This parameter can be one of the these values:
364 * @arg USB_OTG_SPEED_FULL: Full speed mode
365 * @arg USB_OTG_SPEED_LOW: Low speed mode
367 uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef
*USBx
)
370 /* Prevent unused argument(s) compilation warning */
373 if (((USBx_DEVICE
->DSTS
& USB_OTG_DSTS_ENUMSPD
) == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ
)||
374 ((USBx_DEVICE
->DSTS
& USB_OTG_DSTS_ENUMSPD
) == DSTS_ENUMSPD_FS_PHY_48MHZ
))
376 speed
= USB_OTG_SPEED_FULL
;
378 else if((USBx_DEVICE
->DSTS
& USB_OTG_DSTS_ENUMSPD
) == DSTS_ENUMSPD_LS_PHY_6MHZ
)
380 speed
= USB_OTG_SPEED_LOW
;
387 * @brief Activate and configure an endpoint
388 * @param USBx : Selected device
389 * @param ep: pointer to endpoint structure
392 HAL_StatusTypeDef
USB_ActivateEndpoint(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
394 /* Prevent unused argument(s) compilation warning */
399 /* Assign a Tx FIFO */
400 ep
->tx_fifo_num
= ep
->num
;
402 /* Set initial data PID. */
403 if (ep
->type
== EP_TYPE_BULK
)
405 ep
->data_pid_start
= 0;
410 USBx_DEVICE
->DAINTMSK
|= USB_OTG_DAINTMSK_IEPM
& ((1 << (ep
->num
)));
412 if (((USBx_INEP(ep
->num
)->DIEPCTL
) & USB_OTG_DIEPCTL_USBAEP
) == 0)
414 USBx_INEP(ep
->num
)->DIEPCTL
|= ((ep
->maxpacket
& USB_OTG_DIEPCTL_MPSIZ
) | (ep
->type
<< 18 ) |\
415 ((ep
->num
) << 22 ) | (USB_OTG_DIEPCTL_SD0PID_SEVNFRM
) | (USB_OTG_DIEPCTL_USBAEP
));
420 USBx_DEVICE
->DAINTMSK
|= USB_OTG_DAINTMSK_OEPM
& ((1 << (ep
->num
)) << 16);
422 if (((USBx_OUTEP(ep
->num
)->DOEPCTL
) & USB_OTG_DOEPCTL_USBAEP
) == 0)
424 USBx_OUTEP(ep
->num
)->DOEPCTL
|= ((ep
->maxpacket
& USB_OTG_DOEPCTL_MPSIZ
) | (ep
->type
<< 18 ) |\
425 (USB_OTG_DIEPCTL_SD0PID_SEVNFRM
)| (USB_OTG_DOEPCTL_USBAEP
));
433 * @brief De-activate and de-initialize an endpoint
434 * @param USBx : Selected device
435 * @param ep: pointer to endpoint structure
438 HAL_StatusTypeDef
USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
440 /* Prevent unused argument(s) compilation warning */
443 /* Read DEPCTLn register */
446 USBx_DEVICE
->DEACHMSK
&= ~(USB_OTG_DAINTMSK_IEPM
& ((1 << (ep
->num
))));
447 USBx_DEVICE
->DAINTMSK
&= ~(USB_OTG_DAINTMSK_IEPM
& ((1 << (ep
->num
))));
448 USBx_INEP(ep
->num
)->DIEPCTL
&= ~ USB_OTG_DIEPCTL_USBAEP
;
452 USBx_DEVICE
->DEACHMSK
&= ~(USB_OTG_DAINTMSK_OEPM
& ((1 << (ep
->num
)) << 16));
453 USBx_DEVICE
->DAINTMSK
&= ~(USB_OTG_DAINTMSK_OEPM
& ((1 << (ep
->num
)) << 16));
454 USBx_OUTEP(ep
->num
)->DOEPCTL
&= ~USB_OTG_DOEPCTL_USBAEP
;
460 * @brief USB_EPStartXfer : setup and starts a transfer over an EP
461 * @param USBx : Selected device
462 * @param ep: pointer to endpoint structure
465 HAL_StatusTypeDef
USB_EPStartXfer(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
468 /* Prevent unused argument(s) compilation warning */
474 /* Zero Length Packet? */
475 if (ep
->xfer_len
== 0)
477 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_PKTCNT
);
478 USBx_INEP(ep
->num
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_PKTCNT
& (1 << 19)) ;
479 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_XFRSIZ
);
483 /* Program the transfer size and packet count
484 * as follows: xfersize = N * maxpacket +
485 * short_packet pktcnt = N + (short_packet
488 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_XFRSIZ
);
489 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_PKTCNT
);
490 USBx_INEP(ep
->num
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_PKTCNT
& (((ep
->xfer_len
+ ep
->maxpacket
-1)/ ep
->maxpacket
) << 19)) ;
491 USBx_INEP(ep
->num
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_XFRSIZ
& ep
->xfer_len
);
493 if (ep
->type
== EP_TYPE_ISOC
)
495 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_MULCNT
);
496 USBx_INEP(ep
->num
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_MULCNT
& (1 << 29));
500 if (ep
->type
!= EP_TYPE_ISOC
)
502 /* Enable the Tx FIFO Empty Interrupt for this EP */
503 if (ep
->xfer_len
> 0)
505 USBx_DEVICE
->DIEPEMPMSK
|= 1 << ep
->num
;
509 if (ep
->type
== EP_TYPE_ISOC
)
511 if ((USBx_DEVICE
->DSTS
& ( 1 << 8 )) == 0)
513 USBx_INEP(ep
->num
)->DIEPCTL
|= USB_OTG_DIEPCTL_SODDFRM
;
517 USBx_INEP(ep
->num
)->DIEPCTL
|= USB_OTG_DIEPCTL_SD0PID_SEVNFRM
;
521 /* EP enable, IN data in FIFO */
522 USBx_INEP(ep
->num
)->DIEPCTL
|= (USB_OTG_DIEPCTL_CNAK
| USB_OTG_DIEPCTL_EPENA
);
524 if (ep
->type
== EP_TYPE_ISOC
)
526 USB_WritePacket(USBx
, ep
->xfer_buff
, ep
->num
, ep
->xfer_len
);
529 else /* OUT endpoint */
531 /* Program the transfer size and packet count as follows:
533 * xfersize = N * maxpacket
535 USBx_OUTEP(ep
->num
)->DOEPTSIZ
&= ~(USB_OTG_DOEPTSIZ_XFRSIZ
);
536 USBx_OUTEP(ep
->num
)->DOEPTSIZ
&= ~(USB_OTG_DOEPTSIZ_PKTCNT
);
538 if (ep
->xfer_len
== 0)
540 USBx_OUTEP(ep
->num
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_XFRSIZ
& ep
->maxpacket
);
541 USBx_OUTEP(ep
->num
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_PKTCNT
& (1 << 19));
545 pktcnt
= (ep
->xfer_len
+ ep
->maxpacket
-1)/ ep
->maxpacket
;
546 USBx_OUTEP(ep
->num
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_PKTCNT
& (pktcnt
<< 19));
547 USBx_OUTEP(ep
->num
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_XFRSIZ
& (ep
->maxpacket
* pktcnt
));
550 if (ep
->type
== EP_TYPE_ISOC
)
552 if ((USBx_DEVICE
->DSTS
& ( 1 << 8 )) == 0)
554 USBx_OUTEP(ep
->num
)->DOEPCTL
|= USB_OTG_DOEPCTL_SODDFRM
;
558 USBx_OUTEP(ep
->num
)->DOEPCTL
|= USB_OTG_DOEPCTL_SD0PID_SEVNFRM
;
562 USBx_OUTEP(ep
->num
)->DOEPCTL
|= (USB_OTG_DOEPCTL_CNAK
| USB_OTG_DOEPCTL_EPENA
);
569 * @brief USB_EP0StartXfer : setup and starts a transfer over the EP 0
570 * @param USBx : Selected device
571 * @param ep: pointer to endpoint structure
574 HAL_StatusTypeDef
USB_EP0StartXfer(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
576 /* Prevent unused argument(s) compilation warning */
582 /* Zero Length Packet? */
583 if (ep
->xfer_len
== 0)
585 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_PKTCNT
);
586 USBx_INEP(ep
->num
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_PKTCNT
& (1 << 19));
587 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_XFRSIZ
);
591 /* Program the transfer size and packet count
592 * as follows: xfersize = N * maxpacket +
593 * short_packet pktcnt = N + (short_packet
596 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_XFRSIZ
);
597 USBx_INEP(ep
->num
)->DIEPTSIZ
&= ~(USB_OTG_DIEPTSIZ_PKTCNT
);
599 if(ep
->xfer_len
> ep
->maxpacket
)
601 ep
->xfer_len
= ep
->maxpacket
;
603 USBx_INEP(ep
->num
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_PKTCNT
& (1 << 19));
604 USBx_INEP(ep
->num
)->DIEPTSIZ
|= (USB_OTG_DIEPTSIZ_XFRSIZ
& ep
->xfer_len
);
607 /* Enable the Tx FIFO Empty Interrupt for this EP */
608 if (ep
->xfer_len
> 0)
610 USBx_DEVICE
->DIEPEMPMSK
|= 1 << (ep
->num
);
613 /* EP enable, IN data in FIFO */
614 USBx_INEP(ep
->num
)->DIEPCTL
|= (USB_OTG_DIEPCTL_CNAK
| USB_OTG_DIEPCTL_EPENA
);
616 else /* OUT endpoint */
618 /* Program the transfer size and packet count as follows:
620 * xfersize = N * maxpacket
622 USBx_OUTEP(ep
->num
)->DOEPTSIZ
&= ~(USB_OTG_DOEPTSIZ_XFRSIZ
);
623 USBx_OUTEP(ep
->num
)->DOEPTSIZ
&= ~(USB_OTG_DOEPTSIZ_PKTCNT
);
625 if (ep
->xfer_len
> 0)
627 ep
->xfer_len
= ep
->maxpacket
;
630 USBx_OUTEP(ep
->num
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_PKTCNT
& (1 << 19));
631 USBx_OUTEP(ep
->num
)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_XFRSIZ
& (ep
->maxpacket
));
634 USBx_OUTEP(ep
->num
)->DOEPCTL
|= (USB_OTG_DOEPCTL_CNAK
| USB_OTG_DOEPCTL_EPENA
);
641 * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
642 * with the EP/channel
643 * @param USBx : Selected device
644 * @param src : pointer to source buffer
645 * @param ch_ep_num : endpoint or host channel number
646 * @param len : Number of bytes to write
649 HAL_StatusTypeDef
USB_WritePacket(USB_OTG_GlobalTypeDef
*USBx
, uint8_t *src
, uint8_t ch_ep_num
, uint16_t len
)
651 uint32_t count32b
= 0 , index
= 0;
652 /* Prevent unused argument(s) compilation warning */
655 count32b
= (len
+ 3) / 4;
656 for (index
= 0; index
< count32b
; index
++, src
+= 4)
658 USBx_DFIFO(ch_ep_num
) = *((__packed
uint32_t *)src
);
664 * @brief USB_ReadPacket : read a packet from the Tx FIFO associated
665 * with the EP/channel
666 * @param USBx : Selected device
667 * @param dest : destination pointer
668 * @param len : Number of bytes to read
669 * @retval pointer to destination buffer
671 void *USB_ReadPacket(USB_OTG_GlobalTypeDef
*USBx
, uint8_t *dest
, uint16_t len
)
674 uint32_t count32b
= (len
+ 3) / 4;
675 /* Prevent unused argument(s) compilation warning */
678 for ( index
= 0; index
< count32b
; index
++, dest
+= 4 )
680 *(__packed
uint32_t *)dest
= USBx_DFIFO(0);
683 return ((void *)dest
);
687 * @brief USB_EPSetStall : set a stall condition over an EP
688 * @param USBx : Selected device
689 * @param ep: pointer to endpoint structure
692 HAL_StatusTypeDef
USB_EPSetStall(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
694 /* Prevent unused argument(s) compilation warning */
699 if (((USBx_INEP(ep
->num
)->DIEPCTL
) & USB_OTG_DIEPCTL_EPENA
) == 0)
701 USBx_INEP(ep
->num
)->DIEPCTL
&= ~(USB_OTG_DIEPCTL_EPDIS
);
703 USBx_INEP(ep
->num
)->DIEPCTL
|= USB_OTG_DIEPCTL_STALL
;
707 if (((USBx_OUTEP(ep
->num
)->DOEPCTL
) & USB_OTG_DOEPCTL_EPENA
) == 0)
709 USBx_OUTEP(ep
->num
)->DOEPCTL
&= ~(USB_OTG_DOEPCTL_EPDIS
);
711 USBx_OUTEP(ep
->num
)->DOEPCTL
|= USB_OTG_DOEPCTL_STALL
;
717 * @brief USB_EPClearStall : Clear a stall condition over an EP
718 * @param USBx : Selected device
719 * @param ep: pointer to endpoint structure
722 HAL_StatusTypeDef
USB_EPClearStall(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_EPTypeDef
*ep
)
724 /* Prevent unused argument(s) compilation warning */
729 USBx_INEP(ep
->num
)->DIEPCTL
&= ~USB_OTG_DIEPCTL_STALL
;
730 if (ep
->type
== EP_TYPE_INTR
|| ep
->type
== EP_TYPE_BULK
)
732 USBx_INEP(ep
->num
)->DIEPCTL
|= USB_OTG_DIEPCTL_SD0PID_SEVNFRM
; /* DATA0 */
737 USBx_OUTEP(ep
->num
)->DOEPCTL
&= ~USB_OTG_DOEPCTL_STALL
;
738 if (ep
->type
== EP_TYPE_INTR
|| ep
->type
== EP_TYPE_BULK
)
740 USBx_OUTEP(ep
->num
)->DOEPCTL
|= USB_OTG_DOEPCTL_SD0PID_SEVNFRM
; /* DATA0 */
747 * @brief USB_StopDevice : Stop the usb device mode
748 * @param USBx : Selected device
751 HAL_StatusTypeDef
USB_StopDevice(USB_OTG_GlobalTypeDef
*USBx
)
755 /* Clear Pending interrupt */
756 for (index
= 0; index
< 15 ; index
++)
758 USBx_INEP(index
)->DIEPINT
= 0xFF;
759 USBx_OUTEP(index
)->DOEPINT
= 0xFF;
761 USBx_DEVICE
->DAINT
= 0xFFFFFFFF;
763 /* Clear interrupt masks */
764 USBx_DEVICE
->DIEPMSK
= 0;
765 USBx_DEVICE
->DOEPMSK
= 0;
766 USBx_DEVICE
->DAINTMSK
= 0;
769 USB_FlushRxFifo(USBx
);
770 USB_FlushTxFifo(USBx
, 0x10 );
776 * @brief USB_SetDevAddress : Stop the usb device mode
777 * @param USBx : Selected device
778 * @param address : new device address to be assigned
779 * This parameter can be a value from 0 to 255
782 HAL_StatusTypeDef
USB_SetDevAddress (USB_OTG_GlobalTypeDef
*USBx
, uint8_t address
)
784 /* Prevent unused argument(s) compilation warning */
787 USBx_DEVICE
->DCFG
&= ~ (USB_OTG_DCFG_DAD
);
788 USBx_DEVICE
->DCFG
|= (address
<< 4) & USB_OTG_DCFG_DAD
;
794 * @brief USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
795 * @param USBx : Selected device
798 HAL_StatusTypeDef
USB_DevConnect (USB_OTG_GlobalTypeDef
*USBx
)
800 /* Prevent unused argument(s) compilation warning */
803 USBx_DEVICE
->DCTL
&= ~USB_OTG_DCTL_SDIS
;
810 * @brief USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
811 * @param USBx : Selected device
814 HAL_StatusTypeDef
USB_DevDisconnect (USB_OTG_GlobalTypeDef
*USBx
)
816 /* Prevent unused argument(s) compilation warning */
819 USBx_DEVICE
->DCTL
|= USB_OTG_DCTL_SDIS
;
826 * @brief USB_ReadInterrupts: return the global USB interrupt status
827 * @param USBx : Selected device
830 uint32_t USB_ReadInterrupts (USB_OTG_GlobalTypeDef
*USBx
)
834 tmpreg
= USBx
->GINTSTS
;
835 tmpreg
&= USBx
->GINTMSK
;
840 * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
841 * @param USBx : Selected device
844 uint32_t USB_ReadDevAllOutEpInterrupt (USB_OTG_GlobalTypeDef
*USBx
)
847 /* Prevent unused argument(s) compilation warning */
850 tmpreg
= USBx_DEVICE
->DAINT
;
851 tmpreg
&= USBx_DEVICE
->DAINTMSK
;
852 return ((tmpreg
& 0xffff0000) >> 16);
856 * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
857 * @param USBx : Selected device
860 uint32_t USB_ReadDevAllInEpInterrupt (USB_OTG_GlobalTypeDef
*USBx
)
863 /* Prevent unused argument(s) compilation warning */
866 tmpreg
= USBx_DEVICE
->DAINT
;
867 tmpreg
&= USBx_DEVICE
->DAINTMSK
;
868 return ((tmpreg
& 0xFFFF));
872 * @brief Returns Device OUT EP Interrupt register
873 * @param USBx : Selected device
874 * @param epnum : endpoint number
875 * This parameter can be a value from 0 to 15
876 * @retval Device OUT EP Interrupt register
878 uint32_t USB_ReadDevOutEPInterrupt (USB_OTG_GlobalTypeDef
*USBx
, uint8_t epnum
)
880 /* Prevent unused argument(s) compilation warning */
884 tmpreg
= USBx_OUTEP(epnum
)->DOEPINT
;
885 tmpreg
&= USBx_DEVICE
->DOEPMSK
;
890 * @brief Returns Device IN EP Interrupt register
891 * @param USBx : Selected device
892 * @param epnum : endpoint number
893 * This parameter can be a value from 0 to 15
894 * @retval Device IN EP Interrupt register
896 uint32_t USB_ReadDevInEPInterrupt (USB_OTG_GlobalTypeDef
*USBx
, uint8_t epnum
)
898 uint32_t tmpreg
= 0, msk
= 0, emp
= 0;
899 /* Prevent unused argument(s) compilation warning */
902 msk
= USBx_DEVICE
->DIEPMSK
;
903 emp
= USBx_DEVICE
->DIEPEMPMSK
;
904 msk
|= ((emp
>> epnum
) & 0x1) << 7;
905 tmpreg
= USBx_INEP(epnum
)->DIEPINT
& msk
;
910 * @brief USB_ClearInterrupts: clear a USB interrupt
911 * @param USBx : Selected device
912 * @param interrupt : interrupt flag
915 void USB_ClearInterrupts (USB_OTG_GlobalTypeDef
*USBx
, uint32_t interrupt
)
917 USBx
->GINTSTS
|= interrupt
;
921 * @brief Returns USB core mode
922 * @param USBx : Selected device
923 * @retval return core mode : Host or Device
924 * This parameter can be one of the these values:
928 uint32_t USB_GetMode(USB_OTG_GlobalTypeDef
*USBx
)
930 /* Prevent unused argument(s) compilation warning */
933 return ((USBx
->GINTSTS
) & 0x1);
937 * @brief Activate EP0 for Setup transactions
938 * @param USBx : Selected device
941 HAL_StatusTypeDef
USB_ActivateSetup (USB_OTG_GlobalTypeDef
*USBx
)
943 /* Prevent unused argument(s) compilation warning */
945 /* Set the MPS of the IN EP based on the enumeration speed */
946 USBx_INEP(0)->DIEPCTL
&= ~USB_OTG_DIEPCTL_MPSIZ
;
948 if((USBx_DEVICE
->DSTS
& USB_OTG_DSTS_ENUMSPD
) == DSTS_ENUMSPD_LS_PHY_6MHZ
)
950 USBx_INEP(0)->DIEPCTL
|= 3;
952 USBx_DEVICE
->DCTL
|= USB_OTG_DCTL_CGINAK
;
958 * @brief Prepare the EP0 to start the first control setup
959 * @param USBx : Selected device
960 * @param psetup : pointer to setup packet
963 HAL_StatusTypeDef
USB_EP0_OutStart(USB_OTG_GlobalTypeDef
*USBx
, uint8_t *psetup
)
965 /* Prevent unused argument(s) compilation warning */
968 USBx_OUTEP(0)->DOEPTSIZ
= 0;
969 USBx_OUTEP(0)->DOEPTSIZ
|= (USB_OTG_DOEPTSIZ_PKTCNT
& (1 << 19));
970 USBx_OUTEP(0)->DOEPTSIZ
|= (3 * 8);
971 USBx_OUTEP(0)->DOEPTSIZ
|= USB_OTG_DOEPTSIZ_STUPCNT
;
977 * @brief USB_HostInit : Initializes the USB OTG controller registers
979 * @param USBx : Selected device
980 * @param cfg : pointer to a USB_OTG_CfgTypeDef structure that contains
981 * the configuration information for the specified USBx peripheral.
984 HAL_StatusTypeDef
USB_HostInit (USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_CfgTypeDef cfg
)
988 /* Restart the Phy Clock */
992 USBx
->GCCFG
&=~ (USB_OTG_GCCFG_VBUSASEN
);
993 USBx
->GCCFG
&=~ (USB_OTG_GCCFG_VBUSBSEN
);
995 /* Disable the FS/LS support mode only */
996 if((cfg
.speed
== USB_OTG_SPEED_FULL
)&&
997 (USBx
!= USB_OTG_FS
))
999 USBx_HOST
->HCFG
|= USB_OTG_HCFG_FSLSS
;
1003 USBx_HOST
->HCFG
&= ~(USB_OTG_HCFG_FSLSS
);
1006 /* Make sure the FIFOs are flushed. */
1007 USB_FlushTxFifo(USBx
, 0x10 ); /* all Tx FIFOs */
1008 USB_FlushRxFifo(USBx
);
1010 /* Clear all pending HC Interrupts */
1011 for (index
= 0; index
< cfg
.Host_channels
; index
++)
1013 USBx_HC(index
)->HCINT
= 0xFFFFFFFF;
1014 USBx_HC(index
)->HCINTMSK
= 0;
1017 /* Enable VBUS driving */
1018 USB_DriveVbus(USBx
, 1);
1022 /* Disable all interrupts. */
1025 /* Clear any pending interrupts */
1026 USBx
->GINTSTS
= 0xFFFFFFFF;
1028 if(USBx
== USB_OTG_FS
)
1030 /* set Rx FIFO size */
1031 USBx
->GRXFSIZ
= (uint32_t )0x80;
1032 USBx
->DIEPTXF0_HNPTXFSIZ
= (uint32_t )(((0x60 << 16)& USB_OTG_NPTXFD
) | 0x80);
1033 USBx
->HPTXFSIZ
= (uint32_t )(((0x40 << 16)& USB_OTG_HPTXFSIZ_PTXFD
) | 0xE0);
1036 /* Enable the common interrupts */
1037 USBx
->GINTMSK
|= USB_OTG_GINTMSK_RXFLVLM
;
1039 /* Enable interrupts matching to the Host mode ONLY */
1040 USBx
->GINTMSK
|= (USB_OTG_GINTMSK_PRTIM
| USB_OTG_GINTMSK_HCIM
|\
1041 USB_OTG_GINTMSK_SOFM
|USB_OTG_GINTSTS_DISCINT
|\
1042 USB_OTG_GINTMSK_PXFRM_IISOOXFRM
| USB_OTG_GINTMSK_WUIM
);
1048 * @brief USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
1049 * HCFG register on the PHY type and set the right frame interval
1050 * @param USBx : Selected device
1051 * @param freq : clock frequency
1052 * This parameter can be one of the these values:
1053 * HCFG_48_MHZ : Full Speed 48 MHz Clock
1054 * HCFG_6_MHZ : Low Speed 6 MHz Clock
1055 * @retval HAL status
1057 HAL_StatusTypeDef
USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef
*USBx
, uint8_t freq
)
1059 /* Prevent unused argument(s) compilation warning */
1062 USBx_HOST
->HCFG
&= ~(USB_OTG_HCFG_FSLSPCS
);
1063 USBx_HOST
->HCFG
|= (freq
& USB_OTG_HCFG_FSLSPCS
);
1065 if (freq
== HCFG_48_MHZ
)
1067 USBx_HOST
->HFIR
= (uint32_t)48000;
1069 else if (freq
== HCFG_6_MHZ
)
1071 USBx_HOST
->HFIR
= (uint32_t)6000;
1077 * @brief USB_OTG_ResetPort : Reset Host Port
1078 * @param USBx : Selected device
1079 * @retval HAL status
1080 * @note : (1)The application must wait at least 10 ms
1081 * before clearing the reset bit.
1083 HAL_StatusTypeDef
USB_ResetPort(USB_OTG_GlobalTypeDef
*USBx
)
1085 __IO
uint32_t hprt0
= 0;
1086 /* Prevent unused argument(s) compilation warning */
1091 hprt0
&= ~(USB_OTG_HPRT_PENA
| USB_OTG_HPRT_PCDET
|\
1092 USB_OTG_HPRT_PENCHNG
| USB_OTG_HPRT_POCCHNG
);
1094 USBx_HPRT0
= (USB_OTG_HPRT_PRST
| hprt0
);
1095 HAL_Delay (10); /* See Note #1 */
1096 USBx_HPRT0
= ((~USB_OTG_HPRT_PRST
) & hprt0
);
1101 * @brief USB_DriveVbus : activate or de-activate vbus
1102 * @param state : VBUS state
1103 * This parameter can be one of the these values:
1106 * @retval HAL status
1108 HAL_StatusTypeDef
USB_DriveVbus (USB_OTG_GlobalTypeDef
*USBx
, uint8_t state
)
1110 __IO
uint32_t hprt0
= 0;
1111 /* Prevent unused argument(s) compilation warning */
1115 hprt0
&= ~(USB_OTG_HPRT_PENA
| USB_OTG_HPRT_PCDET
|\
1116 USB_OTG_HPRT_PENCHNG
| USB_OTG_HPRT_POCCHNG
);
1118 if (((hprt0
& USB_OTG_HPRT_PPWR
) == 0 ) && (state
== 1 ))
1120 USBx_HPRT0
= (USB_OTG_HPRT_PPWR
| hprt0
);
1122 if (((hprt0
& USB_OTG_HPRT_PPWR
) == USB_OTG_HPRT_PPWR
) && (state
== 0 ))
1124 USBx_HPRT0
= ((~USB_OTG_HPRT_PPWR
) & hprt0
);
1130 * @brief Return Host Core speed
1131 * @param USBx : Selected device
1132 * @retval speed : Host speed
1133 * This parameter can be one of the these values:
1134 * @arg USB_OTG_SPEED_FULL: Full speed mode
1135 * @arg USB_OTG_SPEED_LOW: Low speed mode
1137 uint32_t USB_GetHostSpeed (USB_OTG_GlobalTypeDef
*USBx
)
1139 __IO
uint32_t hprt0
= 0;
1140 /* Prevent unused argument(s) compilation warning */
1144 return ((hprt0
& USB_OTG_HPRT_PSPD
) >> 17);
1148 * @brief Return Host Current Frame number
1149 * @param USBx : Selected device
1150 * @retval current frame number
1152 uint32_t USB_GetCurrentFrame (USB_OTG_GlobalTypeDef
*USBx
)
1154 /* Prevent unused argument(s) compilation warning */
1157 return (USBx_HOST
->HFNUM
& USB_OTG_HFNUM_FRNUM
);
1161 * @brief Initialize a host channel
1162 * @param USBx : Selected device
1163 * @param ch_num : Channel number
1164 * This parameter can be a value from 1 to 15
1165 * @param epnum : Endpoint number
1166 * This parameter can be a value from 1 to 15
1167 * @param dev_address : Current device address
1168 * This parameter can be a value from 0 to 255
1169 * @param speed : Current device speed
1170 * This parameter can be one of the these values:
1171 * @arg USB_OTG_SPEED_FULL: Full speed mode
1172 * @arg USB_OTG_SPEED_LOW: Low speed mode
1173 * @param ep_type : Endpoint Type
1174 * This parameter can be one of the these values:
1175 * @arg EP_TYPE_CTRL: Control type
1176 * @arg EP_TYPE_ISOC: Isochronous type
1177 * @arg EP_TYPE_BULK: Bulk type
1178 * @arg EP_TYPE_INTR: Interrupt type
1179 * @param mps : Max Packet Size
1180 * This parameter can be a value from 0 to32K
1183 HAL_StatusTypeDef
USB_HC_Init(USB_OTG_GlobalTypeDef
*USBx
,
1186 uint8_t dev_address
,
1191 /* Clear old interrupt conditions for this host channel. */
1192 USBx_HC(ch_num
)->HCINT
= 0xFFFFFFFF;
1194 /* Enable channel interrupts required for this transfer. */
1199 USBx_HC(ch_num
)->HCINTMSK
= USB_OTG_HCINTMSK_XFRCM
|\
1200 USB_OTG_HCINTMSK_STALLM
|\
1201 USB_OTG_HCINTMSK_TXERRM
|\
1202 USB_OTG_HCINTMSK_DTERRM
|\
1203 USB_OTG_HCINTMSK_AHBERR
|\
1204 USB_OTG_HCINTMSK_NAKM
;
1208 USBx_HC(ch_num
)->HCINTMSK
|= USB_OTG_HCINTMSK_BBERRM
;
1213 USBx_HC(ch_num
)->HCINTMSK
= USB_OTG_HCINTMSK_XFRCM
|\
1214 USB_OTG_HCINTMSK_STALLM
|\
1215 USB_OTG_HCINTMSK_TXERRM
|\
1216 USB_OTG_HCINTMSK_DTERRM
|\
1217 USB_OTG_HCINTMSK_NAKM
|\
1218 USB_OTG_HCINTMSK_AHBERR
|\
1219 USB_OTG_HCINTMSK_FRMORM
;
1223 USBx_HC(ch_num
)->HCINTMSK
|= USB_OTG_HCINTMSK_BBERRM
;
1229 USBx_HC(ch_num
)->HCINTMSK
= USB_OTG_HCINTMSK_XFRCM
|\
1230 USB_OTG_HCINTMSK_ACKM
|\
1231 USB_OTG_HCINTMSK_AHBERR
|\
1232 USB_OTG_HCINTMSK_FRMORM
;
1236 USBx_HC(ch_num
)->HCINTMSK
|= (USB_OTG_HCINTMSK_TXERRM
| USB_OTG_HCINTMSK_BBERRM
);
1241 /* Enable the top level host channel interrupt. */
1242 USBx_HOST
->HAINTMSK
|= (1 << ch_num
);
1244 /* Make sure host channel interrupts are enabled. */
1245 USBx
->GINTMSK
|= USB_OTG_GINTMSK_HCIM
;
1247 /* Program the HCCHAR register */
1248 USBx_HC(ch_num
)->HCCHAR
= (((dev_address
<< 22) & USB_OTG_HCCHAR_DAD
) |\
1249 (((epnum
& 0x7F)<< 11) & USB_OTG_HCCHAR_EPNUM
)|\
1250 ((((epnum
& 0x80) == 0x80)<< 15) & USB_OTG_HCCHAR_EPDIR
)|\
1251 (((speed
== HPRT0_PRTSPD_LOW_SPEED
)<< 17) & USB_OTG_HCCHAR_LSDEV
)|\
1252 ((ep_type
<< 18) & USB_OTG_HCCHAR_EPTYP
)|\
1253 (mps
& USB_OTG_HCCHAR_MPSIZ
));
1255 if (ep_type
== EP_TYPE_INTR
)
1257 USBx_HC(ch_num
)->HCCHAR
|= USB_OTG_HCCHAR_ODDFRM
;
1264 * @brief Start a transfer over a host channel
1265 * @param USBx : Selected device
1266 * @param hc : pointer to host channel structure
1269 #if defined (__CC_ARM) /*!< ARM Compiler */
1271 #elif defined (__GNUC__) /*!< GNU Compiler */
1272 #pragma GCC optimize ("O0")
1273 #endif /* __CC_ARM */
1274 HAL_StatusTypeDef
USB_HC_StartXfer(USB_OTG_GlobalTypeDef
*USBx
, USB_OTG_HCTypeDef
*hc
)
1276 uint8_t is_oddframe
= 0;
1277 uint16_t len_words
= 0;
1278 uint16_t num_packets
= 0;
1279 uint16_t max_hc_pkt_count
= 256;
1280 uint32_t tmpreg
= 0;
1282 /* Compute the expected number of packets associated to the transfer */
1283 if (hc
->xfer_len
> 0)
1285 num_packets
= (hc
->xfer_len
+ hc
->max_packet
- 1) / hc
->max_packet
;
1287 if (num_packets
> max_hc_pkt_count
)
1289 num_packets
= max_hc_pkt_count
;
1290 hc
->xfer_len
= num_packets
* hc
->max_packet
;
1299 hc
->xfer_len
= num_packets
* hc
->max_packet
;
1302 /* Initialize the HCTSIZn register */
1303 USBx_HC(hc
->ch_num
)->HCTSIZ
= (((hc
->xfer_len
) & USB_OTG_HCTSIZ_XFRSIZ
)) |\
1304 ((num_packets
<< 19) & USB_OTG_HCTSIZ_PKTCNT
) |\
1305 (((hc
->data_pid
) << 29) & USB_OTG_HCTSIZ_DPID
);
1307 is_oddframe
= (USBx_HOST
->HFNUM
& 0x01) ? 0 : 1;
1308 USBx_HC(hc
->ch_num
)->HCCHAR
&= ~USB_OTG_HCCHAR_ODDFRM
;
1309 USBx_HC(hc
->ch_num
)->HCCHAR
|= (is_oddframe
<< 29);
1311 /* Set host channel enable */
1312 tmpreg
= USBx_HC(hc
->ch_num
)->HCCHAR
;
1313 tmpreg
&= ~USB_OTG_HCCHAR_CHDIS
;
1314 tmpreg
|= USB_OTG_HCCHAR_CHENA
;
1315 USBx_HC(hc
->ch_num
)->HCCHAR
= tmpreg
;
1317 if((hc
->ep_is_in
== 0) && (hc
->xfer_len
> 0))
1321 /* Non periodic transfer */
1324 len_words
= (hc
->xfer_len
+ 3) / 4;
1326 /* check if there is enough space in FIFO space */
1327 if(len_words
> (USBx
->HNPTXSTS
& 0xFFFF))
1329 /* need to process data in nptxfempty interrupt */
1330 USBx
->GINTMSK
|= USB_OTG_GINTMSK_NPTXFEM
;
1334 /* Periodic transfer */
1337 len_words
= (hc
->xfer_len
+ 3) / 4;
1338 /* check if there is enough space in FIFO space */
1339 if(len_words
> (USBx_HOST
->HPTXSTS
& 0xFFFF)) /* split the transfer */
1341 /* need to process data in ptxfempty interrupt */
1342 USBx
->GINTMSK
|= USB_OTG_GINTMSK_PTXFEM
;
1350 /* Write packet into the Tx FIFO. */
1351 USB_WritePacket(USBx
, hc
->xfer_buff
, hc
->ch_num
, hc
->xfer_len
);
1358 * @brief Read all host channel interrupts status
1359 * @param USBx : Selected device
1362 uint32_t USB_HC_ReadInterrupt (USB_OTG_GlobalTypeDef
*USBx
)
1364 /* Prevent unused argument(s) compilation warning */
1367 return ((USBx_HOST
->HAINT
) & 0xFFFF);
1371 * @brief Halt a host channel
1372 * @param USBx : Selected device
1373 * @param hc_num : Host Channel number
1374 * This parameter can be a value from 1 to 15
1377 HAL_StatusTypeDef
USB_HC_Halt(USB_OTG_GlobalTypeDef
*USBx
, uint8_t hc_num
)
1380 /* Prevent unused argument(s) compilation warning */
1383 /* Check for space in the request queue to issue the halt. */
1384 if (((((USBx_HC(hc_num
)->HCCHAR
) & USB_OTG_HCCHAR_EPTYP
) >> 18) == HCCHAR_CTRL
) ||
1385 (((((USBx_HC(hc_num
)->HCCHAR
) & USB_OTG_HCCHAR_EPTYP
) >> 18) == HCCHAR_BULK
)))
1387 USBx_HC(hc_num
)->HCCHAR
|= USB_OTG_HCCHAR_CHDIS
;
1389 if ((USBx
->HNPTXSTS
& 0xFFFF) == 0)
1391 USBx_HC(hc_num
)->HCCHAR
&= ~USB_OTG_HCCHAR_CHENA
;
1392 USBx_HC(hc_num
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1393 USBx_HC(hc_num
)->HCCHAR
&= ~USB_OTG_HCCHAR_EPDIR
;
1401 while ((USBx_HC(hc_num
)->HCCHAR
& USB_OTG_HCCHAR_CHENA
) == USB_OTG_HCCHAR_CHENA
);
1405 USBx_HC(hc_num
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1410 USBx_HC(hc_num
)->HCCHAR
|= USB_OTG_HCCHAR_CHDIS
;
1412 if ((USBx_HOST
->HPTXSTS
& 0xFFFF) == 0)
1414 USBx_HC(hc_num
)->HCCHAR
&= ~USB_OTG_HCCHAR_CHENA
;
1415 USBx_HC(hc_num
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1416 USBx_HC(hc_num
)->HCCHAR
&= ~USB_OTG_HCCHAR_EPDIR
;
1424 while ((USBx_HC(hc_num
)->HCCHAR
& USB_OTG_HCCHAR_CHENA
) == USB_OTG_HCCHAR_CHENA
);
1428 USBx_HC(hc_num
)->HCCHAR
|= USB_OTG_HCCHAR_CHENA
;
1436 * @brief Initiate Do Ping protocol
1437 * @param USBx : Selected device
1438 * @param hc_num : Host Channel number
1439 * This parameter can be a value from 1 to 15
1442 HAL_StatusTypeDef
USB_DoPing(USB_OTG_GlobalTypeDef
*USBx
, uint8_t ch_num
)
1444 uint8_t num_packets
= 1;
1445 uint32_t tmpreg
= 0;
1446 /* Prevent unused argument(s) compilation warning */
1449 USBx_HC(ch_num
)->HCTSIZ
= ((num_packets
<< 19) & USB_OTG_HCTSIZ_PKTCNT
) |\
1450 USB_OTG_HCTSIZ_DOPING
;
1452 /* Set host channel enable */
1453 tmpreg
= USBx_HC(ch_num
)->HCCHAR
;
1454 tmpreg
&= ~USB_OTG_HCCHAR_CHDIS
;
1455 tmpreg
|= USB_OTG_HCCHAR_CHENA
;
1456 USBx_HC(ch_num
)->HCCHAR
= tmpreg
;
1462 * @brief Stop Host Core
1463 * @param USBx : Selected device
1466 HAL_StatusTypeDef
USB_StopHost(USB_OTG_GlobalTypeDef
*USBx
)
1472 USB_DisableGlobalInt(USBx
);
1475 USB_FlushTxFifo(USBx
, 0x10);
1476 USB_FlushRxFifo(USBx
);
1478 /* Flush out any leftover queued requests. */
1479 for (index
= 0; index
<= 15; index
++)
1481 value
= USBx_HC(index
)->HCCHAR
;
1482 value
|= USB_OTG_HCCHAR_CHDIS
;
1483 value
&= ~USB_OTG_HCCHAR_CHENA
;
1484 value
&= ~USB_OTG_HCCHAR_EPDIR
;
1485 USBx_HC(index
)->HCCHAR
= value
;
1488 /* Halt all channels to put them into a known state. */
1489 for (index
= 0; index
<= 15; index
++)
1491 value
= USBx_HC(index
)->HCCHAR
;
1492 value
|= USB_OTG_HCCHAR_CHDIS
;
1493 value
|= USB_OTG_HCCHAR_CHENA
;
1494 value
&= ~USB_OTG_HCCHAR_EPDIR
;
1495 USBx_HC(index
)->HCCHAR
= value
;
1504 while ((USBx_HC(index
)->HCCHAR
& USB_OTG_HCCHAR_CHENA
) == USB_OTG_HCCHAR_CHENA
);
1507 /* Clear any pending Host interrupts */
1508 USBx_HOST
->HAINT
= 0xFFFFFFFF;
1509 USBx
->GINTSTS
= 0xFFFFFFFF;
1510 USB_EnableGlobalInt(USBx
);
1516 * @brief USB_ActivateRemoteWakeup : active remote wakeup signalling
1517 * @param USBx : Selected device
1518 * @retval HAL status
1520 HAL_StatusTypeDef
USB_ActivateRemoteWakeup(USB_OTG_GlobalTypeDef
*USBx
)
1522 /* Prevent unused argument(s) compilation warning */
1524 if((USBx_DEVICE
->DSTS
& USB_OTG_DSTS_SUSPSTS
) == USB_OTG_DSTS_SUSPSTS
)
1526 /* active Remote wakeup signalling */
1527 USBx_DEVICE
->DCTL
|= USB_OTG_DCTL_RWUSIG
;
1533 * @brief USB_DeActivateRemoteWakeup : de-active remote wakeup signalling
1534 * @param USBx : Selected device
1535 * @retval HAL status
1537 HAL_StatusTypeDef
USB_DeActivateRemoteWakeup(USB_OTG_GlobalTypeDef
*USBx
)
1539 /* Prevent unused argument(s) compilation warning */
1541 /* active Remote wakeup signalling */
1542 USBx_DEVICE
->DCTL
&= ~(USB_OTG_DCTL_RWUSIG
);
1546 #endif /* USB_OTG_FS */
1548 /*==============================================================================
1549 USB Device FS peripheral available on STM32F102xx and STM32F103xx devices
1550 ==============================================================================*/
1553 * @brief Initializes the USB Core
1554 * @param USBx: USB Instance
1555 * @param cfg : pointer to a USB_CfgTypeDef structure that contains
1556 * the configuration information for the specified USBx peripheral.
1557 * @retval HAL status
1559 HAL_StatusTypeDef
USB_CoreInit(USB_TypeDef
*USBx
, USB_CfgTypeDef cfg
)
1561 /* Prevent unused argument(s) compilation warning */
1564 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1565 only by USB OTG FS peripheral.
1566 - This function is added to ensure compatibility across platforms.
1572 * @brief USB_EnableGlobalInt
1573 * Enables the controller's Global Int in the AHB Config reg
1574 * @param USBx : Selected device
1575 * @retval HAL status
1577 HAL_StatusTypeDef
USB_EnableGlobalInt(USB_TypeDef
*USBx
)
1579 uint32_t winterruptmask
= 0;
1581 /* Set winterruptmask variable */
1582 winterruptmask
= USB_CNTR_CTRM
| USB_CNTR_WKUPM
| USB_CNTR_SUSPM
| USB_CNTR_ERRM \
1583 | USB_CNTR_SOFM
| USB_CNTR_ESOFM
| USB_CNTR_RESETM
;
1585 /* Set interrupt mask */
1586 USBx
->CNTR
|= winterruptmask
;
1592 * @brief USB_DisableGlobalInt
1593 * Disable the controller's Global Int in the AHB Config reg
1594 * @param USBx : Selected device
1595 * @retval HAL status
1597 HAL_StatusTypeDef
USB_DisableGlobalInt(USB_TypeDef
*USBx
)
1599 uint32_t winterruptmask
= 0;
1601 /* Set winterruptmask variable */
1602 winterruptmask
= USB_CNTR_CTRM
| USB_CNTR_WKUPM
| USB_CNTR_SUSPM
| USB_CNTR_ERRM \
1603 | USB_CNTR_ESOFM
| USB_CNTR_RESETM
;
1605 /* Clear interrupt mask */
1606 USBx
->CNTR
&= ~winterruptmask
;
1612 * @brief USB_SetCurrentMode : Set functional mode
1613 * @param USBx : Selected device
1614 * @param mode : current core mode
1615 * This parameter can be one of the these values:
1616 * @arg USB_DEVICE_MODE: Peripheral mode mode
1617 * @retval HAL status
1619 HAL_StatusTypeDef
USB_SetCurrentMode(USB_TypeDef
*USBx
, USB_ModeTypeDef mode
)
1621 /* Prevent unused argument(s) compilation warning */
1624 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1625 only by USB OTG FS peripheral.
1626 - This function is added to ensure compatibility across platforms.
1632 * @brief USB_DevInit : Initializes the USB controller registers
1634 * @param USBx : Selected device
1635 * @param cfg : pointer to a USB_CfgTypeDef structure that contains
1636 * the configuration information for the specified USBx peripheral.
1637 * @retval HAL status
1639 HAL_StatusTypeDef
USB_DevInit (USB_TypeDef
*USBx
, USB_CfgTypeDef cfg
)
1641 /* Prevent unused argument(s) compilation warning */
1646 USBx
->CNTR
= USB_CNTR_FRES
;
1651 /*Clear pending interrupts*/
1654 /*Set Btable Address*/
1655 USBx
->BTABLE
= BTABLE_ADDRESS
;
1657 /* Enable USB Device Interrupt mask */
1658 USB_EnableGlobalInt(USBx
);
1664 * @brief USB_FlushTxFifo : Flush a Tx FIFO
1665 * @param USBx : Selected device
1666 * @param num : FIFO number
1667 * This parameter can be a value from 1 to 15
1668 15 means Flush all Tx FIFOs
1669 * @retval HAL status
1671 HAL_StatusTypeDef
USB_FlushTxFifo (USB_TypeDef
*USBx
, uint32_t num
)
1673 /* Prevent unused argument(s) compilation warning */
1676 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1677 only by USB OTG FS peripheral.
1678 - This function is added to ensure compatibility across platforms.
1684 * @brief USB_FlushRxFifo : Flush Rx FIFO
1685 * @param USBx : Selected device
1686 * @retval HAL status
1688 HAL_StatusTypeDef
USB_FlushRxFifo(USB_TypeDef
*USBx
)
1690 /* Prevent unused argument(s) compilation warning */
1692 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1693 only by USB OTG FS peripheral.
1694 - This function is added to ensure compatibility across platforms.
1700 * @brief Activate and configure an endpoint
1701 * @param USBx : Selected device
1702 * @param ep: pointer to endpoint structure
1703 * @retval HAL status
1705 HAL_StatusTypeDef
USB_ActivateEndpoint(USB_TypeDef
*USBx
, USB_EPTypeDef
*ep
)
1707 /* initialize Endpoint */
1711 PCD_SET_EPTYPE(USBx
, ep
->num
, USB_EP_CONTROL
);
1714 PCD_SET_EPTYPE(USBx
, ep
->num
, USB_EP_BULK
);
1717 PCD_SET_EPTYPE(USBx
, ep
->num
, USB_EP_INTERRUPT
);
1720 PCD_SET_EPTYPE(USBx
, ep
->num
, USB_EP_ISOCHRONOUS
);
1726 PCD_SET_EP_ADDRESS(USBx
, ep
->num
, ep
->num
);
1728 if (ep
->doublebuffer
== 0)
1732 /*Set the endpoint Transmit buffer address */
1733 PCD_SET_EP_TX_ADDRESS(USBx
, ep
->num
, ep
->pmaadress
);
1734 PCD_CLEAR_TX_DTOG(USBx
, ep
->num
);
1735 /* Configure NAK status for the Endpoint*/
1736 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_NAK
);
1740 /*Set the endpoint Receive buffer address */
1741 PCD_SET_EP_RX_ADDRESS(USBx
, ep
->num
, ep
->pmaadress
);
1742 /*Set the endpoint Receive buffer counter*/
1743 PCD_SET_EP_RX_CNT(USBx
, ep
->num
, ep
->maxpacket
);
1744 PCD_CLEAR_RX_DTOG(USBx
, ep
->num
);
1745 /* Configure VALID status for the Endpoint*/
1746 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_VALID
);
1752 /*Set the endpoint as double buffered*/
1753 PCD_SET_EP_DBUF(USBx
, ep
->num
);
1754 /*Set buffer address for double buffered mode*/
1755 PCD_SET_EP_DBUF_ADDR(USBx
, ep
->num
,ep
->pmaaddr0
, ep
->pmaaddr1
);
1759 /* Clear the data toggle bits for the endpoint IN/OUT*/
1760 PCD_CLEAR_RX_DTOG(USBx
, ep
->num
);
1761 PCD_CLEAR_TX_DTOG(USBx
, ep
->num
);
1763 /* Reset value of the data toggle bits for the endpoint out*/
1764 PCD_TX_DTOG(USBx
, ep
->num
);
1766 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_VALID
);
1767 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_DIS
);
1771 /* Clear the data toggle bits for the endpoint IN/OUT*/
1772 PCD_CLEAR_RX_DTOG(USBx
, ep
->num
);
1773 PCD_CLEAR_TX_DTOG(USBx
, ep
->num
);
1774 PCD_RX_DTOG(USBx
, ep
->num
);
1775 /* Configure DISABLE status for the Endpoint*/
1776 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_DIS
);
1777 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_DIS
);
1785 * @brief De-activate and de-initialize an endpoint
1786 * @param USBx : Selected device
1787 * @param ep: pointer to endpoint structure
1788 * @retval HAL status
1790 HAL_StatusTypeDef
USB_DeactivateEndpoint(USB_TypeDef
*USBx
, USB_EPTypeDef
*ep
)
1792 if (ep
->doublebuffer
== 0)
1796 PCD_CLEAR_TX_DTOG(USBx
, ep
->num
);
1797 /* Configure DISABLE status for the Endpoint*/
1798 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_DIS
);
1802 PCD_CLEAR_RX_DTOG(USBx
, ep
->num
);
1803 /* Configure DISABLE status for the Endpoint*/
1804 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_DIS
);
1812 /* Clear the data toggle bits for the endpoint IN/OUT*/
1813 PCD_CLEAR_RX_DTOG(USBx
, ep
->num
);
1814 PCD_CLEAR_TX_DTOG(USBx
, ep
->num
);
1816 /* Reset value of the data toggle bits for the endpoint out*/
1817 PCD_TX_DTOG(USBx
, ep
->num
);
1819 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_DIS
);
1820 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_DIS
);
1824 /* Clear the data toggle bits for the endpoint IN/OUT*/
1825 PCD_CLEAR_RX_DTOG(USBx
, ep
->num
);
1826 PCD_CLEAR_TX_DTOG(USBx
, ep
->num
);
1827 PCD_RX_DTOG(USBx
, ep
->num
);
1828 /* Configure DISABLE status for the Endpoint*/
1829 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_DIS
);
1830 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_DIS
);
1838 * @brief USB_EPStartXfer : setup and starts a transfer over an EP
1839 * @param USBx : Selected device
1840 * @param ep: pointer to endpoint structure
1841 * @retval HAL status
1843 HAL_StatusTypeDef
USB_EPStartXfer(USB_TypeDef
*USBx
, USB_EPTypeDef
*ep
)
1845 uint16_t pmabuffer
= 0;
1846 uint32_t len
= ep
->xfer_len
;
1851 /*Multi packet transfer*/
1852 if (ep
->xfer_len
> ep
->maxpacket
)
1863 /* configure and validate Tx endpoint */
1864 if (ep
->doublebuffer
== 0)
1866 USB_WritePMA(USBx
, ep
->xfer_buff
, ep
->pmaadress
, len
);
1867 PCD_SET_EP_TX_CNT(USBx
, ep
->num
, len
);
1871 /* Write the data to the USB endpoint */
1872 if (PCD_GET_ENDPOINT(USBx
, ep
->num
)& USB_EP_DTOG_TX
)
1874 /* Set the Double buffer counter for pmabuffer1 */
1875 PCD_SET_EP_DBUF1_CNT(USBx
, ep
->num
, ep
->is_in
, len
);
1876 pmabuffer
= ep
->pmaaddr1
;
1880 /* Set the Double buffer counter for pmabuffer0 */
1881 PCD_SET_EP_DBUF0_CNT(USBx
, ep
->num
, ep
->is_in
, len
);
1882 pmabuffer
= ep
->pmaaddr0
;
1884 USB_WritePMA(USBx
, ep
->xfer_buff
, pmabuffer
, len
);
1885 PCD_FreeUserBuffer(USBx
, ep
->num
, ep
->is_in
);
1888 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_VALID
);
1890 else /* OUT endpoint */
1892 /* Multi packet transfer*/
1893 if (ep
->xfer_len
> ep
->maxpacket
)
1904 /* configure and validate Rx endpoint */
1905 if (ep
->doublebuffer
== 0)
1907 /*Set RX buffer count*/
1908 PCD_SET_EP_RX_CNT(USBx
, ep
->num
, len
);
1912 /*Set the Double buffer counter*/
1913 PCD_SET_EP_DBUF_CNT(USBx
, ep
->num
, ep
->is_in
, len
);
1916 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_VALID
);
1923 * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
1924 * with the EP/channel
1925 * @param USBx : Selected device
1926 * @param src : pointer to source buffer
1927 * @param ch_ep_num : endpoint or host channel number
1928 * @param len : Number of bytes to write
1929 * @retval HAL status
1931 HAL_StatusTypeDef
USB_WritePacket(USB_TypeDef
*USBx
, uint8_t *src
, uint8_t ch_ep_num
, uint16_t len
)
1933 /* Prevent unused argument(s) compilation warning */
1938 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1939 only by USB OTG FS peripheral.
1940 - This function is added to ensure compatibility across platforms.
1946 * @brief USB_ReadPacket : read a packet from the Tx FIFO associated
1947 * with the EP/channel
1948 * @param USBx : Selected device
1949 * @param dest : destination pointer
1950 * @param len : Number of bytes to read
1951 * @retval pointer to destination buffer
1953 void *USB_ReadPacket(USB_TypeDef
*USBx
, uint8_t *dest
, uint16_t len
)
1955 /* Prevent unused argument(s) compilation warning */
1959 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1960 only by USB OTG FS peripheral.
1961 - This function is added to ensure compatibility across platforms.
1963 return ((void *)NULL
);
1967 * @brief USB_EPSetStall : set a stall condition over an EP
1968 * @param USBx : Selected device
1969 * @param ep: pointer to endpoint structure
1970 * @retval HAL status
1972 HAL_StatusTypeDef
USB_EPSetStall(USB_TypeDef
*USBx
, USB_EPTypeDef
*ep
)
1976 /* This macro sets STALL status for RX & TX*/
1977 PCD_SET_EP_TXRX_STATUS(USBx
, ep
->num
, USB_EP_RX_STALL
, USB_EP_TX_STALL
);
1983 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_STALL
);
1987 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_STALL
);
1994 * @brief USB_EPClearStall : Clear a stall condition over an EP
1995 * @param USBx : Selected device
1996 * @param ep: pointer to endpoint structure
1997 * @retval HAL status
1999 HAL_StatusTypeDef
USB_EPClearStall(USB_TypeDef
*USBx
, USB_EPTypeDef
*ep
)
2003 PCD_CLEAR_TX_DTOG(USBx
, ep
->num
);
2004 PCD_SET_EP_TX_STATUS(USBx
, ep
->num
, USB_EP_TX_VALID
);
2008 PCD_CLEAR_RX_DTOG(USBx
, ep
->num
);
2009 PCD_SET_EP_RX_STATUS(USBx
, ep
->num
, USB_EP_RX_VALID
);
2015 * @brief USB_StopDevice : Stop the usb device mode
2016 * @param USBx : Selected device
2017 * @retval HAL status
2019 HAL_StatusTypeDef
USB_StopDevice(USB_TypeDef
*USBx
)
2021 /* disable all interrupts and force USB reset */
2022 USBx
->CNTR
= USB_CNTR_FRES
;
2024 /* clear interrupt status register */
2027 /* switch-off device */
2028 USBx
->CNTR
= (USB_CNTR_FRES
| USB_CNTR_PDWN
);
2034 * @brief USB_SetDevAddress : Stop the usb device mode
2035 * @param USBx : Selected device
2036 * @param address : new device address to be assigned
2037 * This parameter can be a value from 0 to 255
2038 * @retval HAL status
2040 HAL_StatusTypeDef
USB_SetDevAddress (USB_TypeDef
*USBx
, uint8_t address
)
2044 /* set device address and enable function */
2045 USBx
->DADDR
= USB_DADDR_EF
;
2052 * @brief USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
2053 * @param USBx : Selected device
2054 * @retval HAL status
2056 HAL_StatusTypeDef
USB_DevConnect (USB_TypeDef
*USBx
)
2058 /* Prevent unused argument(s) compilation warning */
2060 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2061 only by USB OTG FS peripheral.
2062 - This function is added to ensure compatibility across platforms.
2068 * @brief USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
2069 * @param USBx : Selected device
2070 * @retval HAL status
2072 HAL_StatusTypeDef
USB_DevDisconnect (USB_TypeDef
*USBx
)
2074 /* Prevent unused argument(s) compilation warning */
2076 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2077 only by USB OTG FS peripheral.
2078 - This function is added to ensure compatibility across platforms.
2084 * @brief USB_ReadInterrupts: return the global USB interrupt status
2085 * @param USBx : Selected device
2086 * @retval HAL status
2088 uint32_t USB_ReadInterrupts (USB_TypeDef
*USBx
)
2090 uint32_t tmpreg
= 0;
2092 tmpreg
= USBx
->ISTR
;
2097 * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
2098 * @param USBx : Selected device
2099 * @retval HAL status
2101 uint32_t USB_ReadDevAllOutEpInterrupt (USB_TypeDef
*USBx
)
2103 /* Prevent unused argument(s) compilation warning */
2105 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2106 only by USB OTG FS peripheral.
2107 - This function is added to ensure compatibility across platforms.
2113 * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
2114 * @param USBx : Selected device
2115 * @retval HAL status
2117 uint32_t USB_ReadDevAllInEpInterrupt (USB_TypeDef
*USBx
)
2119 /* Prevent unused argument(s) compilation warning */
2121 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2122 only by USB OTG FS peripheral.
2123 - This function is added to ensure compatibility across platforms.
2129 * @brief Returns Device OUT EP Interrupt register
2130 * @param USBx : Selected device
2131 * @param epnum : endpoint number
2132 * This parameter can be a value from 0 to 15
2133 * @retval Device OUT EP Interrupt register
2135 uint32_t USB_ReadDevOutEPInterrupt (USB_TypeDef
*USBx
, uint8_t epnum
)
2137 /* Prevent unused argument(s) compilation warning */
2140 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2141 only by USB OTG FS peripheral.
2142 - This function is added to ensure compatibility across platforms.
2148 * @brief Returns Device IN EP Interrupt register
2149 * @param USBx : Selected device
2150 * @param epnum : endpoint number
2151 * This parameter can be a value from 0 to 15
2152 * @retval Device IN EP Interrupt register
2154 uint32_t USB_ReadDevInEPInterrupt (USB_TypeDef
*USBx
, uint8_t epnum
)
2156 /* Prevent unused argument(s) compilation warning */
2159 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2160 only by USB OTG FS peripheral.
2161 - This function is added to ensure compatibility across platforms.
2167 * @brief USB_ClearInterrupts: clear a USB interrupt
2168 * @param USBx : Selected device
2169 * @param interrupt : interrupt flag
2172 void USB_ClearInterrupts (USB_TypeDef
*USBx
, uint32_t interrupt
)
2174 /* Prevent unused argument(s) compilation warning */
2177 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2178 only by USB OTG FS peripheral.
2179 - This function is added to ensure compatibility across platforms.
2184 * @brief Prepare the EP0 to start the first control setup
2185 * @param USBx : Selected device
2186 * @param psetup : pointer to setup packet
2187 * @retval HAL status
2189 HAL_StatusTypeDef
USB_EP0_OutStart(USB_TypeDef
*USBx
, uint8_t *psetup
)
2191 /* Prevent unused argument(s) compilation warning */
2194 /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2195 only by USB OTG FS peripheral.
2196 - This function is added to ensure compatibility across platforms.
2202 * @brief USB_ActivateRemoteWakeup : active remote wakeup signalling
2203 * @param USBx : Selected device
2204 * @retval HAL status
2206 HAL_StatusTypeDef
USB_ActivateRemoteWakeup(USB_TypeDef
*USBx
)
2208 USBx
->CNTR
|= USB_CNTR_RESUME
;
2214 * @brief USB_DeActivateRemoteWakeup : de-active remote wakeup signalling
2215 * @param USBx : Selected device
2216 * @retval HAL status
2218 HAL_StatusTypeDef
USB_DeActivateRemoteWakeup(USB_TypeDef
*USBx
)
2220 USBx
->CNTR
&= ~(USB_CNTR_RESUME
);
2225 * @brief Copy a buffer from user memory area to packet memory area (PMA)
2226 * @param USBx : pointer to USB register.
2227 * @param pbUsrBuf : pointer to user memory area.
2228 * @param wPMABufAddr : address into PMA.
2229 * @param wNBytes : number of bytes to be copied.
2232 void USB_WritePMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
)
2234 uint32_t nbytes
= (wNBytes
+ 1) >> 1; /* nbytes = (wNBytes + 1) / 2 */
2235 uint32_t index
= 0, temp1
= 0, temp2
= 0;
2236 uint16_t *pdwVal
= NULL
;
2238 pdwVal
= (uint16_t *)(wPMABufAddr
* 2 + (uint32_t)USBx
+ 0x400);
2239 for (index
= nbytes
; index
!= 0; index
--)
2241 temp1
= (uint16_t) * pbUsrBuf
;
2243 temp2
= temp1
| (uint16_t) * pbUsrBuf
<< 8;
2251 * @brief Copy a buffer from user memory area to packet memory area (PMA)
2252 * @param USBx : pointer to USB register.
2253 * @param pbUsrBuf : pointer to user memory area.
2254 * @param wPMABufAddr : address into PMA.
2255 * @param wNBytes : number of bytes to be copied.
2258 void USB_ReadPMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
)
2260 uint32_t nbytes
= (wNBytes
+ 1) >> 1;/* /2*/
2262 uint32_t *pdwVal
= NULL
;
2264 pdwVal
= (uint32_t *)(wPMABufAddr
* 2 + (uint32_t)USBx
+ 0x400);
2265 for (index
= nbytes
; index
!= 0; index
--)
2267 *(uint16_t*)pbUsrBuf
++ = *pdwVal
++;
2281 #if defined (USB_OTG_FS)
2282 /** @addtogroup USB_LL_Private_Functions
2286 * @brief Reset the USB Core (needed after USB clock settings change)
2287 * @param USBx : Selected device
2288 * @retval HAL status
2290 static HAL_StatusTypeDef
USB_CoreReset(USB_OTG_GlobalTypeDef
*USBx
)
2294 /* Wait for AHB master IDLE state. */
2297 if (++count
> 200000)
2302 while ((USBx
->GRSTCTL
& USB_OTG_GRSTCTL_AHBIDL
) == 0);
2304 /* Core Soft Reset */
2306 USBx
->GRSTCTL
|= USB_OTG_GRSTCTL_CSRST
;
2310 if (++count
> 200000)
2315 while ((USBx
->GRSTCTL
& USB_OTG_GRSTCTL_CSRST
) == USB_OTG_GRSTCTL_CSRST
);
2322 #endif /* USB_OTG_FS */
2324 #endif /* STM32F102x6 || STM32F102xB || */
2325 /* STM32F103x6 || STM32F103xB || */
2326 /* STM32F103xE || STM32F103xG || */
2327 /* STM32F105xC || STM32F107xC */
2329 #endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
2338 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/