Understanding Alternate Functions and Pin Remapping in STM32H7

ampheoampheo
3 min read

The STM32H7 series offers advanced pin remapping and alternate function (AF) capabilities compared to earlier STM32 families. This guide explains how to leverage these features for flexible hardware design.


1. Key Features of STM32H7

  • Enhanced GPIO Flexibility: Each pin supports up to 16 alternate functions.

  • Dynamic Remapping: Some peripherals can be remapped without resetting the MCU.

  • Independent GPIO Banks: Multiple banks (GPIOA-K) with separate clock control.


2. How to Configure Alternate Functions

Step 1: Consult Reference Documents

  • Datasheet: Lists pinout and default AF assignments.

  • Reference Manual (e.g., RM0433): Details AF options for each peripheral.

  • STM32CubeMX: Graphical tool to automate configuration (recommended).


Step 2: Configure GPIO for Alternate Function

Example: Remap USART3 to PD8/PD9

c

#include "stm32h7xx.h"

void USART3_Remap_Init() {
    // 1. Enable clocks
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIODEN;   // GPIO Port D clock
    RCC->APB1LENR |= RCC_APB1LENR_USART3EN; // USART3 clock

    // 2. Set pins to Alternate Function Mode
    GPIOD->MODER &= ~(GPIO_MODER_MODE8 | GPIO_MODER_MODE9); // Clear mode bits
    GPIOD->MODER |= (2 << GPIO_MODER_MODE8_Pos) | (2 << GPIO_MODER_MODE9_Pos); // AF mode

    // 3. Select AF7 (USART3) for PD8 (TX) and PD9 (RX)
    GPIOD->AFR[1] |= (7 << GPIO_AFRH_AFSEL8_Pos) | (7 << GPIO_AFRH_AFSEL9_Pos);

    // 4. Configure USART3
    USART3->BRR = SystemCoreClock / 115200; // Baud rate
    USART3->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // Enable
}

Key Points:

  • Use AFR[0] for pins 0-7 and AFR[1] for pins 8-15.

  • AF numbers are peripheral-specific (e.g., AF7 for USART3).


3. Dynamic Remapping at Runtime

STM32H7 allows runtime pin reconfiguration (for select peripherals):

c

// Example: Switch SPI2 SCK from PB13 to PI1
void SPI2_Remap_Dynamic() {
    // 1. Disable old pin (PB13)
    GPIOB->MODER &= ~GPIO_MODER_MODE13_1;

    // 2. Configure new pin (PI1)
    GPIOI->MODER |= (2 << GPIO_MODER_MODE1_Pos); // AF mode
    GPIOI->AFR[0] |= (5 << GPIO_AFRL_AFSEL1_Pos); // AF5 (SPI2)

    // 3. Reinitialize SPI2 if needed
}

4. Using STM32CubeMX

  1. Pinout View: Drag-and-drop peripherals to alternate pins.

  2. Alternate Function Tab: Verify AF assignments.

  3. Generate Code: Export project to your IDE.

https://i.imgur.com/JQ8WzVl.png


5. Common Remapping Scenarios

PeripheralDefault PinsAlternate Pins
USART3PD8/PD9PC10/PC11
SPI1PA5-PA7PB3-PB5
TIM1PE9-PE13PA8-PA11

6. Troubleshooting

IssueSolution
Peripheral not workingVerify clocks and AF settings.
Signal noiseEnable pull-up/down resistors.
Remapping failsCheck pin conflicts in CubeMX.

7. Best Practices

  1. Use CubeMX to avoid manual errors.

  2. Verify clock trees (APB/AHB settings).

  3. Test DMA with remapped peripherals (may need reconfiguration).


Summary

  • STM32H7 provides more flexible remapping than older STM32s.

  • Alternate Functions are set via AFR registers.

  • Dynamic remapping is possible (but use cautiously).

0
Subscribe to my newsletter

Read articles from ampheo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ampheo
ampheo