0
\$\begingroup\$

I am facing the problem of not able to lock the GPIO pins even after using the LCKK bit sequence to initiate the lock but facing some unknown issues.

My references are from HERE, page number 174

The code I'm trying here is to lock an nth pin pin of port port

void digital_lock(GPIO_TypeDef *port, int pin) {
  if ((port->LCKR & GPIO_LCKR_LCKK) == 1) { //if lock bit is set, reset it to remap the sequence 
    port->LCKR |= GPIO_LCKR_LCKK;
    port->LCKR &= ~(GPIO_LCKR_LCKK);  //To modify the bit, this is the sequence
    port->LCKR |= GPIO_LCKR_LCKK;
    port->LCKR &= ~(GPIO_LCKR_LCKK);  //finally clearing the LCKK bit
  }
    
  port->LCKR |= (1<<pin);
  port->LCKR |= GPIO_LCKR_LCKK;
  port->LCKR &= ~(GPIO_LCKR_LCKK);  //To modify the bit, this is the sequence
  port->LCKR |= GPIO_LCKR_LCKK;
  port->LCKR |= GPIO_LCKR_LCKK; //finally locking the sequence
}

Please correct me if my understanding is wrong, Thank you!

\$\endgroup\$
0
\$\begingroup\$

It does not work because doing a read-modify-write operations on the register breaks the required sequence for locking.

Besides when the lock is already set, there is no way to unlock it without reset, so the unlock code will not work, and you can't unlock or lock any other pins if you already have set the lock.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.