7 years, 5 months ago.

How do I change the bloody clock frequency.

Hello everyone. I have a Nucleo-F446RE. It is currently running at 180 MHz, however, I would like to run it much slower, as it is effecting the GPS lock in a project (too noisy). I managed to copy and paste some code form STM32CUBE to generate around 16 MHz. Kind of worked, I got a GPS lock but the rest of the code, ie output to the PC terminal is not working and I don't really know what I'm doing, just guessing. What is the easiest and best way to change the frequency in mbed. I can't find it. Regards Mark. Thanks.

Question relating to:

Affordable and flexible platform to ease prototyping using a STM32F446RET6 microcontroller.

Since I can't comment on my comment :( If the baud is not working then one of the peripheral clocks is getting the wrong clock value. Here is how I made mine run at 50MHz out-of-reset

50MHZ changes for F446RE in SetSysClock_PLL_HSE

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;             // VCO input clock = 2 MHz (8 MHz / 4)
  RCC_OscInitStruct.PLL.PLLN = 50;           // VCO output clock = 100 MHz (2 MHz * 50)
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLLCLK = 180 MHz (360 MHz / 2)
  RCC_OscInitStruct.PLL.PLLQ = 2;             // 
  RCC_OscInitStruct.PLL.PLLR = 2;             // 
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    return 0; // FAIL
  }

  // Activate the OverDrive to reach the 180 MHz Frequency
  if (HAL_PWREx_ActivateOverDrive() != HAL_OK)
  {
    return 0; // FAIL
  }
  
  // Select PLLSAI output as USB clock source
  PeriphClkInitStruct.PLLSAI.PLLSAIM = 16;
  PeriphClkInitStruct.PLLSAI.PLLSAIN = 192;
  PeriphClkInitStruct.PLLSAI.PLLSAIP = RCC_PLLSAIP_DIV2;
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CK48;
  PeriphClkInitStruct.Clk48ClockSelection = RCC_CK48CLKSOURCE_PLLSAIP;
  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  
  // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 180 MHz
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;  //  45 MHz
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  //  90 MHz
posted by Bill Bellis 07 Jul 2017

1 Answer

7 years, 5 months ago.

There is no mbed API for doing this so you're going to need to call one or more of the ST HAL functions.

If you want the clock to be set from out-of-reset then you will need to alter the system_stm32f4xx.c defines for the various clock registers. Or you can call the HAL_RCC_ClockConfig function after you enter main.

The sources for these can be pulled in from "mbed-dev" if you are running mbed2. If you want to run mbed5 then the sources are on GitHub. There are some good threads on here on how to pull these in locally into your project....

The other thing you might need to do is download STM32CubeMX from ST.com and open the pre-defined project for the F446RE Nucleo board. From there click on the clock configuration and get the correct low-level divider values you are going to need.

Hopefully that points you in the right direction for now...

Accepted Answer

Dear Bill, Thank you. I was using STM32CubeMX to get the correct values, and plan on using it as a visual tool to make sure they are correct until I get used to the settings. It compiles and my GPS now instantly get's a lock but the pc baud is not at say 9600 any more. Thanks for your direction I will check out GitHub and mbed-dev after looking in system_stm32f4xx.c . Regards Mark.

I just checked out "system_stm32f4xx.c" I think all the answers I need are there. That was easy, thanks again Bill. Regards Mark

posted by Mark Joyce 07 Jul 2017