Showing results for 
Search instead for 
Did you mean: 

Register programming GPIOA->(AF07<<4) and (AF07<<8)

Associate II

hi come across these code using register programing, STM32F411 Nucleo bd, anyone can advise how does the (AF07<<4) and (AF07<<8) relate to UART2 PA2 & PA3? please advise..


    #define AF07 0x07
    GPIOA->AFR[1]|=(AF07<<4)|(AF07<<8); //ALT7 for UART2 (PA2 and PA3)



ST Employee




PA2 (UART2_TX) and PA3 (UART2_RX) correspond to shift left respectively 0x7 by 8 and 12:



To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

This is writing AFR[1] so here describing settings for PA9 and PA10


10*4 - 32 = 8

9*4 - 32 = 4

Honestly this type of bare-metal nonsense where you blindly OR on values with zero bit(s) needs to be avoided.

Half the stuff I see posted has 3 to 4 times the number of LOAD/STORE operations necessary

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Evangelist III
//ALT7 for UART2 (PA2 and PA3)

Wrong misleading comments are worse than no comments. This one is a perfect example.

Sorry, I do not understand how your objections regarding "blindly OR on values with zero bit(s)" applies for the code above.

Uwe Bonnes
Principal II

Find the meaning of AFxx for some Port pin in the STM32F411 datasheet Chapter 4, Table 9. The Meaning of the GPIO Alternate function register is explained in the reference manual in the GPIO section. Each GPIO in alternate function has a multiplexer where different peripheral IO-Lines are available. The AF value selects the specified multiplexer input, the relation is given in table 9.