4.13.1 SPI - Serial Peripheral Interface

4.13.1.1 Introduction

The Serial Peripheral Interface (SPI) module is a serial interface useful for communicating with other peripheral or microcontroller devices. These peripheral devices may be serial EEPROMs, shift registers, display drivers, Analog-to-Digital (A/D) converters, etc.

4.13.1.2 Supported Device Families

AVR® DxAVR® ExATtinyATmega

4.13.1.3 Required header files:


#include "mcc_generated_files/spi/spi[X].h"
Note: Replace [X] with the selected instance number of the SPI module.

4.13.1.4 How to use the SPI Driver

The SPI driver generates a portable API interface. For an example of how to use the interface to write and read data, refer to this examples linked below. Click the links to view the code snippets associated with each

Instructions common for all code snippets: SPI Example Snippet Instructions

  • SPI Example 1: SPI Host Loopback When in Host mode, the SPI generates the SCK clock. Data is driven out on the MOSI I/O pin according to the configured phase and polarity.
  • SPI Example 2: SPI ClientHere the Host sends bytes to the Micrcontroller running this code, acting as a client. When in Client mode, the SPI receives the SCK clock from the Host and uses this to sample data on the MOSI I/O pin. The phase and polarity must be configured according to the values used by the Host.

4.13.1.5 Module Documentation

4.13.1.5.1 SPI0

This header file provides API prototypes for the SPI0 driver.

Module description

This header file provides API prototypes for the SPI0 driver.

Version: SPI0 Driver Version 3.0.0
Data structures
Functions
  • void SPI0_Initialize (void)

    Initializes the SPI0 module.

  • void SPI0_Deinitialize (void)

    Deinitializes the SPI0 module.

  • bool SPI0_Open (uint8_t spiConfigIndex)

    Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.

  • void SPI0_Close (void)

    Closes the active configuration of the SPI0 module.

  • uint8_t SPI0_ByteExchange (uint8_t byteData)

    Exchanges one byte using the SPI protocol. This function is blocking.

  • void SPI0_BufferExchange (void *bufferData, size_t bufferSize)

    Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferWrite (void *bufferData, size_t bufferSize)

    Writes a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferRead (void *bufferData, size_t bufferSize)

    Reads a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol. This function is blocking in Polling mode.

  • uint8_t SPI0_ByteRead (void)

    Receives one byte using SPI protocol. This function is blocking.

  • bool SPI0_IsRxReady (void)

    Checks if the SPI0 module is ready to read data.

  • bool SPI0_IsTxReady (void)

    Checks if the SPI0 module is ready to write data.

  • void SPI0_RxCompleteCallbackRegister (void(*callbackHandler)(void))

    Sets the callback function to be executed at the completion of data transfer in Interrupt mode.

Function Documentation

SPI0_BufferExchange()

void SPI0_BufferExchange (void * bufferData, size_t bufferSize)

Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.

Parameters:
in,out *bufferData

Buffer address of the data to be exchanged

in bufferSize

Size of the data in bytes

Returns:

None.

SPI0_BufferRead()

void SPI0_BufferRead (void * bufferData, size_t bufferSize)

Reads a buffer using the SPI protocol. This function is blocking in Polling mode.

Parameters:
out *bufferData

Buffer address of the data to be read

in bufferSize

Size of the data in bytes

Returns:

None.

SPI0_BufferWrite()

void SPI0_BufferWrite (void * bufferData, size_t bufferSize)

Writes a buffer using the SPI protocol. This function is blocking in Polling mode.

Parameters:
in *bufferData

Buffer address of the data to be written

in bufferSize

Size of the data in bytes

Returns:

None.

SPI0_ByteExchange()

uint8_t SPI0_ByteExchange (uint8_t byteData)

Exchanges one byte using the SPI protocol. This function is blocking.

Parameters:
byteData

The byte to be written

Returns:

Received data byte

SPI0_ByteRead()

uint8_t SPI0_ByteRead (void )

Receives one byte using SPI protocol. This function is blocking.

Parameters:
None.
Returns:

Received data byte

SPI0_ByteWrite()

void SPI0_ByteWrite (uint8_t byteData)

Writes one byte using the SPI protocol. This function is blocking in Polling mode.

Parameters:
byteData

The byte to be written

Returns:

None.

SPI0_Close()

void SPI0_Close (void )

Closes the active configuration of the SPI0 module.

Parameters:
None.
Returns:

None.

SPI0_Deinitialize()

void SPI0_Deinitialize (void )

Deinitializes the SPI0 module.

Parameters:
None.
Returns:

None.

SPI0_Initialize()

void SPI0_Initialize (void )

Initializes the SPI0 module.

Parameters:
None.
Returns:

None.

SPI0_IsRxReady()

bool SPI0_IsRxReady (void )

Checks if the SPI0 module is ready to read data.

Parameters:
None.
Return values:
True

SPI0 module has data ready in buffer

False

SPI0 module is not ready to read data

SPI0_IsTxReady()

bool SPI0_IsTxReady (void )

Checks if the SPI0 module is ready to write data.

Parameters:
None.
Return values:
True

SPI0 module is ready to write data

False

SPI0 module is not ready to write data

SPI0_Open()

bool SPI0_Open (uint8_t spiConfigIndex)

Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.

Parameters:
spiConfigIndex

The Configuration index

Return values:
True

SPI0 module is enabled successfully with the chosen configuration

False

SPI0 module is already open with another configuration

SPI0_RxCompleteCallbackRegister()

void SPI0_RxCompleteCallbackRegister (void(*)(void) callbackHandler)

Sets the callback function to be executed at the completion of data transfer in Interrupt mode.

Parameters:
Pointer

to the function to be executed

Returns:

None.

Enumeration Type Documentation

SPI0_configuration_name_t

enum enum SPI0_configuration_name_t

Enumeration for the different configurations supported by the driver. A configuration is specified as parameter to SPI0_Open() and is used by the function to set SPI parameters as specified by the configuration.

SPI0_DEFAULT
SPI0_DEFAULT

4.13.1.5.2 SPI

This header file provides API prototypes for the SPI module in Polling and Interrupt mode.

Module description

This header file provides API prototypes for the SPI module in Polling and Interrupt mode.

This header file provides type definitions for the SPI module operation in Polling mode.

This header file provides type definitions for the SPI module operation in Interrupt mode.

Version: SPI Interface Version v3.0.0 SPI Driver Version 3.0.0
Data structures

Enumeration Type Documentation

spi_status_t

enum enum spi_status_t

Enumeration for the different states of the SPI module.

Note:

The enum values are chosen with a specific bit pattern to make sure no error happens in case a single bit changes.

SPI_RESET

SPI module is in Reset state

SPI_IDLE

SPI module is ready to use

SPI_BUSY

SPI module is in use

spi_transfer_type_t

enum enum spi_transfer_type_t

Enumeration for the different transfer types of the SPI module.

Note:

The enum values are chosen with a specific bit pattern to make sure no error happens in case a single bit changes.

SPI_READ

SPI Module is performing a read operation

SPI_WRITE

SPI Module is performing a write operation

SPI_EXCHANGE

SPI Module is performing a exchange operation

4.13.1.5.3 SPI_example

SPI Example Snippet Instructions

The Example code snippets explains Interface APIs Usage for the SPI Driver:
  • Check MISO/MOSI/SCK pin configurations in Pin Manager (LED pin where applicable)

  • Add SPIxPLIB to the project

  • SPIxPLIB as described

  • Generate the code and program the board

  • Add the code snippet to the application code (adding required headers and call the example snippet)

The SPI must be opened by calling <component_name>_open() before it can be used. After use, the SPI is closed by calling <component_name>_close(). A call to <component_name>_open() for a SPI which is already open will return a false value, indicating that the SPI is already busy on behalf of another process.

If several SPI hardware instances are available on the device, the user can select which SPI instance the driver shall use.

SPI Example 1: SPI Host Loopback

When in Host mode, the SPI generates the SCK clock. Data is driven out on the MOSI I/O pin according to the configured phase and polarity.

SPI Example 2: SPI Client

Here the Host sends bytes to the Micrcontroller running this code, acting as a client. When in Client mode, the SPI receives the SCK clock from the Host and uses this to sample data on the MOSI I/O pin. The phase and polarity must be configured according to the values used by the Host.

/* Required header files for example */
#include "/mcc_generated_files/spi/spi0.h"
typedef struct SPIdatas {
    uint8_t buffer[10];
    uint32_t count;
} SPIdata;

SPIdata SPI;

volatile uint8_t SPIFlag;

void SPITransferComplete(void) {
    IO_INTERRUPT_SetHigh();
    SPIFlag = 1;
    IO_INTERRUPT_SetLow();
}

int main(void) {
    SYSTEM_Initialize();
    SPI_Open(0); // Initialize SPI with configuration 0
    SPI_RegisterTransferDoneCallback(SPITransferComplete);
    SPI_BufferRead(SPI.buffer, 1); // set up transfer for new SPI block
    //   CPUINT_Initialize();
    ENABLE_INTERRUPTS();
    while (1) {
        if (SPIFlag == 1) {
            //      SPI.done = 1; // Deal with newly read SPI data
            IO_INTERRUPT_SetHigh();
            SPI_BufferRead(SPI.buffer, 1); // set up transfer for new SPI block
            SPIFlag = 0; // Deal with newly read SPI data
            SPI.count++;
            IO_INTERRUPT_SetLow();
        } else {
        }
    }
}

4.13.1.6 Class Documentation

4.13.1.6.1 spi_configuration_t Struct Reference

#include <spi_interrupt_types.h>

4.13.1.6.2 spi_descriptor_t Struct Reference

#include <spi_interrupt_types.h>

Member Data Documentation

The documentation for this struct was generated from the following file:

source/

spi_interrupt_types.h

buffer

uint8_t* buffer

bufferLength

size_t bufferLength

status

spi_status_t status

transferType

spi_transfer_type_t transferType

4.13.1.6.3 SPI_INTERFACE Struct Reference

SPI driver interface object.

Detailed Description

SPI driver interface object.

SPI Driver prototypes struct.

#include <spi_interface_interrupt.h>

Public Attributes

Member Data Documentation

The documentation for this struct was generated from the following files:

source/

spi_interface_interrupt.h

source/

spi_interface_polling.h

BufferExchange

void(* BufferExchange

BufferRead

void(* BufferRead

BufferWrite

void(* BufferWrite

ByteExchange

uint8_t(* ByteExchange

ByteRead

uint8_t(* ByteRead

ByteWrite

void(* ByteWrite

Close

void(* Close

Deinitialize

void(* Deinitialize

Initialize

void(* Initialize

IsRxReady

bool(* IsRxReady

IsTxReady

bool(* IsTxReady

Open

bool(* Open

RxCompleteCallbackRegister

void(* RxCompleteCallbackRegister

TxCompleteCallbackRegister

void(* TxCompleteCallbackRegister

4.13.1.7 File Documentation

4.13.1.7.1 source/spi0_interrupt.c File Reference

#include "../spi0.h"
#include "../spi_interrupt_types.h"

Functions

  • void SPI0_ISR (void)

  • void SPI0_Initialize (void)

    Initializes the SPI0 module.

  • void SPI0_Deinitialize (void)

    Deinitializes the SPI0 module.

  • bool SPI0_Open (uint8_t spiConfigIndex)

    Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.

  • void SPI0_Close (void)

    Closes the active configuration of the SPI0 module.

  • void SPI0_RxCompleteCallbackRegister (void(*CallbackHandler)(void))

    Sets the callback function to be executed at the completion of data transfer in Interrupt mode.

  • uint8_t SPI0_ByteExchange (uint8_t byteData)

    Exchanges one byte using the SPI protocol. This function is blocking.

  • void SPI0_BufferExchange (void *bufferData, size_t bufferSize)

    Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferWrite (void *bufferData, size_t bufferSize)

    Writes a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferRead (void *bufferData, size_t bufferSize)

    Reads a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol. This function is blocking in Polling mode.

  • uint8_t SPI0_ByteRead (void)

    Receives one byte using SPI protocol. This function is blocking.

  • bool SPI0_IsTxReady (void)

    Checks if the SPI0 module is ready to write data.

  • bool SPI0_IsRxReady (void)

    Checks if the SPI0 module is ready to read data.

  • ISR (SPI0_INT_vect)

Function Documentation

ISR()

ISR (SPI0_INT_vect )

SPI0_ISR()

void SPI0_ISR (void )

Variable Documentation

spi0_configuration

const spi_configuration_t spi0_configuration[][static]

Initial value:

= 
{
    { 0x33, 0x80 }
}
spi0_descriptor

spi_descriptor_t spi0_descriptor[static]

Initial value:

= 
{
    .buffer = NULL,
    .bufferLength = 0,
    .status = SPI_RESET
}
SPI0_RxCompleteCallback

void(* SPI0_RxCompleteCallback) (void)[static]

SPI0_s

const struct SPI_INTERFACE SPI0_s

Initial value:

= 
{
    .Initialize = SPI0_Initialize,
    .Deinitialize = SPI0_Deinitialize,
    .Open = SPI0_Open,
    .Close = SPI0_Close,
    .BufferExchange = SPI0_BufferExchange,
    .BufferWrite = SPI0_BufferWrite,
    .BufferRead = SPI0_BufferRead,  
    .ByteExchange = SPI0_ByteExchange,
    .ByteWrite = SPI0_ByteWrite,
    .ByteRead = SPI0_ByteRead,
    .IsTxReady = SPI0_IsTxReady,
    .IsRxReady = SPI0_IsRxReady,
    .RxCompleteCallbackRegister = SPI0_RxCompleteCallbackRegister,
    .TxCompleteCallbackRegister = NULL
}

4.13.1.7.2 source/spi0_interrupt.h File Reference

#include <stdbool.h>
#include "../system/utils/compiler.h"
#include "spi_interface.h"

Functions

  • void SPI0_Initialize (void)

    Initializes the SPI0 module.

  • void SPI0_Deinitialize (void)

    Deinitializes the SPI0 module.

  • bool SPI0_Open (uint8_t spiConfigIndex)

    Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.

  • void SPI0_Close (void)

    Closes the active configuration of the SPI0 module.

  • uint8_t SPI0_ByteExchange (uint8_t byteData)

    Exchanges one byte using the SPI protocol. This function is blocking.

  • void SPI0_BufferExchange (void *bufferData, size_t bufferSize)

    Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferWrite (void *bufferData, size_t bufferSize)

    Writes a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferRead (void *bufferData, size_t bufferSize)

    Reads a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol. This function is blocking in Polling mode.

  • uint8_t SPI0_ByteRead (void)

    Receives one byte using SPI protocol. This function is blocking.

  • bool SPI0_IsRxReady (void)

    Checks if the SPI0 module is ready to read data.

  • bool SPI0_IsTxReady (void)

    Checks if the SPI0 module is ready to write data.

  • void SPI0_RxCompleteCallbackRegister (void(*callbackHandler)(void))

    Sets the callback function to be executed at the completion of data transfer in Interrupt mode.

Variable Documentation

SPI0_s

const struct SPI_INTERFACE SPI0_s

4.13.1.7.3 source/spi0_polling.c File Reference

#include "../spi0.h"
#include "../spi_polling_types.h"

Functions

  • void SPI0_Initialize (void)

    Initializes the SPI0 module.

  • void SPI0_Deinitialize (void)

    Deinitializes the SPI0 module.

  • bool SPI0_Open (uint8_t spiConfigIndex)

    Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.

  • void SPI0_Close (void)

    Closes the active configuration of the SPI0 module.

  • uint8_t SPI0_ByteExchange (uint8_t byteData)

    Exchanges one byte using the SPI protocol. This function is blocking.

  • bool SPI0_IsTxReady (void)

    Checks if the SPI0 module is ready to write data.

  • bool SPI0_IsRxReady (void)

    Checks if the SPI0 module is ready to read data.

  • void SPI0_BufferExchange (void *bufferData, size_t bufferSize)

    Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferWrite (void *bufferData, size_t bufferSize)

    Writes a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferRead (void *bufferData, size_t bufferSize)

    Reads a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol. This function is blocking in Polling mode.

  • uint8_t SPI0_ByteRead (void)

    Receives one byte using SPI protocol. This function is blocking.

Variable Documentation

spi0_configuration

const spi_configuration_t spi0_configuration[][static]

Initial value:

=
{
    { 0x33, 0xC0 }
}

4.13.1.7.4 source/spi0_polling.h File Reference

#include <stdbool.h>
#include "../system/utils/compiler.h"
#include "spi_interface.h"

Functions

  • void SPI0_Initialize (void)

    Initializes the SPI0 module.

  • void SPI0_Deinitialize (void)

    Deinitializes the SPI0 module.

  • bool SPI0_Open (uint8_t spiConfigIndex)

    Enables the SPI0 module with the configurations passed as the parameter. See spi0_configuration_name_t for the available configurations.

  • void SPI0_Close (void)

    Closes the active configuration of the SPI0 module.

  • uint8_t SPI0_ByteExchange (uint8_t byteData)

    Exchanges one byte using the SPI protocol. This function is blocking.

  • void SPI0_BufferExchange (void *bufferData, size_t bufferSize)

    Exchanges the buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferWrite (void *bufferData, size_t bufferSize)

    Writes a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_BufferRead (void *bufferData, size_t bufferSize)

    Reads a buffer using the SPI protocol. This function is blocking in Polling mode.

  • void SPI0_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol. This function is blocking in Polling mode.

  • uint8_t SPI0_ByteRead (void)

    Receives one byte using SPI protocol. This function is blocking.

  • bool SPI0_IsRxReady (void)

    Checks if the SPI0 module is ready to read data.

  • bool SPI0_IsTxReady (void)

    Checks if the SPI0 module is ready to write data.

Enumeration Type Documentation

SPI0_configuration_name_t

enum SPI0_configuration_name_t

SPI0_DEFAULT
SPI0_DEFAULT

Variable Documentation

SPI0_s

const struct SPI_INTERFACE SPI0_s

4.13.1.7.5 source/spi_interface_interrupt.h File Reference

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

Data structures

4.13.1.7.6 source/spi_interface_polling.h File Reference

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

Data structures

4.13.1.7.8 source/spi_polling_types.h File Reference

Detailed Description

SPI Type Definitions Header File