Microchip® Advanced Software Framework

Quick Start Guide for SERCOM SPI Master - Polled

In this use case, the SPI on extension header 1 of the Xplained Pro board will be configured with the following settings:

  • Master Mode enabled
  • MSB of the data is transmitted first
  • Transfer mode 0
  • SPI MUX Setting E (see Master Mode Settings)
  • 8-bit character size
  • Not enabled in sleep mode
  • Baudrate 100000
  • GLCK generator 0

Setup

Prerequisites

There are no special setup requirements for this use-case.

Code

The following must be added to the user application:

A sample buffer to send via SPI.

static uint8_t buffer[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

GPIO pin to use as Slave Select.

#define SLAVE_SELECT_PIN CONF_MASTER_SS_PIN

A globally available software device instance struct to store the SPI driver state while it is in use.

struct spi_module spi_master_instance;

A globally available peripheral slave software device instance struct.

struct spi_slave_inst slave;

A function for configuring the SPI.

void configure_spi_master(void)
{
struct spi_config config_spi_master;
struct spi_slave_inst_config slave_dev_config;
/* Configure and initialize software device instance of peripheral slave */
slave_dev_config.ss_pin = SLAVE_SELECT_PIN;
spi_attach_slave(&slave, &slave_dev_config);
/* Configure, initialize and enable SERCOM SPI module */
spi_get_config_defaults(&config_spi_master);
config_spi_master.mux_setting = CONF_MASTER_MUX_SETTING;
config_spi_master.pinmux_pad0 = CONF_MASTER_PINMUX_PAD0;
config_spi_master.pinmux_pad1 = CONF_MASTER_PINMUX_PAD1;
config_spi_master.pinmux_pad2 = CONF_MASTER_PINMUX_PAD2;
config_spi_master.pinmux_pad3 = CONF_MASTER_PINMUX_PAD3;
spi_init(&spi_master_instance, CONF_MASTER_SPI_MODULE, &config_spi_master);
spi_enable(&spi_master_instance);
}

Add to user application main().

configure_spi_master();

Workflow

  1. Initialize system.
  2. Set-up the SPI.
    configure_spi_master();
    1. Create configuration struct.
      struct spi_config config_spi_master;
    2. Create peripheral slave configuration struct.
      struct spi_slave_inst_config slave_dev_config;
    3. Create peripheral slave software device instance struct.
      struct spi_slave_inst slave;
    4. Get default peripheral slave configuration.
    5. Set Slave Select pin.
      slave_dev_config.ss_pin = SLAVE_SELECT_PIN;
    6. Initialize peripheral slave software instance with configuration.
      spi_attach_slave(&slave, &slave_dev_config);
    7. Get default configuration to edit.
      spi_get_config_defaults(&config_spi_master);
    8. Set MUX setting E.
      config_spi_master.mux_setting = CONF_MASTER_MUX_SETTING;
    9. Set pinmux for pad 0 (data in (MISO)).
    10. Set pinmux for pad 1 as unused, so the pin can be used for other purposes.
    11. Set pinmux for pad 2 (data out (MOSI)).
    12. Set pinmux for pad 3 (SCK).
    13. Initialize SPI module with configuration.
      spi_init(&spi_master_instance, CONF_MASTER_SPI_MODULE, &config_spi_master);
    14. Enable SPI module.
      spi_enable(&spi_master_instance);

Use Case

Code

Add the following to your user application main().

while (true) {
/* Infinite loop */
if(!port_pin_get_input_level(BUTTON_0_PIN)) {
spi_select_slave(&spi_master_instance, &slave, true);
spi_write_buffer_wait(&spi_master_instance, buffer, BUF_LENGTH);
spi_select_slave(&spi_master_instance, &slave, false);
port_pin_set_output_level(LED_0_PIN, LED0_ACTIVE);
}
}

Workflow

  1. Select slave.
    spi_select_slave(&spi_master_instance, &slave, true);
  2. Write buffer to SPI slave.
    spi_write_buffer_wait(&spi_master_instance, buffer, BUF_LENGTH);
  3. Deselect slave.
    spi_select_slave(&spi_master_instance, &slave, false);
  4. Light up.
    port_pin_set_output_level(LED_0_PIN, LED0_ACTIVE);
  5. Infinite loop.
    while (true) {
    /* Infinite loop */
    if(!port_pin_get_input_level(BUTTON_0_PIN)) {
    spi_select_slave(&spi_master_instance, &slave, true);
    spi_write_buffer_wait(&spi_master_instance, buffer, BUF_LENGTH);
    spi_select_slave(&spi_master_instance, &slave, false);
    port_pin_set_output_level(LED_0_PIN, LED0_ACTIVE);
    }
    }