Changing the frequency of the STM32F4

CubeMX screenshot

Working on a project with the OV7670 I wanted to be sure of the value of the clock on the STM32F446 nucleo board. The startup file sets the clock to HSI, so 16MHz. In my case i want 160MHz so, for the clock i feed an external HSE clock (8MHz) in to the PLL VCO ( settings used are 4, 160, 2, 2, 2). I am doing this for the first time on the STM32F4 family micro controller so i searched the Internet for the correct routine. I came across the following code example:

(source)

This code uses also the HSE clock as input in to the PLL. The MCU was freezing when entering the SystemCodeClockUpdate function. I thought that it has something to do with the clock setup, i lowered the clock and tried again, still no success. After this I commented all out but not the last two functions. I changed the system clock to the HSI from PLLCLK and tried again. Now it is working, 16MHz, but working.

Continuing my search I found a good hint, compare my code to the one of the CUBEmx software from ST. Downloaded and installed the software, configured it for the F446. After generating the code i get this:

i found this line interesting:

This line sets the number of wait states for accessing the flash memory on higher frequencies. In the case of the F446 this value can be found in the reference manual (RM0390) under section “3.4.1¬†Relation between CPU clock frequency and Flash memory read time”. Here it is stated that for a frequency of 160MHz i need to set the wait state to 5 (if VCC is¬†2.7 V – 3.6 V). There is also a list of steps for increasing or decreasing the frequency.

The final code for a 160MHz system clock driven by PLLCLK , and that driven by the HSE is:

If there are any questions please leave them in the comments.

Add a Comment

Your email address will not be published. Required fields are marked *