@@ -1076,6 +1076,58 @@ static void SetSysClockTo72(void)
10761076 else
10771077 { /* If HSE fails to start-up, the application will have wrong clock
10781078 configuration. User can add here some code to deal with this error */
1079+
1080+ /* Fallback to internal HSI clock 8MHz. USB requires 72MHz or 48MHz due to
1081+ * devider 1 or 1.5. Maximum frequency that can be configured for PLL with
1082+ * HSI source is 64MHz so select 48MHz */
1083+
1084+ /* Disable HSE */
1085+ RCC -> CR &= ~RCC_CR_HSEON ;
1086+ /* Disable PLL */
1087+ RCC -> CR &= ~RCC_CR_PLLON ;
1088+
1089+ /* At this stage the HSI is already enabled */
1090+
1091+ /* Enable Prefetch Buffer */
1092+ FLASH -> ACR |= FLASH_ACR_PRFTBE ;
1093+
1094+ /* Flash 2 wait state */
1095+ FLASH -> ACR &= (uint32_t )((uint32_t )~FLASH_ACR_LATENCY );
1096+ FLASH -> ACR |= (uint32_t )FLASH_ACR_LATENCY_2 ;
1097+
1098+ /* HCLK = SYSCLK */
1099+ RCC -> CFGR |= (uint32_t )RCC_CFGR_HPRE_DIV1 ;
1100+
1101+ /* PCLK2 = HCLK */
1102+ RCC -> CFGR |= (uint32_t )RCC_CFGR_PPRE2_DIV1 ;
1103+
1104+ /* PCLK1 = HCLK */
1105+ RCC -> CFGR |= (uint32_t )RCC_CFGR_PPRE1_DIV2 ;
1106+
1107+ /* PLL configuration: PLLCLK = HSI/2 * 12 = 48 MHz */
1108+ RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
1109+ RCC_CFGR_PLLMULL ));
1110+ RCC -> CFGR |= (uint32_t )(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLMULL12 );
1111+
1112+ /* Enable PLL */
1113+ RCC -> CR |= RCC_CR_PLLON ;
1114+
1115+ /* Wait till PLL is ready */
1116+ while ((RCC -> CR & RCC_CR_PLLRDY ) == 0 )
1117+ {
1118+ }
1119+
1120+ /* Select PLL as system clock source */
1121+ RCC -> CFGR &= (uint32_t )((uint32_t )~(RCC_CFGR_SW ));
1122+ RCC -> CFGR |= (uint32_t )RCC_CFGR_SW_PLL ;
1123+
1124+ /* Wait till PLL is used as system clock source */
1125+ while ((RCC -> CFGR & (uint32_t )RCC_CFGR_SWS ) != (uint32_t )0x08 )
1126+ {
1127+ }
1128+
1129+ /* Update frequency */
1130+ SystemCoreClockUpdate ();
10791131 }
10801132}
10811133#endif
0 commit comments