Skip to main content
srbhktyr
Associate
September 6, 2024
Solved

Problem with selecting HSE as system clock

  • September 6, 2024
  • 2 replies
  • 734 views

Hi Every one.

When i am trying to write CS bit of RCC_CFGR register of STM32G491RE MCU to select HSE as a system clock (i.e 10) it changes to 11 (01 is default so i guess i am not able to clear these bits before assigning new value). Can anyone tell me why this happening. Here is the code for your reference.

if(pCLK->SysClkSrc == SYS_CLK_SRC_HSE){

if((RCC->CR) &(1<<CR_HSERDY)){

 

// Set the system clock source to HSE

RCC->CFGR &= ~(1 << 0); // Set SW bits to 10 for HSE

RCC->CFGR |= (1 << 1);

}

else{

error_handle();

}

}

This topic has been closed for replies.
Best answer by waclawek.jan

> RCC->CFGR &= ~(1 << 0); // Set SW bits to 10 for HSE

At this point, you tried to write 00 into the SW bits, which is a reserved value and the hardware rejected that write. So the subsequent

> RCC->CFGR |= (1 << 1);

then just ORed 0b0 to the reset value 0b01 to result in 0b11.

Generally, avoid sequences of RMW writes to a hardware register. Registers are *not* memory words. Read the register to a temporary value, modify there what you need, and make one single write back.

JW

2 replies

waclawek.jan
waclawek.janBest answer
Super User
September 6, 2024

> RCC->CFGR &= ~(1 << 0); // Set SW bits to 10 for HSE

At this point, you tried to write 00 into the SW bits, which is a reserved value and the hardware rejected that write. So the subsequent

> RCC->CFGR |= (1 << 1);

then just ORed 0b0 to the reset value 0b01 to result in 0b11.

Generally, avoid sequences of RMW writes to a hardware register. Registers are *not* memory words. Read the register to a temporary value, modify there what you need, and make one single write back.

JW

srbhktyr
srbhktyrAuthor
Associate
September 7, 2024

Thankyou so much for help and good suggestions.. Much appreciated :)