static void CRS_Init(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; HAL_RCC_GetOscConfig(&RCC_OscInitStruct); if (RCC_OscInitStruct.HSI48State != RCC_HSI48_ON) { /* Enable HSI48 and LSE Oscillator*/ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } } /* Enable CRS clock*/ __HAL_RCC_CRS_CLK_ENABLE(); /* Output HSI48 MCO pin(PA8) */ HAL_RCC_MCOConfig(RCC_MCO, RCC_MCOSOURCE_HSI48, RCC_MCO_DIV1); }
static void CRS_Init(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; HAL_RCC_GetOscConfig(&RCC_OscInitStruct); if (RCC_OscInitStruct.HSI48State != RCC_HSI48_ON) { /* Enable HSI48 and LSE Oscillator*/ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } } /* Enable CRS clock*/ __HAL_RCC_CRS_CLK_ENABLE(); /* Enable and set RCC/CRS Interrupt*/ HAL_NVIC_SetPriority(RCC_CRS_IRQn, 0x03, 0x00); HAL_NVIC_EnableIRQ(RCC_CRS_IRQn); /* Output HSI48 MCO pin(PA8) */ HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_HSI48, RCC_MCODIV_1); }
/** * @brief USB Clock Configuration: Enable Resources * @param None * @retval None */ void USBClock_Enable(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; static RCC_CRSInitTypeDef RCC_CRSInitStruct; /* Enable HSI48 Oscillator to be used as USB clock source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48; RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; HAL_RCC_OscConfig(&RCC_OscInitStruct); /* Select HSI48 as USB clock source */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); /*Configure the clock recovery system (CRS)**********************************/ /*Enable CRS Clock*/ __HAL_RCC_CRS_CLK_ENABLE(); /* Default Synchro Signal division factor (not divided) */ RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1; /* Set the SYNCSRC[1:0] bits according to CRS_Source value */ RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB; /* HSI48 is synchronized with USB SOF at 1KHz rate */ RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000); RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT; /* Set the TRIM[5:0] to the default value*/ RCC_CRSInitStruct.HSI48CalibrationValue = 0x20; /* Start automatic synchronization */ HAL_RCCEx_CRSConfig (&RCC_CRSInitStruct); }
/** * @brief System Clock Configuration * The system Clock is configured as follow: * * HSI48 used as USB clock source (USE_USB_CLKSOURCE_CRSHSI48 defined in main.h) * - System Clock source = HSI48 * - SYSCLK(Hz) = 48000000 * - HCLK(Hz) = 48000000 * - AHB Prescaler = 1 * - APB1 Prescaler = 1 * - Flash Latency(WS) = 1 * * - PLL(HSE) used as USB clock source (USE_USB_CLKSOURCE_PLL defined in main.h) * - System Clock source = PLL (HSE) * - SYSCLK(Hz) = 48000000 * - HCLK(Hz) = 48000000 * - AHB Prescaler = 1 * - APB1 Prescaler = 1 * - HSE Frequency(Hz) = 8000000 * - PREDIV = 1 * - PLLMUL = 6 * - Flash Latency(WS) = 1 * * @param None * @retval None */ static void SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; #if defined (USE_USB_CLKSOURCE_CRSHSI48) static RCC_CRSInitTypeDef RCC_CRSInitStruct; #endif #if defined (USE_USB_CLKSOURCE_CRSHSI48) /* Enable HSI48 Oscillator to be used as system clock source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48; HAL_RCC_OscConfig(&RCC_OscInitStruct); /* Select HSI48 as USB clock source */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); /* Select HSI48 as system clock source and configure the HCLK and PCLK1 clock dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1); /*Configure the clock recovery system (CRS)**********************************/ /*Enable CRS Clock*/ __HAL_RCC_CRS_CLK_ENABLE(); /* Default Synchro Signal division factor (not divided) */ RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1; /* Set the SYNCSRC[1:0] bits according to CRS_Source value */ RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB; /* HSI48 is synchronized with USB SOF at 1KHz rate */ RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_CALCULATE_RELOADVALUE(48000000, 1000); RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT; /* Set the TRIM[5:0] to the default value*/ RCC_CRSInitStruct.HSI48CalibrationValue = 0x20; /* Start automatic synchronization */ HAL_RCCEx_CRSConfig (&RCC_CRSInitStruct); #elif defined (USE_USB_CLKSOURCE_PLL) /* Enable HSE Oscillator and activate PLL with HSE as source PLLCLK = (8 * 6) / 1) = 48 MHz */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6; RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1; HAL_RCC_OscConfig(&RCC_OscInitStruct); /*Select PLL 48 MHz output as USB clock source */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLLCLK; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); /* Select PLL as system clock source and configure the HCLK and PCLK1 clock dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1); #endif /*USE_USB_CLKSOURCE_CRSHSI48*/ }