@@ -405,17 +405,23 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
405
405
/******************************************************************************/
406
406
uint8_t SetSysClock_PLL_HSI (void )
407
407
{
408
- RCC_ClkInitTypeDef RCC_ClkInitStruct ;
409
- RCC_OscInitTypeDef RCC_OscInitStruct ;
408
+ RCC_ClkInitTypeDef RCC_ClkInitStruct = {0 };
409
+ RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
410
+ RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0 };
411
+ RCC_CRSInitTypeDef RCC_CRSInitStruct = {0 };
412
+
413
+ /* The voltage scaling allows optimizing the power consumption when the device is
414
+ clocked below the maximum system frequency, to update the voltage scaling value
415
+ regarding system frequency refer to product datasheet. */
416
+ __HAL_RCC_PWR_CLK_ENABLE ();
417
+ __HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE1 );
418
+ __HAL_RCC_PWR_CLK_DISABLE ();
410
419
411
420
/* Enable HSI and HSI48 oscillators and activate PLL with HSI as source */
412
- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48 ;
421
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48 ;
413
422
RCC_OscInitStruct .HSEState = RCC_HSE_OFF ;
414
423
RCC_OscInitStruct .HSIState = RCC_HSI_ON ;
415
- #if !defined (STM32L031xx ) && !defined (STM32L041xx ) && !defined(STM32L051xx ) && !defined(STM32L061xx ) && !defined(STM32L071xx ) && !defined(STM32L081xx ) && \
416
- !defined (STM32L011xx ) && !defined (STM32L021xx )
417
424
RCC_OscInitStruct .HSI48State = RCC_HSI48_ON ; /* For USB and RNG clock */
418
- #endif
419
425
RCC_OscInitStruct .HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT ;
420
426
// PLLCLK = (16 MHz * 6)/3 = 32 MHz
421
427
RCC_OscInitStruct .PLL .PLLState = RCC_PLL_ON ;
@@ -427,14 +433,10 @@ uint8_t SetSysClock_PLL_HSI(void)
427
433
return 0 ; // FAIL
428
434
}
429
435
430
- /* The voltage scaling allows optimizing the power consumption when the device is
431
- clocked below the maximum system frequency, to update the voltage scaling value
432
- regarding system frequency refer to product datasheet. */
433
- __PWR_CLK_ENABLE ();
434
- __HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE1 );
435
-
436
- /* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */
437
- while (__HAL_PWR_GET_FLAG (PWR_FLAG_VOS ) != RESET ) {};
436
+ /* Select HSI48 as USB clock source */
437
+ PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_USB ;
438
+ PeriphClkInitStruct .UsbClockSelection = RCC_USBCLKSOURCE_HSI48 ;
439
+ HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
438
440
439
441
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
440
442
RCC_ClkInitStruct .ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 );
@@ -447,6 +449,21 @@ uint8_t SetSysClock_PLL_HSI(void)
447
449
return 0 ; // FAIL
448
450
}
449
451
452
+ /* Configure the clock recovery system (CRS) ********************************/
453
+ /* Enable CRS Clock */
454
+ __HAL_RCC_CRS_CLK_ENABLE ();
455
+ /* Default Synchro Signal division factor (not divided) */
456
+ RCC_CRSInitStruct .Prescaler = RCC_CRS_SYNC_DIV1 ;
457
+ /* Set the SYNCSRC[1:0] bits according to CRS_Source value */
458
+ RCC_CRSInitStruct .Source = RCC_CRS_SYNC_SOURCE_USB ;
459
+ /* HSI48 is synchronized with USB SOF at 1KHz rate */
460
+ RCC_CRSInitStruct .ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE (48000000 , 1000 );
461
+ RCC_CRSInitStruct .ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT ;
462
+ /* Set the TRIM[5:0] to the default value */
463
+ RCC_CRSInitStruct .HSI48CalibrationValue = 0x20 ;
464
+ /* Start automatic synchronization */
465
+ HAL_RCCEx_CRSConfig (& RCC_CRSInitStruct );
466
+
450
467
/* Output clock on MCO1 pin(PA8) for debugging purpose */
451
468
//HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz
452
469
0 commit comments