2024-09-06 04:09 AM
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();
}
}
Solved! Go to Solution.
2024-09-06 04:28 AM - edited 2024-09-06 04:30 AM
> 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
2024-09-06 04:28 AM - edited 2024-09-06 04:30 AM
> 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
2024-09-06 09:45 PM
Thankyou so much for help and good suggestions.. Much appreciated