Skip to main content
Visitor II
April 8, 2024
Solved

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

  • April 8, 2024
  • 4 replies
  • 1053 views

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)

 

 

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    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

    4 replies

    Technical Moderator
    April 8, 2024

     

     GPIOA->AFR[1]|=(0x07<<12)|(0x07<<8);

     

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

    SofLit_0-1712589738838.png

     

    Graduate II
    April 8, 2024

    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

    Graduate II
    April 8, 2024

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

    Super User
    April 8, 2024
    //ALT7 for UART2 (PA2 and PA3)

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

    Graduate II
    April 8, 2024

    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.