4.13.2 SPI with Control and Status Registers

4.13.2.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.2.2 Supported Device Families

ATmega32x

4.13.2.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.2.4 Module Documentation

4.13.2.4.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 v2.0.0.
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_config_names_t for the available configurations.

  • void SPI0_Close (void)

    Closes the active configuration of the SPI0 module.

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

  • uint8_t SPI0_ByteExchange (uint8_t byteData)

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

  • void SPI0_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI0_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI0_IsRxReady (void)

    Checks if the SPI0 module is ready to read data.

  • bool SPI0_IsTxReady (void)

    Checks if the SPI0 is ready to write data.

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

Byte to be written

Returns:

Received data byte

SPI0_ByteRead()

uint8_t SPI0_ByteRead (void )

Receives one byte using the SPI protocol.

Parameters:
None.
Returns:

Received data byte

SPI0_ByteWrite()

void SPI0_ByteWrite (uint8_t byteData)

Writes one byte using the SPI protocol.

Parameters:
byteData

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 is ready to read data

False

SPI0 module is not ready to read data

SPI0_IsTxReady()

bool SPI0_IsTxReady (void )

Checks if the SPI0 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_config_names_t for the available configurations.

Parameters:
spiConfigIndex

Configuration index

Return values:
True

SPI0 module is enabled successfully with the chosen configuration

False

SPI0 module is already open with another configuration

4.13.2.4.2 SPI1

This header file provides API prototypes for the SPI1 driver.

Module description

This header file provides API prototypes for the SPI1 driver.

Version: SPI1 Driver Version v2.0.0.
Functions
  • void SPI1_Initialize (void)

    Initializes the SPI1 module.

  • void SPI1_Deinitialize (void)

    Deinitializes the SPI1 module.

  • bool SPI1_Open (uint8_t spiConfigIndex)

    Enables the SPI1 module with the configurations passed as the parameter. See spi1_config_names_t for the available configurations.

  • void SPI1_Close (void)

    Closes the active configuration of the SPI1 module.

  • void SPI1_BufferExchange (void *bufferData, size_t bufferSize)

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

  • void SPI1_BufferWrite (void *bufferData, size_t bufferSize)

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

  • void SPI1_BufferRead (void *bufferData, size_t bufferSize)

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

  • uint8_t SPI1_ByteExchange (uint8_t byteData)

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

  • void SPI1_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI1_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI1_IsRxReady (void)

    Checks if the SPI1 module is ready to read data.

  • bool SPI1_IsTxReady (void)

    Checks if the SPI1 is ready to write data.

  • void SPI1_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))

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

  • void SPI1_TxCompleteCallbackRegister (void(*txCompleteCallbackHandler)(void))

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

  • void SPI1_Receive_ISR (void)

    Interrupt Service Routine (ISR) with receive interrupt RXIF for the SPI1 module.

  • void SPI1_Transmit_ISR (void)

    ISR with transmit interrupt TXIF for the SPI1 module.

  • void SPI1_ISR (void)

    ISR for the SPI1 module.

Function Documentation

SPI1_BufferExchange()

void SPI1_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.

SPI1_BufferRead()

void SPI1_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.

SPI1_BufferWrite()

void SPI1_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.

SPI1_ByteExchange()

uint8_t SPI1_ByteExchange (uint8_t byteData)

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

Parameters:
byteData

Byte to be written

Returns:

Received data byte

SPI1_ByteRead()

uint8_t SPI1_ByteRead (void )

Receives one byte using the SPI protocol.

Parameters:
None.
Returns:

Received data byte

SPI1_ByteWrite()

void SPI1_ByteWrite (uint8_t byteData)

Writes one byte using the SPI protocol.

Parameters:
byteData

Byte to be written

Returns:

None.

SPI1_Close()

void SPI1_Close (void )

Closes the active configuration of the SPI1 module.

Parameters:
None.
Returns:

None.

SPI1_Deinitialize()

void SPI1_Deinitialize (void )

Deinitializes the SPI1 module.

Parameters:
None.
Returns:

None.

SPI1_Initialize()

void SPI1_Initialize (void )

Initializes the SPI1 module.

Parameters:
None.
Returns:

None.

SPI1_ISR()

void SPI1_ISR (void )

ISR for the SPI1 module.

Parameters:
None.
Returns:

None.

SPI1_IsRxReady()

bool SPI1_IsRxReady (void )

Checks if the SPI1 module is ready to read data.

Parameters:
None.
Return values:
True

SPI1 module is ready to read data

False

SPI1 module is not ready to read data

SPI1_IsTxReady()

bool SPI1_IsTxReady (void )

Checks if the SPI1 is ready to write data.

Parameters:
None.
Return values:
True

SPI1 module is ready to write data

False

SPI1 module is not ready to write data

SPI1_Open()

bool SPI1_Open (uint8_t spiConfigIndex)

Enables the SPI1 module with the configurations passed as the parameter. See spi1_config_names_t for the available configurations.

Parameters:
spiConfigIndex

Configuration index

Return values:
True

SPI1 module is enabled successfully with the chosen configuration

False

SPI1 module is already open with another configuration

SPI1_Receive_ISR()

void SPI1_Receive_ISR (void )

Interrupt Service Routine (ISR) with receive interrupt RXIF for the SPI1 module.

Parameters:
None.
Returns:

None.

SPI1_RxCompleteCallbackRegister()

void SPI1_RxCompleteCallbackRegister (void(*)(void) rxCompleteCallbackHandler)

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

Parameters:
Pointer

to the function to be executed

Returns:

None.

SPI1_Transmit_ISR()

void SPI1_Transmit_ISR (void )

ISR with transmit interrupt TXIF for the SPI1 module.

Parameters:
None.
Returns:

None.

SPI1_TxCompleteCallbackRegister()

void SPI1_TxCompleteCallbackRegister (void(*)(void) txCompleteCallbackHandler)

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

Parameters:
Pointer

to the function to be executed

Returns:

None.

4.13.2.4.3 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 v2.0.0.

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.2.5 Class Documentation

4.13.2.5.1 spi_configuration_t Struct Reference

#include <spi_interrupt_types.h>

Public Attributes

Member Data Documentation

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

source/source-files/

spi_interrupt_types.h

source/source-files/

spi_polling_types.h

spcr

uint8_t spcr

spsr

uint8_t spsr

4.13.2.5.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/source-files/

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.2.5.3 SPI_INTERFACE Struct Reference

Defines an object for SPI_INTERFACE functions.

Detailed Description

Defines an object for SPI_INTERFACE functions.

SPI Driver prototypes struct.

SPI driver interface object.

#include <spi_interface_interrupt.h>

Public Attributes

Member Data Documentation

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

source/source-files/

spi_interface_interrupt.h

source/source-files/

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.2.6 File Documentation

4.13.2.6.1 source/source-files/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_config_names_t for the available configurations.

  • void SPI0_Close (void)

    Closes the active configuration of the SPI0 module.

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

  • uint8_t SPI0_ByteExchange (uint8_t byteData)

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

  • void SPI0_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI0_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI0_IsRxReady (void)

    Checks if the SPI0 module is ready to read data.

  • bool SPI0_IsTxReady (void)

    Checks if the SPI0 is ready to write data.

Variable Documentation

spi0_configuration

const spi_configuration_t spi0_configuration[][static]

Initial value:

= {
    { 0x53, 0x0 }
}

4.13.2.6.2 source/source-files/spi0_polling.h File Reference

#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_config_names_t for the available configurations.

  • void SPI0_Close (void)

    Closes the active configuration of the SPI0 module.

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

  • uint8_t SPI0_ByteExchange (uint8_t byteData)

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

  • void SPI0_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI0_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI0_IsRxReady (void)

    Checks if the SPI0 module is ready to read data.

  • bool SPI0_IsTxReady (void)

    Checks if the SPI0 is ready to write data.

Enumeration Type Documentation

spi0_configuration_name_t

enum spi0_configuration_name_t

SPI0_DEFAULT

Variable Documentation

SPI0

const struct SPI_INTERFACE SPI0

4.13.2.6.3 source/source-files/spi1_interrupt.c File Reference

#include "../spi1.h"
#include "../spi_interrupt_types.h"

Functions

  • void SPI1_Initialize (void)

    Initializes the SPI1 module.

  • void SPI1_Deinitialize (void)

    Deinitializes the SPI1 module.

  • bool SPI1_Open (uint8_t spiConfigIndex)

    Enables the SPI1 module with the configurations passed as the parameter. See spi1_config_names_t for the available configurations.

  • void SPI1_Close (void)

    Closes the active configuration of the SPI1 module.

  • void SPI1_BufferExchange (void *bufferData, size_t bufferSize)

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

  • void SPI1_BufferWrite (void *bufferData, size_t bufferSize)

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

  • void SPI1_BufferRead (void *bufferData, size_t bufferSize)

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

  • uint8_t SPI1_ByteExchange (uint8_t byteData)

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

  • void SPI1_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI1_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI1_IsTxReady (void)

    Checks if the SPI1 is ready to write data.

  • bool SPI1_IsRxReady (void)

    Checks if the SPI1 module is ready to read data.

  • void SPI1_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))

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

  • void SPI1_ISR (void)

    ISR for the SPI1 module.

  • ISR (SPI1_STC_vect)

Function Documentation

ISR()

ISR (SPI1_STC_vect )

Variable Documentation

SPI1

const struct SPI_INTERFACE SPI1

Initial value:

= {
    .Initialize = SPI1_Initialize,
    .Deinitialize = SPI1_Deinitialize,
    .Open = SPI1_Open,
    .Close = SPI1_Close,
    .BufferExchange = SPI1_BufferExchange,
    .BufferWrite = SPI1_BufferWrite,
    .BufferRead = SPI1_BufferRead,
    .ByteExchange = SPI1_ByteExchange,
    .ByteWrite = SPI1_ByteWrite,
    .ByteRead = SPI1_ByteRead,
    .IsTxReady = SPI1_IsTxReady,
    .IsRxReady = SPI1_IsRxReady,
    .RxCompleteCallbackRegister = SPI1_RxCompleteCallbackRegister,
    .TxCompleteCallbackRegister = NULL
}
spi1_configuration

const spi_configuration_t spi1_configuration[][static]

Initial value:

= {
    { 0x53, 0x0 } 
}
spi1_descriptor

spi_descriptor_t spi1_descriptor[static]

Initial value:

= {
    .buffer         = NULL,
    .bufferLength   = 0U,
    .status         = SPI_RESET
}
SPI1_RxCompleteCallback

void(* SPI1_RxCompleteCallback) (void)[static]

4.13.2.6.4 source/source-files/spi1_interrupt.h File Reference

#include "../system/utils/compiler.h"
#include "spi_interface.h"

Functions

  • void SPI1_Initialize (void)

    Initializes the SPI1 module.

  • void SPI1_Deinitialize (void)

    Deinitializes the SPI1 module.

  • bool SPI1_Open (uint8_t spiConfigIndex)

    Enables the SPI1 module with the configurations passed as the parameter. See spi1_config_names_t for the available configurations.

  • void SPI1_Close (void)

    Closes the active configuration of the SPI1 module.

  • void SPI1_BufferExchange (void *bufferData, size_t bufferSize)

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

  • void SPI1_BufferWrite (void *bufferData, size_t bufferSize)

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

  • void SPI1_BufferRead (void *bufferData, size_t bufferSize)

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

  • uint8_t SPI1_ByteExchange (uint8_t byteData)

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

  • void SPI1_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI1_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI1_IsRxReady (void)

    Checks if the SPI1 module is ready to read data.

  • bool SPI1_IsTxReady (void)

    Checks if the SPI1 is ready to write data.

  • void SPI1_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))

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

  • void SPI1_TxCompleteCallbackRegister (void(*txCompleteCallbackHandler)(void))

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

  • void SPI1_Receive_ISR (void)

    Interrupt Service Routine (ISR) with receive interrupt RXIF for the SPI1 module.

  • void SPI1_Transmit_ISR (void)

    ISR with transmit interrupt TXIF for the SPI1 module.

  • void SPI1_ISR (void)

    ISR for the SPI1 module.

Enumeration Type Documentation

spi1_configuration_name_t

enum spi1_configuration_name_t

SPI1_DEFAULT

Variable Documentation

SPI1

const struct SPI_INTERFACE SPI1

4.13.2.6.5 source/source-files/spi_interface_interrupt.h File Reference

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

4.13.2.6.6 source/source-files/spi_interface_polling.h File Reference

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

4.13.2.6.8 source/source-files/spi_polling_types.h File Reference

Detailed Description

SPI Type Definitions Header File