AnsweredAssumed Answered

CubeMX generates invalid code in SystemClock_Config function

Question asked by ryabov.alexander on Dec 30, 2015
Latest reply on Jan 1, 2016 by ryabov.alexander
Hello,
I've made a project based on stm32f103c8 mcu with keil4, init functions were generated in cubemx,
The problem is MCU system clock: it is 8 MHz with any AHB Prescaler.
System clock source = HSE, Crystal 8MHz
PLL OFF
HCLK = 8 MHz with ANY AHB prescaler :(

void SystemClock_Config(void)
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_ADC;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

Tested:
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
DIV2, DIV4, DIV8.
All the time i got 8 MHz system

BTW using manual correction is OK, working code, but cannot use it in production:
SystemClock_Config();
  RCC->CFGR|=0x90; // SYSCLK divided by 4
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  HAL_Delay(100);


Outcomes