AnsweredAssumed Answered

[SOLVED]Proper clock initialization for STM32F401RB

Question asked by Majchrak.Peter on Aug 23, 2014
Hi,
I am using a custom board with STM32F401RB MCU and I want to run it at 84MHz from PLL<-HSE configuration. I have an 8MHz crystal on the board. But I don't know how to do it. I ran the STM32CubeMX (I don't know the name of the software but it let's you configure the pins and IP and clocks and then it generates the project init for you) I selected the correct MCU I selected the correct clock and everything. But when I run the program the clock is not what I expected. I don't know what it is using as it's clock source but When I use HAL_Delay() is delays for approximately 3.12 times longer than it should.
Q1
What are all the files I need to modify in order to successfully switch to a different clock (PLL<-HSE)?
Because I noticed that the generated code contains a function which looks like it should do all of this... So do I only need to add this code to main.c and it does everything?

01.void SystemClock_Config(void) {
02. 
03.    RCC_ClkInitTypeDef RCC_ClkInitStruct;
04.    RCC_OscInitTypeDef RCC_OscInitStruct;
05.    HAL_StatusTypeDef res;
06. 
07.    __PWR_CLK_ENABLE();
08. 
09.    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
10. 
11.    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
12.    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
13.    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
14.    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
15.    RCC_OscInitStruct.PLL.PLLM = 8;
16.    RCC_OscInitStruct.PLL.PLLN = 336;
17.    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
18.    RCC_OscInitStruct.PLL.PLLQ = 7;
19.    res=HAL_RCC_OscConfig(&RCC_OscInitStruct);
20. 
21.    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
22.    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
23.    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
24.    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
25.    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
26.    res=HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
27. 
28.}

And it gets called after HAL_Init()

1.int main(void) {
2.    HAL_Init();
3.    SystemClock_Config();
4....}

I checked all the results (in SystemClock_Config) they are OK.
Q2
After this SystemClock_Config() function sets up the clock is the SysTick timer reconfigured to run at the correct frequency? Because it screws up my HAL_Delay()...

SOLUTION
Try to compile it correctly!!!

Outcomes