1

I am trying to program an STM32f10xx MCU and trying to set the Clock. In the Reference manual It is written that the PLL when turned on, a flag will be set by the hardware indicating that it is ready or LOCKED, The flag bit is called PLLRDY. The PLLRDY is set by hardware to :

1 when the PLL is locked
0 when the PLL is not locked (NOT READY)

the CR register or Control Register's reset value is ZERO by default. and the RCC_CR_PLLRDY = 0x02000000

I need to put a while loop to check if the PLL is ready, is my implementation correct?

  // Turn On PLL
  RCC->CR |= RCC_CR_PLLON;

  // Wait while the PLL "Phase LOCKED LOOP" is Locked and stable:
  // Which is going to be set? the CR itself or the PLLRDY register?
  while( !(RCC->CR & RCC_CR_PLLRDY) )
  {
    // Error when a certain time passes and the PLL is not ready!
  }

or should it be

while( !(RCC->CR | RCC_CR_PLLRDY) )
{
  //SOME CODE!
}


Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
The_M_Code
  • 127
  • 2
  • 10

1 Answers1

1

!(RCC->CR | RCC_CR_PLLRDY) this will be always false as RCC_CR_PLLRDY is not zero and non_zero | any_value is always not zero.

To test if some bits are set you need to use & operator.

The first solution is OK

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Ok but how is RCC_CR_PLLRDY not zero! the Reference manual states that this bit is set by hardware and the reset value of the register (of the RCC_CR_PLLRDY bit itself) is 0. Supposing that initially the RCC_CR_PLLRDY is zero then !( ZERO & ZERO) = ONE , and if the bit is set then !(ZERO & ONE) = ONE and this means that control will always be stuck in this while loop? am I missing something here? – The_M_Code Nov 29 '20 at 17:33
  • `RCC_CR_PLLRDY` is a constant and it is not 0. So `RCC->CR | RCC_CR_PLLRDY` will always be non-zero – independent of the value `RCC->CR` – and `!(RCC->CR | RCC_CR_PLLRDY)` will always evaluate to `false`. That's how bitwise OR works. – Codo Nov 29 '20 at 18:46
  • @The_M_Code I recommend you check the compiler settings. All current compilers can warn you about such programming errors. – Codo Nov 29 '20 at 18:50