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.

Note

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

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.