Fix SPI GPIO alternate function assignment on STM32H7/F7#11389
Fix SPI GPIO alternate function assignment on STM32H7/F7#11389sensei-hacker wants to merge 2 commits intoiNavFlight:maintenance-9.xfrom
Conversation
Create per-pin AF lookup tables (bus_spi_stm32h7xx.h and bus_spi_stm32f7xx.h) and use them in bus_spi_hal_ll.c to automatically resolve the correct GPIO AF for each SPI pin. Previously, SPI3 on STM32H743 applied AF6 to all pins. On H743, AF6 on PB5 is SPI3_MISO - the MOSI function requires AF7. Targets routing SPI3_MOSI to PB5 (e.g. HAKRCH743 MAX7456 OSD) received no MOSI signal. Targets may still define SPI*_SCK/MISO/MOSI_AF in target.h to override the table.
Review Summary by QodoFix SPI GPIO alternate function assignment on STM32H7/F7
WalkthroughsDescription• This description is generated by an AI tool. It may have inaccuracies • Replace hardcoded SPI alternate function fallbacks with per-pin lookup tables for STM32H7 and STM32F7 • Fix SPI3_MOSI on STM32H743 PB5 requiring AF7 instead of AF6 (broken on HAKRCH743 MAX7456 OSD) • Auto-resolve correct GPIO AF per pin at compile time using SPI_PIN_AF_HELPER macro • Allow targets to override individual pin AFs via target.h while maintaining safe defaults Diagramflowchart LR
A["bus_spi_hal_ll.c<br/>Hardcoded AF fallbacks"] -->|Replace with| B["bus_spi_stm32h7xx.h<br/>Per-pin AF lookup table"]
A -->|Replace with| C["bus_spi_stm32f7xx.h<br/>Per-pin AF lookup table"]
B -->|SPI_PIN_AF_HELPER| D["Auto-resolve AF<br/>at compile time"]
C -->|SPI_PIN_AF_HELPER| D
D -->|Fail safely| E["Undefined identifier<br/>if pin not in table"]
D -->|Allow override| F["target.h explicit<br/>SPI*_AF defines"]
File Changes1. src/main/drivers/bus_spi_hal_ll.c
|
Code Review by Qodo
1.
|
|
Test firmware build ready — commit Download firmware for PR #11389 223 targets built. Find your board's
|
Both bus_spi_stm32h7xx.h and bus_spi_stm32f7xx.h use CONCAT4 but relied on transitive includes from the including translation unit to provide it. Add an explicit include to make the dependency self-contained.
Problem
On STM32H743, non-default SPI pins couldn't be used without adding esoteric macros like
define SPI3_MISO_AF GPIO_AF6_SPI3Contrast UART and i2c, where the AF is automtically looked up in a table.
This PR adds automatic lookup for SPI, similar to uart, i2c, etc.
bus_spi_hal_ll.cappliedGPIO_AF6_SPI3to all SPI3 pins when no explicit AF was defined intarget.h. On this H7 AF6 on PB5 is SPI3_MISO — the MOSI function requires AF7. Targets routing SPI3_MOSI to PB5 without an explicit override received no MOSI signal, silently breaking any SPI3 peripheral on that pin (e.g. the MAX7456 OSD on HAKRCH743).The same structural issue existed in the STM32F7 path (hardcoded single-AF fallbacks), though the F7 AF assignments happen to be correct for the pins used by current targets.
Solution
Create a macro of AF lookup tables and use them to auto-resolve the correct alternate function per pin at compile time:
bus_spi_stm32h7xx.h— AF table for STM32H743, verified against DS12110 Tables 10–14bus_spi_stm32f7xx.h— AF table for STM32F722/F745/F765, verified against DS11853/DS10916/DS11532 Table 9bus_spi_hal_ll.c— replace hardcoded single-AF fallbacks with#ifndefguards that auto-resolve from the table; targets may still defineSPI*_SCK/MISO/MOSI_AFintarget.hto overrideIf a target uses a pin not in the table the build fails with an "undefined identifier" error — a safe, explicit failure rather than silently applying the wrong AF.
Targets built and verified