SPI Master Switch CS

This sample demonstrates how SPI1 dynamically switches CS signals. In this example, SPI is configured as a master and the direction is full-duplex. The chip writes some data to SPI slave device0, then switches to slave device1 and device2 and sends data to them.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P1_3 (master CS0) to CS of SPI slave device 0, connect P1_4 (master CS1) to CS of SPI slave device 1, connect P1_5 (master CS2) to CS of SPI slave device 2, connect P1_0 (master SCK) to SCK of SPI slave device 0/1/2, connect P1_1 (master MISO) to MISO of SPI slave device 0/1/2, and connect P1_2 (master MOSI) to MOSI of SPI slave device 0/1/2.

The hardware connection of SPI sample code is shown in the figure below.

../../../_images/SPI_Demo_4_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_SPI1_SCK P1_0

    • #define PIN_SPI1_MOSI P1_1

    • #define PIN_SPI1_MISO P1_2

    • #define PIN_SPI1_CS0 P1_3

    • #define PIN_SPI1_CS1 P1_4

    • #define PIN_SPI1_CS2 P1_5

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

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

  2. The data in array SPI_WriteBuf is sent to SPI slave device 0.

  3. Then the data in array SPI_WriteBuf is sent to SPI slave device 1.

  4. Then the data in array SPI_WriteBuf is sent to SPI slave device 2.

Code Overview

Source Code Directory

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

  • Source code directory: sdk\src\sample\io_demo\spi\dynamic_switch_cs\spi_switchcs_demo.c .

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_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_CS0, SPI1_SS_N_0_MASTER);
       Pinmux_Config(PIN_SPI1_CS1, SPI1_SS_N_1_MASTER);
       Pinmux_Config(PIN_SPI1_CS2, SPI1_SS_N_2_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_CS0, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE,
                   PAD_OUT_HIGH);
       Pad_Config(PIN_SPI1_CS1, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE,
                   PAD_OUT_HIGH);
       Pad_Config(PIN_SPI1_CS2, 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.

    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_2Edge

    Clock Div

    SPI_InitTypeDef::SPI_BaudRatePrescaler

    100

    Frame Format

    SPI_InitTypeDef::SPI_FrameFormat

    SPI_Frame_Motorola

  4. Call SPI_Cmd() to enable SPI.

Functional Implementation

Master Send Data by Polling

  1. Call SPI_SendBuffer() to send the data in SPI_WriteBuf to the slave.

  2. Call SPI_GetFlagState() to check SPI_FLAG_TFE and SPI_FLAG_BUSY flag state, and wait for the SPI data transfer to complete.

  3. Call SPI_SetCSNumber() to switch slave device.

uint8_t SPI_WriteBuf[3] = {0x01, 0x02, 0x03};

SPI_SendBuffer(SPI1, SPI_WriteBuf, 3);

/* wait TX FIFO empty */
while (SPI_GetFlagState(SPI1, SPI_FLAG_TFE) == RESET);
while (SPI_GetFlagState(SPI1, SPI_FLAG_BUSY));

SPI_SetCSNumber(SPI1, 1);