cancel
Showing results for 
Search instead for 
Did you mean: 

How do I unlock and write to the GPIOx->LCKR register so I can reconfigure GPIOx pins?

magene
Senior II

I'm trying wean myself off the STM HAL library and learn how to program peripherals at the register level. I'm starting with UART1 on a STM32H7A3. UART1 with Alternate Function 7 has TX on GPIOB pin 6 and RX on GPIOB pin 7. So I think I need to set GPIOB->MODER6[1:0] and GPIO->MODER7[1:0] to 10 (alternate function mode). The problem is the H7 boots up with the GPIOB->LCKR register set to 0xFFFFFEBF which means pin 7 is locked. There's some information in the reference manual and the web about how to lock a pin with the LCKR register but I haven't found anything telling me how to unlock a pin. Just writing a 0 to the LCKR register lock key LCKK bit doesn't seem to work.

How do I unlock and write to the GPIOB->LCKR register so I can unlock pin 7 (or any pin) so I can reconfigure it?

2 REPLIES 2

> How do I unlock and write to the GPIOB->LCKR register so I can unlock pin 7 (or any pin) so I can reconfigure it?

You can't. That's the point of the lock, only reset can unlock.

> The problem is the H7 boots up with the GPIOB->LCKR register set to 0xFFFFFEBF

Not likely.

0693W00000QNAHlQAP.pngI can imagine this value being result of running some kind of bootloader, or maybe some wicked startup code.

JW

magene
Senior II

I think I figured it out. As you point out, the H7 reference manual says "the value of this port bit can no longer be modified until the next MCU reset or peripheral reset". I couldn't figure out how to do the "... or peripheral reset". By excavating through 3 or 4 layers of HAL code, I found that setting the GPIOBEN pin in RCC->AHB4ENR |= (1 << 1) sets GPIOB->LCKR to 0x00000000 (unlocked) and I can set the GPIOB->MODER register as required. I was setting GPIOBEN, just later in my code after I was trying to set the MODER register. I still have no idea why the GPIOB->LCKR register is 0xFFFFFEBF at bootup but if I put a breakpoint at the first line in main(), that's what the value is. I'll leave that mystery for another day.