2024-04-08 08:13 AM
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)
2024-04-08 08:23 AM - edited 2024-04-08 08:31 AM
GPIOA->AFR[1]|=(0x07<<12)|(0x07<<8);
PA2 (UART2_TX) and PA3 (UART2_RX) correspond to shift left respectively 0x7 by 8 and 12:
2024-04-08 08:55 AM
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
2024-04-08 09:35 AM
//ALT7 for UART2 (PA2 and PA3)
Wrong misleading comments are worse than no comments. This one is a perfect example.
2024-04-08 09:47 AM
Sorry, I do not understand how your objections regarding "blindly OR on values with zero bit(s)" applies for the code above.
2024-04-08 09:55 AM
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.