Changing clock frequency in mbed Studio

Hello, I am currently using an STM332F303K8 on my own board and I would quite like to increase the clock frequency (as its default is 8MHz but its 72MHz capable). I am working inside Mbed Studio and using the mbed.h libary. Once the clock frequency is changed I would quite like the “wait” functions and timers to still be accurate. I am still at university so all of this is new to me. I understand its possible, but I dont know where to start.

Hello Edward,

If you are using the mbed-os library (means you are building a Mbed OS project) and you build for the NUCLEO_F303K8 target then the default clock frequency is set to 64MHz.

To check it:

  • In your Mbed Studio project open the mbed-os/targets/targets.json file and search for NUCLEO_F303K8. You’ll find something like:
    "NUCLEO_F303K8": {
        "inherits": [
            "MCU_STM32F3"
        ],
        "c_lib": "small",
        "extra_labels_add": [
            "STM32F303x8",
            "STM32F303K8"
        ],
        "overrides": {
            "clock_source": "USE_PLL_HSI",
            "lse_available": 0
        },
        "detect_code": [
            "0775"
        ],
        "device_name": "STM32F303K8"

As you can see the “USE_PLL_HSI” (High Speed Internal) clock is selected.

  • Open the mbed-os/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/TARGET_NUCLEO_F303K8/system_clock.c file.

There is the following table:

/**
  * This file configures the system clock as follows:
  *-----------------------------------------------------------------------------
  * System clock source                | 1- USE_PLL_HSE_EXTC    | 3- USE_PLL_HSI
  *                                    | (external 8 MHz clock) | (internal 8 MHz)
  *                                    | 2- USE_PLL_HSE_XTAL    |
  *                                    | (external 8 MHz xtal)  |
  *-----------------------------------------------------------------------------
  * SYSCLK(MHz)                        | 72                     | 64
  *-----------------------------------------------------------------------------
  * AHBCLK (MHz)                       | 72                     | 64
  *-----------------------------------------------------------------------------
  * APB1CLK (MHz)                      | 36                     | 32
  *-----------------------------------------------------------------------------
  * APB2CLK (MHz)                      | 72                     | 64
  *-----------------------------------------------------------------------------
  * USB capable                        | NO                     | NO
  *-----------------------------------------------------------------------------
  */

It says that when the “USE_PLL_HSI” is selected an internal 8MHz oscillator is used as the clock signal source (which is multiplied eight times using a PLL loop) so the system (MCU) clock becomes 64MHz.

If you’d like to increase the system clock from 64MHz to 72MHz then you have two options:

  1. Either connect an 8MHz clock signal coming from an external oscillator to the chip’s clock input and override the “clock_source” value in your custom target json file to USE_PLL_HSE_EXTC (High Speed External Clock).

  2. Or connect an 8MHz crystal to the chip’s clock inputs decoupled with 15pF capacitors and override the “clock_source” value in your custom target json file to USE_PLL_HSE_XTAL (High Speed External Crystal).

In both cases Mbed will take care that all timer functions will be adjusted and work correctly.

Best regards, Zoltan

1 Like

Zoltan,

Champion- found the same file and its just like yours. 64MHz it is! Ill leave it at that, not worth mucking around with it for the sake of a few extra Hz. Wrote some simple code just to loop round and round toggling an LED and I only got a frequency of around 600kHz on my scope. Seems a little low for something running at 64MHz? Unless the GPIO’s have a lower max speed.

Ill leave it at that, not worth mucking around with it for the sake of a few extra Hz.

I fully agree with you.

Wrote some simple code just to loop round and round toggling an LED and I only got a frequency of around 600kHz on my scope. Seems a little low for something running at 64MHz? Unless the GPIO’s have a lower max speed.

Again, you can go a bit higher but that requires to use HAL (Hardware Application Layer) functions. There are also some libraries which can increase the GPIO (and other API’s) speed. A search for “Fast” or “Burst” on the Mbed pages can help.

1 Like

in addition: the HSI is a RC oscillator on chip and is more temperature dependent, for higher precision a crystal will be the better solution.
When you check the clock settings with CubeMX, you can see also that USB is possible with 8 MHz crystal and 72 MHz Sysclock. This could be improved in Mbed for this target.
USB is also possible with HSI, but USB has stronger timing constraints and using a RC oscillator requires some tricks with clock synchronisation.