Skip to content

Commit 2d29449

Browse files
committed
DISCO_L072CZ_LRWAN1: Update HSI clock config for USB
1 parent 24d94c8 commit 2d29449

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/device/system_stm32l0xx.c

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,23 @@ uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
405405
/******************************************************************************/
406406
uint8_t SetSysClock_PLL_HSI(void)
407407
{
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();
410419

411420
/* 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;
413422
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
414423
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)
417424
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; /* For USB and RNG clock */
418-
#endif
419425
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
420426
// PLLCLK = (16 MHz * 6)/3 = 32 MHz
421427
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
@@ -427,14 +433,10 @@ uint8_t SetSysClock_PLL_HSI(void)
427433
return 0; // FAIL
428434
}
429435

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);
438440

439441
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
440442
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)
447449
return 0; // FAIL
448450
}
449451

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+
450467
/* Output clock on MCO1 pin(PA8) for debugging purpose */
451468
//HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz
452469

0 commit comments

Comments
 (0)