Microchip® Advanced Software Framework

Quickstart guide for SAM SPI driver

This is the quickstart guide for the SAM SPI driver, with step-by-step instructions on how to configure and use the driver in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g.the main application function.

Basic use case

In this basic use case, the SPI module are configured for:

  • Master mode
  • Interrupt-based handling

Prerequisites

  1. System Clock Management (Sysclock)

Setup steps

Example code

Add to application C-file:

void spi_master_init(Spi *p_spi)
{
spi_reset(p_spi);
spi_get_pcs(DEFAULT_CHIP_ID));
spi_set_delay_between_chip_select(p_spi, CONFIG_SPI_MASTER_DELAY_BCS);
}
void spi_master_setup_device(Spi *p_spi, struct spi_device *device,
spi_flags_t flags, uint32_t baud_rate, board_spi_select_id_t sel_id)
{
spi_set_transfer_delay(p_spi, device->id, CONFIG_SPI_MASTER_DELAY_BS,
CONFIG_SPI_MASTER_DELAY_BCT);
spi_set_bits_per_transfer(p_spi, device->id, CONFIG_SPI_MASTER_BITS_PER_TRANSFER);
spi_set_baudrate_div(p_spi, device->id,
spi_set_clock_polarity(p_spi, device->id, flags >> 1);
spi_set_clock_phase(p_spi, device->id, ((flags & 0x1) ^ 0x1));
}

Workflow

  1. Initialize the SPI in master mode:
    • void spi_master_init(SPI_EXAMPLE);
  2. Set up an SPI device:
    • void spi_master_setup_device(SPI_EXAMPLE, &SPI_DEVICE_EXAMPLE,
      SPI_MODE_0, SPI_EXAMPLE_BAUDRATE, 0);
    • Note
      The returned device descriptor structure must be passed to the driver whenever that device should be used as current slave device.
  3. Enable SPI module: