Q: How to use STM32 SPI in master and slave mode with DMA?
Answer
In master mode STM32 controls the clock (SCK). Configure CPOL and CPHA to match the slave device. For high-speed SPI (>10 MHz) use GPIO output speed Very High and short PCB traces. Use NSS (chip select) as GPIO under software control for multiple slaves. In slave mode ensure MISO is Hi-Z when NSS is high. DMA TX/RX should be configured before starting the transfer. For continuous transfers use DMA circular mode. On STM32H7 use Octo-SPI (OSPI) for external NOR flash at up to 90 MHz. Always handle TXE/RXNE flags correctly or use DMA exclusively to avoid data corruption.
Filed under: FAQ
