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().
enum spi_status_t |
int16_t getBaudDiv | ( | const uint32_t | baudrate, |
uint32_t | pb_hz | ||
) |
Calculates the baudrate divider.
baudrate | Baudrate value. |
pb_hz | SPI module input clock frequency (PBA clock, Hz). |
>=0 | Success. |
<0 | Error. |
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.
spi | Base address of the SPI instance. |
|
inlinestatic |
Disable Chip Select Decoding of the SPI.
spi | Base address of the SPI instance. |
Referenced by spi_master_init().
|
inlinestatic |
Disable Loopback of the SPI.
spi | Base address of the SPI instance. |
Referenced by spi_master_init().
|
inlinestatic |
Disable Modfault of the SPI.
spi | Base address of the SPI instance. |
Referenced by spi_master_init().
|
inlinestatic |
Disable Variable Chip Select of the SPI.
spi | Base address of the SPI instance. |
Referenced by spi_master_init().
void spi_enable | ( | volatile avr32_spi_t * | spi | ) |
Enables the SPI.
spi | Base address of the SPI instance. |
Referenced by at45dbx_spi_init(), and memories_initialization().
|
inlinestatic |
Enable Active mode of a Chip Selects of the SPI.
spi | Base address of the SPI instance. |
chip_select | Chip Select. |
Referenced by spi_master_setup_device().
|
inlinestatic |
Enable Chip Select Decoding of the SPI.
spi | Base address of the SPI instance. |
|
inlinestatic |
Enable Loopback of the SPI.
spi | Base address of the SPI instance. |
|
inlinestatic |
Enable Modfault of the SPI.
spi | Base address of the SPI instance. |
|
inlinestatic |
Enable Variable Chip Select of the SPI.
spi | Base address of the SPI instance. |
|
inlinestatic |
Get one data to a SPI peripheral.
spi | Base address of the SPI instance. |
Referenced by spi_read_single().
uint8_t spi_getStatus | ( | volatile avr32_spi_t * | spi | ) |
Gets status information from the SPI.
spi | Base address of the SPI instance. |
SPI_OK | Success. |
SPI_ERROR_OVERRUN | Overrun error. |
SPI_ERROR_MODE_FAULT | Mode fault (SPI addressed as slave while in master mode). |
SPI_ERROR_OVERRUN_AND_MODE_FAULT | Overrun 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.
spi | Base address of the SPI instance. |
options | Pointer to a structure containing initialization options. |
SPI_OK | Success. |
SPI_ERROR_ARGUMENT | Invalid 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.
spi | Base address of the SPI instance. |
bits | Number of bits in each transmitted character (8 to 16). |
spi_mode | Clock polarity and phase. |
SPI_OK | Success. |
SPI_ERROR_ARGUMENT | Invalid 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).
spi | Base address of the SPI instance. |
SPI_OK | Success. |
References SPI_OK.
bool spi_is_enabled | ( | volatile avr32_spi_t * | spi | ) |
Tests if the SPI is enabled.
spi | Base address of the SPI instance. |
true
if the SPI is enabled, otherwise false
. Referenced by at45dbx_spi_init(), and memories_initialization().
|
inlinestatic |
Check if the SPI contains a received character.
spi | Base address of the SPI instance. |
1
if the SPI Receive Holding Register is full, otherwise 0
.
|
inlinestatic |
Checks if all reception is ready.
spi | Base address of the SPI instance. |
1
if the SPI Receiver is ready, otherwise 0
.
|
inlinestatic |
Checks if all transmissions are complete.
spi | Base address of the SPI instance. |
1 | All transmissions complete. |
0 | Transmissions not complete. |
|
inlinestatic |
Checks if all transmissions is ready.
spi | Base address of the SPI instance. |
1 | All transmissions complete. |
0 | Transmissions not complete. |
|
inlinestatic |
Put one data to a SPI peripheral.
spi | Base address of the SPI instance. |
data | The 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.
spi | Base address of the SPI instance. |
data | Pointer to the location where to store the received data word. |
SPI_OK | Success. |
SPI_ERROR_TIMEOUT | Time-out. |
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.
spi | Base address of the SPI instance. |
1 | Some data in RDR. |
0 | No data in RDR. |
|
inlinestatic |
Reset the SPI.
Resets the SPI controller.
spi | Base 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.
spi | Base address of the SPI instance. |
chip | Slave chip number (normal: 0 to 3, externally decoded signal: 0 to 14). |
SPI_OK | Success. |
SPI_ERROR_ARGUMENT | Invalid 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).
spi | Base address of the SPI instance. |
variable_ps | Target slave is selected in transfer register for every character to transmit. |
pcs_decode | The four chip select lines are decoded externally. Values 0 to 14 can be given to spi_selectChip. |
delay | Delay in PBA periods between chip selects. |
SPI_OK | Success. |
SPI_ERROR_ARGUMENT | Invalid 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().
|
inlinestatic |
Set baudrate for a Chip Selects of the SPI.
spi | Base address of the SPI instance. |
chip_select | Chip Select. |
scbr | Baudrate Register. |
Referenced by spi_master_setup_device().
|
inlinestatic |
Set Delay Before SPCK on a Chip Selects of the SPI.
spi | Base address of the SPI instance. |
chip_select | Chip Select. |
len | Bits per Transfer [8...16]. |
References Assert.
Referenced by spi_master_setup_device().
|
inlinestatic |
Set Chip Select of the SPI.
spi | Base address of the SPI instance. |
chip_select | Chip Select. |
Referenced by spi_master_init().
|
inlinestatic |
Set Delay Between Consecutive Transfer on a Chip Selects of the SPI.
spi | Base address of the SPI instance. |
chip_select | Chip Select. |
delay | Delay. |
References Assert.
Referenced by spi_master_setup_device().
|
inlinestatic |
Set Delay Before SPCK on a Chip Selects of the SPI.
spi | Base address of the SPI instance. |
chip_select | Chip Select. |
delay | Delay. |
References Assert.
Referenced by spi_master_setup_device().
|
inlinestatic |
Set Delay Between Chip Selects of the SPI.
spi | Base address of the SPI instance. |
delay | Delay. |
Referenced by spi_master_init().
|
inlinestatic |
Set Master Mode of the SPI.
spi | Base address of the SPI instance. |
Referenced by spi_master_init().
|
inlinestatic |
Set Mode of the SPI.
spi | Base address of the SPI instance. |
chip_select | Chip Select. |
flags | SPI Mode. |
Referenced by spi_master_setup_device().
|
inlinestatic |
Set Slave Mode of the SPI.
spi | Base 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.
spi | Base address of the SPI instance. |
options | Pointer to a structure containing initialization options for an SPI channel. |
pb_hz | SPI module input clock frequency (PBA clock, Hz). |
SPI_OK | Success. |
SPI_ERROR_ARGUMENT | Invalid 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.
spi | Base address of the SPI instance. |
chip | Slave chip number (normal: 0 to 3, externally decoded signal: 0 to 14). |
SPI_OK | Success. |
SPI_ERROR_TIMEOUT | Time-out. |
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.
spi | Base address of the SPI instance. |
data | The data word to write. |
pcs | Slave selector (bit 0 -> nCS line 0, bit 1 -> nCS line 1, etc.). |
lastxfer | Boolean indicating whether this is the last data word transfer. |
SPI_OK | Success. |
SPI_ERROR_TIMEOUT | Time-out. |
SPI_ERROR_ARGUMENT | Invalid argument(s) passed. |
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.
spi | Base address of the SPI instance. |
data | The data word to write. |
SPI_OK | Success. |
SPI_ERROR_TIMEOUT | Time-out. |
References SPI_ERROR_TIMEOUT, SPI_OK, and SPI_TIMEOUT.
uint8_t spi_writeEndCheck | ( | volatile avr32_spi_t * | spi | ) |
Checks if all transmissions are complete.
spi | Base address of the SPI instance. |
1 | All transmissions complete. |
0 | Transmissions not complete. |
uint8_t spi_writeRegisterEmptyCheck | ( | volatile avr32_spi_t * | spi | ) |
Checks if there is no data in the transmit register.
spi | Base address of the SPI instance. |
1 | No data in TDR. |
0 | Some data in TDR. |