consider HSIDIV flag correctly

This commit is contained in:
olikraus 2017-04-08 16:14:14 +02:00
parent a85a7d7ebb
commit 4b66f1ce36
2 changed files with 10 additions and 7 deletions

View File

@ -113,7 +113,7 @@ void delay_system_ticks(uint32_t sys_ticks)
/*
Delay by the provided number of micro seconds.
Limitation: "us" * System-Freq in MHz must now overflow in 32 bit.
Limitation: "us" * System-Freq in MHz must not overflow in 32 bit.
Values between 0 and 1.000.000 (1 second) are ok.
Important: Call SystemCoreClockUpdate() before calling this function.

View File

@ -222,18 +222,20 @@ void SystemInit (void)
void SystemCoreClockUpdate (void)
{
uint32_t tmp = 0U, pllmul = 0U, plldiv = 0U, pllsource = 0U, msirange = 0U;
uint32_t hsi_value;
/* Get SYSCLK source -------------------------------------------------------*/
tmp = RCC->CFGR & RCC_CFGR_SWS;
/* added by olikraus@gmail.com: The HSIDIV flag was not considered in the original code */
hsi_value = 16000000UL;
if ( (RCC->CR & RCC_CR_HSIDIVF) != 0 )
hsi_value /= 4;
switch (tmp)
{
case 0x00U: /* MSI used as system clock */
msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13U;
SystemCoreClock = (32768U * (1U << (msirange + 1U)));
break;
case 0x04U: /* HSI used as system clock */
SystemCoreClock = HSI_VALUE;
SystemCoreClock = hsi_value;
break;
case 0x08U: /* HSE used as system clock */
SystemCoreClock = HSE_VALUE;
@ -250,7 +252,7 @@ void SystemCoreClockUpdate (void)
if (pllsource == 0x00U)
{
/* HSI oscillator clock selected as PLL clock entry */
SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
SystemCoreClock = (((hsi_value) * pllmul) / plldiv);
}
else
{
@ -258,6 +260,7 @@ void SystemCoreClockUpdate (void)
SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
}
break;
case 0x00U: /* MSI used as system clock */
default: /* MSI used as system clock */
msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13U;
SystemCoreClock = (32768U * (1U << (msirange + 1U)));