SPI 2SPI DMA Write Read
This document introduces two SPI communication samples. The sample1 demonstrates how SPI0 reads data and SPI1 sends data by GDMA. The sample2 demonstrates how SPI0 reads data and SPI1 sends data by auto-reload GDMA. In both examples, SPI is configured as a master, and operates in GDMA mode. The direction of SPI0 is EEPROM read, the direction of SPI1 is TX only. The chip reads data from the SPI slave device0 and writes data to the SPI slave device1.
Requirements
For hardware requirements, please refer to the Requirements.
Wiring
Connect P1_7 to CS of SPI slave device 0, connect P1_4 to SCK of SPI slave device 0, connect P1_5 to MISO of SPI slave device 0, and connect P1_6 to MOSI of SPI slave device 0. Connect P2_5 to CS of SPI slave device 1, connect P2_2 to SCK of SPI slave device 1, connect P2_3 to MISO of SPI slave device 1, and connect P2_4 to MOSI of SPI slave device 1. 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.
#define PIN_SPI0_SCK P1_4
#define PIN_SPI0_MOSI P1_5
#define PIN_SPI0_MISO P1_6
#define PIN_SPI0_CS P1_7
#define PIN_SPI1_SCK P2_2
#define PIN_SPI1_MOSI P2_3
#define PIN_SPI1_MISO P2_4
#define PIN_SPI1_CS P2_5
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:
spi2spi_dma_demo();
For sample 2, use the following entry function:
spi2spi_auto_reload_dma_demo();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Sample Verification
Press the Reset button on the EVB.
SPI0 reads data from SPI slave device 0 and SPI1 sends the same data to SPI slave device 1.
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\spi\spi2spi_dma\spi2spi_dma_demo.c
.
Sample 2 source code:
Source code directory:
sdk\src\sample\io_demo\spi\spi2spi_auto_reload_dma\spi2spi_auto_reload_dma_demo.c
.
2SPI Initialization Flow
The initialization flow for peripherals can refer to Initialization Flow.
The SPI initialization flow can refer to SPI Initialization Flow Chart.
Call
Pad_Config()
andPinmux_Config()
to initialize the SPI0 and SPI1 pins.static void board_spi_init(void) { Pinmux_Config(PIN_SPI0_SCK, SPI0_CLK_MASTER); Pinmux_Config(PIN_SPI0_MOSI, SPI0_MO_MASTER); Pinmux_Config(PIN_SPI0_MISO, SPI0_MI_MASTER); Pinmux_Config(PIN_SPI0_CS, SPI0_SS_N_0_MASTER); 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_SPI0_SCK, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI0_MOSI, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI0_MISO, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI0_CS, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI1_SCK, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI1_MOSI, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI1_MISO, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(PIN_SPI1_CS, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); }
Call
RCC_PeriphClockCmd()
to enable the SPI0 and SPI1 clock and function.Define the
SPI_InitTypeDef
typeSPI_InitStructure
, and callSPI_StructInit()
to pre-fillSPI_InitStructure
with default values.Initialize the SPI0 peripheral:
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.Call
SPI_Cmd()
to enable SPI0.
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
RX Number of Data Frames
255
RX DMA Enable
RX Water Level
8
Initialize the SPI1 peripheral:
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.Call
SPI_Cmd()
to enable SPI1.
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
Frame Format
RX DMA Enable
RX Water Level
35
2SPI DMA Initialization Flow
The initialization flow for peripherals can refer to Initialization Flow.
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
SPI2SPI_DMA_CHANNEL_NUM
Transfer Direction
Buffer Size
256
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
SPI0->DR
Destination Address
SPI1->DR
Source Handshake
Destination Handshake
Call
GDMA_INTConfig()
to enable GDMA transfer complete interruptGDMA_INT_Transfer
.Call
NVIC_Init()
to enable NVIC of GDMA.
2SPI Auto-Reload DMA Initialization Flow
The initialization flow for peripherals can refer to Initialization Flow.
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_AUTO_RELOAD_DMA_CHANNEL_NUM
Transfer Direction
Buffer Size
256
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
SPI0->DR
Destination Address
SPI1->DR
Source Handshake
Destination Handshake
Multi Block Enable
ENABLE
Multi Block Mode
Functional Implementation
2SPI DMA Interupt Handle
When GDMA transfer is completed, transfer complete interrupt is triggered.
Call
GDMA_ClearAllTypeINT()
to clear the all GDMA channel interrupt.Call
GDMA_channel_release()
to release the used GDMA channel.