Microchip® Advanced Software Framework

sam0/drivers/sercom/spi/spi.h File Reference

SAM Serial Peripheral Interface Driver.

Copyright (c) 2012-2020 Microchip Technology Inc. and its subsidiaries.

#include <compiler.h>
#include <port.h>
#include <sercom.h>
#include <pinmux.h>
#include <string.h>
#include <conf_spi.h>
#include <sercom_interrupt.h>

Data Structures

struct  spi_config
 SPI configuration structure. More...
 
struct  spi_master_config
 SPI Master configuration structure. More...
 
struct  spi_module
 SERCOM SPI driver software device instance structure. More...
 
struct  spi_slave_config
 SPI slave configuration structure. More...
 
struct  spi_slave_inst
 SPI peripheral slave instance structure. More...
 
struct  spi_slave_inst_config
 SPI peripheral slave configuration structure. More...
 

Macros

#define PINMUX_DEFAULT   0
 Default pinmux. More...
 
#define PINMUX_UNUSED   0xFFFFFFFF
 Unused pinmux. More...
 
#define SPI_TIMEOUT   10000
 SPI timeout value. More...
 
Driver Feature Definition

Define SERCOM SPI features set according to different device family.

#define FEATURE_SPI_SLAVE_SELECT_LOW_DETECT
 SPI slave select low detection. More...
 
#define FEATURE_SPI_HARDWARE_SLAVE_SELECT
 Slave select can be controlled by hardware. More...
 
#define FEATURE_SPI_ERROR_INTERRUPT
 SPI with error detect feature. More...
 
#define FEATURE_SPI_SYNC_SCHEME_VERSION_2
 SPI sync scheme version 2. More...
 

Typedefs

typedef void(* spi_callback_t )(struct spi_module *const module)
 Type of the callback functions. More...
 

Enumerations

enum  spi_addr_mode {
  SPI_ADDR_MODE_MASK = SERCOM_SPI_CTRLB_AMODE(0),
  SPI_ADDR_MODE_UNIQUE = SERCOM_SPI_CTRLB_AMODE(1),
  SPI_ADDR_MODE_RANGE = SERCOM_SPI_CTRLB_AMODE(2)
}
 SPI address modes enum. More...
 
enum  spi_callback {
  SPI_CALLBACK_BUFFER_TRANSMITTED,
  SPI_CALLBACK_BUFFER_RECEIVED,
  SPI_CALLBACK_BUFFER_TRANSCEIVED,
  SPI_CALLBACK_ERROR,
  SPI_CALLBACK_SLAVE_TRANSMISSION_COMPLETE,
  SPI_CALLBACK_SLAVE_SELECT_LOW,
  SPI_CALLBACK_COMBINED_ERROR
}
 SPI Callback enum. More...
 
enum  spi_character_size {
  SPI_CHARACTER_SIZE_8BIT = SERCOM_SPI_CTRLB_CHSIZE(0),
  SPI_CHARACTER_SIZE_9BIT = SERCOM_SPI_CTRLB_CHSIZE(1)
}
 SPI character size enum. More...
 
enum  spi_data_order {
  SPI_DATA_ORDER_LSB = SERCOM_SPI_CTRLA_DORD,
  SPI_DATA_ORDER_MSB = 0
}
 SPI data order enum. More...
 
enum  spi_frame_format {
  SPI_FRAME_FORMAT_SPI_FRAME = SERCOM_SPI_CTRLA_FORM(0),
  SPI_FRAME_FORMAT_SPI_FRAME_ADDR = SERCOM_SPI_CTRLA_FORM(2)
}
 SPI frame format enum. More...
 
enum  spi_interrupt_flag {
  SPI_INTERRUPT_FLAG_DATA_REGISTER_EMPTY = SERCOM_SPI_INTFLAG_DRE,
  SPI_INTERRUPT_FLAG_TX_COMPLETE = SERCOM_SPI_INTFLAG_TXC,
  SPI_INTERRUPT_FLAG_RX_COMPLETE = SERCOM_SPI_INTFLAG_RXC,
  SPI_INTERRUPT_FLAG_SLAVE_SELECT_LOW = SERCOM_SPI_INTFLAG_SSL,
  SPI_INTERRUPT_FLAG_COMBINED_ERROR = SERCOM_SPI_INTFLAG_ERROR
}
 SPI Interrupt Flags. More...
 
enum  spi_mode {
  SPI_MODE_MASTER = 1,
  SPI_MODE_SLAVE = 0
}
 SPI modes enum. More...
 
enum  spi_signal_mux_setting {
  SPI_SIGNAL_MUX_SETTING_A,
  SPI_SIGNAL_MUX_SETTING_B,
  SPI_SIGNAL_MUX_SETTING_C,
  SPI_SIGNAL_MUX_SETTING_D,
  SPI_SIGNAL_MUX_SETTING_E,
  SPI_SIGNAL_MUX_SETTING_F,
  SPI_SIGNAL_MUX_SETTING_G,
  SPI_SIGNAL_MUX_SETTING_H,
  SPI_SIGNAL_MUX_SETTING_I,
  SPI_SIGNAL_MUX_SETTING_J,
  SPI_SIGNAL_MUX_SETTING_K,
  SPI_SIGNAL_MUX_SETTING_L,
  SPI_SIGNAL_MUX_SETTING_M,
  SPI_SIGNAL_MUX_SETTING_N,
  SPI_SIGNAL_MUX_SETTING_O,
  SPI_SIGNAL_MUX_SETTING_P
}
 SPI signal MUX settings. More...
 
enum  spi_transfer_mode {
  SPI_TRANSFER_MODE_0 = 0,
  SPI_TRANSFER_MODE_1 = SERCOM_SPI_CTRLA_CPHA,
  SPI_TRANSFER_MODE_2 = SERCOM_SPI_CTRLA_CPOL,
  SPI_TRANSFER_MODE_3 = SERCOM_SPI_CTRLA_CPHA | SERCOM_SPI_CTRLA_CPOL
}
 SPI transfer modes enum. More...
 

Functions

static bool spi_is_syncing (struct spi_module *const module)
 Determines if the SPI module is currently synchronizing to the bus. More...
 
enum status_code spi_set_baudrate (struct spi_module *const module, uint32_t baudrate)
 Set the baudrate of the SPI module. More...
 
Driver Initialization and Configuration
static void spi_get_config_defaults (struct spi_config *const config)
 Initializes an SPI configuration structure to default values. More...
 
static void spi_slave_inst_get_config_defaults (struct spi_slave_inst_config *const config)
 Initializes an SPI peripheral slave device configuration structure to default values. More...
 
static void spi_attach_slave (struct spi_slave_inst *const slave, const struct spi_slave_inst_config *const config)
 Attaches an SPI peripheral slave. More...
 
enum status_code spi_init (struct spi_module *const module, Sercom *const hw, const struct spi_config *const config)
 Initializes the SERCOM SPI module. More...
 
Enable/Disable
static void spi_enable (struct spi_module *const module)
 Enables the SERCOM SPI module. More...
 
static void spi_disable (struct spi_module *const module)
 Disables the SERCOM SPI module. More...
 
void spi_reset (struct spi_module *const module)
 Resets the SPI module. More...
 
Lock/Unlock
static enum status_code spi_lock (struct spi_module *const module)
 Attempt to get lock on driver instance. More...
 
static void spi_unlock (struct spi_module *const module)
 Unlock driver instance. More...
 
Ready to Write/Read
static bool spi_is_write_complete (struct spi_module *const module)
 Checks if the SPI in master mode has shifted out last data, or if the master has ended the transfer in slave mode. More...
 
static bool spi_is_ready_to_write (struct spi_module *const module)
 Checks if the SPI module is ready to write data. More...
 
static bool spi_is_ready_to_read (struct spi_module *const module)
 Checks if the SPI module is ready to read data. More...
 
Read/Write
static enum status_code spi_write (struct spi_module *module, uint16_t tx_data)
 Transfers a single SPI character. More...
 
enum status_code spi_write_buffer_wait (struct spi_module *const module, const uint8_t *tx_data, uint16_t length)
 Sends a buffer of length SPI characters. More...
 
static enum status_code spi_read (struct spi_module *const module, uint16_t *rx_data)
 Reads last received SPI character. More...
 
enum status_code spi_read_buffer_wait (struct spi_module *const module, uint8_t *rx_data, uint16_t length, uint16_t dummy)
 Reads buffer of length SPI characters. More...
 
enum status_code spi_transceive_wait (struct spi_module *const module, uint16_t tx_data, uint16_t *rx_data)
 Sends and reads a single SPI character. More...
 
enum status_code spi_transceive_buffer_wait (struct spi_module *const module, uint8_t *tx_data, uint8_t *rx_data, uint16_t length)
 Sends and receives a buffer of length SPI characters. More...
 
enum status_code spi_select_slave (struct spi_module *const module, struct spi_slave_inst *const slave, bool select)
 Selects slave device. More...