cancel
Showing results for 
Search instead for 
Did you mean: 

Hello Team, I would like to change the Frequency of the clock in two different threads. I tried 3 ways.

ANala.2
Associate II

Hello Team,

I would like to change the Frequency of the clock in two different threads. I tried two ways.

1.

/* Enable HSE Oscillator and activate PLL with HSE as source */

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.HSIState = RCC_HSI_OFF;

RCC_OscInitStruct.CSIState = RCC_CSI_OFF;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

2.

RCC_DeInit();

  // Enable external crystal (HSE)

RCC_HSEConfig(RCC_HSE_ON);

 // Wait until HSE ready to use or not

ErrorStatus errorStatus = RCC_WaitForHSEStartUp();

if (errorStatus == SUCCESS)

{

 // Configure the PLL for 168MHz SysClk and 48MHz for USB OTG, SDIO

 RCC_PLLConfig(RCC_PLLSource_HSE, 8, 336, 2, 7); // values changed for 144MHz

 // Enable PLL

 RCC_PLLCmd(ENABLE);

 // Wait until main PLL clock ready

 while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

 // Set flash latency

 FLASH_SetLatency(FLASH_Latency_5);

 // AHB 168MHz

 RCC_HCLKConfig(RCC_SYSCLK_Div1);

 RCC_PCLK1Config(RCC_HCLK_Div4);

 RCC_PCLK2Config(RCC_HCLK_Div2);

 // Set SysClk using PLL

 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

 }

 else

 {

  // Do something to indicate that error clock configuration

  while (1);

 }

SystemCoreClockUpdate();

Second way RCC_PLLConfig() function is not present in drivers of STM32F4xxx drivers , please provide me the link of the github code where I can get RCC_PLLConfig() function definition.

3.

void Speed_Up_Clock()

{

uint16_t timeout;

/ Enable HSI clock /

RCC->CR |= RCC_CR_HSION;

/ Wait till HSI is ready /

timeout = 0xFFFF;

while (!(RCC->CR & RCC_CR_HSIRDY) && timeout--);

/ Select HSI clock as main clock /

RCC->CFGR = (RCC->CFGR & ~(RCC_CFGR_SW)) | RCC_CFGR_SW_HSI;

/ Disable PLL /

RCC->CR &= ~RCC_CR_PLLON;

SystemClock_Config(); //This is the STM32 generated function to configure the clock originally at program start

}

void Slow_Down_Clock()

{

uint16_t timeout;

/ Enable HSI clock /

RCC->CR |= RCC_CR_HSION;

/ Wait till HSI is ready /

timeout = 0xFFFF;

while (!(RCC->CR & RCC_CR_HSIRDY) && timeout--);

/ Select HSI clock as main clock /

RCC->CFGR = (RCC->CFGR & ~(RCC_CFGR_SW)) | RCC_CFGR_SW_HSI;

/ Disable PLL /

RCC->CR &= ~RCC_CR_PLLON;

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_MUL4;

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_DIV4;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != 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);

}

The above function doesn't help me to change the Frequency of the clock, So please suggest me a way to change my clock frequency for different thread execution in freeRTOS .

Please do the needful.

Thansks and Regards

Abhijith N.M.

1 REPLY 1

Decide whether you are using SPL or HAL

Decide what clocks you want in each case, write the top level specification so you can communicate it clearly.

Do not arbitrarily disable the PLL, especially if you're running from it.

Different concurrent threads cannot run at different speeds.

Leave the HSI running, you can switch to it later.

Clear stack based automatic variables

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..