Microchip® Advanced Software Framework

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

See Quick start guide for SPI driver on AVR32 devices.

Driver for the SPI (Serial Peripheral Interface). The SPI circuit is a synchronous serial data link that provides communication with external devices in Master or Slave mode. Connection to Peripheral DMA Controller channel capabilities optimizes data transfers.

Data Structures

struct  spi_options_t
 Option structure for SPI channels. More...
 

Macros

#define SPI_MODE_0   0
 SPI Mode 0. More...
 
#define SPI_MODE_1   1
 SPI Mode 1. More...
 
#define SPI_MODE_2   2
 SPI Mode 2. More...
 
#define SPI_MODE_3   3
 SPI Mode 3. More...
 
#define SPI_TIMEOUT   15000
 Time-out value (number of attempts). More...
 

Enumerations

enum  spi_status_t {
  SPI_ERROR = -1,
  SPI_OK = 0,
  SPI_ERROR_TIMEOUT = 1,
  SPI_ERROR_ARGUMENT,
  SPI_ERROR_OVERRUN,
  SPI_ERROR_MODE_FAULT,
  SPI_ERROR_OVERRUN_AND_MODE_FAULT
}
 Status codes used by the SPI driver. More...
 

Functions

int16_t getBaudDiv (const uint32_t baudrate, uint32_t pb_hz)
 Calculates the baudrate divider. More...
 
void spi_disable (volatile avr32_spi_t *spi)
 Disables the SPI. More...
 
static void spi_disable_chipselect_decoding (volatile avr32_spi_t *spi)
 Disable Chip Select Decoding of the SPI. More...
 
static void spi_disable_loopback (volatile avr32_spi_t *spi)
 Disable Loopback of the SPI. More...
 
static void spi_disable_modfault (volatile avr32_spi_t *spi)
 Disable Modfault of the SPI. More...
 
static void spi_disable_variable_chipselect (volatile avr32_spi_t *spi)
 Disable Variable Chip Select of the SPI. More...
 
void spi_enable (volatile avr32_spi_t *spi)
 Enables the SPI. More...
 
static void spi_enable_active_mode (volatile avr32_spi_t *spi, uint8_t chip_select)
 Enable Active mode of a Chip Selects of the SPI. More...
 
static void spi_enable_chipselect_decoding (volatile avr32_spi_t *spi)
 Enable Chip Select Decoding of the SPI. More...
 
static void spi_enable_loopback (volatile avr32_spi_t *spi)
 Enable Loopback of the SPI. More...
 
static void spi_enable_modfault (volatile avr32_spi_t *spi)
 Enable Modfault of the SPI. More...
 
static void spi_enable_variable_chipselect (volatile avr32_spi_t *spi)
 Enable Variable Chip Select of the SPI. More...
 
static uint16_t spi_get (volatile avr32_spi_t *spi)
 Get one data to a SPI peripheral. More...
 
uint8_t spi_getStatus (volatile avr32_spi_t *spi)
 Gets status information from the SPI. More...
 
spi_status_t spi_initMaster (volatile avr32_spi_t *spi, const spi_options_t *options)
 Initializes the SPI in master mode. More...
 
spi_status_t spi_initSlave (volatile avr32_spi_t *spi, uint8_t bits, uint8_t spi_mode)
 Initializes the SPI in slave mode. More...
 
spi_status_t spi_initTest (volatile avr32_spi_t *spi)
 Sets up the SPI in a test mode where the transmitter is connected to the receiver (local loopback). More...
 
bool spi_is_enabled (volatile avr32_spi_t *spi)
 Tests if the SPI is enabled. More...
 
static bool spi_is_rx_full (volatile avr32_spi_t *spi)
 Check if the SPI contains a received character. More...
 
static bool spi_is_rx_ready (volatile avr32_spi_t *spi)
 Checks if all reception is ready. More...
 
static bool spi_is_tx_empty (volatile avr32_spi_t *spi)
 Checks if all transmissions are complete. More...
 
static bool spi_is_tx_ready (volatile avr32_spi_t *spi)
 Checks if all transmissions is ready. More...
 
static void spi_put (volatile avr32_spi_t *spi, uint16_t data)
 Put one data to a SPI peripheral. More...
 
spi_status_t spi_read (volatile avr32_spi_t *spi, uint16_t *data)
 Reads one data word in master mode or in slave mode. More...
 
uint8_t spi_readRegisterFullCheck (volatile avr32_spi_t *spi)
 Checks if there is data in the receive register. More...
 
static void spi_reset (volatile avr32_spi_t *spi)
 Reset the SPI. More...
 
spi_status_t spi_selectChip (volatile avr32_spi_t *spi, uint8_t chip)
 Selects slave chip. More...
 
spi_status_t spi_selectionMode (volatile avr32_spi_t *spi, uint8_t variable_ps, uint8_t pcs_decode, uint8_t delay)
 Sets up how and when the slave chips are selected (master mode only). More...
 
static void spi_set_baudrate_register (volatile avr32_spi_t *spi, uint8_t chip_select, uint8_t scbr)
 Set baudrate for a Chip Selects of the SPI. More...
 
static void spi_set_bits_per_transfer (volatile avr32_spi_t *spi, uint8_t chip_select, uint8_t len)
 Set Delay Before SPCK on a Chip Selects of the SPI. More...
 
static void spi_set_chipselect (volatile avr32_spi_t *spi, uint8_t chip_select)
 Set Chip Select of the SPI. More...
 
static void spi_set_chipselect_delay_bct (volatile avr32_spi_t *spi, uint8_t chip_select, uint8_t delay)
 Set Delay Between Consecutive Transfer on a Chip Selects of the SPI. More...
 
static void spi_set_chipselect_delay_bs (volatile avr32_spi_t *spi, uint8_t chip_select, uint8_t delay)
 Set Delay Before SPCK on a Chip Selects of the SPI. More...
 
static void spi_set_delay (volatile avr32_spi_t *spi, uint8_t delay)
 Set Delay Between Chip Selects of the SPI. More...
 
static void spi_set_master_mode (volatile avr32_spi_t *spi)
 Set Master Mode of the SPI. More...
 
static void spi_set_mode (volatile avr32_spi_t *spi, uint8_t chip_select, uint8_t flags)
 Set Mode of the SPI. More...
 
static void spi_set_slave_mode (volatile avr32_spi_t *spi)
 Set Slave Mode of the SPI. More...
 
spi_status_t spi_setupChipReg (volatile avr32_spi_t *spi, const spi_options_t *options, uint32_t pb_hz)
 Sets options for a specific slave chip. More...
 
spi_status_t spi_unselectChip (volatile avr32_spi_t *spi, uint8_t chip)
 Unselects slave chip. More...
 
spi_status_t spi_variableSlaveWrite (volatile avr32_spi_t *spi, uint16_t data, uint8_t pcs, uint8_t lastxfer)
 Selects a slave in master variable peripheral select mode and writes one data word to it. More...
 
spi_status_t spi_write (volatile avr32_spi_t *spi, uint16_t data)
 Writes one data word in master fixed peripheral select mode or in slave mode. More...
 
uint8_t spi_writeEndCheck (volatile avr32_spi_t *spi)
 Checks if all transmissions are complete. More...
 
uint8_t spi_writeRegisterEmptyCheck (volatile avr32_spi_t *spi)
 Checks if there is no data in the transmit register. More...
 

#define SPI_MODE_0   0

SPI Mode 0.

#define SPI_MODE_1   1

SPI Mode 1.

#define SPI_MODE_2   2

SPI Mode 2.

#define SPI_MODE_3   3

SPI Mode 3.

#define SPI_TIMEOUT   15000

Time-out value (number of attempts).

Referenced by spi_read(), spi_read_packet(), spi_unselectChip(), spi_variableSlaveWrite(), spi_write(), and spi_write_packet().

Status codes used by the SPI driver.

Enumerator
SPI_ERROR 
SPI_OK 
SPI_ERROR_TIMEOUT 
SPI_ERROR_ARGUMENT 
SPI_ERROR_OVERRUN 
SPI_ERROR_MODE_FAULT 
SPI_ERROR_OVERRUN_AND_MODE_FAULT 

int16_t getBaudDiv ( const uint32_t  baudrate,
uint32_t  pb_hz 
)

Calculates the baudrate divider.

Parameters
baudrateBaudrate value.
pb_hzSPI module input clock frequency (PBA clock, Hz).
Returns
Divider or error code.
Return values
>=0Success.
<0Error.

References div_ceil.

Referenced by spi_master_setup_device(), and spi_setupChipReg().

void spi_disable ( volatile avr32_spi_t *  spi)

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.
static void spi_disable_chipselect_decoding ( volatile avr32_spi_t *  spi)
inlinestatic

Disable Chip Select Decoding of the SPI.

Parameters
spiBase address of the SPI instance.

Referenced by spi_master_init().

static void spi_disable_loopback ( volatile avr32_spi_t *  spi)
inlinestatic

Disable Loopback of the SPI.

Parameters
spiBase address of the SPI instance.

Referenced by spi_master_init().

static void spi_disable_modfault ( volatile avr32_spi_t *  spi)
inlinestatic

Disable Modfault of the SPI.

Parameters
spiBase address of the SPI instance.

Referenced by spi_master_init().

static void spi_disable_variable_chipselect ( volatile avr32_spi_t *  spi)
inlinestatic

Disable Variable Chip Select of the SPI.

Parameters
spiBase address of the SPI instance.

Referenced by spi_master_init().

void spi_enable ( volatile avr32_spi_t *  spi)

Enables the SPI.

Parameters
spiBase address of the SPI instance.

Referenced by at45dbx_spi_init(), and memories_initialization().

static void spi_enable_active_mode ( volatile avr32_spi_t *  spi,
uint8_t  chip_select 
)
inlinestatic

Enable Active mode of a Chip Selects of the SPI.

Parameters
spiBase address of the SPI instance.
chip_selectChip Select.

Referenced by spi_master_setup_device().

static void spi_enable_chipselect_decoding ( volatile avr32_spi_t *  spi)
inlinestatic

Enable Chip Select Decoding of the SPI.

Parameters
spiBase address of the SPI instance.
static void spi_enable_loopback ( volatile avr32_spi_t *  spi)
inlinestatic

Enable Loopback of the SPI.

Parameters
spiBase address of the SPI instance.
static void spi_enable_modfault ( volatile avr32_spi_t *  spi)
inlinestatic

Enable Modfault of the SPI.

Parameters
spiBase address of the SPI instance.
static void spi_enable_variable_chipselect ( volatile avr32_spi_t *  spi)
inlinestatic

Enable Variable Chip Select of the SPI.

Parameters
spiBase address of the SPI instance.
static uint16_t spi_get ( volatile avr32_spi_t *  spi)
inlinestatic

Get one data to a SPI peripheral.

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

Referenced by spi_read_single().

uint8_t spi_getStatus ( volatile avr32_spi_t *  spi)

Gets status information from the SPI.

Parameters
spiBase address of the SPI instance.
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_OVERRUNOverrun error.
SPI_ERROR_MODE_FAULTMode fault (SPI addressed as slave while in master mode).
SPI_ERROR_OVERRUN_AND_MODE_FAULTOverrun error and mode fault.

References SPI_ERROR_MODE_FAULT, SPI_ERROR_OVERRUN, SPI_ERROR_OVERRUN_AND_MODE_FAULT, and SPI_OK.

spi_status_t spi_initMaster ( volatile avr32_spi_t *  spi,
const spi_options_t options 
)

Initializes the SPI in master mode.

Parameters
spiBase address of the SPI instance.
optionsPointer to a structure containing initialization options.
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_ARGUMENTInvalid argument(s) passed.

References spi_options_t::modfdis, u_avr32_spi_mr_t::mr, u_avr32_spi_mr_t::MR, SPI_ERROR_ARGUMENT, and SPI_OK.

Referenced by memories_initialization().

spi_status_t spi_initSlave ( volatile avr32_spi_t *  spi,
uint8_t  bits,
uint8_t  spi_mode 
)

Initializes the SPI in slave mode.

Parameters
spiBase address of the SPI instance.
bitsNumber of bits in each transmitted character (8 to 16).
spi_modeClock polarity and phase.
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_ARGUMENTInvalid argument(s) passed.

References SPI_ERROR_ARGUMENT, and SPI_OK.

spi_status_t spi_initTest ( volatile avr32_spi_t *  spi)

Sets up the SPI in a test mode where the transmitter is connected to the receiver (local loopback).

Parameters
spiBase address of the SPI instance.
Returns
Status.
Return values
SPI_OKSuccess.

References SPI_OK.

bool spi_is_enabled ( volatile avr32_spi_t *  spi)

Tests if the SPI is enabled.

Parameters
spiBase address of the SPI instance.
Returns
true if the SPI is enabled, otherwise false.

Referenced by at45dbx_spi_init(), and memories_initialization().

static bool spi_is_rx_full ( volatile avr32_spi_t *  spi)
inlinestatic

Check if the SPI contains a received character.

Parameters
spiBase address of the SPI instance.
Returns
1 if the SPI Receive Holding Register is full, otherwise 0.
static bool spi_is_rx_ready ( volatile avr32_spi_t *  spi)
inlinestatic

Checks if all reception is ready.

Parameters
spiBase address of the SPI instance.
Returns
1 if the SPI Receiver is ready, otherwise 0.
static bool spi_is_tx_empty ( volatile avr32_spi_t *  spi)
inlinestatic

Checks if all transmissions are complete.

Parameters
spiBase address of the SPI instance.
Returns
Status.
Return values
1All transmissions complete.
0Transmissions not complete.
static bool spi_is_tx_ready ( volatile avr32_spi_t *  spi)
inlinestatic

Checks if all transmissions is ready.

Parameters
spiBase address of the SPI instance.
Returns
Status.
Return values
1All transmissions complete.
0Transmissions not complete.
static void spi_put ( volatile avr32_spi_t *  spi,
uint16_t  data 
)
inlinestatic

Put one data to a SPI peripheral.

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

Referenced by spi_write_single().

spi_status_t spi_read ( volatile avr32_spi_t *  spi,
uint16_t *  data 
)

Reads one data word in master mode or in slave mode.

Parameters
spiBase address of the SPI instance.
dataPointer to the location where to store the received data word.
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_TIMEOUTTime-out.
Note
Will block program execution until time-out occurs if no data is received or last transmission is not complete. Invoke spi_writeEndCheck or spi_readRegisterFullCheck beforehand if needed.

References SPI_ERROR_TIMEOUT, SPI_OK, and SPI_TIMEOUT.

uint8_t spi_readRegisterFullCheck ( volatile avr32_spi_t *  spi)

Checks if there is data in the receive register.

Parameters
spiBase address of the SPI instance.
Returns
Status.
Return values
1Some data in RDR.
0No data in RDR.
void spi_reset ( volatile avr32_spi_t *  spi)
inlinestatic

Reset the SPI.

Resets the SPI controller.

Parameters
spiBase address of the SPI instance.

Referenced by spi_master_init().

spi_status_t spi_selectChip ( volatile avr32_spi_t *  spi,
uint8_t  chip 
)

Selects slave chip.

Parameters
spiBase address of the SPI instance.
chipSlave chip number (normal: 0 to 3, externally decoded signal: 0 to 14).
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_ARGUMENTInvalid argument(s) passed.

References SPI_ERROR_ARGUMENT, and SPI_OK.

Referenced by spi_select_device().

spi_status_t spi_selectionMode ( volatile avr32_spi_t *  spi,
uint8_t  variable_ps,
uint8_t  pcs_decode,
uint8_t  delay 
)

Sets up how and when the slave chips are selected (master mode only).

Parameters
spiBase address of the SPI instance.
variable_psTarget slave is selected in transfer register for every character to transmit.
pcs_decodeThe four chip select lines are decoded externally. Values 0 to 14 can be given to spi_selectChip.
delayDelay in PBA periods between chip selects.
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_ARGUMENTInvalid argument(s) passed.

References u_avr32_spi_mr_t::mr, u_avr32_spi_mr_t::MR, SPI_ERROR_ARGUMENT, and SPI_OK.

Referenced by memories_initialization().

static void spi_set_baudrate_register ( volatile avr32_spi_t *  spi,
uint8_t  chip_select,
uint8_t  scbr 
)
inlinestatic

Set baudrate for a Chip Selects of the SPI.

Parameters
spiBase address of the SPI instance.
chip_selectChip Select.
scbrBaudrate Register.

Referenced by spi_master_setup_device().

static void spi_set_bits_per_transfer ( volatile avr32_spi_t *  spi,
uint8_t  chip_select,
uint8_t  len 
)
inlinestatic

Set Delay Before SPCK on a Chip Selects of the SPI.

Parameters
spiBase address of the SPI instance.
chip_selectChip Select.
lenBits per Transfer [8...16].

References Assert.

Referenced by spi_master_setup_device().

static void spi_set_chipselect ( volatile avr32_spi_t *  spi,
uint8_t  chip_select 
)
inlinestatic

Set Chip Select of the SPI.

Parameters
spiBase address of the SPI instance.
chip_selectChip Select.

Referenced by spi_master_init().

static void spi_set_chipselect_delay_bct ( volatile avr32_spi_t *  spi,
uint8_t  chip_select,
uint8_t  delay 
)
inlinestatic

Set Delay Between Consecutive Transfer on a Chip Selects of the SPI.

Parameters
spiBase address of the SPI instance.
chip_selectChip Select.
delayDelay.

References Assert.

Referenced by spi_master_setup_device().

static void spi_set_chipselect_delay_bs ( volatile avr32_spi_t *  spi,
uint8_t  chip_select,
uint8_t  delay 
)
inlinestatic

Set Delay Before SPCK on a Chip Selects of the SPI.

Parameters
spiBase address of the SPI instance.
chip_selectChip Select.
delayDelay.

References Assert.

Referenced by spi_master_setup_device().

static void spi_set_delay ( volatile avr32_spi_t *  spi,
uint8_t  delay 
)
inlinestatic

Set Delay Between Chip Selects of the SPI.

Parameters
spiBase address of the SPI instance.
delayDelay.

Referenced by spi_master_init().

static void spi_set_master_mode ( volatile avr32_spi_t *  spi)
inlinestatic

Set Master Mode of the SPI.

Parameters
spiBase address of the SPI instance.

Referenced by spi_master_init().

static void spi_set_mode ( volatile avr32_spi_t *  spi,
uint8_t  chip_select,
uint8_t  flags 
)
inlinestatic

Set Mode of the SPI.

Parameters
spiBase address of the SPI instance.
chip_selectChip Select.
flagsSPI Mode.

Referenced by spi_master_setup_device().

static void spi_set_slave_mode ( volatile avr32_spi_t *  spi)
inlinestatic

Set Slave Mode of the SPI.

Parameters
spiBase address of the SPI instance.
spi_status_t spi_setupChipReg ( volatile avr32_spi_t *  spi,
const spi_options_t options,
uint32_t  pb_hz 
)

Sets options for a specific slave chip.

The baudrate field has to be written before transfer in master mode. Four similar registers exist, one for each slave. When using encoded slave addressing, reg=0 sets options for slaves 0 to 3, reg=1 for slaves 4 to 7 and so on.

Parameters
spiBase address of the SPI instance.
optionsPointer to a structure containing initialization options for an SPI channel.
pb_hzSPI module input clock frequency (PBA clock, Hz).
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_ARGUMENTInvalid argument(s) passed.

References spi_options_t::baudrate, spi_options_t::bits, u_avr32_spi_csr_t::csr, u_avr32_spi_csr_t::CSR, getBaudDiv(), spi_options_t::reg, spi_options_t::spck_delay, SPI_ERROR_ARGUMENT, spi_options_t::spi_mode, SPI_OK, spi_options_t::stay_act, and spi_options_t::trans_delay.

spi_status_t spi_unselectChip ( volatile avr32_spi_t *  spi,
uint8_t  chip 
)

Unselects slave chip.

Parameters
spiBase address of the SPI instance.
chipSlave chip number (normal: 0 to 3, externally decoded signal: 0 to 14).
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_TIMEOUTTime-out.
Note
Will block program execution until time-out occurs if last transmission is not complete. Invoke spi_writeEndCheck beforehand if needed.

References SPI_ERROR_TIMEOUT, SPI_OK, and SPI_TIMEOUT.

Referenced by spi_deselect_device().

spi_status_t spi_variableSlaveWrite ( volatile avr32_spi_t *  spi,
uint16_t  data,
uint8_t  pcs,
uint8_t  lastxfer 
)

Selects a slave in master variable peripheral select mode and writes one data word to it.

Parameters
spiBase address of the SPI instance.
dataThe data word to write.
pcsSlave selector (bit 0 -> nCS line 0, bit 1 -> nCS line 1, etc.).
lastxferBoolean indicating whether this is the last data word transfer.
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_TIMEOUTTime-out.
SPI_ERROR_ARGUMENTInvalid argument(s) passed.
Note
Will block program execution until time-out occurs if transmitter is busy and transmit buffer is full. Invoke spi_writeRegisterEmptyCheck beforehand if needed.
Once the data has been written to the transmit buffer, the end of transmission is not waited for. Invoke spi_writeEndCheck if needed.

References SPI_ERROR_ARGUMENT, SPI_ERROR_TIMEOUT, SPI_OK, and SPI_TIMEOUT.

spi_status_t spi_write ( volatile avr32_spi_t *  spi,
uint16_t  data 
)

Writes one data word in master fixed peripheral select mode or in slave mode.

Parameters
spiBase address of the SPI instance.
dataThe data word to write.
Returns
Status.
Return values
SPI_OKSuccess.
SPI_ERROR_TIMEOUTTime-out.
Note
Will block program execution until time-out occurs if transmitter is busy and transmit buffer is full. Invoke spi_writeRegisterEmptyCheck beforehand if needed.
Once the data has been written to the transmit buffer, the end of transmission is not waited for. Invoke spi_writeEndCheck if needed.

References SPI_ERROR_TIMEOUT, SPI_OK, and SPI_TIMEOUT.

uint8_t spi_writeEndCheck ( volatile avr32_spi_t *  spi)

Checks if all transmissions are complete.

Parameters
spiBase address of the SPI instance.
Returns
Status.
Return values
1All transmissions complete.
0Transmissions not complete.
uint8_t spi_writeRegisterEmptyCheck ( volatile avr32_spi_t *  spi)

Checks if there is no data in the transmit register.

Parameters
spiBase address of the SPI instance.
Returns
Status.
Return values
1No data in TDR.
0Some data in TDR.