2022-08-07 04:22 PM
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?
2022-08-07 11:14 PM
> 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.
I can imagine this value being result of running some kind of bootloader, or maybe some wicked startup code.
JW
2022-08-08 07:32 PM
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.