GPIO Alternate FunctionsAF0 – AF15-Embedded C Course Online

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Tutorial 05 — GPIO

GPIO Alternate Functions
AF0 – AF15

How STM32F407 pins share multiple peripheral functions — AFRL/AFRH registers, the complete AF mapping table, and step-by-step configuration for UART, SPI, and I2C.

🔁 AF0–AF15 Mapping⏱ ~25 min read⚡ AFRL/AFRH Registers📈 Pin Mux Configuration

What Are Alternate Functions?

A modern microcontroller has far more peripherals (UART, SPI, I2C, CAN, TIM, etc.) than physical pins. The solution is pin multiplexing — each GPIO pin can be connected to multiple peripheral signals, but only one at a time.

On the STM32F407, each GPIO pin can serve as:

  • GPIO Input or Output (MODER = 00 or 01)
  • Alternate Function (MODER = 10) — connected to a peripheral
  • Analog (MODER = 11) — connected to ADC/DAC

When MODER is set to 10 (Alternate Function), the actual peripheral is selected by the AFRL or AFRH register — which chooses from AF0 to AF15.

💡 Important: You must set MODER to 10 (AF mode) AND select the correct AF number in AFRL/AFRH. Setting only one without the other will not work. The AF number comes from the datasheet’s “Alternate function mapping” table.

AF Numbers AF0–AF15 — What Each Means

Each AF number is a fixed assignment defined by ST. Here’s the global mapping of what each AF number means:

AF NumberPeripheralExample Signals
AF0System / RTC / MCO / SWDMCO1, MCO2, SWDIO, SWDCLK, TRACESWO, RTC
AF1TIM1, TIM2TIM1_CH1-4, TIM2_CH1-4, TIM1_ETR
AF2TIM3, TIM4, TIM5TIM3_CH1-4, TIM4_CH1-4, TIM5_CH1-4
AF3TIM8, TIM9, TIM10, TIM11TIM8_CH1-4, TIM9_CH1-2, TIM10_CH1, TIM11_CH1
AF4I2C1, I2C2, I2C3I2Cx_SCL, I2Cx_SDA, I2Cx_SMBA
AF5SPI1, SPI2, I2S2, I2S2extSPIx_SCK, SPIx_MISO, SPIx_MOSI, SPIx_NSS
AF6SPI3, I2S3, I2S3extSPI3_SCK, SPI3_MISO, SPI3_MOSI, I2S3_SD
AF7USART1, USART2, USART3USARTx_TX, USARTx_RX, USARTx_CTS, USARTx_RTS
AF8UART4, UART5, USART6UARTx_TX, UARTx_RX, USART6_TX/RX
AF9CAN1, CAN2, TIM12, TIM13, TIM14CANx_TX, CANx_RX, TIM12_CH1-2
AF10OTG_FS, OTG_HSOTG_FS_DM/DP, OTG_HS ULPI signals
AF11ETH (Ethernet)ETH_MII_*, ETH_RMII_*
AF12FSMC, SDIO, OTG_HSFSMC_D*, FSMC_A*, SDIO_D*, SDIO_CLK
AF13DCMIDCMI_D*, DCMI_PIXCLK, DCMI_HSYNC, DCMI_VSYNC
AF14(Reserved)—
AF15EVENTOUTSends a pulse to other Cortex-M cores (not commonly used)

AFRL — Alternate Function Low Register (Pins 0–7)

The GPIOx_AFRL register selects the alternate function for pins 0 through 7. Each pin uses 4 bits, giving 16 possible AF values (AF0–AF15). All 8 pins fit in one 32-bit register.

GPIOx_AFRL — AFR[0] (Offset: 0x20) — 4 bits per pin, covers pins 7 down to 0
31:28
AFRL7
27:24
AFRL6
23:20
AFRL5
19:16
AFRL4
15:12
AFRL3
11:8
AFRL2
7:4
AFRL1
3:0
AFRL0

Pin position n → bits [(n*4)+3 : n*4]

Bit and field descriptions
Bit / FieldDescription
31:28 AFRL7AFRL7[3:0]
27:24 AFRL6AFRL6[3:0]
23:20 AFRL5AFRL5[3:0]
19:16 AFRL4AFRL4[3:0]
15:12 AFRL3AFRL3[3:0]
11:8 AFRL2AFRL2[3:0]
7:4 AFRL1AFRL1[3:0]
3:0 AFRL0AFRL0[3:0]
/* AFRL: Set AF for pins 0-7 */
/* Formula: bit position for pin y = y * 4 */
/* Mask = 0xF (4 bits), Value = AF number (0-15) */

/* Example: Set PA5 to AF5 (SPI1_SCK) */
/* PA5 is in AFRL (pins 0-7), bit position = 5 * 4 = 20 */
GPIOA->AFR[0] &= ~(0xFU << (5 * 4));  /* clear bits [23:20] */
GPIOA->AFR[0] |=  (0x5U << (5 * 4));  /* AF5 = SPI1 */

/* Example: Set PA2 to AF7 (USART2_TX) */
GPIOA->AFR[0] &= ~(0xFU << (2 * 4));  /* clear bits [11:8] */
GPIOA->AFR[0] |=  (0x7U << (2 * 4));  /* AF7 = USART2 */

AFRH — Alternate Function High Register (Pins 8–15)

The GPIOx_AFRH register works identically to AFRL but covers pins 8 through 15. It is accessed as GPIOx->AFR[1] in code (or at register offset 0x24).

GPIOx_AFRH — AFR[1] (Offset: 0x24) — 4 bits per pin, covers pins 15 down to 8
31:28
AFRH15
27:24
AFRH14
23:20
AFRH13
19:16
AFRH12
15:12
AFRH11
11:8
AFRH10
7:4
AFRH9
3:0
AFRH8
Bit and field descriptions
Bit / FieldDescription
31:28 AFRH15AFRH15[3:0]
27:24 AFRH14AFRH14[3:0]
23:20 AFRH13AFRH13[3:0]
19:16 AFRH12AFRH12[3:0]
15:12 AFRH11AFRH11[3:0]
11:8 AFRH10AFRH10[3:0]
7:4 AFRH9AFRH9[3:0]
3:0 AFRH8AFRH8[3:0]
/* AFRH: Set AF for pins 8-15 */
/* IMPORTANT: use AFR[1], and pin index within register = pin - 8 */
/* Formula: bit position = (pin - 8) * 4 */

/* Example: Set PA9 to AF7 (USART1_TX) */
/* PA9 is in AFRH (pin ≥ 8), adjusted pin = 9 - 8 = 1 */
GPIOA->AFR[1] &= ~(0xFU << ((9-8) * 4));  /* clear bits [7:4] */
GPIOA->AFR[1] |=  (0x7U << ((9-8) * 4));  /* AF7 = USART1_TX */

/* Example: Set PA10 to AF7 (USART1_RX) */
GPIOA->AFR[1] &= ~(0xFU << ((10-8) * 4)); /* clear bits [11:8] */
GPIOA->AFR[1] |=  (0x7U << ((10-8) * 4)); /* AF7 = USART1_RX */

/* Reusable macro for any pin */
#define GPIO_SET_AF(port, pin, af) \
    do { \
        (port)->AFR[(pin) >> 3] &= ~(0xFU << (((pin) & 7) * 4)); \
        (port)->AFR[(pin) >> 3] |=  ((af)  << (((pin) & 7) * 4)); \
    } while(0)

/* Usage: */
GPIO_SET_AF(GPIOA, 9,  7);   /* PA9  = AF7 = USART1_TX */
GPIO_SET_AF(GPIOA, 10, 7);   /* PA10 = AF7 = USART1_RX */
GPIO_SET_AF(GPIOB, 6,  4);   /* PB6  = AF4 = I2C1_SCL  */
GPIO_SET_AF(GPIOA, 5,  5);   /* PA5  = AF5 = SPI1_SCK  */

Complete AF Mapping Table (Key Pins)

Pin AF0AF1AF2 AF3AF4AF5 AF6AF7AF8 AF9AF10
PA0—TIM2_CH1TIM5_CH1TIM8_ETR———USART2_CTSUART4_TX—OTG_HS
PA2—TIM2_CH3TIM5_CH3TIM9_CH1———USART2_TX——OTG_HS
PA3—TIM2_CH4TIM5_CH4TIM9_CH2———USART2_RX——OTG_HS
PA4—————SPI1_NSSSPI3_NSSUSART2_CK———
PA5—TIM2_CH1———SPI1_SCK————OTG_HS
PA6—TIM1_BKINTIM3_CH1TIM8_BKIN—SPI1_MISO———TIM13_CH1—
PA7—TIM1_CH1NTIM3_CH2TIM8_CH1N—SPI1_MOSI———TIM14_CH1—
PA8MCO1TIM1_CH1——I2C3_SCL——USART1_CK——OTG_FS_SOF
PA9—TIM1_CH2——I2C3_SMBA——USART1_TX——OTG_FS_VBUS
PA10—TIM1_CH3—————USART1_RX——OTG_FS_ID
PA11—TIM1_CH4—————USART1_CTS—CAN1_RXOTG_FS_DM
PA12—TIM1_ETR—————USART1_RTS—CAN1_TXOTG_FS_DP
PB6—TIM4_CH1——I2C1_SCL——USART1_TX—CAN2_TX—
PB7—TIM4_CH2——I2C1_SDA——USART1_RX———
PB10—TIM2_CH3——I2C2_SCLSPI2_SCKI2S2_CKUSART3_TX——OTG_HS
PC10——————SPI3_SCKUSART3_TXUART4_TX——

4-Step AF Configuration Checklist

Every time you configure a GPIO for alternate function, follow these exact steps in this order:

Mandatory Configuration Steps
  1. Step 1: Enable GPIO port clock via RCC_AHB1ENR
  2. Step 2: Set GPIOx_MODER to 10 (AF mode)
  3. Step 3: Set AFRL or AFRH to the correct AF number
  4. Step 4: Configure OTYPER, OSPEEDR, PUPDR as required by the peripheral
  5. Step 5: Enable the peripheral clock via the appropriate RCC register

Example: USART1 on PA9 (TX) and PA10 (RX)

/* Configure PA9=USART1_TX and PA10=USART1_RX (AF7) */
void usart1_gpio_init(void) {
    /* Step 1: Enable GPIOA clock */
    RCC_AHB1ENR |= (1U << 0);

    /* Step 2: Set PA9 and PA10 to AF mode (MODER = 10) */
    GPIOA->MODER &= ~((0x3U<<18) | (0x3U<<20));  /* clear [19:18],[21:20] */
    GPIOA->MODER |=  ((0x2U<<18) | (0x2U<<20));  /* set AF mode */

    /* Step 3: Set AF7 for PA9 and PA10 in AFRH (AFR[1]) */
    GPIOA->AFR[1] &= ~((0xFU<<4) | (0xFU<<8));  /* clear AFRH9,AFRH10 */
    GPIOA->AFR[1] |=  ((0x7U<<4) | (0x7U<<8));  /* AF7 = USART1 */

    /* Step 4: Push-pull, high speed, no pull */
    GPIOA->OTYPER  &= ~((1U<<9) | (1U<<10));   /* push-pull */
    GPIOA->OSPEEDR |=  ((0x2U<<18)|(0x2U<<20)); /* fast speed */
    GPIOA->PUPDR   &= ~((0x3U<<18)|(0x3U<<20)); /* no pull */

    /* Note: Also enable USART1 clock: RCC_APB2ENR |= (1U << 4) */
}

Example: SPI1 on PA5/PA6/PA7

/* Configure PA5=SPI1_SCK, PA6=SPI1_MISO, PA7=SPI1_MOSI (AF5) */
void spi1_gpio_init(void) {
    RCC_AHB1ENR |= (1U << 0);  /* enable GPIOA clock */

    /* Set PA5, PA6, PA7 to AF mode */
    GPIOA->MODER &= ~((0x3U<<10)|(0x3U<<12)|(0x3U<<14));
    GPIOA->MODER |=  ((0x2U<<10)|(0x2U<<12)|(0x2U<<14));

    /* Set AF5 (SPI1) for PA5, PA6, PA7 in AFRL */
    GPIOA->AFR[0] &= ~((0xFU<<20)|(0xFU<<24)|(0xFU<<28));
    GPIOA->AFR[0] |=  ((0x5U<<20)|(0x5U<<24)|(0x5U<<28));

    /* Push-pull, high speed */
    GPIOA->OTYPER  &= ~((1U<<5)|(1U<<6)|(1U<<7));
    GPIOA->OSPEEDR |=  ((0x3U<<10)|(0x3U<<12)|(0x3U<<14)); /* high */

    /* Also configure PE3 as software CS (regular GPIO output) */
    RCC_AHB1ENR |= (1U << 4);  /* enable GPIOE clock */
    GPIOE->MODER &= ~(0x3U<<6);
    GPIOE->MODER |=  (0x1U<<6);  /* PE3 output */
    GPIOE->BSRR = (1U<<(3+16));  /* CS initially HIGH (deselect) */
}

Example: I2C1 on PB6 (SCL) / PB7 (SDA)

/* Configure PB6=I2C1_SCL and PB7=I2C1_SDA (AF4) */
/* I2C requires: AF mode, open-drain output, pull-up */
void i2c1_gpio_init(void) {
    RCC_AHB1ENR |= (1U << 1);  /* enable GPIOB clock */

    /* Set PB6 and PB7 to AF mode (MODER bits [13:12],[15:14] = 10) */
    GPIOB->MODER &= ~((0x3U<<12)|(0x3U<<14));
    GPIOB->MODER |=  ((0x2U<<12)|(0x2U<<14));

    /* AF4 = I2C1 for PB6 and PB7 in AFRL */
    GPIOB->AFR[0] &= ~((0xFU<<24)|(0xFU<<28));
    GPIOB->AFR[0] |=  ((0x4U<<24)|(0x4U<<28));  /* AF4 */

    /* MANDATORY: Open-drain for I2C */
    GPIOB->OTYPER |= (1U<<6) | (1U<<7);

    /* Fast speed for I2C Fast Mode (400 kHz) */
    GPIOB->OSPEEDR &= ~((0x3U<<12)|(0x3U<<14));
    GPIOB->OSPEEDR |=  ((0x2U<<12)|(0x2U<<14));  /* fast */

    /* Pull-up (use external 4.7kΩ in production!) */
    GPIOB->PUPDR &= ~((0x3U<<12)|(0x3U<<14));
    GPIOB->PUPDR |=  ((0x1U<<12)|(0x1U<<14));  /* pull-up */
}
Tutorial 05 Summary
  • Each GPIO pin supports AF0–AF15 — one peripheral function at a time
  • Must set MODER = 10 before the AF register has any effect
  • AFRL (AFR[0]) covers pins 0–7; AFRH (AFR[1]) covers pins 8–15
  • 4 bits per pin in AFR: bit position = (pin % 8) × 4
  • AF7 = USART1/2/3 | AF5 = SPI1/2 | AF6 = SPI3 | AF4 = I2C1/2/3 | AF9 = CAN
  • I2C always needs open-drain output type + pull-up resistors
  • Always also enable the peripheral’s own clock (APB1/APB2) separately

STM32F407 Discovery Tutorial Series — Tutorial 05: GPIO Alternate Functions AF0–AF15

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →