19.8.4.1.2 Code

The following must be added to the user application source file, outside any functions.

A sample buffer to send via SPI.
static uint8_t buffer_rx[BUF_LENGTH] = {0x00,};
static uint8_t buffer_expect[BUF_LENGTH] = {
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
         0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13
};
Number of entries in the sample buffer.
#define BUF_LENGTH 20
A globally available software device instance struct to store the SPI driver state while it is in use.
struct spi_module spi_slave_instance;
A function for configuring the SPI.
void configure_spi_slave(void)
{
    struct spi_config config_spi_slave;
    /* Configure, initialize and enable SERCOM SPI module */
    spi_get_config_defaults(&config_spi_slave);
    config_spi_slave.mode = SPI_MODE_SLAVE;
    config_spi_slave.mode_specific.slave.preload_enable = true;
    config_spi_slave.mode_specific.slave.frame_format = SPI_FRAME_FORMAT_SPI_FRAME;
    config_spi_slave.mux_setting = EXT1_SPI_SERCOM_MUX_SETTING;
    /* Configure pad 0 for data in */
    config_spi_slave.pinmux_pad0 = EXT1_SPI_SERCOM_PINMUX_PAD0;
    /* Configure pad 1 as unused */
    config_spi_slave.pinmux_pad1 = EXT1_SPI_SERCOM_PINMUX_PAD1;
    /* Configure pad 2 for data out */
    config_spi_slave.pinmux_pad2 = EXT1_SPI_SERCOM_PINMUX_PAD2;
    /* Configure pad 3 for SCK */
    config_spi_slave.pinmux_pad3 = EXT1_SPI_SERCOM_PINMUX_PAD3;
    spi_init(&spi_slave_instance, EXT1_SPI_MODULE, &config_spi_slave);

    spi_enable(&spi_slave_instance);

}
A function for configuring the callback functionality of the SPI.
void configure_spi_slave_callbacks(void)
{
    spi_register_callback(&spi_slave_instance, spi_slave_callback,
            SPI_CALLBACK_BUFFER_RECEIVED);
    spi_enable_callback(&spi_slave_instance, SPI_CALLBACK_BUFFER_RECEIVED);
}
A global variable that can flag to the application that the buffer has been transferred.
volatile bool transfer_complete_spi_slave = false;
Callback function.
static void spi_slave_callback(struct spi_module *const module)
{
    transfer_complete_spi_slave = true;
}
Add to user application main().
uint8_t result = 0;

/* Initialize system */
system_init();

configure_spi_slave();
configure_spi_slave_callbacks();