SPI DMA Circular Transfer Example
Source code path: example/hal/spi_dma
Supported Platforms
The example can run on the following development boards:
sf32lb52-lcd_n16r8
sf32lb58-lcd_n16r64n4
Overview
This example demonstrates the continuous transmission and reception of SPI HAL in DMA Circular mode:
SPI1 master mode,
2Lines, 8bit, defaultSPI MODE0TX DMA + RX DMA working simultaneously, continuously triggering clock and circularly transferring data
Statistics of operation status through DMA half-full/full callbacks
Statistics of exceptions and printing of error codes through error callbacks
The focus of this example is to verify “whether the DMA circular link is stable”, not to read NOR ID in blocking mode.
Usage of the Example
Compilation and Burning
Taking sf32lb52-lcd_n16r8 development board as an example
Switch to the example project directory and run the scons command to compile:
scons --board=sf32lb52-lcd_n16r8 -j8
Run `build_sf32lb52-lcd_n16r8_hcpu\uart_download.bat` and select the port as prompted to download:
> build_sf32lb52-lcd_n16r8_hcpu\uart_download.bat
Uart Download
please input the serial port num:5
For detailed steps on compilation and download, please refer to the relevant introduction in Getting Started Guide.
Enable SPI1 DMA related switches in project/proj.conf:
CONFIG_BSP_SPI1_TX_USING_DMA=y
CONFIG_BSP_SPI1_RX_USING_DMA=y
Hardware Connection
Only short
MOSI(DIO/DO)toMISO(DI)CLKandCSremain normal output, no need to connect to external slave devices
You can also connect to a real slave device (another board as SPI Slave), in which case connect according to the standard four-wire connection and share ground.
Refer to the following diagram for the hardware schematic of sf32lb52-lcd_n16r8:

Example Output Results:
Startup Log
Start spi dma circular demo!
spi dma running, tx/rx circular started.
tip: short SPI1 MOSI(DIO/DO) to MISO(DI) for loopback verification.
Running Log (Example)
half=1234, full=1233, err=0
rx[0..7]: 00 01 02 03 04 05 06 07, rx[mid..mid+7]: 80 81 82 83 84 85 86 87
half=2470, full=2469, err=0
rx[0..7]: 00 01 02 03 04 05 06 07, rx[mid..mid+7]: 80 81 82 83 84 85 86 87
Description:
half/fullwill continuously increase, indicating that DMA is working in circular modeerrshould normally remain0In loopback short-circuit scenario,
rxis commonly a cyclic sequence of 0x00~0xFF (may have phase offset)
Functional Flow
Configure SPI1 pinmux and clock
Initialize SPI1 (Master, 2Lines, 8bit)
Initialize RX/TX DMA, both in
DMA_CIRCULARmodeCall
HAL_SPI_TransmitReceive_DMA()to start continuous transmission and receptionStatistics status in DMA half-full/full callbacks
Print status and sample RX data in main loop
Exception Diagnosis
half/fullnot increasing for a long timeCheck if SPI1 DMA switch is enabled in
proj.confCheck if DMA IRQ is correctly enabled
errcontinuously increasingPrint
spi_errerror code for location (already output in log)Check if DMA request/channel matches board-level configuration
Data irregular in loopback scenario
Check if MOSI and MISO are indeed short-circuited
Check pin level and connection reliability
Reference Documents
EH-SF32LB52X_Pin_config_V1.3.0_20231110.xlsx
DS0052-SF32LB52x-芯片技术规格书 V0p3.pdf
PY25Q128HA_datasheet_V1.1.pdf
Update Records
Version |
Date |
Release Notes |
|---|---|---|
0.1.0 |
03/2026 |
Initial version |