stm32l4 MSI PLL-mode (auto trimming with LSE ) stable only when VBAT supply present
Hi All,
In our product we have to avoid HSE, so decided to use MSI in PLL mode.
The below code snippet works fine for MSI in PLL-mode , when auto-trimmed with LSE.
But if the battery is say removed (The battery can get depleted in the field), the accuracy of the
MSI detoriates.
If the VBAT is available, the MSI is very stable. Not sure what i am missing.
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_OscInitTypeDef LSE_OscInitStruct = {0};
LSE_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
LSE_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
LSE_OscInitStruct.LSEState = RCC_LSE_ON;
LSE_OscInitStruct.LSIState = RCC_LSI_OFF;
// configure the oscillator
if (HAL_RCC_OscConfig(&LSE_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
// set drive level to maximum, to ensure RTC accuracy
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_HIGH);
HAL_RCCEx_DisableLSECSS();
/* Set Oscillator to MSI*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI |
RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.MSIClockRange = RCC_CR_MSIRANGE_7;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLM = 2;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 5;
RCC_OscInitStruct.PLL.PLLR = 2;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
{
// enable MSI auto-calibration through LSE
_HAL_RCC_SET_MSIPLLEN();
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) == HAL_OK)
{
__HAL_RCC_HSI_DISABLE();
//redo systick configuration
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
__SYSCFG_CLK_ENABLE();
__PWR_CLK_ENABLE();
/* Enable GPIO Clocks */
}
else
{
Error_Handler();
}
}
else
{
Error_Handler();
}
In the bootloader, there is no clock configuration apart from the call to default implementation in SystemInit().
One extra observation made was, When on VDD and no VBAT if the micro is reset (system reset) the System clock seems to be very stable.
But on Power on Reset with no VBAT, the system clock is not stable.
