9 years ago.

Can't change System clock in mbed?

I'm using an STM32 NUCLEO-L152RE.

Summary:

If I generate a template in mbed or include mbed.h in a project, any sort of system clock configuration is completely ignored and it defaults to 24MHz.

Details

I used the STM32CubeMX software to generate the appropriate function to set the clock to 1 MHz using the MSI source (although it incorrectly spits out FLASH_LATENCY_0 as part of it which needs to be fixed to 2 or ((uint32_t)0x00000002)). Anyway, no matter what way I spin it, if the mbed.h library is included, the clock will not actually change and will stay fixed at 24MHz, this is tested by outputting the system clock to PA8 and monitoring via oscilloscope as well as SystemCoreClockUpdate() and printing SystemCoreClock.

I've also played with all manner of the different clocks, prescalars, etc. All of them are always ignored.

What does work:

  • Generate code in STM32CubeMX for IAR, open, fix the flash latency, compile and load in IAR -> correctly uses 1 MHz clock.

What does not work:

  • Generate code in STM32CubeMX for IAR, upload appropriate code and libraries to mbed (Note, I'm not even including mbed.h here), fix flash latency, compile & load -> 24MHz
  • Create template project in mbed -> export to IAR -> edit in working STM32CubeMX code from above -> compile and load in IAR -> 24MHz clock

I'd really like to use all the lovely mbed abstractions and some of the user-created libraries but modifying the clock is required for my project as it's battery-powered and battery life is paramount. Is there any way to resolve this?

For reference, here's a bare template that'll work in IAR:

Code to set SystemClock to MSI clock at 1MHz

#include "stm32l1xx_hal.h"

void SystemClock_Config(void);

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  while(1) 
  {
  }

}

void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  __PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV64;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, ((uint32_t)0x00000002));

  HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);

  __SYSCFG_CLK_ENABLE();

}

1 Answer

9 years ago.

Hi,

This is normal. In mbed code the clock is configured to 24MHz for the NUCLEO_L152RE. If you want to change it, you need to do it inside this file:

/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/system_stm32l1xx.c

Functions: SetSysClock_PLL_HSE or SetSysClock_PLL_HSI

Regards.