SPI Master DMA Write Read
This document introduces two SPI communication samples. The sample1 demonstrates how SPI1 sends and receives data by GDMA. The sample2 demonstrates how SPI0 sends data by GDMA. In both examples, SPI is configured as a master, and the direction is full-duplex. The chip sends data to the SPI slave and reads data from it.
Requirements
For hardware requirements, please refer to the Requirements.
Wiring
Sample 1 Wiring
Connect P0_0 (master SCK) to SCK of SPI slave device, connect P1_0 (master MOSI) to MOSI of SPI slave device, connect P1_1 (master MISO) to MISO of SPI slave device, and connect P0_1 (master CS) to CS of SPI slave device. The hardware connection of SPI sample code is shown in the figure below.

SPI Sample Code Hardware Connection Diagram
Sample 2 Wiring
Connect P2_1 (master SCK) to SCK of SPI slave device, connect P2_2 (master MOSI) to MOSI of SPI slave device, connect P2_3 (master MISO) to MISO of SPI slave device, and connect P0_1 (master CS) to CS of SPI slave device. The hardware connection of SPI sample code is shown in the figure below.

SPI Sample Code Hardware Connection Diagram
Configurations
The following macros can be configured to modify pin definitions for sample1.
#define PIN_SPI1_SCK P0_0
#define PIN_SPI1_MOSI P1_0
#define PIN_SPI1_MISO P1_1
#define PIN_SPI1_CS P0_1
The following macros can be configured to modify pin definitions for sample2.
#define SPI0_SCK P2_1
#define SPI0_MOSI P2_2
#define SPI0_MISO P2_3
#define SPI0_CS P0_1
The entry function are as follows, call this function in
main()
to run this sample code. For more details, please refer to the Initialization.For sample 1, use the following entry function:
spi_dma_demo();
For sample 2, use the following entry function:
spi_master_tx_dma_demo();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Sample 1 Verification
Press the Reset button on the EVB.
The data in array
sendbuf
is sent to SPI slave device. When completing the transmission, it enters the GDMA interrupt and prints log.spi_tx_dma_handler
After the slave device sends data to the chip, the chip stores the received data in array
readbuf
. When completing the transmission, it enters the GDMA interrupt and prints log.spi_rx_dma_handler
Sample 2 Verification
Press the Reset button on the EVB.
The data in array
SPI_TX_Buf
is sent to SPI slave device. When completing the transmission, it enters the GDMA interrupt and prints log.spi_master_tx_dma_handler! tx_len_all 60
Code Overview
Source Code Directory
For both samples, please refer to the Source Code Directory for the project directory.
Sample 1 source code:
Source code directory:
sdk\src\sample\io_demo\gdma\spi_dma\spi_dma_demo.c
.
Sample 2 source code:
Source code directory:
sdk\src\sample\io_demo\gdma\spi_dma\spi_master_tx_dma_demo.c
.
SPI DMA Initialization Flow
The initialization flow for peripherals can refer to Initialization Flow.
The SPI TX or RX DMA initialization flow requires first initializing the SPI peripheral, followed by TX or RX DMA initialization.
SPI Initialization Flow
The SPI initialization flow can refer to SPI Initialization Flow Chart.
Call
Pad_Config()
andPinmux_Config()
to initialize the pin.static void board_spi_init(void) { Pinmux_Config(PIN_SPI1_SCK, SPI1_CLK_MASTER); Pinmux_Config(PIN_SPI1_MOSI, SPI1_MO_MASTER); Pinmux_Config(PIN_SPI1_MISO, SPI1_MI_MASTER); Pinmux_Config(PIN_SPI1_CS, SPI1_SS_N_0_MASTER); Pad_Config(PIN_SPI1_SCK, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI1_MOSI, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI1_MISO, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI1_CS, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_HIGH); }
Call
RCC_PeriphClockCmd()
to enable the SPI clock and function.Initialize the SPI peripheral:
Define the
SPI_InitTypeDef
typeSPI_InitStructure
, and callSPI_StructInit()
to pre-fillSPI_InitStructure
with default values.Modify the
SPI_InitStructure
parameters as needed. The SPI initialization parameter configuration is shown in the table below.Call
SPI_Init()
to initialize the SPI peripheral,SPI0_HS
is seleted.
SPI Initialization Parameters SPI Hardware Parameters
Setting in the
SPI_InitStructure
SPI
Direction
Device Role (SPI Master or SPI Slave)
Data Frame Size
Clock Polarity
Clock Phase
Clock Div
100
Frame Format
TX Water Level
1
RX Water Level
7
Call
SPI_Cmd()
to enable SPI.
TX DMA Initialization Flow
The SPI TX DMA initialization flow can refer to SPI TX DMA Initialization Flow Chart.
Both sample 1 and 2 implement the TX DMA function, the difference is that sample 1 uses SPI1, while sample 2 uses SPI0. Take sample 1 as an example for a detailed description.
Call
RCC_PeriphClockCmd()
to enable the GDMA clock and function.Call
GDMA_channel_request
to request a free GDMA channel and register the GDMA interrupt handler.Initialize the GDMA peripheral:
Define the
GDMA_InitTypeDef
typeGDMA_InitStruct
, and callGDMA_StructInit()
to pre-fillGDMA_InitStruct
with default values.Modify the
GDMA_InitStruct
parameters as needed. The GDMA initialization parameter configuration is shown in the table below.Call
GDMA_Init()
to initialize the GDMA peripheral.
GDMA Initialization Parameters GDMA Hardware Parameters
Setting in the
GDMA_InitStruct
GDMA
Channel Num
SPI_TX_DMA_CHANNEL_NUM
Transfer Direction
Buffer Size
TEST_SIZE
Source Address Increment or Decrement
Destination Address Increment or Decrement
Source Data Size
Destination Data Size
Source Burst Transaction Length
Destination Burst Transaction Length
Source Address
sendbuf
Destination Address
SPI1->DR
Destination Handshake
GDMA_Handshake_SPI1_TX
Call
GDMA_INTConfig()
to enable TX GDMA transfer complete interruptGDMA_INT_Transfer
.Call
NVIC_Init()
to enable NVIC of TX GDMA.
RX DMA Initialization Flow
The SPI RX DMA initialization flow can refer to SPI RX DMA Initialization Flow Chart.
Call
RCC_PeriphClockCmd()
to enable the GDMA clock and function.Call
GDMA_channel_request
to request a free GDMA channel and register the GDMA interrupt handler.Initialize the GDMA peripheral:
Define the
GDMA_InitTypeDef
typeGDMA_InitStruct
, and callGDMA_StructInit()
to pre-fillGDMA_InitStruct
with default values.Modify the
GDMA_InitStruct
parameters as needed. The GDMA initialization parameter configuration is shown in the table below.Call
GDMA_Init()
to initialize the GDMA peripheral.
GDMA Initialization Parameters GDMA Hardware Parameters
Setting in the
GDMA_InitStruct
GDMA
Channel Num
SPI_RX_DMA_CHANNEL_NUM
Transfer Direction
Buffer Size
TEST_SIZE
Source Address Increment or Decrement
Destination Address Increment or Decrement
Source Data Size
Destination Data Size
Source Burst Transaction Length
Destination Burst Transaction Length
Source Address
SPI1->DR
Destination Address
readbuf
Source Handshake
GDMA_Handshake_SPI1_RX
Call
GDMA_INTConfig()
to enable RX GDMA transfer complete interruptGDMA_INT_Transfer
.Call
NVIC_Init()
to enable NVIC of RX GDMA.
Functional Implementation
Master Receive Data by DMA
Call
SPI_GDMACmd()
to disable and then enable SPI GDMA RX Function.Call
GDMA_Cmd()
to enable RX DMA transfers.When GDMA transfer is completed, transfer complete interrupt is triggered. Then call
GDMA_ClearINTPendingBit()
to clearGDMA_INT_Transfer
interrupt.
Master Send Data by DMA
Call
SPI_GDMACmd()
to disable and then enable SPI GDMA TX Function.Call
GDMA_Cmd()
to enable TX DMA transfers.When GDMA transfer is completed, transfer complete interrupt is triggered. Then call
GDMA_ClearINTPendingBit()
to clearGDMA_INT_Transfer
interrupt.