DIY Electronics
— Building circuits, programming microcontrollers, and making things blinkDMA on STM32 for SPI Displays
Driving a 240x320 ILI9341 TFT display by polling the SPI bus is slow and CPU-intensive. With DMA, the CPU initiates the transfer and is free for other work while pixels stream to the display.
The setup in STM32 HAL: configure a DMA channel for SPI TX, set memory-to-peripheral transfer, enable the TC (transfer complete) interrupt. In code, HAL_SPI_Transmit_DMA() starts the transfer and returns immediately. In the DMA TC callback, you deassert CS and set a flag that the frame is done.
Measured improvement on a 240x320 full-frame update: polling at 72MHz SPI clock takes 15ms and 100% CPU. DMA at the same clock takes 15ms but 0% CPU during the transfer. This unlocks smooth 60fps animation with CPU time left over for touch input, sensor reads, and WiFi.