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.

../../../_images/SPI_Demo_1_2_Hardware_Connection_Diagram.png

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.

../../../_images/SPI_Demo_10_2_Hardware_Connection_Diagram.png

SPI Sample Code Hardware Connection Diagram

Configurations

  1. 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

  2. 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

  3. 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

  1. Press the Reset button on the EVB.

  2. 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
    
  3. 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

  1. Press the Reset button on the EVB.

  2. 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.

  1. Call Pad_Config() and Pinmux_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);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the SPI clock and function.

  3. Initialize the SPI peripheral:

    1. Define the SPI_InitTypeDef type SPI_InitStructure, and call SPI_StructInit() to pre-fill SPI_InitStructure with default values.

    2. Modify the SPI_InitStructure parameters as needed. The SPI initialization parameter configuration is shown in the table below.

    3. 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

    SPI_InitTypeDef::SPI_Direction

    SPI_Direction_FullDuplex

    Device Role (SPI Master or SPI Slave)

    SPI_InitTypeDef::SPI_Mode

    SPI_Mode_Master

    Data Frame Size

    SPI_InitTypeDef::SPI_DataSize

    SPI_DataSize_8b

    Clock Polarity

    SPI_InitTypeDef::SPI_CPOL

    SPI_CPOL_High

    Clock Phase

    SPI_InitTypeDef::SPI_CPHA

    SPI_CPHA_1Edge

    Clock Div

    SPI_InitTypeDef::SPI_BaudRatePrescaler

    100

    Frame Format

    SPI_InitTypeDef::SPI_FrameFormat

    SPI_Frame_Motorola

    TX Water Level

    SPI_InitTypeDef::SPI_RxWaterlevel

    1

    RX Water Level

    SPI_InitTypeDef::SPI_TxWaterlevel

    7

  4. 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.

  1. Call RCC_PeriphClockCmd() to enable the GDMA clock and function.

  2. Call GDMA_channel_request to request a free GDMA channel and register the GDMA interrupt handler.

  3. Initialize the GDMA peripheral:

    1. Define the GDMA_InitTypeDef type GDMA_InitStruct, and call GDMA_StructInit() to pre-fill GDMA_InitStruct with default values.

    2. Modify the GDMA_InitStruct parameters as needed. The GDMA initialization parameter configuration is shown in the table below.

    3. Call GDMA_Init() to initialize the GDMA peripheral.

    GDMA Initialization Parameters

    GDMA Hardware Parameters

    Setting in the GDMA_InitStruct

    GDMA

    Channel Num

    GDMA_InitTypeDef::GDMA_ChannelNum

    SPI_TX_DMA_CHANNEL_NUM

    Transfer Direction

    GDMA_InitTypeDef::GDMA_DIR

    GDMA_DIR_MemoryToPeripheral

    Buffer Size

    GDMA_InitTypeDef::GDMA_BufferSize

    TEST_SIZE

    Source Address Increment or Decrement

    GDMA_InitTypeDef::GDMA_SourceInc

    DMA_SourceInc_Inc

    Destination Address Increment or Decrement

    GDMA_InitTypeDef::GDMA_DestinationInc

    DMA_DestinationInc_Fix

    Source Data Size

    GDMA_InitTypeDef::GDMA_SourceDataSize

    GDMA_DataSize_Byte

    Destination Data Size

    GDMA_InitTypeDef::GDMA_DestinationDataSize

    GDMA_DataSize_Byte

    Source Burst Transaction Length

    GDMA_InitTypeDef::GDMA_SourceMsize

    GDMA_Msize_1

    Destination Burst Transaction Length

    GDMA_InitTypeDef::GDMA_DestinationMsize

    GDMA_Msize_1

    Source Address

    GDMA_InitTypeDef::GDMA_SourceAddr

    sendbuf

    Destination Address

    GDMA_InitTypeDef::GDMA_DestinationAddr

    SPI1->DR

    Destination Handshake

    GDMA_InitTypeDef::GDMA_DestHandshake

    GDMA_Handshake_SPI1_TX

  4. Call GDMA_INTConfig() to enable TX GDMA transfer complete interrupt GDMA_INT_Transfer.

  5. 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.

  1. Call RCC_PeriphClockCmd() to enable the GDMA clock and function.

  2. Call GDMA_channel_request to request a free GDMA channel and register the GDMA interrupt handler.

  3. Initialize the GDMA peripheral:

    1. Define the GDMA_InitTypeDef type GDMA_InitStruct, and call GDMA_StructInit() to pre-fill GDMA_InitStruct with default values.

    2. Modify the GDMA_InitStruct parameters as needed. The GDMA initialization parameter configuration is shown in the table below.

    3. Call GDMA_Init() to initialize the GDMA peripheral.

    GDMA Initialization Parameters

    GDMA Hardware Parameters

    Setting in the GDMA_InitStruct

    GDMA

    Channel Num

    GDMA_InitTypeDef::GDMA_ChannelNum

    SPI_RX_DMA_CHANNEL_NUM

    Transfer Direction

    GDMA_InitTypeDef::GDMA_DIR

    GDMA_DIR_PeripheralToMemory

    Buffer Size

    GDMA_InitTypeDef::GDMA_BufferSize

    TEST_SIZE

    Source Address Increment or Decrement

    GDMA_InitTypeDef::GDMA_SourceInc

    DMA_SourceInc_Fix

    Destination Address Increment or Decrement

    GDMA_InitTypeDef::GDMA_DestinationInc

    DMA_DestinationInc_Inc

    Source Data Size

    GDMA_InitTypeDef::GDMA_SourceDataSize

    GDMA_DataSize_Byte

    Destination Data Size

    GDMA_InitTypeDef::GDMA_DestinationDataSize

    GDMA_DataSize_Byte

    Source Burst Transaction Length

    GDMA_InitTypeDef::GDMA_SourceMsize

    GDMA_Msize_1

    Destination Burst Transaction Length

    GDMA_InitTypeDef::GDMA_DestinationMsize

    GDMA_Msize_1

    Source Address

    GDMA_InitTypeDef::GDMA_SourceAddr

    SPI1->DR

    Destination Address

    GDMA_InitTypeDef::GDMA_DestinationAddr

    readbuf

    Source Handshake

    GDMA_InitTypeDef::GDMA_SourceHandshake

    GDMA_Handshake_SPI1_RX

  4. Call GDMA_INTConfig() to enable RX GDMA transfer complete interrupt GDMA_INT_Transfer.

  5. Call NVIC_Init() to enable NVIC of RX GDMA.

Functional Implementation

Master Receive Data by DMA

  1. Call SPI_GDMACmd() to disable and then enable SPI GDMA RX Function.

  2. Call GDMA_Cmd() to enable RX DMA transfers.

  3. When GDMA transfer is completed, transfer complete interrupt is triggered. Then call GDMA_ClearINTPendingBit() to clear GDMA_INT_Transfer interrupt.

Master Send Data by DMA

  1. Call SPI_GDMACmd() to disable and then enable SPI GDMA TX Function.

  2. Call GDMA_Cmd() to enable TX DMA transfers.

  3. When GDMA transfer is completed, transfer complete interrupt is triggered. Then call GDMA_ClearINTPendingBit() to clear GDMA_INT_Transfer interrupt.