4.22.1 SPI - Serial Peripheral Interface

4.22.1.1 Introduction

The Serial Peripheral Interface (SPI) module is a synchronous serial data communication bus that operates in Full Duplex mode.

4.22.1.2 Supported Device Families

PIC18F-Q20 PIC18F-Q40 PIC18F-Q41
PIC18F-K83 PIC18F-Q71 PIC18F-Q24
PIC18F-K42 PIC18F-Q43 PIC18F-Q83
PIC18F-Q84

4.22.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.22.1.4 How to use the SPI Driver

4.22.1.5 Module Documentation

4.22.1.5.1 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 5.0.0.
Enumerations
  • enum spi1_configuration_name_t { SPI1_DEFAULT }

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

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.

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_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

Enumeration Type Documentation

spi1_configuration_name_t

enum enum spi1_configuration_name_t

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

SPI1_DEFAULT

4.22.1.5.2 SPI2

This header file provides API prototypes for the SPI2 driver.

Module description

This header file provides API prototypes for the SPI2 driver.

Version: SPI2 Driver Version 5.0.0.
Enumerations
  • enum spi2_configuration_name_t { SPI2_DEFAULT }

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

Functions
  • void SPI2_Initialize (void)

    Initializes the SPI2 module.

  • void SPI2_Deinitialize (void)

    Deinitializes the SPI2 module.

  • bool SPI2_Open (uint8_t spiConfigIndex)

    Enables the SPI2 module with the configurations passed as the parameter. See spi2_config_names_t for the available configurations.

  • void SPI2_Close (void)

    Closes the active configuration of the SPI2 module.

  • void SPI2_BufferExchange (void *bufferData, size_t bufferSize)

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

  • void SPI2_BufferWrite (void *bufferData, size_t bufferSize)

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

  • void SPI2_BufferRead (void *bufferData, size_t bufferSize)

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

  • uint8_t SPI2_ByteExchange (uint8_t byteData)

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

  • void SPI2_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI2_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI2_IsRxReady (void)

    Checks if the SPI2 module is ready to read data.

  • bool SPI2_IsTxReady (void)

    Checks if the SPI2 is ready to write data.

  • void SPI2_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))

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

  • void SPI2_TxCompleteCallbackRegister (void(*txCompleteCallbackHandler)(void))

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

  • void SPI2_Receive_ISR (void)

    Interrupt service routine (ISR) with receive interrupt RXIF for the SPI2 module.

  • void SPI2_Transmit_ISR (void)

    ISR with transmit interrupt TXIF for the SPI2 module.

  • void SPI2_ISR (void)

    ISR for the SPI2 module.

Function Documentation

SPI2_BufferExchange()

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

SPI2_BufferRead()

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

SPI2_BufferWrite()

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

SPI2_ByteExchange()

uint8_t SPI2_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

SPI2_ByteRead()

uint8_t SPI2_ByteRead (void )

Receives one byte using the SPI protocol.

Parameters:
None.
Returns:

Received data byte.

SPI2_ByteWrite()

void SPI2_ByteWrite (uint8_t byteData)

Writes one byte using the SPI protocol.

Parameters:
byteData

Byte to be written

Returns:

None.

SPI2_Close()

void SPI2_Close (void )

Closes the active configuration of the SPI2 module.

Parameters:
None.
Returns:

None.

SPI2_Deinitialize()

void SPI2_Deinitialize (void )

Deinitializes the SPI2 module.

Parameters:
None.
Returns:

None.

SPI2_Initialize()

void SPI2_Initialize (void )

Initializes the SPI2 module.

Parameters:
None.
Returns:

None.

SPI2_ISR()

void SPI2_ISR (void )

ISR for the SPI2 module.

Parameters:
None.
Returns:

None.

SPI2_IsRxReady()

bool SPI2_IsRxReady (void )

Checks if the SPI2 module is ready to read data.

Parameters:
None.
Return values:
True

SPI2 module is ready to read data

False

SPI2 module is not ready to read data

SPI2_IsTxReady()

bool SPI2_IsTxReady (void )

Checks if the SPI2 is ready to write data.

Parameters:
None.
Return values:
True

SPI2 module is ready to write data

False

SPI2 module is not ready to write data

SPI2_Open()

bool SPI2_Open (uint8_t spiConfigIndex)

Enables the SPI2 module with the configurations passed as the parameter. See spi2_config_names_t for the available configurations.

Parameters:
spiConfigIndex

Configuration index

Return values:
True

SPI2 module is enabled successfully with the chosen configuration

False

SPI2 module is already open with another configuration

SPI2_Receive_ISR()

void SPI2_Receive_ISR (void )

Interrupt service routine (ISR) with receive interrupt RXIF for the SPI2 module.

Parameters:
None.
Returns:

None.

SPI2_RxCompleteCallbackRegister()

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

SPI2_Transmit_ISR()

void SPI2_Transmit_ISR (void )

ISR with transmit interrupt TXIF for the SPI2 module.

Parameters:
None.
Returns:

None.

SPI2_TxCompleteCallbackRegister()

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

Enumeration Type Documentation

spi2_configuration_name_t

enum enum spi2_configuration_name_t

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

SPI2_DEFAULT

4.22.1.5.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 5.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

4.22.1.6 Class Documentation

4.22.1.6.1 spi_configuration_t Struct Reference

#include <spi_interrupt_types.h>

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

baud

uint8_t baud

clksel

uint8_t clksel

con0

uint8_t con0

con1

uint8_t con1

con2

uint8_t con2

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

spi_interrupt_types.h

bytesToReceive

size_t bytesToReceive

bytesToTransmit

size_t bytesToTransmit

receiveBuffer

uint8_t* receiveBuffer

status

spi_status_t status

transmitBuffer

uint8_t* transmitBuffer

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

Section: Included Files

#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.22.1.7 File Documentation

4.22.1.7.1 source/source-files/spi1_polling.c File Reference

#include <xc.h>
#include "../spi_interface.h"
#include "../spi1.h"
#include "../spi_polling_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.

Variable Documentation

spi1_configuration

const spi_configuration_t spi1_configuration[][static]

Initial value:

= {   
    { 0x2, 0x40, 0x0, 0x0, 0x0 }
}

4.22.1.7.2 source/source-files/spi1_polling.h File Reference

#include <stdint.h>
#include <stdbool.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.

Enumerations

  • enum spi1_configuration_name_t { SPI1_DEFAULT }

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

Variable Documentation

SPI1

const struct SPI_INTERFACE SPI1

4.22.1.7.3 source/source-files/spi2_interrupt.c File Reference

#include <xc.h>
#include "../spi_interface.h"
#include "../spi2.h"
#include "../spi_interrupt_types.h"

Functions

  • void SPI2_Initialize (void)

    Initializes the SPI2 module.

  • void SPI2_Deinitialize (void)

    Deinitializes the SPI2 module.

  • bool SPI2_Open (uint8_t spiConfigIndex)

    Enables the SPI2 module with the configurations passed as the parameter. See spi2_config_names_t for the available configurations.

  • void SPI2_Close (void)

    Closes the active configuration of the SPI2 module.

  • void SPI2_BufferExchange (void *bufferData, size_t bufferSize)

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

  • void SPI2_BufferWrite (void *bufferData, size_t bufferSize)

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

  • void SPI2_BufferRead (void *bufferData, size_t bufferSize)

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

  • uint8_t SPI2_ByteExchange (uint8_t byteData)

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

  • void SPI2_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI2_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI2_IsTxReady (void)

    Checks if the SPI2 is ready to write data.

  • bool SPI2_IsRxReady (void)

    Checks if the SPI2 module is ready to read data.

  • void SPI2_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))

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

  • void SPI2_TxCompleteCallbackRegister (void(*txCompleteCallbackHandler)(void))

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

  • void SPI2_Receive_ISR (void)

    Interrupt service routine (ISR) with receive interrupt RXIF for the SPI2 module.

  • void SPI2_Transmit_ISR (void)

    ISR with transmit interrupt TXIF for the SPI2 module.

  • void SPI2_ISR (void)

    ISR for the SPI2 module.

Variable Documentation

SPI2

const struct SPI_INTERFACE SPI2

Initial value:

= {
    .Initialize = SPI2_Initialize,
    .Deinitialize = SPI2_Deinitialize,
    .Open = SPI2_Open,
    .Close = SPI2_Close,
    .BufferExchange = SPI2_BufferExchange,
    .BufferWrite = SPI2_BufferWrite,
    .BufferRead = SPI2_BufferRead,  
    .ByteExchange = SPI2_ByteExchange,
    .ByteWrite = SPI2_ByteWrite,
    .ByteRead = SPI2_ByteRead,
    .IsTxReady = SPI2_IsTxReady,
    .IsRxReady = SPI2_IsRxReady,
    .RxCompleteCallbackRegister = SPI2_RxCompleteCallbackRegister,
    .TxCompleteCallbackRegister = SPI2_TxCompleteCallbackRegister
}
spi2_configuration

const spi_configuration_t spi2_configuration[][static]

Initial value:

= {   
    { 0x2, 0x40, 0x0, 0x0, 0x0 }
}
spi2_descriptor

spi_descriptor_t spi2_descriptor[static]

Initial value:

= {
    .transmitBuffer     = NULL,
    .receiveBuffer  = NULL,
    .bytesToTransmit    = 0,
    .bytesToReceive     = 0,
    .status         = SPI_RESET
}
SPI2_RxCompleteCallback

void(* SPI2_RxCompleteCallback) (void)[static]

SPI2_TxCompleteCallback

void(* SPI2_TxCompleteCallback) (void)[static]

4.22.1.7.4 source/source-files/spi2_interrupt.h File Reference

#include <stdint.h>
#include <stdbool.h>
#include "spi_interface.h"

Functions

  • void SPI2_Initialize (void)

    Initializes the SPI2 module.

  • void SPI2_Deinitialize (void)

    Deinitializes the SPI2 module.

  • bool SPI2_Open (uint8_t spiConfigIndex)

    Enables the SPI2 module with the configurations passed as the parameter. See spi2_config_names_t for the available configurations.

  • void SPI2_Close (void)

    Closes the active configuration of the SPI2 module.

  • void SPI2_BufferExchange (void *bufferData, size_t bufferSize)

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

  • void SPI2_BufferWrite (void *bufferData, size_t bufferSize)

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

  • void SPI2_BufferRead (void *bufferData, size_t bufferSize)

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

  • uint8_t SPI2_ByteExchange (uint8_t byteData)

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

  • void SPI2_ByteWrite (uint8_t byteData)

    Writes one byte using the SPI protocol.

  • uint8_t SPI2_ByteRead (void)

    Receives one byte using the SPI protocol.

  • bool SPI2_IsRxReady (void)

    Checks if the SPI2 module is ready to read data.

  • bool SPI2_IsTxReady (void)

    Checks if the SPI2 is ready to write data.

  • void SPI2_RxCompleteCallbackRegister (void(*rxCompleteCallbackHandler)(void))

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

  • void SPI2_TxCompleteCallbackRegister (void(*txCompleteCallbackHandler)(void))

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

  • void SPI2_Receive_ISR (void)

    Interrupt service routine (ISR) with receive interrupt RXIF for the SPI2 module.

  • void SPI2_Transmit_ISR (void)

    ISR with transmit interrupt TXIF for the SPI2 module.

  • void SPI2_ISR (void)

    ISR for the SPI2 module.

Enumerations

  • enum spi2_configuration_name_t { SPI2_DEFAULT }

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

Variable Documentation

SPI2

const struct SPI_INTERFACE SPI2

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

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

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

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

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

Detailed Description

SPI Type Definitions Header File

4.22.1.8 Module Documentation

4.22.1.8.1 SPI Use Cases

SPI Use Case Code Snippet Instructions

The use cases show example uses of the SPI PLIB Driver, within a MCC Melody project:

  • Add SPIx to the project

  • Configure:
    1. SPIx component as described in the example.

    2. Any other components or pins needed for the use case.

  • Generate the code

  • Add the code snippet(s) to the application code

  • Program the board

Use Case: Host Mode Transmit a Byte of Data via SPI

This example shows how to Setup Host Mode and to transmit a byte of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Host

    2. SPI Mode: Mode 0

    3. Input data sampled at: Middle

    4. Requested Clock Frequency (Hz): 500000

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Advanced Settings:
    1. Clock Source Selection: HFINTOSC

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"  
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

    uint8_t    data = 0xA5;
    while(1)
    {
    //Send Byte via SPI
    SPI1_Open(SPI1_DEFAULT);
    SPI1TCNT = 1;
    SPI1_ByteWrite(data);
    while(!PIR3bits.SPI1RXIF);
    SPI1_Close();
    }    
}

Use Case: Host Mode Receive a Byte of Data via SPI

This example shows how to Setup Host Mode and to Receive a byte of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Host

    2. SPI Mode: Select Mode 0

    3. Input data sampled at: Middle

    4. Requested Clock Frequency (Hz): 500000

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Advanced Settings:
    1. Clock Source Selection: HFINTOSC

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"  
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

    while(1)
    {
    uint8_t    receivedData;
    SPI1_Open(SPI1_DEFAULT);
    receivedData = SPI1_ByteRead();
    __delay_us(80);
    SPI1_Close();
    }    
}

Use Case: Host Mode Exchange a Byte of Data via SPI

This example shows how to Setup Host Mode and to Exchange a byte of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Host

    2. SPI Mode: Select Mode 0

    3. Input data sampled at: Middle

    4. Requested Clock Frequency (Hz): 500000

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Advanced Settings:
    1. Clock Source Selection: HFINTOSC

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:  

#include "mcc_generated_files/system/system.h"  
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

    while(1)
    {
                            //Exchange Byte via SPI
uint8_t    data = 0xA5;
uint8_t    receivedData;
    SPI1_Open(SPI1_DEFAULT);
    receivedData  = SPI1_ByteExchange(data);
    SPI1_Close();
    }    
}

Use Case: Host Mode Transmit a Block of Data via SPI

This example shows how to Setup Host Mode and to transmit a block of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Host

    2. SPI Mode: Select Mode 0

    3. Input data sampled at: Middle

    4. Requested Clock Frequency (Hz): 500000

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Advanced Settings:
    1. Clock Source Selection: HFINTOSC

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"  
uint32_t    bufferSize = 64;        
uint8_t dataBufPtr[] =
        {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
        0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
        0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
        0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
        0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
        0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40
    };
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

    while(1)
    {
    SPI1_Open(SPI1_DEFAULT);
    SPI1_BufferWrite(dataBufPtr, bufferSize);
    SPI1_Close();
    }    
}

Use Case: Host Mode Receive a Block of Data via SPI

This example shows how to Setup Host Mode and to Receive a block of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Host

    2. SPI Mode: Select Mode 0

    3. Input data sampled at: Middle

    4. Requested Clock Frequency (Hz): 500000

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Advanced Settings:
    1. Clock Source Selection: HFINTOSC

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"  
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

    while(1)
    {
#define bufferSize 64        
volatile uint8_t readDataBuffer[bufferSize];

    SPI1_Open(SPI1_DEFAULT);
    SS1_SetLow();
    SPI1_BufferRead(readDataBuffer, bufferSize);
    SS1_SetHigh();
    SPI1_Close();
    }    
}

Use Case: Host Mode Exchange a Block of Data via SPI

This example shows how to setup Host Mode and to Exchange a block of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Host

    2. SPI Mode: Select Mode 0

    3. Input data sampled at: Middle

    4. Requested Clock Frequency (Hz): 500000

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Advanced Settings:
    1. Clock Source Selection: HFINTOSC

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:  

#include "mcc_generated_files/system/system.h"  
uint32_t    bufferSize = 64;
volatile uint8_t dataBufPtr[] =
        {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
        0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
        0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
        0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
        0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
        0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40,
    };
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();
    
    SPI1_Open(SPI1_DEFAULT);
    SS1_SetLow();
    SPI1_BufferWrite(dataBufPtr, bufferSize);
    SS1_SetHigh();
    SPI1_Close();
    while(1)
    {
    }    
}

Use Case: Client Mode Transmit a Byte of Data via SPI

This example shows how to setup Client Mode to transmit a byte of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Client

    2. SPI Mode: Mode 0

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"  
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

uint8_t    data = 0xA5;
    while(1)
    {
    SPI1_Open(SPI1_DEFAULT);
    SPI1_ByteWrite(data);
    SPI1_Close();
    }    
}

Use Case: Client Mode Receive a Byte of Data via SPI

This example shows how to setup Client Mode to Receive a byte of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Client

    2. SPI Mode: Mode 0

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"  
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

    while(1)
    {
    uint8_t    receivedData;
    SPI1_Open(SPI1_DEFAULT);
    receivedData = SPI1_ByteRead();
    SPI1_Close();
    }    
}

Use Case: Client Mode Exchange a Byte of Data via SPI

This example shows how to setup Client Mode to Exchange a byte of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Client

    2. SPI Mode: Mode 0

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
#include "mcc_generated_files/system/system.h"  
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

    while(1)
    {
                            //Exchange Byte via SPI
uint8_t    data = 0xA5;
uint8_t    receivedData;
    SPI1_Open(SPI1_DEFAULT);
    receivedData  = SPI1_ByteExchange(data);
    SPI1_Close();
    }    
}

Use Case: Client Mode Transmit a Block of Data via SPI

This example shows how to setup Client Mode to Transmit a block of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Client

    2. SPI Mode: Mode 0

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:
uint32_t    bufferSize = 64;        
volatile uint8_t dataBufPtr[] =
        {
        0x40, 0x3F, 0x3E, 0x3D, 0x3C, 0x3B, 0x3A, 0x39,
        0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31,
        0x30, 0x2F, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
        0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21,
        0x20, 0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x19,
        0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11,
        0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09,
        0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01
    };
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();
    SPI1_Open(SPI1_DEFAULT);
    SPI1_BufferExchange(dataBufPtr, bufferSize);
    SPI1_Close();
    while(1)
    { 
    }    
}

Use Case: Client Mode Receive a Block of Data via SPI

This example shows how to Setup Client Mode and to Receive a block of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Client

    2. SPI Mode: Mode 0

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO.

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"  
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();

    while(1)
    {
        uint8_t    receivedData;
    SPI1_Open(SPI1_DEFAULT);
    receivedData = SPI1_ByteRead();
    SPI1_Close();
    }    
}

Use Case: Client Mode Exchange a Block of Data via SPI

This example shows how to setup Client Mode and to Exchange a block of data via the SPI peripheral.

Device Resources:
  1. Add SPIx

SPI Configuration:
  • Software Settings:
    1. Custome Name: SPI1

  • Hardware Settings:
    1. Mode: Client

    2. SPI Mode: Mode 0

  • Interrupt Settings:
    1. Enable SPI Interrupt: Disabled

  • Dependency Selection:
    1. SPI Dependency Selection: SPI1

System Configuration:
  • Pin Grid View:
    1. Select PINS for SCKx, SDIx and SDOx

  • PINS:
    1. Name pins SCKx, SDIx and SDOx as SCK, SDI and SDO..

After configuring the components as described above, click 'Generate' to generate code. Then add the following code snippets to your application:  

#include "mcc_generated_files/system/system.h"  
uint32_t    bufferSize = 64;        
volatile uint8_t dataBufPtr[] =
        {
        0x40, 0x3F, 0x3E, 0x3D, 0x3C, 0x3B, 0x3A, 0x39,
        0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31,
        0x30, 0x2F, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
        0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21,
        0x20, 0x1F, 0x1E, 0x1D, 0x1C, 0x1B, 0x1A, 0x19,
        0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11,
        0x10, 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09,
        0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01
    };
int main(void)
{
    SYSTEM_Initialize();
    SPI1_Close();
    SPI1_Open(SPI1_DEFAULT);
    SPI1_BufferExchange(dataBufPtr, bufferSize);
    SPI1_Close();
    while(1)
    { 
    }    
}