Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SPI - Serial Peripheral Interface

Driver for the Serial Peripheral Interface (SPI).

Provides functions for configuring and using the SPI.

Modules

 

Macros

#define BAUD_RATE_MASK   0x03
 
#define DOUBLE_SPEED_MASK   0x04
 
#define SPI_CLK2X_bm   0x01 /* Enable Double Speed bit mask. */
 
#define SPI_CLK2X_bp   0 /* Enable Double Speed bit position. */
 
#define SPI_CPHA_bm   0x04 /* SPI Clock phase bit 1 mask. */
 
#define SPI_CPHA_bp   2 /* SPI Clock phase bit 1 position. */
 
#define SPI_CPOL_bm   0x08 /* SPI Clock polarity bit 1 mask. */
 
#define SPI_CPOL_bp   3 /* SPI Clock polarity bit 1 position. */
 
#define SPI_DORD_bm   0x20 /* Data Order Setting bit mask. */
 
#define SPI_DORD_bp   5 /* Data Order Setting bit position. */
 
#define SPI_ENABLE_bm   0x40 /* Enable Module bit mask. */
 
#define SPI_ENABLE_bp   6 /* Enable Module bit position. */
 
#define SPI_IF_bm   0x80 /* Interrupt Flag bit mask. */
 
#define SPI_IF_bp   7 /* Interrupt Flag bit position. */
 
#define SPI_MASTER_bm   0x10 /* Master Operation Enable bit mask. */
 
#define SPI_MASTER_bp   4 /* Master Operation Enable bit position. */
 
#define SPI_MODE0   0
 
#define SPI_MODE1   (SPI_CPHA)
 
#define SPI_MODE2   (SPI_CPOL)
 
#define SPI_MODE3   (SPI_CPOL | SPI_CPHA)
 
#define SPI_MODE_gm   0x0C /* SPI Mode group mask. */
 
#define SPI_MODE_gp   2 /* SPI Mode group position. */
 
#define SPI_PRESCALER0_bm   (1 << 0) /* Prescaler bit 0 mask. */
 
#define SPI_PRESCALER0_bp   0 /* Prescaler bit 0 position. */
 
#define SPI_PRESCALER1_bm   (1 << 1) /* Prescaler bit 1 mask. */
 
#define SPI_PRESCALER1_bp   1 /* Prescaler bit 1 position. */
 
#define SPI_PRESCALER_gm   0x03 /* Prescaler group mask. */
 
#define SPI_PRESCALER_gp   0 /* Prescaler group position. */
 
#define SPI_SPIE_bm   0x80 /* Interrupt Enable bit mask. */
 
#define SPI_SPIE_bp   7 /* Interrupt Enable bit position. */
 
#define SPI_WRCOL_bm   0x40 /* Write Collision bit mask. */
 
#define SPI_WRCOL_bp   6 /* Write Collision bit position. */
 

Typedefs

typedef void(* spi_int_callback_t )(void)
 Interrupt event callback function type. More...
 
typedef enum SPI_PRESCALER_enum SPI_PRESCALER_t
 

Enumerations

enum  SPI_PRESCALER_enum {
  SPI_PRESCALER_DIV4_gc = (0x00 << 0),
  SPI_PRESCALER_DIV16_gc = (0x01 << 0),
  SPI_PRESCALER_DIV64_gc = (0x02 << 0),
  SPI_PRESCALER_DIV128_gc = (0x03 << 0)
}
 

Functions

static void spi_disable (volatile void *spi)
 Disables the SPI. More...
 
static void spi_disable_interrupt (volatile void *spi)
 Disables the SPI's interrupt. More...
 
static void spi_double_speed_disable (volatile void *spi)
 Disable SPI Double Speed. More...
 
static void spi_double_speed_enable (volatile void *spi)
 Enable SPI Double Speed. More...
 
static void spi_enable (volatile void *spi)
 Enables the SPI. More...
 
static void spi_enable_interrupt (volatile void *spi)
 Enables the SPI in interrupt mode. More...
 
static void spi_enable_master_mode (volatile void *spi)
 Activate SPI master mode of a SPI peripheral. More...
 
static void spi_enable_slave_mode (volatile void *spi)
 Activate SPI slave mode of a SPI peripheral. More...
 
static uint8_t spi_get (volatile void *spi)
 Get one data byte from SPI peripheral. More...
 
static bool spi_is_enabled (volatile void *spi)
 Tests if the SPI is enabled. More...
 
static bool spi_is_tx_ok (volatile void *spi)
 Tests if the SPI transfer is complete. More...
 
static bool spi_is_wc_flag_set (volatile void *spi)
 Tests if the Write collision occurs during SPI transfer. More...
 
static void spi_put (volatile void *spi, uint8_t data)
 Put one data byte to SPI peripheral. More...
 
int8_t spi_set_baud_div (volatile void *spi, uint32_t baudrate, uint32_t clkper_hz)
 Calculates the SPI baudrate divider. More...
 
static void spi_set_clock_mode (volatile void *spi, uint8_t mode)
 Set SPI clock mode. More...
 
static void spi_set_clock_phase (volatile void *spi, bool polarity)
 Set Data Capture Phase. More...
 
static void spi_set_clock_polarity (volatile void *spi, bool polarity)
 Set clock default state. More...
 
static void spi_set_dord_lsb (volatile void *spi)
 Set SPI data order to LSB first. More...
 
static void spi_set_dord_msb (volatile void *spi)
 Set SPI data order to MSB first. More...
 
void spi_set_interrupt_callback (spi_int_callback_t callback)
 Set SPI interrupt callback function. More...
 

#define BAUD_RATE_MASK   0x03
#define DOUBLE_SPEED_MASK   0x04
#define SPI_CLK2X_bm   0x01 /* Enable Double Speed bit mask. */
#define SPI_CLK2X_bp   0 /* Enable Double Speed bit position. */
#define SPI_CPHA_bm   0x04 /* SPI Clock phase bit 1 mask. */

Referenced by spi_set_clock_phase().

#define SPI_CPHA_bp   2 /* SPI Clock phase bit 1 position. */
#define SPI_CPOL_bm   0x08 /* SPI Clock polarity bit 1 mask. */

Referenced by spi_set_clock_polarity().

#define SPI_CPOL_bp   3 /* SPI Clock polarity bit 1 position. */
#define SPI_DORD_bm   0x20 /* Data Order Setting bit mask. */
#define SPI_DORD_bp   5 /* Data Order Setting bit position. */
#define SPI_ENABLE_bm   0x40 /* Enable Module bit mask. */
#define SPI_ENABLE_bp   6 /* Enable Module bit position. */
#define SPI_IF_bm   0x80 /* Interrupt Flag bit mask. */

Referenced by spi_is_tx_ok().

#define SPI_IF_bp   7 /* Interrupt Flag bit position. */
#define SPI_MASTER_bm   0x10 /* Master Operation Enable bit mask. */
#define SPI_MASTER_bp   4 /* Master Operation Enable bit position. */
#define SPI_MODE0   0
#define SPI_MODE1   (SPI_CPHA)
#define SPI_MODE2   (SPI_CPOL)
#define SPI_MODE3   (SPI_CPOL | SPI_CPHA)
#define SPI_MODE_gm   0x0C /* SPI Mode group mask. */
#define SPI_MODE_gp   2 /* SPI Mode group position. */
#define SPI_PRESCALER0_bm   (1 << 0) /* Prescaler bit 0 mask. */
#define SPI_PRESCALER0_bp   0 /* Prescaler bit 0 position. */
#define SPI_PRESCALER1_bm   (1 << 1) /* Prescaler bit 1 mask. */
#define SPI_PRESCALER1_bp   1 /* Prescaler bit 1 position. */
#define SPI_PRESCALER_gm   0x03 /* Prescaler group mask. */
#define SPI_PRESCALER_gp   0 /* Prescaler group position. */
#define SPI_SPIE_bm   0x80 /* Interrupt Enable bit mask. */
#define SPI_SPIE_bp   7 /* Interrupt Enable bit position. */
#define SPI_WRCOL_bm   0x40 /* Write Collision bit mask. */

Referenced by spi_is_wc_flag_set().

#define SPI_WRCOL_bp   6 /* Write Collision bit position. */

typedef void(* spi_int_callback_t)(void)

Interrupt event callback function type.

The interrupt handler can be configured to do a function callback, the callback function must match the api_int_callback_t type.

Enumerator
SPI_PRESCALER_DIV4_gc 
SPI_PRESCALER_DIV16_gc 
SPI_PRESCALER_DIV64_gc 
SPI_PRESCALER_DIV128_gc 

static void spi_disable ( volatile void *  spi)
inlinestatic

Disables the SPI.

Ensures that nothing is transferred while setting up buffers.

Parameters
spiBase address of the SPI instance.
Warning
This may cause data loss if used on a slave SPI.

References SPI_ENABLE_bm.

Referenced by epd_spi_detach().

static void spi_disable_interrupt ( volatile void *  spi)
inlinestatic

Disables the SPI's interrupt.

Parameters
spiBase address of the SPI instance.

References SPI_SPIE_bm.

static void spi_double_speed_disable ( volatile void *  spi)
inlinestatic

Disable SPI Double Speed.

Parameters
spiBase address of the SPI instance.

References SPI_CLK2X_bm.

Referenced by spi_set_baud_div().

static void spi_double_speed_enable ( volatile void *  spi)
inlinestatic

Enable SPI Double Speed.

Parameters
spiBase address of the SPI instance.

References SPI_CLK2X_bm.

Referenced by spi_set_baud_div().

static void spi_enable ( volatile void *  spi)
inlinestatic

Enables the SPI.

Parameters
spiBase address of the SPI instance.

References SPI_ENABLE_bm.

Referenced by at45dbx_spi_init(), epd_spi_init(), main(), run_baudrate_set_test(), and run_spi_transfer_test().

static void spi_enable_interrupt ( volatile void *  spi)
inlinestatic

Enables the SPI in interrupt mode.

Parameters
spiBase address of the SPI instance.

References SPI_SPIE_bm.

Referenced by spi_slave_init().

static void spi_enable_master_mode ( volatile void *  spi)
inlinestatic

Activate SPI master mode of a SPI peripheral.

Parameters
spiBase address of the SPI instance.

References SPI_MASTER_bm.

Referenced by run_spi_transfer_test(), and spi_master_init().

static void spi_enable_slave_mode ( volatile void *  spi)
inlinestatic

Activate SPI slave mode of a SPI peripheral.

Parameters
spiBase address of the SPI instance.

References SPI_MASTER_bm.

Referenced by spi_slave_init().

static uint8_t spi_get ( volatile void *  spi)
inlinestatic

Get one data byte from SPI peripheral.

Parameters
spiBase address of the SPI instance.
Returns
The data byte

Referenced by spi_interrupt_callback(), spi_read(), and spi_read_single().

static bool spi_is_enabled ( volatile void *  spi)
inlinestatic

Tests if the SPI is enabled.

Parameters
spiBase address of the SPI instance.
Returns
1 if the SPI is enabled, otherwise 0.

References SPI_ENABLE_bm.

Referenced by at45dbx_spi_init().

static bool spi_is_tx_ok ( volatile void *  spi)
inlinestatic

Tests if the SPI transfer is complete.

Parameters
spiBase address of the SPI instance.
Returns
1 if the SPI transfer is complete, otherwise 0.

References SPI_IF_bm.

Referenced by spi_is_rx_full(), spi_is_rx_ready(), spi_is_tx_empty(), spi_is_tx_ready(), spi_read(), and spi_write().

static bool spi_is_wc_flag_set ( volatile void *  spi)
inlinestatic

Tests if the Write collision occurs during SPI transfer.

Parameters
spiBase address of the SPI instance.
Returns
1 if the SPI Write collision occurred, otherwise 0.

References SPI_WRCOL_bm.

static void spi_put ( volatile void *  spi,
uint8_t  data 
)
inlinestatic

Put one data byte to SPI peripheral.

Parameters
spiBase address of the SPI instance.
dataThe data byte to be loaded

References data.

Referenced by spi_interrupt_callback(), spi_read(), spi_write(), and spi_write_single().

int8_t spi_set_baud_div ( volatile void *  spi,
uint32_t  baudrate,
uint32_t  clkper_hz 
)

Calculates the SPI baudrate divider.

Parameters
baudrateThe expected baudrate on the SPI.
clkper_hzSPI module input clock frequency (Peripheral clock, Hz).
spiThe SPI module address
Returns
Status of operation.
Return values
>=0Success.
<0Error.

References Assert, spi_double_speed_disable(), spi_double_speed_enable(), SPI_PRESCALER_DIV128_gc, SPI_PRESCALER_DIV16_gc, SPI_PRESCALER_DIV4_gc, SPI_PRESCALER_DIV64_gc, and SPI_PRESCALER_gm.

Referenced by run_baudrate_set_test(), run_spi_transfer_test(), and spi_master_setup_device().

static void spi_set_clock_mode ( volatile void *  spi,
uint8_t  mode 
)
inlinestatic

Set SPI clock mode.

Parameters
spiBase address of the SPI instance.
modeValue of the mode to be set.

References SPI_MODE_gm, and SPI_MODE_gp.

Referenced by run_spi_transfer_test(), and spi_slave_init().

static void spi_set_clock_phase ( volatile void *  spi,
bool  polarity 
)
inlinestatic

Set Data Capture Phase.

Parameters
spiBase address of the SPI instance.
polaritySet clock phase as logical one(high)/zero(low).

References SPI_CPHA_bm.

static void spi_set_clock_polarity ( volatile void *  spi,
bool  polarity 
)
inlinestatic

Set clock default state.

Parameters
spiBase address of the SPI instance.
polaritySet clock polarity as logical one(high)/zero(low).

References SPI_CPOL_bm.

static void spi_set_dord_lsb ( volatile void *  spi)
inlinestatic

Set SPI data order to LSB first.

Parameters
spiBase address of the SPI instance.

References SPI_DORD_bm.

static void spi_set_dord_msb ( volatile void *  spi)
inlinestatic

Set SPI data order to MSB first.

Parameters
spiBase address of the SPI instance.

References SPI_DORD_bm.

void spi_set_interrupt_callback ( spi_int_callback_t  callback)

Set SPI interrupt callback function.

This function allows the caller to set and change the interrupt callback function. Without setting a callback function the interrupt handler in the driver will only clear the interrupt flags.

Parameters
callbackReference to a callback function

References spi_int_callback.

Referenced by spi_slave_init().