SPI API
Configuration Defines
The file spi_conf.h can be provided in the application source code, without it
the default values specified in spi_master.h and spi_slave.h will be used.
This file can set the following defines:
SPI master Defines
DEFAULT_SPI_CLOCK_DIV
This define sets the default clock divider, which the application can use
when initialising the SPI master. See spi_clock_div parameter of
spi_master_init() in Configuration Functions for clock divider format.
SPI_MASTER_MODE
The SPI mode the master operates in.
Mode
|
CPOL
|
CPHA
|
0
|
0
|
0
|
1
|
0
|
1
|
2
|
1
|
0
|
3
|
1
|
1
|
SPI slave Defines
SPI_SLAVE_MODE
The SPI mode the slave operates in.
Mode
|
CPOL
|
CPHA
|
0
|
0
|
0
|
1
|
0
|
1
|
2
|
1
|
0
|
3
|
1
|
1
|
SPI master API
Data Structures
-
spi_master_interface
Structure containing the resources required for the SPI master interface.
It consists of two 8bit buffered output ports, and one 8bit input port.
Select lines are intentionally not part of API, they are simple port outputs, which depend on how many slaves there are and how they’re connected.
Structure Members:
-
clock blk1
-
clock blk2
-
out buffered port:8 mosi
-
out buffered port:8 sclk
-
in buffered port:8 miso
Configuration Functions
-
void spi_master_init(spi_master_interface &spi_if, int spi_clock_div)
Configure ports and clocks, clearing port buffers.
Must be called before any SPI data input or output functions are used.
Example: To achieve an sclk frequency of 25MHz, a divider of 2 must be specified, as 100(MHz)/(2*2) = 25(MHz).
Example: To achieve an sclk frequency of 625kHz, a divider of 80 must be specified, as 100(MHz)/(2*80) = 0.625(MHz).
This function has the following parameters:spi_if
Resources for the SPI interface being initialised
spi_clock_div
SPI clock frequency is fref/(2*spi_clock_div), where freq defaults to 100MHz
Stops the clocks running.
Should be called when all SPI input and output is completed.
This function has the following parameters:
Receive Functions
This function returns:
The received byte
unsigned short spi_master_in_short(spi_master_interface &spi_if)
Receive one short.
Most significant bit first order. Big endian byte order.
This function has the following parameters:
This function returns: The received short
unsigned int spi_master_in_word(spi_master_interface &spi_if)
Receive one word.
Most significant bit first order. Big endian byte order.
This function has the following parameters:
This function returns: The received word
void spi_master_in_buffer(spi_master_interface &spi_if, unsigned char buffer[], int num_bytes)
Receive specified number of bytes.
Most significant bit first order. Big endian byte order.
This function has the following parameters:spi_if
Resources for the SPI interface
buffer
The array the received data will be written to
num_bytes
The number of bytes to read from the SPI interface, this must not be greater than the size of buffer
Transmit Functions
void spi_master_out_short(spi_master_interface &spi_if, unsigned short data)
Transmit one short.
Most significant bit first order. Big endian byte order.
This function has the following parameters:
void spi_master_out_word(spi_master_interface &spi_if, unsigned int data)
Transmit one word.
Most significant bit first order. Big endian byte order.
This function has the following parameters:
void spi_master_out_buffer(spi_master_interface &spi_if, const unsigned char buffer[], int num_bytes)
Transmit specified number of bytes.
Most significant bit first order. Big endian byte order.
This function has the following parameters:spi_if
Resources for the SPI interface
buffer
The array of data to transmit
num_bytes
The number of bytes to write to the SPI interface, this must not be greater than the size of buffer
SPI slave API
Data Structures
-
spi_slave_interface
Structure containing the resources required for the SPI slave interface.
It consists of two 1bit input ports, one 8bit buffered input port, and one 8bit buffered output port.
Structure Members:
-
clock blk
-
in port ss
-
in buffered port:8 mosi
-
out buffered port:8 miso
-
in port sclk
Configuration Functions
-
Configure ports and clocks, clearing port buffers.
Must be called before any SPI data input or output functions are used.
This function has the following parameters:
Stops the clocks running, and disables the ports.
Should be called when all SPI input and output is completed.
This function has the following parameters:
Receive Functions
This function returns: The received byte
unsigned short spi_slave_in_short(spi_slave_interface &spi_if)
Receive one short.
Most significant bit first order. Big endian byte order.
This function has the following parameters:
This function returns: The received short
unsigned int spi_slave_in_word(spi_slave_interface &spi_if)
Receive one word.
Most significant bit first order. Big endian byte order.
This function has the following parameters:
This function returns: The received word
void spi_slave_in_buffer(spi_slave_interface &spi_if, unsigned char buffer[], int num_bytes)
Receive specified number of bytes.
Most significant bit first order. Big endian byte order.
This function has the following parameters:spi_if
Resources for the SPI interface
buffer
The array the received data will be written to
num_bytes
The number of bytes to read from the SPI interface, this must not be greater than the size of buffer
Transmit Functions
void spi_slave_out_short(spi_slave_interface &spi_if, unsigned short data)
Transmit one short.
Most significant bit first order. Big endian byte order.
This function has the following parameters:
void spi_slave_out_word(spi_slave_interface &spi_if, unsigned int data)
Transmit one word.
Most significant bit first order. Big endian byte order.
This function has the following parameters:
void spi_slave_out_buffer(spi_slave_interface &spi_if, const unsigned char buffer[], int num_bytes)
Transmit specified number of bytes.
Most significant bit first order. Big endian byte order.
This function has the following parameters:spi_if
Resources for the SPI interface
buffer
The array of data to transmit
num_bytes
The number of bytes to write to the SPI interface, this must not be greater than the size of buffer