Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
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.
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.
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 Number | Peripheral | Example Signals |
|---|---|---|
| AF0 | System / RTC / MCO / SWD | MCO1, MCO2, SWDIO, SWDCLK, TRACESWO, RTC |
| AF1 | TIM1, TIM2 | TIM1_CH1-4, TIM2_CH1-4, TIM1_ETR |
| AF2 | TIM3, TIM4, TIM5 | TIM3_CH1-4, TIM4_CH1-4, TIM5_CH1-4 |
| AF3 | TIM8, TIM9, TIM10, TIM11 | TIM8_CH1-4, TIM9_CH1-2, TIM10_CH1, TIM11_CH1 |
| AF4 | I2C1, I2C2, I2C3 | I2Cx_SCL, I2Cx_SDA, I2Cx_SMBA |
| AF5 | SPI1, SPI2, I2S2, I2S2ext | SPIx_SCK, SPIx_MISO, SPIx_MOSI, SPIx_NSS |
| AF6 | SPI3, I2S3, I2S3ext | SPI3_SCK, SPI3_MISO, SPI3_MOSI, I2S3_SD |
| AF7 | USART1, USART2, USART3 | USARTx_TX, USARTx_RX, USARTx_CTS, USARTx_RTS |
| AF8 | UART4, UART5, USART6 | UARTx_TX, UARTx_RX, USART6_TX/RX |
| AF9 | CAN1, CAN2, TIM12, TIM13, TIM14 | CANx_TX, CANx_RX, TIM12_CH1-2 |
| AF10 | OTG_FS, OTG_HS | OTG_FS_DM/DP, OTG_HS ULPI signals |
| AF11 | ETH (Ethernet) | ETH_MII_*, ETH_RMII_* |
| AF12 | FSMC, SDIO, OTG_HS | FSMC_D*, FSMC_A*, SDIO_D*, SDIO_CLK |
| AF13 | DCMI | DCMI_D*, DCMI_PIXCLK, DCMI_HSYNC, DCMI_VSYNC |
| AF14 | (Reserved) | — |
| AF15 | EVENTOUT | Sends 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.
Pin position n → bits [(n*4)+3 : n*4]
Bit and field descriptions
| Bit / Field | Description |
|---|---|
| 31:28 AFRL7 | AFRL7[3:0] |
| 27:24 AFRL6 | AFRL6[3:0] |
| 23:20 AFRL5 | AFRL5[3:0] |
| 19:16 AFRL4 | AFRL4[3:0] |
| 15:12 AFRL3 | AFRL3[3:0] |
| 11:8 AFRL2 | AFRL2[3:0] |
| 7:4 AFRL1 | AFRL1[3:0] |
| 3:0 AFRL0 | AFRL0[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).
Bit and field descriptions
| Bit / Field | Description |
|---|---|
| 31:28 AFRH15 | AFRH15[3:0] |
| 27:24 AFRH14 | AFRH14[3:0] |
| 23:20 AFRH13 | AFRH13[3:0] |
| 19:16 AFRH12 | AFRH12[3:0] |
| 15:12 AFRH11 | AFRH11[3:0] |
| 11:8 AFRH10 | AFRH10[3:0] |
| 7:4 AFRH9 | AFRH9[3:0] |
| 3:0 AFRH8 | AFRH8[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 | AF0 | AF1 | AF2 | AF3 | AF4 | AF5 | AF6 | AF7 | AF8 | AF9 | AF10 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| PA0 | — | TIM2_CH1 | TIM5_CH1 | TIM8_ETR | — | — | — | USART2_CTS | UART4_TX | — | OTG_HS |
| PA2 | — | TIM2_CH3 | TIM5_CH3 | TIM9_CH1 | — | — | — | USART2_TX | — | — | OTG_HS |
| PA3 | — | TIM2_CH4 | TIM5_CH4 | TIM9_CH2 | — | — | — | USART2_RX | — | — | OTG_HS |
| PA4 | — | — | — | — | — | SPI1_NSS | SPI3_NSS | USART2_CK | — | — | — |
| PA5 | — | TIM2_CH1 | — | — | — | SPI1_SCK | — | — | — | — | OTG_HS |
| PA6 | — | TIM1_BKIN | TIM3_CH1 | TIM8_BKIN | — | SPI1_MISO | — | — | — | TIM13_CH1 | — |
| PA7 | — | TIM1_CH1N | TIM3_CH2 | TIM8_CH1N | — | SPI1_MOSI | — | — | — | TIM14_CH1 | — |
| PA8 | MCO1 | TIM1_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_RX | OTG_FS_DM |
| PA12 | — | TIM1_ETR | — | — | — | — | — | USART1_RTS | — | CAN1_TX | OTG_FS_DP |
| PB6 | — | TIM4_CH1 | — | — | I2C1_SCL | — | — | USART1_TX | — | CAN2_TX | — |
| PB7 | — | TIM4_CH2 | — | — | I2C1_SDA | — | — | USART1_RX | — | — | — |
| PB10 | — | TIM2_CH3 | — | — | I2C2_SCL | SPI2_SCK | I2S2_CK | USART3_TX | — | — | OTG_HS |
| PC10 | — | — | — | — | — | — | SPI3_SCK | USART3_TX | UART4_TX | — | — |
4-Step AF Configuration Checklist
Every time you configure a GPIO for alternate function, follow these exact steps in this order:
- Step 1: Enable GPIO port clock via
RCC_AHB1ENR - Step 2: Set
GPIOx_MODERto10(AF mode) - Step 3: Set
AFRLorAFRHto the correct AF number - Step 4: Configure
OTYPER,OSPEEDR,PUPDRas required by the peripheral - 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 */
}
- Each GPIO pin supports AF0–AF15 — one peripheral function at a time
- Must set MODER =
10before 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 More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments