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)

Initializes a SPI slave.

Note

Verified at 25000 kbps, with a 2000ns CS assertion to first clock in all modes. The CS to first clock minimum delay will vary based on the duration of the slave_transaction_started callback.

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.