cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED]Proper clock initialization for STM32F401RB

petoknm
Associate II
Posted on August 23, 2014 at 09:52

Hi,

I am using a custom board withSTM32F401RB 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?


void
SystemClock_Config(
void
) {


RCC_ClkInitTypeDef RCC_ClkInitStruct;

RCC_OscInitTypeDef RCC_OscInitStruct;

HAL_StatusTypeDef res;


__PWR_CLK_ENABLE();


__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);


RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLM = 8;

RCC_OscInitStruct.PLL.PLLN = 336;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

RCC_OscInitStruct.PLL.PLLQ = 7;

res=HAL_RCC_OscConfig(&RCC_OscInitStruct);


RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

res=HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);


}

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!!! #stm32-clock-delay-rcc-pll
0 REPLIES 0