SPI Slave Write DMA

The sample demonstrates how SPI0 slave sends data by GDMA and receives data by interrupt. The chip sends data to the SPI master and receives data from it.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P1_0 (slave SCK) to SCK of SPI master device, connect P1_1 (slave MOSI) to MOSI of SPI master device, connect P2_1 (slave MISO) to MISO of SPI master device, and connect P2_2 (slave CS) to CS of SPI master device. The hardware connection of SPI sample code is shown in the figure below.

../../../_images/SPI_Demo_7_1_Hardware_Connection_Diagram.png

SPI Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify pin definitions.

    • #define PIN_SPI_SCK P1_0

    • #define PIN_SPI_MOSI P1_1

    • #define PIN_SPI_MISO P2_1

    • #define PIN_SPI_CS P2_2

  2. The entry function are as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    spi_slave_write_demo();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

  1. Press the Reset button on the EVB, the data in array send_buf is sent to SPI TX FIFO.

  2. When master device sends data to the chip, the send_buf can be sent to SPI master. When completing the transmission, it enters the GDMA interrupt and prints log.

    spi_slave_tx_dma_handler
    
  3. And the chip stores the received data in array SPI_ReadINTBuf and prints log in Debug Analyzer.

    spi_slave_handler: SPI_ReadINTBuf[0] 0x%x
    ...
    spi_slave_handler: SPI_ReadINTBuf[15] 0x%x
    

Code Overview

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\spi\slave\spi_slave_write_demo.c .

SPI Initialization Flow

The initialization flow for peripherals can refer to Initialization Flow.

SPI initialization flow is shown in the following figure.

../../../_images/SPI_Initialization_Flow_Chart.png

SPI Initialization Flow Chart

  1. Call Pad_Config() and Pinmux_Config() to initialize the pins.

    static void board_spi_pinmux_init(void)
    {
       Pinmux_Config(PIN_SPI_SCK, SPI_CLK_SLAVE);
       Pinmux_Config(PIN_SPI_MOSI, SPI_MO_SLAVE);
       Pinmux_Config(PIN_SPI_MISO, SPI_MI_SLAVE);
       Pinmux_Config(PIN_SPI_CS, SPI_SS_N_0_SLAVE);
    
       Pad_Config(PIN_SPI_SCK, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE,
                   PAD_OUT_HIGH);
       Pad_Config(PIN_SPI_MOSI, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE,
                   PAD_OUT_HIGH);
       Pad_Config(PIN_SPI_MISO, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE,
                   PAD_OUT_HIGH);
       Pad_Config(PIN_SPI_CS, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, 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.

    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_Slave

    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

    Receive FIFO Threshold Level

    SPI_InitTypeDef::SPI_RxThresholdLevel

    0

    TX Water Level

    SPI_InitTypeDef::SPI_TxWaterlevel

    7

  4. Call RamVectorTableUpdate() to register SPI slave interrupt handler.

  5. Call SPI_INTConfig() to enable RX FIFO full interrupt SPI_INT_RXF.

  6. Call NVIC_Init() to enable NVIC of SPI.

  7. Call SPI_Cmd() to enable SPI.

TX DMA Initialization Flow

The initialization flow for peripherals can refer to Initialization Flow.

SPI TX DMA initialization flow is shown in the following figure.

../../../_images/SPI_TX_DMA_Initialization_Flow_Chart.png

SPI TX 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_SLAVE_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

    SPI0->DR

    Destination Handshake

    GDMA_InitTypeDef::GDMA_DestHandshake

    GDMA_Handshake_SPI0_TX

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

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

Functional Implementation

Slave Send Data by DMA

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

  2. Call GDMA_Cmd() to enable DMA transfers.

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

Slave Receive Data by Interrupt

Please refer to Receive Data by Interrupt.