<<

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.

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

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).

void spi_master_shutdown(spi_master_interface &spi_if)

Stops the clocks running.

Should be called when all SPI input and output is completed.

Parameters:
  • spi_if – Resources for the SPI interface being shutdown

Receive Functions

unsigned char spi_master_in_byte(spi_master_interface &spi_if)

Receive one byte.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI interface
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.

Parameters:
  • spi_if – Resources for the SPI interface
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.

Parameters:
  • spi_if – Resources for the SPI interface
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.

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_byte(spi_master_interface &spi_if, unsigned char data)

Transmit one byte.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI interface
  • data – The byte to transmit
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.

Parameters:
  • spi_if – Resources for the SPI interface
  • data – The short to transmit
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.

Parameters:
  • spi_if – Resources for the SPI interface
  • data – The word to transmit
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.

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

void spi_slave_init(spi_slave_interface &spi_if)

Configure ports and clocks, clearing port buffers.

Must be called before any SPI data input or output functions are used.

Parameters:
  • spi_if – Resources for the SPI interface being initialised
void spi_slave_shutdown(spi_slave_interface &spi_if)

Stops the clocks running, and disables the ports.

Should be called when all SPI input and output is completed.

Parameters:
  • spi_if – Resources for the SPI interface being shutdown

Receive Functions

unsigned char spi_slave_in_byte(spi_slave_interface &spi_if)

Receive one byte.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI interface
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.

Parameters:
  • spi_if – Resources for the SPI interface
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.

Parameters:
  • spi_if – Resources for the SPI interface
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.

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_byte(spi_slave_interface &spi_if, unsigned char data)

Transmit one byte.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI interface
  • data – The byte to transmit
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.

Parameters:
  • spi_if – Resources for the SPI interface
  • data – The short to transmit
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.

Parameters:
  • spi_if – Resources for the SPI interface
  • data – The word to transmit
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.

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