AnsweredAssumed Answered

Changing clock speed during runtime (64MHz to 1MHz)

Question asked by Matt Roybal on Apr 11, 2018

Hello, I am wanting to change the clock speed during runtime. I have an area of the code where I want to enter a lower power mode, shut down all peripherals, and wait for an external interrupt to occur. The specific chip I am using is the STM32F303CCT6 and the STM32F301C6, and I am using STM32CubeMX to generate the projects. Here are my current clock settings from STM32CubeMX:

 

STM32CubeMX Clock Settings

And here is the code it generated in the project:

 

void SystemClock_Config(void)
{

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_TIM16|RCC_PERIPHCLK_TIM17
|RCC_PERIPHCLK_ADC1;
PeriphClkInit.Tim16ClockSelection = RCC_TIM16CLK_HCLK;
PeriphClkInit.Tim17ClockSelection = RCC_TIM17CLK_HCLK;
PeriphClkInit.Adc1ClockSelection = RCC_ADC1PLLCLK_DIV1;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

 

I was hoping I would be able to change the clock settings in STM32CubeMX by just typing in a "1" in "HCLK(MHz)", generate the code, then copy the clock settings, then revert back to 64MHZ. Then paste the clock settings in a new function and call that, thus switching to the 1MHZ clock setting. It doesn't seem to be that easy though?

 

What would be the best way to change back and forth from 64MHz and 1MHz? Any help or advice is greatly appreciated, thanks!

Outcomes