cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with selecting HSE as system clock

srbhktyr
Associate II

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();

}

}

1 ACCEPTED SOLUTION

Accepted Solutions

> 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

View solution in original post

2 REPLIES 2

> 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
Associate II

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