Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# SPI Slave

## SPI Slave Usage

The following code snippet demonstrates the basic usage of an SPI slave device.

    #include <xs1.h>
    #include "spi.h"

    // Setup callbacks
    //  NOTE: See API or SDK examples for more on using the callbacks
    spi_slave_callback_group_t spi_cbg = {
         .slave_transaction_started = (slave_transaction_started_t) start,
         .slave_transaction_ended = (slave_transaction_ended_t) end,
         .app_data = NULL
    };

    port_t p_miso = XS1_PORT_1A;
    port_t p_cs   = XS1_PORT_1B;
    port_t p_sclk = XS1_PORT_1C;
    port_t p_mosi = XS1_PORT_1D;
    xclock_t cb   = XS1_CLKBLK_1;

    // Start the slave device in this thread
    //  NOTE: You may wish to launch the slave device in a different thread.
    //        See the XTC Tools documentation reference for lib_xcore.
    spi_slave(&spi_cbg, p_sclk, p_mosi, p_miso, p_cs, cb, SPI_MODE_0);

## SPI Slave API

The following structures and functions are used to initialize and start an SPI slave instance.

void spi_slave(const spi_slave_callback_group_t *spi_cbg, port_t p_sclk, port_t p_mosi, port_t p_miso, port_t p_cs, xclock_t clk, int cpol, int cpha, uint32_t thread_mode)

  Initializes a SPI slave.

  The CS to first clock minimum delay, sometimes referred to as setup time, will vary based on the duration of the slave_transaction_started callback. This parameter will be application specific. To determine the typical value, time the duration of the slave_transaction_started callback, and add 2000ns as a safety factor. If slave_transaction_started has a non-deterministic runtime, perhaps due to waiting on an XCORE resource, then the application developer must decide an appropriate CS to first SCLK specification.

  The minimum delay between consecutive transactions varies based on SPI mode, and if MISO is used.

  ###

  #### MISO | CPOL | CPHA | Min Delay (ns)

  enabled | 0 | 0 | 2270 enabled | 0 | 1 | 2240 enabled | 1 | 0 | 2240 enabled | 1 | 1 | 2270 disabled | 0 | 0 | 440 disabled | 0 | 1 | 420 disabled | 1 | 0 | 430 disabled | 1 | 1 | 430

  Note

  Verified at 25000 kbps, with a 2000ns CS assertion to first clock in all modes.

  Parameters:

    - spi_cbg – The spi_slave_callback_group_t context to use.

    - p_sclk – The SPI slave’s SCLK port. Must be a 1-bit port.

    - p_mosi – The SPI slave’s MOSI port. Must be a 1-bit port.

    - p_miso – The SPI slave’s MISO port. Must be a 1-bit port.

    - p_cs – The SPI slave’s CS port. Must be a 1-bit port.

    - clock_block – The clock block to use for the SPI slave.

    - cpol – The clock polarity to use.

    - cpha – The clock phase to use.

struct spi_slave_callback_group_t
  #include <spi.h>

  Callback group representing callback events that can occur during the operation of the SPI slave task. Must be initialized by the application prior to passing it to one of the SPI slaves.