2.26 SPI Host Driver

2.26.1 Introduction

Overview

The Serial Peripheral Interface (SPI) module is a synchronous serial interface useful for communicating with other peripheral or microcontroller devices. These peripheral devices can be serial EEPROMs, Shift registers, display drivers, Analog-to-Digital Converters and so on.

Features

  • Allows to setup multiple Host Configurations to interact with multiple clients having different configurations. SPIx_Open and SPIx_Close are the APIs to be used for selecting/switching between different Host configurations at run time.
  • Requested Speed , SPI Mode and Data Input Sample Point are configurable items for each Host Configuration table entry.
  • User can provide Custom Name for each Host Configuration table entry.
  • Calculated speed depends on the Bus Clock source which can be configured as part the the selected PLIB.
  • Supports Enhanced Buffer mode with communication width of 8 bit only.
  • Currently, only polling mode is supported.

2.26.2 Module Documentation

2.26.2.1 SPI Host Driver

Serial Peripheral Interface (SPI) HOST Driver using dsPIC MCUs.

2.26.2.1.1 Module description

Serial Peripheral Interface (SPI) HOST Driver using dsPIC MCUs.

2.26.2.1.1.1 Data structures
2.26.2.1.1.2 Enumerations
  • enum SPI1_HOST_USER_CONFIG { HOST_CONFIG }

    Enum list is the Custom names for the SPI1 HOST multiple configuration, configured by user in MCC Melody user interface. These Host Configurations can be used to switch at runtime to interact with multiple clients.

2.26.2.1.1.3 Functions
  • void SPI1_Initialize (void)

    Initializes SPI1 module, using the given initialization data. This function must be called before any other SPI1 function is called.

  • void SPI1_Deinitialize (void)

    Deinitializes the SPI1 to POR values.

  • void SPI1_Close (void)

    Disables the SPI1 module.

  • bool SPI1_Open (uint8_t spiConfigIndex)

    Configures SPI1 module with user defined unique configuration.

  • uint8_t SPI1_ByteExchange (uint8_t byteData)

    Exchanges one byte of data. This is a blocking function.

  • void SPI1_BufferExchange (void *bufferData, size_t bufferSize)

    This function is used to transmit data from the buffer pointer and updates the received data in the same buffer location. This is a blocking function.

  • void SPI1_BufferWrite (void *bufferData, size_t bufferSize)

    Writes data from a buffer. This is a blocking routine.

  • void SPI1_BufferRead (void *bufferData, size_t bufferSize)

    Reads data from a buffer. This is a blocking routine.

  • void SPI1_ByteWrite (uint8_t byteData)

    Writes one byte of data.

  • uint8_t SPI1_ByteRead (void)

    Reads one byte of data from SPI1.

  • bool SPI1_IsRxReady (void)

    Returns a boolean value if data is available to read.

  • bool SPI1_IsTxReady (void)

    Returns a boolean value if data can be written.

2.26.2.1.2 Function Documentation

2.26.2.1.2.1 SPI1_BufferExchange()

void SPI1_BufferExchange (void * bufferData, size_t bufferSize)

This function is used to transmit data from the buffer pointer and updates the received data in the same buffer location. This is a blocking function.

Precondition:

The SPI1_IsTxReady function must be called before calling this function.

Parameters:
in,out bufferData

- Address of data buffer to be excanged.

in bufferSize

- Number of bytes in a buffer.

Returns:

none

2.26.2.1.2.2 SPI1_BufferRead()

void SPI1_BufferRead (void * bufferData, size_t bufferSize)

Reads data from a buffer. This is a blocking routine.

Precondition:

The SPI1_IsRxReady function must be called before calling this function.

Parameters:
out bufferData

- Address of data buffer to be read.

in bufferSize

- Number of bytes in a buffer.

Returns:

none

2.26.2.1.2.3 SPI1_BufferWrite()

void SPI1_BufferWrite (void * bufferData, size_t bufferSize)

Writes data from a buffer. This is a blocking routine.

Precondition:

The SPI1_IsTxReady function must be called before calling this function.

Parameters:
in bufferData

- Address of data buffer to be written.

in bufferSize

- Number of bytes in a buffer.

Returns:

none

2.26.2.1.2.4 SPI1_ByteExchange()

uint8_t SPI1_ByteExchange (uint8_t byteData)

Exchanges one byte of data. This is a blocking function.

Precondition:

The SPI1_IsTxReady function must be called before calling this function.

Parameters:
in data-

Data byte to be exchanged.

Returns:

none

2.26.2.1.2.5 SPI1_ByteRead()

uint8_t SPI1_ByteRead (void )

Reads one byte of data from SPI1.

Precondition:

The SPI1_IsRxReady function must be called before calling this function. Also note that byte read must be after byte write sequence.

Parameters:
none
Returns:

Data read from SPI1.

2.26.2.1.2.6 SPI1_ByteWrite()

void SPI1_ByteWrite (uint8_t byteData)

Writes one byte of data.

Precondition:

The SPI1_IsTxReady function must be called before calling this function. Also note that every byte write has to be followed by a byte read.

Parameters:
in byteData

- Data to be written.

Returns:

none

2.26.2.1.2.7 SPI1_Close()

void SPI1_Close (void )

Disables the SPI1 module.

Precondition:

The SPI1_Open function must be called before calling this function.

Parameters:
none
Returns:

none

2.26.2.1.2.8 SPI1_Deinitialize()

void SPI1_Deinitialize (void )

Deinitializes the SPI1 to POR values.

Parameters:
none
Returns:

none

2.26.2.1.2.9 SPI1_Initialize()

void SPI1_Initialize (void )

Initializes SPI1 module, using the given initialization data. This function must be called before any other SPI1 function is called.

Parameters:
none
Returns:

none

2.26.2.1.2.10 SPI1_IsRxReady()

bool SPI1_IsRxReady (void )

Returns a boolean value if data is available to read.

Precondition:

The SPI1_Open function must be called before calling this function.

Parameters:
none
Returns:

true - Data available to read

false - Data not available to read

2.26.2.1.2.11 SPI1_IsTxReady()

bool SPI1_IsTxReady (void )

Returns a boolean value if data can be written.

Precondition:

The SPI1_Open function must be called before calling this function.

Parameters:
none
Returns:

true - Data can be written

false - Data buffer is full

2.26.2.1.2.12 SPI1_Open()

bool SPI1_Open (uint8_t spiConfigIndex)

Configures SPI1 module with user defined unique configuration.

Parameters:
in spiConfigIndex

- SPI1_USER_CONFIG Enum value

Returns:

bool true - SPI1 is configured successfully.

bool false - SPI1 is not configured successfully.

2.26.2.1.3 Enumeration Type Documentation

2.26.2.1.3.1 SPI1_HOST_USER_CONFIG

enum SPI1_HOST_USER_CONFIG

Enum list is the Custom names for the SPI1 HOST multiple configuration, configured by user in MCC Melody user interface. These Host Configurations can be used to switch at runtime to interact with multiple clients.

Note:

The enum list in the Help document might be just a reference to illustrate multiple configuration. Generated enum list is based on the configuration done by user in the MCC Melody user interface

HOST_CONFIG

Custom name for configuration setting: SPI Mode : Mode 1, Sampled at : Middle, Clock Frequency : 125 kHz

2.26.2.1.4 Variable Documentation

2.26.2.1.4.1 SPI1_Host

const struct SPI_HOST_INTERFACE SPI1_Host

Structure object of type SPI_HOST_INTERFACE assigned with name displayed in the Melody Driver User interface.

A structure pointer can be used to achieve portability across the SPI HOST having same interface structure.

2.26.3 Class Documentation

2.26.3.1 SPI_HOST_INTERFACE Struct Reference

Structure containing the function pointers of SPI HOST driver.

2.26.3.1.1 Detailed Description

Structure containing the function pointers of SPI HOST driver.

#include <spi_host_interface.h>

2.26.3.1.1.1 Public Attributes

2.26.3.1.2 Member Data Documentation

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

source/

spi_host_interface.h

2.26.3.1.2.1 BufferExchange

void(* BufferExchange) (void *bufferData, size_t bufferSize)

Pointer to SPIx_BufferExchange e.g. SPI1_BufferExchange.

2.26.3.1.2.2 BufferRead

void(* BufferRead) (void *bufferData, size_t bufferSize)

Pointer to SPIx_BufferRead e.g. SPI1_BufferRead.

2.26.3.1.2.3 BufferWrite

void(* BufferWrite) (void *bufferData, size_t bufferSize)

Pointer to SPIx_BufferWrite e.g. SPI1_BufferWrite.

2.26.3.1.2.4 ByteExchange

uint8_t(* ByteExchange) (uint8_t byteData)

Pointer to SPIx_ByteExchange e.g. SPI1_ByteExchange.

2.26.3.1.2.5 ByteRead

uint8_t(* ByteRead) (void)

Pointer to SPIx_ByteRead e.g. SPI1_ByteRead.

2.26.3.1.2.6 ByteWrite

void(* ByteWrite) (uint8_t byteData)

Pointer to SPIx_ByteWrite e.g. SPI1_ByteWrite.

2.26.3.1.2.7 Close

void(* Close) (void)

Pointer to SPIx_Close e.g. SPI1_Close.

2.26.3.1.2.8 Deinitialize

void(* Deinitialize) (void)

Pointer to SPIx_Deinitialize e.g. SPI1_Deinitialize.

2.26.3.1.2.9 Initialize

void(* Initialize) (void)

Pointer to SPIx_Initialize e.g. SPI1_Initialize.

2.26.3.1.2.10 IsRxReady

bool(* IsRxReady) (void)

Pointer to SPIx_IsRxReady e.g. SPI1_IsRxReady.

2.26.3.1.2.11 IsTxReady

bool(* IsTxReady) (void)

Pointer to SPIx_IsTxReady e.g. SPI1_IsTxReady.

2.26.3.1.2.12 Open

bool(* Open) (uint8_t spiConfigIndex)

Pointer to SPIx_Open e.g. SPI1_Open.

2.26.4 File Documentation

2.26.4.1 source/spi1.h File Reference

This is the generated driver header file for the SPI1 driver.

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

2.26.4.1.1 Functions

  • void SPI1_Initialize (void)

    Initializes SPI1 module, using the given initialization data. This function must be called before any other SPI1 function is called.

  • void SPI1_Deinitialize (void)

    Deinitializes the SPI1 to POR values.

  • void SPI1_Close (void)

    Disables the SPI1 module.

  • bool SPI1_Open (uint8_t spiConfigIndex)

    Configures SPI1 module with user defined unique configuration.

  • uint8_t SPI1_ByteExchange (uint8_t byteData)

    Exchanges one byte of data. This is a blocking function.

  • void SPI1_BufferExchange (void *bufferData, size_t bufferSize)

    This function is used to transmit data from the buffer pointer and updates the received data in the same buffer location. This is a blocking function.

  • void SPI1_BufferWrite (void *bufferData, size_t bufferSize)

    Writes data from a buffer. This is a blocking routine.

  • void SPI1_BufferRead (void *bufferData, size_t bufferSize)

    Reads data from a buffer. This is a blocking routine.

  • void SPI1_ByteWrite (uint8_t byteData)

    Writes one byte of data.

  • uint8_t SPI1_ByteRead (void)

    Reads one byte of data from SPI1.

  • bool SPI1_IsRxReady (void)

    Returns a boolean value if data is available to read.

  • bool SPI1_IsTxReady (void)

    Returns a boolean value if data can be written.

2.26.4.1.2 Enumerations

  • enum SPI1_HOST_USER_CONFIG { HOST_CONFIG }

    Enum list is the Custom names for the SPI1 HOST multiple configuration, configured by user in MCC Melody user interface. These Host Configurations can be used to switch at runtime to interact with multiple clients.

2.26.4.1.4 Detailed Description

This is the generated driver header file for the SPI1 driver.

SPI1 Generated Driver Header File

2.26.4.2 source/spi_host_interface.h File Reference

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

2.26.4.2.1 Data structures

2.26.4.2.2 Detailed Description

SPI Generated Driver Interface Header File