4444 *
4545 ******************************************************************************
4646 */
47-
47+ #ifdef USBCON
4848/* Includes ------------------------------------------------------------------*/
49- #include "main.h"
49+ #include "usbd_conf.h"
50+ #include "usbd_core.h"
51+ #include "hw_config.h"
5052
5153/* Private typedef -----------------------------------------------------------*/
5254/* Private define ------------------------------------------------------------*/
5355/* Private macro -------------------------------------------------------------*/
5456/* Private variables ---------------------------------------------------------*/
55- PCD_HandleTypeDef hpcd ;
57+ PCD_HandleTypeDef g_hpcd ;
5658__IO uint32_t remotewakeupon = 0 ;
5759
5860/* Private function prototypes -----------------------------------------------*/
59- static void SystemClockConfig_STOP (void );
6061/* Private functions ---------------------------------------------------------*/
6162
6263/*******************************************************************************
@@ -72,6 +73,9 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
7273{
7374 GPIO_InitTypeDef GPIO_InitStruct ;
7475
76+ /* Enable USB power on Pwrctrl CR2 register */
77+ HAL_PWREx_EnableVddUSB ();
78+
7579 /* Configure USB FS GPIOs */
7680 __HAL_RCC_GPIOA_CLK_ENABLE ();
7781
@@ -96,11 +100,21 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
96100 GPIO_InitStruct .Alternate = GPIO_AF10_OTG_FS ;
97101 HAL_GPIO_Init (GPIOA , & GPIO_InitStruct );
98102
103+ /* Configure power enable pin (USB_OTG_FS_PWR_EN) */
104+ __HAL_RCC_GPIOD_CLK_ENABLE ();
105+ GPIO_InitStruct .Pin = GPIO_PIN_12 ;
106+ GPIO_InitStruct .Mode = GPIO_MODE_OUTPUT_PP ;
107+ GPIO_InitStruct .Pull = GPIO_PULLUP ;
108+ HAL_GPIO_Init (GPIOD , & GPIO_InitStruct );
109+
110+ /* USB power output is disabled in device mode */
111+ HAL_GPIO_WritePin (GPIOD , GPIO_PIN_12 , GPIO_PIN_SET );
112+
99113 /* Enable USB FS Clock */
100114 __HAL_RCC_USB_OTG_FS_CLK_ENABLE ();
101115
102116 /* Set USB FS Interrupt priority */
103- HAL_NVIC_SetPriority (OTG_FS_IRQn , 0x0F , 0 );
117+ HAL_NVIC_SetPriority (OTG_FS_IRQn , 5 , 0 );
104118
105119 /* Enable USB FS Interrupt */
106120 HAL_NVIC_EnableIRQ (OTG_FS_IRQn );
@@ -177,11 +191,32 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
177191 */
178192void HAL_PCD_ResetCallback (PCD_HandleTypeDef * hpcd )
179193{
194+ USBD_SpeedTypeDef speed = USBD_SPEED_FULL ;
195+
196+ /* Set USB Current Speed */
197+ switch (hpcd -> Init .speed )
198+ {
199+ //Not supported on STM32L4xx boards
200+ #ifndef STM32L4xx
201+ case PCD_SPEED_HIGH :
202+ speed = USBD_SPEED_HIGH ;
203+ break ;
204+ #endif
205+
206+ case PCD_SPEED_FULL :
207+ speed = USBD_SPEED_FULL ;
208+ break ;
209+
210+ default :
211+ speed = USBD_SPEED_FULL ;
212+ break ;
213+ }
214+
180215 /* Reset Device */
181216 USBD_LL_Reset (hpcd -> pData );
182217
183218 /* Set USB Current Speed */
184- USBD_LL_SetSpeed (hpcd -> pData , USBD_SPEED_FULL );
219+ USBD_LL_SetSpeed (hpcd -> pData , speed );
185220}
186221
187222/**
@@ -213,7 +248,9 @@ void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
213248{
214249 if ((hpcd -> Init .low_power_enable )&& (remotewakeupon == 0 ))
215250 {
216- SystemClockConfig_STOP ();
251+ /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
252+ PLL as system clock source (HSE and PLL are disabled in STOP mode) */
253+ SystemClock_Config ();
217254
218255 /* Reset SLEEPDEEP bit of Cortex System Control Register */
219256 SCB -> SCR &= (uint32_t )~((uint32_t )(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk ));
@@ -266,6 +303,17 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
266303}
267304
268305
306+
307+ /**
308+ * @brief This function handles USB-On-The-Go FS global interrupt request.
309+ * @param None
310+ * @retval None
311+ */
312+ void OTG_FS_IRQHandler (void )
313+ {
314+ HAL_PCD_IRQHandler (& g_hpcd );
315+ }
316+
269317/*******************************************************************************
270318 LL Driver Interface (USB Device Library --> PCD)
271319*******************************************************************************/
@@ -278,28 +326,29 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
278326USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef * pdev )
279327{
280328 /* Set LL Driver parameters */
281- hpcd .Instance = USB_OTG_FS ;
282- hpcd .Init .dev_endpoints = 5 ;
283- hpcd .Init .use_dedicated_ep1 = 0 ;
284- hpcd .Init .ep0_mps = 0x40 ;
285- hpcd .Init .dma_enable = 0 ;
286- hpcd .Init .low_power_enable = 1 ;
287- hpcd .Init .lpm_enable = 0 ;
288- hpcd .Init .battery_charging_enable = 0 ;
289- hpcd .Init .phy_itface = PCD_PHY_EMBEDDED ;
290- hpcd .Init .Sof_enable = 0 ;
291- hpcd .Init .speed = PCD_SPEED_FULL ;
292- hpcd .Init .vbus_sensing_enable = 1 ;
329+ g_hpcd .Instance = USB_OTG_FS ;
330+ g_hpcd .Init .dev_endpoints = 4 ;
331+ g_hpcd .Init .use_dedicated_ep1 = 0 ;
332+ g_hpcd .Init .ep0_mps = 0x40 ;
333+ g_hpcd .Init .dma_enable = 0 ;
334+ g_hpcd .Init .low_power_enable = 0 ;
335+ g_hpcd .Init .lpm_enable = 0 ;
336+ g_hpcd .Init .battery_charging_enable = 0 ;
337+ g_hpcd .Init .phy_itface = PCD_PHY_EMBEDDED ;
338+ g_hpcd .Init .Sof_enable = 0 ;
339+ g_hpcd .Init .speed = PCD_SPEED_FULL ;
340+ g_hpcd .Init .vbus_sensing_enable = 1 ;
293341 /* Link The driver to the stack */
294- hpcd .pData = pdev ;
295- pdev -> pData = & hpcd ;
342+ g_hpcd .pData = pdev ;
343+ pdev -> pData = & g_hpcd ;
296344 /* Initialize LL Driver */
297- HAL_PCD_Init (& hpcd );
345+ HAL_PCD_Init (& g_hpcd );
298346
299347 /* configure EPs FIFOs */
300- HAL_PCDEx_SetRxFiFo (& hpcd , 0x80 );
301- HAL_PCDEx_SetTxFiFo (& hpcd , 0 , 0x40 );
302- HAL_PCDEx_SetTxFiFo (& hpcd , 1 , 0x80 );
348+ HAL_PCDEx_SetRxFiFo (& g_hpcd , 0x80 );
349+ HAL_PCDEx_SetTxFiFo (& g_hpcd , 0 , 0x40 );
350+ HAL_PCDEx_SetTxFiFo (& g_hpcd , 1 , 0x10 );
351+ HAL_PCDEx_SetTxFiFo (& g_hpcd , 2 , 0x10 );
303352
304353 return USBD_OK ;
305354}
@@ -483,120 +532,6 @@ uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
483532 return HAL_PCD_EP_GetRxCount (pdev -> pData , ep_addr );
484533}
485534
486- /**
487- * @brief Configures system clock after wakeup from STOP mode.
488- * @param None
489- * @retval None
490- */
491- static void SystemClockConfig_STOP (void )
492- {
493- #if defined (USB_USE_LSE_MSI_CLOCK )
494-
495- /* Configures system clock after wake-up from STOP: enable LSE, PLL and select
496- PLL as system clock source (LSE and PLL are disabled in STOP mode) */
497-
498- __HAL_RCC_LSE_CONFIG (RCC_LSE_ON );
499-
500- /* Wait till HSE is ready */
501- while (__HAL_RCC_GET_FLAG (RCC_FLAG_LSERDY ) == RESET )
502- {}
503-
504- /* Enable the main PLL. */
505- __HAL_RCC_PLL_ENABLE ();
506-
507- /* Wait till PLL is ready */
508- while (__HAL_RCC_GET_FLAG (RCC_FLAG_PLLRDY ) == RESET )
509- {}
510-
511- /* Select PLL as SYSCLK */
512- MODIFY_REG (RCC -> CFGR , RCC_CFGR_SW , RCC_SYSCLKSOURCE_PLLCLK );
513-
514- /* Wait till system clock switch to PLL */
515- while (__HAL_RCC_GET_SYSCLK_SOURCE () != RCC_CFGR_SWS_PLL )
516- {}
517-
518- #elif defined (USB_USE_HSE_CLOCK )
519- RCC_PeriphCLKInitTypeDef PeriphClkInitStruct ;
520- /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
521- PLL as system clock source (HSE and PLL are disabled in STOP mode) */
522-
523- __HAL_RCC_HSE_CONFIG (RCC_HSE_ON );
524-
525- /* Wait till HSE is ready */
526- while (__HAL_RCC_GET_FLAG (RCC_FLAG_HSERDY ) == RESET )
527- {}
528-
529- /* Enable the main PLL. */
530- __HAL_RCC_PLL_ENABLE ();
531-
532- /* Wait till PLL is ready */
533- while (__HAL_RCC_GET_FLAG (RCC_FLAG_PLLRDY ) == RESET )
534- {}
535-
536- /* Select PLL as SYSCLK */
537- MODIFY_REG (RCC -> CFGR , RCC_CFGR_SW , RCC_SYSCLKSOURCE_PLLCLK );
538-
539- /* Wait till system clock switch to PLL */
540- while (__HAL_RCC_GET_SYSCLK_SOURCE () != RCC_CFGR_SWS_PLL )
541- {}
542-
543- /* Select PLLSAI output as USB clock source */
544- PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_USB ;
545- PeriphClkInitStruct .UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1 ;
546- PeriphClkInitStruct .PLLSAI1 .PLLSAI1N = 24 ;
547- PeriphClkInitStruct .PLLSAI1 .PLLSAI1Q = 4 ;
548- PeriphClkInitStruct .PLLSAI1 .PLLSAI1P = 1 ;
549- PeriphClkInitStruct .PLLSAI1 .PLLSAI1M = 1 ;
550- PeriphClkInitStruct .PLLSAI1 .PLLSAI1Source = RCC_PLLSOURCE_HSE ;
551- PeriphClkInitStruct .PLLSAI1 .PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK ;
552- HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
553-
554- #endif /* USB_USE_LSE_MSI_CLOCK */
555- }
556-
557- /**
558- * @brief GPIO EXTI Callback function
559- * Handle remote-wakeup through Tamper button
560- * @param GPIO_Pin
561- * @retval None
562- */
563- void HAL_GPIO_EXTI_Callback (uint16_t GPIO_Pin )
564- {
565- if (GPIO_Pin == GPIO_PIN_13 )
566- {
567- if ((((USBD_HandleTypeDef * )hpcd .pData )-> dev_remote_wakeup == 1 )&&
568- (((USBD_HandleTypeDef * )hpcd .pData )-> dev_state == USBD_STATE_SUSPENDED ))
569- {
570- if ((& hpcd )-> Init .low_power_enable )
571- {
572- /* Reset SLEEPDEEP bit of Cortex System Control Register */
573- SCB -> SCR &= (uint32_t )~((uint32_t )(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk ));
574-
575- SystemClockConfig_STOP ();
576- }
577-
578- /* Ungate PHY clock */
579- __HAL_PCD_UNGATE_PHYCLOCK ((& hpcd ));
580-
581- /* Activate Remote wakeup */
582- HAL_PCD_ActivateRemoteWakeup ((& hpcd ));
583-
584- /* Remote wakeup delay */
585- HAL_Delay (10 );
586-
587- /* Disable Remote wakeup */
588- HAL_PCD_DeActivateRemoteWakeup ((& hpcd ));
589-
590- /* change state to configured */
591- ((USBD_HandleTypeDef * )hpcd .pData )-> dev_state = USBD_STATE_CONFIGURED ;
592-
593- /* Change remote_wakeup feature to 0*/
594- ((USBD_HandleTypeDef * )hpcd .pData )-> dev_remote_wakeup = 0 ;
595- remotewakeupon = 1 ;
596- }
597- }
598- }
599-
600535/**
601536 * @brief Delays routine for the USB Device Library.
602537 * @param Delay: Delay in ms
@@ -607,4 +542,5 @@ void USBD_LL_Delay(uint32_t Delay)
607542 HAL_Delay (Delay );
608543}
609544
545+ #endif // USBCON
610546/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
0 commit comments