cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX generates invalid code in SystemClock_Config function

mailbox
Associate II
Posted on December 30, 2015 at 11:14

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

#stm32f103 #stm32cube #bug

3 REPLIES 3
mailbox
Associate II
Nesrine M_O
Lead II
Posted on December 30, 2015 at 12:07

Hi  ryabov.alexander

I cannot reproduce your issue,I display the SystemCoreClock variable in the KEIL watch and I get correct frequency for each AHB divider. 

For the manual correction that you did: RCC->CFGR|=0x90, it means that you use HSI instead of HSE (0x95).

Could you please share your .ioc file for check?

-Syrine-

mailbox
Associate II
Posted on December 31, 2015 at 18:27

For the manual correction that you did: RCC->CFGR|=0x90, it means that you use HSI instead of HSE (0x95).

No, it is not. ''|='' is not ''=''

Could you please share your .ioc file for check?

sure, after holiday will post it